1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/export.h>
3 #include <linux/sched/signal.h>
4 #include <linux/sched/task.h>
6 #include <linux/path.h>
7 #include <linux/slab.h>
8 #include <linux/fs_struct.h>
12 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
15 void set_fs_root(struct fs_struct
*fs
, const struct path
*path
)
21 write_seqcount_begin(&fs
->seq
);
24 write_seqcount_end(&fs
->seq
);
25 spin_unlock(&fs
->lock
);
31 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
34 void set_fs_pwd(struct fs_struct
*fs
, const struct path
*path
)
40 write_seqcount_begin(&fs
->seq
);
43 write_seqcount_end(&fs
->seq
);
44 spin_unlock(&fs
->lock
);
50 static inline int replace_path(struct path
*p
, const struct path
*old
, const struct path
*new)
52 if (likely(p
->dentry
!= old
->dentry
|| p
->mnt
!= old
->mnt
))
58 void chroot_fs_refs(const struct path
*old_root
, const struct path
*new_root
)
60 struct task_struct
*g
, *p
;
64 read_lock(&tasklist_lock
);
65 do_each_thread(g
, p
) {
71 write_seqcount_begin(&fs
->seq
);
72 hits
+= replace_path(&fs
->root
, old_root
, new_root
);
73 hits
+= replace_path(&fs
->pwd
, old_root
, new_root
);
74 write_seqcount_end(&fs
->seq
);
79 spin_unlock(&fs
->lock
);
82 } while_each_thread(g
, p
);
83 read_unlock(&tasklist_lock
);
88 void free_fs_struct(struct fs_struct
*fs
)
92 kmem_cache_free(fs_cachep
, fs
);
95 void exit_fs(struct task_struct
*tsk
)
97 struct fs_struct
*fs
= tsk
->fs
;
102 spin_lock(&fs
->lock
);
105 spin_unlock(&fs
->lock
);
112 struct fs_struct
*copy_fs_struct(struct fs_struct
*old
)
114 struct fs_struct
*fs
= kmem_cache_alloc(fs_cachep
, GFP_KERNEL
);
115 /* We don't need to lock fs - think why ;-) */
119 spin_lock_init(&fs
->lock
);
120 seqcount_init(&fs
->seq
);
121 fs
->umask
= old
->umask
;
123 spin_lock(&old
->lock
);
124 fs
->root
= old
->root
;
128 spin_unlock(&old
->lock
);
133 int unshare_fs_struct(void)
135 struct fs_struct
*fs
= current
->fs
;
136 struct fs_struct
*new_fs
= copy_fs_struct(fs
);
143 spin_lock(&fs
->lock
);
145 current
->fs
= new_fs
;
146 spin_unlock(&fs
->lock
);
147 task_unlock(current
);
154 EXPORT_SYMBOL_GPL(unshare_fs_struct
);
156 int current_umask(void)
158 return current
->fs
->umask
;
160 EXPORT_SYMBOL(current_umask
);
162 /* to be mentioned only in INIT_TASK */
163 struct fs_struct init_fs
= {
165 .lock
= __SPIN_LOCK_UNLOCKED(init_fs
.lock
),
166 .seq
= SEQCNT_ZERO(init_fs
.seq
),