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