xref: /netbsd-src/sys/fs/udf/udf_vfsops.c (revision cac8e449158efc7261bebc8657cbb0125a2cfdde)
1 /* $NetBSD: udf_vfsops.c,v 1.46 2008/07/28 19:41:13 reinoud Exp $ */
2 
3 /*
4  * Copyright (c) 2006, 2008 Reinoud Zandijk
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 ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 #ifndef lint
31 __KERNEL_RCSID(0, "$NetBSD: udf_vfsops.c,v 1.46 2008/07/28 19:41:13 reinoud Exp $");
32 #endif /* not lint */
33 
34 
35 #if defined(_KERNEL_OPT)
36 #include "opt_quota.h"
37 #include "opt_compat_netbsd.h"
38 #include "opt_udf.h"
39 #endif
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/sysctl.h>
44 #include <sys/namei.h>
45 #include <sys/proc.h>
46 #include <sys/kernel.h>
47 #include <sys/vnode.h>
48 #include <miscfs/genfs/genfs.h>
49 #include <miscfs/specfs/specdev.h>
50 #include <sys/mount.h>
51 #include <sys/buf.h>
52 #include <sys/file.h>
53 #include <sys/device.h>
54 #include <sys/disklabel.h>
55 #include <sys/ioctl.h>
56 #include <sys/malloc.h>
57 #include <sys/dirent.h>
58 #include <sys/stat.h>
59 #include <sys/conf.h>
60 #include <sys/kauth.h>
61 #include <sys/module.h>
62 
63 #include <fs/udf/ecma167-udf.h>
64 #include <fs/udf/udf_mount.h>
65 
66 #include "udf.h"
67 #include "udf_subr.h"
68 #include "udf_bswap.h"
69 
70 MODULE(MODULE_CLASS_VFS, udf, NULL);
71 
72 #define VTOI(vnode) ((struct udf_node *) vnode->v_data)
73 
74 /* verbose levels of the udf filingsystem */
75 int udf_verbose = UDF_DEBUGGING;
76 
77 /* maximum dirhash size of all UDF filesystems combined(!) */
78 kmutex_t udf_dirhashmutex;
79 uint32_t udf_maxdirhashsize = UDF_DIRHASH_DEFAULTMEM;
80 uint32_t udf_dirhashsize    = 0;
81 struct _udf_dirhash udf_dirhash_queue;
82 
83 /* malloc regions */
84 MALLOC_JUSTDEFINE(M_UDFMNT,   "UDF mount",	"UDF mount structures");
85 MALLOC_JUSTDEFINE(M_UDFVOLD,  "UDF volspace",	"UDF volume space descriptors");
86 MALLOC_JUSTDEFINE(M_UDFTEMP,  "UDF temp",	"UDF scrap space");
87 struct pool udf_node_pool;
88 
89 /* supported functions predefined */
90 VFS_PROTOS(udf);
91 
92 static struct sysctllog *udf_sysctl_log;
93 
94 /* internal functions */
95 static int udf_mountfs(struct vnode *, struct mount *, struct lwp *, struct udf_args *);
96 
97 
98 /* --------------------------------------------------------------------- */
99 
100 /* predefine vnode-op list descriptor */
101 extern const struct vnodeopv_desc udf_vnodeop_opv_desc;
102 
103 const struct vnodeopv_desc * const udf_vnodeopv_descs[] = {
104 	&udf_vnodeop_opv_desc,
105 	NULL,
106 };
107 
108 
109 /* vfsops descriptor linked in as anchor point for the filingsystem */
110 struct vfsops udf_vfsops = {
111 	MOUNT_UDF,			/* vfs_name */
112 	sizeof (struct udf_args),
113 	udf_mount,
114 	udf_start,
115 	udf_unmount,
116 	udf_root,
117 	(void *)eopnotsupp,		/* vfs_quotactl */
118 	udf_statvfs,
119 	udf_sync,
120 	udf_vget,
121 	udf_fhtovp,
122 	udf_vptofh,
123 	udf_init,
124 	udf_reinit,
125 	udf_done,
126 	udf_mountroot,
127 	udf_snapshot,
128 	vfs_stdextattrctl,
129 	(void *)eopnotsupp,		/* vfs_suspendctl */
130 	genfs_renamelock_enter,
131 	genfs_renamelock_exit,
132 	(void *)eopnotsupp,
133 	udf_vnodeopv_descs,
134 	0, /* int vfs_refcount   */
135 	{ NULL, NULL, }, /* LIST_ENTRY(vfsops) */
136 };
137 
138 /* --------------------------------------------------------------------- */
139 
140 /* file system starts here */
141 void
142 udf_init(void)
143 {
144 	size_t size;
145 	uint32_t max_entries;
146 
147 	/* initialise dirhash queue */
148 	TAILQ_INIT(&udf_dirhash_queue);
149 
150 	/* setup memory types */
151 	malloc_type_attach(M_UDFMNT);
152 	malloc_type_attach(M_UDFVOLD);
153 	malloc_type_attach(M_UDFTEMP);
154 
155 	/* init node pools */
156 	size = sizeof(struct udf_node);
157 	pool_init(&udf_node_pool, size, 0, 0, 0,
158 		"udf_node_pool", NULL, IPL_NONE);
159 
160 	/* init dirhash pools */
161 	size = sizeof(struct udf_dirhash);
162 	pool_init(&udf_dirhash_pool, size, 0, 0, 0,
163 		"udf_dirhash_pool", NULL, IPL_NONE);
164 
165 	size = sizeof(struct udf_dirhash_entry);
166 	pool_init(&udf_dirhash_entry_pool, size, 0, 0, 0,
167 		"udf_dirhash_entry_pool", NULL, IPL_NONE);
168 
169 	mutex_init(&udf_dirhashmutex, MUTEX_DEFAULT, IPL_NONE);
170 	max_entries = udf_maxdirhashsize / size;
171 	pool_sethiwat(&udf_dirhash_entry_pool, max_entries);
172 	udf_dirhashsize = 0;
173 }
174 
175 
176 void
177 udf_reinit(void)
178 {
179 	/* nothing to do */
180 }
181 
182 
183 void
184 udf_done(void)
185 {
186 	/* remove pools */
187 	pool_destroy(&udf_node_pool);
188 	pool_destroy(&udf_dirhash_pool);
189 	pool_destroy(&udf_dirhash_entry_pool);
190 
191 	malloc_type_detach(M_UDFMNT);
192 	malloc_type_detach(M_UDFVOLD);
193 	malloc_type_detach(M_UDFTEMP);
194 }
195 
196 /*
197  * If running a DEBUG kernel, provide an easy way to set the debug flags when
198  * running into a problem.
199  */
200 #define UDF_VERBOSE_SYSCTLOPT        1
201 #define UDF_CURDIRHASHSIZE_SYSCTLOPT 2
202 #define UDF_MAXDIRHASHSIZE_SYSCTLOPT 3
203 
204 static int
205 udf_modcmd(modcmd_t cmd, void *arg)
206 {
207 	const struct sysctlnode *node;
208 	int error;
209 
210 	switch (cmd) {
211 	case MODULE_CMD_INIT:
212 		error = vfs_attach(&udf_vfsops);
213 		if (error != 0)
214 			break;
215 		/*
216 		 * XXX the "24" below could be dynamic, thereby eliminating one
217 		 * more instance of the "number to vfs" mapping problem, but
218 		 * "24" is the order as taken from sys/mount.h
219 		 */
220 		sysctl_createv(&udf_sysctl_log, 0, NULL, NULL,
221 			       CTLFLAG_PERMANENT,
222 			       CTLTYPE_NODE, "vfs", NULL,
223 			       NULL, 0, NULL, 0,
224 			       CTL_VFS, CTL_EOL);
225 		sysctl_createv(&udf_sysctl_log, 0, NULL, &node,
226 			       CTLFLAG_PERMANENT,
227 			       CTLTYPE_NODE, "udf",
228 			       SYSCTL_DESCR("OSTA Universal File System"),
229 			       NULL, 0, NULL, 0,
230 			       CTL_VFS, 24, CTL_EOL);
231 		sysctl_createv(&udf_sysctl_log, 0, NULL, &node,
232 			       CTLFLAG_PERMANENT,
233 			       CTLTYPE_INT, "curdirhashsize",
234 			       SYSCTL_DESCR("Current memory to be used by dirhash"),
235 			       NULL, 0, &udf_dirhashsize, 0,
236 			       CTL_VFS, 24, UDF_CURDIRHASHSIZE_SYSCTLOPT, CTL_EOL);
237 		sysctl_createv(&udf_sysctl_log, 0, NULL, &node,
238 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
239 			       CTLTYPE_INT, "maxdirhashsize",
240 			       SYSCTL_DESCR("Max memory to be used by dirhash"),
241 			       NULL, 0, &udf_maxdirhashsize, 0,
242 			       CTL_VFS, 24, UDF_MAXDIRHASHSIZE_SYSCTLOPT, CTL_EOL);
243 #ifdef DEBUG
244 		sysctl_createv(&udf_sysctl_log, 0, NULL, &node,
245 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
246 			       CTLTYPE_INT, "verbose",
247 			       SYSCTL_DESCR("Bitmask for filesystem debugging"),
248 			       NULL, 0, &udf_verbose, 0,
249 			       CTL_VFS, 24, UDF_VERBOSE_SYSCTLOPT, CTL_EOL);
250 #endif
251 		break;
252 	case MODULE_CMD_FINI:
253 		error = vfs_detach(&udf_vfsops);
254 		if (error != 0)
255 			break;
256 		sysctl_teardown(&udf_sysctl_log);
257 		break;
258 	default:
259 		error = ENOTTY;
260 		break;
261 	}
262 
263 	return (error);
264 }
265 
266 /* --------------------------------------------------------------------- */
267 
268 int
269 udf_mountroot(void)
270 {
271 	return EOPNOTSUPP;
272 }
273 
274 /* --------------------------------------------------------------------- */
275 
276 #define MPFREE(a, lst) \
277 	if ((a)) free((a), lst);
278 static void
279 free_udf_mountinfo(struct mount *mp)
280 {
281 	struct udf_mount *ump;
282 	int i;
283 
284 	if (!mp)
285 		return;
286 
287 	ump = VFSTOUDF(mp);
288 	if (ump) {
289 		/* clear our data */
290 		for (i = 0; i < UDF_ANCHORS; i++)
291 			MPFREE(ump->anchors[i], M_UDFVOLD);
292 		MPFREE(ump->primary_vol,      M_UDFVOLD);
293 		MPFREE(ump->logical_vol,      M_UDFVOLD);
294 		MPFREE(ump->unallocated,      M_UDFVOLD);
295 		MPFREE(ump->implementation,   M_UDFVOLD);
296 		MPFREE(ump->logvol_integrity, M_UDFVOLD);
297 		for (i = 0; i < UDF_PARTITIONS; i++) {
298 			MPFREE(ump->partitions[i],        M_UDFVOLD);
299 			MPFREE(ump->part_unalloc_dscr[i], M_UDFVOLD);
300 			MPFREE(ump->part_freed_dscr[i],   M_UDFVOLD);
301 		}
302 		MPFREE(ump->metadata_unalloc_dscr, M_UDFVOLD);
303 
304 		MPFREE(ump->fileset_desc,   M_UDFVOLD);
305 		MPFREE(ump->sparing_table,  M_UDFVOLD);
306 
307 		MPFREE(ump->la_node_ad_cpy, M_UDFMNT);
308 		MPFREE(ump->la_pmapping,    M_TEMP);
309 		MPFREE(ump->la_lmapping,    M_TEMP);
310 
311 		mutex_destroy(&ump->ihash_lock);
312 		mutex_destroy(&ump->get_node_lock);
313 
314 		mutex_destroy(&ump->allocate_mutex);
315 		MPFREE(ump->vat_table, M_UDFVOLD);
316 
317 		free(ump, M_UDFMNT);
318 	}
319 }
320 #undef MPFREE
321 
322 /* --------------------------------------------------------------------- */
323 
324 /* if the system nodes exist, release them */
325 static void
326 udf_release_system_nodes(struct mount *mp)
327 {
328 	struct udf_mount *ump = VFSTOUDF(mp);
329 	int error;
330 
331 	/* if we haven't even got an ump, dont bother */
332 	if (!ump)
333 		return;
334 
335 	/* VAT partition support */
336 	if (ump->vat_node)
337 		vrele(ump->vat_node->vnode);
338 
339 	/* Metadata partition support */
340 	if (ump->metadata_node)
341 		vrele(ump->metadata_node->vnode);
342 	if (ump->metadatamirror_node)
343 		vrele(ump->metadatamirror_node->vnode);
344 	if (ump->metadatabitmap_node)
345 		vrele(ump->metadatabitmap_node->vnode);
346 
347 	/* This flush should NOT write anything nor allow any node to remain */
348 	if ((error = vflush(ump->vfs_mountp, NULLVP, 0)) != 0)
349 		panic("Failure to flush UDF system vnodes\n");
350 }
351 
352 
353 int
354 udf_mount(struct mount *mp, const char *path,
355 	  void *data, size_t *data_len)
356 {
357 	struct lwp *l = curlwp;
358 	struct nameidata nd;
359 	struct udf_args *args = data;
360 	struct udf_mount *ump;
361 	struct vnode *devvp;
362 	int openflags, accessmode, error;
363 
364 	DPRINTF(CALL, ("udf_mount called\n"));
365 
366 	if (*data_len < sizeof *args)
367 		return EINVAL;
368 
369 	if (mp->mnt_flag & MNT_GETARGS) {
370 		/* request for the mount arguments */
371 		ump = VFSTOUDF(mp);
372 		if (ump == NULL)
373 			return EINVAL;
374 		*args = ump->mount_args;
375 		*data_len = sizeof *args;
376 		return 0;
377 	}
378 
379 	/* handle request for updating mount parameters */
380 	/* TODO can't update my mountpoint yet */
381 	if (mp->mnt_flag & MNT_UPDATE) {
382 		return EOPNOTSUPP;
383 	}
384 
385 	/* OK, so we are asked to mount the device */
386 
387 	/* check/translate struct version */
388 	/* TODO sanity checking other mount arguments */
389 	if (args->version != 1) {
390 		printf("mount_udf: unrecognized argument structure version\n");
391 		return EINVAL;
392 	}
393 
394 	/* lookup name to get its vnode */
395 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec);
396 	error = namei(&nd);
397 	if (error)
398 		return error;
399 	devvp = nd.ni_vp;
400 
401 #ifdef DEBUG
402 	if (udf_verbose & UDF_DEBUG_VOLUMES)
403 		vprint("UDF mount, trying to mount \n", devvp);
404 #endif
405 
406 	/* check if its a block device specified */
407 	if (devvp->v_type != VBLK) {
408 		vrele(devvp);
409 		return ENOTBLK;
410 	}
411 	if (bdevsw_lookup(devvp->v_rdev) == NULL) {
412 		vrele(devvp);
413 		return ENXIO;
414 	}
415 
416 #ifndef UDF_READWRITE
417 	/* force read-only for now */
418 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
419 		printf( "Enable kernel/module option UDF_READWRITE for "
420 			"writing, downgrading access to read-only\n");
421 		mp->mnt_flag |= MNT_RDONLY;
422 	}
423 #endif
424 
425 	/*
426 	 * If mount by non-root, then verify that user has necessary
427 	 * permissions on the device.
428 	 */
429 	if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL)) {
430 		accessmode = VREAD;
431 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
432 			accessmode |= VWRITE;
433 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
434 		error = VOP_ACCESS(devvp, accessmode, l->l_cred);
435 		VOP_UNLOCK(devvp, 0);
436 		if (error) {
437 			vrele(devvp);
438 			return error;
439 		}
440 	}
441 
442 	/*
443 	 * Open device and try to mount it!
444 	 */
445 	if (mp->mnt_flag & MNT_RDONLY) {
446 		openflags = FREAD;
447 	} else {
448 		openflags = FREAD | FWRITE;
449 	}
450 	error = VOP_OPEN(devvp, openflags, FSCRED);
451 	if (error == 0) {
452 		/* opened ok, try mounting */
453 		error = udf_mountfs(devvp, mp, l, args);
454 		if (error) {
455 			udf_release_system_nodes(mp);
456 			/* cleanup */
457 			udf_discstrat_finish(VFSTOUDF(mp));
458 			free_udf_mountinfo(mp);
459 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
460 			(void) VOP_CLOSE(devvp, openflags, NOCRED);
461 			VOP_UNLOCK(devvp, 0);
462 		}
463 	}
464 	if (error) {
465 		/* devvp is still locked */
466 		vrele(devvp);
467 		return error;
468 	}
469 
470 	/* register our mountpoint being on this device */
471 	devvp->v_specmountpoint = mp;
472 
473 	/* successfully mounted */
474 	DPRINTF(VOLUMES, ("udf_mount() successfull\n"));
475 
476 	error = set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
477 			mp->mnt_op->vfs_name, mp, l);
478 	if (error)
479 		return error;
480 
481 	/* If we're not opened read-only, open its logical volume */
482 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
483 		if ((error = udf_open_logvol(VFSTOUDF(mp))) != 0) {
484 			printf( "mount_udf: can't open logical volume for "
485 				"writing, downgrading access to read-only\n");
486 			mp->mnt_flag |= MNT_RDONLY;
487 			/* FIXME we can't return error now on open failure */
488 			return 0;
489 		}
490 	}
491 
492 	return 0;
493 }
494 
495 /* --------------------------------------------------------------------- */
496 
497 #ifdef DEBUG
498 static void
499 udf_unmount_sanity_check(struct mount *mp)
500 {
501 	struct vnode *vp;
502 
503 	printf("On unmount, i found the following nodes:\n");
504 	TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
505 		vprint("", vp);
506 		if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE) {
507 			printf("  is locked\n");
508 		}
509 		if (vp->v_usecount > 1)
510 			printf("  more than one usecount %d\n", vp->v_usecount);
511 	}
512 }
513 #endif
514 
515 
516 int
517 udf_unmount(struct mount *mp, int mntflags)
518 {
519 	struct udf_mount *ump;
520 	int error, flags, closeflags;
521 
522 	DPRINTF(CALL, ("udf_umount called\n"));
523 
524 	ump = VFSTOUDF(mp);
525 	if (!ump)
526 		panic("UDF unmount: empty ump\n");
527 
528 	flags = (mntflags & MNT_FORCE) ? FORCECLOSE : 0;
529 	/* TODO remove these paranoid functions */
530 #ifdef DEBUG
531 	if (udf_verbose & UDF_DEBUG_LOCKING)
532 		udf_unmount_sanity_check(mp);
533 #endif
534 
535 	/*
536 	 * By specifying SKIPSYSTEM we can skip vnodes marked with VV_SYSTEM.
537 	 * This hardly documented feature allows us to exempt certain files
538 	 * from being flushed.
539 	 */
540 	if ((error = vflush(mp, NULLVP, flags | SKIPSYSTEM)) != 0)
541 		return error;
542 
543 	/* update nodes and wait for completion of writeout of system nodes */
544 	udf_sync(mp, FSYNC_WAIT, NOCRED);
545 
546 #ifdef DEBUG
547 	if (udf_verbose & UDF_DEBUG_LOCKING)
548 		udf_unmount_sanity_check(mp);
549 #endif
550 
551 	/* flush again, to check if we are still busy for something else */
552 	if ((error = vflush(ump->vfs_mountp, NULLVP, flags | SKIPSYSTEM)) != 0)
553 		return error;
554 
555 	DPRINTF(VOLUMES, ("flush OK on unmount\n"));
556 
557 	/* close logical volume and close session if requested */
558 	if ((error = udf_close_logvol(ump, mntflags)) != 0)
559 		return error;
560 
561 #ifdef DEBUG
562 	DPRINTF(VOLUMES, ("FINAL sanity check\n"));
563 	if (udf_verbose & UDF_DEBUG_LOCKING)
564 		udf_unmount_sanity_check(mp);
565 #endif
566 
567 	/* NOTE release system nodes should NOT write anything */
568 	udf_release_system_nodes(mp);
569 
570 	/* finalise disc strategy */
571 	udf_discstrat_finish(ump);
572 
573 	/* synchronise device caches */
574 	(void) udf_synchronise_caches(ump);
575 
576 	/* close device */
577 	DPRINTF(VOLUMES, ("closing device\n"));
578 	if (mp->mnt_flag & MNT_RDONLY) {
579 		closeflags = FREAD;
580 	} else {
581 		closeflags = FREAD | FWRITE;
582 	}
583 
584 	/* devvp is still locked by us */
585 	vn_lock(ump->devvp, LK_EXCLUSIVE | LK_RETRY);
586 	error = VOP_CLOSE(ump->devvp, closeflags, NOCRED);
587 	if (error)
588 		printf("Error during closure of device! error %d, "
589 		       "device might stay locked\n", error);
590 	DPRINTF(VOLUMES, ("device close ok\n"));
591 
592 	/* clear our mount reference and release device node */
593 	ump->devvp->v_specmountpoint = NULL;
594 	vput(ump->devvp);
595 
596 	/* free our ump */
597 	free_udf_mountinfo(mp);
598 
599 	/* free ump struct references */
600 	mp->mnt_data = NULL;
601 	mp->mnt_flag &= ~MNT_LOCAL;
602 
603 	DPRINTF(VOLUMES, ("Fin unmount\n"));
604 	return error;
605 }
606 
607 /* --------------------------------------------------------------------- */
608 
609 /*
610  * Helper function of udf_mount() that actually mounts the disc.
611  */
612 
613 static int
614 udf_mountfs(struct vnode *devvp, struct mount *mp,
615 	    struct lwp *l, struct udf_args *args)
616 {
617 	struct udf_mount     *ump;
618 	uint32_t sector_size, lb_size, bshift;
619 	uint32_t logvol_integrity;
620 	int    num_anchors, error, lst;
621 
622 	/* flush out any old buffers remaining from a previous use. */
623 	if ((error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0)))
624 		return error;
625 
626 	/* setup basic mount information */
627 	mp->mnt_data = NULL;
628 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (uint32_t) devvp->v_rdev;
629 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_UDF);
630 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
631 	mp->mnt_stat.f_namemax = UDF_MAX_NAMELEN;
632 	mp->mnt_flag |= MNT_LOCAL;
633 
634 	/* allocate udf part of mount structure; malloc always succeeds */
635 	ump = malloc(sizeof(struct udf_mount), M_UDFMNT, M_WAITOK | M_ZERO);
636 
637 	/* init locks */
638 	mutex_init(&ump->logvol_mutex, MUTEX_DEFAULT, IPL_NONE);
639 	mutex_init(&ump->ihash_lock, MUTEX_DEFAULT, IPL_NONE);
640 	mutex_init(&ump->get_node_lock, MUTEX_DEFAULT, IPL_NONE);
641 	mutex_init(&ump->allocate_mutex, MUTEX_DEFAULT, IPL_NONE);
642 	cv_init(&ump->dirtynodes_cv, "udfsync2");
643 
644 	/* init `ino_t' to udf_node hash table and other lists */
645 	for (lst = 0; lst < UDF_INODE_HASHSIZE; lst++) {
646 		LIST_INIT(&ump->udf_nodes[lst]);
647 	}
648 
649 	/* set up linkage */
650 	mp->mnt_data    = ump;
651 	ump->vfs_mountp = mp;
652 
653 	/* set up arguments and device */
654 	ump->mount_args = *args;
655 	ump->devvp      = devvp;
656 	if ((error = udf_update_discinfo(ump))) {
657 		printf("UDF mount: error inspecting fs node\n");
658 		return error;
659 	}
660 
661 	/* inspect sector size */
662 	sector_size = ump->discinfo.sector_size;
663 	bshift = 1;
664 	while ((1 << bshift) < sector_size)
665 		bshift++;
666 	if ((1 << bshift) != sector_size) {
667 		printf("UDF mount: "
668 		       "hit NetBSD implementation fence on sector size\n");
669 		return EIO;
670 	}
671 
672 	/* temporary check to overcome sectorsize >= 8192 bytes panic */
673 	if (sector_size >= 8192) {
674 		printf("UDF mount: "
675 			"hit implementation limit, sectorsize to big\n");
676 		return EIO;
677 	}
678 
679 	/*
680 	 * Inspect if we're asked to mount read-write on a non recordable or
681 	 * closed sequential disc.
682 	 */
683 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
684 		if ((ump->discinfo.mmc_cur & MMC_CAP_RECORDABLE) == 0) {
685 			printf("UDF mount: disc is not recordable\n");
686 			return EROFS;
687 		}
688 		/*
689 		 * TODO if on sequential media and last session is closed,
690 		 * check for enough space to open/close new session
691 		 */
692 	}
693 
694 	/* initialise bootstrap disc strategy */
695 	ump->strategy = &udf_strat_bootstrap;
696 	udf_discstrat_init(ump);
697 
698 	/* read all anchors to get volume descriptor sequence */
699 	num_anchors = udf_read_anchors(ump);
700 	if (num_anchors == 0)
701 		return EINVAL;
702 
703 	DPRINTF(VOLUMES, ("Read %d anchors on this disc, session %d\n",
704 	    num_anchors, args->sessionnr));
705 
706 	/* read in volume descriptor sequence */
707 	if ((error = udf_read_vds_space(ump))) {
708 		printf("UDF mount: error reading volume space\n");
709 		return error;
710 	}
711 
712 	/* close down (direct) disc strategy */
713 	udf_discstrat_finish(ump);
714 
715 	/* check consistency and completeness */
716 	if ((error = udf_process_vds(ump))) {
717 		printf( "UDF mount: disc not properly formatted"
718 			"(bad VDS)\n");
719 		return error;
720 	}
721 
722 	/* switch to new disc strategy */
723 	KASSERT(ump->strategy != &udf_strat_bootstrap);
724 	udf_discstrat_init(ump);
725 
726 	/* initialise late allocation administration space */
727 	ump->la_lmapping = malloc(sizeof(uint64_t) * UDF_MAX_MAPPINGS,
728 			M_TEMP, M_WAITOK);
729 	ump->la_pmapping = malloc(sizeof(uint64_t) * UDF_MAX_MAPPINGS,
730 			M_TEMP, M_WAITOK);
731 
732 	/* setup node cleanup extents copy space */
733 	lb_size = udf_rw32(ump->logical_vol->lb_size);
734 	ump->la_node_ad_cpy = malloc(lb_size * UDF_MAX_ALLOC_EXTENTS,
735 		M_UDFMNT, M_WAITOK);
736 	memset(ump->la_node_ad_cpy, 0, lb_size * UDF_MAX_ALLOC_EXTENTS);
737 
738 	/* setup rest of mount information */
739 	mp->mnt_data = ump;
740 
741 	/* bshift is allways equal to disc sector size */
742 	mp->mnt_dev_bshift = bshift;
743 	mp->mnt_fs_bshift  = bshift;
744 
745 	/* note that the mp info needs to be initialised for reading! */
746 	/* read vds support tables like VAT, sparable etc. */
747 	if ((error = udf_read_vds_tables(ump))) {
748 		printf( "UDF mount: error in format or damaged disc "
749 			"(VDS tables failing)\n");
750 		return error;
751 	}
752 
753 	/* check if volume integrity is closed otherwise its dirty */
754 	logvol_integrity = udf_rw32(ump->logvol_integrity->integrity_type);
755 	if (logvol_integrity != UDF_INTEGRITY_CLOSED) {
756 		printf("UDF mount: file system is not clean; ");
757 		printf("please fsck(8)\n");
758 		return EPERM;
759 	}
760 
761 	/* read root directory */
762 	if ((error = udf_read_rootdirs(ump))) {
763 		printf( "UDF mount: "
764 			"disc not properly formatted or damaged disc "
765 			"(rootdirs failing)\n");
766 		return error;
767 	}
768 
769 	/* do we have to set this? */
770 	devvp->v_specmountpoint = mp;
771 
772 	/* success! */
773 	return 0;
774 }
775 
776 /* --------------------------------------------------------------------- */
777 
778 int
779 udf_start(struct mount *mp, int flags)
780 {
781 	/* do we have to do something here? */
782 	return 0;
783 }
784 
785 /* --------------------------------------------------------------------- */
786 
787 int
788 udf_root(struct mount *mp, struct vnode **vpp)
789 {
790 	struct vnode *vp;
791 	struct long_ad *dir_loc;
792 	struct udf_mount *ump = VFSTOUDF(mp);
793 	struct udf_node *root_dir;
794 	int error;
795 
796 	DPRINTF(CALL, ("udf_root called\n"));
797 
798 	dir_loc = &ump->fileset_desc->rootdir_icb;
799 	error = udf_get_node(ump, dir_loc, &root_dir);
800 
801 	if (!root_dir)
802 		error = ENOENT;
803 	if (error)
804 		return error;
805 
806 	vp = root_dir->vnode;
807 	root_dir->vnode->v_vflag |= VV_ROOT;
808 
809 	*vpp = vp;
810 	return 0;
811 }
812 
813 /* --------------------------------------------------------------------- */
814 
815 int
816 udf_statvfs(struct mount *mp, struct statvfs *sbp)
817 {
818 	struct udf_mount *ump = VFSTOUDF(mp);
819 	struct logvol_int_desc *lvid;
820 	struct udf_logvol_info *impl;
821 	uint64_t freeblks, sizeblks;
822 	uint32_t *pos1, *pos2;
823 	int part, num_part;
824 
825 	DPRINTF(CALL, ("udf_statvfs called\n"));
826 	sbp->f_flag   = mp->mnt_flag;
827 	sbp->f_bsize  = ump->discinfo.sector_size;
828 	sbp->f_frsize = ump->discinfo.sector_size;
829 	sbp->f_iosize = ump->discinfo.sector_size;
830 
831 	mutex_enter(&ump->allocate_mutex);
832 	lvid = ump->logvol_integrity;
833 	freeblks = sizeblks = 0;
834 
835 	/* Sequentials report free space directly (CD/DVD/BD-R) */
836 	KASSERT(lvid);
837 	num_part = udf_rw32(lvid->num_part);
838 	impl = (struct udf_logvol_info *) (lvid->tables + 2*num_part);
839 
840 	if (ump->discinfo.mmc_cur & MMC_CAP_SEQUENTIAL) {
841 		/* XXX assumption at most two tracks open */
842 		freeblks = ump->data_track.free_blocks;
843 		if (ump->data_track.tracknr != ump->metadata_track.tracknr)
844 			freeblks += ump->metadata_track.free_blocks;
845 		sizeblks = ump->discinfo.last_possible_lba;
846 	} else {
847 		/* free and used space for mountpoint based on logvol integrity */
848 		for (part=0; part < num_part; part++) {
849 			pos1 = &lvid->tables[0] + part;
850 			pos2 = &lvid->tables[0] + num_part + part;
851 			if (udf_rw32(*pos1) != (uint32_t) -1) {
852 				freeblks += udf_rw32(*pos1);
853 				sizeblks += udf_rw32(*pos2);
854 			}
855 		}
856 	}
857 	freeblks -= ump->uncomitted_lb;
858 
859 	sbp->f_blocks = sizeblks;
860 	sbp->f_bfree  = freeblks;
861 	sbp->f_files  = 0;
862 	if (impl) {
863 		sbp->f_files  = udf_rw32(impl->num_files);
864 		sbp->f_files += udf_rw32(impl->num_directories);
865 	}
866 
867 	/* XXX read only for now XXX */
868 	sbp->f_bavail = 0;
869 	sbp->f_bresvd = 0;
870 
871 	/* tricky, next only aplies to ffs i think, so set to zero */
872 	sbp->f_ffree  = 0;
873 	sbp->f_favail = 0;
874 	sbp->f_fresvd = 0;
875 
876 	mutex_exit(&ump->allocate_mutex);
877 
878 	copy_statvfs_info(sbp, mp);
879 	return 0;
880 }
881 
882 /* --------------------------------------------------------------------- */
883 
884 /*
885  * TODO what about writing out free space maps, lvid etc? only on `waitfor'
886  * i.e. explicit syncing by the user?
887  */
888 
889 int
890 udf_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
891 {
892 	struct udf_mount *ump = VFSTOUDF(mp);
893 	int error;
894 
895 	DPRINTF(CALL, ("udf_sync called\n"));
896 	/* if called when mounted readonly, just ignore */
897 	if (mp->mnt_flag & MNT_RDONLY)
898 		return 0;
899 
900 	if (ump->syncing && !waitfor) {
901 		printf("UDF: skipping autosync\n");
902 		return 0;
903 	}
904 
905 	/* get sync lock */
906 	ump->syncing = 1;
907 
908 	/* pre-sync */
909 	udf_do_sync(ump, cred, waitfor);
910 
911 	error = 0;
912 #if 0
913 	if (waitfor == MNT_WAIT) {
914 		/* XXX lock for VAT en bitmaps? */
915 		/* metadata nodes are written synchronous */
916 		DPRINTF(CALL, ("udf_sync: syncing metadata\n"));
917 		if (ump->lvclose & UDF_WRITE_VAT)
918 			udf_writeout_vat(ump);
919 
920 		if (ump->lvclose & UDF_WRITE_PART_BITMAPS) {
921 			/* writeout metadata spacetable if existing */
922 			error = udf_write_metadata_partition_spacetable(ump,
923 				waitfor);
924 			if (error)
925 				printf( "udf_sync: writeout of metadata space "
926 					"bitmap failed\n");
927 
928 			/* writeout partition spacetables */
929 			error = udf_write_physical_partition_spacetables(ump,
930 					waitfor);
931 			if (error)
932 				printf( "udf_sync: writeout of space tables "
933 					"failed\n");
934 			ump->lvclose &= ~UDF_WRITE_PART_BITMAPS;
935 		}
936 	}
937 #endif
938 
939 	DPRINTF(CALL, ("end of udf_sync()\n"));
940 	ump->syncing = 0;
941 
942 	return 0;
943 }
944 
945 /* --------------------------------------------------------------------- */
946 
947 /*
948  * Get vnode for the file system type specific file id ino for the fs. Its
949  * used for reference to files by unique ID and for NFSv3.
950  * (optional) TODO lookup why some sources state NFSv3
951  */
952 int
953 udf_vget(struct mount *mp, ino_t ino,
954     struct vnode **vpp)
955 {
956 	DPRINTF(NOTIMPL, ("udf_vget called\n"));
957 	return EOPNOTSUPP;
958 }
959 
960 /* --------------------------------------------------------------------- */
961 
962 /*
963  * Lookup vnode for file handle specified
964  */
965 int
966 udf_fhtovp(struct mount *mp, struct fid *fhp,
967     struct vnode **vpp)
968 {
969 	DPRINTF(NOTIMPL, ("udf_fhtovp called\n"));
970 	return EOPNOTSUPP;
971 }
972 
973 /* --------------------------------------------------------------------- */
974 
975 /*
976  * Create an unique file handle. Its structure is opaque and won't be used by
977  * other subsystems. It should uniquely identify the file in the filingsystem
978  * and enough information to know if a file has been removed and/or resources
979  * have been recycled.
980  */
981 int
982 udf_vptofh(struct vnode *vp, struct fid *fid,
983     size_t *fh_size)
984 {
985 	DPRINTF(NOTIMPL, ("udf_vptofh called\n"));
986 	return EOPNOTSUPP;
987 }
988 
989 /* --------------------------------------------------------------------- */
990 
991 /*
992  * Create a filingsystem snapshot at the specified timestamp. Could be
993  * implemented by explicitly creating a new session or with spare room in the
994  * integrity descriptor space
995  */
996 int
997 udf_snapshot(struct mount *mp, struct vnode *vp,
998     struct timespec *tm)
999 {
1000 	DPRINTF(NOTIMPL, ("udf_snapshot called\n"));
1001 	return EOPNOTSUPP;
1002 }
1003 
1004 /* --------------------------------------------------------------------- */
1005