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