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