xref: /freebsd/sys/fs/udf/udf_vfsops.c (revision 8847579c57d6aff2b3371c707dce7a2cee8389aa)
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/proc.h>
88 #include <sys/queue.h>
89 #include <sys/vnode.h>
90 #include <sys/endian.h>
91 
92 #include <geom/geom.h>
93 #include <geom/geom_vfs.h>
94 
95 #include <vm/uma.h>
96 
97 #include <fs/udf/ecma167-udf.h>
98 #include <fs/udf/osta.h>
99 #include <fs/udf/udf.h>
100 #include <fs/udf/udf_mount.h>
101 
102 static MALLOC_DEFINE(M_UDFMOUNT, "udf_mount", "UDF mount structure");
103 MALLOC_DEFINE(M_UDFFENTRY, "udf_fentry", "UDF file entry structure");
104 
105 struct iconv_functions *udf_iconv = NULL;
106 
107 /* Zones */
108 uma_zone_t udf_zone_trans = NULL;
109 uma_zone_t udf_zone_node = NULL;
110 uma_zone_t udf_zone_ds = NULL;
111 
112 static vfs_init_t      udf_init;
113 static vfs_uninit_t    udf_uninit;
114 static vfs_mount_t     udf_mount;
115 static vfs_root_t      udf_root;
116 static vfs_statfs_t    udf_statfs;
117 static vfs_unmount_t   udf_unmount;
118 static vfs_fhtovp_t	udf_fhtovp;
119 static vfs_vptofh_t	udf_vptofh;
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 	.vfs_vptofh =		udf_vptofh,
133 };
134 VFS_SET(udf_vfsops, udf, VFCF_READONLY);
135 
136 MODULE_VERSION(udf, 1);
137 
138 static int udf_mountfs(struct vnode *, struct mount *, struct thread *);
139 
140 static int
141 udf_init(struct vfsconf *foo)
142 {
143 
144 	/*
145 	 * This code used to pre-allocate a certain number of pages for each
146 	 * pool, reducing the need to grow the zones later on.  UMA doesn't
147 	 * advertise any such functionality, unfortunately =-<
148 	 */
149 	udf_zone_trans = uma_zcreate("UDF translation buffer, zone", MAXNAMLEN *
150 	    sizeof(unicode_t), NULL, NULL, NULL, NULL, 0, 0);
151 
152 	udf_zone_node = uma_zcreate("UDF Node zone", sizeof(struct udf_node),
153 	    NULL, NULL, NULL, NULL, 0, 0);
154 
155 	udf_zone_ds = uma_zcreate("UDF Dirstream zone",
156 	    sizeof(struct udf_dirstream), NULL, NULL, NULL, NULL, 0, 0);
157 
158 	if ((udf_zone_node == NULL) || (udf_zone_trans == NULL) ||
159 	    (udf_zone_ds == NULL)) {
160 		printf("Cannot create allocation zones.\n");
161 		return (ENOMEM);
162 	}
163 
164 	return 0;
165 }
166 
167 static int
168 udf_uninit(struct vfsconf *foo)
169 {
170 
171 	if (udf_zone_trans != NULL) {
172 		uma_zdestroy(udf_zone_trans);
173 		udf_zone_trans = NULL;
174 	}
175 
176 	if (udf_zone_node != NULL) {
177 		uma_zdestroy(udf_zone_node);
178 		udf_zone_node = NULL;
179 	}
180 
181 	if (udf_zone_ds != NULL) {
182 		uma_zdestroy(udf_zone_ds);
183 		udf_zone_ds = NULL;
184 	}
185 
186 	return (0);
187 }
188 
189 static int
190 udf_mount(struct mount *mp, struct thread *td)
191 {
192 	struct vnode *devvp;	/* vnode of the mount device */
193 	struct udf_mnt *imp = 0;
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 	opts = mp->mnt_optnew;
200 
201 	/*
202 	 * Unconditionally mount as read-only.
203 	 */
204 	mp->mnt_flag |= MNT_RDONLY;
205 
206 	/*
207 	 * No root filesystem support.  Probably not a big deal, since the
208 	 * bootloader doesn't understand UDF.
209 	 */
210 	if (mp->mnt_flag & MNT_ROOTFS)
211 		return (ENOTSUP);
212 
213 	fspec = NULL;
214 	error = vfs_getopt(opts, "from", (void **)&fspec, &len);
215 	if (!error && fspec[len - 1] != '\0')
216 		return (EINVAL);
217 
218 	if (mp->mnt_flag & MNT_UPDATE) {
219 		return (0);
220 	}
221 
222 	/* Check that the mount device exists */
223 	if (fspec == NULL)
224 		return (EINVAL);
225 	NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
226 	if ((error = namei(ndp)))
227 		return (error);
228 	NDFREE(ndp, NDF_ONLY_PNBUF);
229 	devvp = ndp->ni_vp;
230 
231 	if (vn_isdisk(devvp, &error) == 0) {
232 		vput(devvp);
233 		return (error);
234 	}
235 
236 	/* Check the access rights on the mount device */
237 	error = VOP_ACCESS(devvp, VREAD, td->td_ucred, td);
238 	if (error)
239 		error = suser(td);
240 	if (error) {
241 		vput(devvp);
242 		return (error);
243 	}
244 
245 	if ((error = udf_mountfs(devvp, mp, td))) {
246 		vrele(devvp);
247 		return (error);
248 	}
249 
250 	imp = VFSTOUDFFS(mp);
251 
252 	udf_flags = NULL;
253 	error = vfs_getopt(opts, "flags", (void **)&udf_flags, &len);
254 	if (error || len != sizeof(int))
255 		return (EINVAL);
256 	imp->im_flags = *udf_flags;
257 
258 	if (imp->im_flags & UDFMNT_KICONV && udf_iconv) {
259 		cs_disk = NULL;
260 		error = vfs_getopt(opts, "cs_disk", (void **)&cs_disk, &len);
261 		if (!error && cs_disk[len - 1] != '\0')
262 			return (EINVAL);
263 		cs_local = NULL;
264 		error = vfs_getopt(opts, "cs_local", (void **)&cs_local, &len);
265 		if (!error && cs_local[len - 1] != '\0')
266 			return (EINVAL);
267 		udf_iconv->open(cs_local, cs_disk, &imp->im_d2l);
268 #if 0
269 		udf_iconv->open(cs_disk, cs_local, &imp->im_l2d);
270 #endif
271 	}
272 
273 	vfs_mountedfrom(mp, fspec);
274 	return 0;
275 };
276 
277 /*
278  * Check the descriptor tag for both the correct id and correct checksum.
279  * Return zero if all is good, EINVAL if not.
280  */
281 int
282 udf_checktag(struct desc_tag *tag, uint16_t id)
283 {
284 	uint8_t *itag;
285 	uint8_t i, cksum = 0;
286 
287 	itag = (uint8_t *)tag;
288 
289 	if (le16toh(tag->id) != id)
290 		return (EINVAL);
291 
292 	for (i = 0; i < 15; i++)
293 		cksum = cksum + itag[i];
294 	cksum = cksum - itag[4];
295 
296 	if (cksum == tag->cksum)
297 		return (0);
298 
299 	return (EINVAL);
300 }
301 
302 static int
303 udf_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td) {
304 	struct buf *bp = NULL;
305 	struct anchor_vdp avdp;
306 	struct udf_mnt *udfmp = NULL;
307 	struct part_desc *pd;
308 	struct logvol_desc *lvd;
309 	struct fileset_desc *fsd;
310 	struct file_entry *root_fentry;
311 	uint32_t sector, size, mvds_start, mvds_end;
312 	uint32_t logical_secsize;
313 	uint32_t fsd_offset = 0;
314 	uint16_t part_num = 0, fsd_part = 0;
315 	int error = EINVAL;
316 	int logvol_found = 0, part_found = 0, fsd_found = 0;
317 	int bsize;
318 	struct g_consumer *cp;
319 	struct bufobj *bo;
320 
321 	DROP_GIANT();
322 	g_topology_lock();
323 	error = g_vfs_open(devvp, &cp, "udf", 0);
324 	g_topology_unlock();
325 	PICKUP_GIANT();
326 	VOP_UNLOCK(devvp, 0, td);
327 	if (error)
328 		return error;
329 
330 	bo = &devvp->v_bufobj;
331 
332 	/* XXX: should be M_WAITOK */
333 	MALLOC(udfmp, struct udf_mnt *, sizeof(struct udf_mnt), M_UDFMOUNT,
334 	    M_NOWAIT | M_ZERO);
335 	if (udfmp == NULL) {
336 		printf("Cannot allocate UDF mount struct\n");
337 		error = ENOMEM;
338 		goto bail;
339 	}
340 
341 	mp->mnt_data = (qaddr_t)udfmp;
342 	mp->mnt_stat.f_fsid.val[0] = dev2udev(devvp->v_rdev);
343 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
344 	mp->mnt_flag |= MNT_LOCAL;
345 	udfmp->im_mountp = mp;
346 	udfmp->im_dev = devvp->v_rdev;
347 	udfmp->im_devvp = devvp;
348 	udfmp->im_d2l = NULL;
349 	udfmp->im_cp = cp;
350 	udfmp->im_bo = bo;
351 
352 #if 0
353 	udfmp->im_l2d = NULL;
354 #endif
355 	/*
356 	 * The UDF specification defines a logical sectorsize of 2048
357 	 * for DVD media.
358 	 */
359 	logical_secsize = 2048;
360 
361 	if (((logical_secsize % cp->provider->sectorsize) != 0) ||
362 	    (logical_secsize < cp->provider->sectorsize)) {
363 		DROP_GIANT();
364 		g_topology_lock();
365 		g_vfs_close(cp, td);
366 		g_topology_unlock();
367 		PICKUP_GIANT();
368 		return (EINVAL);
369 	}
370 
371 	bsize = cp->provider->sectorsize;
372 
373 	/*
374 	 * Get the Anchor Volume Descriptor Pointer from sector 256.
375 	 * XXX Should also check sector n - 256, n, and 512.
376 	 */
377 	sector = 256;
378 	if ((error = bread(devvp, sector * btodb(logical_secsize), bsize,
379 			   NOCRED, &bp)) != 0)
380 		goto bail;
381 	if ((error = udf_checktag((struct desc_tag *)bp->b_data, TAGID_ANCHOR)))
382 		goto bail;
383 
384 	bcopy(bp->b_data, &avdp, sizeof(struct anchor_vdp));
385 	brelse(bp);
386 	bp = NULL;
387 
388 	/*
389 	 * Extract the Partition Descriptor and Logical Volume Descriptor
390 	 * from the Volume Descriptor Sequence.
391 	 * XXX Should we care about the partition type right now?
392 	 * XXX What about multiple partitions?
393 	 */
394 	mvds_start = le32toh(avdp.main_vds_ex.loc);
395 	mvds_end = mvds_start + (le32toh(avdp.main_vds_ex.len) - 1) / bsize;
396 	for (sector = mvds_start; sector < mvds_end; sector++) {
397 		if ((error = bread(devvp, sector * btodb(logical_secsize),
398 				   bsize, NOCRED, &bp)) != 0) {
399 			printf("Can't read sector %d of VDS\n", sector);
400 			goto bail;
401 		}
402 		lvd = (struct logvol_desc *)bp->b_data;
403 		if (!udf_checktag(&lvd->tag, TAGID_LOGVOL)) {
404 			udfmp->bsize = le32toh(lvd->lb_size);
405 			udfmp->bmask = udfmp->bsize - 1;
406 			udfmp->bshift = ffs(udfmp->bsize) - 1;
407 			fsd_part = le16toh(lvd->_lvd_use.fsd_loc.loc.part_num);
408 			fsd_offset = le32toh(lvd->_lvd_use.fsd_loc.loc.lb_num);
409 			if (udf_find_partmaps(udfmp, lvd))
410 				break;
411 			logvol_found = 1;
412 		}
413 		pd = (struct part_desc *)bp->b_data;
414 		if (!udf_checktag(&pd->tag, TAGID_PARTITION)) {
415 			part_found = 1;
416 			part_num = le16toh(pd->part_num);
417 			udfmp->part_len = le32toh(pd->part_len);
418 			udfmp->part_start = le32toh(pd->start_loc);
419 		}
420 
421 		brelse(bp);
422 		bp = NULL;
423 		if ((part_found) && (logvol_found))
424 			break;
425 	}
426 
427 	if (!part_found || !logvol_found) {
428 		error = EINVAL;
429 		goto bail;
430 	}
431 
432 	if (fsd_part != part_num) {
433 		printf("FSD does not lie within the partition!\n");
434 		error = EINVAL;
435 		goto bail;
436 	}
437 
438 
439 	/*
440 	 * Grab the Fileset Descriptor
441 	 * Thanks to Chuck McCrobie <mccrobie@cablespeed.com> for pointing
442 	 * me in the right direction here.
443 	 */
444 	sector = udfmp->part_start + fsd_offset;
445 	if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
446 		printf("Cannot read sector %d of FSD\n", sector);
447 		goto bail;
448 	}
449 	fsd = (struct fileset_desc *)bp->b_data;
450 	if (!udf_checktag(&fsd->tag, TAGID_FSD)) {
451 		fsd_found = 1;
452 		bcopy(&fsd->rootdir_icb, &udfmp->root_icb,
453 		    sizeof(struct long_ad));
454 	}
455 
456 	brelse(bp);
457 	bp = NULL;
458 
459 	if (!fsd_found) {
460 		printf("Couldn't find the fsd\n");
461 		error = EINVAL;
462 		goto bail;
463 	}
464 
465 	/*
466 	 * Find the file entry for the root directory.
467 	 */
468 	sector = le32toh(udfmp->root_icb.loc.lb_num) + udfmp->part_start;
469 	size = le32toh(udfmp->root_icb.len);
470 	if ((error = udf_readlblks(udfmp, sector, size, &bp)) != 0) {
471 		printf("Cannot read sector %d\n", sector);
472 		goto bail;
473 	}
474 
475 	root_fentry = (struct file_entry *)bp->b_data;
476 	if ((error = udf_checktag(&root_fentry->tag, TAGID_FENTRY))) {
477 		printf("Invalid root file entry!\n");
478 		goto bail;
479 	}
480 
481 	brelse(bp);
482 	bp = NULL;
483 
484 	return 0;
485 
486 bail:
487 	if (udfmp != NULL)
488 		FREE(udfmp, M_UDFMOUNT);
489 	if (bp != NULL)
490 		brelse(bp);
491 	DROP_GIANT();
492 	g_topology_lock();
493 	g_vfs_close(cp, td);
494 	g_topology_unlock();
495 	PICKUP_GIANT();
496 	return error;
497 };
498 
499 static int
500 udf_unmount(struct mount *mp, int mntflags, struct thread *td)
501 {
502 	struct udf_mnt *udfmp;
503 	int error, flags = 0;
504 
505 	udfmp = VFSTOUDFFS(mp);
506 
507 	if (mntflags & MNT_FORCE)
508 		flags |= FORCECLOSE;
509 
510 	if ((error = vflush(mp, 0, flags, td)))
511 		return (error);
512 
513 	if (udfmp->im_flags & UDFMNT_KICONV && udf_iconv) {
514 		if (udfmp->im_d2l)
515 			udf_iconv->close(udfmp->im_d2l);
516 #if 0
517 		if (udfmp->im_l2d)
518 			udf_iconv->close(udfmp->im_l2d);
519 #endif
520 	}
521 
522 	DROP_GIANT();
523 	g_topology_lock();
524 	g_vfs_close(udfmp->im_cp, td);
525 	g_topology_unlock();
526 	PICKUP_GIANT();
527 	vrele(udfmp->im_devvp);
528 
529 	if (udfmp->s_table != NULL)
530 		FREE(udfmp->s_table, M_UDFMOUNT);
531 
532 	FREE(udfmp, M_UDFMOUNT);
533 
534 	mp->mnt_data = (qaddr_t)0;
535 	mp->mnt_flag &= ~MNT_LOCAL;
536 
537 	return (0);
538 }
539 
540 static int
541 udf_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
542 {
543 	struct udf_mnt *udfmp;
544 	struct vnode *vp;
545 	ino_t id;
546 	int error;
547 
548 	udfmp = VFSTOUDFFS(mp);
549 
550 	id = udf_getid(&udfmp->root_icb);
551 
552 	error = udf_vget(mp, id, LK_EXCLUSIVE, vpp);
553 	if (error)
554 		return error;
555 
556 	vp = *vpp;
557 	vp->v_vflag |= VV_ROOT;
558 
559 	return (0);
560 }
561 
562 static int
563 udf_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
564 {
565 	struct udf_mnt *udfmp;
566 
567 	udfmp = VFSTOUDFFS(mp);
568 
569 	sbp->f_bsize = udfmp->bsize;
570 	sbp->f_iosize = udfmp->bsize;
571 	sbp->f_blocks = udfmp->part_len;
572 	sbp->f_bfree = 0;
573 	sbp->f_bavail = 0;
574 	sbp->f_files = 0;
575 	sbp->f_ffree = 0;
576 	return 0;
577 }
578 
579 int
580 udf_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
581 {
582 	struct buf *bp;
583 	struct vnode *devvp;
584 	struct udf_mnt *udfmp;
585 	struct thread *td;
586 	struct vnode *vp;
587 	struct udf_node *unode;
588 	struct file_entry *fe;
589 	int error, sector, size;
590 
591 	error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
592 	if (error || *vpp != NULL)
593 		return (error);
594 
595 	td = curthread;
596 	udfmp = VFSTOUDFFS(mp);
597 
598 	unode = uma_zalloc(udf_zone_node, M_WAITOK | M_ZERO);
599 
600 	if ((error = udf_allocv(mp, &vp, td))) {
601 		printf("Error from udf_allocv\n");
602 		uma_zfree(udf_zone_node, unode);
603 		return (error);
604 	}
605 
606 	unode->i_vnode = vp;
607 	unode->hash_id = ino;
608 	unode->udfmp = udfmp;
609 	vp->v_data = unode;
610 
611 	error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
612 	if (error || *vpp != NULL)
613 		return (error);
614 
615 	/*
616 	 * Copy in the file entry.  Per the spec, the size can only be 1 block.
617 	 */
618 	sector = ino + udfmp->part_start;
619 	devvp = udfmp->im_devvp;
620 	if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
621 		printf("Cannot read sector %d\n", sector);
622 		vput(vp);
623 		brelse(bp);
624 		*vpp = NULL;
625 		return (error);
626 	}
627 
628 	fe = (struct file_entry *)bp->b_data;
629 	if (udf_checktag(&fe->tag, TAGID_FENTRY)) {
630 		printf("Invalid file entry!\n");
631 		vput(vp);
632 		brelse(bp);
633 		*vpp = NULL;
634 		return (ENOMEM);
635 	}
636 	size = UDF_FENTRY_SIZE + le32toh(fe->l_ea) + le32toh(fe->l_ad);
637 	MALLOC(unode->fentry, struct file_entry *, size, M_UDFFENTRY,
638 	    M_NOWAIT | M_ZERO);
639 	if (unode->fentry == NULL) {
640 		printf("Cannot allocate file entry block\n");
641 		vput(vp);
642 		brelse(bp);
643 		*vpp = NULL;
644 		return (ENOMEM);
645 	}
646 
647 	bcopy(bp->b_data, unode->fentry, size);
648 
649 	brelse(bp);
650 	bp = NULL;
651 
652 	switch (unode->fentry->icbtag.file_type) {
653 	default:
654 		vp->v_type = VBAD;
655 		break;
656 	case 4:
657 		vp->v_type = VDIR;
658 		break;
659 	case 5:
660 		vp->v_type = VREG;
661 		break;
662 	case 6:
663 		vp->v_type = VBLK;
664 		break;
665 	case 7:
666 		vp->v_type = VCHR;
667 		break;
668 	case 9:
669 		vp->v_type = VFIFO;
670 		break;
671 	case 10:
672 		vp->v_type = VSOCK;
673 		break;
674 	case 12:
675 		vp->v_type = VLNK;
676 		break;
677 	}
678 	*vpp = vp;
679 
680 	return (0);
681 }
682 
683 struct ifid {
684 	u_short	ifid_len;
685 	u_short	ifid_pad;
686 	int	ifid_ino;
687 	long	ifid_start;
688 };
689 
690 static int
691 udf_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
692 {
693 	struct ifid *ifhp;
694 	struct vnode *nvp;
695 	int error;
696 
697 	ifhp = (struct ifid *)fhp;
698 
699 	if ((error = VFS_VGET(mp, ifhp->ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
700 		*vpp = NULLVP;
701 		return (error);
702 	}
703 
704 	*vpp = nvp;
705 	vnode_create_vobject(*vpp, 0, curthread);
706 	return (0);
707 }
708 
709 static int
710 udf_vptofh (struct vnode *vp, struct fid *fhp)
711 {
712 	struct udf_node *node;
713 	struct ifid *ifhp;
714 
715 	node = VTON(vp);
716 	ifhp = (struct ifid *)fhp;
717 	ifhp->ifid_len = sizeof(struct ifid);
718 	ifhp->ifid_ino = node->hash_id;
719 
720 	return (0);
721 }
722 
723 static int
724 udf_find_partmaps(struct udf_mnt *udfmp, struct logvol_desc *lvd)
725 {
726 	union udf_pmap *pmap;
727 	struct part_map_spare *pms;
728 	struct regid *pmap_id;
729 	struct buf *bp;
730 	unsigned char regid_id[UDF_REGID_ID_SIZE + 1];
731 	int i, k, ptype, psize, error;
732 
733 	for (i = 0; i < le32toh(lvd->n_pm); i++) {
734 		pmap = (union udf_pmap *)&lvd->maps[i * UDF_PMAP_SIZE];
735 		ptype = pmap->data[0];
736 		psize = pmap->data[1];
737 		if (((ptype != 1) && (ptype != 2)) ||
738 		    ((psize != UDF_PMAP_SIZE) && (psize != 6))) {
739 			printf("Invalid partition map found\n");
740 			return (1);
741 		}
742 
743 		if (ptype == 1) {
744 			/* Type 1 map.  We don't care */
745 			continue;
746 		}
747 
748 		/* Type 2 map.  Gotta find out the details */
749 		pmap_id = (struct regid *)&pmap->data[4];
750 		bzero(&regid_id[0], UDF_REGID_ID_SIZE);
751 		bcopy(&pmap_id->id[0], &regid_id[0], UDF_REGID_ID_SIZE);
752 
753 		if (bcmp(&regid_id[0], "*UDF Sparable Partition",
754 		    UDF_REGID_ID_SIZE)) {
755 			printf("Unsupported partition map: %s\n", &regid_id[0]);
756 			return (1);
757 		}
758 
759 		pms = &pmap->pms;
760 		MALLOC(udfmp->s_table, struct udf_sparing_table *,
761 		    le32toh(pms->st_size), M_UDFMOUNT, M_NOWAIT | M_ZERO);
762 		if (udfmp->s_table == NULL)
763 			return (ENOMEM);
764 
765 		/* Calculate the number of sectors per packet. */
766 		/* XXX Logical or physical? */
767 		udfmp->p_sectors = le16toh(pms->packet_len) / udfmp->bsize;
768 
769 		/*
770 		 * XXX If reading the first Sparing Table fails, should look
771 		 * for another table.
772 		 */
773 		if ((error = udf_readlblks(udfmp, le32toh(pms->st_loc[0]),
774 					   le32toh(pms->st_size), &bp)) != 0) {
775 			if (bp != NULL)
776 				brelse(bp);
777 			printf("Failed to read Sparing Table at sector %d\n",
778 			    le32toh(pms->st_loc[0]));
779 			FREE(udfmp->s_table, M_UDFMOUNT);
780 			return (error);
781 		}
782 		bcopy(bp->b_data, udfmp->s_table, le32toh(pms->st_size));
783 		brelse(bp);
784 
785 		if (udf_checktag(&udfmp->s_table->tag, 0)) {
786 			printf("Invalid sparing table found\n");
787 			FREE(udfmp->s_table, M_UDFMOUNT);
788 			return (EINVAL);
789 		}
790 
791 		/* See how many valid entries there are here.  The list is
792 		 * supposed to be sorted. 0xfffffff0 and higher are not valid
793 		 */
794 		for (k = 0; k < le16toh(udfmp->s_table->rt_l); k++) {
795 			udfmp->s_table_entries = k;
796 			if (le32toh(udfmp->s_table->entries[k].org) >=
797 			    0xfffffff0)
798 				break;
799 		}
800 	}
801 
802 	return (0);
803 }
804