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