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