xref: /netbsd-src/sys/fs/udf/udf_vfsops.c (revision 0c4ddb1599a0bea866fde8522a74cfbd2f68cd1b)
1 /* $NetBSD: udf_vfsops.c,v 1.42 2008/07/19 16:36:40 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.42 2008/07/19 16:36:40 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/module option UDF_READWRITE for "
388 			"writing, downgrading access to read-only\n");
389 		mp->mnt_flag |= MNT_RDONLY;
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 	/* temporary check to overcome sectorsize >= 8192 bytes panic */
641 	if (sector_size >= 8192) {
642 		printf("UDF mount: "
643 			"hit implementation limit, sectorsize to big\n");
644 		return EIO;
645 	}
646 
647 	/*
648 	 * Inspect if we're asked to mount read-write on a non recordable or
649 	 * closed sequential disc.
650 	 */
651 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
652 		if ((ump->discinfo.mmc_cur & MMC_CAP_RECORDABLE) == 0) {
653 			printf("UDF mount: disc is not recordable\n");
654 			return EROFS;
655 		}
656 		/*
657 		 * TODO if on sequential media and last session is closed,
658 		 * check for enough space to open/close new session
659 		 */
660 	}
661 
662 	/* initialise bootstrap disc strategy */
663 	ump->strategy = &udf_strat_bootstrap;
664 	udf_discstrat_init(ump);
665 
666 	/* read all anchors to get volume descriptor sequence */
667 	num_anchors = udf_read_anchors(ump);
668 	if (num_anchors == 0)
669 		return EINVAL;
670 
671 	DPRINTF(VOLUMES, ("Read %d anchors on this disc, session %d\n",
672 	    num_anchors, args->sessionnr));
673 
674 	/* read in volume descriptor sequence */
675 	if ((error = udf_read_vds_space(ump))) {
676 		printf("UDF mount: error reading volume space\n");
677 		return error;
678 	}
679 
680 	/* close down (direct) disc strategy */
681 	udf_discstrat_finish(ump);
682 
683 	/* check consistency and completeness */
684 	if ((error = udf_process_vds(ump))) {
685 		printf( "UDF mount: disc not properly formatted"
686 			"(bad VDS)\n");
687 		return error;
688 	}
689 
690 	/* switch to new disc strategy */
691 	KASSERT(ump->strategy != &udf_strat_bootstrap);
692 	udf_discstrat_init(ump);
693 
694 	/* initialise late allocation administration space */
695 	ump->la_lmapping = malloc(sizeof(uint64_t) * UDF_MAX_MAPPINGS,
696 			M_TEMP, M_WAITOK);
697 	ump->la_pmapping = malloc(sizeof(uint64_t) * UDF_MAX_MAPPINGS,
698 			M_TEMP, M_WAITOK);
699 
700 	/* setup node cleanup extents copy space */
701 	lb_size = udf_rw32(ump->logical_vol->lb_size);
702 	ump->la_node_ad_cpy = malloc(lb_size * UDF_MAX_ALLOC_EXTENTS,
703 		M_UDFMNT, M_WAITOK);
704 	memset(ump->la_node_ad_cpy, 0, lb_size * UDF_MAX_ALLOC_EXTENTS);
705 
706 	/* setup rest of mount information */
707 	mp->mnt_data = ump;
708 
709 	/* bshift is allways equal to disc sector size */
710 	mp->mnt_dev_bshift = bshift;
711 	mp->mnt_fs_bshift  = bshift;
712 
713 	/* note that the mp info needs to be initialised for reading! */
714 	/* read vds support tables like VAT, sparable etc. */
715 	if ((error = udf_read_vds_tables(ump))) {
716 		printf( "UDF mount: error in format or damaged disc "
717 			"(VDS tables failing)\n");
718 		return error;
719 	}
720 
721 	/* check if volume integrity is closed otherwise its dirty */
722 	logvol_integrity = udf_rw32(ump->logvol_integrity->integrity_type);
723 	if (logvol_integrity != UDF_INTEGRITY_CLOSED) {
724 		printf("UDF mount: file system is not clean; ");
725 		printf("please fsck(8)\n");
726 		return EPERM;
727 	}
728 
729 	/* read root directory */
730 	if ((error = udf_read_rootdirs(ump))) {
731 		printf( "UDF mount: "
732 			"disc not properly formatted or damaged disc "
733 			"(rootdirs failing)\n");
734 		return error;
735 	}
736 
737 	/* do we have to set this? */
738 	devvp->v_specmountpoint = mp;
739 
740 	/* success! */
741 	return 0;
742 }
743 
744 /* --------------------------------------------------------------------- */
745 
746 int
747 udf_start(struct mount *mp, int flags)
748 {
749 	/* do we have to do something here? */
750 	return 0;
751 }
752 
753 /* --------------------------------------------------------------------- */
754 
755 int
756 udf_root(struct mount *mp, struct vnode **vpp)
757 {
758 	struct vnode *vp;
759 	struct long_ad *dir_loc;
760 	struct udf_mount *ump = VFSTOUDF(mp);
761 	struct udf_node *root_dir;
762 	int error;
763 
764 	DPRINTF(CALL, ("udf_root called\n"));
765 
766 	dir_loc = &ump->fileset_desc->rootdir_icb;
767 	error = udf_get_node(ump, dir_loc, &root_dir);
768 
769 	if (!root_dir)
770 		error = ENOENT;
771 	if (error)
772 		return error;
773 
774 	vp = root_dir->vnode;
775 	root_dir->vnode->v_vflag |= VV_ROOT;
776 
777 	*vpp = vp;
778 	return 0;
779 }
780 
781 /* --------------------------------------------------------------------- */
782 
783 int
784 udf_statvfs(struct mount *mp, struct statvfs *sbp)
785 {
786 	struct udf_mount *ump = VFSTOUDF(mp);
787 	struct logvol_int_desc *lvid;
788 	struct udf_logvol_info *impl;
789 	uint64_t freeblks, sizeblks;
790 	uint32_t *pos1, *pos2;
791 	int part, num_part;
792 
793 	DPRINTF(CALL, ("udf_statvfs called\n"));
794 	sbp->f_flag   = mp->mnt_flag;
795 	sbp->f_bsize  = ump->discinfo.sector_size;
796 	sbp->f_frsize = ump->discinfo.sector_size;
797 	sbp->f_iosize = ump->discinfo.sector_size;
798 
799 	mutex_enter(&ump->allocate_mutex);
800 	lvid = ump->logvol_integrity;
801 	freeblks = sizeblks = 0;
802 
803 	/* Sequentials report free space directly (CD/DVD/BD-R) */
804 	KASSERT(lvid);
805 	num_part = udf_rw32(lvid->num_part);
806 	impl = (struct udf_logvol_info *) (lvid->tables + 2*num_part);
807 
808 	if (ump->discinfo.mmc_cur & MMC_CAP_SEQUENTIAL) {
809 		/* XXX assumption at most two tracks open */
810 		freeblks = ump->data_track.free_blocks;
811 		if (ump->data_track.tracknr != ump->metadata_track.tracknr)
812 			freeblks += ump->metadata_track.free_blocks;
813 		sizeblks = ump->discinfo.last_possible_lba;
814 	} else {
815 		/* free and used space for mountpoint based on logvol integrity */
816 		for (part=0; part < num_part; part++) {
817 			pos1 = &lvid->tables[0] + part;
818 			pos2 = &lvid->tables[0] + num_part + part;
819 			if (udf_rw32(*pos1) != (uint32_t) -1) {
820 				freeblks += udf_rw32(*pos1);
821 				sizeblks += udf_rw32(*pos2);
822 			}
823 		}
824 	}
825 	freeblks -= ump->uncomitted_lb;
826 
827 	sbp->f_blocks = sizeblks;
828 	sbp->f_bfree  = freeblks;
829 	sbp->f_files  = 0;
830 	if (impl) {
831 		sbp->f_files  = udf_rw32(impl->num_files);
832 		sbp->f_files += udf_rw32(impl->num_directories);
833 	}
834 
835 	/* XXX read only for now XXX */
836 	sbp->f_bavail = 0;
837 	sbp->f_bresvd = 0;
838 
839 	/* tricky, next only aplies to ffs i think, so set to zero */
840 	sbp->f_ffree  = 0;
841 	sbp->f_favail = 0;
842 	sbp->f_fresvd = 0;
843 
844 	mutex_exit(&ump->allocate_mutex);
845 
846 	copy_statvfs_info(sbp, mp);
847 	return 0;
848 }
849 
850 /* --------------------------------------------------------------------- */
851 
852 /*
853  * TODO what about writing out free space maps, lvid etc? only on `waitfor'
854  * i.e. explicit syncing by the user?
855  */
856 
857 int
858 udf_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
859 {
860 	struct udf_mount *ump = VFSTOUDF(mp);
861 	int error;
862 
863 	DPRINTF(CALL, ("udf_sync called\n"));
864 	/* if called when mounted readonly, just ignore */
865 	if (mp->mnt_flag & MNT_RDONLY)
866 		return 0;
867 
868 	if (ump->syncing && !waitfor) {
869 		printf("UDF: skipping autosync\n");
870 		return 0;
871 	}
872 
873 	/* get sync lock */
874 	ump->syncing = 1;
875 
876 	udf_do_sync(ump, cred, waitfor);
877 
878 	if (waitfor == MNT_WAIT) {
879 		/* XXX lock for VAT en bitmaps? */
880 		/* metadata nodes are written synchronous */
881 		DPRINTF(CALL, ("udf_sync: syncing metadata\n"));
882 		if (ump->lvclose & UDF_WRITE_VAT)
883 			udf_writeout_vat(ump);
884 		if (ump->lvclose & UDF_WRITE_PART_BITMAPS) {
885 			error = udf_write_partition_spacetables(ump, waitfor);
886 			if (error) {
887 				printf( "udf_close_logvol: writeout of space "
888 					"tables failed\n");
889 			}
890 			ump->lvclose &= ~UDF_WRITE_PART_BITMAPS;
891 		}
892 
893 	}
894 	DPRINTF(CALL, ("end of udf_sync()\n"));
895 	ump->syncing = 0;
896 
897 	return 0;
898 }
899 
900 /* --------------------------------------------------------------------- */
901 
902 /*
903  * Get vnode for the file system type specific file id ino for the fs. Its
904  * used for reference to files by unique ID and for NFSv3.
905  * (optional) TODO lookup why some sources state NFSv3
906  */
907 int
908 udf_vget(struct mount *mp, ino_t ino,
909     struct vnode **vpp)
910 {
911 	DPRINTF(NOTIMPL, ("udf_vget called\n"));
912 	return EOPNOTSUPP;
913 }
914 
915 /* --------------------------------------------------------------------- */
916 
917 /*
918  * Lookup vnode for file handle specified
919  */
920 int
921 udf_fhtovp(struct mount *mp, struct fid *fhp,
922     struct vnode **vpp)
923 {
924 	DPRINTF(NOTIMPL, ("udf_fhtovp called\n"));
925 	return EOPNOTSUPP;
926 }
927 
928 /* --------------------------------------------------------------------- */
929 
930 /*
931  * Create an unique file handle. Its structure is opaque and won't be used by
932  * other subsystems. It should uniquely identify the file in the filingsystem
933  * and enough information to know if a file has been removed and/or resources
934  * have been recycled.
935  */
936 int
937 udf_vptofh(struct vnode *vp, struct fid *fid,
938     size_t *fh_size)
939 {
940 	DPRINTF(NOTIMPL, ("udf_vptofh called\n"));
941 	return EOPNOTSUPP;
942 }
943 
944 /* --------------------------------------------------------------------- */
945 
946 /*
947  * Create a filingsystem snapshot at the specified timestamp. Could be
948  * implemented by explicitly creating a new session or with spare room in the
949  * integrity descriptor space
950  */
951 int
952 udf_snapshot(struct mount *mp, struct vnode *vp,
953     struct timespec *tm)
954 {
955 	DPRINTF(NOTIMPL, ("udf_snapshot called\n"));
956 	return EOPNOTSUPP;
957 }
958 
959 /* --------------------------------------------------------------------- */
960