Date: Sat, 18 Jul 2026 00:28:10 +0000 From: Konstantin Belousov <kib@FreeBSD.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org Subject: git: e0fe82303f0d - stable/15 - renameat(2): add AT_RENAME_EXCHANGE flag Message-ID: <6a5ac89a.3f32c.70f2323c@gitrepo.freebsd.org>
index | next in thread | raw e-mail
The branch stable/15 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=e0fe82303f0d8474849103cf77976198380af1a5 commit e0fe82303f0d8474849103cf77976198380af1a5 Author: Konstantin Belousov <kib@FreeBSD.org> AuthorDate: 2026-06-18 18:12:01 +0000 Commit: Konstantin Belousov <kib@FreeBSD.org> CommitDate: 2026-07-18 00:27:24 +0000 renameat(2): add AT_RENAME_EXCHANGE flag (cherry picked from commit 8e6e77ec25bc5809500d29bf318a843b40205657) --- sys/kern/vfs_syscalls.c | 70 ++++++++++++++++++++++++++++++++++++++++--------- sys/sys/fcntl.h | 2 ++ 2 files changed, 59 insertions(+), 13 deletions(-) diff --git a/sys/kern/vfs_syscalls.c b/sys/kern/vfs_syscalls.c index 2e6b4e9a3cad..769ebbcd18dc 100644 --- a/sys/kern/vfs_syscalls.c +++ b/sys/kern/vfs_syscalls.c @@ -3754,12 +3754,13 @@ sys_renameat2(struct thread *td, struct renameat2_args *uap) #ifdef MAC static int kern_renameat_mac(struct thread *td, int oldfd, const char *old, int newfd, - const char *new, enum uio_seg pathseg, struct nameidata *fromnd) + const char *new, enum uio_seg pathseg, struct nameidata *fromnd, int op, + int ndflags) { int error; - NDINIT_ATRIGHTS(fromnd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1, - pathseg, old, oldfd, &cap_renameat_source_rights); + NDINIT_ATRIGHTS(fromnd, op, LOCKPARENT | LOCKLEAF | AUDITVNODE1 | + ndflags, pathseg, old, oldfd, &cap_renameat_source_rights); if ((error = namei(fromnd)) != 0) return (error); error = mac_vnode_check_rename_from(td->td_ucred, fromnd->ni_dvp, @@ -3784,23 +3785,35 @@ kern_renameat(struct thread *td, int oldfd, const char *old, int newfd, struct vnode *tvp, *fvp, *tdvp; struct nameidata fromnd, tond; uint64_t tondflags; - int error; + int error, fndflags, op; short irflag; + bool exchange; - if ((flags & ~(AT_RENAME_NOREPLACE)) != 0) + if ((flags & ~(AT_RENAME_NOREPLACE | AT_RENAME_EXCHANGE)) != 0) + return (EINVAL); + if ((flags & (AT_RENAME_NOREPLACE | AT_RENAME_EXCHANGE)) == + (AT_RENAME_NOREPLACE | AT_RENAME_EXCHANGE)) return (EINVAL); + if ((flags & AT_RENAME_EXCHANGE) != 0) { + op = RENAME; + exchange = true; + } else { + op = DELETE; + exchange = false; + } + fndflags = 0; again: tmp = mp = NULL; bwillwrite(); #ifdef MAC if (mac_vnode_check_rename_from_enabled()) { error = kern_renameat_mac(td, oldfd, old, newfd, new, pathseg, - &fromnd); + &fromnd, op, fndflags); if (error != 0) return (error); } else { #endif - NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | AUDITVNODE1, + NDINIT_ATRIGHTS(&fromnd, op, WANTPARENT | AUDITVNODE1 | fndflags, pathseg, old, oldfd, &cap_renameat_source_rights); if ((error = namei(&fromnd)) != 0) return (error); @@ -3808,6 +3821,11 @@ again: } #endif fvp = fromnd.ni_vp; + if (exchange && fvp == NULL) { + NDFREE_PNBUF(&fromnd); + vrele(fromnd.ni_dvp); + return (ENOENT); + } tondflags = LOCKPARENT | LOCKLEAF | NOCACHE | AUDITVNODE2; if (fromnd.ni_vp->v_type == VDIR) tondflags |= WILLBEDIR; @@ -3846,6 +3864,29 @@ again: error = EEXIST; goto out; } + if (exchange) { + if (tvp == NULL) { + error = ENOENT; + goto out; + } + if (tvp->v_type == VDIR && fndflags == 0) { + fndflags = WILLBEDIR; +again2: + NDFREE_PNBUF(&fromnd); + vrele(fromnd.ni_dvp); + vrele(fvp); + NDFREE_PNBUF(&tond); + VOP_VPUT_PAIR(tdvp, &tvp, true); + error = sig_intr(); + if (error != 0) + goto out1; + goto again; + } + if (tvp->v_type != VDIR && fndflags != 0) { + fndflags = 0; + goto again2; + } + } error = vn_start_write(fvp, &mp, V_NOWAIT); if (error != 0) { again1: @@ -3888,12 +3929,15 @@ again1: goto out; } if (tvp != NULL) { - if (fvp->v_type == VDIR && tvp->v_type != VDIR) { - error = ENOTDIR; - goto out; - } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) { - error = EISDIR; - goto out; + if (!exchange) { + if (fvp->v_type == VDIR && tvp->v_type != VDIR) { + error = ENOTDIR; + goto out; + } else if (fvp->v_type != VDIR && + tvp->v_type == VDIR) { + error = EISDIR; + goto out; + } } #ifdef CAPABILITIES if (newfd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) { diff --git a/sys/sys/fcntl.h b/sys/sys/fcntl.h index 80cbca4ea753..44f43f745762 100644 --- a/sys/sys/fcntl.h +++ b/sys/sys/fcntl.h @@ -260,7 +260,9 @@ typedef __pid_t pid_t; #define AT_EMPTY_PATH 0x4000 /* Operate on dirfd if path is empty */ #define AT_RENAME_NOREPLACE 0x0001 /* Fail rename if target exists */ +#define AT_RENAME_EXCHANGE 0x0002 /* Atomically exchange 'from' and 'to' */ #define RENAME_NOREPLACE AT_RENAME_NOREPLACE +#define RENAME_EXCHANGE AT_RENAME_EXCHANGE #endif /* __BSD_VISIBLE */ /*home | help
Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?6a5ac89a.3f32c.70f2323c>
