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