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