Support RENAME_EXCHANGE on bpffs
Subsystems (6)
cover Support RENAME_EXCHANGE on bpffs
Add support for renameat2(RENAME_EXCHANGE) on bpffs. This is useful for atomic upgrades of our sk_lookup control plane.
* Create a temporary directory on bpffs
* Migrate maps and pin them into temporary directory
* Load new sk_lookup BPF, attach it and pin the link into temp dir
* renameat2(temp dir, state dir, RENAME_EXCHANGE)
* rmdir temp dir
Due to the sk_lookup semantics this means we can never end up in a situation where an upgrade breaks the existing control plane.
Lorenz Bauer (2):
libfs: support RENAME_EXCHANGE in simple_rename()
selftests: bpf: test RENAME_EXCHANGE and RENAME_NOREPLACE on bpffs
fs/libfs.c | 6 ++- .../selftests/bpf/prog_tests/test_bpffs.c | 39 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-)
Signature · 1 line
2.30.2
PATCH 1/2 libfs: support RENAME_EXCHANGE in simple_rename()
Allow atomic exchange via RENAME_EXCHANGE when using simple_rename. This affects binderfs, ramfs, hubetlbfs and bpffs. There isn't much to do except update the various *time fields.
--- fs/libfs.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
fs/libfs.c +5 -1
@@ -455,9 +455,12 @@ int simple_rename(struct user_namespace *mnt_userns, struct inode *old_dir,struct inode *inode =d_inode (old_dentry);int they_are_dirs =d_is_dir (old_dentry); -if (flags & ~RENAME_NOREPLACE ) +if (flags & ~(RENAME_NOREPLACE |RENAME_EXCHANGE ))return -EINVAL ; +if (flags &RENAME_EXCHANGE ) + goto done; +if (!simple_empty (new_dentry))return -ENOTEMPTY ;
@@ -472,6 +475,7 @@ int simple_rename(struct user_namespace *mnt_userns, struct inode *old_dir,inc_nlink (new_dir); } +done: old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime = new_dir->i_mtime = inode->i_ctime =current_time (old_dir);
Signature · 1 line
2.30.2
PATCH 2/2 selftests: bpf: test RENAME_EXCHANGE and RENAME_NOREPLACE on bpffs
In reply to Lorenz BauerAdd tests to exercise the behaviour of RENAME_EXCHANGE and RENAME_NOREPLACE on bpffs. The former checks that after an exchange the inode of two directories has changed. The latter checks that the source still exists after a failed rename.
--- .../selftests/bpf/prog_tests/test_bpffs.c | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+)
tools/testing/selftests/bpf/prog_tests/test_bpffs.c +39 -0
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0 /* Copyright (c) 2020 Facebook */ #define _GNU_SOURCE +#include <stdio.h> #include <sched.h> #include <sys/mount.h> #include <sys/stat.h>
@@ -29,6 +30,7 @@ static int read_iter(char *file)static int fn (void ) { +struct stat a, b;int err, duration =0 ; err =unshare (CLONE_NEWNS );
@@ -67,6 +69,43 @@ static int fn(void) err =read_iter (TDIR "/fs2/progs.debug" );if (CHECK (err,"reading " TDIR "/fs2/progs.debug" ,"failed\n" )) goto out; + + err =mkdir (TDIR "/fs1/a" ,0777 ); +if (CHECK (err,"creating " TDIR "/fs1/a" ,"failed\n" )) + goto out; + err =mkdir (TDIR "/fs1/a/1" ,0777 ); +if (CHECK (err,"creating " TDIR "/fs1/a/1" ,"failed\n" )) + goto out; + err =mkdir (TDIR "/fs1/b" ,0777 ); +if (CHECK (err,"creating " TDIR "/fs1/b" ,"failed\n" )) + goto out; + +/* Check that RENAME_EXCHANGE works. */ + err =stat (TDIR "/fs1/a" , &a); +if (CHECK (err,"stat(" TDIR "/fs1/a)" ,"failed\n" )) + goto out; + err =renameat2 (0 ,TDIR "/fs1/a" ,0 ,TDIR "/fs1/b" ,RENAME_EXCHANGE ); +if (CHECK (err,"renameat2(RENAME_EXCHANGE)" ,"failed\n" )) + goto out; + err =stat (TDIR "/fs1/b" , &b); +if (CHECK (err,"stat(" TDIR "/fs1/b)" ,"failed\n" )) + goto out; +if (CHECK (a.st_ino != b.st_ino,"b should have a's inode" ,"failed\n" )) + goto out; + err =access (TDIR "/fs1/b/1" ,F_OK ); +if (CHECK (err,"access(" TDIR "/fs1/b/1)" ,"failed\n" )) + goto out; + +/* Check that RENAME_NOREPLACE works. */ + err =renameat2 (0 ,TDIR "/fs1/b" ,0 ,TDIR "/fs1/a" ,RENAME_NOREPLACE ); +if (CHECK (!err,"renameat2(RENAME_NOREPLACE)" ,"succeeded\n" )) { + err = -EINVAL ; + goto out; + } + err =access (TDIR "/fs1/b" ,F_OK ); +if (CHECK (err,"access(" TDIR "/fs1/b)" ,"failed\n" )) + goto out; + out:umount (TDIR "/fs1" );umount (TDIR "/fs2" );
Signature · 1 line
2.30.2
Re: [PATCH bpf-next 2/2] selftests: bpf: test RENAME_EXCHANGE and RENAME_NOREPLACE on bpffs
Quoted 62 lines · Lorenz Bauer
Add tests to exercise the behaviour of
RENAME_EXCHANGEandRENAME_NOREPLACEon bpffs. The former checks that after an exchange the inode of two directories has changed. The latter checks that the source still exists after a failed rename.Signed-off-by Lorenz Bauer <[email protected]>--- .../selftests/bpf/prog_tests/test_bpffs.c | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+)tools/testing/selftests/bpf/prog_tests/test_bpffs.c +29
-0@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0 /* Copyright (c) 2020 Facebook */ #define _GNU_SOURCE +#include <stdio.h> #include <sched.h> #include <sys/mount.h> #include <sys/stat.h> @@ -29,6 +30,7 @@ static int read_iter(char *file)static int fn (void ) { +struct stat a, b;int err, duration =0 ; err =unshare (CLONE_NEWNS );@@ -67,6 +69,43 @@ static int fn(void) err =read_iter (TDIR "/fs2/progs.debug" );if (CHECK (err,"reading " TDIR "/fs2/progs.debug" ,"failed\n" )) goto out; + + err =mkdir (TDIR "/fs1/a" ,0777 ); +if (CHECK (err,"creating " TDIR "/fs1/a" ,"failed\n" )) + goto out; + err =mkdir (TDIR "/fs1/a/1" ,0777 ); +if (CHECK (err,"creating " TDIR "/fs1/a/1" ,"failed\n" )) + goto out; + err =mkdir (TDIR "/fs1/b" ,0777 ); +if (CHECK (err,"creating " TDIR "/fs1/b" ,"failed\n" )) + goto out; + +/* Check that RENAME_EXCHANGE works. */ + err =stat (TDIR "/fs1/a" , &a); +if (CHECK (err,"stat(" TDIR "/fs1/a)" ,"failed\n" )) + goto out; + err =renameat2 (0 ,TDIR "/fs1/a" ,0 ,TDIR "/fs1/b" ,RENAME_EXCHANGE ); +if (CHECK (err,"renameat2(RENAME_EXCHANGE)" ,"failed\n" )) + goto out; + err =stat (TDIR "/fs1/b" , &b); +if (CHECK (err,"stat(" TDIR "/fs1/b)" ,"failed\n" )) + goto out; +if (CHECK (a.st_ino != b.st_ino,"b should have a's inode" ,"failed\n" )) + goto out; + err =access (TDIR "/fs1/b/1" ,F_OK ); +if (CHECK (err,"access(" TDIR "/fs1/b/1)" ,"failed\n" )) + goto out; +
tools/testing/selftests/bpf/prog_tests/test_bpffs.c static int fn(void) · line 108
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/test_bpffs.c +10
-0+/* Check that RENAME_NOREPLACE works. */ + err =renameat2 (0 ,TDIR "/fs1/b" ,0 ,TDIR "/fs1/a" ,RENAME_NOREPLACE ); +if (CHECK (!err,"renameat2(RENAME_NOREPLACE)" ,"succeeded\n" )) { + err = -EINVAL ; + goto out; + } + err =access (TDIR "/fs1/b" ,F_OK ); +if (CHECK (err,"access(" TDIR "/fs1/b)" ,"failed\n" )) + goto out; +
Please use ASSERT_xxx() for new code.
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/test_bpffs.c +0
-1out:umount (TDIR "/fs1" );umount (TDIR "/fs2" ); --2.30.2
Quoted 11 lines · Lorenz Bauer
Add support for
renameat2(RENAME_EXCHANGE)on bpffs. This is useful for atomic upgrades of oursk_lookupcontrol plane.* Create a temporary directory on bpffs
* Migrate maps and pin them into temporary directory
* Load newsk_lookupBPF, attach it and pin the link into temp dir
*renameat2(temp dir, state dir, RENAME_EXCHANGE)
* rmdir temp dirDue to the
sk_lookupsemantics this means we can never end up in a situation where an upgrade breaks the existing control plane.
That is a cool idea.
The selftest doesn't exercise all of this logic.
It's only testing that RENAME_EXCHANGE works on directories within bpffs. Do we need a test for other types of paths?