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