unionfs: Support renaming symbolic links

This adds support for renaming a symbolic link found on the lower fs,
which necessitates copying it to the upper fs, as well as basic tests.

MFC after:	1 week
Sponsored by:	Klara, Inc.
Sponsored by:	NetApp, Inc.
Reviewed by:	olce, siderop1_netapp.com, jah
Differential Revision:	https://reviews.freebsd.org/D54229
This commit is contained in:
Dag-Erling Smørgrav 2025-12-17 23:38:11 +01:00
parent 104827151e
commit a678e87f55
7 changed files with 355 additions and 0 deletions

View file

@ -826,6 +826,8 @@
..
tmpfs
..
unionfs
..
..
geom
class

View file

@ -142,6 +142,7 @@ void unionfs_tryrem_node_status(struct unionfs_node *,
int unionfs_check_rmdir(struct vnode *, struct ucred *, struct thread *td);
int unionfs_copyfile(struct vnode *, int, struct ucred *,
struct thread *);
int unionfs_copylink(struct vnode *, struct ucred *, struct thread *);
void unionfs_create_uppervattr_core(struct unionfs_mount *, struct vattr *,
struct vattr *, struct thread *);
int unionfs_create_uppervattr(struct unionfs_mount *, struct vnode *,

View file

@ -1516,6 +1516,174 @@ unionfs_copyfile_cleanup:
return (error);
}
/*
* Create a new symbolic link on upper.
*
* If an error is returned, *vpp will be invalid, otherwise it will hold a
* locked, referenced and opened vnode.
*
* unp is never updated.
*/
static int
unionfs_vn_symlink_on_upper(struct vnode **vpp, struct vnode *udvp,
struct vnode *vp, struct vattr *uvap, const char *target,
struct thread *td)
{
struct unionfs_mount *ump;
struct unionfs_node *unp;
struct vnode *uvp;
struct vnode *lvp;
struct ucred *cred;
struct vattr lva;
struct nameidata nd;
int error;
ASSERT_VOP_ELOCKED(vp, __func__);
unp = VTOUNIONFS(vp);
ump = MOUNTTOUNIONFSMOUNT(UNIONFSTOV(unp)->v_mount);
uvp = NULL;
lvp = unp->un_lowervp;
cred = td->td_ucred;
error = 0;
if ((error = VOP_GETATTR(lvp, &lva, cred)) != 0)
return (error);
unionfs_create_uppervattr_core(ump, &lva, uvap, td);
if (unp->un_path == NULL)
panic("%s: NULL un_path", __func__);
nd.ni_cnd.cn_namelen = unp->un_pathlen;
nd.ni_cnd.cn_pnbuf = unp->un_path;
nd.ni_cnd.cn_nameiop = CREATE;
nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | ISLASTCN;
nd.ni_cnd.cn_lkflags = LK_EXCLUSIVE;
nd.ni_cnd.cn_cred = cred;
nd.ni_cnd.cn_nameptr = nd.ni_cnd.cn_pnbuf;
NDPREINIT(&nd);
vref(udvp);
VOP_UNLOCK(vp);
if ((error = vfs_relookup(udvp, &uvp, &nd.ni_cnd, false)) != 0) {
vrele(udvp);
return (error);
}
if (uvp != NULL) {
if (uvp == udvp)
vrele(uvp);
else
vput(uvp);
error = EEXIST;
goto unionfs_vn_symlink_on_upper_cleanup;
}
error = VOP_SYMLINK(udvp, &uvp, &nd.ni_cnd, uvap, target);
if (error == 0)
*vpp = uvp;
unionfs_vn_symlink_on_upper_cleanup:
vput(udvp);
return (error);
}
/*
* Copy symbolic link from lower to upper.
*
* vp is a unionfs vnode that should be locked on entry and will be
* locked on return.
*
* If no error returned, unp will be updated.
*/
int
unionfs_copylink(struct vnode *vp, struct ucred *cred,
struct thread *td)
{
struct unionfs_node *unp;
struct unionfs_node *dunp;
struct mount *mp;
struct vnode *udvp;
struct vnode *lvp;
struct vnode *uvp;
struct vattr uva;
char *buf = NULL;
struct uio uio;
struct iovec iov;
int error;
ASSERT_VOP_ELOCKED(vp, __func__);
unp = VTOUNIONFS(vp);
lvp = unp->un_lowervp;
uvp = NULL;
if ((UNIONFSTOV(unp)->v_mount->mnt_flag & MNT_RDONLY))
return (EROFS);
if (unp->un_dvp == NULL)
return (EINVAL);
if (unp->un_uppervp != NULL)
return (EEXIST);
udvp = NULL;
VI_LOCK(unp->un_dvp);
dunp = VTOUNIONFS(unp->un_dvp);
if (dunp != NULL)
udvp = dunp->un_uppervp;
VI_UNLOCK(unp->un_dvp);
if (udvp == NULL)
return (EROFS);
if ((udvp->v_mount->mnt_flag & MNT_RDONLY))
return (EROFS);
ASSERT_VOP_UNLOCKED(udvp, __func__);
error = unionfs_set_in_progress_flag(vp, UNIONFS_COPY_IN_PROGRESS);
if (error == EJUSTRETURN)
return (0);
else if (error != 0)
return (error);
uio.uio_td = td;
uio.uio_segflg = UIO_SYSSPACE;
uio.uio_offset = 0;
uio.uio_iov = &iov;
uio.uio_iovcnt = 1;
iov.iov_base = buf = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
uio.uio_resid = iov.iov_len = MAXPATHLEN;
uio.uio_rw = UIO_READ;
if ((error = VOP_READLINK(lvp, &uio, cred)) != 0)
goto unionfs_copylink_cleanup;
buf[iov.iov_len - uio.uio_resid] = '\0';
if ((error = vn_start_write(udvp, &mp, V_WAIT | V_PCATCH)) != 0)
goto unionfs_copylink_cleanup;
error = unionfs_vn_symlink_on_upper(&uvp, udvp, vp, &uva, buf, td);
vn_finished_write(mp);
if (error != 0) {
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
goto unionfs_copylink_cleanup;
}
vn_lock_pair(vp, false, LK_EXCLUSIVE, uvp, true, LK_EXCLUSIVE);
unp = VTOUNIONFS(vp);
if (unp == NULL) {
error = ENOENT;
goto unionfs_copylink_cleanup;
}
if (error == 0) {
/* Reset the attributes. Ignore errors. */
uva.va_type = VNON;
VOP_SETATTR(uvp, &uva, cred);
unionfs_node_update(unp, uvp, td);
}
unionfs_copylink_cleanup:
if (buf != NULL)
free(buf, M_TEMP);
unionfs_clear_in_progress_flag(vp, UNIONFS_COPY_IN_PROGRESS);
return (error);
}
/*
* Determine if the unionfs view of a directory is empty such that
* an rmdir operation can be permitted.

View file

@ -1478,6 +1478,13 @@ unionfs_rename(struct vop_rename_args *ap)
*/
VOP_UNLOCK(tdvp);
relock_tdvp = true;
} else if (fvp->v_type == VLNK) {
/*
* The symbolic link case is similar to the
* regular file case.
*/
VOP_UNLOCK(tdvp);
relock_tdvp = true;
} else if (fvp->v_type == VDIR && tdvp != fdvp) {
/*
* For directories, unionfs_mkshadowdir() will expect
@ -1501,6 +1508,9 @@ unionfs_rename(struct vop_rename_args *ap)
case VREG:
error = unionfs_copyfile(fvp, 1, fcnp->cn_cred, td);
break;
case VLNK:
error = unionfs_copylink(fvp, fcnp->cn_cred, td);
break;
case VDIR:
error = unionfs_mkshadowdir(fdvp, fvp, fcnp, td);
break;

View file

@ -14,6 +14,7 @@ TESTS_SUBDIRS+= fusefs
.endif
TESTS_SUBDIRS+= tarfs
TESTS_SUBDIRS+= tmpfs
TESTS_SUBDIRS+= unionfs
${PACKAGE}FILES+= h_funcs.subr
${PACKAGE}FILESDIR= ${TESTSDIR}

View file

@ -0,0 +1,8 @@
PACKAGE= tests
TESTSDIR= ${TESTSBASE}/sys/fs/unionfs
BINDIR= ${TESTSDIR}
ATF_TESTS_SH+= unionfs_test
.include <bsd.test.mk>

View file

@ -0,0 +1,165 @@
#!/bin/sh
#-
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (c) 2025 Klara, Inc.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# Create and mount a filesystem for use in our tests
unionfs_mkfs() {
local name=$1
local size=${2:-1}
# Create mountpoint
atf_check mkdir ${name}
# Create filesystem image
atf_check -e ignore dd if=/dev/zero of=${name}.img bs=1m count=${size}
echo ${name} >>imgs
# Create memory disk
atf_check -o save:${name}.md mdconfig ${name}.img
md=$(cat ${name}.md)
echo ${md} >>mds
# Format and mount filesystem
atf_check -o ignore newfs /dev/${md}
atf_check mount /dev/${md} ${name}
echo ${name} >>mounts
}
# Mount a unionfs
unionfs_mount() {
local upper=$1
local lower=$2
# Mount upper over lower
atf_check mount -t unionfs ${upper} ${lower}
echo ${lower} >>mounts
}
# Clean up after a test
unionfs_cleanup() {
# Unmount filesystems
if [ -f mounts ]; then
tail -r mounts | while read mount; do
umount ${mount} || true
done
fi
# Destroy memory disks
if [ -f mds ]; then
tail -r mds | while read md; do
mdconfig -d -u ${md} || true
done
fi
# Delete filesystem images and mountpoints
if [ -f imgs ]; then
tail -r imgs | while read name; do
rm -f ${name}.img || true
rmdir ${name} || true
done
fi
}
atf_test_case unionfs_basic cleanup
unionfs_basic_head() {
atf_set "descr" "Basic function test"
atf_set "require.user" "root"
atf_set "require.kmods" "unionfs"
}
unionfs_basic_body() {
# Create upper and lower
unionfs_mkfs upper
unionfs_mkfs lower
# Mount upper over lower
unionfs_mount upper lower
# Create object on unionfs
atf_check touch upper/file
atf_check mkdir upper/dir
atf_check touch lower/dir/file
# Verify that objects were created on upper
atf_check test -f lower/file
atf_check test -d lower/dir
atf_check test -f upper/dir/file
}
unionfs_basic_cleanup() {
unionfs_cleanup
}
atf_test_case unionfs_exec cleanup
unionfs_exec_head() {
atf_set "descr" "Test executing programs"
atf_set "require.user" "root"
atf_set "require.kmods" "unionfs"
}
unionfs_exec_body() {
# Create upper and copy a binary to it
unionfs_mkfs upper
atf_check cp -p /usr/bin/true upper/upper
# Create lower and copy a binary to it
unionfs_mkfs lower
atf_check cp -p /usr/bin/true lower/lower
# Mount upper over lower
unionfs_mount upper lower
# Execute both binaries
atf_check lower/lower
atf_check lower/upper
}
unionfs_exec_cleanup() {
unionfs_cleanup
}
atf_test_case unionfs_rename cleanup
unionfs_rename_head() {
atf_set "descr" "Test renaming objects on lower"
atf_set "require.user" "root"
atf_set "require.kmods" "unionfs"
}
unionfs_rename_body() {
# Create upper and lower
unionfs_mkfs upper
unionfs_mkfs lower
# Create objects on lower
atf_check touch lower/file
atf_check mkdir lower/dir
atf_check ln -s dead lower/link
# Mount upper over lower
unionfs_mount upper lower
# Rename objects
atf_check mv lower/file lower/newfile
atf_check mv lower/dir lower/newdir
atf_check mv lower/link lower/newlink
# Verify that old names no longer exist
atf_check test ! -f lower/file
atf_check test ! -d lower/dir
atf_check test ! -L lower/link
# Verify that new names exist on upper
atf_check test -f upper/newfile
atf_check test -d upper/newdir
atf_check test -L upper/newlink
}
unionfs_rename_cleanup() {
unionfs_cleanup
}
atf_init_test_cases() {
atf_add_test_case unionfs_basic
atf_add_test_case unionfs_exec
atf_add_test_case unionfs_rename
}