1 #include <linux/export.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
)
29 path_get_longterm(path
);
31 write_seqcount_begin(&fs
->seq
);
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
)
48 path_get_longterm(path
);
50 write_seqcount_begin(&fs
->seq
);
53 write_seqcount_end(&fs
->seq
);
54 spin_unlock(&fs
->lock
);
57 path_put_longterm(&old_pwd
);
60 static inline int replace_path(struct path
*p
, const struct path
*old
, const struct path
*new)
62 if (likely(p
->dentry
!= old
->dentry
|| p
->mnt
!= old
->mnt
))
68 void chroot_fs_refs(struct path
*old_root
, struct path
*new_root
)
70 struct task_struct
*g
, *p
;
74 read_lock(&tasklist_lock
);
75 do_each_thread(g
, p
) {
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
);
87 path_get_longterm(new_root
);
89 spin_unlock(&fs
->lock
);
92 } while_each_thread(g
, p
);
93 read_unlock(&tasklist_lock
);
95 path_put_longterm(old_root
);
98 void free_fs_struct(struct fs_struct
*fs
)
100 path_put_longterm(&fs
->root
);
101 path_put_longterm(&fs
->pwd
);
102 kmem_cache_free(fs_cachep
, fs
);
105 void exit_fs(struct task_struct
*tsk
)
107 struct fs_struct
*fs
= tsk
->fs
;
112 spin_lock(&fs
->lock
);
115 spin_unlock(&fs
->lock
);
122 struct fs_struct
*copy_fs_struct(struct fs_struct
*old
)
124 struct fs_struct
*fs
= kmem_cache_alloc(fs_cachep
, GFP_KERNEL
);
125 /* We don't need to lock fs - think why ;-) */
129 spin_lock_init(&fs
->lock
);
130 seqcount_init(&fs
->seq
);
131 fs
->umask
= old
->umask
;
133 spin_lock(&old
->lock
);
134 fs
->root
= old
->root
;
135 path_get_longterm(&fs
->root
);
137 path_get_longterm(&fs
->pwd
);
138 spin_unlock(&old
->lock
);
143 int unshare_fs_struct(void)
145 struct fs_struct
*fs
= current
->fs
;
146 struct fs_struct
*new_fs
= copy_fs_struct(fs
);
153 spin_lock(&fs
->lock
);
155 current
->fs
= new_fs
;
156 spin_unlock(&fs
->lock
);
157 task_unlock(current
);
164 EXPORT_SYMBOL_GPL(unshare_fs_struct
);
166 int current_umask(void)
168 return current
->fs
->umask
;
170 EXPORT_SYMBOL(current_umask
);
172 /* to be mentioned only in INIT_TASK */
173 struct fs_struct init_fs
= {
175 .lock
= __SPIN_LOCK_UNLOCKED(init_fs
.lock
),
180 void daemonize_fs_struct(void)
182 struct fs_struct
*fs
= current
->fs
;
189 spin_lock(&init_fs
.lock
);
191 spin_unlock(&init_fs
.lock
);
193 spin_lock(&fs
->lock
);
194 current
->fs
= &init_fs
;
196 spin_unlock(&fs
->lock
);
198 task_unlock(current
);