xref: /freebsd/sys/fs/udf/udf_vfsops.c (revision f9218d3d4fd34f082473b3a021c6d4d109fb47cf)
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 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 uma_zone_t udf_zone_ds = NULL;
104 
105 static int udf_init(struct vfsconf *);
106 static int udf_uninit(struct vfsconf *);
107 static int udf_mount(struct mount *, struct nameidata *, struct thread *);
108 static int udf_unmount(struct mount *, int, struct thread *);
109 static int udf_root(struct mount *, struct vnode **);
110 static int udf_statfs(struct mount *, struct statfs *, struct thread *);
111 static int udf_fhtovp(struct mount *, struct fid *, struct vnode **);
112 static int udf_vptofh(struct vnode *, struct fid *);
113 static int udf_find_partmaps(struct udf_mnt *, struct logvol_desc *);
114 
115 static struct vfsops udf_vfsops = {
116 	NULL,
117 	vfs_stdstart,
118 	udf_unmount,
119 	udf_root,
120 	vfs_stdquotactl,
121 	udf_statfs,
122 	vfs_stdsync,
123 	udf_vget,
124 	udf_fhtovp,
125 	vfs_stdcheckexp,
126 	udf_vptofh,
127 	udf_init,
128 	udf_uninit,
129 	vfs_stdextattrctl,
130 	udf_mount,
131 };
132 VFS_SET(udf_vfsops, udf, VFCF_READONLY);
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;
193 	size_t size;
194 	int error, len;
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 	copystr(fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
256 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
257 	udf_statfs(mp, &mp->mnt_stat, td);
258 	return 0;
259 };
260 
261 /*
262  * Check the descriptor tag for both the correct id and correct checksum.
263  * Return zero if all is good, EINVAL if not.
264  */
265 int
266 udf_checktag(struct desc_tag *tag, uint16_t id)
267 {
268 	uint8_t *itag;
269 	uint8_t i, cksum = 0;
270 
271 	itag = (uint8_t *)tag;
272 
273 	if (tag->id != id)
274 		return (EINVAL);
275 
276 	for (i = 0; i < 15; i++)
277 		cksum = cksum + itag[i];
278 	cksum = cksum - itag[4];
279 
280 	if (cksum == tag->cksum)
281 		return (0);
282 
283 	return (EINVAL);
284 }
285 
286 static int
287 udf_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td) {
288 	struct buf *bp = NULL;
289 	struct anchor_vdp avdp;
290 	struct udf_mnt *udfmp = NULL;
291 	struct part_desc *pd;
292 	struct logvol_desc *lvd;
293 	struct fileset_desc *fsd;
294 	struct file_entry *root_fentry;
295 	uint32_t sector, size, mvds_start, mvds_end;
296 	uint32_t fsd_offset = 0;
297 	uint16_t part_num = 0, fsd_part = 0;
298 	int error = EINVAL, needclose = 0;
299 	int logvol_found = 0, part_found = 0, fsd_found = 0;
300 	int bsize;
301 
302 	/*
303 	 * Disallow multiple mounts of the same device. Flush the buffer
304 	 * cache for the device.
305 	 */
306 	if ((error = vfs_mountedon(devvp)))
307 		return (error);
308 	if (vcount(devvp) > 1)
309 		return (EBUSY);
310 	if ((error = vinvalbuf(devvp, V_SAVE, td->td_ucred, td, 0, 0)))
311 		return (error);
312 
313 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
314 	error = VOP_OPEN(devvp, FREAD, FSCRED, td);
315 	VOP_UNLOCK(devvp, 0, td);
316 	if (error)
317 		return error;
318 	needclose = 1;
319 
320 	MALLOC(udfmp, struct udf_mnt *, sizeof(struct udf_mnt), M_UDFMOUNT,
321 	    M_NOWAIT | M_ZERO);
322 	if (udfmp == NULL) {
323 		printf("Cannot allocate UDF mount struct\n");
324 		error = ENOMEM;
325 		goto bail;
326 	}
327 
328 	mp->mnt_data = (qaddr_t)udfmp;
329 	mp->mnt_stat.f_fsid.val[0] = dev2udev(devvp->v_rdev);
330 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
331 	mp->mnt_flag |= MNT_LOCAL;
332 	udfmp->im_mountp = mp;
333 	udfmp->im_dev = devvp->v_rdev;
334 	udfmp->im_devvp = devvp;
335 
336 	bsize = 2048;	/* XXX Should probe the media for it's size */
337 
338 	/*
339 	 * Get the Anchor Volume Descriptor Pointer from sector 256.
340 	 * XXX Should also check sector n - 256, n, and 512.
341 	 */
342 	sector = 256;
343 	if ((error = bread(devvp, sector * btodb(bsize), bsize, NOCRED,
344 			   &bp)) != 0)
345 		goto bail;
346 	if ((error = udf_checktag((struct desc_tag *)bp->b_data, TAGID_ANCHOR)))
347 		goto bail;
348 
349 	bcopy(bp->b_data, &avdp, sizeof(struct anchor_vdp));
350 	brelse(bp);
351 	bp = NULL;
352 
353 	/*
354 	 * Extract the Partition Descriptor and Logical Volume Descriptor
355 	 * from the Volume Descriptor Sequence.
356 	 * XXX Should we care about the partition type right now?
357 	 * XXX What about multiple partitions?
358 	 */
359 	mvds_start = avdp.main_vds_ex.loc;
360 	mvds_end = mvds_start + (avdp.main_vds_ex.len - 1) / bsize;
361 	for (sector = mvds_start; sector < mvds_end; sector++) {
362 		if ((error = bread(devvp, sector * btodb(bsize), bsize,
363 				   NOCRED, &bp)) != 0) {
364 			printf("Can't read sector %d of VDS\n", sector);
365 			goto bail;
366 		}
367 		lvd = (struct logvol_desc *)bp->b_data;
368 		if (!udf_checktag(&lvd->tag, TAGID_LOGVOL)) {
369 			udfmp->bsize = lvd->lb_size;
370 			udfmp->bmask = udfmp->bsize - 1;
371 			udfmp->bshift = ffs(udfmp->bsize) - 1;
372 			fsd_part = lvd->_lvd_use.fsd_loc.loc.part_num;
373 			fsd_offset = lvd->_lvd_use.fsd_loc.loc.lb_num;
374 			if (udf_find_partmaps(udfmp, lvd))
375 				break;
376 			logvol_found = 1;
377 		}
378 		pd = (struct part_desc *)bp->b_data;
379 		if (!udf_checktag(&pd->tag, TAGID_PARTITION)) {
380 			part_found = 1;
381 			part_num = pd->part_num;
382 			udfmp->part_len = pd->part_len;
383 			udfmp->part_start = pd->start_loc;
384 		}
385 
386 		brelse(bp);
387 		bp = NULL;
388 		if ((part_found) && (logvol_found))
389 			break;
390 	}
391 
392 	if (!part_found || !logvol_found) {
393 		error = EINVAL;
394 		goto bail;
395 	}
396 
397 	if (fsd_part != part_num) {
398 		printf("FSD does not lie within the partition!\n");
399 		error = EINVAL;
400 		goto bail;
401 	}
402 
403 
404 	/*
405 	 * Grab the Fileset Descriptor
406 	 * Thanks to Chuck McCrobie <mccrobie@cablespeed.com> for pointing
407 	 * me in the right direction here.
408 	 */
409 	sector = udfmp->part_start + fsd_offset;
410 	if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
411 		printf("Cannot read sector %d of FSD\n", sector);
412 		goto bail;
413 	}
414 	fsd = (struct fileset_desc *)bp->b_data;
415 	if (!udf_checktag(&fsd->tag, TAGID_FSD)) {
416 		fsd_found = 1;
417 		bcopy(&fsd->rootdir_icb, &udfmp->root_icb,
418 		    sizeof(struct long_ad));
419 	}
420 
421 	brelse(bp);
422 	bp = NULL;
423 
424 	if (!fsd_found) {
425 		printf("Couldn't find the fsd\n");
426 		error = EINVAL;
427 		goto bail;
428 	}
429 
430 	/*
431 	 * Find the file entry for the root directory.
432 	 */
433 	sector = udfmp->root_icb.loc.lb_num + udfmp->part_start;
434 	size = udfmp->root_icb.len;
435 	if ((error = udf_readlblks(udfmp, sector, size, &bp)) != 0) {
436 		printf("Cannot read sector %d\n", sector);
437 		goto bail;
438 	}
439 
440 	root_fentry = (struct file_entry *)bp->b_data;
441 	if ((error = udf_checktag(&root_fentry->tag, TAGID_FENTRY))) {
442 		printf("Invalid root file entry!\n");
443 		goto bail;
444 	}
445 
446 	brelse(bp);
447 	bp = NULL;
448 
449 	TAILQ_INIT(&udfmp->udf_tqh);
450 	devvp->v_rdev->si_mountpoint = mp;
451 
452 	mtx_init(&udfmp->hash_mtx, "udf_hash", NULL, MTX_DEF);
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_UDFSTABLE);
485 	FREE(udfmp, M_UDFMOUNT);
486 
487 	mp->mnt_data = (qaddr_t)0;
488 	mp->mnt_flag &= ~MNT_LOCAL;
489 
490 	return (0);
491 }
492 
493 static int
494 udf_root(struct mount *mp, struct vnode **vpp)
495 {
496 	struct udf_mnt *udfmp;
497 	struct vnode *vp;
498 	ino_t id;
499 	int error;
500 
501 	udfmp = VFSTOUDFFS(mp);
502 
503 	id = udf_getid(&udfmp->root_icb);
504 
505 	error = udf_vget(mp, id, LK_EXCLUSIVE, vpp);
506 	if (error)
507 		return error;
508 
509 	vp = *vpp;
510 	vp->v_vflag |= VV_ROOT;
511 	udfmp->root_vp = vp;
512 
513 	return (0);
514 }
515 
516 static int
517 udf_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
518 {
519 	struct udf_mnt *udfmp;
520 
521 	udfmp = VFSTOUDFFS(mp);
522 
523 	sbp->f_bsize = udfmp->bsize;
524 	sbp->f_iosize = udfmp->bsize;
525 	sbp->f_blocks = udfmp->part_len;
526 	sbp->f_bfree = 0;
527 	sbp->f_bavail = 0;
528 	sbp->f_files = 0;
529 	sbp->f_ffree = 0;
530 	if (sbp != &mp->mnt_stat) {
531 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
532 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
533 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
534 	}
535 
536 	return 0;
537 }
538 
539 int
540 udf_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
541 {
542 	struct buf *bp;
543 	struct vnode *devvp;
544 	struct udf_mnt *udfmp;
545 	struct thread *td;
546 	struct vnode *vp;
547 	struct udf_node *unode;
548 	struct file_entry *fe;
549 	int error, sector, size;
550 
551 	td = curthread;
552 	udfmp = VFSTOUDFFS(mp);
553 
554 	/* See if we already have this in the cache */
555 	if ((error = udf_hashlookup(udfmp, ino, flags, vpp)) != 0)
556 		return (error);
557 	if (*vpp != NULL) {
558 		return (0);
559 	}
560 
561 	/*
562 	 * Allocate memory and check the tag id's before grabbing a new
563 	 * vnode, since it's hard to roll back if there is a problem.
564 	 */
565 	unode = uma_zalloc(udf_zone_node, M_WAITOK);
566 	if (unode == NULL) {
567 		printf("Cannot allocate udf node\n");
568 		return (ENOMEM);
569 	}
570 
571 	/*
572 	 * Copy in the file entry.  Per the spec, the size can only be 1 block.
573 	 */
574 	sector = ino + udfmp->part_start;
575 	devvp = udfmp->im_devvp;
576 	if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
577 		printf("Cannot read sector %d\n", sector);
578 		uma_zfree(udf_zone_node, unode);
579 		return (error);
580 	}
581 
582 	fe = (struct file_entry *)bp->b_data;
583 	if (udf_checktag(&fe->tag, TAGID_FENTRY)) {
584 		printf("Invalid file entry!\n");
585 		uma_zfree(udf_zone_node, unode);
586 		brelse(bp);
587 		return (ENOMEM);
588 	}
589 	size = UDF_FENTRY_SIZE + fe->l_ea + fe->l_ad;
590 	MALLOC(unode->fentry, struct file_entry *, size, M_UDFFENTRY,
591 	    M_NOWAIT | M_ZERO);
592 	if (unode->fentry == NULL) {
593 		printf("Cannot allocate file entry block\n");
594 		uma_zfree(udf_zone_node, unode);
595 		brelse(bp);
596 		return (ENOMEM);
597 	}
598 
599 	bcopy(bp->b_data, unode->fentry, size);
600 
601 	brelse(bp);
602 	bp = NULL;
603 
604 	if ((error = udf_allocv(mp, &vp, td))) {
605 		printf("Error from udf_allocv\n");
606 		uma_zfree(udf_zone_node, unode);
607 		return (error);
608 	}
609 
610 	unode->i_vnode = vp;
611 	unode->hash_id = ino;
612 	unode->i_devvp = udfmp->im_devvp;
613 	unode->i_dev = udfmp->im_dev;
614 	unode->udfmp = udfmp;
615 	vp->v_data = unode;
616 	VREF(udfmp->im_devvp);
617 	udf_hashins(unode);
618 
619 	switch (unode->fentry->icbtag.file_type) {
620 	default:
621 		vp->v_type = VBAD;
622 		break;
623 	case 4:
624 		vp->v_type = VDIR;
625 		break;
626 	case 5:
627 		vp->v_type = VREG;
628 		break;
629 	case 6:
630 		vp->v_type = VBLK;
631 		break;
632 	case 7:
633 		vp->v_type = VCHR;
634 		break;
635 	case 9:
636 		vp->v_type = VFIFO;
637 		break;
638 	case 10:
639 		vp->v_type = VSOCK;
640 		break;
641 	case 12:
642 		vp->v_type = VLNK;
643 		break;
644 	}
645 	*vpp = vp;
646 
647 	return (0);
648 }
649 
650 struct ifid {
651 	ushort	ifid_len;
652 	ushort	ifid_pad;
653 	int	ifid_ino;
654 	long	ifid_start;
655 };
656 
657 static int
658 udf_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
659 {
660 	struct ifid *ifhp;
661 	struct vnode *nvp;
662 	int error;
663 
664 	ifhp = (struct ifid *)fhp;
665 
666 	if ((error = VFS_VGET(mp, ifhp->ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
667 		*vpp = NULLVP;
668 		return (error);
669 	}
670 
671 	*vpp = nvp;
672 	return (0);
673 }
674 
675 static int
676 udf_vptofh (struct vnode *vp, struct fid *fhp)
677 {
678 	struct udf_node *node;
679 	struct ifid *ifhp;
680 
681 	node = VTON(vp);
682 	ifhp = (struct ifid *)fhp;
683 	ifhp->ifid_len = sizeof(struct ifid);
684 	ifhp->ifid_ino = node->hash_id;
685 
686 	return (0);
687 }
688 
689 static int
690 udf_find_partmaps(struct udf_mnt *udfmp, struct logvol_desc *lvd)
691 {
692 	union udf_pmap *pmap;
693 	struct part_map_spare *pms;
694 	struct regid *pmap_id;
695 	struct buf *bp;
696 	unsigned char regid_id[UDF_REGID_ID_SIZE + 1];
697 	int i, ptype, psize, error;
698 
699 	for (i = 0; i < lvd->n_pm; i++) {
700 		pmap = (union udf_pmap *)&lvd->maps[i * UDF_PMAP_SIZE];
701 		ptype = pmap->data[0];
702 		psize = pmap->data[1];
703 		if (((ptype != 1) && (ptype != 2)) ||
704 		    ((psize != UDF_PMAP_SIZE) && (psize != 6))) {
705 			printf("Invalid partition map found\n");
706 			return (1);
707 		}
708 
709 		if (ptype == 1) {
710 			/* Type 1 map.  We don't care */
711 			continue;
712 		}
713 
714 		/* Type 2 map.  Gotta find out the details */
715 		pmap_id = (struct regid *)&pmap->data[4];
716 		bzero(&regid_id[0], UDF_REGID_ID_SIZE);
717 		bcopy(&pmap_id->id[0], &regid_id[0], UDF_REGID_ID_SIZE);
718 
719 		if (bcmp(&regid_id[0], "*UDF Sparable Partition",
720 		    UDF_REGID_ID_SIZE)) {
721 			printf("Unsupported partition map: %s\n", &regid_id[0]);
722 			return (1);
723 		}
724 
725 		pms = &pmap->pms;
726 		MALLOC(udfmp->s_table, struct udf_sparing_table *, pms->st_size,
727 		    M_UDFSTABLE, M_NOWAIT | M_ZERO);
728 		if (udfmp->s_table == NULL)
729 			return (ENOMEM);
730 
731 		/* Calculate the number of sectors per packet. */
732 		/* XXX Logical or physical? */
733 		udfmp->p_sectors = pms->packet_len / udfmp->bsize;
734 
735 		/*
736 		 * XXX If reading the first Sparing Table fails, should look
737 		 * for another table.
738 		 */
739 		if ((error = udf_readlblks(udfmp, pms->st_loc[0], pms->st_size,
740 		    &bp)) != 0) {
741 			printf("Failed to read Sparing Table at sector %d\n",
742 			    pms->st_loc[0]);
743 			return (error);
744 		}
745 		bcopy(bp->b_data, udfmp->s_table, pms->st_size);
746 		brelse(bp);
747 
748 		if (udf_checktag(&udfmp->s_table->tag, 0)) {
749 			printf("Invalid sparing table found\n");
750 			return (EINVAL);
751 		}
752 
753 		/* See how many valid entries there are here.  The list is
754 		 * supposed to be sorted. 0xfffffff0 and higher are not valid
755 		 */
756 		for (i = 0; i < udfmp->s_table->rt_l; i++) {
757 			udfmp->s_table_entries = i;
758 			if (udfmp->s_table->entries[i].org >= 0xfffffff0)
759 				break;
760 		}
761 	}
762 
763 	return (0);
764 }
765