xref: /openbsd-src/sys/isofs/udf/udf_vfsops.c (revision 6a13ef69787db04ae501a22e92fa10865b44fd7c)
1 /*	$OpenBSD: udf_vfsops.c,v 1.57 2016/09/24 18:38:23 tedu Exp $	*/
2 
3 /*
4  * Copyright (c) 2001, 2002 Scott Long <scottl@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sys/fs/udf/udf_vfsops.c,v 1.25 2005/01/25 15:52:03 phk Exp $
29  */
30 
31 /*
32  * Ported to OpenBSD by Pedro Martelletto in February 2005.
33  */
34 
35 /*
36  * Ok, here's how it goes.  The UDF specs are pretty clear on how each data
37  * structure is made up, but not very clear on how they relate to each other.
38  * Here is the skinny... This demostrates a filesystem with one file in the
39  * root directory.  Subdirectories are treated just as normal files, but they
40  * have File Id Descriptors of their children as their file data.  As for the
41  * Anchor Volume Descriptor Pointer, it can exist in two of the following three
42  * places: sector 256, sector n (the max sector of the disk), or sector
43  * n - 256.  It's a pretty good bet that one will exist at sector 256 though.
44  * One caveat is unclosed CD media.  For that, sector 256 cannot be written,
45  * so the Anchor Volume Descriptor Pointer can exist at sector 512 until the
46  * media is closed.
47  */
48 
49 #include <sys/types.h>
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/uio.h>
53 #include <sys/buf.h>
54 #include <sys/dirent.h>
55 #include <sys/fcntl.h>
56 #include <sys/kernel.h>
57 #include <sys/malloc.h>
58 #include <sys/mutex.h>
59 #include <sys/mount.h>
60 #include <sys/namei.h>
61 #include <sys/pool.h>
62 #include <sys/proc.h>
63 #include <sys/lock.h>
64 #include <sys/queue.h>
65 #include <sys/vnode.h>
66 #include <sys/lock.h>
67 #include <sys/endian.h>
68 #include <sys/specdev.h>
69 
70 #include <crypto/siphash.h>
71 
72 #include <isofs/udf/ecma167-udf.h>
73 #include <isofs/udf/udf.h>
74 #include <isofs/udf/udf_extern.h>
75 
76 struct pool udf_trans_pool;
77 struct pool unode_pool;
78 struct pool udf_ds_pool;
79 
80 int udf_find_partmaps(struct umount *, struct logvol_desc *);
81 int udf_get_vpartmap(struct umount *, struct part_map_virt *);
82 int udf_get_spartmap(struct umount *, struct part_map_spare *);
83 int udf_get_mpartmap(struct umount *, struct part_map_meta *);
84 int udf_mountfs(struct vnode *, struct mount *, uint32_t, struct proc *);
85 
86 const struct vfsops udf_vfsops = {
87 	.vfs_fhtovp =		udf_fhtovp,
88 	.vfs_init =		udf_init,
89 	.vfs_mount =		udf_mount,
90 	.vfs_start =		udf_start,
91 	.vfs_root =		udf_root,
92 	.vfs_quotactl =		udf_quotactl,
93 	.vfs_statfs =		udf_statfs,
94 	.vfs_sync =		udf_sync,
95 	.vfs_unmount =		udf_unmount,
96 	.vfs_vget =		udf_vget,
97 	.vfs_vptofh =		udf_vptofh,
98 	.vfs_sysctl =		udf_sysctl,
99 	.vfs_checkexp =		udf_checkexp,
100 };
101 
102 int
103 udf_init(struct vfsconf *foo)
104 {
105 	pool_init(&udf_trans_pool, MAXNAMLEN * sizeof(unicode_t), 0, IPL_NONE,
106 	    PR_WAITOK, "udftrpl", NULL);
107 	pool_init(&unode_pool, sizeof(struct unode), 0, IPL_NONE,
108 	    PR_WAITOK, "udfndpl", NULL);
109 	pool_init(&udf_ds_pool, sizeof(struct udf_dirstream), 0, IPL_NONE,
110 	    PR_WAITOK, "udfdspl", NULL);
111 
112 	return (0);
113 }
114 
115 int
116 udf_start(struct mount *mp, int flags, struct proc *p)
117 {
118 	return (0);
119 }
120 
121 int
122 udf_mount(struct mount *mp, const char *path, void *data,
123     struct nameidata *ndp,  struct proc *p)
124 {
125 	struct vnode *devvp;	/* vnode of the mount device */
126 	struct udf_args args;
127 	char fspec[MNAMELEN];
128 	int error;
129 
130 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
131 		mp->mnt_flag |= MNT_RDONLY;
132 #ifdef UDF_DEBUG
133 		printf("udf_mount: enforcing read-only mode\n");
134 #endif
135 	}
136 
137 	/*
138 	 * No root filesystem support.  Probably not a big deal, since the
139 	 * bootloader doesn't understand UDF.
140 	 */
141 	if (mp->mnt_flag & MNT_ROOTFS)
142 		return (EOPNOTSUPP);
143 
144 	error = copyin(data, &args, sizeof(struct udf_args));
145 	if (error)
146 		return (error);
147 
148 	if (args.fspec == NULL)
149 		return (EINVAL);
150 
151 	error = copyinstr(args.fspec, fspec, sizeof(fspec), NULL);
152 	if (error)
153 		return (error);
154 
155 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fspec, p);
156 	if ((error = namei(ndp)))
157 		return (error);
158 
159 	devvp = ndp->ni_vp;
160 	if (devvp->v_type != VBLK) {
161 		vrele(devvp);
162 		return (ENOTBLK);
163 	}
164 
165 	if (major(devvp->v_rdev) >= nblkdev) {
166 		vrele(devvp);
167 		return (ENXIO);
168 	}
169 
170 	if ((error = udf_mountfs(devvp, mp, args.lastblock, p))) {
171 		vrele(devvp);
172 		return (error);
173 	}
174 
175 	/*
176 	 * Keep a copy of the mount information.
177 	 */
178 	bzero(mp->mnt_stat.f_mntonname, MNAMELEN);
179 	strlcpy(mp->mnt_stat.f_mntonname, path, MNAMELEN);
180 	bzero(mp->mnt_stat.f_mntfromname, MNAMELEN);
181 	strlcpy(mp->mnt_stat.f_mntfromname, fspec, MNAMELEN);
182 	bzero(mp->mnt_stat.f_mntfromspec, MNAMELEN);
183 	strlcpy(mp->mnt_stat.f_mntfromspec, fspec, MNAMELEN);
184 
185 	return (0);
186 };
187 
188 /*
189  * Check the descriptor tag for both the correct id and correct checksum.
190  * Return zero if all is good, EINVAL if not.
191  */
192 int
193 udf_checktag(struct desc_tag *tag, uint16_t id)
194 {
195 	uint8_t *itag;
196 	uint8_t i, cksum = 0;
197 
198 	itag = (uint8_t *)tag;
199 
200 	if (letoh16(tag->id) != id)
201 		return (EINVAL);
202 
203 	for (i = 0; i < 15; i++)
204 		cksum = cksum + itag[i];
205 	cksum = cksum - itag[4];
206 
207 	if (cksum == tag->cksum)
208 		return (0);
209 
210 	return (EINVAL);
211 }
212 
213 int
214 udf_mountfs(struct vnode *devvp, struct mount *mp, uint32_t lb, struct proc *p)
215 {
216 	struct buf *bp = NULL;
217 	struct anchor_vdp avdp;
218 	struct umount *ump = NULL;
219 	struct part_desc *pd;
220 	struct logvol_desc *lvd;
221 	struct fileset_desc *fsd;
222 	struct extfile_entry *xfentry;
223 	struct file_entry *fentry;
224 	uint32_t sector, size, mvds_start, mvds_end;
225 	uint32_t fsd_offset = 0;
226 	uint16_t part_num = 0, fsd_part = 0;
227 	int error = EINVAL;
228 	int logvol_found = 0, part_found = 0, fsd_found = 0;
229 	int bsize;
230 
231 	/*
232 	 * Disallow multiple mounts of the same device.
233 	 * Disallow mounting of a device that is currently in use
234 	 * (except for root, which might share swap device for miniroot).
235 	 * Flush out any old buffers remaining from a previous use.
236 	 */
237 	if ((error = vfs_mountedon(devvp)))
238 		return (error);
239 	if (vcount(devvp) > 1 && devvp != rootvp)
240 		return (EBUSY);
241 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
242 	error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0);
243 	VOP_UNLOCK(devvp, p);
244 	if (error)
245 		return (error);
246 
247 	error = VOP_OPEN(devvp, FREAD, FSCRED, p);
248 	if (error)
249 		return (error);
250 
251 	ump = malloc(sizeof(*ump), M_UDFMOUNT, M_WAITOK | M_ZERO);
252 
253 	mp->mnt_data = ump;
254 	mp->mnt_stat.f_fsid.val[0] = devvp->v_rdev;
255 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
256 	mp->mnt_stat.f_namemax = NAME_MAX;
257 	mp->mnt_flag |= MNT_LOCAL;
258 
259 	ump->um_mountp = mp;
260 	ump->um_dev = devvp->v_rdev;
261 	ump->um_devvp = devvp;
262 
263 	bsize = 2048;	/* Should probe the media for its size. */
264 
265 	/*
266 	 * Get the Anchor Volume Descriptor Pointer from sector 256.
267 	 * Should also check sector n - 256, n, and 512.
268 	 */
269 	sector = 256;
270 	if ((error = bread(devvp, sector * btodb(bsize), bsize, &bp)) != 0)
271 		goto bail;
272 	if ((error = udf_checktag((struct desc_tag *)bp->b_data, TAGID_ANCHOR)))
273 		goto bail;
274 
275 	bcopy(bp->b_data, &avdp, sizeof(struct anchor_vdp));
276 	brelse(bp);
277 	bp = NULL;
278 
279 	/*
280 	 * Extract the Partition Descriptor and Logical Volume Descriptor
281 	 * from the Volume Descriptor Sequence.
282 	 * Should we care about the partition type right now?
283 	 * What about multiple partitions?
284 	 */
285 	mvds_start = letoh32(avdp.main_vds_ex.loc);
286 	mvds_end = mvds_start + (letoh32(avdp.main_vds_ex.len) - 1) / bsize;
287 	for (sector = mvds_start; sector < mvds_end; sector++) {
288 		if ((error = bread(devvp, sector * btodb(bsize), bsize,
289 				   &bp)) != 0) {
290 			printf("Can't read sector %d of VDS\n", sector);
291 			goto bail;
292 		}
293 		lvd = (struct logvol_desc *)bp->b_data;
294 		if (!udf_checktag(&lvd->tag, TAGID_LOGVOL)) {
295 			ump->um_bsize = letoh32(lvd->lb_size);
296 			ump->um_bmask = ump->um_bsize - 1;
297 			ump->um_bshift = ffs(ump->um_bsize) - 1;
298 			fsd_part = letoh16(lvd->_lvd_use.fsd_loc.loc.part_num);
299 			fsd_offset = letoh32(lvd->_lvd_use.fsd_loc.loc.lb_num);
300 			if (udf_find_partmaps(ump, lvd))
301 				break;
302 			logvol_found = 1;
303 		}
304 		pd = (struct part_desc *)bp->b_data;
305 		if (!udf_checktag(&pd->tag, TAGID_PARTITION)) {
306 			part_found = 1;
307 			part_num = letoh16(pd->part_num);
308 			ump->um_len = ump->um_reallen = letoh32(pd->part_len);
309 			ump->um_start = ump->um_realstart = letoh32(pd->start_loc);
310 		}
311 
312 		brelse(bp);
313 		bp = NULL;
314 		if ((part_found) && (logvol_found))
315 			break;
316 	}
317 
318 	if (!part_found || !logvol_found) {
319 		error = EINVAL;
320 		goto bail;
321 	}
322 
323 	if (ISSET(ump->um_flags, UDF_MNT_USES_META)) {
324 		/* Read Metadata File 'File Entry' to find Metadata file. */
325 		struct long_ad *la;
326 		sector = ump->um_start + ump->um_meta_start; /* Set in udf_get_mpartmap() */
327 		if ((error = RDSECTOR(devvp, sector, ump->um_bsize, &bp)) != 0) {
328 			printf("Cannot read sector %d for Metadata File Entry\n", sector);
329 			error = EINVAL;
330 			goto bail;
331 		}
332 		xfentry = (struct extfile_entry *)bp->b_data;
333 		fentry = (struct file_entry *)bp->b_data;
334 		if (udf_checktag(&xfentry->tag, TAGID_EXTFENTRY) == 0)
335 			la = (struct long_ad *)&xfentry->data[letoh32(xfentry->l_ea)];
336 		else if (udf_checktag(&fentry->tag, TAGID_FENTRY) == 0)
337 			la = (struct long_ad *)&fentry->data[letoh32(fentry->l_ea)];
338 		else {
339 			printf("Invalid Metadata File FE @ sector %d! (tag.id %d)\n",
340 			    sector, fentry->tag.id);
341 			error = EINVAL;
342 			goto bail;
343 		}
344 		ump->um_meta_start = letoh32(la->loc.lb_num);
345 		ump->um_meta_len = letoh32(la->len);
346 		if (bp != NULL) {
347 			brelse(bp);
348 			bp = NULL;
349 		}
350 	} else if (fsd_part != part_num) {
351 		printf("FSD does not lie within the partition!\n");
352 		error = EINVAL;
353 		goto bail;
354 	}
355 
356 	mtx_init(&ump->um_hashmtx, IPL_NONE);
357 	ump->um_hashtbl = hashinit(UDF_HASHTBLSIZE, M_UDFMOUNT, M_WAITOK,
358 	    &ump->um_hashsz);
359 	arc4random_buf(&ump->um_hashkey, sizeof(ump->um_hashkey));
360 
361 	/* Get the VAT, if needed */
362 	if (ump->um_flags & UDF_MNT_FIND_VAT) {
363 		error = udf_vat_get(ump, lb);
364 		if (error)
365 			goto bail;
366 	}
367 
368 	/*
369 	 * Grab the Fileset Descriptor
370 	 * Thanks to Chuck McCrobie <mccrobie@cablespeed.com> for pointing
371 	 * me in the right direction here.
372 	 */
373 
374 	if (ISSET(ump->um_flags, UDF_MNT_USES_META))
375 		sector = ump->um_meta_start;
376 	else
377 		sector = fsd_offset;
378 	udf_vat_map(ump, &sector);
379 	if ((error = RDSECTOR(devvp, sector, ump->um_bsize, &bp)) != 0) {
380 		printf("Cannot read sector %d of FSD\n", sector);
381 		goto bail;
382 	}
383 	fsd = (struct fileset_desc *)bp->b_data;
384 	if (!udf_checktag(&fsd->tag, TAGID_FSD)) {
385 		fsd_found = 1;
386 		bcopy(&fsd->rootdir_icb, &ump->um_root_icb,
387 		    sizeof(struct long_ad));
388 		if (ISSET(ump->um_flags, UDF_MNT_USES_META)) {
389 			ump->um_root_icb.loc.lb_num += ump->um_meta_start;
390 			ump->um_root_icb.loc.part_num = part_num;
391 		}
392 	}
393 
394 	brelse(bp);
395 	bp = NULL;
396 
397 	if (!fsd_found) {
398 		printf("Couldn't find the fsd\n");
399 		error = EINVAL;
400 		goto bail;
401 	}
402 
403 	/*
404 	 * Find the file entry for the root directory.
405 	 */
406 	sector = letoh32(ump->um_root_icb.loc.lb_num);
407 	size = letoh32(ump->um_root_icb.len);
408 	udf_vat_map(ump, &sector);
409 	if ((error = udf_readlblks(ump, sector, size, &bp)) != 0) {
410 		printf("Cannot read sector %d\n", sector);
411 		goto bail;
412 	}
413 
414 	xfentry = (struct extfile_entry *)bp->b_data;
415 	fentry = (struct file_entry *)bp->b_data;
416 	error = udf_checktag(&xfentry->tag, TAGID_EXTFENTRY);
417 	if (error) {
418 	    	error = udf_checktag(&fentry->tag, TAGID_FENTRY);
419 		if (error) {
420 			printf("Invalid root file entry!\n");
421 			goto bail;
422 		}
423 	}
424 
425 	brelse(bp);
426 	bp = NULL;
427 
428 	devvp->v_specmountpoint = mp;
429 
430 	return (0);
431 
432 bail:
433 	hashfree(ump->um_hashtbl, UDF_HASHTBLSIZE, M_UDFMOUNT);
434 
435 	if (ump != NULL) {
436 		free(ump, M_UDFMOUNT, 0);
437 		mp->mnt_data = NULL;
438 		mp->mnt_flag &= ~MNT_LOCAL;
439 	}
440 	if (devvp->v_specinfo)
441 		devvp->v_specmountpoint = NULL;
442 	if (bp != NULL)
443 		brelse(bp);
444 
445 	vn_lock(devvp, LK_EXCLUSIVE|LK_RETRY, p);
446 	VOP_CLOSE(devvp, FREAD, FSCRED, p);
447 	VOP_UNLOCK(devvp, p);
448 
449 	return (error);
450 }
451 
452 int
453 udf_unmount(struct mount *mp, int mntflags, struct proc *p)
454 {
455 	struct umount *ump;
456 	struct vnode *devvp;
457 	int error, flags = 0;
458 
459 	ump = VFSTOUDFFS(mp);
460 	devvp = ump->um_devvp;
461 
462 	if (mntflags & MNT_FORCE)
463 		flags |= FORCECLOSE;
464 
465 	if ((error = vflush(mp, NULL, flags)))
466 		return (error);
467 
468 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
469 	vinvalbuf(devvp, V_SAVE, NOCRED, p, 0, 0);
470 	(void)VOP_CLOSE(devvp, FREAD, NOCRED, p);
471 	VOP_UNLOCK(devvp, p);
472 
473 	devvp->v_specmountpoint = NULL;
474 	vrele(devvp);
475 
476 	if (ump->um_flags & UDF_MNT_USES_VAT)
477 		free(ump->um_vat, M_UDFMOUNT, 0);
478 
479 	if (ump->um_stbl != NULL)
480 		free(ump->um_stbl, M_UDFMOUNT, 0);
481 
482 	hashfree(ump->um_hashtbl, UDF_HASHTBLSIZE, M_UDFMOUNT);
483 	free(ump, M_UDFMOUNT, 0);
484 
485 	mp->mnt_data = NULL;
486 	mp->mnt_flag &= ~MNT_LOCAL;
487 
488 	return (0);
489 }
490 
491 int
492 udf_root(struct mount *mp, struct vnode **vpp)
493 {
494 	struct umount *ump;
495 	struct vnode *vp;
496 	udfino_t id;
497 	int error;
498 
499 	ump = VFSTOUDFFS(mp);
500 
501 	id = udf_getid(&ump->um_root_icb);
502 
503 	error = udf_vget(mp, id, vpp);
504 	if (error)
505 		return (error);
506 
507 	vp = *vpp;
508 	vp->v_flag |= VROOT;
509 
510 	return (0);
511 }
512 
513 int
514 udf_quotactl(struct mount *mp, int cmds, uid_t uid, caddr_t arg,
515     struct proc *p)
516 {
517 	return (EOPNOTSUPP);
518 }
519 
520 int
521 udf_statfs(struct mount *mp, struct statfs *sbp, struct proc *p)
522 {
523 	struct umount *ump;
524 
525 	ump = VFSTOUDFFS(mp);
526 
527 	sbp->f_bsize = ump->um_bsize;
528 	sbp->f_iosize = ump->um_bsize;
529 	sbp->f_blocks = ump->um_len;
530 	sbp->f_bfree = 0;
531 	sbp->f_bavail = 0;
532 	sbp->f_files = 0;
533 	sbp->f_ffree = 0;
534 	sbp->f_favail = 0;
535 	copy_statfs_info(sbp, mp);
536 
537 	return (0);
538 }
539 
540 int
541 udf_sync(struct mount *mp, int waitfor, struct ucred *cred, struct proc *p)
542 {
543 	return (0);
544 }
545 
546 int
547 udf_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
548 {
549 	struct buf *bp;
550 	struct vnode *devvp;
551 	struct umount *ump;
552 	struct proc *p;
553 	struct vnode *vp, *nvp;
554 	struct unode *up;
555 	struct extfile_entry *xfe;
556 	struct file_entry *fe;
557 	uint32_t sector;
558 	int error, size;
559 
560 	if (ino > (udfino_t)-1)
561 		panic("udf_vget: alien ino_t %llu", (unsigned long long)ino);
562 
563 	p = curproc;
564 	bp = NULL;
565 	*vpp = NULL;
566 	ump = VFSTOUDFFS(mp);
567 
568 	/* See if we already have this in the cache */
569 	if ((error = udf_hashlookup(ump, ino, LK_EXCLUSIVE, vpp)) != 0)
570 		return (error);
571 	if (*vpp != NULL)
572 		return (0);
573 
574 	/*
575 	 * Allocate memory and check the tag id's before grabbing a new
576 	 * vnode, since it's hard to roll back if there is a problem.
577 	 */
578 	up = pool_get(&unode_pool, PR_WAITOK | PR_ZERO);
579 
580 	/*
581 	 * Copy in the file entry.  Per the spec, the size can only be 1 block.
582 	 */
583 	sector = ino;
584 	devvp = ump->um_devvp;
585 	udf_vat_map(ump, &sector);
586 	if ((error = RDSECTOR(devvp, sector, ump->um_bsize, &bp)) != 0) {
587 		printf("Cannot read sector %d\n", sector);
588 		pool_put(&unode_pool, up);
589 		if (bp != NULL)
590 			brelse(bp);
591 		return (error);
592 	}
593 
594 	xfe = (struct extfile_entry *)bp->b_data;
595 	fe = (struct file_entry *)bp->b_data;
596 	error = udf_checktag(&xfe->tag, TAGID_EXTFENTRY);
597 	if (error == 0) {
598 		size = letoh32(xfe->l_ea) + letoh32(xfe->l_ad);
599 	} else {
600 		error = udf_checktag(&fe->tag, TAGID_FENTRY);
601 		if (error) {
602 			printf("Invalid file entry!\n");
603 			pool_put(&unode_pool, up);
604 			if (bp != NULL)
605 				brelse(bp);
606 			return (ENOMEM);
607 		} else
608 			size = letoh32(fe->l_ea) + letoh32(fe->l_ad);
609 	}
610 
611 	/* Allocate max size of FE/XFE. */
612 	up->u_fentry = malloc(size + UDF_EXTFENTRY_SIZE, M_UDFFENTRY, M_NOWAIT | M_ZERO);
613 	if (up->u_fentry == NULL) {
614 		pool_put(&unode_pool, up);
615 		if (bp != NULL)
616 			brelse(bp);
617 		return (ENOMEM); /* Cannot allocate file entry block */
618 	}
619 
620 	if (udf_checktag(&xfe->tag, TAGID_EXTFENTRY) == 0)
621 		bcopy(bp->b_data, up->u_fentry, size + UDF_EXTFENTRY_SIZE);
622 	else
623 		bcopy(bp->b_data, up->u_fentry, size + UDF_FENTRY_SIZE);
624 
625 	brelse(bp);
626 	bp = NULL;
627 
628 	if ((error = udf_allocv(mp, &vp, p))) {
629 		free(up->u_fentry, M_UDFFENTRY, 0);
630 		pool_put(&unode_pool, up);
631 		return (error); /* Error from udf_allocv() */
632 	}
633 
634 	up->u_vnode = vp;
635 	up->u_ino = ino;
636 	up->u_devvp = ump->um_devvp;
637 	up->u_dev = ump->um_dev;
638 	up->u_ump = ump;
639 	vp->v_data = up;
640 	vref(ump->um_devvp);
641 
642 	rrw_init(&up->u_lock, "unode");
643 
644 	/*
645 	 * udf_hashins() will lock the vnode for us.
646 	 */
647 	udf_hashins(up);
648 
649 	switch (up->u_fentry->icbtag.file_type) {
650 	default:
651 		printf("Unrecognized file type (%d)\n", vp->v_type);
652 		vp->v_type = VREG;
653 		break;
654 	case UDF_ICB_FILETYPE_DIRECTORY:
655 		vp->v_type = VDIR;
656 		break;
657 	case UDF_ICB_FILETYPE_BLOCKDEVICE:
658 		vp->v_type = VBLK;
659 		break;
660 	case UDF_ICB_FILETYPE_CHARDEVICE:
661 		vp->v_type = VCHR;
662 		break;
663 	case UDF_ICB_FILETYPE_FIFO:
664 		vp->v_type = VFIFO;
665 		break;
666 	case UDF_ICB_FILETYPE_SOCKET:
667 		vp->v_type = VSOCK;
668 		break;
669 	case UDF_ICB_FILETYPE_SYMLINK:
670 		vp->v_type = VLNK;
671 		break;
672 	case UDF_ICB_FILETYPE_RANDOMACCESS:
673 	case UDF_ICB_FILETYPE_REALTIME:
674 	case UDF_ICB_FILETYPE_UNKNOWN:
675 		vp->v_type = VREG;
676 		break;
677 	}
678 
679 	/* check if this is a vnode alias */
680 	if ((nvp = checkalias(vp, up->u_dev, ump->um_mountp)) != NULL) {
681 		printf("found a vnode alias\n");
682 		/*
683 		 * Discard unneeded vnode, but save its udf_node.
684 		 * Note that the lock is carried over in the udf_node
685 		 */
686 		nvp->v_data = vp->v_data;
687 		vp->v_data = NULL;
688 		vp->v_op = &spec_vops;
689 		vrele(vp);
690 		vgone(vp);
691 		/*
692 		 * Reinitialize aliased inode.
693 		 */
694 		vp = nvp;
695 		ump->um_devvp = vp;
696 	}
697 
698 	*vpp = vp;
699 
700 	return (0);
701 }
702 
703 struct ifid {
704 	u_short	ifid_len;
705 	u_short	ifid_pad;
706 	int	ifid_ino;
707 	long	ifid_start;
708 };
709 
710 int
711 udf_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
712 {
713 	struct ifid *ifhp;
714 	struct vnode *nvp;
715 	int error;
716 
717 	ifhp = (struct ifid *)fhp;
718 
719 	if ((error = VFS_VGET(mp, ifhp->ifid_ino, &nvp)) != 0) {
720 		*vpp = NULLVP;
721 		return (error);
722 	}
723 
724 	*vpp = nvp;
725 
726 	return (0);
727 }
728 
729 int
730 udf_vptofh(struct vnode *vp, struct fid *fhp)
731 {
732 	struct unode *up;
733 	struct ifid *ifhp;
734 
735 	up = VTOU(vp);
736 	ifhp = (struct ifid *)fhp;
737 	ifhp->ifid_len = sizeof(struct ifid);
738 	ifhp->ifid_ino = up->u_ino;
739 
740 	return (0);
741 }
742 
743 int
744 udf_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
745     size_t newlen, struct proc *p)
746 {
747 	return (EINVAL);
748 }
749 
750 int
751 udf_checkexp(struct mount *mp, struct mbuf *nam, int *exflagsp,
752     struct ucred **credanonp)
753 {
754 	return (EACCES); /* For the time being */
755 }
756 
757 /* Handle a virtual partition map */
758 int
759 udf_get_vpartmap(struct umount *ump, struct part_map_virt *pmv)
760 {
761 	ump->um_flags |= UDF_MNT_FIND_VAT; /* Should do more than this */
762 	return (0);
763 }
764 
765 /* Handle a sparable partition map */
766 int
767 udf_get_spartmap(struct umount *ump, struct part_map_spare *pms)
768 {
769 	struct buf *bp;
770 	int i, error;
771 
772 	ump->um_stbl = malloc(letoh32(pms->st_size), M_UDFMOUNT, M_NOWAIT);
773 	if (ump->um_stbl == NULL)
774 		return (ENOMEM);
775 
776 	bzero(ump->um_stbl, letoh32(pms->st_size));
777 
778 	/* Calculate the number of sectors per packet */
779 	ump->um_psecs = letoh16(pms->packet_len) / ump->um_bsize;
780 
781 	error = udf_readlblks(ump, letoh32(pms->st_loc[0]),
782 	    letoh32(pms->st_size), &bp);
783 
784 	if (error) {
785 		if (bp != NULL)
786 			brelse(bp);
787 		free(ump->um_stbl, M_UDFMOUNT, 0);
788 		return (error); /* Failed to read sparing table */
789 	}
790 
791 	bcopy(bp->b_data, ump->um_stbl, letoh32(pms->st_size));
792 	brelse(bp);
793 	bp = NULL;
794 
795 	if (udf_checktag(&ump->um_stbl->tag, 0)) {
796 		free(ump->um_stbl, M_UDFMOUNT, 0);
797 		return (EINVAL); /* Invalid sparing table found */
798 	}
799 
800 	/*
801 	 * See how many valid entries there are here. The list is
802 	 * supposed to be sorted, 0xfffffff0 and higher are not valid.
803 	 */
804 	for (i = 0; i < letoh16(ump->um_stbl->rt_l); i++) {
805 		ump->um_stbl_len = i;
806 		if (letoh32(ump->um_stbl->entries[i].org) >= 0xfffffff0)
807 			break;
808 	}
809 
810 	return (0);
811 }
812 
813 /* Handle a metadata partition map */
814 int
815 udf_get_mpartmap(struct umount *ump, struct part_map_meta *pmm)
816 {
817 	ump->um_flags |= UDF_MNT_USES_META;
818 	ump->um_meta_start = pmm->meta_file_lbn;
819 	return (0);
820 }
821 
822 /* Scan the partition maps */
823 int
824 udf_find_partmaps(struct umount *ump, struct logvol_desc *lvd)
825 {
826 	struct regid *pmap_id;
827 	unsigned char regid_id[UDF_REGID_ID_SIZE + 1];
828 	int i, ptype, psize, error;
829 	uint8_t *pmap = (uint8_t *) &lvd->maps[0];
830 
831 	for (i = 0; i < letoh32(lvd->n_pm); i++) {
832 		ptype = pmap[0];
833 		psize = pmap[1];
834 
835 		if (ptype != 1 && ptype != 2)
836 			return (EINVAL); /* Invalid partition map type */
837 
838 		if (psize != sizeof(struct part_map_1)  &&
839 		    psize != sizeof(struct part_map_2))
840 			return (EINVAL); /* Invalid partition map size */
841 
842 		if (ptype == 1) {
843 			pmap += sizeof(struct part_map_1);
844 			continue;
845 		}
846 
847 		/* Type 2 map. Find out the details */
848 		pmap_id = (struct regid *) &pmap[4];
849 		regid_id[UDF_REGID_ID_SIZE] = '\0';
850 		bcopy(&pmap_id->id[0], &regid_id[0], UDF_REGID_ID_SIZE);
851 
852 		if (!bcmp(&regid_id[0], "*UDF Virtual Partition",
853 		    UDF_REGID_ID_SIZE))
854 			error = udf_get_vpartmap(ump,
855 			    (struct part_map_virt *) pmap);
856 		else if (!bcmp(&regid_id[0], "*UDF Sparable Partition",
857 		    UDF_REGID_ID_SIZE))
858 			error = udf_get_spartmap(ump,
859 			    (struct part_map_spare *) pmap);
860 		else if (!bcmp(&regid_id[0], "*UDF Metadata Partition",
861 		    UDF_REGID_ID_SIZE))
862 			error = udf_get_mpartmap(ump,
863 			    (struct part_map_meta *) pmap);
864 		else
865 			return (EINVAL); /* Unsupported partition map */
866 
867 		if (error)
868 			return (error); /* Error getting partition */
869 
870 		pmap += sizeof(struct part_map_2);
871 	}
872 
873 	return (0);
874 }
875