Home | History | Annotate | Line # | Download | only in udf
udf_vfsops.c revision 1.69
      1 /* $NetBSD: udf_vfsops.c,v 1.69 2015/08/24 08:30:17 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.69 2015/08/24 08:30:17 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 = (void *)eopnotsupp,
    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->ihash_lock);
    263 		mutex_destroy(&ump->logvol_mutex);
    264 		mutex_destroy(&ump->allocate_mutex);
    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 	int error;
    281 
    282 	/* if we haven't even got an ump, dont bother */
    283 	if (!ump)
    284 		return;
    285 
    286 	/* VAT partition support */
    287 	if (ump->vat_node)
    288 		vrele(ump->vat_node->vnode);
    289 
    290 	/* Metadata partition support */
    291 	if (ump->metadata_node)
    292 		vrele(ump->metadata_node->vnode);
    293 	if (ump->metadatamirror_node)
    294 		vrele(ump->metadatamirror_node->vnode);
    295 	if (ump->metadatabitmap_node)
    296 		vrele(ump->metadatabitmap_node->vnode);
    297 
    298 	/* This flush should NOT write anything nor allow any node to remain */
    299 	if ((error = vflush(ump->vfs_mountp, NULLVP, 0)) != 0)
    300 		panic("Failure to flush UDF system vnodes\n");
    301 }
    302 
    303 
    304 int
    305 udf_mount(struct mount *mp, const char *path,
    306 	  void *data, size_t *data_len)
    307 {
    308 	struct lwp *l = curlwp;
    309 	struct udf_args *args = data;
    310 	struct udf_mount *ump;
    311 	struct vnode *devvp;
    312 	int openflags, accessmode, error;
    313 
    314 	DPRINTF(CALL, ("udf_mount called\n"));
    315 
    316 	if (args == NULL)
    317 		return EINVAL;
    318 	if (*data_len < sizeof *args)
    319 		return EINVAL;
    320 
    321 	if (mp->mnt_flag & MNT_GETARGS) {
    322 		/* request for the mount arguments */
    323 		ump = VFSTOUDF(mp);
    324 		if (ump == NULL)
    325 			return EINVAL;
    326 		*args = ump->mount_args;
    327 		*data_len = sizeof *args;
    328 		return 0;
    329 	}
    330 
    331 	/* handle request for updating mount parameters */
    332 	/* TODO can't update my mountpoint yet */
    333 	if (mp->mnt_flag & MNT_UPDATE) {
    334 		return EOPNOTSUPP;
    335 	}
    336 
    337 	/* OK, so we are asked to mount the device */
    338 
    339 	/* check/translate struct version */
    340 	/* TODO sanity checking other mount arguments */
    341 	if (args->version != 1) {
    342 		printf("mount_udf: unrecognized argument structure version\n");
    343 		return EINVAL;
    344 	}
    345 
    346 	/* lookup name to get its vnode */
    347 	error = namei_simple_user(args->fspec,
    348 				NSM_FOLLOW_NOEMULROOT, &devvp);
    349 	if (error)
    350 		return error;
    351 
    352 #ifdef DEBUG
    353 	if (udf_verbose & UDF_DEBUG_VOLUMES)
    354 		vprint("UDF mount, trying to mount \n", devvp);
    355 #endif
    356 
    357 	/* check if its a block device specified */
    358 	if (devvp->v_type != VBLK) {
    359 		vrele(devvp);
    360 		return ENOTBLK;
    361 	}
    362 	if (bdevsw_lookup(devvp->v_rdev) == NULL) {
    363 		vrele(devvp);
    364 		return ENXIO;
    365 	}
    366 
    367 	/*
    368 	 * If mount by non-root, then verify that user has necessary
    369 	 * permissions on the device.
    370 	 */
    371 	accessmode = VREAD;
    372 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
    373 		accessmode |= VWRITE;
    374 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    375 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
    376 	    KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp, KAUTH_ARG(accessmode));
    377 	VOP_UNLOCK(devvp);
    378 	if (error) {
    379 		vrele(devvp);
    380 		return error;
    381 	}
    382 
    383 	/*
    384 	 * Open device and try to mount it!
    385 	 */
    386 	if (mp->mnt_flag & MNT_RDONLY) {
    387 		openflags = FREAD;
    388 	} else {
    389 		openflags = FREAD | FWRITE;
    390 	}
    391 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    392 	error = VOP_OPEN(devvp, openflags, FSCRED);
    393 	VOP_UNLOCK(devvp);
    394 	if (error == 0) {
    395 		/* opened ok, try mounting */
    396 		error = udf_mountfs(devvp, mp, l, args);
    397 		if (error) {
    398 			udf_release_system_nodes(mp);
    399 			/* cleanup */
    400 			udf_discstrat_finish(VFSTOUDF(mp));
    401 			free_udf_mountinfo(mp);
    402 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    403 			(void) VOP_CLOSE(devvp, openflags, NOCRED);
    404 			VOP_UNLOCK(devvp);
    405 		}
    406 	}
    407 	if (error) {
    408 		/* devvp is still locked */
    409 		vrele(devvp);
    410 		return error;
    411 	}
    412 
    413 	/* register our mountpoint being on this device */
    414 	spec_node_setmountedfs(devvp, mp);
    415 
    416 	/* successfully mounted */
    417 	DPRINTF(VOLUMES, ("udf_mount() successfull\n"));
    418 
    419 	error = set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
    420 			mp->mnt_op->vfs_name, mp, l);
    421 	if (error)
    422 		return error;
    423 
    424 	/* If we're not opened read-only, open its logical volume */
    425 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    426 		if ((error = udf_open_logvol(VFSTOUDF(mp))) != 0) {
    427 			printf( "mount_udf: can't open logical volume for "
    428 				"writing, downgrading access to read-only\n");
    429 			mp->mnt_flag |= MNT_RDONLY;
    430 			/* FIXME we can't return error now on open failure */
    431 			return 0;
    432 		}
    433 	}
    434 
    435 	return 0;
    436 }
    437 
    438 /* --------------------------------------------------------------------- */
    439 
    440 #ifdef DEBUG
    441 static void
    442 udf_unmount_sanity_check(struct mount *mp)
    443 {
    444 	struct vnode *vp;
    445 
    446 	printf("On unmount, i found the following nodes:\n");
    447 	TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
    448 		vprint("", vp);
    449 		if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE) {
    450 			printf("  is locked\n");
    451 		}
    452 		if (vp->v_usecount > 1)
    453 			printf("  more than one usecount %d\n", vp->v_usecount);
    454 	}
    455 }
    456 #endif
    457 
    458 
    459 int
    460 udf_unmount(struct mount *mp, int mntflags)
    461 {
    462 	struct udf_mount *ump;
    463 	int error, flags, closeflags;
    464 
    465 	DPRINTF(CALL, ("udf_umount called\n"));
    466 
    467 	ump = VFSTOUDF(mp);
    468 	if (!ump)
    469 		panic("UDF unmount: empty ump\n");
    470 
    471 	flags = (mntflags & MNT_FORCE) ? FORCECLOSE : 0;
    472 	/* TODO remove these paranoid functions */
    473 #ifdef DEBUG
    474 	if (udf_verbose & UDF_DEBUG_LOCKING)
    475 		udf_unmount_sanity_check(mp);
    476 #endif
    477 
    478 	/*
    479 	 * By specifying SKIPSYSTEM we can skip vnodes marked with VV_SYSTEM.
    480 	 * This hardly documented feature allows us to exempt certain files
    481 	 * from being flushed.
    482 	 */
    483 	if ((error = vflush(mp, NULLVP, flags | SKIPSYSTEM)) != 0)
    484 		return error;
    485 
    486 	/* update nodes and wait for completion of writeout of system nodes */
    487 	udf_sync(mp, FSYNC_WAIT, NOCRED);
    488 
    489 #ifdef DEBUG
    490 	if (udf_verbose & UDF_DEBUG_LOCKING)
    491 		udf_unmount_sanity_check(mp);
    492 #endif
    493 
    494 	/* flush again, to check if we are still busy for something else */
    495 	if ((error = vflush(ump->vfs_mountp, NULLVP, flags | SKIPSYSTEM)) != 0)
    496 		return error;
    497 
    498 	DPRINTF(VOLUMES, ("flush OK on unmount\n"));
    499 
    500 	/* close logical volume and close session if requested */
    501 	if ((error = udf_close_logvol(ump, mntflags)) != 0)
    502 		return error;
    503 
    504 #ifdef DEBUG
    505 	DPRINTF(VOLUMES, ("FINAL sanity check\n"));
    506 	if (udf_verbose & UDF_DEBUG_LOCKING)
    507 		udf_unmount_sanity_check(mp);
    508 #endif
    509 
    510 	/* NOTE release system nodes should NOT write anything */
    511 	udf_release_system_nodes(mp);
    512 
    513 	/* finalise disc strategy */
    514 	udf_discstrat_finish(ump);
    515 
    516 	/* synchronise device caches */
    517 	(void) udf_synchronise_caches(ump);
    518 
    519 	/* close device */
    520 	DPRINTF(VOLUMES, ("closing device\n"));
    521 	if (mp->mnt_flag & MNT_RDONLY) {
    522 		closeflags = FREAD;
    523 	} else {
    524 		closeflags = FREAD | FWRITE;
    525 	}
    526 
    527 	/* devvp is still locked by us */
    528 	vn_lock(ump->devvp, LK_EXCLUSIVE | LK_RETRY);
    529 	error = VOP_CLOSE(ump->devvp, closeflags, NOCRED);
    530 	if (error)
    531 		printf("Error during closure of device! error %d, "
    532 		       "device might stay locked\n", error);
    533 	DPRINTF(VOLUMES, ("device close ok\n"));
    534 
    535 	/* clear our mount reference and release device node */
    536 	spec_node_setmountedfs(ump->devvp, NULL);
    537 	vput(ump->devvp);
    538 
    539 	/* free our ump */
    540 	free_udf_mountinfo(mp);
    541 
    542 	/* free ump struct references */
    543 	mp->mnt_data = NULL;
    544 	mp->mnt_flag &= ~MNT_LOCAL;
    545 
    546 	DPRINTF(VOLUMES, ("Fin unmount\n"));
    547 	return error;
    548 }
    549 
    550 /* --------------------------------------------------------------------- */
    551 
    552 /*
    553  * Helper function of udf_mount() that actually mounts the disc.
    554  */
    555 
    556 static int
    557 udf_mountfs(struct vnode *devvp, struct mount *mp,
    558 	    struct lwp *l, struct udf_args *args)
    559 {
    560 	struct udf_mount     *ump;
    561 	uint32_t sector_size, lb_size, bshift;
    562 	uint32_t logvol_integrity;
    563 	int    num_anchors, error;
    564 
    565 	/* flush out any old buffers remaining from a previous use. */
    566 	if ((error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0)))
    567 		return error;
    568 
    569 	/* setup basic mount information */
    570 	mp->mnt_data = NULL;
    571 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (uint32_t) devvp->v_rdev;
    572 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_UDF);
    573 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
    574 	mp->mnt_stat.f_namemax = UDF_MAXNAMLEN;
    575 	mp->mnt_flag |= MNT_LOCAL;
    576 //	mp->mnt_iflag |= IMNT_MPSAFE;
    577 
    578 	/* allocate udf part of mount structure; malloc always succeeds */
    579 	ump = malloc(sizeof(struct udf_mount), M_UDFMNT, M_WAITOK | M_ZERO);
    580 
    581 	/* init locks */
    582 	mutex_init(&ump->logvol_mutex, MUTEX_DEFAULT, IPL_NONE);
    583 	mutex_init(&ump->ihash_lock, MUTEX_DEFAULT, IPL_NONE);
    584 	mutex_init(&ump->allocate_mutex, MUTEX_DEFAULT, IPL_NONE);
    585 
    586 	/* init rbtree for nodes, ordered by their icb address (long_ad) */
    587 	udf_init_nodes_tree(ump);
    588 
    589 	/* set up linkage */
    590 	mp->mnt_data    = ump;
    591 	ump->vfs_mountp = mp;
    592 
    593 	/* set up arguments and device */
    594 	ump->mount_args = *args;
    595 	ump->devvp      = devvp;
    596 	if ((error = udf_update_discinfo(ump))) {
    597 		printf("UDF mount: error inspecting fs node\n");
    598 		return error;
    599 	}
    600 
    601 	/* inspect sector size */
    602 	sector_size = ump->discinfo.sector_size;
    603 	bshift = 1;
    604 	while ((1 << bshift) < sector_size)
    605 		bshift++;
    606 	if ((1 << bshift) != sector_size) {
    607 		printf("UDF mount: "
    608 		       "hit NetBSD implementation fence on sector size\n");
    609 		return EIO;
    610 	}
    611 
    612 	/* temporary check to overcome sectorsize >= 8192 bytes panic */
    613 	if (sector_size >= 8192) {
    614 		printf("UDF mount: "
    615 			"hit implementation limit, sectorsize to big\n");
    616 		return EIO;
    617 	}
    618 
    619 	/*
    620 	 * Inspect if we're asked to mount read-write on a non recordable or
    621 	 * closed sequential disc.
    622 	 */
    623 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    624 		if ((ump->discinfo.mmc_cur & MMC_CAP_RECORDABLE) == 0) {
    625 			printf("UDF mount: disc is not recordable\n");
    626 			return EROFS;
    627 		}
    628 		if (ump->discinfo.mmc_cur & MMC_CAP_SEQUENTIAL) {
    629 			if (ump->discinfo.disc_state == MMC_STATE_FULL) {
    630 				printf("UDF mount: disc is not appendable\n");
    631 				return EROFS;
    632 			}
    633 
    634 			/*
    635 			 * TODO if the last session is closed check if there
    636 			 * is enough space to open/close new session
    637 			 */
    638 		}
    639 		/* double check if we're not mounting a pervious session RW */
    640 		if (args->sessionnr != 0) {
    641 			printf("UDF mount: updating a previous session "
    642 				"not yet allowed\n");
    643 			return EROFS;
    644 		}
    645 	}
    646 
    647 	/* initialise bootstrap disc strategy */
    648 	ump->strategy = &udf_strat_bootstrap;
    649 	udf_discstrat_init(ump);
    650 
    651 	/* read all anchors to get volume descriptor sequence */
    652 	num_anchors = udf_read_anchors(ump);
    653 	if (num_anchors == 0)
    654 		return EINVAL;
    655 
    656 	DPRINTF(VOLUMES, ("Read %d anchors on this disc, session %d\n",
    657 	    num_anchors, args->sessionnr));
    658 
    659 	/* read in volume descriptor sequence */
    660 	if ((error = udf_read_vds_space(ump))) {
    661 		printf("UDF mount: error reading volume space\n");
    662 		return error;
    663 	}
    664 
    665 	/* close down bootstrap disc strategy */
    666 	udf_discstrat_finish(ump);
    667 
    668 	/* check consistency and completeness */
    669 	if ((error = udf_process_vds(ump))) {
    670 		printf( "UDF mount: disc not properly formatted"
    671 			"(bad VDS)\n");
    672 		return error;
    673 	}
    674 
    675 	/* switch to new disc strategy */
    676 	KASSERT(ump->strategy != &udf_strat_bootstrap);
    677 	udf_discstrat_init(ump);
    678 
    679 	/* initialise late allocation administration space */
    680 	ump->la_lmapping = malloc(sizeof(uint64_t) * UDF_MAX_MAPPINGS,
    681 			M_TEMP, M_WAITOK);
    682 	ump->la_pmapping = malloc(sizeof(uint64_t) * UDF_MAX_MAPPINGS,
    683 			M_TEMP, M_WAITOK);
    684 
    685 	/* setup node cleanup extents copy space */
    686 	lb_size = udf_rw32(ump->logical_vol->lb_size);
    687 	ump->la_node_ad_cpy = malloc(lb_size * UDF_MAX_ALLOC_EXTENTS,
    688 		M_UDFMNT, M_WAITOK);
    689 	memset(ump->la_node_ad_cpy, 0, lb_size * UDF_MAX_ALLOC_EXTENTS);
    690 
    691 	/* setup rest of mount information */
    692 	mp->mnt_data = ump;
    693 
    694 	/* bshift is allways equal to disc sector size */
    695 	mp->mnt_dev_bshift = bshift;
    696 	mp->mnt_fs_bshift  = bshift;
    697 
    698 	/* note that the mp info needs to be initialised for reading! */
    699 	/* read vds support tables like VAT, sparable etc. */
    700 	if ((error = udf_read_vds_tables(ump))) {
    701 		printf( "UDF mount: error in format or damaged disc "
    702 			"(VDS tables failing)\n");
    703 		return error;
    704 	}
    705 
    706 	/* check if volume integrity is closed otherwise its dirty */
    707 	logvol_integrity = udf_rw32(ump->logvol_integrity->integrity_type);
    708 	if (logvol_integrity != UDF_INTEGRITY_CLOSED) {
    709 		printf("UDF mount: file system is not clean; ");
    710 		printf("please fsck(8)\n");
    711 		return EPERM;
    712 	}
    713 
    714 	/* read root directory */
    715 	if ((error = udf_read_rootdirs(ump))) {
    716 		printf( "UDF mount: "
    717 			"disc not properly formatted or damaged disc "
    718 			"(rootdirs failing)\n");
    719 		return error;
    720 	}
    721 
    722 	/* success! */
    723 	return 0;
    724 }
    725 
    726 /* --------------------------------------------------------------------- */
    727 
    728 int
    729 udf_start(struct mount *mp, int flags)
    730 {
    731 	/* do we have to do something here? */
    732 	return 0;
    733 }
    734 
    735 /* --------------------------------------------------------------------- */
    736 
    737 int
    738 udf_root(struct mount *mp, struct vnode **vpp)
    739 {
    740 	struct vnode *vp;
    741 	struct long_ad *dir_loc;
    742 	struct udf_mount *ump = VFSTOUDF(mp);
    743 	struct udf_node *root_dir;
    744 	int error;
    745 
    746 	DPRINTF(CALL, ("udf_root called\n"));
    747 
    748 	dir_loc = &ump->fileset_desc->rootdir_icb;
    749 	error = udf_get_node(ump, dir_loc, &root_dir);
    750 
    751 	if (!root_dir)
    752 		error = ENOENT;
    753 	if (error)
    754 		return error;
    755 
    756 	vp = root_dir->vnode;
    757 	KASSERT(vp->v_vflag & VV_ROOT);
    758 
    759 	*vpp = vp;
    760 	return 0;
    761 }
    762 
    763 /* --------------------------------------------------------------------- */
    764 
    765 int
    766 udf_statvfs(struct mount *mp, struct statvfs *sbp)
    767 {
    768 	struct udf_mount *ump = VFSTOUDF(mp);
    769 	struct logvol_int_desc *lvid;
    770 	struct udf_logvol_info *impl;
    771 	uint64_t freeblks, sizeblks;
    772 	int num_part;
    773 
    774 	DPRINTF(CALL, ("udf_statvfs called\n"));
    775 	sbp->f_flag   = mp->mnt_flag;
    776 	sbp->f_bsize  = ump->discinfo.sector_size;
    777 	sbp->f_frsize = ump->discinfo.sector_size;
    778 	sbp->f_iosize = ump->discinfo.sector_size;
    779 
    780 	mutex_enter(&ump->allocate_mutex);
    781 
    782 	udf_calc_freespace(ump, &sizeblks, &freeblks);
    783 
    784 	sbp->f_blocks = sizeblks;
    785 	sbp->f_bfree  = freeblks;
    786 	sbp->f_files  = 0;
    787 
    788 	lvid = ump->logvol_integrity;
    789 	num_part = udf_rw32(lvid->num_part);
    790 	impl = (struct udf_logvol_info *) (lvid->tables + 2*num_part);
    791 	if (impl) {
    792 		sbp->f_files  = udf_rw32(impl->num_files);
    793 		sbp->f_files += udf_rw32(impl->num_directories);
    794 	}
    795 
    796 	/* XXX read only for now XXX */
    797 	sbp->f_bavail = 0;
    798 	sbp->f_bresvd = 0;
    799 
    800 	/* tricky, next only aplies to ffs i think, so set to zero */
    801 	sbp->f_ffree  = 0;
    802 	sbp->f_favail = 0;
    803 	sbp->f_fresvd = 0;
    804 
    805 	mutex_exit(&ump->allocate_mutex);
    806 
    807 	copy_statvfs_info(sbp, mp);
    808 	return 0;
    809 }
    810 
    811 /* --------------------------------------------------------------------- */
    812 
    813 /*
    814  * TODO what about writing out free space maps, lvid etc? only on `waitfor'
    815  * i.e. explicit syncing by the user?
    816  */
    817 
    818 static int
    819 udf_sync_writeout_system_files(struct udf_mount *ump, int clearflags)
    820 {
    821 	int error;
    822 
    823 	/* XXX lock for VAT en bitmaps? */
    824 	/* metadata nodes are written synchronous */
    825 	DPRINTF(CALL, ("udf_sync: syncing metadata\n"));
    826 	if (ump->lvclose & UDF_WRITE_VAT)
    827 		udf_writeout_vat(ump);
    828 
    829 	error = 0;
    830 	if (ump->lvclose & UDF_WRITE_PART_BITMAPS) {
    831 		/* writeout metadata spacetable if existing */
    832 		error = udf_write_metadata_partition_spacetable(ump, MNT_WAIT);
    833 		if (error)
    834 			printf( "udf_writeout_system_files : "
    835 				" writeout of metadata space bitmap failed\n");
    836 
    837 		/* writeout partition spacetables */
    838 		error = udf_write_physical_partition_spacetables(ump, MNT_WAIT);
    839 		if (error)
    840 			printf( "udf_writeout_system_files : "
    841 				"writeout of space tables failed\n");
    842 		if (!error && clearflags)
    843 			ump->lvclose &= ~UDF_WRITE_PART_BITMAPS;
    844 	}
    845 
    846 	return error;
    847 }
    848 
    849 
    850 int
    851 udf_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
    852 {
    853 	struct udf_mount *ump = VFSTOUDF(mp);
    854 
    855 	DPRINTF(CALL, ("udf_sync called\n"));
    856 	/* if called when mounted readonly, just ignore */
    857 	if (mp->mnt_flag & MNT_RDONLY)
    858 		return 0;
    859 
    860 	if (ump->syncing && !waitfor) {
    861 		printf("UDF: skipping autosync\n");
    862 		return 0;
    863 	}
    864 
    865 	/* get sync lock */
    866 	ump->syncing = 1;
    867 
    868 	/* pre-sync */
    869 	udf_do_sync(ump, cred, waitfor);
    870 
    871 	if (waitfor == MNT_WAIT)
    872 		udf_sync_writeout_system_files(ump, true);
    873 
    874 	DPRINTF(CALL, ("end of udf_sync()\n"));
    875 	ump->syncing = 0;
    876 
    877 	return 0;
    878 }
    879 
    880 /* --------------------------------------------------------------------- */
    881 
    882 /*
    883  * Get vnode for the file system type specific file id ino for the fs. Its
    884  * used for reference to files by unique ID and for NFSv3.
    885  * (optional) TODO lookup why some sources state NFSv3
    886  */
    887 int
    888 udf_vget(struct mount *mp, ino_t ino,
    889     struct vnode **vpp)
    890 {
    891 	DPRINTF(NOTIMPL, ("udf_vget called\n"));
    892 	return EOPNOTSUPP;
    893 }
    894 
    895 /* --------------------------------------------------------------------- */
    896 
    897 /*
    898  * Lookup vnode for file handle specified
    899  */
    900 int
    901 udf_fhtovp(struct mount *mp, struct fid *fhp,
    902     struct vnode **vpp)
    903 {
    904 	DPRINTF(NOTIMPL, ("udf_fhtovp called\n"));
    905 	return EOPNOTSUPP;
    906 }
    907 
    908 /* --------------------------------------------------------------------- */
    909 
    910 /*
    911  * Create an unique file handle. Its structure is opaque and won't be used by
    912  * other subsystems. It should uniquely identify the file in the filingsystem
    913  * and enough information to know if a file has been removed and/or resources
    914  * have been recycled.
    915  */
    916 int
    917 udf_vptofh(struct vnode *vp, struct fid *fid,
    918     size_t *fh_size)
    919 {
    920 	DPRINTF(NOTIMPL, ("udf_vptofh called\n"));
    921 	return EOPNOTSUPP;
    922 }
    923 
    924 /* --------------------------------------------------------------------- */
    925 
    926 /*
    927  * Create a filingsystem snapshot at the specified timestamp. Could be
    928  * implemented by explicitly creating a new session or with spare room in the
    929  * integrity descriptor space
    930  */
    931 int
    932 udf_snapshot(struct mount *mp, struct vnode *vp,
    933     struct timespec *tm)
    934 {
    935 	DPRINTF(NOTIMPL, ("udf_snapshot called\n"));
    936 	return EOPNOTSUPP;
    937 }
    938 
    939 /* --------------------------------------------------------------------- */
    940