xref: /openbsd-src/sys/isofs/udf/udf_vfsops.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: udf_vfsops.c,v 1.56 2016/09/15 02:00:18 dlg 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 	if (ump->um_hashtbl != NULL)
434 		free(ump->um_hashtbl, M_UDFMOUNT, 0);
435 
436 	if (ump != NULL) {
437 		free(ump, M_UDFMOUNT, 0);
438 		mp->mnt_data = NULL;
439 		mp->mnt_flag &= ~MNT_LOCAL;
440 	}
441 	if (devvp->v_specinfo)
442 		devvp->v_specmountpoint = NULL;
443 	if (bp != NULL)
444 		brelse(bp);
445 
446 	vn_lock(devvp, LK_EXCLUSIVE|LK_RETRY, p);
447 	VOP_CLOSE(devvp, FREAD, FSCRED, p);
448 	VOP_UNLOCK(devvp, p);
449 
450 	return (error);
451 }
452 
453 int
454 udf_unmount(struct mount *mp, int mntflags, struct proc *p)
455 {
456 	struct umount *ump;
457 	struct vnode *devvp;
458 	int error, flags = 0;
459 
460 	ump = VFSTOUDFFS(mp);
461 	devvp = ump->um_devvp;
462 
463 	if (mntflags & MNT_FORCE)
464 		flags |= FORCECLOSE;
465 
466 	if ((error = vflush(mp, NULL, flags)))
467 		return (error);
468 
469 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
470 	vinvalbuf(devvp, V_SAVE, NOCRED, p, 0, 0);
471 	(void)VOP_CLOSE(devvp, FREAD, NOCRED, p);
472 	VOP_UNLOCK(devvp, p);
473 
474 	devvp->v_specmountpoint = NULL;
475 	vrele(devvp);
476 
477 	if (ump->um_flags & UDF_MNT_USES_VAT)
478 		free(ump->um_vat, M_UDFMOUNT, 0);
479 
480 	if (ump->um_stbl != NULL)
481 		free(ump->um_stbl, M_UDFMOUNT, 0);
482 
483 	if (ump->um_hashtbl != NULL)
484 		free(ump->um_hashtbl, M_UDFMOUNT, 0);
485 
486 	free(ump, M_UDFMOUNT, 0);
487 
488 	mp->mnt_data = NULL;
489 	mp->mnt_flag &= ~MNT_LOCAL;
490 
491 	return (0);
492 }
493 
494 int
495 udf_root(struct mount *mp, struct vnode **vpp)
496 {
497 	struct umount *ump;
498 	struct vnode *vp;
499 	udfino_t id;
500 	int error;
501 
502 	ump = VFSTOUDFFS(mp);
503 
504 	id = udf_getid(&ump->um_root_icb);
505 
506 	error = udf_vget(mp, id, vpp);
507 	if (error)
508 		return (error);
509 
510 	vp = *vpp;
511 	vp->v_flag |= VROOT;
512 
513 	return (0);
514 }
515 
516 int
517 udf_quotactl(struct mount *mp, int cmds, uid_t uid, caddr_t arg,
518     struct proc *p)
519 {
520 	return (EOPNOTSUPP);
521 }
522 
523 int
524 udf_statfs(struct mount *mp, struct statfs *sbp, struct proc *p)
525 {
526 	struct umount *ump;
527 
528 	ump = VFSTOUDFFS(mp);
529 
530 	sbp->f_bsize = ump->um_bsize;
531 	sbp->f_iosize = ump->um_bsize;
532 	sbp->f_blocks = ump->um_len;
533 	sbp->f_bfree = 0;
534 	sbp->f_bavail = 0;
535 	sbp->f_files = 0;
536 	sbp->f_ffree = 0;
537 	sbp->f_favail = 0;
538 	copy_statfs_info(sbp, mp);
539 
540 	return (0);
541 }
542 
543 int
544 udf_sync(struct mount *mp, int waitfor, struct ucred *cred, struct proc *p)
545 {
546 	return (0);
547 }
548 
549 int
550 udf_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
551 {
552 	struct buf *bp;
553 	struct vnode *devvp;
554 	struct umount *ump;
555 	struct proc *p;
556 	struct vnode *vp, *nvp;
557 	struct unode *up;
558 	struct extfile_entry *xfe;
559 	struct file_entry *fe;
560 	uint32_t sector;
561 	int error, size;
562 
563 	if (ino > (udfino_t)-1)
564 		panic("udf_vget: alien ino_t %llu", (unsigned long long)ino);
565 
566 	p = curproc;
567 	bp = NULL;
568 	*vpp = NULL;
569 	ump = VFSTOUDFFS(mp);
570 
571 	/* See if we already have this in the cache */
572 	if ((error = udf_hashlookup(ump, ino, LK_EXCLUSIVE, vpp)) != 0)
573 		return (error);
574 	if (*vpp != NULL)
575 		return (0);
576 
577 	/*
578 	 * Allocate memory and check the tag id's before grabbing a new
579 	 * vnode, since it's hard to roll back if there is a problem.
580 	 */
581 	up = pool_get(&unode_pool, PR_WAITOK | PR_ZERO);
582 
583 	/*
584 	 * Copy in the file entry.  Per the spec, the size can only be 1 block.
585 	 */
586 	sector = ino;
587 	devvp = ump->um_devvp;
588 	udf_vat_map(ump, &sector);
589 	if ((error = RDSECTOR(devvp, sector, ump->um_bsize, &bp)) != 0) {
590 		printf("Cannot read sector %d\n", sector);
591 		pool_put(&unode_pool, up);
592 		if (bp != NULL)
593 			brelse(bp);
594 		return (error);
595 	}
596 
597 	xfe = (struct extfile_entry *)bp->b_data;
598 	fe = (struct file_entry *)bp->b_data;
599 	error = udf_checktag(&xfe->tag, TAGID_EXTFENTRY);
600 	if (error == 0) {
601 		size = letoh32(xfe->l_ea) + letoh32(xfe->l_ad);
602 	} else {
603 		error = udf_checktag(&fe->tag, TAGID_FENTRY);
604 		if (error) {
605 			printf("Invalid file entry!\n");
606 			pool_put(&unode_pool, up);
607 			if (bp != NULL)
608 				brelse(bp);
609 			return (ENOMEM);
610 		} else
611 			size = letoh32(fe->l_ea) + letoh32(fe->l_ad);
612 	}
613 
614 	/* Allocate max size of FE/XFE. */
615 	up->u_fentry = malloc(size + UDF_EXTFENTRY_SIZE, M_UDFFENTRY, M_NOWAIT | M_ZERO);
616 	if (up->u_fentry == NULL) {
617 		pool_put(&unode_pool, up);
618 		if (bp != NULL)
619 			brelse(bp);
620 		return (ENOMEM); /* Cannot allocate file entry block */
621 	}
622 
623 	if (udf_checktag(&xfe->tag, TAGID_EXTFENTRY) == 0)
624 		bcopy(bp->b_data, up->u_fentry, size + UDF_EXTFENTRY_SIZE);
625 	else
626 		bcopy(bp->b_data, up->u_fentry, size + UDF_FENTRY_SIZE);
627 
628 	brelse(bp);
629 	bp = NULL;
630 
631 	if ((error = udf_allocv(mp, &vp, p))) {
632 		free(up->u_fentry, M_UDFFENTRY, 0);
633 		pool_put(&unode_pool, up);
634 		return (error); /* Error from udf_allocv() */
635 	}
636 
637 	up->u_vnode = vp;
638 	up->u_ino = ino;
639 	up->u_devvp = ump->um_devvp;
640 	up->u_dev = ump->um_dev;
641 	up->u_ump = ump;
642 	vp->v_data = up;
643 	vref(ump->um_devvp);
644 
645 	rrw_init(&up->u_lock, "unode");
646 
647 	/*
648 	 * udf_hashins() will lock the vnode for us.
649 	 */
650 	udf_hashins(up);
651 
652 	switch (up->u_fentry->icbtag.file_type) {
653 	default:
654 		printf("Unrecognized file type (%d)\n", vp->v_type);
655 		vp->v_type = VREG;
656 		break;
657 	case UDF_ICB_FILETYPE_DIRECTORY:
658 		vp->v_type = VDIR;
659 		break;
660 	case UDF_ICB_FILETYPE_BLOCKDEVICE:
661 		vp->v_type = VBLK;
662 		break;
663 	case UDF_ICB_FILETYPE_CHARDEVICE:
664 		vp->v_type = VCHR;
665 		break;
666 	case UDF_ICB_FILETYPE_FIFO:
667 		vp->v_type = VFIFO;
668 		break;
669 	case UDF_ICB_FILETYPE_SOCKET:
670 		vp->v_type = VSOCK;
671 		break;
672 	case UDF_ICB_FILETYPE_SYMLINK:
673 		vp->v_type = VLNK;
674 		break;
675 	case UDF_ICB_FILETYPE_RANDOMACCESS:
676 	case UDF_ICB_FILETYPE_REALTIME:
677 	case UDF_ICB_FILETYPE_UNKNOWN:
678 		vp->v_type = VREG;
679 		break;
680 	}
681 
682 	/* check if this is a vnode alias */
683 	if ((nvp = checkalias(vp, up->u_dev, ump->um_mountp)) != NULL) {
684 		printf("found a vnode alias\n");
685 		/*
686 		 * Discard unneeded vnode, but save its udf_node.
687 		 * Note that the lock is carried over in the udf_node
688 		 */
689 		nvp->v_data = vp->v_data;
690 		vp->v_data = NULL;
691 		vp->v_op = &spec_vops;
692 		vrele(vp);
693 		vgone(vp);
694 		/*
695 		 * Reinitialize aliased inode.
696 		 */
697 		vp = nvp;
698 		ump->um_devvp = vp;
699 	}
700 
701 	*vpp = vp;
702 
703 	return (0);
704 }
705 
706 struct ifid {
707 	u_short	ifid_len;
708 	u_short	ifid_pad;
709 	int	ifid_ino;
710 	long	ifid_start;
711 };
712 
713 int
714 udf_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
715 {
716 	struct ifid *ifhp;
717 	struct vnode *nvp;
718 	int error;
719 
720 	ifhp = (struct ifid *)fhp;
721 
722 	if ((error = VFS_VGET(mp, ifhp->ifid_ino, &nvp)) != 0) {
723 		*vpp = NULLVP;
724 		return (error);
725 	}
726 
727 	*vpp = nvp;
728 
729 	return (0);
730 }
731 
732 int
733 udf_vptofh(struct vnode *vp, struct fid *fhp)
734 {
735 	struct unode *up;
736 	struct ifid *ifhp;
737 
738 	up = VTOU(vp);
739 	ifhp = (struct ifid *)fhp;
740 	ifhp->ifid_len = sizeof(struct ifid);
741 	ifhp->ifid_ino = up->u_ino;
742 
743 	return (0);
744 }
745 
746 int
747 udf_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
748     size_t newlen, struct proc *p)
749 {
750 	return (EINVAL);
751 }
752 
753 int
754 udf_checkexp(struct mount *mp, struct mbuf *nam, int *exflagsp,
755     struct ucred **credanonp)
756 {
757 	return (EACCES); /* For the time being */
758 }
759 
760 /* Handle a virtual partition map */
761 int
762 udf_get_vpartmap(struct umount *ump, struct part_map_virt *pmv)
763 {
764 	ump->um_flags |= UDF_MNT_FIND_VAT; /* Should do more than this */
765 	return (0);
766 }
767 
768 /* Handle a sparable partition map */
769 int
770 udf_get_spartmap(struct umount *ump, struct part_map_spare *pms)
771 {
772 	struct buf *bp;
773 	int i, error;
774 
775 	ump->um_stbl = malloc(letoh32(pms->st_size), M_UDFMOUNT, M_NOWAIT);
776 	if (ump->um_stbl == NULL)
777 		return (ENOMEM);
778 
779 	bzero(ump->um_stbl, letoh32(pms->st_size));
780 
781 	/* Calculate the number of sectors per packet */
782 	ump->um_psecs = letoh16(pms->packet_len) / ump->um_bsize;
783 
784 	error = udf_readlblks(ump, letoh32(pms->st_loc[0]),
785 	    letoh32(pms->st_size), &bp);
786 
787 	if (error) {
788 		if (bp != NULL)
789 			brelse(bp);
790 		free(ump->um_stbl, M_UDFMOUNT, 0);
791 		return (error); /* Failed to read sparing table */
792 	}
793 
794 	bcopy(bp->b_data, ump->um_stbl, letoh32(pms->st_size));
795 	brelse(bp);
796 	bp = NULL;
797 
798 	if (udf_checktag(&ump->um_stbl->tag, 0)) {
799 		free(ump->um_stbl, M_UDFMOUNT, 0);
800 		return (EINVAL); /* Invalid sparing table found */
801 	}
802 
803 	/*
804 	 * See how many valid entries there are here. The list is
805 	 * supposed to be sorted, 0xfffffff0 and higher are not valid.
806 	 */
807 	for (i = 0; i < letoh16(ump->um_stbl->rt_l); i++) {
808 		ump->um_stbl_len = i;
809 		if (letoh32(ump->um_stbl->entries[i].org) >= 0xfffffff0)
810 			break;
811 	}
812 
813 	return (0);
814 }
815 
816 /* Handle a metadata partition map */
817 int
818 udf_get_mpartmap(struct umount *ump, struct part_map_meta *pmm)
819 {
820 	ump->um_flags |= UDF_MNT_USES_META;
821 	ump->um_meta_start = pmm->meta_file_lbn;
822 	return (0);
823 }
824 
825 /* Scan the partition maps */
826 int
827 udf_find_partmaps(struct umount *ump, struct logvol_desc *lvd)
828 {
829 	struct regid *pmap_id;
830 	unsigned char regid_id[UDF_REGID_ID_SIZE + 1];
831 	int i, ptype, psize, error;
832 	uint8_t *pmap = (uint8_t *) &lvd->maps[0];
833 
834 	for (i = 0; i < letoh32(lvd->n_pm); i++) {
835 		ptype = pmap[0];
836 		psize = pmap[1];
837 
838 		if (ptype != 1 && ptype != 2)
839 			return (EINVAL); /* Invalid partition map type */
840 
841 		if (psize != sizeof(struct part_map_1)  &&
842 		    psize != sizeof(struct part_map_2))
843 			return (EINVAL); /* Invalid partition map size */
844 
845 		if (ptype == 1) {
846 			pmap += sizeof(struct part_map_1);
847 			continue;
848 		}
849 
850 		/* Type 2 map. Find out the details */
851 		pmap_id = (struct regid *) &pmap[4];
852 		regid_id[UDF_REGID_ID_SIZE] = '\0';
853 		bcopy(&pmap_id->id[0], &regid_id[0], UDF_REGID_ID_SIZE);
854 
855 		if (!bcmp(&regid_id[0], "*UDF Virtual Partition",
856 		    UDF_REGID_ID_SIZE))
857 			error = udf_get_vpartmap(ump,
858 			    (struct part_map_virt *) pmap);
859 		else if (!bcmp(&regid_id[0], "*UDF Sparable Partition",
860 		    UDF_REGID_ID_SIZE))
861 			error = udf_get_spartmap(ump,
862 			    (struct part_map_spare *) pmap);
863 		else if (!bcmp(&regid_id[0], "*UDF Metadata Partition",
864 		    UDF_REGID_ID_SIZE))
865 			error = udf_get_mpartmap(ump,
866 			    (struct part_map_meta *) pmap);
867 		else
868 			return (EINVAL); /* Unsupported partition map */
869 
870 		if (error)
871 			return (error); /* Error getting partition */
872 
873 		pmap += sizeof(struct part_map_2);
874 	}
875 
876 	return (0);
877 }
878