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