fs_struct.c 3.2 KB
Newer Older
1 2 3 4 5
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/path.h>
#include <linux/slab.h>
6
#include <linux/fs_struct.h>
7 8 9 10 11 12 13 14 15

/*
 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
 * It can block.
 */
void set_fs_root(struct fs_struct *fs, struct path *path)
{
	struct path old_root;

N
Nick Piggin 已提交
16
	spin_lock(&fs->lock);
17 18 19
	old_root = fs->root;
	fs->root = *path;
	path_get(path);
N
Nick Piggin 已提交
20
	spin_unlock(&fs->lock);
21 22 23 24 25 26 27 28 29 30 31 32
	if (old_root.dentry)
		path_put(&old_root);
}

/*
 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
 * It can block.
 */
void set_fs_pwd(struct fs_struct *fs, struct path *path)
{
	struct path old_pwd;

N
Nick Piggin 已提交
33
	spin_lock(&fs->lock);
34 35 36
	old_pwd = fs->pwd;
	fs->pwd = *path;
	path_get(path);
N
Nick Piggin 已提交
37
	spin_unlock(&fs->lock);
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

	if (old_pwd.dentry)
		path_put(&old_pwd);
}

void chroot_fs_refs(struct path *old_root, struct path *new_root)
{
	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) {
N
Nick Piggin 已提交
54
			spin_lock(&fs->lock);
55 56 57 58 59 60 61 62 63 64 65 66
			if (fs->root.dentry == old_root->dentry
			    && fs->root.mnt == old_root->mnt) {
				path_get(new_root);
				fs->root = *new_root;
				count++;
			}
			if (fs->pwd.dentry == old_root->dentry
			    && fs->pwd.mnt == old_root->mnt) {
				path_get(new_root);
				fs->pwd = *new_root;
				count++;
			}
N
Nick Piggin 已提交
67
			spin_unlock(&fs->lock);
68 69 70 71 72 73 74 75
		}
		task_unlock(p);
	} while_each_thread(g, p);
	read_unlock(&tasklist_lock);
	while (count--)
		path_put(old_root);
}

A
Al Viro 已提交
76
void free_fs_struct(struct fs_struct *fs)
77
{
A
Al Viro 已提交
78 79 80
	path_put(&fs->root);
	path_put(&fs->pwd);
	kmem_cache_free(fs_cachep, fs);
81 82 83 84
}

void exit_fs(struct task_struct *tsk)
{
A
Al Viro 已提交
85
	struct fs_struct *fs = tsk->fs;
86 87

	if (fs) {
A
Al Viro 已提交
88
		int kill;
89
		task_lock(tsk);
N
Nick Piggin 已提交
90
		spin_lock(&fs->lock);
91
		tsk->fs = NULL;
A
Al Viro 已提交
92
		kill = !--fs->users;
N
Nick Piggin 已提交
93
		spin_unlock(&fs->lock);
94
		task_unlock(tsk);
A
Al Viro 已提交
95 96
		if (kill)
			free_fs_struct(fs);
97 98 99 100 101 102 103 104
	}
}

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) {
A
Al Viro 已提交
105 106
		fs->users = 1;
		fs->in_exec = 0;
N
Nick Piggin 已提交
107
		spin_lock_init(&fs->lock);
108
		fs->umask = old->umask;
109
		get_fs_root_and_pwd(old, &fs->root, &fs->pwd);
110 111 112 113 114 115
	}
	return fs;
}

int unshare_fs_struct(void)
{
A
Al Viro 已提交
116 117 118 119 120
	struct fs_struct *fs = current->fs;
	struct fs_struct *new_fs = copy_fs_struct(fs);
	int kill;

	if (!new_fs)
121
		return -ENOMEM;
A
Al Viro 已提交
122 123

	task_lock(current);
N
Nick Piggin 已提交
124
	spin_lock(&fs->lock);
A
Al Viro 已提交
125 126
	kill = !--fs->users;
	current->fs = new_fs;
N
Nick Piggin 已提交
127
	spin_unlock(&fs->lock);
A
Al Viro 已提交
128 129 130 131 132
	task_unlock(current);

	if (kill)
		free_fs_struct(fs);

133 134 135 136
	return 0;
}
EXPORT_SYMBOL_GPL(unshare_fs_struct);

A
Al Viro 已提交
137 138 139 140 141 142
int current_umask(void)
{
	return current->fs->umask;
}
EXPORT_SYMBOL(current_umask);

143 144
/* to be mentioned only in INIT_TASK */
struct fs_struct init_fs = {
A
Al Viro 已提交
145
	.users		= 1,
N
Nick Piggin 已提交
146
	.lock		= __SPIN_LOCK_UNLOCKED(init_fs.lock),
147 148 149 150 151
	.umask		= 0022,
};

void daemonize_fs_struct(void)
{
A
Al Viro 已提交
152 153 154 155 156 157
	struct fs_struct *fs = current->fs;

	if (fs) {
		int kill;

		task_lock(current);
158

N
Nick Piggin 已提交
159
		spin_lock(&init_fs.lock);
A
Al Viro 已提交
160
		init_fs.users++;
N
Nick Piggin 已提交
161
		spin_unlock(&init_fs.lock);
A
Al Viro 已提交
162

N
Nick Piggin 已提交
163
		spin_lock(&fs->lock);
A
Al Viro 已提交
164 165
		current->fs = &init_fs;
		kill = !--fs->users;
N
Nick Piggin 已提交
166
		spin_unlock(&fs->lock);
A
Al Viro 已提交
167 168 169 170 171

		task_unlock(current);
		if (kill)
			free_fs_struct(fs);
	}
172
}