xref: /freebsd/sys/fs/udf/udf_vfsops.c (revision d0b2dbfa)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2001, 2002 Scott Long <scottl@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /* udf_vfsops.c */
30 /* Implement the VFS side of things */
31 
32 /*
33  * Ok, here's how it goes.  The UDF specs are pretty clear on how each data
34  * structure is made up, but not very clear on how they relate to each other.
35  * Here is the skinny... This demostrates a filesystem with one file in the
36  * root directory.  Subdirectories are treated just as normal files, but they
37  * have File Id Descriptors of their children as their file data.  As for the
38  * Anchor Volume Descriptor Pointer, it can exist in two of the following three
39  * places: sector 256, sector n (the max sector of the disk), or sector
40  * n - 256.  It's a pretty good bet that one will exist at sector 256 though.
41  * One caveat is unclosed CD media.  For that, sector 256 cannot be written,
42  * so the Anchor Volume Descriptor Pointer can exist at sector 512 until the
43  * media is closed.
44  *
45  *  Sector:
46  *     256:
47  *       n: Anchor Volume Descriptor Pointer
48  * n - 256:	|
49  *		|
50  *		|-->Main Volume Descriptor Sequence
51  *			|	|
52  *			|	|
53  *			|	|-->Logical Volume Descriptor
54  *			|			  |
55  *			|-->Partition Descriptor  |
56  *				|		  |
57  *				|		  |
58  *				|-->Fileset Descriptor
59  *					|
60  *					|
61  *					|-->Root Dir File Entry
62  *						|
63  *						|
64  *						|-->File data:
65  *						    File Id Descriptor
66  *							|
67  *							|
68  *							|-->File Entry
69  *								|
70  *								|
71  *								|-->File data
72  */
73 #include <sys/types.h>
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/uio.h>
77 #include <sys/bio.h>
78 #include <sys/buf.h>
79 #include <sys/conf.h>
80 #include <sys/dirent.h>
81 #include <sys/fcntl.h>
82 #include <sys/iconv.h>
83 #include <sys/kernel.h>
84 #include <sys/malloc.h>
85 #include <sys/mount.h>
86 #include <sys/namei.h>
87 #include <sys/priv.h>
88 #include <sys/proc.h>
89 #include <sys/queue.h>
90 #include <sys/vnode.h>
91 #include <sys/endian.h>
92 
93 #include <geom/geom.h>
94 #include <geom/geom_vfs.h>
95 
96 #include <vm/uma.h>
97 
98 #include <fs/udf/ecma167-udf.h>
99 #include <fs/udf/osta.h>
100 #include <fs/udf/udf.h>
101 #include <fs/udf/udf_mount.h>
102 
103 static MALLOC_DEFINE(M_UDFMOUNT, "udf_mount", "UDF mount structure");
104 MALLOC_DEFINE(M_UDFFENTRY, "udf_fentry", "UDF file entry structure");
105 
106 struct iconv_functions *udf_iconv = NULL;
107 
108 /* Zones */
109 uma_zone_t udf_zone_trans = NULL;
110 uma_zone_t udf_zone_node = NULL;
111 uma_zone_t udf_zone_ds = NULL;
112 
113 static vfs_init_t      udf_init;
114 static vfs_uninit_t    udf_uninit;
115 static vfs_mount_t     udf_mount;
116 static vfs_root_t      udf_root;
117 static vfs_statfs_t    udf_statfs;
118 static vfs_unmount_t   udf_unmount;
119 static vfs_fhtovp_t	udf_fhtovp;
120 
121 static int udf_find_partmaps(struct udf_mnt *, struct logvol_desc *);
122 
123 static struct vfsops udf_vfsops = {
124 	.vfs_fhtovp =		udf_fhtovp,
125 	.vfs_init =		udf_init,
126 	.vfs_mount =		udf_mount,
127 	.vfs_root =		udf_root,
128 	.vfs_statfs =		udf_statfs,
129 	.vfs_uninit =		udf_uninit,
130 	.vfs_unmount =		udf_unmount,
131 	.vfs_vget =		udf_vget,
132 };
133 VFS_SET(udf_vfsops, udf, VFCF_READONLY);
134 
135 MODULE_VERSION(udf, 1);
136 
137 static int udf_mountfs(struct vnode *, struct mount *);
138 
139 static int
140 udf_init(struct vfsconf *foo)
141 {
142 
143 	/*
144 	 * This code used to pre-allocate a certain number of pages for each
145 	 * pool, reducing the need to grow the zones later on.  UMA doesn't
146 	 * advertise any such functionality, unfortunately =-<
147 	 */
148 	udf_zone_trans = uma_zcreate("UDF translation buffer, zone", MAXNAMLEN *
149 	    sizeof(unicode_t), NULL, NULL, NULL, NULL, 0, 0);
150 
151 	udf_zone_node = uma_zcreate("UDF Node zone", sizeof(struct udf_node),
152 	    NULL, NULL, NULL, NULL, 0, 0);
153 
154 	udf_zone_ds = uma_zcreate("UDF Dirstream zone",
155 	    sizeof(struct udf_dirstream), NULL, NULL, NULL, NULL, 0, 0);
156 
157 	if ((udf_zone_node == NULL) || (udf_zone_trans == NULL) ||
158 	    (udf_zone_ds == NULL)) {
159 		printf("Cannot create allocation zones.\n");
160 		return (ENOMEM);
161 	}
162 
163 	return 0;
164 }
165 
166 static int
167 udf_uninit(struct vfsconf *foo)
168 {
169 
170 	if (udf_zone_trans != NULL) {
171 		uma_zdestroy(udf_zone_trans);
172 		udf_zone_trans = NULL;
173 	}
174 
175 	if (udf_zone_node != NULL) {
176 		uma_zdestroy(udf_zone_node);
177 		udf_zone_node = NULL;
178 	}
179 
180 	if (udf_zone_ds != NULL) {
181 		uma_zdestroy(udf_zone_ds);
182 		udf_zone_ds = NULL;
183 	}
184 
185 	return (0);
186 }
187 
188 static int
189 udf_mount(struct mount *mp)
190 {
191 	struct vnode *devvp;	/* vnode of the mount device */
192 	struct thread *td;
193 	struct udf_mnt *imp = NULL;
194 	struct vfsoptlist *opts;
195 	char *fspec, *cs_disk, *cs_local;
196 	int error, len, *udf_flags;
197 	struct nameidata nd, *ndp = &nd;
198 
199 	td = curthread;
200 	opts = mp->mnt_optnew;
201 
202 	/*
203 	 * Unconditionally mount as read-only.
204 	 */
205 	MNT_ILOCK(mp);
206 	mp->mnt_flag |= MNT_RDONLY;
207 	MNT_IUNLOCK(mp);
208 
209 	/*
210 	 * No root filesystem support.  Probably not a big deal, since the
211 	 * bootloader doesn't understand UDF.
212 	 */
213 	if (mp->mnt_flag & MNT_ROOTFS)
214 		return (ENOTSUP);
215 
216 	fspec = NULL;
217 	error = vfs_getopt(opts, "from", (void **)&fspec, &len);
218 	if (!error && fspec[len - 1] != '\0')
219 		return (EINVAL);
220 
221 	if (mp->mnt_flag & MNT_UPDATE) {
222 		return (0);
223 	}
224 
225 	/* Check that the mount device exists */
226 	if (fspec == NULL)
227 		return (EINVAL);
228 	NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec);
229 	if ((error = namei(ndp)))
230 		return (error);
231 	NDFREE_PNBUF(ndp);
232 	devvp = ndp->ni_vp;
233 
234 	if (!vn_isdisk_error(devvp, &error)) {
235 		vput(devvp);
236 		return (error);
237 	}
238 
239 	/* Check the access rights on the mount device */
240 	error = VOP_ACCESS(devvp, VREAD, td->td_ucred, td);
241 	if (error)
242 		error = priv_check(td, PRIV_VFS_MOUNT_PERM);
243 	if (error) {
244 		vput(devvp);
245 		return (error);
246 	}
247 
248 	if ((error = udf_mountfs(devvp, mp))) {
249 		vrele(devvp);
250 		return (error);
251 	}
252 
253 	imp = VFSTOUDFFS(mp);
254 
255 	udf_flags = NULL;
256 	error = vfs_getopt(opts, "flags", (void **)&udf_flags, &len);
257 	if (error || len != sizeof(int))
258 		return (EINVAL);
259 	imp->im_flags = *udf_flags;
260 
261 	if (imp->im_flags & UDFMNT_KICONV && udf_iconv) {
262 		cs_disk = NULL;
263 		error = vfs_getopt(opts, "cs_disk", (void **)&cs_disk, &len);
264 		if (!error && cs_disk[len - 1] != '\0')
265 			return (EINVAL);
266 		cs_local = NULL;
267 		error = vfs_getopt(opts, "cs_local", (void **)&cs_local, &len);
268 		if (!error && cs_local[len - 1] != '\0')
269 			return (EINVAL);
270 		udf_iconv->open(cs_local, cs_disk, &imp->im_d2l);
271 #if 0
272 		udf_iconv->open(cs_disk, cs_local, &imp->im_l2d);
273 #endif
274 	}
275 
276 	vfs_mountedfrom(mp, fspec);
277 	return 0;
278 };
279 
280 /*
281  * Check the descriptor tag for both the correct id and correct checksum.
282  * Return zero if all is good, EINVAL if not.
283  */
284 int
285 udf_checktag(struct desc_tag *tag, uint16_t id)
286 {
287 	uint8_t *itag;
288 	uint8_t i, cksum = 0;
289 
290 	itag = (uint8_t *)tag;
291 
292 	if (le16toh(tag->id) != id)
293 		return (EINVAL);
294 
295 	for (i = 0; i < 16; i++)
296 		cksum = cksum + itag[i];
297 	cksum = cksum - itag[4];
298 
299 	if (cksum == tag->cksum)
300 		return (0);
301 
302 	return (EINVAL);
303 }
304 
305 static int
306 udf_mountfs(struct vnode *devvp, struct mount *mp)
307 {
308 	struct buf *bp = NULL;
309 	struct cdev *dev;
310 	struct anchor_vdp avdp;
311 	struct udf_mnt *udfmp = NULL;
312 	struct part_desc *pd;
313 	struct logvol_desc *lvd;
314 	struct fileset_desc *fsd;
315 	struct file_entry *root_fentry;
316 	uint32_t sector, size, mvds_start, mvds_end;
317 	uint32_t logical_secsize;
318 	uint32_t fsd_offset = 0;
319 	uint16_t part_num = 0, fsd_part = 0;
320 	int error = EINVAL;
321 	int logvol_found = 0, part_found = 0, fsd_found = 0;
322 	int bsize;
323 	struct g_consumer *cp;
324 	struct bufobj *bo;
325 
326 	dev = devvp->v_rdev;
327 	dev_ref(dev);
328 	g_topology_lock();
329 	error = g_vfs_open(devvp, &cp, "udf", 0);
330 	g_topology_unlock();
331 	VOP_UNLOCK(devvp);
332 	if (error)
333 		goto bail;
334 
335 	bo = &devvp->v_bufobj;
336 
337 	if (devvp->v_rdev->si_iosize_max != 0)
338 		mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
339 	if (mp->mnt_iosize_max > maxphys)
340 		mp->mnt_iosize_max = maxphys;
341 
342 	/* XXX: should be M_WAITOK */
343 	udfmp = malloc(sizeof(struct udf_mnt), M_UDFMOUNT,
344 	    M_NOWAIT | M_ZERO);
345 	if (udfmp == NULL) {
346 		printf("Cannot allocate UDF mount struct\n");
347 		error = ENOMEM;
348 		goto bail;
349 	}
350 
351 	mp->mnt_data = udfmp;
352 	mp->mnt_stat.f_fsid.val[0] = dev2udev(devvp->v_rdev);
353 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
354 	MNT_ILOCK(mp);
355 	mp->mnt_flag |= MNT_LOCAL;
356 	mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED;
357 	MNT_IUNLOCK(mp);
358 	udfmp->im_mountp = mp;
359 	udfmp->im_dev = dev;
360 	udfmp->im_devvp = devvp;
361 	udfmp->im_d2l = NULL;
362 	udfmp->im_cp = cp;
363 	udfmp->im_bo = bo;
364 
365 #if 0
366 	udfmp->im_l2d = NULL;
367 #endif
368 	/*
369 	 * The UDF specification defines a logical sectorsize of 2048
370 	 * for DVD media.
371 	 */
372 	logical_secsize = 2048;
373 
374 	if (((logical_secsize % cp->provider->sectorsize) != 0) ||
375 	    (logical_secsize < cp->provider->sectorsize)) {
376 		error = EINVAL;
377 		goto bail;
378 	}
379 
380 	bsize = cp->provider->sectorsize;
381 
382 	/*
383 	 * Get the Anchor Volume Descriptor Pointer from sector 256.
384 	 * XXX Should also check sector n - 256, n, and 512.
385 	 */
386 	sector = 256;
387 	if ((error = bread(devvp, sector * btodb(logical_secsize), bsize,
388 			   NOCRED, &bp)) != 0)
389 		goto bail;
390 	if ((error = udf_checktag((struct desc_tag *)bp->b_data, TAGID_ANCHOR)))
391 		goto bail;
392 
393 	bcopy(bp->b_data, &avdp, sizeof(struct anchor_vdp));
394 	brelse(bp);
395 	bp = NULL;
396 
397 	/*
398 	 * Extract the Partition Descriptor and Logical Volume Descriptor
399 	 * from the Volume Descriptor Sequence.
400 	 * XXX Should we care about the partition type right now?
401 	 * XXX What about multiple partitions?
402 	 */
403 	mvds_start = le32toh(avdp.main_vds_ex.loc);
404 	mvds_end = mvds_start + (le32toh(avdp.main_vds_ex.len) - 1) / bsize;
405 	for (sector = mvds_start; sector < mvds_end; sector++) {
406 		if ((error = bread(devvp, sector * btodb(logical_secsize),
407 				   bsize, NOCRED, &bp)) != 0) {
408 			printf("Can't read sector %d of VDS\n", sector);
409 			goto bail;
410 		}
411 		lvd = (struct logvol_desc *)bp->b_data;
412 		if (!udf_checktag(&lvd->tag, TAGID_LOGVOL)) {
413 			udfmp->bsize = le32toh(lvd->lb_size);
414 			if (udfmp->bsize < 0 || udfmp->bsize > maxbcachebuf) {
415 				printf("lvd block size %d\n", udfmp->bsize);
416 				error = EINVAL;
417 				goto bail;
418 			}
419 			udfmp->bmask = udfmp->bsize - 1;
420 			udfmp->bshift = ffs(udfmp->bsize) - 1;
421 			fsd_part = le16toh(lvd->_lvd_use.fsd_loc.loc.part_num);
422 			fsd_offset = le32toh(lvd->_lvd_use.fsd_loc.loc.lb_num);
423 			if (udf_find_partmaps(udfmp, lvd))
424 				break;
425 			logvol_found = 1;
426 		}
427 		pd = (struct part_desc *)bp->b_data;
428 		if (!udf_checktag(&pd->tag, TAGID_PARTITION)) {
429 			part_found = 1;
430 			part_num = le16toh(pd->part_num);
431 			udfmp->part_len = le32toh(pd->part_len);
432 			udfmp->part_start = le32toh(pd->start_loc);
433 		}
434 
435 		brelse(bp);
436 		bp = NULL;
437 		if ((part_found) && (logvol_found))
438 			break;
439 	}
440 
441 	if (!part_found || !logvol_found) {
442 		error = EINVAL;
443 		goto bail;
444 	}
445 
446 	if (fsd_part != part_num) {
447 		printf("FSD does not lie within the partition!\n");
448 		error = EINVAL;
449 		goto bail;
450 	}
451 
452 	/*
453 	 * Grab the Fileset Descriptor
454 	 * Thanks to Chuck McCrobie <mccrobie@cablespeed.com> for pointing
455 	 * me in the right direction here.
456 	 */
457 	sector = udfmp->part_start + fsd_offset;
458 	if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
459 		printf("Cannot read sector %d of FSD\n", sector);
460 		goto bail;
461 	}
462 	fsd = (struct fileset_desc *)bp->b_data;
463 	if (!udf_checktag(&fsd->tag, TAGID_FSD)) {
464 		fsd_found = 1;
465 		bcopy(&fsd->rootdir_icb, &udfmp->root_icb,
466 		    sizeof(struct long_ad));
467 	}
468 
469 	brelse(bp);
470 	bp = NULL;
471 
472 	if (!fsd_found) {
473 		printf("Couldn't find the fsd\n");
474 		error = EINVAL;
475 		goto bail;
476 	}
477 
478 	/*
479 	 * Find the file entry for the root directory.
480 	 */
481 	sector = le32toh(udfmp->root_icb.loc.lb_num) + udfmp->part_start;
482 	size = le32toh(udfmp->root_icb.len);
483 	if (size < UDF_FENTRY_SIZE) {
484 		printf("Invalid root directory file entry length %u\n",
485 		    size);
486 		goto bail;
487 	}
488 	if ((error = udf_readdevblks(udfmp, sector, size, &bp)) != 0) {
489 		printf("Cannot read sector %d\n", sector);
490 		goto bail;
491 	}
492 
493 	root_fentry = (struct file_entry *)bp->b_data;
494 	if ((error = udf_checktag(&root_fentry->tag, TAGID_FENTRY))) {
495 		printf("Invalid root file entry!\n");
496 		goto bail;
497 	}
498 
499 	brelse(bp);
500 	bp = NULL;
501 
502 	return 0;
503 
504 bail:
505 	if (udfmp != NULL)
506 		free(udfmp, M_UDFMOUNT);
507 	if (bp != NULL)
508 		brelse(bp);
509 	if (cp != NULL) {
510 		g_topology_lock();
511 		g_vfs_close(cp);
512 		g_topology_unlock();
513 	}
514 	dev_rel(dev);
515 	return error;
516 };
517 
518 static int
519 udf_unmount(struct mount *mp, int mntflags)
520 {
521 	struct udf_mnt *udfmp;
522 	int error, flags = 0;
523 
524 	udfmp = VFSTOUDFFS(mp);
525 
526 	if (mntflags & MNT_FORCE)
527 		flags |= FORCECLOSE;
528 
529 	if ((error = vflush(mp, 0, flags, curthread)))
530 		return (error);
531 
532 	if (udfmp->im_flags & UDFMNT_KICONV && udf_iconv) {
533 		if (udfmp->im_d2l)
534 			udf_iconv->close(udfmp->im_d2l);
535 #if 0
536 		if (udfmp->im_l2d)
537 			udf_iconv->close(udfmp->im_l2d);
538 #endif
539 	}
540 
541 	g_topology_lock();
542 	g_vfs_close(udfmp->im_cp);
543 	g_topology_unlock();
544 	vrele(udfmp->im_devvp);
545 	dev_rel(udfmp->im_dev);
546 
547 	if (udfmp->s_table != NULL)
548 		free(udfmp->s_table, M_UDFMOUNT);
549 
550 	free(udfmp, M_UDFMOUNT);
551 
552 	mp->mnt_data = NULL;
553 	return (0);
554 }
555 
556 static int
557 udf_root(struct mount *mp, int flags, struct vnode **vpp)
558 {
559 	struct udf_mnt *udfmp;
560 	ino_t id;
561 
562 	udfmp = VFSTOUDFFS(mp);
563 
564 	id = udf_getid(&udfmp->root_icb);
565 
566 	return (udf_vget(mp, id, flags, vpp));
567 }
568 
569 static int
570 udf_statfs(struct mount *mp, struct statfs *sbp)
571 {
572 	struct udf_mnt *udfmp;
573 
574 	udfmp = VFSTOUDFFS(mp);
575 
576 	sbp->f_bsize = udfmp->bsize;
577 	sbp->f_iosize = udfmp->bsize;
578 	sbp->f_blocks = udfmp->part_len;
579 	sbp->f_bfree = 0;
580 	sbp->f_bavail = 0;
581 	sbp->f_files = 0;
582 	sbp->f_ffree = 0;
583 	return 0;
584 }
585 
586 int
587 udf_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
588 {
589 	struct buf *bp;
590 	struct vnode *devvp;
591 	struct udf_mnt *udfmp;
592 	struct thread *td;
593 	struct vnode *vp;
594 	struct udf_node *unode;
595 	struct file_entry *fe;
596 	uint32_t lea, lad;
597 	int error, sector, size;
598 
599 	error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
600 	if (error || *vpp != NULL)
601 		return (error);
602 
603 	/*
604 	 * We must promote to an exclusive lock for vnode creation.  This
605 	 * can happen if lookup is passed LOCKSHARED.
606  	 */
607 	if ((flags & LK_TYPE_MASK) == LK_SHARED) {
608 		flags &= ~LK_TYPE_MASK;
609 		flags |= LK_EXCLUSIVE;
610 	}
611 
612 	/*
613 	 * We do not lock vnode creation as it is believed to be too
614 	 * expensive for such rare case as simultaneous creation of vnode
615 	 * for same ino by different processes. We just allow them to race
616 	 * and check later to decide who wins. Let the race begin!
617 	 */
618 
619 	td = curthread;
620 	udfmp = VFSTOUDFFS(mp);
621 
622 	unode = uma_zalloc(udf_zone_node, M_WAITOK | M_ZERO);
623 
624 	if ((error = udf_allocv(mp, &vp, td))) {
625 		printf("Error from udf_allocv\n");
626 		uma_zfree(udf_zone_node, unode);
627 		return (error);
628 	}
629 
630 	unode->i_vnode = vp;
631 	unode->hash_id = ino;
632 	unode->udfmp = udfmp;
633 	vp->v_data = unode;
634 
635 	lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
636 	error = insmntque(vp, mp);
637 	if (error != 0) {
638 		uma_zfree(udf_zone_node, unode);
639 		return (error);
640 	}
641 	error = vfs_hash_insert(vp, ino, flags, td, vpp, NULL, NULL);
642 	if (error || *vpp != NULL)
643 		return (error);
644 
645 	/*
646 	 * Copy in the file entry.  Per the spec, the size can only be 1 block.
647 	 */
648 	sector = ino + udfmp->part_start;
649 	devvp = udfmp->im_devvp;
650 	if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
651 		printf("Cannot read sector %d\n", sector);
652 		goto error;
653 	}
654 
655 	/*
656 	 * File entry length validation.
657 	 */
658 	fe = (struct file_entry *)bp->b_data;
659 	if (udf_checktag(&fe->tag, TAGID_FENTRY)) {
660 		printf("Invalid file entry!\n");
661 		error = ENOMEM;
662 		goto error;
663 	}
664 	lea = le32toh(fe->l_ea);
665 	lad = le32toh(fe->l_ad);
666 	if (lea > udfmp->bsize || lad > udfmp->bsize) {
667 		printf("Invalid EA and AD lengths %u, %u\n", lea, lad);
668 		error = EIO;
669 		goto error;
670 	}
671 	size = UDF_FENTRY_SIZE + lea + lad;
672 	if (size > udfmp->bsize) {
673 		printf("Invalid file entry size %u\n", size);
674 		error = EIO;
675 		goto error;
676 	}
677 
678 	unode->fentry = malloc(size, M_UDFFENTRY, M_NOWAIT | M_ZERO);
679 	if (unode->fentry == NULL) {
680 		printf("Cannot allocate file entry block\n");
681 		error = ENOMEM;
682 		goto error;
683 	}
684 
685 	bcopy(bp->b_data, unode->fentry, size);
686 
687 	brelse(bp);
688 	bp = NULL;
689 
690 	switch (unode->fentry->icbtag.file_type) {
691 	default:
692 		vp->v_type = VBAD;
693 		break;
694 	case 4:
695 		vp->v_type = VDIR;
696 		break;
697 	case 5:
698 		vp->v_type = VREG;
699 		break;
700 	case 6:
701 		vp->v_type = VBLK;
702 		break;
703 	case 7:
704 		vp->v_type = VCHR;
705 		break;
706 	case 9:
707 		vp->v_type = VFIFO;
708 		vp->v_op = &udf_fifoops;
709 		break;
710 	case 10:
711 		vp->v_type = VSOCK;
712 		break;
713 	case 12:
714 		vp->v_type = VLNK;
715 		break;
716 	}
717 
718 	if (vp->v_type != VFIFO)
719 		VN_LOCK_ASHARE(vp);
720 
721 	if (ino == udf_getid(&udfmp->root_icb))
722 		vp->v_vflag |= VV_ROOT;
723 
724 	vn_set_state(vp, VSTATE_CONSTRUCTED);
725 	*vpp = vp;
726 
727 	return (0);
728 
729 error:
730 	vgone(vp);
731 	vput(vp);
732 	brelse(bp);
733 	*vpp = NULL;
734 	return (error);
735 }
736 
737 static int
738 udf_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
739 {
740 	struct ifid *ifhp;
741 	struct vnode *nvp;
742 	struct udf_node *np;
743 	off_t fsize;
744 	int error;
745 
746 	ifhp = (struct ifid *)fhp;
747 
748 	if ((error = VFS_VGET(mp, ifhp->ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
749 		*vpp = NULLVP;
750 		return (error);
751 	}
752 
753 	np = VTON(nvp);
754 	fsize = le64toh(np->fentry->inf_len);
755 
756 	*vpp = nvp;
757 	vnode_create_vobject(*vpp, fsize, curthread);
758 	return (0);
759 }
760 
761 static int
762 udf_find_partmaps(struct udf_mnt *udfmp, struct logvol_desc *lvd)
763 {
764 	struct part_map_spare *pms;
765 	struct regid *pmap_id;
766 	struct buf *bp;
767 	unsigned char regid_id[UDF_REGID_ID_SIZE + 1];
768 	int i, k, ptype, psize, error;
769 	uint8_t *pmap = (uint8_t *) &lvd->maps[0];
770 
771 	for (i = 0; i < le32toh(lvd->n_pm); i++) {
772 		ptype = pmap[0];
773 		psize = pmap[1];
774 		if (((ptype != 1) && (ptype != 2)) ||
775 		    ((psize != UDF_PMAP_TYPE1_SIZE) &&
776 		     (psize != UDF_PMAP_TYPE2_SIZE))) {
777 			printf("Invalid partition map found\n");
778 			return (1);
779 		}
780 
781 		if (ptype == 1) {
782 			/* Type 1 map.  We don't care */
783 			pmap += UDF_PMAP_TYPE1_SIZE;
784 			continue;
785 		}
786 
787 		/* Type 2 map.  Gotta find out the details */
788 		pmap_id = (struct regid *)&pmap[4];
789 		bzero(&regid_id[0], UDF_REGID_ID_SIZE);
790 		bcopy(&pmap_id->id[0], &regid_id[0], UDF_REGID_ID_SIZE);
791 
792 		if (bcmp(&regid_id[0], "*UDF Sparable Partition",
793 		    UDF_REGID_ID_SIZE)) {
794 			printf("Unsupported partition map: %s\n", &regid_id[0]);
795 			return (1);
796 		}
797 
798 		pms = (struct part_map_spare *)pmap;
799 		pmap += UDF_PMAP_TYPE2_SIZE;
800 		udfmp->s_table = malloc(le32toh(pms->st_size),
801 		    M_UDFMOUNT, M_NOWAIT | M_ZERO);
802 		if (udfmp->s_table == NULL)
803 			return (ENOMEM);
804 
805 		/* Calculate the number of sectors per packet. */
806 		/* XXX Logical or physical? */
807 		udfmp->p_sectors = le16toh(pms->packet_len) / udfmp->bsize;
808 
809 		/*
810 		 * XXX If reading the first Sparing Table fails, should look
811 		 * for another table.
812 		 */
813 		if ((error = udf_readdevblks(udfmp, le32toh(pms->st_loc[0]),
814 					   le32toh(pms->st_size), &bp)) != 0) {
815 			if (bp != NULL)
816 				brelse(bp);
817 			printf("Failed to read Sparing Table at sector %d\n",
818 			    le32toh(pms->st_loc[0]));
819 			free(udfmp->s_table, M_UDFMOUNT);
820 			return (error);
821 		}
822 		bcopy(bp->b_data, udfmp->s_table, le32toh(pms->st_size));
823 		brelse(bp);
824 
825 		if (udf_checktag(&udfmp->s_table->tag, 0)) {
826 			printf("Invalid sparing table found\n");
827 			free(udfmp->s_table, M_UDFMOUNT);
828 			return (EINVAL);
829 		}
830 
831 		/* See how many valid entries there are here.  The list is
832 		 * supposed to be sorted. 0xfffffff0 and higher are not valid
833 		 */
834 		for (k = 0; k < le16toh(udfmp->s_table->rt_l); k++) {
835 			udfmp->s_table_entries = k;
836 			if (le32toh(udfmp->s_table->entries[k].org) >=
837 			    0xfffffff0)
838 				break;
839 		}
840 	}
841 
842 	return (0);
843 }
844