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