xref: /freebsd/sys/fs/udf/udf_vfsops.c (revision 8957464be7644ab4da9559ab479c9a996fbcbd29)
1 /*-
2  * Copyright (c) 2001, 2002 Scott Long <scottl@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
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, struct thread *td)
190 {
191 	struct vnode *devvp;	/* vnode of the mount device */
192 	struct udf_mnt *imp = 0;
193 	struct vfsoptlist *opts;
194 	char *fspec, *cs_disk, *cs_local;
195 	int error, len, *udf_flags;
196 	struct nameidata nd, *ndp = &nd;
197 
198 	opts = mp->mnt_optnew;
199 
200 	/*
201 	 * Unconditionally mount as read-only.
202 	 */
203 	MNT_ILOCK(mp);
204 	mp->mnt_flag |= MNT_RDONLY;
205 	MNT_IUNLOCK(mp);
206 
207 	/*
208 	 * No root filesystem support.  Probably not a big deal, since the
209 	 * bootloader doesn't understand UDF.
210 	 */
211 	if (mp->mnt_flag & MNT_ROOTFS)
212 		return (ENOTSUP);
213 
214 	fspec = NULL;
215 	error = vfs_getopt(opts, "from", (void **)&fspec, &len);
216 	if (!error && fspec[len - 1] != '\0')
217 		return (EINVAL);
218 
219 	if (mp->mnt_flag & MNT_UPDATE) {
220 		return (0);
221 	}
222 
223 	/* Check that the mount device exists */
224 	if (fspec == NULL)
225 		return (EINVAL);
226 	NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
227 	if ((error = namei(ndp)))
228 		return (error);
229 	NDFREE(ndp, NDF_ONLY_PNBUF);
230 	devvp = ndp->ni_vp;
231 
232 	if (vn_isdisk(devvp, &error) == 0) {
233 		vput(devvp);
234 		return (error);
235 	}
236 
237 	/* Check the access rights on the mount device */
238 	error = VOP_ACCESS(devvp, VREAD, td->td_ucred, td);
239 	if (error)
240 		error = priv_check(td, PRIV_VFS_MOUNT_PERM);
241 	if (error) {
242 		vput(devvp);
243 		return (error);
244 	}
245 
246 	if ((error = udf_mountfs(devvp, mp))) {
247 		vrele(devvp);
248 		return (error);
249 	}
250 
251 	imp = VFSTOUDFFS(mp);
252 
253 	udf_flags = NULL;
254 	error = vfs_getopt(opts, "flags", (void **)&udf_flags, &len);
255 	if (error || len != sizeof(int))
256 		return (EINVAL);
257 	imp->im_flags = *udf_flags;
258 
259 	if (imp->im_flags & UDFMNT_KICONV && udf_iconv) {
260 		cs_disk = NULL;
261 		error = vfs_getopt(opts, "cs_disk", (void **)&cs_disk, &len);
262 		if (!error && cs_disk[len - 1] != '\0')
263 			return (EINVAL);
264 		cs_local = NULL;
265 		error = vfs_getopt(opts, "cs_local", (void **)&cs_local, &len);
266 		if (!error && cs_local[len - 1] != '\0')
267 			return (EINVAL);
268 		udf_iconv->open(cs_local, cs_disk, &imp->im_d2l);
269 #if 0
270 		udf_iconv->open(cs_disk, cs_local, &imp->im_l2d);
271 #endif
272 	}
273 
274 	vfs_mountedfrom(mp, fspec);
275 	return 0;
276 };
277 
278 /*
279  * Check the descriptor tag for both the correct id and correct checksum.
280  * Return zero if all is good, EINVAL if not.
281  */
282 int
283 udf_checktag(struct desc_tag *tag, uint16_t id)
284 {
285 	uint8_t *itag;
286 	uint8_t i, cksum = 0;
287 
288 	itag = (uint8_t *)tag;
289 
290 	if (le16toh(tag->id) != id)
291 		return (EINVAL);
292 
293 	for (i = 0; i < 16; i++)
294 		cksum = cksum + itag[i];
295 	cksum = cksum - itag[4];
296 
297 	if (cksum == tag->cksum)
298 		return (0);
299 
300 	return (EINVAL);
301 }
302 
303 static int
304 udf_mountfs(struct vnode *devvp, struct mount *mp)
305 {
306 	struct buf *bp = NULL;
307 	struct cdev *dev;
308 	struct anchor_vdp avdp;
309 	struct udf_mnt *udfmp = NULL;
310 	struct part_desc *pd;
311 	struct logvol_desc *lvd;
312 	struct fileset_desc *fsd;
313 	struct file_entry *root_fentry;
314 	uint32_t sector, size, mvds_start, mvds_end;
315 	uint32_t logical_secsize;
316 	uint32_t fsd_offset = 0;
317 	uint16_t part_num = 0, fsd_part = 0;
318 	int error = EINVAL;
319 	int logvol_found = 0, part_found = 0, fsd_found = 0;
320 	int bsize;
321 	struct g_consumer *cp;
322 	struct bufobj *bo;
323 
324 	dev = devvp->v_rdev;
325 	dev_ref(dev);
326 	DROP_GIANT();
327 	g_topology_lock();
328 	error = g_vfs_open(devvp, &cp, "udf", 0);
329 	g_topology_unlock();
330 	PICKUP_GIANT();
331 	VOP_UNLOCK(devvp, 0);
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_MPSAFE | MNTK_LOOKUP_SHARED |
357 	    MNTK_EXTENDED_SHARED;
358 	MNT_IUNLOCK(mp);
359 	udfmp->im_mountp = mp;
360 	udfmp->im_dev = dev;
361 	udfmp->im_devvp = devvp;
362 	udfmp->im_d2l = NULL;
363 	udfmp->im_cp = cp;
364 	udfmp->im_bo = bo;
365 
366 #if 0
367 	udfmp->im_l2d = NULL;
368 #endif
369 	/*
370 	 * The UDF specification defines a logical sectorsize of 2048
371 	 * for DVD media.
372 	 */
373 	logical_secsize = 2048;
374 
375 	if (((logical_secsize % cp->provider->sectorsize) != 0) ||
376 	    (logical_secsize < cp->provider->sectorsize)) {
377 		error = EINVAL;
378 		goto bail;
379 	}
380 
381 	bsize = cp->provider->sectorsize;
382 
383 	/*
384 	 * Get the Anchor Volume Descriptor Pointer from sector 256.
385 	 * XXX Should also check sector n - 256, n, and 512.
386 	 */
387 	sector = 256;
388 	if ((error = bread(devvp, sector * btodb(logical_secsize), bsize,
389 			   NOCRED, &bp)) != 0)
390 		goto bail;
391 	if ((error = udf_checktag((struct desc_tag *)bp->b_data, TAGID_ANCHOR)))
392 		goto bail;
393 
394 	bcopy(bp->b_data, &avdp, sizeof(struct anchor_vdp));
395 	brelse(bp);
396 	bp = NULL;
397 
398 	/*
399 	 * Extract the Partition Descriptor and Logical Volume Descriptor
400 	 * from the Volume Descriptor Sequence.
401 	 * XXX Should we care about the partition type right now?
402 	 * XXX What about multiple partitions?
403 	 */
404 	mvds_start = le32toh(avdp.main_vds_ex.loc);
405 	mvds_end = mvds_start + (le32toh(avdp.main_vds_ex.len) - 1) / bsize;
406 	for (sector = mvds_start; sector < mvds_end; sector++) {
407 		if ((error = bread(devvp, sector * btodb(logical_secsize),
408 				   bsize, NOCRED, &bp)) != 0) {
409 			printf("Can't read sector %d of VDS\n", sector);
410 			goto bail;
411 		}
412 		lvd = (struct logvol_desc *)bp->b_data;
413 		if (!udf_checktag(&lvd->tag, TAGID_LOGVOL)) {
414 			udfmp->bsize = le32toh(lvd->lb_size);
415 			udfmp->bmask = udfmp->bsize - 1;
416 			udfmp->bshift = ffs(udfmp->bsize) - 1;
417 			fsd_part = le16toh(lvd->_lvd_use.fsd_loc.loc.part_num);
418 			fsd_offset = le32toh(lvd->_lvd_use.fsd_loc.loc.lb_num);
419 			if (udf_find_partmaps(udfmp, lvd))
420 				break;
421 			logvol_found = 1;
422 		}
423 		pd = (struct part_desc *)bp->b_data;
424 		if (!udf_checktag(&pd->tag, TAGID_PARTITION)) {
425 			part_found = 1;
426 			part_num = le16toh(pd->part_num);
427 			udfmp->part_len = le32toh(pd->part_len);
428 			udfmp->part_start = le32toh(pd->start_loc);
429 		}
430 
431 		brelse(bp);
432 		bp = NULL;
433 		if ((part_found) && (logvol_found))
434 			break;
435 	}
436 
437 	if (!part_found || !logvol_found) {
438 		error = EINVAL;
439 		goto bail;
440 	}
441 
442 	if (fsd_part != part_num) {
443 		printf("FSD does not lie within the partition!\n");
444 		error = EINVAL;
445 		goto bail;
446 	}
447 
448 
449 	/*
450 	 * Grab the Fileset Descriptor
451 	 * Thanks to Chuck McCrobie <mccrobie@cablespeed.com> for pointing
452 	 * me in the right direction here.
453 	 */
454 	sector = udfmp->part_start + fsd_offset;
455 	if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
456 		printf("Cannot read sector %d of FSD\n", sector);
457 		goto bail;
458 	}
459 	fsd = (struct fileset_desc *)bp->b_data;
460 	if (!udf_checktag(&fsd->tag, TAGID_FSD)) {
461 		fsd_found = 1;
462 		bcopy(&fsd->rootdir_icb, &udfmp->root_icb,
463 		    sizeof(struct long_ad));
464 	}
465 
466 	brelse(bp);
467 	bp = NULL;
468 
469 	if (!fsd_found) {
470 		printf("Couldn't find the fsd\n");
471 		error = EINVAL;
472 		goto bail;
473 	}
474 
475 	/*
476 	 * Find the file entry for the root directory.
477 	 */
478 	sector = le32toh(udfmp->root_icb.loc.lb_num) + udfmp->part_start;
479 	size = le32toh(udfmp->root_icb.len);
480 	if ((error = udf_readdevblks(udfmp, sector, size, &bp)) != 0) {
481 		printf("Cannot read sector %d\n", sector);
482 		goto bail;
483 	}
484 
485 	root_fentry = (struct file_entry *)bp->b_data;
486 	if ((error = udf_checktag(&root_fentry->tag, TAGID_FENTRY))) {
487 		printf("Invalid root file entry!\n");
488 		goto bail;
489 	}
490 
491 	brelse(bp);
492 	bp = NULL;
493 
494 	return 0;
495 
496 bail:
497 	if (udfmp != NULL)
498 		free(udfmp, M_UDFMOUNT);
499 	if (bp != NULL)
500 		brelse(bp);
501 	if (cp != NULL) {
502 		DROP_GIANT();
503 		g_topology_lock();
504 		g_vfs_close(cp);
505 		g_topology_unlock();
506 		PICKUP_GIANT();
507 	}
508 	dev_rel(dev);
509 	return error;
510 };
511 
512 static int
513 udf_unmount(struct mount *mp, int mntflags, struct thread *td)
514 {
515 	struct udf_mnt *udfmp;
516 	int error, flags = 0;
517 
518 	udfmp = VFSTOUDFFS(mp);
519 
520 	if (mntflags & MNT_FORCE)
521 		flags |= FORCECLOSE;
522 
523 	if ((error = vflush(mp, 0, flags, td)))
524 		return (error);
525 
526 	if (udfmp->im_flags & UDFMNT_KICONV && udf_iconv) {
527 		if (udfmp->im_d2l)
528 			udf_iconv->close(udfmp->im_d2l);
529 #if 0
530 		if (udfmp->im_l2d)
531 			udf_iconv->close(udfmp->im_l2d);
532 #endif
533 	}
534 
535 	DROP_GIANT();
536 	g_topology_lock();
537 	g_vfs_close(udfmp->im_cp);
538 	g_topology_unlock();
539 	PICKUP_GIANT();
540 	vrele(udfmp->im_devvp);
541 	dev_rel(udfmp->im_dev);
542 
543 	if (udfmp->s_table != NULL)
544 		free(udfmp->s_table, M_UDFMOUNT);
545 
546 	free(udfmp, M_UDFMOUNT);
547 
548 	mp->mnt_data = NULL;
549 	MNT_ILOCK(mp);
550 	mp->mnt_flag &= ~MNT_LOCAL;
551 	MNT_IUNLOCK(mp);
552 
553 	return (0);
554 }
555 
556 static int
557 udf_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
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, struct thread *td)
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 	int error, sector, size;
597 
598 	error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
599 	if (error || *vpp != NULL)
600 		return (error);
601 
602 	/*
603 	 * We must promote to an exclusive lock for vnode creation.  This
604 	 * can happen if lookup is passed LOCKSHARED.
605  	 */
606 	if ((flags & LK_TYPE_MASK) == LK_SHARED) {
607 		flags &= ~LK_TYPE_MASK;
608 		flags |= LK_EXCLUSIVE;
609 	}
610 
611 	/*
612 	 * We do not lock vnode creation as it is believed to be too
613 	 * expensive for such rare case as simultaneous creation of vnode
614 	 * for same ino by different processes. We just allow them to race
615 	 * and check later to decide who wins. Let the race begin!
616 	 */
617 
618 	td = curthread;
619 	udfmp = VFSTOUDFFS(mp);
620 
621 	unode = uma_zalloc(udf_zone_node, M_WAITOK | M_ZERO);
622 
623 	if ((error = udf_allocv(mp, &vp, td))) {
624 		printf("Error from udf_allocv\n");
625 		uma_zfree(udf_zone_node, unode);
626 		return (error);
627 	}
628 
629 	unode->i_vnode = vp;
630 	unode->hash_id = ino;
631 	unode->udfmp = udfmp;
632 	vp->v_data = unode;
633 
634 	lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
635 	error = insmntque(vp, mp);
636 	if (error != 0) {
637 		uma_zfree(udf_zone_node, unode);
638 		return (error);
639 	}
640 	error = vfs_hash_insert(vp, ino, flags, td, vpp, NULL, NULL);
641 	if (error || *vpp != NULL)
642 		return (error);
643 
644 	/*
645 	 * Copy in the file entry.  Per the spec, the size can only be 1 block.
646 	 */
647 	sector = ino + udfmp->part_start;
648 	devvp = udfmp->im_devvp;
649 	if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
650 		printf("Cannot read sector %d\n", sector);
651 		vgone(vp);
652 		vput(vp);
653 		brelse(bp);
654 		*vpp = NULL;
655 		return (error);
656 	}
657 
658 	fe = (struct file_entry *)bp->b_data;
659 	if (udf_checktag(&fe->tag, TAGID_FENTRY)) {
660 		printf("Invalid file entry!\n");
661 		vgone(vp);
662 		vput(vp);
663 		brelse(bp);
664 		*vpp = NULL;
665 		return (ENOMEM);
666 	}
667 	size = UDF_FENTRY_SIZE + le32toh(fe->l_ea) + le32toh(fe->l_ad);
668 	unode->fentry = malloc(size, M_UDFFENTRY, M_NOWAIT | M_ZERO);
669 	if (unode->fentry == NULL) {
670 		printf("Cannot allocate file entry block\n");
671 		vgone(vp);
672 		vput(vp);
673 		brelse(bp);
674 		*vpp = NULL;
675 		return (ENOMEM);
676 	}
677 
678 	bcopy(bp->b_data, unode->fentry, size);
679 
680 	brelse(bp);
681 	bp = NULL;
682 
683 	switch (unode->fentry->icbtag.file_type) {
684 	default:
685 		vp->v_type = VBAD;
686 		break;
687 	case 4:
688 		vp->v_type = VDIR;
689 		break;
690 	case 5:
691 		vp->v_type = VREG;
692 		break;
693 	case 6:
694 		vp->v_type = VBLK;
695 		break;
696 	case 7:
697 		vp->v_type = VCHR;
698 		break;
699 	case 9:
700 		vp->v_type = VFIFO;
701 		vp->v_op = &udf_fifoops;
702 		break;
703 	case 10:
704 		vp->v_type = VSOCK;
705 		break;
706 	case 12:
707 		vp->v_type = VLNK;
708 		break;
709 	}
710 
711 	if (vp->v_type != VFIFO)
712 		VN_LOCK_ASHARE(vp);
713 
714 	if (ino == udf_getid(&udfmp->root_icb))
715 		vp->v_vflag |= VV_ROOT;
716 
717 	*vpp = vp;
718 
719 	return (0);
720 }
721 
722 static int
723 udf_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
724 {
725 	struct ifid *ifhp;
726 	struct vnode *nvp;
727 	struct udf_node *np;
728 	off_t fsize;
729 	int error;
730 
731 	ifhp = (struct ifid *)fhp;
732 
733 	if ((error = VFS_VGET(mp, ifhp->ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
734 		*vpp = NULLVP;
735 		return (error);
736 	}
737 
738 	np = VTON(nvp);
739 	fsize = le64toh(np->fentry->inf_len);
740 
741 	*vpp = nvp;
742 	vnode_create_vobject(*vpp, fsize, curthread);
743 	return (0);
744 }
745 
746 static int
747 udf_find_partmaps(struct udf_mnt *udfmp, struct logvol_desc *lvd)
748 {
749 	struct part_map_spare *pms;
750 	struct regid *pmap_id;
751 	struct buf *bp;
752 	unsigned char regid_id[UDF_REGID_ID_SIZE + 1];
753 	int i, k, ptype, psize, error;
754 	uint8_t *pmap = (uint8_t *) &lvd->maps[0];
755 
756 	for (i = 0; i < le32toh(lvd->n_pm); i++) {
757 		ptype = pmap[0];
758 		psize = pmap[1];
759 		if (((ptype != 1) && (ptype != 2)) ||
760 		    ((psize != UDF_PMAP_TYPE1_SIZE) &&
761 		     (psize != UDF_PMAP_TYPE2_SIZE))) {
762 			printf("Invalid partition map found\n");
763 			return (1);
764 		}
765 
766 		if (ptype == 1) {
767 			/* Type 1 map.  We don't care */
768 			pmap += UDF_PMAP_TYPE1_SIZE;
769 			continue;
770 		}
771 
772 		/* Type 2 map.  Gotta find out the details */
773 		pmap_id = (struct regid *)&pmap[4];
774 		bzero(&regid_id[0], UDF_REGID_ID_SIZE);
775 		bcopy(&pmap_id->id[0], &regid_id[0], UDF_REGID_ID_SIZE);
776 
777 		if (bcmp(&regid_id[0], "*UDF Sparable Partition",
778 		    UDF_REGID_ID_SIZE)) {
779 			printf("Unsupported partition map: %s\n", &regid_id[0]);
780 			return (1);
781 		}
782 
783 		pms = (struct part_map_spare *)pmap;
784 		pmap += UDF_PMAP_TYPE2_SIZE;
785 		udfmp->s_table = malloc(le32toh(pms->st_size),
786 		    M_UDFMOUNT, M_NOWAIT | M_ZERO);
787 		if (udfmp->s_table == NULL)
788 			return (ENOMEM);
789 
790 		/* Calculate the number of sectors per packet. */
791 		/* XXX Logical or physical? */
792 		udfmp->p_sectors = le16toh(pms->packet_len) / udfmp->bsize;
793 
794 		/*
795 		 * XXX If reading the first Sparing Table fails, should look
796 		 * for another table.
797 		 */
798 		if ((error = udf_readdevblks(udfmp, le32toh(pms->st_loc[0]),
799 					   le32toh(pms->st_size), &bp)) != 0) {
800 			if (bp != NULL)
801 				brelse(bp);
802 			printf("Failed to read Sparing Table at sector %d\n",
803 			    le32toh(pms->st_loc[0]));
804 			free(udfmp->s_table, M_UDFMOUNT);
805 			return (error);
806 		}
807 		bcopy(bp->b_data, udfmp->s_table, le32toh(pms->st_size));
808 		brelse(bp);
809 
810 		if (udf_checktag(&udfmp->s_table->tag, 0)) {
811 			printf("Invalid sparing table found\n");
812 			free(udfmp->s_table, M_UDFMOUNT);
813 			return (EINVAL);
814 		}
815 
816 		/* See how many valid entries there are here.  The list is
817 		 * supposed to be sorted. 0xfffffff0 and higher are not valid
818 		 */
819 		for (k = 0; k < le16toh(udfmp->s_table->rt_l); k++) {
820 			udfmp->s_table_entries = k;
821 			if (le32toh(udfmp->s_table->entries[k].org) >=
822 			    0xfffffff0)
823 				break;
824 		}
825 	}
826 
827 	return (0);
828 }
829