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