xref: /freebsd-src/sys/fs/udf/udf_vfsops.c (revision 78c51db3c4927db2437ec616b33ba1faf73f08ee)
151a7b740SScott Long /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d63027b6SPedro F. Giffuni  *
451a7b740SScott Long  * Copyright (c) 2001, 2002 Scott Long <scottl@freebsd.org>
551a7b740SScott Long  * All rights reserved.
651a7b740SScott Long  *
751a7b740SScott Long  * Redistribution and use in source and binary forms, with or without
851a7b740SScott Long  * modification, are permitted provided that the following conditions
951a7b740SScott Long  * are met:
1051a7b740SScott Long  * 1. Redistributions of source code must retain the above copyright
1151a7b740SScott Long  *    notice, this list of conditions and the following disclaimer.
1251a7b740SScott Long  * 2. Redistributions in binary form must reproduce the above copyright
1351a7b740SScott Long  *    notice, this list of conditions and the following disclaimer in the
1451a7b740SScott Long  *    documentation and/or other materials provided with the distribution.
1551a7b740SScott Long  *
1651a7b740SScott Long  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1751a7b740SScott Long  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1851a7b740SScott Long  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1951a7b740SScott Long  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2051a7b740SScott Long  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2151a7b740SScott Long  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2251a7b740SScott Long  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2351a7b740SScott Long  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2451a7b740SScott Long  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2551a7b740SScott Long  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2651a7b740SScott Long  * SUCH DAMAGE.
2751a7b740SScott Long  */
2851a7b740SScott Long 
2951a7b740SScott Long /* udf_vfsops.c */
3051a7b740SScott Long /* Implement the VFS side of things */
3151a7b740SScott Long 
3251a7b740SScott Long /*
3351a7b740SScott Long  * Ok, here's how it goes.  The UDF specs are pretty clear on how each data
3451a7b740SScott Long  * structure is made up, but not very clear on how they relate to each other.
3551a7b740SScott Long  * Here is the skinny... This demostrates a filesystem with one file in the
3651a7b740SScott Long  * root directory.  Subdirectories are treated just as normal files, but they
3751a7b740SScott Long  * have File Id Descriptors of their children as their file data.  As for the
3851a7b740SScott Long  * Anchor Volume Descriptor Pointer, it can exist in two of the following three
3951a7b740SScott Long  * places: sector 256, sector n (the max sector of the disk), or sector
4051a7b740SScott Long  * n - 256.  It's a pretty good bet that one will exist at sector 256 though.
4151a7b740SScott Long  * One caveat is unclosed CD media.  For that, sector 256 cannot be written,
4251a7b740SScott Long  * so the Anchor Volume Descriptor Pointer can exist at sector 512 until the
4351a7b740SScott Long  * media is closed.
4451a7b740SScott Long  *
4551a7b740SScott Long  *  Sector:
4651a7b740SScott Long  *     256:
4751a7b740SScott Long  *       n: Anchor Volume Descriptor Pointer
4851a7b740SScott Long  * n - 256:	|
4951a7b740SScott Long  *		|
5051a7b740SScott Long  *		|-->Main Volume Descriptor Sequence
5151a7b740SScott Long  *			|	|
5251a7b740SScott Long  *			|	|
5351a7b740SScott Long  *			|	|-->Logical Volume Descriptor
5451a7b740SScott Long  *			|			  |
5551a7b740SScott Long  *			|-->Partition Descriptor  |
5651a7b740SScott Long  *				|		  |
5751a7b740SScott Long  *				|		  |
5851a7b740SScott Long  *				|-->Fileset Descriptor
5951a7b740SScott Long  *					|
6051a7b740SScott Long  *					|
6151a7b740SScott Long  *					|-->Root Dir File Entry
6251a7b740SScott Long  *						|
6351a7b740SScott Long  *						|
6451a7b740SScott Long  *						|-->File data:
6551a7b740SScott Long  *						    File Id Descriptor
6651a7b740SScott Long  *							|
6751a7b740SScott Long  *							|
6851a7b740SScott Long  *							|-->File Entry
6951a7b740SScott Long  *								|
7051a7b740SScott Long  *								|
7151a7b740SScott Long  *								|-->File data
7251a7b740SScott Long  */
7351a7b740SScott Long #include <sys/types.h>
7451a7b740SScott Long #include <sys/param.h>
7551a7b740SScott Long #include <sys/systm.h>
76fa860c78SMark Murray #include <sys/uio.h>
7751a7b740SScott Long #include <sys/bio.h>
7851a7b740SScott Long #include <sys/buf.h>
7951a7b740SScott Long #include <sys/conf.h>
8051a7b740SScott Long #include <sys/dirent.h>
81fa860c78SMark Murray #include <sys/fcntl.h>
826565282cSScott Long #include <sys/iconv.h>
83fa860c78SMark Murray #include <sys/kernel.h>
84fa860c78SMark Murray #include <sys/malloc.h>
85fa860c78SMark Murray #include <sys/mount.h>
86fa860c78SMark Murray #include <sys/namei.h>
87acd3428bSRobert Watson #include <sys/priv.h>
88fa860c78SMark Murray #include <sys/proc.h>
89fa860c78SMark Murray #include <sys/queue.h>
90fa860c78SMark Murray #include <sys/vnode.h>
9189ec2c3cSScott Long #include <sys/endian.h>
9251a7b740SScott Long 
93429c018aSPoul-Henning Kamp #include <geom/geom.h>
94429c018aSPoul-Henning Kamp #include <geom/geom_vfs.h>
95429c018aSPoul-Henning Kamp 
9651a7b740SScott Long #include <vm/uma.h>
9751a7b740SScott Long 
9851a7b740SScott Long #include <fs/udf/ecma167-udf.h>
9951a7b740SScott Long #include <fs/udf/osta.h>
1006565282cSScott Long #include <fs/udf/udf.h>
1016565282cSScott Long #include <fs/udf/udf_mount.h>
10251a7b740SScott Long 
1035bb84bc8SRobert Watson static MALLOC_DEFINE(M_UDFMOUNT, "udf_mount", "UDF mount structure");
1045bb84bc8SRobert Watson MALLOC_DEFINE(M_UDFFENTRY, "udf_fentry", "UDF file entry structure");
10551a7b740SScott Long 
1066565282cSScott Long struct iconv_functions *udf_iconv = NULL;
1076565282cSScott Long 
10851a7b740SScott Long /* Zones */
10951a7b740SScott Long uma_zone_t udf_zone_trans = NULL;
11051a7b740SScott Long uma_zone_t udf_zone_node = NULL;
1111703656aSScott Long uma_zone_t udf_zone_ds = NULL;
11251a7b740SScott Long 
1137652131bSPoul-Henning Kamp static vfs_init_t      udf_init;
1147652131bSPoul-Henning Kamp static vfs_uninit_t    udf_uninit;
1155e8c582aSPoul-Henning Kamp static vfs_mount_t     udf_mount;
1167652131bSPoul-Henning Kamp static vfs_root_t      udf_root;
1177652131bSPoul-Henning Kamp static vfs_statfs_t    udf_statfs;
1187652131bSPoul-Henning Kamp static vfs_unmount_t   udf_unmount;
1197652131bSPoul-Henning Kamp static vfs_fhtovp_t	udf_fhtovp;
1207652131bSPoul-Henning Kamp 
12151a7b740SScott Long static int udf_find_partmaps(struct udf_mnt *, struct logvol_desc *);
12251a7b740SScott Long 
12351a7b740SScott Long static struct vfsops udf_vfsops = {
1247652131bSPoul-Henning Kamp 	.vfs_fhtovp =		udf_fhtovp,
1257652131bSPoul-Henning Kamp 	.vfs_init =		udf_init,
1265e8c582aSPoul-Henning Kamp 	.vfs_mount =		udf_mount,
1277652131bSPoul-Henning Kamp 	.vfs_root =		udf_root,
1287652131bSPoul-Henning Kamp 	.vfs_statfs =		udf_statfs,
1297652131bSPoul-Henning Kamp 	.vfs_uninit =		udf_uninit,
1307652131bSPoul-Henning Kamp 	.vfs_unmount =		udf_unmount,
1317652131bSPoul-Henning Kamp 	.vfs_vget =		udf_vget,
13251a7b740SScott Long };
13351a7b740SScott Long VFS_SET(udf_vfsops, udf, VFCF_READONLY);
13451a7b740SScott Long 
1356565282cSScott Long MODULE_VERSION(udf, 1);
1366565282cSScott Long 
1370d7935fdSAttilio Rao static int udf_mountfs(struct vnode *, struct mount *);
13851a7b740SScott Long 
13951a7b740SScott Long static int
udf_init(struct vfsconf * foo)14051a7b740SScott Long udf_init(struct vfsconf *foo)
14151a7b740SScott Long {
14251a7b740SScott Long 
14351a7b740SScott Long 	udf_zone_trans = uma_zcreate("UDF translation buffer, zone", MAXNAMLEN *
14451a7b740SScott Long 	    sizeof(unicode_t), NULL, NULL, NULL, NULL, 0, 0);
14551a7b740SScott Long 
14651a7b740SScott Long 	udf_zone_node = uma_zcreate("UDF Node zone", sizeof(struct udf_node),
14751a7b740SScott Long 	    NULL, NULL, NULL, NULL, 0, 0);
14851a7b740SScott Long 
1491703656aSScott Long 	udf_zone_ds = uma_zcreate("UDF Dirstream zone",
1501703656aSScott Long 	    sizeof(struct udf_dirstream), NULL, NULL, NULL, NULL, 0, 0);
1511703656aSScott Long 
15251a7b740SScott Long 	return 0;
15351a7b740SScott Long }
15451a7b740SScott Long 
15551a7b740SScott Long static int
udf_uninit(struct vfsconf * foo)15651a7b740SScott Long udf_uninit(struct vfsconf *foo)
15751a7b740SScott Long {
15851a7b740SScott Long 
15951a7b740SScott Long 	if (udf_zone_trans != NULL) {
16051a7b740SScott Long 		uma_zdestroy(udf_zone_trans);
16151a7b740SScott Long 		udf_zone_trans = NULL;
16251a7b740SScott Long 	}
16351a7b740SScott Long 
16451a7b740SScott Long 	if (udf_zone_node != NULL) {
16551a7b740SScott Long 		uma_zdestroy(udf_zone_node);
16651a7b740SScott Long 		udf_zone_node = NULL;
16751a7b740SScott Long 	}
16851a7b740SScott Long 
1691703656aSScott Long 	if (udf_zone_ds != NULL) {
1701703656aSScott Long 		uma_zdestroy(udf_zone_ds);
1711703656aSScott Long 		udf_zone_ds = NULL;
1721703656aSScott Long 	}
1731703656aSScott Long 
17451a7b740SScott Long 	return (0);
17551a7b740SScott Long }
17651a7b740SScott Long 
17751a7b740SScott Long static int
udf_mount(struct mount * mp)178dfd233edSAttilio Rao udf_mount(struct mount *mp)
17951a7b740SScott Long {
18051a7b740SScott Long 	struct vnode *devvp;	/* vnode of the mount device */
181dfd233edSAttilio Rao 	struct thread *td;
182f7a3729cSKevin Lo 	struct udf_mnt *imp = NULL;
183c3210a83SMaxime Henrion 	struct vfsoptlist *opts;
1846565282cSScott Long 	char *fspec, *cs_disk, *cs_local;
1856565282cSScott Long 	int error, len, *udf_flags;
1865e8c582aSPoul-Henning Kamp 	struct nameidata nd, *ndp = &nd;
187c3210a83SMaxime Henrion 
188dfd233edSAttilio Rao 	td = curthread;
189c3210a83SMaxime Henrion 	opts = mp->mnt_optnew;
19051a7b740SScott Long 
191073833a4SCraig Rodrigues 	/*
192073833a4SCraig Rodrigues 	 * Unconditionally mount as read-only.
193073833a4SCraig Rodrigues 	 */
1945da56ddbSTor Egge 	MNT_ILOCK(mp);
195073833a4SCraig Rodrigues 	mp->mnt_flag |= MNT_RDONLY;
1965da56ddbSTor Egge 	MNT_IUNLOCK(mp);
19751a7b740SScott Long 
19851a7b740SScott Long 	/*
19951a7b740SScott Long 	 * No root filesystem support.  Probably not a big deal, since the
20051a7b740SScott Long 	 * bootloader doesn't understand UDF.
20151a7b740SScott Long 	 */
20251a7b740SScott Long 	if (mp->mnt_flag & MNT_ROOTFS)
20351a7b740SScott Long 		return (ENOTSUP);
20451a7b740SScott Long 
205c3210a83SMaxime Henrion 	fspec = NULL;
206c3210a83SMaxime Henrion 	error = vfs_getopt(opts, "from", (void **)&fspec, &len);
207c3210a83SMaxime Henrion 	if (!error && fspec[len - 1] != '\0')
208c3210a83SMaxime Henrion 		return (EINVAL);
20951a7b740SScott Long 
21051a7b740SScott Long 	if (mp->mnt_flag & MNT_UPDATE) {
21123badd10SCraig Rodrigues 		return (0);
21251a7b740SScott Long 	}
21351a7b740SScott Long 
21451a7b740SScott Long 	/* Check that the mount device exists */
215c3210a83SMaxime Henrion 	if (fspec == NULL)
216c3210a83SMaxime Henrion 		return (EINVAL);
2177e1d3eefSMateusz Guzik 	NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec);
21851a7b740SScott Long 	if ((error = namei(ndp)))
21951a7b740SScott Long 		return (error);
220bb92cd7bSMateusz Guzik 	NDFREE_PNBUF(ndp);
22151a7b740SScott Long 	devvp = ndp->ni_vp;
22251a7b740SScott Long 
2237ad2a82dSMateusz Guzik 	if (!vn_isdisk_error(devvp, &error)) {
22475d7ba93SSuleiman Souhlal 		vput(devvp);
22551a7b740SScott Long 		return (error);
22651a7b740SScott Long 	}
22751a7b740SScott Long 
22851a7b740SScott Long 	/* Check the access rights on the mount device */
22951a7b740SScott Long 	error = VOP_ACCESS(devvp, VREAD, td->td_ucred, td);
23051a7b740SScott Long 	if (error)
231acd3428bSRobert Watson 		error = priv_check(td, PRIV_VFS_MOUNT_PERM);
23251a7b740SScott Long 	if (error) {
23351a7b740SScott Long 		vput(devvp);
23451a7b740SScott Long 		return (error);
23551a7b740SScott Long 	}
23651a7b740SScott Long 
2370d7935fdSAttilio Rao 	if ((error = udf_mountfs(devvp, mp))) {
23851a7b740SScott Long 		vrele(devvp);
23951a7b740SScott Long 		return (error);
24051a7b740SScott Long 	}
24151a7b740SScott Long 
24251a7b740SScott Long 	imp = VFSTOUDFFS(mp);
2436565282cSScott Long 
2446565282cSScott Long 	udf_flags = NULL;
2456565282cSScott Long 	error = vfs_getopt(opts, "flags", (void **)&udf_flags, &len);
2466565282cSScott Long 	if (error || len != sizeof(int))
2476565282cSScott Long 		return (EINVAL);
2486565282cSScott Long 	imp->im_flags = *udf_flags;
2496565282cSScott Long 
2506565282cSScott Long 	if (imp->im_flags & UDFMNT_KICONV && udf_iconv) {
2516565282cSScott Long 		cs_disk = NULL;
2526565282cSScott Long 		error = vfs_getopt(opts, "cs_disk", (void **)&cs_disk, &len);
2536565282cSScott Long 		if (!error && cs_disk[len - 1] != '\0')
2546565282cSScott Long 			return (EINVAL);
2556565282cSScott Long 		cs_local = NULL;
2566565282cSScott Long 		error = vfs_getopt(opts, "cs_local", (void **)&cs_local, &len);
2576565282cSScott Long 		if (!error && cs_local[len - 1] != '\0')
2586565282cSScott Long 			return (EINVAL);
2596565282cSScott Long 		udf_iconv->open(cs_local, cs_disk, &imp->im_d2l);
2606565282cSScott Long #if 0
2616565282cSScott Long 		udf_iconv->open(cs_disk, cs_local, &imp->im_l2d);
2626565282cSScott Long #endif
2636565282cSScott Long 	}
2646565282cSScott Long 
265935ab476SPoul-Henning Kamp 	vfs_mountedfrom(mp, fspec);
26651a7b740SScott Long 	return 0;
26751a7b740SScott Long };
26851a7b740SScott Long 
26951a7b740SScott Long /*
27051a7b740SScott Long  * Check the descriptor tag for both the correct id and correct checksum.
27151a7b740SScott Long  * Return zero if all is good, EINVAL if not.
27251a7b740SScott Long  */
27351a7b740SScott Long int
udf_checktag(struct desc_tag * tag,uint16_t id)274c2d6947dSJeroen Ruigrok van der Werven udf_checktag(struct desc_tag *tag, uint16_t id)
27551a7b740SScott Long {
276c2d6947dSJeroen Ruigrok van der Werven 	uint8_t *itag;
277c2d6947dSJeroen Ruigrok van der Werven 	uint8_t i, cksum = 0;
27851a7b740SScott Long 
279c2d6947dSJeroen Ruigrok van der Werven 	itag = (uint8_t *)tag;
28051a7b740SScott Long 
281937a2387SWill Andrews 	if (le16toh(tag->id) != id)
28251a7b740SScott Long 		return (EINVAL);
28351a7b740SScott Long 
284a8a27cb0SMarkus Brueffer 	for (i = 0; i < 16; i++)
28551a7b740SScott Long 		cksum = cksum + itag[i];
28651a7b740SScott Long 	cksum = cksum - itag[4];
28751a7b740SScott Long 
28851a7b740SScott Long 	if (cksum == tag->cksum)
28951a7b740SScott Long 		return (0);
29051a7b740SScott Long 
29151a7b740SScott Long 	return (EINVAL);
29251a7b740SScott Long }
29351a7b740SScott Long 
29451a7b740SScott Long static int
udf_mountfs(struct vnode * devvp,struct mount * mp)2950b2ab5ecSJohn Baldwin udf_mountfs(struct vnode *devvp, struct mount *mp)
2960b2ab5ecSJohn Baldwin {
29751a7b740SScott Long 	struct buf *bp = NULL;
2980b2ab5ecSJohn Baldwin 	struct cdev *dev;
29951a7b740SScott Long 	struct anchor_vdp avdp;
30051a7b740SScott Long 	struct udf_mnt *udfmp = NULL;
30151a7b740SScott Long 	struct part_desc *pd;
30251a7b740SScott Long 	struct logvol_desc *lvd;
30351a7b740SScott Long 	struct fileset_desc *fsd;
30451a7b740SScott Long 	struct file_entry *root_fentry;
305c2d6947dSJeroen Ruigrok van der Werven 	uint32_t sector, size, mvds_start, mvds_end;
306a3d7f575SCraig Rodrigues 	uint32_t logical_secsize;
307c2d6947dSJeroen Ruigrok van der Werven 	uint32_t fsd_offset = 0;
308c2d6947dSJeroen Ruigrok van der Werven 	uint16_t part_num = 0, fsd_part = 0;
309429c018aSPoul-Henning Kamp 	int error = EINVAL;
31051a7b740SScott Long 	int logvol_found = 0, part_found = 0, fsd_found = 0;
31151a7b740SScott Long 	int bsize;
312429c018aSPoul-Henning Kamp 	struct g_consumer *cp;
313429c018aSPoul-Henning Kamp 	struct bufobj *bo;
31451a7b740SScott Long 
3150b2ab5ecSJohn Baldwin 	dev = devvp->v_rdev;
3160b2ab5ecSJohn Baldwin 	dev_ref(dev);
317429c018aSPoul-Henning Kamp 	g_topology_lock();
318429c018aSPoul-Henning Kamp 	error = g_vfs_open(devvp, &cp, "udf", 0);
319429c018aSPoul-Henning Kamp 	g_topology_unlock();
320b249ce48SMateusz Guzik 	VOP_UNLOCK(devvp);
32151a7b740SScott Long 	if (error)
3220b2ab5ecSJohn Baldwin 		goto bail;
32351a7b740SScott Long 
324429c018aSPoul-Henning Kamp 	bo = &devvp->v_bufobj;
325429c018aSPoul-Henning Kamp 
326fb2a76ccSAndriy Gapon 	if (devvp->v_rdev->si_iosize_max != 0)
327fb2a76ccSAndriy Gapon 		mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
328cd853791SKonstantin Belousov 	if (mp->mnt_iosize_max > maxphys)
329cd853791SKonstantin Belousov 		mp->mnt_iosize_max = maxphys;
330fb2a76ccSAndriy Gapon 
331429c018aSPoul-Henning Kamp 	/* XXX: should be M_WAITOK */
3321ede983cSDag-Erling Smørgrav 	udfmp = malloc(sizeof(struct udf_mnt), M_UDFMOUNT,
33351a7b740SScott Long 	    M_NOWAIT | M_ZERO);
33451a7b740SScott Long 	if (udfmp == NULL) {
33551a7b740SScott Long 		printf("Cannot allocate UDF mount struct\n");
33651a7b740SScott Long 		error = ENOMEM;
33751a7b740SScott Long 		goto bail;
33851a7b740SScott Long 	}
33951a7b740SScott Long 
34077465d93SAlfred Perlstein 	mp->mnt_data = udfmp;
34151a7b740SScott Long 	mp->mnt_stat.f_fsid.val[0] = dev2udev(devvp->v_rdev);
34251a7b740SScott Long 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
3435da56ddbSTor Egge 	MNT_ILOCK(mp);
34451a7b740SScott Long 	mp->mnt_flag |= MNT_LOCAL;
345bc2258daSAttilio Rao 	mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED;
3465da56ddbSTor Egge 	MNT_IUNLOCK(mp);
34751a7b740SScott Long 	udfmp->im_mountp = mp;
3480b2ab5ecSJohn Baldwin 	udfmp->im_dev = dev;
34951a7b740SScott Long 	udfmp->im_devvp = devvp;
3506565282cSScott Long 	udfmp->im_d2l = NULL;
351429c018aSPoul-Henning Kamp 	udfmp->im_cp = cp;
352429c018aSPoul-Henning Kamp 	udfmp->im_bo = bo;
353429c018aSPoul-Henning Kamp 
3546565282cSScott Long #if 0
3556565282cSScott Long 	udfmp->im_l2d = NULL;
3566565282cSScott Long #endif
357a3d7f575SCraig Rodrigues 	/*
358a3d7f575SCraig Rodrigues 	 * The UDF specification defines a logical sectorsize of 2048
359a3d7f575SCraig Rodrigues 	 * for DVD media.
360a3d7f575SCraig Rodrigues 	 */
361a3d7f575SCraig Rodrigues 	logical_secsize = 2048;
36251a7b740SScott Long 
363a3d7f575SCraig Rodrigues 	if (((logical_secsize % cp->provider->sectorsize) != 0) ||
364a3d7f575SCraig Rodrigues 	    (logical_secsize < cp->provider->sectorsize)) {
3650b2ab5ecSJohn Baldwin 		error = EINVAL;
3660b2ab5ecSJohn Baldwin 		goto bail;
367a3d7f575SCraig Rodrigues 	}
368a3d7f575SCraig Rodrigues 
369a3d7f575SCraig Rodrigues 	bsize = cp->provider->sectorsize;
37051a7b740SScott Long 
37151a7b740SScott Long 	/*
37251a7b740SScott Long 	 * Get the Anchor Volume Descriptor Pointer from sector 256.
37351a7b740SScott Long 	 * XXX Should also check sector n - 256, n, and 512.
37451a7b740SScott Long 	 */
37551a7b740SScott Long 	sector = 256;
376a3d7f575SCraig Rodrigues 	if ((error = bread(devvp, sector * btodb(logical_secsize), bsize,
377a3d7f575SCraig Rodrigues 			   NOCRED, &bp)) != 0)
37851a7b740SScott Long 		goto bail;
37951a7b740SScott Long 	if ((error = udf_checktag((struct desc_tag *)bp->b_data, TAGID_ANCHOR)))
38051a7b740SScott Long 		goto bail;
38151a7b740SScott Long 
38251a7b740SScott Long 	bcopy(bp->b_data, &avdp, sizeof(struct anchor_vdp));
38351a7b740SScott Long 	brelse(bp);
38451a7b740SScott Long 	bp = NULL;
38551a7b740SScott Long 
38651a7b740SScott Long 	/*
38751a7b740SScott Long 	 * Extract the Partition Descriptor and Logical Volume Descriptor
38851a7b740SScott Long 	 * from the Volume Descriptor Sequence.
38951a7b740SScott Long 	 * XXX Should we care about the partition type right now?
39051a7b740SScott Long 	 * XXX What about multiple partitions?
39151a7b740SScott Long 	 */
39289ec2c3cSScott Long 	mvds_start = le32toh(avdp.main_vds_ex.loc);
39389ec2c3cSScott Long 	mvds_end = mvds_start + (le32toh(avdp.main_vds_ex.len) - 1) / bsize;
39451a7b740SScott Long 	for (sector = mvds_start; sector < mvds_end; sector++) {
395a3d7f575SCraig Rodrigues 		if ((error = bread(devvp, sector * btodb(logical_secsize),
396a3d7f575SCraig Rodrigues 				   bsize, NOCRED, &bp)) != 0) {
39751a7b740SScott Long 			printf("Can't read sector %d of VDS\n", sector);
39851a7b740SScott Long 			goto bail;
39951a7b740SScott Long 		}
40051a7b740SScott Long 		lvd = (struct logvol_desc *)bp->b_data;
40151a7b740SScott Long 		if (!udf_checktag(&lvd->tag, TAGID_LOGVOL)) {
40289ec2c3cSScott Long 			udfmp->bsize = le32toh(lvd->lb_size);
4039e6941a2SKonstantin Belousov 			if (udfmp->bsize < 0 || udfmp->bsize > maxbcachebuf) {
4049e6941a2SKonstantin Belousov 				printf("lvd block size %d\n", udfmp->bsize);
4059e6941a2SKonstantin Belousov 				error = EINVAL;
4069e6941a2SKonstantin Belousov 				goto bail;
4079e6941a2SKonstantin Belousov 			}
40851a7b740SScott Long 			udfmp->bmask = udfmp->bsize - 1;
40951a7b740SScott Long 			udfmp->bshift = ffs(udfmp->bsize) - 1;
41089ec2c3cSScott Long 			fsd_part = le16toh(lvd->_lvd_use.fsd_loc.loc.part_num);
41189ec2c3cSScott Long 			fsd_offset = le32toh(lvd->_lvd_use.fsd_loc.loc.lb_num);
41251a7b740SScott Long 			if (udf_find_partmaps(udfmp, lvd))
41351a7b740SScott Long 				break;
41451a7b740SScott Long 			logvol_found = 1;
41551a7b740SScott Long 		}
41651a7b740SScott Long 		pd = (struct part_desc *)bp->b_data;
41751a7b740SScott Long 		if (!udf_checktag(&pd->tag, TAGID_PARTITION)) {
41851a7b740SScott Long 			part_found = 1;
41989ec2c3cSScott Long 			part_num = le16toh(pd->part_num);
42089ec2c3cSScott Long 			udfmp->part_len = le32toh(pd->part_len);
42189ec2c3cSScott Long 			udfmp->part_start = le32toh(pd->start_loc);
42251a7b740SScott Long 		}
42351a7b740SScott Long 
42451a7b740SScott Long 		brelse(bp);
42551a7b740SScott Long 		bp = NULL;
42651a7b740SScott Long 		if ((part_found) && (logvol_found))
42751a7b740SScott Long 			break;
42851a7b740SScott Long 	}
42951a7b740SScott Long 
43051a7b740SScott Long 	if (!part_found || !logvol_found) {
43151a7b740SScott Long 		error = EINVAL;
43251a7b740SScott Long 		goto bail;
43351a7b740SScott Long 	}
43451a7b740SScott Long 
43551a7b740SScott Long 	if (fsd_part != part_num) {
43651a7b740SScott Long 		printf("FSD does not lie within the partition!\n");
43751a7b740SScott Long 		error = EINVAL;
43851a7b740SScott Long 		goto bail;
43951a7b740SScott Long 	}
44051a7b740SScott Long 
44151a7b740SScott Long 	/*
44251a7b740SScott Long 	 * Grab the Fileset Descriptor
44351a7b740SScott Long 	 * Thanks to Chuck McCrobie <mccrobie@cablespeed.com> for pointing
44451a7b740SScott Long 	 * me in the right direction here.
44551a7b740SScott Long 	 */
44651a7b740SScott Long 	sector = udfmp->part_start + fsd_offset;
44751a7b740SScott Long 	if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
44851a7b740SScott Long 		printf("Cannot read sector %d of FSD\n", sector);
44951a7b740SScott Long 		goto bail;
45051a7b740SScott Long 	}
45151a7b740SScott Long 	fsd = (struct fileset_desc *)bp->b_data;
45251a7b740SScott Long 	if (!udf_checktag(&fsd->tag, TAGID_FSD)) {
45351a7b740SScott Long 		fsd_found = 1;
45451a7b740SScott Long 		bcopy(&fsd->rootdir_icb, &udfmp->root_icb,
45551a7b740SScott Long 		    sizeof(struct long_ad));
45651a7b740SScott Long 	}
45751a7b740SScott Long 
45851a7b740SScott Long 	brelse(bp);
45951a7b740SScott Long 	bp = NULL;
46051a7b740SScott Long 
46151a7b740SScott Long 	if (!fsd_found) {
46251a7b740SScott Long 		printf("Couldn't find the fsd\n");
46351a7b740SScott Long 		error = EINVAL;
46451a7b740SScott Long 		goto bail;
46551a7b740SScott Long 	}
46651a7b740SScott Long 
46751a7b740SScott Long 	/*
46851a7b740SScott Long 	 * Find the file entry for the root directory.
46951a7b740SScott Long 	 */
47089ec2c3cSScott Long 	sector = le32toh(udfmp->root_icb.loc.lb_num) + udfmp->part_start;
47189ec2c3cSScott Long 	size = le32toh(udfmp->root_icb.len);
472*c70e6150SJohn Baldwin 	if (size < UDF_FENTRY_SIZE) {
473*c70e6150SJohn Baldwin 		printf("Invalid root directory file entry length %u\n",
474*c70e6150SJohn Baldwin 		    size);
475*c70e6150SJohn Baldwin 		goto bail;
476*c70e6150SJohn Baldwin 	}
477b0c0fb59SAndriy Gapon 	if ((error = udf_readdevblks(udfmp, sector, size, &bp)) != 0) {
47851a7b740SScott Long 		printf("Cannot read sector %d\n", sector);
47951a7b740SScott Long 		goto bail;
48051a7b740SScott Long 	}
48151a7b740SScott Long 
48251a7b740SScott Long 	root_fentry = (struct file_entry *)bp->b_data;
48351a7b740SScott Long 	if ((error = udf_checktag(&root_fentry->tag, TAGID_FENTRY))) {
48451a7b740SScott Long 		printf("Invalid root file entry!\n");
48551a7b740SScott Long 		goto bail;
48651a7b740SScott Long 	}
48751a7b740SScott Long 
48851a7b740SScott Long 	brelse(bp);
48951a7b740SScott Long 	bp = NULL;
49051a7b740SScott Long 
49151a7b740SScott Long 	return 0;
49251a7b740SScott Long 
49351a7b740SScott Long bail:
49451a7b740SScott Long 	if (udfmp != NULL)
4951ede983cSDag-Erling Smørgrav 		free(udfmp, M_UDFMOUNT);
49651a7b740SScott Long 	if (bp != NULL)
49751a7b740SScott Long 		brelse(bp);
4980b2ab5ecSJohn Baldwin 	if (cp != NULL) {
499429c018aSPoul-Henning Kamp 		g_topology_lock();
5000d7935fdSAttilio Rao 		g_vfs_close(cp);
501429c018aSPoul-Henning Kamp 		g_topology_unlock();
5020b2ab5ecSJohn Baldwin 	}
5030b2ab5ecSJohn Baldwin 	dev_rel(dev);
50451a7b740SScott Long 	return error;
50551a7b740SScott Long };
50651a7b740SScott Long 
50751a7b740SScott Long static int
udf_unmount(struct mount * mp,int mntflags)508dfd233edSAttilio Rao udf_unmount(struct mount *mp, int mntflags)
50951a7b740SScott Long {
51051a7b740SScott Long 	struct udf_mnt *udfmp;
51151a7b740SScott Long 	int error, flags = 0;
51251a7b740SScott Long 
51351a7b740SScott Long 	udfmp = VFSTOUDFFS(mp);
51451a7b740SScott Long 
51551a7b740SScott Long 	if (mntflags & MNT_FORCE)
51651a7b740SScott Long 		flags |= FORCECLOSE;
51751a7b740SScott Long 
518dfd233edSAttilio Rao 	if ((error = vflush(mp, 0, flags, curthread)))
51951a7b740SScott Long 		return (error);
52051a7b740SScott Long 
5216565282cSScott Long 	if (udfmp->im_flags & UDFMNT_KICONV && udf_iconv) {
5226565282cSScott Long 		if (udfmp->im_d2l)
5236565282cSScott Long 			udf_iconv->close(udfmp->im_d2l);
5246565282cSScott Long #if 0
5256565282cSScott Long 		if (udfmp->im_l2d)
5266565282cSScott Long 			udf_iconv->close(udfmp->im_l2d);
5276565282cSScott Long #endif
5286565282cSScott Long 	}
5296565282cSScott Long 
5304e94fafcSPoul-Henning Kamp 	g_topology_lock();
5310d7935fdSAttilio Rao 	g_vfs_close(udfmp->im_cp);
5324e94fafcSPoul-Henning Kamp 	g_topology_unlock();
53351a7b740SScott Long 	vrele(udfmp->im_devvp);
5340b2ab5ecSJohn Baldwin 	dev_rel(udfmp->im_dev);
53551a7b740SScott Long 
53651a7b740SScott Long 	if (udfmp->s_table != NULL)
5371ede983cSDag-Erling Smørgrav 		free(udfmp->s_table, M_UDFMOUNT);
538c9c0dc5bSScott Long 
5391ede983cSDag-Erling Smørgrav 	free(udfmp, M_UDFMOUNT);
54051a7b740SScott Long 
54177465d93SAlfred Perlstein 	mp->mnt_data = NULL;
54251a7b740SScott Long 	return (0);
54351a7b740SScott Long }
54451a7b740SScott Long 
54551a7b740SScott Long static int
udf_root(struct mount * mp,int flags,struct vnode ** vpp)546dfd233edSAttilio Rao udf_root(struct mount *mp, int flags, struct vnode **vpp)
54751a7b740SScott Long {
54851a7b740SScott Long 	struct udf_mnt *udfmp;
54951a7b740SScott Long 	ino_t id;
55051a7b740SScott Long 
55151a7b740SScott Long 	udfmp = VFSTOUDFFS(mp);
55251a7b740SScott Long 
55351a7b740SScott Long 	id = udf_getid(&udfmp->root_icb);
55451a7b740SScott Long 
5554ad0d60bSJohn Baldwin 	return (udf_vget(mp, id, flags, vpp));
55651a7b740SScott Long }
55751a7b740SScott Long 
55851a7b740SScott Long static int
udf_statfs(struct mount * mp,struct statfs * sbp)559dfd233edSAttilio Rao udf_statfs(struct mount *mp, struct statfs *sbp)
56051a7b740SScott Long {
56151a7b740SScott Long 	struct udf_mnt *udfmp;
56251a7b740SScott Long 
56351a7b740SScott Long 	udfmp = VFSTOUDFFS(mp);
56451a7b740SScott Long 
56551a7b740SScott Long 	sbp->f_bsize = udfmp->bsize;
56651a7b740SScott Long 	sbp->f_iosize = udfmp->bsize;
56751a7b740SScott Long 	sbp->f_blocks = udfmp->part_len;
56851a7b740SScott Long 	sbp->f_bfree = 0;
56951a7b740SScott Long 	sbp->f_bavail = 0;
57051a7b740SScott Long 	sbp->f_files = 0;
57151a7b740SScott Long 	sbp->f_ffree = 0;
57251a7b740SScott Long 	return 0;
57351a7b740SScott Long }
57451a7b740SScott Long 
57551a7b740SScott Long int
udf_vget(struct mount * mp,ino_t ino,int flags,struct vnode ** vpp)57651a7b740SScott Long udf_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
57751a7b740SScott Long {
57851a7b740SScott Long 	struct buf *bp;
57951a7b740SScott Long 	struct vnode *devvp;
58051a7b740SScott Long 	struct udf_mnt *udfmp;
58151a7b740SScott Long 	struct thread *td;
58251a7b740SScott Long 	struct vnode *vp;
58351a7b740SScott Long 	struct udf_node *unode;
58451a7b740SScott Long 	struct file_entry *fe;
5858e13d6dfSMark Johnston 	uint32_t lea, lad;
58651a7b740SScott Long 	int error, sector, size;
58751a7b740SScott Long 
58851f5ce0cSPoul-Henning Kamp 	error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
589e82ef95cSPoul-Henning Kamp 	if (error || *vpp != NULL)
5904e94fafcSPoul-Henning Kamp 		return (error);
5914e94fafcSPoul-Henning Kamp 
5924ad0d60bSJohn Baldwin 	/*
5934ad0d60bSJohn Baldwin 	 * We must promote to an exclusive lock for vnode creation.  This
5944ad0d60bSJohn Baldwin 	 * can happen if lookup is passed LOCKSHARED.
5954ad0d60bSJohn Baldwin  	 */
5964ad0d60bSJohn Baldwin 	if ((flags & LK_TYPE_MASK) == LK_SHARED) {
5974ad0d60bSJohn Baldwin 		flags &= ~LK_TYPE_MASK;
5984ad0d60bSJohn Baldwin 		flags |= LK_EXCLUSIVE;
5994ad0d60bSJohn Baldwin 	}
6004ad0d60bSJohn Baldwin 
6014ad0d60bSJohn Baldwin 	/*
6024ad0d60bSJohn Baldwin 	 * We do not lock vnode creation as it is believed to be too
6034ad0d60bSJohn Baldwin 	 * expensive for such rare case as simultaneous creation of vnode
6044ad0d60bSJohn Baldwin 	 * for same ino by different processes. We just allow them to race
6054ad0d60bSJohn Baldwin 	 * and check later to decide who wins. Let the race begin!
6064ad0d60bSJohn Baldwin 	 */
6074ad0d60bSJohn Baldwin 
60851a7b740SScott Long 	td = curthread;
60951a7b740SScott Long 	udfmp = VFSTOUDFFS(mp);
61051a7b740SScott Long 
6114e94fafcSPoul-Henning Kamp 	unode = uma_zalloc(udf_zone_node, M_WAITOK | M_ZERO);
61251a7b740SScott Long 
61351a7b740SScott Long 	if ((error = udf_allocv(mp, &vp, td))) {
61451a7b740SScott Long 		printf("Error from udf_allocv\n");
61551a7b740SScott Long 		uma_zfree(udf_zone_node, unode);
61651a7b740SScott Long 		return (error);
61751a7b740SScott Long 	}
61851a7b740SScott Long 
61951a7b740SScott Long 	unode->i_vnode = vp;
62051a7b740SScott Long 	unode->hash_id = ino;
62151a7b740SScott Long 	unode->udfmp = udfmp;
62251a7b740SScott Long 	vp->v_data = unode;
6234e94fafcSPoul-Henning Kamp 
6240e9eb108SAttilio Rao 	lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
62561b9d89fSTor Egge 	error = insmntque(vp, mp);
62661b9d89fSTor Egge 	if (error != 0) {
62761b9d89fSTor Egge 		uma_zfree(udf_zone_node, unode);
62861b9d89fSTor Egge 		return (error);
62961b9d89fSTor Egge 	}
63061b9d89fSTor Egge 	error = vfs_hash_insert(vp, ino, flags, td, vpp, NULL, NULL);
63145c26fa2SPoul-Henning Kamp 	if (error || *vpp != NULL)
6324e94fafcSPoul-Henning Kamp 		return (error);
6334e94fafcSPoul-Henning Kamp 
6344e94fafcSPoul-Henning Kamp 	/*
6354e94fafcSPoul-Henning Kamp 	 * Copy in the file entry.  Per the spec, the size can only be 1 block.
6364e94fafcSPoul-Henning Kamp 	 */
6374e94fafcSPoul-Henning Kamp 	sector = ino + udfmp->part_start;
6384e94fafcSPoul-Henning Kamp 	devvp = udfmp->im_devvp;
6394e94fafcSPoul-Henning Kamp 	if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
6404e94fafcSPoul-Henning Kamp 		printf("Cannot read sector %d\n", sector);
6418e13d6dfSMark Johnston 		goto error;
6424e94fafcSPoul-Henning Kamp 	}
6434e94fafcSPoul-Henning Kamp 
6448e13d6dfSMark Johnston 	/*
6458e13d6dfSMark Johnston 	 * File entry length validation.
6468e13d6dfSMark Johnston 	 */
6474e94fafcSPoul-Henning Kamp 	fe = (struct file_entry *)bp->b_data;
6484e94fafcSPoul-Henning Kamp 	if (udf_checktag(&fe->tag, TAGID_FENTRY)) {
6494e94fafcSPoul-Henning Kamp 		printf("Invalid file entry!\n");
6508e13d6dfSMark Johnston 		error = ENOMEM;
6518e13d6dfSMark Johnston 		goto error;
6524e94fafcSPoul-Henning Kamp 	}
6538e13d6dfSMark Johnston 	lea = le32toh(fe->l_ea);
6548e13d6dfSMark Johnston 	lad = le32toh(fe->l_ad);
6558e13d6dfSMark Johnston 	if (lea > udfmp->bsize || lad > udfmp->bsize) {
6568e13d6dfSMark Johnston 		printf("Invalid EA and AD lengths %u, %u\n", lea, lad);
6578e13d6dfSMark Johnston 		error = EIO;
6588e13d6dfSMark Johnston 		goto error;
6598e13d6dfSMark Johnston 	}
6608e13d6dfSMark Johnston 	size = UDF_FENTRY_SIZE + lea + lad;
6618e13d6dfSMark Johnston 	if (size > udfmp->bsize) {
6628e13d6dfSMark Johnston 		printf("Invalid file entry size %u\n", size);
6638e13d6dfSMark Johnston 		error = EIO;
6648e13d6dfSMark Johnston 		goto error;
6658e13d6dfSMark Johnston 	}
6668e13d6dfSMark Johnston 
667e11e3f18SDag-Erling Smørgrav 	unode->fentry = malloc(size, M_UDFFENTRY, M_NOWAIT | M_ZERO);
6684e94fafcSPoul-Henning Kamp 	if (unode->fentry == NULL) {
6694e94fafcSPoul-Henning Kamp 		printf("Cannot allocate file entry block\n");
6708e13d6dfSMark Johnston 		error = ENOMEM;
6718e13d6dfSMark Johnston 		goto error;
6724e94fafcSPoul-Henning Kamp 	}
6734e94fafcSPoul-Henning Kamp 
6744e94fafcSPoul-Henning Kamp 	bcopy(bp->b_data, unode->fentry, size);
6754e94fafcSPoul-Henning Kamp 
6764e94fafcSPoul-Henning Kamp 	brelse(bp);
6774e94fafcSPoul-Henning Kamp 	bp = NULL;
67851a7b740SScott Long 
67951a7b740SScott Long 	switch (unode->fentry->icbtag.file_type) {
68051a7b740SScott Long 	default:
68151a7b740SScott Long 		vp->v_type = VBAD;
68251a7b740SScott Long 		break;
68351a7b740SScott Long 	case 4:
68451a7b740SScott Long 		vp->v_type = VDIR;
68551a7b740SScott Long 		break;
68651a7b740SScott Long 	case 5:
68751a7b740SScott Long 		vp->v_type = VREG;
68851a7b740SScott Long 		break;
68951a7b740SScott Long 	case 6:
69051a7b740SScott Long 		vp->v_type = VBLK;
69151a7b740SScott Long 		break;
69251a7b740SScott Long 	case 7:
69351a7b740SScott Long 		vp->v_type = VCHR;
69451a7b740SScott Long 		break;
69551a7b740SScott Long 	case 9:
69651a7b740SScott Long 		vp->v_type = VFIFO;
69761e69c80SJohn Baldwin 		vp->v_op = &udf_fifoops;
69851a7b740SScott Long 		break;
69951a7b740SScott Long 	case 10:
70051a7b740SScott Long 		vp->v_type = VSOCK;
70151a7b740SScott Long 		break;
70251a7b740SScott Long 	case 12:
70351a7b740SScott Long 		vp->v_type = VLNK;
70451a7b740SScott Long 		break;
70551a7b740SScott Long 	}
7064ad0d60bSJohn Baldwin 
70761e1c193SJohn Baldwin 	if (vp->v_type != VFIFO)
7084ad0d60bSJohn Baldwin 		VN_LOCK_ASHARE(vp);
7094ad0d60bSJohn Baldwin 
7104ad0d60bSJohn Baldwin 	if (ino == udf_getid(&udfmp->root_icb))
7114ad0d60bSJohn Baldwin 		vp->v_vflag |= VV_ROOT;
7124ad0d60bSJohn Baldwin 
713829f0bcbSMateusz Guzik 	vn_set_state(vp, VSTATE_CONSTRUCTED);
71451a7b740SScott Long 	*vpp = vp;
71551a7b740SScott Long 
71651a7b740SScott Long 	return (0);
7178e13d6dfSMark Johnston 
7188e13d6dfSMark Johnston error:
7198e13d6dfSMark Johnston 	vgone(vp);
7208e13d6dfSMark Johnston 	vput(vp);
7218e13d6dfSMark Johnston 	brelse(bp);
7228e13d6dfSMark Johnston 	*vpp = NULL;
7238e13d6dfSMark Johnston 	return (error);
72451a7b740SScott Long }
72551a7b740SScott Long 
72651a7b740SScott Long static int
udf_fhtovp(struct mount * mp,struct fid * fhp,int flags,struct vnode ** vpp)727694a586aSRick Macklem udf_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
72851a7b740SScott Long {
72951a7b740SScott Long 	struct ifid *ifhp;
73051a7b740SScott Long 	struct vnode *nvp;
7319f3eef13SPav Lucistnik 	struct udf_node *np;
7329f3eef13SPav Lucistnik 	off_t fsize;
73351a7b740SScott Long 	int error;
73451a7b740SScott Long 
73551a7b740SScott Long 	ifhp = (struct ifid *)fhp;
73651a7b740SScott Long 
73751a7b740SScott Long 	if ((error = VFS_VGET(mp, ifhp->ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
73851a7b740SScott Long 		*vpp = NULLVP;
73951a7b740SScott Long 		return (error);
74051a7b740SScott Long 	}
74151a7b740SScott Long 
7429f3eef13SPav Lucistnik 	np = VTON(nvp);
7439f3eef13SPav Lucistnik 	fsize = le64toh(np->fentry->inf_len);
7449f3eef13SPav Lucistnik 
74551a7b740SScott Long 	*vpp = nvp;
7469f3eef13SPav Lucistnik 	vnode_create_vobject(*vpp, fsize, curthread);
74751a7b740SScott Long 	return (0);
74851a7b740SScott Long }
74951a7b740SScott Long 
75051a7b740SScott Long static int
udf_find_partmaps(struct udf_mnt * udfmp,struct logvol_desc * lvd)75151a7b740SScott Long udf_find_partmaps(struct udf_mnt *udfmp, struct logvol_desc *lvd)
75251a7b740SScott Long {
75351a7b740SScott Long 	struct part_map_spare *pms;
75451a7b740SScott Long 	struct regid *pmap_id;
75551a7b740SScott Long 	struct buf *bp;
75651a7b740SScott Long 	unsigned char regid_id[UDF_REGID_ID_SIZE + 1];
75709e19031SScott Long 	int i, k, ptype, psize, error;
75869f0212fSYaroslav Tykhiy 	uint8_t *pmap = (uint8_t *) &lvd->maps[0];
75951a7b740SScott Long 
76089ec2c3cSScott Long 	for (i = 0; i < le32toh(lvd->n_pm); i++) {
76169f0212fSYaroslav Tykhiy 		ptype = pmap[0];
76269f0212fSYaroslav Tykhiy 		psize = pmap[1];
76351a7b740SScott Long 		if (((ptype != 1) && (ptype != 2)) ||
76469f0212fSYaroslav Tykhiy 		    ((psize != UDF_PMAP_TYPE1_SIZE) &&
76569f0212fSYaroslav Tykhiy 		     (psize != UDF_PMAP_TYPE2_SIZE))) {
76651a7b740SScott Long 			printf("Invalid partition map found\n");
76751a7b740SScott Long 			return (1);
76851a7b740SScott Long 		}
76951a7b740SScott Long 
77051a7b740SScott Long 		if (ptype == 1) {
77151a7b740SScott Long 			/* Type 1 map.  We don't care */
77269f0212fSYaroslav Tykhiy 			pmap += UDF_PMAP_TYPE1_SIZE;
77351a7b740SScott Long 			continue;
77451a7b740SScott Long 		}
77551a7b740SScott Long 
77651a7b740SScott Long 		/* Type 2 map.  Gotta find out the details */
77769f0212fSYaroslav Tykhiy 		pmap_id = (struct regid *)&pmap[4];
77851a7b740SScott Long 		bzero(&regid_id[0], UDF_REGID_ID_SIZE);
77951a7b740SScott Long 		bcopy(&pmap_id->id[0], &regid_id[0], UDF_REGID_ID_SIZE);
78051a7b740SScott Long 
78151a7b740SScott Long 		if (bcmp(&regid_id[0], "*UDF Sparable Partition",
78251a7b740SScott Long 		    UDF_REGID_ID_SIZE)) {
78351a7b740SScott Long 			printf("Unsupported partition map: %s\n", &regid_id[0]);
78451a7b740SScott Long 			return (1);
78551a7b740SScott Long 		}
78651a7b740SScott Long 
78769f0212fSYaroslav Tykhiy 		pms = (struct part_map_spare *)pmap;
78869f0212fSYaroslav Tykhiy 		pmap += UDF_PMAP_TYPE2_SIZE;
789e11e3f18SDag-Erling Smørgrav 		udfmp->s_table = malloc(le32toh(pms->st_size),
790e11e3f18SDag-Erling Smørgrav 		    M_UDFMOUNT, M_NOWAIT | M_ZERO);
79151a7b740SScott Long 		if (udfmp->s_table == NULL)
79251a7b740SScott Long 			return (ENOMEM);
79351a7b740SScott Long 
79451a7b740SScott Long 		/* Calculate the number of sectors per packet. */
79551a7b740SScott Long 		/* XXX Logical or physical? */
79689ec2c3cSScott Long 		udfmp->p_sectors = le16toh(pms->packet_len) / udfmp->bsize;
79751a7b740SScott Long 
79851a7b740SScott Long 		/*
79951a7b740SScott Long 		 * XXX If reading the first Sparing Table fails, should look
80051a7b740SScott Long 		 * for another table.
80151a7b740SScott Long 		 */
802b0c0fb59SAndriy Gapon 		if ((error = udf_readdevblks(udfmp, le32toh(pms->st_loc[0]),
80389ec2c3cSScott Long 					   le32toh(pms->st_size), &bp)) != 0) {
804744bb56dSScott Long 			if (bp != NULL)
805744bb56dSScott Long 				brelse(bp);
80651a7b740SScott Long 			printf("Failed to read Sparing Table at sector %d\n",
80789ec2c3cSScott Long 			    le32toh(pms->st_loc[0]));
8081ede983cSDag-Erling Smørgrav 			free(udfmp->s_table, M_UDFMOUNT);
80951a7b740SScott Long 			return (error);
81051a7b740SScott Long 		}
81189ec2c3cSScott Long 		bcopy(bp->b_data, udfmp->s_table, le32toh(pms->st_size));
81251a7b740SScott Long 		brelse(bp);
81351a7b740SScott Long 
81451a7b740SScott Long 		if (udf_checktag(&udfmp->s_table->tag, 0)) {
81551a7b740SScott Long 			printf("Invalid sparing table found\n");
8161ede983cSDag-Erling Smørgrav 			free(udfmp->s_table, M_UDFMOUNT);
81751a7b740SScott Long 			return (EINVAL);
81851a7b740SScott Long 		}
81951a7b740SScott Long 
82051a7b740SScott Long 		/* See how many valid entries there are here.  The list is
82151a7b740SScott Long 		 * supposed to be sorted. 0xfffffff0 and higher are not valid
82251a7b740SScott Long 		 */
82309e19031SScott Long 		for (k = 0; k < le16toh(udfmp->s_table->rt_l); k++) {
82409e19031SScott Long 			udfmp->s_table_entries = k;
82509e19031SScott Long 			if (le32toh(udfmp->s_table->entries[k].org) >=
82689ec2c3cSScott Long 			    0xfffffff0)
82751a7b740SScott Long 				break;
82851a7b740SScott Long 		}
82951a7b740SScott Long 	}
83051a7b740SScott Long 
83151a7b740SScott Long 	return (0);
83251a7b740SScott Long }
833