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
);
48 EXPORT_SYMBOL(set_fs_pwd
);
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
);
94 EXPORT_SYMBOL(free_fs_struct
);
96 void exit_fs(struct task_struct
*tsk
)
98 struct fs_struct
*fs
= tsk
->fs
;
103 spin_lock(&fs
->lock
);
106 spin_unlock(&fs
->lock
);
113 struct fs_struct
*copy_fs_struct(struct fs_struct
*old
)
115 struct fs_struct
*fs
= kmem_cache_alloc(fs_cachep
, GFP_KERNEL
);
116 /* We don't need to lock fs - think why ;-) */
120 spin_lock_init(&fs
->lock
);
121 seqcount_init(&fs
->seq
);
122 fs
->umask
= old
->umask
;
124 spin_lock(&old
->lock
);
125 fs
->root
= old
->root
;
129 spin_unlock(&old
->lock
);
133 EXPORT_SYMBOL_GPL(copy_fs_struct
);
135 int unshare_fs_struct(void)
137 struct fs_struct
*fs
= current
->fs
;
138 struct fs_struct
*new_fs
= copy_fs_struct(fs
);
145 spin_lock(&fs
->lock
);
147 current
->fs
= new_fs
;
148 spin_unlock(&fs
->lock
);
149 task_unlock(current
);
156 EXPORT_SYMBOL_GPL(unshare_fs_struct
);
158 int current_umask(void)
160 return current
->fs
->umask
;
162 EXPORT_SYMBOL(current_umask
);
164 /* to be mentioned only in INIT_TASK */
165 struct fs_struct init_fs
= {
167 .lock
= __SPIN_LOCK_UNLOCKED(init_fs
.lock
),
168 .seq
= SEQCNT_ZERO(init_fs
.seq
),