summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorChristian Brauner <brauner@kernel.org>2026-06-01 15:56:36 +0200
committerChristian Brauner <brauner@kernel.org>2026-06-29 10:38:48 +0200
commit67c54d1d730a8b7a43f2560dcea9b7dc95fba1cd (patch)
tree2ea97e2319dfa880a0e910da56221b2ca527e859 /include
parentd114eb8577bd8d6c307ab0a096ae7df93fb7e011 (diff)
downloadlinux-next-67c54d1d730a8b7a43f2560dcea9b7dc95fba1cd.tar.gz
linux-next-67c54d1d730a8b7a43f2560dcea9b7dc95fba1cd.zip
fs: add scoped_with_init_fs()
Similar to scoped_with_kernel_creds() allow a temporary override of current->fs to serve the few places where lookup is performed from kthread context or needs init's filesytem state. Link: https://patch.msgid.link/20260601-work-kthread-nullfs-v4-3-77ee053060e0@kernel.org Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
Diffstat (limited to 'include')
-rw-r--r--include/linux/fs_struct.h31
1 files changed, 31 insertions, 0 deletions
diff --git a/include/linux/fs_struct.h b/include/linux/fs_struct.h
index ade459383f92..e11d0e57168f 100644
--- a/include/linux/fs_struct.h
+++ b/include/linux/fs_struct.h
@@ -6,6 +6,7 @@
#include <linux/path.h>
#include <linux/spinlock.h>
#include <linux/seqlock.h>
+#include <linux/vfsdebug.h>
struct fs_struct {
int users;
@@ -49,4 +50,34 @@ static inline int current_umask(void)
return current->fs->umask;
}
+/*
+ * Temporarily use userspace_init_fs for path resolution in kthreads.
+ * Callers should use scoped_with_init_fs() which automatically
+ * restores the original fs_struct at scope exit.
+ */
+static inline struct fs_struct *__override_init_fs(void)
+{
+ struct fs_struct *fs;
+
+ fs = current->fs;
+ WRITE_ONCE(current->fs, fs);
+ return fs;
+}
+
+static inline void __revert_init_fs(struct fs_struct *revert_fs)
+{
+ VFS_WARN_ON_ONCE(current->fs != revert_fs);
+ WRITE_ONCE(current->fs, revert_fs);
+}
+
+DEFINE_CLASS(__override_init_fs,
+ struct fs_struct *,
+ __revert_init_fs(_T),
+ __override_init_fs(), void)
+
+#define scoped_with_init_fs() \
+ scoped_class(__override_init_fs, __UNIQUE_ID(label))
+
+void __init init_userspace_fs(void);
+
#endif /* _LINUX_FS_STRUCT_H */