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