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