Blame view

fs/fs_struct.c 3.31 KB
630d9c472   Paul Gortmaker   fs: reduce the us...
1
  #include <linux/export.h>
3f07c0144   Ingo Molnar   sched/headers: Pr...
2
  #include <linux/sched/signal.h>
299300258   Ingo Molnar   sched/headers: Pr...
3
  #include <linux/sched/task.h>
3e93cd671   Al Viro   Take fs_struct ha...
4
5
6
  #include <linux/fs.h>
  #include <linux/path.h>
  #include <linux/slab.h>
5ad4e53bd   Al Viro   Get rid of indire...
7
  #include <linux/fs_struct.h>
f03c65993   Al Viro   sanitize vfsmount...
8
  #include "internal.h"
3e93cd671   Al Viro   Take fs_struct ha...
9
10
11
12
  /*
   * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
   * It can block.
   */
dcf787f39   Al Viro   constify path_get...
13
  void set_fs_root(struct fs_struct *fs, const struct path *path)
3e93cd671   Al Viro   Take fs_struct ha...
14
15
  {
  	struct path old_root;
f7a99c5b7   Al Viro   get rid of ->mnt_...
16
  	path_get(path);
2a4419b5b   Nick Piggin   fs: fs_struct rwl...
17
  	spin_lock(&fs->lock);
c28cc3646   Nick Piggin   fs: fs_struct use...
18
  	write_seqcount_begin(&fs->seq);
3e93cd671   Al Viro   Take fs_struct ha...
19
20
  	old_root = fs->root;
  	fs->root = *path;
c28cc3646   Nick Piggin   fs: fs_struct use...
21
  	write_seqcount_end(&fs->seq);
2a4419b5b   Nick Piggin   fs: fs_struct rwl...
22
  	spin_unlock(&fs->lock);
3e93cd671   Al Viro   Take fs_struct ha...
23
  	if (old_root.dentry)
f7a99c5b7   Al Viro   get rid of ->mnt_...
24
  		path_put(&old_root);
3e93cd671   Al Viro   Take fs_struct ha...
25
26
27
28
29
30
  }
  
  /*
   * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
   * It can block.
   */
dcf787f39   Al Viro   constify path_get...
31
  void set_fs_pwd(struct fs_struct *fs, const struct path *path)
3e93cd671   Al Viro   Take fs_struct ha...
32
33
  {
  	struct path old_pwd;
f7a99c5b7   Al Viro   get rid of ->mnt_...
34
  	path_get(path);
2a4419b5b   Nick Piggin   fs: fs_struct rwl...
35
  	spin_lock(&fs->lock);
c28cc3646   Nick Piggin   fs: fs_struct use...
36
  	write_seqcount_begin(&fs->seq);
3e93cd671   Al Viro   Take fs_struct ha...
37
38
  	old_pwd = fs->pwd;
  	fs->pwd = *path;
c28cc3646   Nick Piggin   fs: fs_struct use...
39
  	write_seqcount_end(&fs->seq);
2a4419b5b   Nick Piggin   fs: fs_struct rwl...
40
  	spin_unlock(&fs->lock);
3e93cd671   Al Viro   Take fs_struct ha...
41
42
  
  	if (old_pwd.dentry)
f7a99c5b7   Al Viro   get rid of ->mnt_...
43
  		path_put(&old_pwd);
3e93cd671   Al Viro   Take fs_struct ha...
44
  }
82234e61a   Al Viro   vfs: take path_ge...
45
46
47
48
49
50
51
  static inline int replace_path(struct path *p, const struct path *old, const struct path *new)
  {
  	if (likely(p->dentry != old->dentry || p->mnt != old->mnt))
  		return 0;
  	*p = *new;
  	return 1;
  }
dcf787f39   Al Viro   constify path_get...
52
  void chroot_fs_refs(const struct path *old_root, const struct path *new_root)
3e93cd671   Al Viro   Take fs_struct ha...
53
54
55
56
57
58
59
60
61
62
  {
  	struct task_struct *g, *p;
  	struct fs_struct *fs;
  	int count = 0;
  
  	read_lock(&tasklist_lock);
  	do_each_thread(g, p) {
  		task_lock(p);
  		fs = p->fs;
  		if (fs) {
82234e61a   Al Viro   vfs: take path_ge...
63
  			int hits = 0;
2a4419b5b   Nick Piggin   fs: fs_struct rwl...
64
  			spin_lock(&fs->lock);
c28cc3646   Nick Piggin   fs: fs_struct use...
65
  			write_seqcount_begin(&fs->seq);
82234e61a   Al Viro   vfs: take path_ge...
66
67
68
69
  			hits += replace_path(&fs->root, old_root, new_root);
  			hits += replace_path(&fs->pwd, old_root, new_root);
  			write_seqcount_end(&fs->seq);
  			while (hits--) {
3e93cd671   Al Viro   Take fs_struct ha...
70
  				count++;
f7a99c5b7   Al Viro   get rid of ->mnt_...
71
  				path_get(new_root);
3e93cd671   Al Viro   Take fs_struct ha...
72
  			}
2a4419b5b   Nick Piggin   fs: fs_struct rwl...
73
  			spin_unlock(&fs->lock);
3e93cd671   Al Viro   Take fs_struct ha...
74
75
76
77
78
  		}
  		task_unlock(p);
  	} while_each_thread(g, p);
  	read_unlock(&tasklist_lock);
  	while (count--)
f7a99c5b7   Al Viro   get rid of ->mnt_...
79
  		path_put(old_root);
3e93cd671   Al Viro   Take fs_struct ha...
80
  }
498052bba   Al Viro   New locking/refco...
81
  void free_fs_struct(struct fs_struct *fs)
3e93cd671   Al Viro   Take fs_struct ha...
82
  {
f7a99c5b7   Al Viro   get rid of ->mnt_...
83
84
  	path_put(&fs->root);
  	path_put(&fs->pwd);
498052bba   Al Viro   New locking/refco...
85
  	kmem_cache_free(fs_cachep, fs);
3e93cd671   Al Viro   Take fs_struct ha...
86
87
88
89
  }
  
  void exit_fs(struct task_struct *tsk)
  {
498052bba   Al Viro   New locking/refco...
90
  	struct fs_struct *fs = tsk->fs;
3e93cd671   Al Viro   Take fs_struct ha...
91
92
  
  	if (fs) {
498052bba   Al Viro   New locking/refco...
93
  		int kill;
3e93cd671   Al Viro   Take fs_struct ha...
94
  		task_lock(tsk);
2a4419b5b   Nick Piggin   fs: fs_struct rwl...
95
  		spin_lock(&fs->lock);
3e93cd671   Al Viro   Take fs_struct ha...
96
  		tsk->fs = NULL;
498052bba   Al Viro   New locking/refco...
97
  		kill = !--fs->users;
2a4419b5b   Nick Piggin   fs: fs_struct rwl...
98
  		spin_unlock(&fs->lock);
3e93cd671   Al Viro   Take fs_struct ha...
99
  		task_unlock(tsk);
498052bba   Al Viro   New locking/refco...
100
101
  		if (kill)
  			free_fs_struct(fs);
3e93cd671   Al Viro   Take fs_struct ha...
102
103
104
105
106
107
108
109
  	}
  }
  
  struct fs_struct *copy_fs_struct(struct fs_struct *old)
  {
  	struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
  	/* We don't need to lock fs - think why ;-) */
  	if (fs) {
498052bba   Al Viro   New locking/refco...
110
111
  		fs->users = 1;
  		fs->in_exec = 0;
2a4419b5b   Nick Piggin   fs: fs_struct rwl...
112
  		spin_lock_init(&fs->lock);
c28cc3646   Nick Piggin   fs: fs_struct use...
113
  		seqcount_init(&fs->seq);
3e93cd671   Al Viro   Take fs_struct ha...
114
  		fs->umask = old->umask;
b3e19d924   Nick Piggin   fs: scale mntget/...
115
116
117
  
  		spin_lock(&old->lock);
  		fs->root = old->root;
f7a99c5b7   Al Viro   get rid of ->mnt_...
118
  		path_get(&fs->root);
b3e19d924   Nick Piggin   fs: scale mntget/...
119
  		fs->pwd = old->pwd;
f7a99c5b7   Al Viro   get rid of ->mnt_...
120
  		path_get(&fs->pwd);
b3e19d924   Nick Piggin   fs: scale mntget/...
121
  		spin_unlock(&old->lock);
3e93cd671   Al Viro   Take fs_struct ha...
122
123
124
125
126
127
  	}
  	return fs;
  }
  
  int unshare_fs_struct(void)
  {
498052bba   Al Viro   New locking/refco...
128
129
130
131
132
  	struct fs_struct *fs = current->fs;
  	struct fs_struct *new_fs = copy_fs_struct(fs);
  	int kill;
  
  	if (!new_fs)
3e93cd671   Al Viro   Take fs_struct ha...
133
  		return -ENOMEM;
498052bba   Al Viro   New locking/refco...
134
135
  
  	task_lock(current);
2a4419b5b   Nick Piggin   fs: fs_struct rwl...
136
  	spin_lock(&fs->lock);
498052bba   Al Viro   New locking/refco...
137
138
  	kill = !--fs->users;
  	current->fs = new_fs;
2a4419b5b   Nick Piggin   fs: fs_struct rwl...
139
  	spin_unlock(&fs->lock);
498052bba   Al Viro   New locking/refco...
140
141
142
143
  	task_unlock(current);
  
  	if (kill)
  		free_fs_struct(fs);
3e93cd671   Al Viro   Take fs_struct ha...
144
145
146
  	return 0;
  }
  EXPORT_SYMBOL_GPL(unshare_fs_struct);
ce3b0f8d5   Al Viro   New helper - curr...
147
148
149
150
151
  int current_umask(void)
  {
  	return current->fs->umask;
  }
  EXPORT_SYMBOL(current_umask);
3e93cd671   Al Viro   Take fs_struct ha...
152
153
  /* to be mentioned only in INIT_TASK */
  struct fs_struct init_fs = {
498052bba   Al Viro   New locking/refco...
154
  	.users		= 1,
2a4419b5b   Nick Piggin   fs: fs_struct rwl...
155
  	.lock		= __SPIN_LOCK_UNLOCKED(init_fs.lock),
1ca7d67cf   John Stultz   seqcount: Add loc...
156
  	.seq		= SEQCNT_ZERO(init_fs.seq),
3e93cd671   Al Viro   Take fs_struct ha...
157
158
  	.umask		= 0022,
  };