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