Blame view

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