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