at commit 89c2c777

1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_FS_STRUCT_H
3 #define _LINUX_FS_STRUCT_H
4
5 #include <linux/sched.h>
6 #include <linux/path.h>
7 #include <linux/spinlock.h>
8 #include <linux/seqlock.h>
9 #include <linux/vfsdebug.h>
10
11 struct fs_struct {
12 int users;
13 seqlock_t seq;
14 int umask;
15 int in_exec;
16 struct path root, pwd;
17 } __randomize_layout;
18
19 extern struct kmem_cache *fs_cachep;
20 extern struct fs_struct *userspace_init_fs;
21
22 extern void exit_fs(struct task_struct *);
23 extern void set_fs_root(struct fs_struct *, const struct path *);
24 extern void set_fs_pwd(struct fs_struct *, const struct path *);
25 extern struct fs_struct *copy_fs_struct(struct fs_struct *);
26 extern void free_fs_struct(struct fs_struct *);
27 extern int unshare_fs_struct(void);
28
29 static inline void get_fs_root(struct fs_struct *fs, struct path *root)
30 {
31 read_seqlock_excl(&fs->seq);
32 *root = fs->root;
33 path_get(root);
34 read_sequnlock_excl(&fs->seq);
35 }
36
37 static inline void get_fs_pwd(struct fs_struct *fs, struct path *pwd)
38 {
39 read_seqlock_excl(&fs->seq);
40 *pwd = fs->pwd;
41 path_get(pwd);
42 read_sequnlock_excl(&fs->seq);
43 }
44
45 struct fs_struct *switch_fs_struct(struct fs_struct *new_fs);
46
47 extern bool current_chrooted(void);
48
49 static inline int current_umask(void)
50 {
51 return current->fs->umask;
52 }
53
54 /*
55 * Temporarily use userspace_init_fs for path resolution in kthreads.
56 * Callers should use scoped_with_init_fs() which automatically
57 * restores the original fs_struct at scope exit.
58 */
59 static inline struct fs_struct *__override_init_fs(void)
60 {
61 struct fs_struct *old_fs;
62
63 old_fs = current->fs;
64 WRITE_ONCE(current->fs, userspace_init_fs);
65 return old_fs;
66 }
67
68 static inline void __revert_init_fs(struct fs_struct *old_fs)
69 {
70 VFS_WARN_ON_ONCE(current->fs != userspace_init_fs);
71 WRITE_ONCE(current->fs, old_fs);
72 }
73
74 DEFINE_CLASS(__override_init_fs,
75 struct fs_struct *,
76 __revert_init_fs(_T),
77 __override_init_fs(), void)
78
79 #define scoped_with_init_fs() \
80 scoped_class(__override_init_fs, __UNIQUE_ID(label))
81
82 void __init init_userspace_fs(void);
83
84 #endif /* _LINUX_FS_STRUCT_H */