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