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