1 #include <linux/module.h>
2 #include <linux/sched.h>
4 #include <linux/path.h>
5 #include <linux/slab.h>
6 #include <linux/fs_struct.h>
9 static inline void path_get_longterm(struct path
*path
)
12 mnt_make_longterm(path
->mnt
);
15 static inline void path_put_longterm(struct path
*path
)
17 mnt_make_shortterm(path
->mnt
);
22 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
25 void set_fs_root(struct fs_struct
*fs
, struct path
*path
)
30 write_seqcount_begin(&fs
->seq
);
33 path_get_longterm(path
);
34 write_seqcount_end(&fs
->seq
);
35 spin_unlock(&fs
->lock
);
37 path_put_longterm(&old_root
);
41 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
44 void set_fs_pwd(struct fs_struct
*fs
, struct path
*path
)
49 write_seqcount_begin(&fs
->seq
);
52 path_get_longterm(path
);
53 write_seqcount_end(&fs
->seq
);
54 spin_unlock(&fs
->lock
);
57 path_put_longterm(&old_pwd
);
60 void chroot_fs_refs(struct path
*old_root
, struct path
*new_root
)
62 struct task_struct
*g
, *p
;
66 read_lock(&tasklist_lock
);
67 do_each_thread(g
, p
) {
72 write_seqcount_begin(&fs
->seq
);
73 if (fs
->root
.dentry
== old_root
->dentry
74 && fs
->root
.mnt
== old_root
->mnt
) {
75 path_get_longterm(new_root
);
79 if (fs
->pwd
.dentry
== old_root
->dentry
80 && fs
->pwd
.mnt
== old_root
->mnt
) {
81 path_get_longterm(new_root
);
85 write_seqcount_end(&fs
->seq
);
86 spin_unlock(&fs
->lock
);
89 } while_each_thread(g
, p
);
90 read_unlock(&tasklist_lock
);
92 path_put_longterm(old_root
);
95 void free_fs_struct(struct fs_struct
*fs
)
97 path_put_longterm(&fs
->root
);
98 path_put_longterm(&fs
->pwd
);
99 kmem_cache_free(fs_cachep
, fs
);
102 void exit_fs(struct task_struct
*tsk
)
104 struct fs_struct
*fs
= tsk
->fs
;
109 spin_lock(&fs
->lock
);
110 write_seqcount_begin(&fs
->seq
);
113 write_seqcount_end(&fs
->seq
);
114 spin_unlock(&fs
->lock
);
121 struct fs_struct
*copy_fs_struct(struct fs_struct
*old
)
123 struct fs_struct
*fs
= kmem_cache_alloc(fs_cachep
, GFP_KERNEL
);
124 /* We don't need to lock fs - think why ;-) */
128 spin_lock_init(&fs
->lock
);
129 seqcount_init(&fs
->seq
);
130 fs
->umask
= old
->umask
;
132 spin_lock(&old
->lock
);
133 fs
->root
= old
->root
;
134 path_get_longterm(&fs
->root
);
136 path_get_longterm(&fs
->pwd
);
137 spin_unlock(&old
->lock
);
142 int unshare_fs_struct(void)
144 struct fs_struct
*fs
= current
->fs
;
145 struct fs_struct
*new_fs
= copy_fs_struct(fs
);
152 spin_lock(&fs
->lock
);
154 current
->fs
= new_fs
;
155 spin_unlock(&fs
->lock
);
156 task_unlock(current
);
163 EXPORT_SYMBOL_GPL(unshare_fs_struct
);
165 int current_umask(void)
167 return current
->fs
->umask
;
169 EXPORT_SYMBOL(current_umask
);
171 /* to be mentioned only in INIT_TASK */
172 struct fs_struct init_fs
= {
174 .lock
= __SPIN_LOCK_UNLOCKED(init_fs
.lock
),
179 void daemonize_fs_struct(void)
181 struct fs_struct
*fs
= current
->fs
;
188 spin_lock(&init_fs
.lock
);
190 spin_unlock(&init_fs
.lock
);
192 spin_lock(&fs
->lock
);
193 current
->fs
= &init_fs
;
195 spin_unlock(&fs
->lock
);
197 task_unlock(current
);