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