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