1 #include <linux/export.h>
2 #include <linux/sched/signal.h>
3 #include <linux/sched/task.h>
5 #include <linux/path.h>
6 #include <linux/slab.h>
7 #include <linux/fs_struct.h>
11 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
14 void set_fs_root(struct fs_struct
*fs
, const struct path
*path
)
20 write_seqcount_begin(&fs
->seq
);
23 write_seqcount_end(&fs
->seq
);
24 spin_unlock(&fs
->lock
);
30 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
33 void set_fs_pwd(struct fs_struct
*fs
, const struct path
*path
)
39 write_seqcount_begin(&fs
->seq
);
42 write_seqcount_end(&fs
->seq
);
43 spin_unlock(&fs
->lock
);
49 static inline int replace_path(struct path
*p
, const struct path
*old
, const struct path
*new)
51 if (likely(p
->dentry
!= old
->dentry
|| p
->mnt
!= old
->mnt
))
57 void chroot_fs_refs(const struct path
*old_root
, const struct path
*new_root
)
59 struct task_struct
*g
, *p
;
63 read_lock(&tasklist_lock
);
64 do_each_thread(g
, p
) {
70 write_seqcount_begin(&fs
->seq
);
71 hits
+= replace_path(&fs
->root
, old_root
, new_root
);
72 hits
+= replace_path(&fs
->pwd
, old_root
, new_root
);
73 write_seqcount_end(&fs
->seq
);
78 spin_unlock(&fs
->lock
);
81 } while_each_thread(g
, p
);
82 read_unlock(&tasklist_lock
);
87 void free_fs_struct(struct fs_struct
*fs
)
91 kmem_cache_free(fs_cachep
, fs
);
94 void exit_fs(struct task_struct
*tsk
)
96 struct fs_struct
*fs
= tsk
->fs
;
101 spin_lock(&fs
->lock
);
104 spin_unlock(&fs
->lock
);
111 struct fs_struct
*copy_fs_struct(struct fs_struct
*old
)
113 struct fs_struct
*fs
= kmem_cache_alloc(fs_cachep
, GFP_KERNEL
);
114 /* We don't need to lock fs - think why ;-) */
118 spin_lock_init(&fs
->lock
);
119 seqcount_init(&fs
->seq
);
120 fs
->umask
= old
->umask
;
122 spin_lock(&old
->lock
);
123 fs
->root
= old
->root
;
127 spin_unlock(&old
->lock
);
132 int unshare_fs_struct(void)
134 struct fs_struct
*fs
= current
->fs
;
135 struct fs_struct
*new_fs
= copy_fs_struct(fs
);
142 spin_lock(&fs
->lock
);
144 current
->fs
= new_fs
;
145 spin_unlock(&fs
->lock
);
146 task_unlock(current
);
153 EXPORT_SYMBOL_GPL(unshare_fs_struct
);
155 int current_umask(void)
157 return current
->fs
->umask
;
159 EXPORT_SYMBOL(current_umask
);
161 /* to be mentioned only in INIT_TASK */
162 struct fs_struct init_fs
= {
164 .lock
= __SPIN_LOCK_UNLOCKED(init_fs
.lock
),
165 .seq
= SEQCNT_ZERO(init_fs
.seq
),