xref: /freebsd/sys/fs/udf/udf_vfsops.c (revision 3193579b66fd7067f898dbc54bdea81a0e6f9bd0)
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 
91 #include <vm/uma.h>
92 
93 #include <fs/udf/ecma167-udf.h>
94 #include <fs/udf/osta.h>
95 #include <fs/udf/udf.h>
96 #include <fs/udf/udf_mount.h>
97 
98 MALLOC_DEFINE(M_UDFMOUNT, "UDF mount", "UDF mount structure");
99 MALLOC_DEFINE(M_UDFFENTRY, "UDF fentry", "UDF file entry structure");
100 
101 struct iconv_functions *udf_iconv = NULL;
102 
103 /* Zones */
104 uma_zone_t udf_zone_trans = NULL;
105 uma_zone_t udf_zone_node = NULL;
106 uma_zone_t udf_zone_ds = NULL;
107 
108 static vfs_init_t      udf_init;
109 static vfs_uninit_t    udf_uninit;
110 static vfs_nmount_t    udf_mount;
111 static vfs_root_t      udf_root;
112 static vfs_statfs_t    udf_statfs;
113 static vfs_unmount_t   udf_unmount;
114 static vfs_fhtovp_t	udf_fhtovp;
115 static vfs_vptofh_t	udf_vptofh;
116 
117 static int udf_find_partmaps(struct udf_mnt *, struct logvol_desc *);
118 
119 static struct vfsops udf_vfsops = {
120 	.vfs_fhtovp =		udf_fhtovp,
121 	.vfs_init =		udf_init,
122 	.vfs_nmount =		udf_mount,
123 	.vfs_root =		udf_root,
124 	.vfs_statfs =		udf_statfs,
125 	.vfs_uninit =		udf_uninit,
126 	.vfs_unmount =		udf_unmount,
127 	.vfs_vget =		udf_vget,
128 	.vfs_vptofh =		udf_vptofh,
129 };
130 VFS_SET(udf_vfsops, udf, VFCF_READONLY);
131 
132 MODULE_VERSION(udf, 1);
133 
134 static int udf_mountfs(struct vnode *, struct mount *, struct thread *);
135 
136 static int
137 udf_init(struct vfsconf *foo)
138 {
139 
140 	/*
141 	 * This code used to pre-allocate a certain number of pages for each
142 	 * pool, reducing the need to grow the zones later on.  UMA doesn't
143 	 * advertise any such functionality, unfortunately =-<
144 	 */
145 	udf_zone_trans = uma_zcreate("UDF translation buffer, zone", MAXNAMLEN *
146 	    sizeof(unicode_t), NULL, NULL, NULL, NULL, 0, 0);
147 
148 	udf_zone_node = uma_zcreate("UDF Node zone", sizeof(struct udf_node),
149 	    NULL, NULL, NULL, NULL, 0, 0);
150 
151 	udf_zone_ds = uma_zcreate("UDF Dirstream zone",
152 	    sizeof(struct udf_dirstream), NULL, NULL, NULL, NULL, 0, 0);
153 
154 	if ((udf_zone_node == NULL) || (udf_zone_trans == NULL) ||
155 	    (udf_zone_ds == NULL)) {
156 		printf("Cannot create allocation zones.\n");
157 		return (ENOMEM);
158 	}
159 
160 	return 0;
161 }
162 
163 static int
164 udf_uninit(struct vfsconf *foo)
165 {
166 
167 	if (udf_zone_trans != NULL) {
168 		uma_zdestroy(udf_zone_trans);
169 		udf_zone_trans = NULL;
170 	}
171 
172 	if (udf_zone_node != NULL) {
173 		uma_zdestroy(udf_zone_node);
174 		udf_zone_node = NULL;
175 	}
176 
177 	if (udf_zone_ds != NULL) {
178 		uma_zdestroy(udf_zone_ds);
179 		udf_zone_ds = NULL;
180 	}
181 
182 	return (0);
183 }
184 
185 static int
186 udf_mount(struct mount *mp, struct nameidata *ndp, struct thread *td)
187 {
188 	struct vnode *devvp;	/* vnode of the mount device */
189 	struct udf_mnt *imp = 0;
190 	struct export_args *export;
191 	struct vfsoptlist *opts;
192 	char *fspec, *cs_disk, *cs_local;
193 	size_t size;
194 	int error, len, *udf_flags;
195 
196 	opts = mp->mnt_optnew;
197 
198 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
199 		return (EROFS);
200 
201 	/*
202 	 * No root filesystem support.  Probably not a big deal, since the
203 	 * bootloader doesn't understand UDF.
204 	 */
205 	if (mp->mnt_flag & MNT_ROOTFS)
206 		return (ENOTSUP);
207 
208 	fspec = NULL;
209 	error = vfs_getopt(opts, "from", (void **)&fspec, &len);
210 	if (!error && fspec[len - 1] != '\0')
211 		return (EINVAL);
212 
213 	if (mp->mnt_flag & MNT_UPDATE) {
214 		imp = VFSTOUDFFS(mp);
215 		if (fspec == NULL) {
216 			error = vfs_getopt(opts, "export", (void **)&export,
217 			    &len);
218 			if (error || len != sizeof(struct export_args))
219 				return (EINVAL);
220 			return (vfs_export(mp, export));
221 		}
222 	}
223 
224 	/* Check that the mount device exists */
225 	if (fspec == NULL)
226 		return (EINVAL);
227 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fspec, td);
228 	if ((error = namei(ndp)))
229 		return (error);
230 	NDFREE(ndp, NDF_ONLY_PNBUF);
231 	devvp = ndp->ni_vp;
232 
233 	if (vn_isdisk(devvp, &error) == 0) {
234 		vrele(devvp);
235 		return (error);
236 	}
237 
238 	/* Check the access rights on the mount device */
239 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
240 	error = VOP_ACCESS(devvp, VREAD, td->td_ucred, td);
241 	if (error)
242 		error = suser(td);
243 	if (error) {
244 		vput(devvp);
245 		return (error);
246 	}
247 	VOP_UNLOCK(devvp, 0, td);
248 
249 	if ((error = udf_mountfs(devvp, mp, td))) {
250 		vrele(devvp);
251 		return (error);
252 	}
253 
254 	imp = VFSTOUDFFS(mp);
255 
256 	udf_flags = NULL;
257 	error = vfs_getopt(opts, "flags", (void **)&udf_flags, &len);
258 	if (error || len != sizeof(int))
259 		return (EINVAL);
260 	imp->im_flags = *udf_flags;
261 
262 	if (imp->im_flags & UDFMNT_KICONV && udf_iconv) {
263 		cs_disk = NULL;
264 		error = vfs_getopt(opts, "cs_disk", (void **)&cs_disk, &len);
265 		if (!error && cs_disk[len - 1] != '\0')
266 			return (EINVAL);
267 		cs_local = NULL;
268 		error = vfs_getopt(opts, "cs_local", (void **)&cs_local, &len);
269 		if (!error && cs_local[len - 1] != '\0')
270 			return (EINVAL);
271 		udf_iconv->open(cs_local, cs_disk, &imp->im_d2l);
272 #if 0
273 		udf_iconv->open(cs_disk, cs_local, &imp->im_l2d);
274 #endif
275 	}
276 
277 	copystr(fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
278 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
279 	udf_statfs(mp, &mp->mnt_stat, td);
280 	return 0;
281 };
282 
283 /*
284  * Check the descriptor tag for both the correct id and correct checksum.
285  * Return zero if all is good, EINVAL if not.
286  */
287 int
288 udf_checktag(struct desc_tag *tag, uint16_t id)
289 {
290 	uint8_t *itag;
291 	uint8_t i, cksum = 0;
292 
293 	itag = (uint8_t *)tag;
294 
295 	if (tag->id != id)
296 		return (EINVAL);
297 
298 	for (i = 0; i < 15; i++)
299 		cksum = cksum + itag[i];
300 	cksum = cksum - itag[4];
301 
302 	if (cksum == tag->cksum)
303 		return (0);
304 
305 	return (EINVAL);
306 }
307 
308 static int
309 udf_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td) {
310 	struct buf *bp = NULL;
311 	struct anchor_vdp avdp;
312 	struct udf_mnt *udfmp = NULL;
313 	struct part_desc *pd;
314 	struct logvol_desc *lvd;
315 	struct fileset_desc *fsd;
316 	struct file_entry *root_fentry;
317 	uint32_t sector, size, mvds_start, mvds_end;
318 	uint32_t fsd_offset = 0;
319 	uint16_t part_num = 0, fsd_part = 0;
320 	int error = EINVAL, needclose = 0;
321 	int logvol_found = 0, part_found = 0, fsd_found = 0;
322 	int bsize;
323 
324 	/*
325 	 * Disallow multiple mounts of the same device. Flush the buffer
326 	 * cache for the device.
327 	 */
328 	if ((error = vfs_mountedon(devvp)))
329 		return (error);
330 	if (vcount(devvp) > 1)
331 		return (EBUSY);
332 	if ((error = vinvalbuf(devvp, V_SAVE, td->td_ucred, td, 0, 0)))
333 		return (error);
334 
335 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
336 	error = VOP_OPEN(devvp, FREAD, FSCRED, td, -1);
337 	VOP_UNLOCK(devvp, 0, td);
338 	if (error)
339 		return error;
340 	needclose = 1;
341 
342 	MALLOC(udfmp, struct udf_mnt *, sizeof(struct udf_mnt), M_UDFMOUNT,
343 	    M_NOWAIT | M_ZERO);
344 	if (udfmp == NULL) {
345 		printf("Cannot allocate UDF mount struct\n");
346 		error = ENOMEM;
347 		goto bail;
348 	}
349 
350 	mp->mnt_data = (qaddr_t)udfmp;
351 	mp->mnt_stat.f_fsid.val[0] = dev2udev(devvp->v_rdev);
352 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
353 	mp->mnt_flag |= MNT_LOCAL;
354 	udfmp->im_mountp = mp;
355 	udfmp->im_dev = devvp->v_rdev;
356 	udfmp->im_devvp = devvp;
357 	udfmp->im_d2l = NULL;
358 #if 0
359 	udfmp->im_l2d = NULL;
360 #endif
361 
362 	bsize = 2048;	/* XXX Should probe the media for it's size */
363 
364 	/*
365 	 * Get the Anchor Volume Descriptor Pointer from sector 256.
366 	 * XXX Should also check sector n - 256, n, and 512.
367 	 */
368 	sector = 256;
369 	if ((error = bread(devvp, sector * btodb(bsize), bsize, NOCRED,
370 			   &bp)) != 0)
371 		goto bail;
372 	if ((error = udf_checktag((struct desc_tag *)bp->b_data, TAGID_ANCHOR)))
373 		goto bail;
374 
375 	bcopy(bp->b_data, &avdp, sizeof(struct anchor_vdp));
376 	brelse(bp);
377 	bp = NULL;
378 
379 	/*
380 	 * Extract the Partition Descriptor and Logical Volume Descriptor
381 	 * from the Volume Descriptor Sequence.
382 	 * XXX Should we care about the partition type right now?
383 	 * XXX What about multiple partitions?
384 	 */
385 	mvds_start = avdp.main_vds_ex.loc;
386 	mvds_end = mvds_start + (avdp.main_vds_ex.len - 1) / bsize;
387 	for (sector = mvds_start; sector < mvds_end; sector++) {
388 		if ((error = bread(devvp, sector * btodb(bsize), bsize,
389 				   NOCRED, &bp)) != 0) {
390 			printf("Can't read sector %d of VDS\n", sector);
391 			goto bail;
392 		}
393 		lvd = (struct logvol_desc *)bp->b_data;
394 		if (!udf_checktag(&lvd->tag, TAGID_LOGVOL)) {
395 			udfmp->bsize = lvd->lb_size;
396 			udfmp->bmask = udfmp->bsize - 1;
397 			udfmp->bshift = ffs(udfmp->bsize) - 1;
398 			fsd_part = lvd->_lvd_use.fsd_loc.loc.part_num;
399 			fsd_offset = lvd->_lvd_use.fsd_loc.loc.lb_num;
400 			if (udf_find_partmaps(udfmp, lvd))
401 				break;
402 			logvol_found = 1;
403 		}
404 		pd = (struct part_desc *)bp->b_data;
405 		if (!udf_checktag(&pd->tag, TAGID_PARTITION)) {
406 			part_found = 1;
407 			part_num = pd->part_num;
408 			udfmp->part_len = pd->part_len;
409 			udfmp->part_start = pd->start_loc;
410 		}
411 
412 		brelse(bp);
413 		bp = NULL;
414 		if ((part_found) && (logvol_found))
415 			break;
416 	}
417 
418 	if (!part_found || !logvol_found) {
419 		error = EINVAL;
420 		goto bail;
421 	}
422 
423 	if (fsd_part != part_num) {
424 		printf("FSD does not lie within the partition!\n");
425 		error = EINVAL;
426 		goto bail;
427 	}
428 
429 
430 	/*
431 	 * Grab the Fileset Descriptor
432 	 * Thanks to Chuck McCrobie <mccrobie@cablespeed.com> for pointing
433 	 * me in the right direction here.
434 	 */
435 	sector = udfmp->part_start + fsd_offset;
436 	if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
437 		printf("Cannot read sector %d of FSD\n", sector);
438 		goto bail;
439 	}
440 	fsd = (struct fileset_desc *)bp->b_data;
441 	if (!udf_checktag(&fsd->tag, TAGID_FSD)) {
442 		fsd_found = 1;
443 		bcopy(&fsd->rootdir_icb, &udfmp->root_icb,
444 		    sizeof(struct long_ad));
445 	}
446 
447 	brelse(bp);
448 	bp = NULL;
449 
450 	if (!fsd_found) {
451 		printf("Couldn't find the fsd\n");
452 		error = EINVAL;
453 		goto bail;
454 	}
455 
456 	/*
457 	 * Find the file entry for the root directory.
458 	 */
459 	sector = udfmp->root_icb.loc.lb_num + udfmp->part_start;
460 	size = udfmp->root_icb.len;
461 	if ((error = udf_readlblks(udfmp, sector, size, &bp)) != 0) {
462 		printf("Cannot read sector %d\n", sector);
463 		goto bail;
464 	}
465 
466 	root_fentry = (struct file_entry *)bp->b_data;
467 	if ((error = udf_checktag(&root_fentry->tag, TAGID_FENTRY))) {
468 		printf("Invalid root file entry!\n");
469 		goto bail;
470 	}
471 
472 	brelse(bp);
473 	bp = NULL;
474 
475 	devvp->v_rdev->si_mountpoint = mp;
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 	if (needclose)
488 		VOP_CLOSE(devvp, FREAD, NOCRED, td);
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)))
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 	udfmp->im_devvp->v_rdev->si_mountpoint = NULL;
516 	error = VOP_CLOSE(udfmp->im_devvp, FREAD, NOCRED, td);
517 	vrele(udfmp->im_devvp);
518 
519 	if (udfmp->s_table != NULL)
520 		FREE(udfmp->s_table, M_UDFMOUNT);
521 
522 	if (udfmp->hashtbl != NULL)
523 		FREE(udfmp->hashtbl, 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, struct vnode **vpp)
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 	udfmp->root_vp = vp;
552 
553 	return (0);
554 }
555 
556 static int
557 udf_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
558 {
559 	struct udf_mnt *udfmp;
560 
561 	udfmp = VFSTOUDFFS(mp);
562 
563 	sbp->f_bsize = udfmp->bsize;
564 	sbp->f_iosize = udfmp->bsize;
565 	sbp->f_blocks = udfmp->part_len;
566 	sbp->f_bfree = 0;
567 	sbp->f_bavail = 0;
568 	sbp->f_files = 0;
569 	sbp->f_ffree = 0;
570 	if (sbp != &mp->mnt_stat) {
571 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
572 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
573 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
574 	}
575 
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 	td = curthread;
592 	udfmp = VFSTOUDFFS(mp);
593 
594 	/* See if we already have this in the cache */
595 	if ((error = udf_hashlookup(udfmp, ino, flags, vpp)) != 0)
596 		return (error);
597 	if (*vpp != NULL) {
598 		return (0);
599 	}
600 
601 	/*
602 	 * Allocate memory and check the tag id's before grabbing a new
603 	 * vnode, since it's hard to roll back if there is a problem.
604 	 */
605 	unode = uma_zalloc(udf_zone_node, M_WAITOK);
606 	if (unode == NULL) {
607 		printf("Cannot allocate udf node\n");
608 		return (ENOMEM);
609 	}
610 
611 	/*
612 	 * Copy in the file entry.  Per the spec, the size can only be 1 block.
613 	 */
614 	sector = ino + udfmp->part_start;
615 	devvp = udfmp->im_devvp;
616 	if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
617 		printf("Cannot read sector %d\n", sector);
618 		uma_zfree(udf_zone_node, unode);
619 		return (error);
620 	}
621 
622 	fe = (struct file_entry *)bp->b_data;
623 	if (udf_checktag(&fe->tag, TAGID_FENTRY)) {
624 		printf("Invalid file entry!\n");
625 		uma_zfree(udf_zone_node, unode);
626 		brelse(bp);
627 		return (ENOMEM);
628 	}
629 	size = UDF_FENTRY_SIZE + fe->l_ea + 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 		uma_zfree(udf_zone_node, unode);
635 		brelse(bp);
636 		return (ENOMEM);
637 	}
638 
639 	bcopy(bp->b_data, unode->fentry, size);
640 
641 	brelse(bp);
642 	bp = NULL;
643 
644 	if ((error = udf_allocv(mp, &vp, td))) {
645 		printf("Error from udf_allocv\n");
646 		uma_zfree(udf_zone_node, unode);
647 		return (error);
648 	}
649 
650 	unode->i_vnode = vp;
651 	unode->hash_id = ino;
652 	unode->i_devvp = udfmp->im_devvp;
653 	unode->i_dev = udfmp->im_dev;
654 	unode->udfmp = udfmp;
655 	vp->v_data = unode;
656 	VREF(udfmp->im_devvp);
657 	udf_hashins(unode);
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 	return (0);
713 }
714 
715 static int
716 udf_vptofh (struct vnode *vp, struct fid *fhp)
717 {
718 	struct udf_node *node;
719 	struct ifid *ifhp;
720 
721 	node = VTON(vp);
722 	ifhp = (struct ifid *)fhp;
723 	ifhp->ifid_len = sizeof(struct ifid);
724 	ifhp->ifid_ino = node->hash_id;
725 
726 	return (0);
727 }
728 
729 static int
730 udf_find_partmaps(struct udf_mnt *udfmp, struct logvol_desc *lvd)
731 {
732 	union udf_pmap *pmap;
733 	struct part_map_spare *pms;
734 	struct regid *pmap_id;
735 	struct buf *bp;
736 	unsigned char regid_id[UDF_REGID_ID_SIZE + 1];
737 	int i, ptype, psize, error;
738 
739 	for (i = 0; i < lvd->n_pm; i++) {
740 		pmap = (union udf_pmap *)&lvd->maps[i * UDF_PMAP_SIZE];
741 		ptype = pmap->data[0];
742 		psize = pmap->data[1];
743 		if (((ptype != 1) && (ptype != 2)) ||
744 		    ((psize != UDF_PMAP_SIZE) && (psize != 6))) {
745 			printf("Invalid partition map found\n");
746 			return (1);
747 		}
748 
749 		if (ptype == 1) {
750 			/* Type 1 map.  We don't care */
751 			continue;
752 		}
753 
754 		/* Type 2 map.  Gotta find out the details */
755 		pmap_id = (struct regid *)&pmap->data[4];
756 		bzero(&regid_id[0], UDF_REGID_ID_SIZE);
757 		bcopy(&pmap_id->id[0], &regid_id[0], UDF_REGID_ID_SIZE);
758 
759 		if (bcmp(&regid_id[0], "*UDF Sparable Partition",
760 		    UDF_REGID_ID_SIZE)) {
761 			printf("Unsupported partition map: %s\n", &regid_id[0]);
762 			return (1);
763 		}
764 
765 		pms = &pmap->pms;
766 		MALLOC(udfmp->s_table, struct udf_sparing_table *, pms->st_size,
767 		    M_UDFMOUNT, M_NOWAIT | M_ZERO);
768 		if (udfmp->s_table == NULL)
769 			return (ENOMEM);
770 
771 		/* Calculate the number of sectors per packet. */
772 		/* XXX Logical or physical? */
773 		udfmp->p_sectors = pms->packet_len / udfmp->bsize;
774 
775 		/*
776 		 * XXX If reading the first Sparing Table fails, should look
777 		 * for another table.
778 		 */
779 		if ((error = udf_readlblks(udfmp, pms->st_loc[0], pms->st_size,
780 		    &bp)) != 0) {
781 			printf("Failed to read Sparing Table at sector %d\n",
782 			    pms->st_loc[0]);
783 			return (error);
784 		}
785 		bcopy(bp->b_data, udfmp->s_table, pms->st_size);
786 		brelse(bp);
787 
788 		if (udf_checktag(&udfmp->s_table->tag, 0)) {
789 			printf("Invalid sparing table found\n");
790 			return (EINVAL);
791 		}
792 
793 		/* See how many valid entries there are here.  The list is
794 		 * supposed to be sorted. 0xfffffff0 and higher are not valid
795 		 */
796 		for (i = 0; i < udfmp->s_table->rt_l; i++) {
797 			udfmp->s_table_entries = i;
798 			if (udfmp->s_table->entries[i].org >= 0xfffffff0)
799 				break;
800 		}
801 	}
802 
803 	return (0);
804 }
805