Linux Audio

Check our new training course

Loading...
v4.10.11
  1#include <linux/export.h>
  2#include <linux/sched.h>
  3#include <linux/fs.h>
  4#include <linux/path.h>
  5#include <linux/slab.h>
  6#include <linux/fs_struct.h>
  7#include "internal.h"
  8
 
 
 
 
 
 
 
 
 
 
 
 
  9/*
 10 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
 11 * It can block.
 12 */
 13void set_fs_root(struct fs_struct *fs, const struct path *path)
 14{
 15	struct path old_root;
 16
 17	path_get(path);
 18	spin_lock(&fs->lock);
 19	write_seqcount_begin(&fs->seq);
 20	old_root = fs->root;
 21	fs->root = *path;
 22	write_seqcount_end(&fs->seq);
 23	spin_unlock(&fs->lock);
 24	if (old_root.dentry)
 25		path_put(&old_root);
 26}
 27
 28/*
 29 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
 30 * It can block.
 31 */
 32void set_fs_pwd(struct fs_struct *fs, const struct path *path)
 33{
 34	struct path old_pwd;
 35
 36	path_get(path);
 37	spin_lock(&fs->lock);
 38	write_seqcount_begin(&fs->seq);
 39	old_pwd = fs->pwd;
 40	fs->pwd = *path;
 41	write_seqcount_end(&fs->seq);
 42	spin_unlock(&fs->lock);
 43
 44	if (old_pwd.dentry)
 45		path_put(&old_pwd);
 46}
 47
 48static inline int replace_path(struct path *p, const struct path *old, const struct path *new)
 49{
 50	if (likely(p->dentry != old->dentry || p->mnt != old->mnt))
 51		return 0;
 52	*p = *new;
 53	return 1;
 54}
 55
 56void chroot_fs_refs(const struct path *old_root, const struct path *new_root)
 57{
 58	struct task_struct *g, *p;
 59	struct fs_struct *fs;
 60	int count = 0;
 61
 62	read_lock(&tasklist_lock);
 63	do_each_thread(g, p) {
 64		task_lock(p);
 65		fs = p->fs;
 66		if (fs) {
 67			int hits = 0;
 68			spin_lock(&fs->lock);
 69			write_seqcount_begin(&fs->seq);
 70			hits += replace_path(&fs->root, old_root, new_root);
 71			hits += replace_path(&fs->pwd, old_root, new_root);
 72			write_seqcount_end(&fs->seq);
 73			while (hits--) {
 74				count++;
 75				path_get(new_root);
 76			}
 77			spin_unlock(&fs->lock);
 78		}
 79		task_unlock(p);
 80	} while_each_thread(g, p);
 81	read_unlock(&tasklist_lock);
 82	while (count--)
 83		path_put(old_root);
 84}
 85
 86void free_fs_struct(struct fs_struct *fs)
 87{
 88	path_put(&fs->root);
 89	path_put(&fs->pwd);
 90	kmem_cache_free(fs_cachep, fs);
 91}
 92
 93void exit_fs(struct task_struct *tsk)
 94{
 95	struct fs_struct *fs = tsk->fs;
 96
 97	if (fs) {
 98		int kill;
 99		task_lock(tsk);
100		spin_lock(&fs->lock);
101		tsk->fs = NULL;
102		kill = !--fs->users;
103		spin_unlock(&fs->lock);
104		task_unlock(tsk);
105		if (kill)
106			free_fs_struct(fs);
107	}
108}
109
110struct fs_struct *copy_fs_struct(struct fs_struct *old)
111{
112	struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
113	/* We don't need to lock fs - think why ;-) */
114	if (fs) {
115		fs->users = 1;
116		fs->in_exec = 0;
117		spin_lock_init(&fs->lock);
118		seqcount_init(&fs->seq);
119		fs->umask = old->umask;
120
121		spin_lock(&old->lock);
122		fs->root = old->root;
123		path_get(&fs->root);
124		fs->pwd = old->pwd;
125		path_get(&fs->pwd);
126		spin_unlock(&old->lock);
127	}
128	return fs;
129}
130
131int unshare_fs_struct(void)
132{
133	struct fs_struct *fs = current->fs;
134	struct fs_struct *new_fs = copy_fs_struct(fs);
135	int kill;
136
137	if (!new_fs)
138		return -ENOMEM;
139
140	task_lock(current);
141	spin_lock(&fs->lock);
142	kill = !--fs->users;
143	current->fs = new_fs;
144	spin_unlock(&fs->lock);
145	task_unlock(current);
146
147	if (kill)
148		free_fs_struct(fs);
149
150	return 0;
151}
152EXPORT_SYMBOL_GPL(unshare_fs_struct);
153
154int current_umask(void)
155{
156	return current->fs->umask;
157}
158EXPORT_SYMBOL(current_umask);
159
160/* to be mentioned only in INIT_TASK */
161struct fs_struct init_fs = {
162	.users		= 1,
163	.lock		= __SPIN_LOCK_UNLOCKED(init_fs.lock),
164	.seq		= SEQCNT_ZERO(init_fs.seq),
165	.umask		= 0022,
166};
v3.5.6
  1#include <linux/export.h>
  2#include <linux/sched.h>
  3#include <linux/fs.h>
  4#include <linux/path.h>
  5#include <linux/slab.h>
  6#include <linux/fs_struct.h>
  7#include "internal.h"
  8
  9static inline void path_get_longterm(struct path *path)
 10{
 11	path_get(path);
 12	mnt_make_longterm(path->mnt);
 13}
 14
 15static inline void path_put_longterm(struct path *path)
 16{
 17	mnt_make_shortterm(path->mnt);
 18	path_put(path);
 19}
 20
 21/*
 22 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
 23 * It can block.
 24 */
 25void set_fs_root(struct fs_struct *fs, struct path *path)
 26{
 27	struct path old_root;
 28
 29	path_get_longterm(path);
 30	spin_lock(&fs->lock);
 31	write_seqcount_begin(&fs->seq);
 32	old_root = fs->root;
 33	fs->root = *path;
 34	write_seqcount_end(&fs->seq);
 35	spin_unlock(&fs->lock);
 36	if (old_root.dentry)
 37		path_put_longterm(&old_root);
 38}
 39
 40/*
 41 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
 42 * It can block.
 43 */
 44void set_fs_pwd(struct fs_struct *fs, struct path *path)
 45{
 46	struct path old_pwd;
 47
 48	path_get_longterm(path);
 49	spin_lock(&fs->lock);
 50	write_seqcount_begin(&fs->seq);
 51	old_pwd = fs->pwd;
 52	fs->pwd = *path;
 53	write_seqcount_end(&fs->seq);
 54	spin_unlock(&fs->lock);
 55
 56	if (old_pwd.dentry)
 57		path_put_longterm(&old_pwd);
 58}
 59
 60static inline int replace_path(struct path *p, const struct path *old, const struct path *new)
 61{
 62	if (likely(p->dentry != old->dentry || p->mnt != old->mnt))
 63		return 0;
 64	*p = *new;
 65	return 1;
 66}
 67
 68void chroot_fs_refs(struct path *old_root, struct path *new_root)
 69{
 70	struct task_struct *g, *p;
 71	struct fs_struct *fs;
 72	int count = 0;
 73
 74	read_lock(&tasklist_lock);
 75	do_each_thread(g, p) {
 76		task_lock(p);
 77		fs = p->fs;
 78		if (fs) {
 79			int hits = 0;
 80			spin_lock(&fs->lock);
 81			write_seqcount_begin(&fs->seq);
 82			hits += replace_path(&fs->root, old_root, new_root);
 83			hits += replace_path(&fs->pwd, old_root, new_root);
 84			write_seqcount_end(&fs->seq);
 85			while (hits--) {
 86				count++;
 87				path_get_longterm(new_root);
 88			}
 89			spin_unlock(&fs->lock);
 90		}
 91		task_unlock(p);
 92	} while_each_thread(g, p);
 93	read_unlock(&tasklist_lock);
 94	while (count--)
 95		path_put_longterm(old_root);
 96}
 97
 98void free_fs_struct(struct fs_struct *fs)
 99{
100	path_put_longterm(&fs->root);
101	path_put_longterm(&fs->pwd);
102	kmem_cache_free(fs_cachep, fs);
103}
104
105void exit_fs(struct task_struct *tsk)
106{
107	struct fs_struct *fs = tsk->fs;
108
109	if (fs) {
110		int kill;
111		task_lock(tsk);
112		spin_lock(&fs->lock);
113		tsk->fs = NULL;
114		kill = !--fs->users;
115		spin_unlock(&fs->lock);
116		task_unlock(tsk);
117		if (kill)
118			free_fs_struct(fs);
119	}
120}
121
122struct fs_struct *copy_fs_struct(struct fs_struct *old)
123{
124	struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
125	/* We don't need to lock fs - think why ;-) */
126	if (fs) {
127		fs->users = 1;
128		fs->in_exec = 0;
129		spin_lock_init(&fs->lock);
130		seqcount_init(&fs->seq);
131		fs->umask = old->umask;
132
133		spin_lock(&old->lock);
134		fs->root = old->root;
135		path_get_longterm(&fs->root);
136		fs->pwd = old->pwd;
137		path_get_longterm(&fs->pwd);
138		spin_unlock(&old->lock);
139	}
140	return fs;
141}
142
143int unshare_fs_struct(void)
144{
145	struct fs_struct *fs = current->fs;
146	struct fs_struct *new_fs = copy_fs_struct(fs);
147	int kill;
148
149	if (!new_fs)
150		return -ENOMEM;
151
152	task_lock(current);
153	spin_lock(&fs->lock);
154	kill = !--fs->users;
155	current->fs = new_fs;
156	spin_unlock(&fs->lock);
157	task_unlock(current);
158
159	if (kill)
160		free_fs_struct(fs);
161
162	return 0;
163}
164EXPORT_SYMBOL_GPL(unshare_fs_struct);
165
166int current_umask(void)
167{
168	return current->fs->umask;
169}
170EXPORT_SYMBOL(current_umask);
171
172/* to be mentioned only in INIT_TASK */
173struct fs_struct init_fs = {
174	.users		= 1,
175	.lock		= __SPIN_LOCK_UNLOCKED(init_fs.lock),
176	.seq		= SEQCNT_ZERO,
177	.umask		= 0022,
178};
179
180void daemonize_fs_struct(void)
181{
182	struct fs_struct *fs = current->fs;
183
184	if (fs) {
185		int kill;
186
187		task_lock(current);
188
189		spin_lock(&init_fs.lock);
190		init_fs.users++;
191		spin_unlock(&init_fs.lock);
192
193		spin_lock(&fs->lock);
194		current->fs = &init_fs;
195		kill = !--fs->users;
196		spin_unlock(&fs->lock);
197
198		task_unlock(current);
199		if (kill)
200			free_fs_struct(fs);
201	}
202}