xref: /freebsd/sys/fs/udf/udf_vfsops.c (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
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 	mtx_init(&udfmp->hash_mtx, "udf_hash", NULL, MTX_DEF);
478 	udfmp->hashtbl = phashinit(UDF_HASHTBLSIZE, M_UDFMOUNT, &udfmp->hashsz);
479 
480 	return 0;
481 
482 bail:
483 	if (udfmp != NULL)
484 		FREE(udfmp, M_UDFMOUNT);
485 	if (bp != NULL)
486 		brelse(bp);
487 	DROP_GIANT();
488 	g_topology_lock();
489 	g_vfs_close(cp, td);
490 	g_topology_unlock();
491 	PICKUP_GIANT();
492 	return error;
493 };
494 
495 static int
496 udf_unmount(struct mount *mp, int mntflags, struct thread *td)
497 {
498 	struct udf_mnt *udfmp;
499 	int error, flags = 0;
500 
501 	udfmp = VFSTOUDFFS(mp);
502 
503 	if (mntflags & MNT_FORCE)
504 		flags |= FORCECLOSE;
505 
506 	if ((error = vflush(mp, 0, flags, td)))
507 		return (error);
508 
509 	if (udfmp->im_flags & UDFMNT_KICONV && udf_iconv) {
510 		if (udfmp->im_d2l)
511 			udf_iconv->close(udfmp->im_d2l);
512 #if 0
513 		if (udfmp->im_l2d)
514 			udf_iconv->close(udfmp->im_l2d);
515 #endif
516 	}
517 
518 	g_vfs_close(udfmp->im_cp, td);
519 	vrele(udfmp->im_devvp);
520 
521 	if (udfmp->s_table != NULL)
522 		FREE(udfmp->s_table, M_UDFMOUNT);
523 
524 	if (udfmp->hashtbl != NULL)
525 		FREE(udfmp->hashtbl, M_UDFMOUNT);
526 
527 	FREE(udfmp, M_UDFMOUNT);
528 
529 	mp->mnt_data = (qaddr_t)0;
530 	mp->mnt_flag &= ~MNT_LOCAL;
531 
532 	return (0);
533 }
534 
535 static int
536 udf_root(struct mount *mp, struct vnode **vpp, struct thread *td)
537 {
538 	struct udf_mnt *udfmp;
539 	struct vnode *vp;
540 	ino_t id;
541 	int error;
542 
543 	udfmp = VFSTOUDFFS(mp);
544 
545 	id = udf_getid(&udfmp->root_icb);
546 
547 	error = udf_vget(mp, id, LK_EXCLUSIVE, vpp);
548 	if (error)
549 		return error;
550 
551 	vp = *vpp;
552 	vp->v_vflag |= VV_ROOT;
553 	udfmp->root_vp = vp;
554 
555 	return (0);
556 }
557 
558 static int
559 udf_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
560 {
561 	struct udf_mnt *udfmp;
562 
563 	udfmp = VFSTOUDFFS(mp);
564 
565 	sbp->f_bsize = udfmp->bsize;
566 	sbp->f_iosize = udfmp->bsize;
567 	sbp->f_blocks = udfmp->part_len;
568 	sbp->f_bfree = 0;
569 	sbp->f_bavail = 0;
570 	sbp->f_files = 0;
571 	sbp->f_ffree = 0;
572 	return 0;
573 }
574 
575 int
576 udf_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
577 {
578 	struct buf *bp;
579 	struct vnode *devvp;
580 	struct udf_mnt *udfmp;
581 	struct thread *td;
582 	struct vnode *vp;
583 	struct udf_node *unode;
584 	struct file_entry *fe;
585 	int error, sector, size;
586 
587 	td = curthread;
588 	udfmp = VFSTOUDFFS(mp);
589 
590 	/* See if we already have this in the cache */
591 	if ((error = udf_hashlookup(udfmp, ino, flags, vpp)) != 0)
592 		return (error);
593 	if (*vpp != NULL) {
594 		return (0);
595 	}
596 
597 	/*
598 	 * Allocate memory and check the tag id's before grabbing a new
599 	 * vnode, since it's hard to roll back if there is a problem.
600 	 */
601 	unode = uma_zalloc(udf_zone_node, M_WAITOK);
602 	if (unode == NULL) {
603 		printf("Cannot allocate udf node\n");
604 		return (ENOMEM);
605 	}
606 
607 	/*
608 	 * Copy in the file entry.  Per the spec, the size can only be 1 block.
609 	 */
610 	sector = ino + udfmp->part_start;
611 	devvp = udfmp->im_devvp;
612 	if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
613 		printf("Cannot read sector %d\n", sector);
614 		uma_zfree(udf_zone_node, unode);
615 		return (error);
616 	}
617 
618 	fe = (struct file_entry *)bp->b_data;
619 	if (udf_checktag(&fe->tag, TAGID_FENTRY)) {
620 		printf("Invalid file entry!\n");
621 		uma_zfree(udf_zone_node, unode);
622 		brelse(bp);
623 		return (ENOMEM);
624 	}
625 	size = UDF_FENTRY_SIZE + le32toh(fe->l_ea) + le32toh(fe->l_ad);
626 	MALLOC(unode->fentry, struct file_entry *, size, M_UDFFENTRY,
627 	    M_NOWAIT | M_ZERO);
628 	if (unode->fentry == NULL) {
629 		printf("Cannot allocate file entry block\n");
630 		uma_zfree(udf_zone_node, unode);
631 		brelse(bp);
632 		return (ENOMEM);
633 	}
634 
635 	bcopy(bp->b_data, unode->fentry, size);
636 
637 	brelse(bp);
638 	bp = NULL;
639 
640 	if ((error = udf_allocv(mp, &vp, td))) {
641 		printf("Error from udf_allocv\n");
642 		uma_zfree(udf_zone_node, unode);
643 		return (error);
644 	}
645 
646 	unode->i_vnode = vp;
647 	unode->hash_id = ino;
648 	unode->i_devvp = udfmp->im_devvp;
649 	unode->i_dev = udfmp->im_dev;
650 	unode->udfmp = udfmp;
651 	vp->v_data = unode;
652 	VREF(udfmp->im_devvp);
653 	udf_hashins(unode);
654 
655 	switch (unode->fentry->icbtag.file_type) {
656 	default:
657 		vp->v_type = VBAD;
658 		break;
659 	case 4:
660 		vp->v_type = VDIR;
661 		break;
662 	case 5:
663 		vp->v_type = VREG;
664 		break;
665 	case 6:
666 		vp->v_type = VBLK;
667 		break;
668 	case 7:
669 		vp->v_type = VCHR;
670 		break;
671 	case 9:
672 		vp->v_type = VFIFO;
673 		break;
674 	case 10:
675 		vp->v_type = VSOCK;
676 		break;
677 	case 12:
678 		vp->v_type = VLNK;
679 		break;
680 	}
681 	*vpp = vp;
682 
683 	return (0);
684 }
685 
686 struct ifid {
687 	u_short	ifid_len;
688 	u_short	ifid_pad;
689 	int	ifid_ino;
690 	long	ifid_start;
691 };
692 
693 static int
694 udf_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
695 {
696 	struct ifid *ifhp;
697 	struct vnode *nvp;
698 	int error;
699 
700 	ifhp = (struct ifid *)fhp;
701 
702 	if ((error = VFS_VGET(mp, ifhp->ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
703 		*vpp = NULLVP;
704 		return (error);
705 	}
706 
707 	*vpp = nvp;
708 	vnode_create_vobject(*vpp, 0, curthread);
709 	return (0);
710 }
711 
712 static int
713 udf_vptofh (struct vnode *vp, struct fid *fhp)
714 {
715 	struct udf_node *node;
716 	struct ifid *ifhp;
717 
718 	node = VTON(vp);
719 	ifhp = (struct ifid *)fhp;
720 	ifhp->ifid_len = sizeof(struct ifid);
721 	ifhp->ifid_ino = node->hash_id;
722 
723 	return (0);
724 }
725 
726 static int
727 udf_find_partmaps(struct udf_mnt *udfmp, struct logvol_desc *lvd)
728 {
729 	union udf_pmap *pmap;
730 	struct part_map_spare *pms;
731 	struct regid *pmap_id;
732 	struct buf *bp;
733 	unsigned char regid_id[UDF_REGID_ID_SIZE + 1];
734 	int i, ptype, psize, error;
735 
736 	for (i = 0; i < le32toh(lvd->n_pm); i++) {
737 		pmap = (union udf_pmap *)&lvd->maps[i * UDF_PMAP_SIZE];
738 		ptype = pmap->data[0];
739 		psize = pmap->data[1];
740 		if (((ptype != 1) && (ptype != 2)) ||
741 		    ((psize != UDF_PMAP_SIZE) && (psize != 6))) {
742 			printf("Invalid partition map found\n");
743 			return (1);
744 		}
745 
746 		if (ptype == 1) {
747 			/* Type 1 map.  We don't care */
748 			continue;
749 		}
750 
751 		/* Type 2 map.  Gotta find out the details */
752 		pmap_id = (struct regid *)&pmap->data[4];
753 		bzero(&regid_id[0], UDF_REGID_ID_SIZE);
754 		bcopy(&pmap_id->id[0], &regid_id[0], UDF_REGID_ID_SIZE);
755 
756 		if (bcmp(&regid_id[0], "*UDF Sparable Partition",
757 		    UDF_REGID_ID_SIZE)) {
758 			printf("Unsupported partition map: %s\n", &regid_id[0]);
759 			return (1);
760 		}
761 
762 		pms = &pmap->pms;
763 		MALLOC(udfmp->s_table, struct udf_sparing_table *,
764 		    le32toh(pms->st_size), M_UDFMOUNT, M_NOWAIT | M_ZERO);
765 		if (udfmp->s_table == NULL)
766 			return (ENOMEM);
767 
768 		/* Calculate the number of sectors per packet. */
769 		/* XXX Logical or physical? */
770 		udfmp->p_sectors = le16toh(pms->packet_len) / udfmp->bsize;
771 
772 		/*
773 		 * XXX If reading the first Sparing Table fails, should look
774 		 * for another table.
775 		 */
776 		if ((error = udf_readlblks(udfmp, le32toh(pms->st_loc[0]),
777 					   le32toh(pms->st_size), &bp)) != 0) {
778 			if (bp != NULL)
779 				brelse(bp);
780 			printf("Failed to read Sparing Table at sector %d\n",
781 			    le32toh(pms->st_loc[0]));
782 			return (error);
783 		}
784 		bcopy(bp->b_data, udfmp->s_table, le32toh(pms->st_size));
785 		brelse(bp);
786 
787 		if (udf_checktag(&udfmp->s_table->tag, 0)) {
788 			printf("Invalid sparing table found\n");
789 			return (EINVAL);
790 		}
791 
792 		/* See how many valid entries there are here.  The list is
793 		 * supposed to be sorted. 0xfffffff0 and higher are not valid
794 		 */
795 		for (i = 0; i < le16toh(udfmp->s_table->rt_l); i++) {
796 			udfmp->s_table_entries = i;
797 			if (le32toh(udfmp->s_table->entries[i].org) >=
798 			    0xfffffff0)
799 				break;
800 		}
801 	}
802 
803 	return (0);
804 }
805