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