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