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