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