#include <sys/cdefs.h>
#ifndef lint
__KERNEL_RCSID(0, "$NetBSD: udf_vfsops.c,v 1.86 2025/08/25 11:13:20 reinoud Exp $");
#endif
#if defined(_KERNEL_OPT)
#include "opt_compat_netbsd.h"
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sysctl.h>
#include <sys/namei.h>
#include <sys/proc.h>
#include <sys/kernel.h>
#include <sys/vnode.h>
#include <miscfs/genfs/genfs.h>
#include <miscfs/specfs/specdev.h>
#include <sys/mount.h>
#include <sys/buf.h>
#include <sys/file.h>
#include <sys/device.h>
#include <sys/disklabel.h>
#include <sys/ioctl.h>
#include <sys/malloc.h>
#include <sys/dirent.h>
#include <sys/stat.h>
#include <sys/conf.h>
#include <sys/kauth.h>
#include <sys/module.h>
#include <fs/udf/ecma167-udf.h>
#include <fs/udf/udf_mount.h>
#include <sys/dirhash.h>
#include "udf.h"
#include "udf_subr.h"
#include "udf_bswap.h"
MODULE(MODULE_CLASS_VFS, udf, NULL);
#define VTOI(vnode) ((struct udf_node *) vnode->v_data)
int udf_verbose = UDF_DEBUGGING;
MALLOC_JUSTDEFINE(M_UDFMNT, "UDF mount", "UDF mount structures");
MALLOC_JUSTDEFINE(M_UDFVOLD, "UDF volspace", "UDF volume space descriptors");
MALLOC_JUSTDEFINE(M_UDFTEMP, "UDF temp", "UDF scrap space");
struct pool udf_node_pool;
static int udf_mountfs(struct vnode *, struct mount *, struct lwp *, struct udf_args *);
extern const struct vnodeopv_desc udf_vnodeop_opv_desc;
const struct vnodeopv_desc * const udf_vnodeopv_descs[] = {
&udf_vnodeop_opv_desc,
NULL,
};
struct vfsops udf_vfsops = {
.vfs_name = MOUNT_UDF,
.vfs_min_mount_data = sizeof (struct udf_args),
.vfs_mount = udf_mount,
.vfs_start = udf_start,
.vfs_unmount = udf_unmount,
.vfs_root = udf_root,
.vfs_quotactl = (void *)eopnotsupp,
.vfs_statvfs = udf_statvfs,
.vfs_sync = udf_sync,
.vfs_vget = udf_vget,
.vfs_loadvnode = udf_loadvnode,
.vfs_newvnode = udf_newvnode,
.vfs_fhtovp = udf_fhtovp,
.vfs_vptofh = udf_vptofh,
.vfs_init = udf_init,
.vfs_reinit = udf_reinit,
.vfs_done = udf_done,
.vfs_mountroot = udf_mountroot,
.vfs_snapshot = udf_snapshot,
.vfs_extattrctl = vfs_stdextattrctl,
.vfs_suspendctl = genfs_suspendctl,
.vfs_renamelock_enter = genfs_renamelock_enter,
.vfs_renamelock_exit = genfs_renamelock_exit,
.vfs_fsync = (void *)eopnotsupp,
.vfs_opv_descs = udf_vnodeopv_descs
};
void
udf_init(void)
{
size_t size;
malloc_type_attach(M_UDFMNT);
malloc_type_attach(M_UDFVOLD);
malloc_type_attach(M_UDFTEMP);
size = sizeof(struct udf_node);
pool_init(&udf_node_pool, size, 0, 0, 0,
"udf_node_pool", NULL, IPL_NONE);
}
void
udf_reinit(void)
{
}
void
udf_done(void)
{
pool_destroy(&udf_node_pool);
malloc_type_detach(M_UDFMNT);
malloc_type_detach(M_UDFVOLD);
malloc_type_detach(M_UDFTEMP);
}
#define UDF_VERBOSE_SYSCTLOPT 1
SYSCTL_SETUP(udf_sysctl_setup, "udf sysctl")
{
const struct sysctlnode *node;
sysctl_createv(clog, 0, NULL, &node,
CTLFLAG_PERMANENT,
CTLTYPE_NODE, "udf",
SYSCTL_DESCR("OSTA Universal File System"),
NULL, 0, NULL, 0,
CTL_VFS, 24, CTL_EOL);
#ifdef UDF_DEBUG
sysctl_createv(clog, 0, NULL, &node,
CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
CTLTYPE_INT, "verbose",
SYSCTL_DESCR("Bitmask for filesystem debugging"),
NULL, 0, &udf_verbose, 0,
CTL_VFS, 24, UDF_VERBOSE_SYSCTLOPT, CTL_EOL);
#endif
}
static int
udf_modcmd(modcmd_t cmd, void *arg)
{
int error;
switch (cmd) {
case MODULE_CMD_INIT:
error = vfs_attach(&udf_vfsops);
if (error != 0)
break;
break;
case MODULE_CMD_FINI:
error = vfs_detach(&udf_vfsops);
if (error != 0)
break;
break;
default:
error = ENOTTY;
break;
}
return (error);
}
int
udf_mountroot(void)
{
return EOPNOTSUPP;
}
#define MPFREE(a, lst) \
if ((a)) free((a), lst);
static void
free_udf_mountinfo(struct mount *mp)
{
struct udf_mount *ump;
int i;
if (!mp)
return;
ump = VFSTOUDF(mp);
if (ump) {
for (i = 0; i < UDF_ANCHORS; i++)
MPFREE(ump->anchors[i], M_UDFVOLD);
MPFREE(ump->primary_vol, M_UDFVOLD);
MPFREE(ump->logical_vol, M_UDFVOLD);
MPFREE(ump->unallocated, M_UDFVOLD);
MPFREE(ump->implementation, M_UDFVOLD);
MPFREE(ump->logvol_integrity, M_UDFVOLD);
for (i = 0; i < UDF_PARTITIONS; i++) {
MPFREE(ump->partitions[i], M_UDFVOLD);
MPFREE(ump->part_unalloc_dscr[i], M_UDFVOLD);
MPFREE(ump->part_freed_dscr[i], M_UDFVOLD);
}
MPFREE(ump->metadata_unalloc_dscr, M_UDFVOLD);
MPFREE(ump->fileset_desc, M_UDFVOLD);
MPFREE(ump->sparing_table, M_UDFVOLD);
MPFREE(ump->la_node_ad_cpy, M_UDFMNT);
MPFREE(ump->la_pmapping, M_TEMP);
MPFREE(ump->la_lmapping, M_TEMP);
mutex_destroy(&ump->logvol_mutex);
mutex_destroy(&ump->allocate_mutex);
mutex_destroy(&ump->sync_lock);
MPFREE(ump->vat_table, M_UDFVOLD);
free(ump, M_UDFMNT);
}
}
#undef MPFREE
static void
udf_release_system_nodes(struct mount *mp)
{
struct udf_mount *ump = VFSTOUDF(mp);
if (!ump)
return;
if (ump->vat_node)
vrele(ump->vat_node->vnode);
if (ump->metadata_node)
vrele(ump->metadata_node->vnode);
if (ump->metadatamirror_node)
vrele(ump->metadatamirror_node->vnode);
if (ump->metadatabitmap_node)
vrele(ump->metadatabitmap_node->vnode);
}
int
udf_mount(struct mount *mp, const char *path,
void *data, size_t *data_len)
{
struct lwp *l = curlwp;
struct udf_args *args = data;
struct udf_mount *ump;
struct vnode *devvp;
int openflags, accessmode, error;
DPRINTF(CALL, ("udf_mount called\n"));
if (args == NULL)
return EINVAL;
if (*data_len < sizeof *args)
return EINVAL;
if (mp->mnt_flag & MNT_GETARGS) {
ump = VFSTOUDF(mp);
if (ump == NULL)
return EINVAL;
*args = ump->mount_args;
*data_len = sizeof *args;
return 0;
}
if (mp->mnt_flag & MNT_UPDATE) {
return EOPNOTSUPP;
}
if (args->version != 1) {
printf("mount_udf: unrecognized argument structure version\n");
return EINVAL;
}
error = namei_simple_user(args->fspec,
NSM_FOLLOW_NOEMULROOT, &devvp);
if (error)
return error;
#ifdef DEBUG
if (udf_verbose & UDF_DEBUG_VOLUMES)
vprint("UDF mount, trying to mount \n", devvp);
#endif
if (devvp->v_type != VBLK) {
vrele(devvp);
return ENOTBLK;
}
if (bdevsw_lookup(devvp->v_rdev) == NULL) {
vrele(devvp);
return ENXIO;
}
accessmode = VREAD;
if ((mp->mnt_flag & MNT_RDONLY) == 0)
accessmode |= VWRITE;
vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp, KAUTH_ARG(accessmode));
VOP_UNLOCK(devvp);
if (error) {
vrele(devvp);
return error;
}
if (mp->mnt_flag & MNT_RDONLY) {
openflags = FREAD;
} else {
openflags = FREAD | FWRITE;
}
vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
error = VOP_OPEN(devvp, openflags, FSCRED);
VOP_UNLOCK(devvp);
if (error == 0) {
error = udf_mountfs(devvp, mp, l, args);
if (error) {
udf_release_system_nodes(mp);
udf_discstrat_finish(VFSTOUDF(mp));
free_udf_mountinfo(mp);
vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
(void) VOP_CLOSE(devvp, openflags, NOCRED);
VOP_UNLOCK(devvp);
}
}
if (error) {
vrele(devvp);
return error;
}
spec_node_setmountedfs(devvp, mp);
DPRINTF(VOLUMES, ("udf_mount() successful\n"));
error = set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
mp->mnt_op->vfs_name, mp, l);
if (error)
return error;
if ((mp->mnt_flag & MNT_RDONLY) == 0) {
if ((error = udf_open_logvol(VFSTOUDF(mp))) != 0) {
printf( "mount_udf: can't open logical volume for "
"writing, downgrading access to read-only\n");
mp->mnt_flag |= MNT_RDONLY;
return 0;
}
}
return 0;
}
#ifdef DEBUG
static bool
udf_sanity_selector(void *cl, struct vnode *vp)
{
KASSERT(mutex_owned(vp->v_interlock));
vprint("", vp);
if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE) {
printf(" is locked\n");
}
if (vrefcnt(vp) > 1)
printf(" more than one usecount %d\n", vrefcnt(vp));
return false;
}
static void
udf_unmount_sanity_check(struct mount *mp)
{
struct vnode_iterator *marker;
printf("On unmount, i found the following nodes:\n");
vfs_vnode_iterator_init(mp, &marker);
vfs_vnode_iterator_next(marker, udf_sanity_selector, NULL);
vfs_vnode_iterator_destroy(marker);
}
#endif
int
udf_unmount(struct mount *mp, int mntflags)
{
struct udf_mount *ump;
int error, flags, closeflags;
DPRINTF(CALL, ("udf_umount called\n"));
ump = VFSTOUDF(mp);
if (!ump)
panic("UDF unmount: empty ump\n");
flags = (mntflags & MNT_FORCE) ? FORCECLOSE : 0;
#ifdef DEBUG
if (udf_verbose & UDF_DEBUG_LOCKING)
udf_unmount_sanity_check(mp);
#endif
if ((error = vflush(mp, NULLVP, flags | SKIPSYSTEM)) != 0)
return error;
udf_sync(mp, FSYNC_WAIT, NOCRED);
#ifdef DEBUG
if (udf_verbose & UDF_DEBUG_LOCKING)
udf_unmount_sanity_check(mp);
#endif
if ((error = vflush(ump->vfs_mountp, NULLVP, flags | SKIPSYSTEM)) != 0)
return error;
DPRINTF(VOLUMES, ("flush OK on unmount\n"));
if ((error = udf_close_logvol(ump, mntflags)) != 0)
return error;
#ifdef DEBUG
DPRINTF(VOLUMES, ("FINAL sanity check\n"));
if (udf_verbose & UDF_DEBUG_LOCKING)
udf_unmount_sanity_check(mp);
#endif
udf_release_system_nodes(mp);
if ((error = vflush(ump->vfs_mountp, NULLVP, 0)) != 0)
panic("Failure to flush UDF system vnodes\n");
udf_discstrat_finish(ump);
(void) udf_synchronise_caches(ump);
DPRINTF(VOLUMES, ("closing device\n"));
if (mp->mnt_flag & MNT_RDONLY) {
closeflags = FREAD;
} else {
closeflags = FREAD | FWRITE;
}
vn_lock(ump->devvp, LK_EXCLUSIVE | LK_RETRY);
error = VOP_CLOSE(ump->devvp, closeflags, NOCRED);
if (error)
printf("Error during closure of device! error %d, "
"device might stay locked\n", error);
DPRINTF(VOLUMES, ("device close ok\n"));
spec_node_setmountedfs(ump->devvp, NULL);
vput(ump->devvp);
free_udf_mountinfo(mp);
mp->mnt_data = NULL;
mp->mnt_flag &= ~MNT_LOCAL;
DPRINTF(VOLUMES, ("Fin unmount\n"));
return error;
}
static int
udf_mountfs(struct vnode *devvp, struct mount *mp,
struct lwp *l, struct udf_args *args)
{
struct udf_mount *ump;
uint32_t sector_size, lb_size, bshift;
uint32_t logvol_integrity;
int num_anchors, error;
vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0);
VOP_UNLOCK(devvp);
if (error)
return error;
mp->mnt_data = NULL;
mp->mnt_stat.f_fsidx.__fsid_val[0] = (uint32_t) devvp->v_rdev;
mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_UDF);
mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
mp->mnt_stat.f_namemax = UDF_MAXNAMLEN;
mp->mnt_flag |= MNT_LOCAL;
ump = malloc(sizeof(struct udf_mount), M_UDFMNT, M_WAITOK | M_ZERO);
mutex_init(&ump->logvol_mutex, MUTEX_DEFAULT, IPL_NONE);
mutex_init(&ump->allocate_mutex, MUTEX_DEFAULT, IPL_NONE);
mutex_init(&ump->sync_lock, MUTEX_DEFAULT, IPL_NONE);
udf_init_nodes_tree(ump);
mp->mnt_data = ump;
ump->vfs_mountp = mp;
ump->mount_args = *args;
ump->devvp = devvp;
if ((error = udf_update_discinfo(ump))) {
printf("UDF mount: error inspecting fs node\n");
return error;
}
sector_size = ump->discinfo.sector_size;
bshift = 1;
while ((1 << bshift) < sector_size)
bshift++;
if ((1 << bshift) != sector_size) {
printf("UDF mount: "
"hit NetBSD implementation fence on sector size\n");
return EIO;
}
if (sector_size >= 8192) {
printf("UDF mount: "
"hit implementation limit, sectorsize to big\n");
return EIO;
}
if ((mp->mnt_flag & MNT_RDONLY) == 0) {
if ((ump->discinfo.mmc_cur & MMC_CAP_RECORDABLE) == 0) {
printf("UDF mount: disc is not recordable\n");
return EROFS;
}
if (ump->discinfo.mmc_cur & MMC_CAP_SEQUENTIAL) {
if (ump->discinfo.disc_state == MMC_STATE_FULL) {
printf("UDF mount: disc is not appendable\n");
return EROFS;
}
}
if (args->sessionnr != 0) {
printf("UDF mount: updating a previous session "
"not yet allowed\n");
return EROFS;
}
}
ump->strategy = &udf_strat_bootstrap;
udf_discstrat_init(ump);
num_anchors = udf_read_anchors(ump);
if (num_anchors == 0)
return EINVAL;
DPRINTF(VOLUMES, ("Read %d anchors on this disc, session %d\n",
num_anchors, args->sessionnr));
if ((error = udf_read_vds_space(ump))) {
printf("UDF mount: error reading volume space\n");
return error;
}
udf_discstrat_finish(ump);
if ((error = udf_process_vds(ump))) {
printf( "UDF mount: disc not properly formatted"
"(bad VDS)\n");
return error;
}
KASSERT(ump->strategy != &udf_strat_bootstrap);
udf_discstrat_init(ump);
ump->la_lmapping = malloc(sizeof(uint64_t) * UDF_MAX_MAPPINGS,
M_TEMP, M_WAITOK);
ump->la_pmapping = malloc(sizeof(uint64_t) * UDF_MAX_MAPPINGS,
M_TEMP, M_WAITOK);
lb_size = udf_rw32(ump->logical_vol->lb_size);
ump->la_node_ad_cpy = malloc(lb_size * UDF_MAX_ALLOC_EXTENTS,
M_UDFMNT, M_WAITOK);
memset(ump->la_node_ad_cpy, 0, lb_size * UDF_MAX_ALLOC_EXTENTS);
mp->mnt_data = ump;
mp->mnt_dev_bshift = bshift;
mp->mnt_fs_bshift = bshift;
if ((error = udf_read_vds_tables(ump))) {
printf( "UDF mount: error in format or damaged disc "
"(VDS tables failing)\n");
return error;
}
logvol_integrity = udf_rw32(ump->logvol_integrity->integrity_type);
if (logvol_integrity != UDF_INTEGRITY_CLOSED) {
printf("UDF mount: file system is not clean; ");
printf("please fsck(8)\n");
return EPERM;
}
if ((error = udf_read_rootdirs(ump))) {
printf( "UDF mount: "
"disc not properly formatted or damaged disc "
"(rootdirs failing)\n");
return error;
}
return 0;
}
int
udf_start(struct mount *mp, int flags)
{
return 0;
}
int
udf_root(struct mount *mp, int lktype, struct vnode **vpp)
{
struct vnode *vp;
struct long_ad *dir_loc;
struct udf_mount *ump = VFSTOUDF(mp);
struct udf_node *root_dir;
int error;
DPRINTF(CALL, ("udf_root called\n"));
dir_loc = &ump->fileset_desc->rootdir_icb;
error = udf_get_node(ump, dir_loc, &root_dir, lktype);
if (error)
return error;
KASSERT(root_dir);
vp = root_dir->vnode;
KASSERT(vp->v_vflag & VV_ROOT);
*vpp = vp;
return 0;
}
int
udf_statvfs(struct mount *mp, struct statvfs *sbp)
{
struct udf_mount *ump = VFSTOUDF(mp);
struct logvol_int_desc *lvid;
struct udf_logvol_info *impl;
uint64_t freeblks, sizeblks;
int num_part;
DPRINTF(CALL, ("udf_statvfs called\n"));
sbp->f_flag = mp->mnt_flag;
sbp->f_bsize = ump->discinfo.sector_size;
sbp->f_frsize = ump->discinfo.sector_size;
sbp->f_iosize = ump->discinfo.sector_size;
mutex_enter(&ump->allocate_mutex);
udf_calc_freespace(ump, &sizeblks, &freeblks);
sbp->f_blocks = sizeblks;
sbp->f_bfree = freeblks;
sbp->f_files = 0;
lvid = ump->logvol_integrity;
num_part = udf_rw32(lvid->num_part);
impl = (struct udf_logvol_info *) (lvid->tables + 2*num_part);
if (impl) {
sbp->f_files = udf_rw32(impl->num_files);
sbp->f_files += udf_rw32(impl->num_directories);
}
sbp->f_bavail = 0;
sbp->f_bresvd = 0;
sbp->f_ffree = 0;
sbp->f_favail = 0;
sbp->f_fresvd = 0;
mutex_exit(&ump->allocate_mutex);
copy_statvfs_info(sbp, mp);
return 0;
}
static int
udf_sync_writeout_system_files(struct udf_mount *ump, int clearflags)
{
int error;
DPRINTF(CALL, ("udf_sync: syncing metadata\n"));
if (ump->lvclose & UDF_WRITE_VAT)
udf_writeout_vat(ump);
error = 0;
if (ump->lvclose & UDF_WRITE_PART_BITMAPS) {
error = udf_write_metadata_partition_spacetable(ump, MNT_WAIT);
if (error)
printf( "udf_writeout_system_files : "
" writeout of metadata space bitmap failed\n");
error = udf_write_physical_partition_spacetables(ump, MNT_WAIT);
if (error)
printf( "udf_writeout_system_files : "
"writeout of space tables failed\n");
if (!error && clearflags)
ump->lvclose &= ~UDF_WRITE_PART_BITMAPS;
}
return error;
}
int
udf_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
{
struct udf_mount *ump = VFSTOUDF(mp);
DPRINTF(CALL, ("udf_sync called\n"));
if (mp->mnt_flag & MNT_RDONLY)
return 0;
if (ump->syncing && !waitfor) {
printf("UDF: skipping autosync\n");
return 0;
}
ump->syncing = 1;
udf_do_sync(ump, cred, waitfor);
if (waitfor == MNT_WAIT)
udf_sync_writeout_system_files(ump, true);
DPRINTF(CALL, ("end of udf_sync()\n"));
ump->syncing = 0;
return 0;
}
int
udf_vget(struct mount *mp, ino_t ino, int lktype,
struct vnode **vpp)
{
DPRINTF(NOTIMPL, ("udf_vget called\n"));
return EOPNOTSUPP;
}
int
udf_fhtovp(struct mount *mp, struct fid *fhp, int lktype,
struct vnode **vpp)
{
DPRINTF(NOTIMPL, ("udf_fhtovp called\n"));
return EOPNOTSUPP;
}
int
udf_vptofh(struct vnode *vp, struct fid *fid,
size_t *fh_size)
{
DPRINTF(NOTIMPL, ("udf_vptofh called\n"));
return EOPNOTSUPP;
}
int
udf_snapshot(struct mount *mp, struct vnode *vp,
struct timespec *tm)
{
DPRINTF(NOTIMPL, ("udf_snapshot called\n"));
return EOPNOTSUPP;
}