init: add an init_utimes helper
[linux-2.6-block.git] / fs / init.c
CommitLineData
c60166f0
CH
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Routines that mimic syscalls, but don't use the user address space or file
4 * descriptors. Only for init/ and related early init code.
5 */
6#include <linux/init.h>
7#include <linux/mount.h>
8#include <linux/namei.h>
9#include <linux/fs.h>
db63f1e3 10#include <linux/fs_struct.h>
c60166f0 11#include <linux/init_syscalls.h>
4b7ca501 12#include <linux/security.h>
c60166f0
CH
13#include "internal.h"
14
15int __init init_mount(const char *dev_name, const char *dir_name,
16 const char *type_page, unsigned long flags, void *data_page)
17{
18 struct path path;
19 int ret;
20
21 ret = kern_path(dir_name, LOOKUP_FOLLOW, &path);
22 if (ret)
23 return ret;
24 ret = path_mount(dev_name, &path, type_page, flags, data_page);
25 path_put(&path);
26 return ret;
27}
09267def
CH
28
29int __init init_umount(const char *name, int flags)
30{
31 int lookup_flags = LOOKUP_MOUNTPOINT;
32 struct path path;
33 int ret;
34
35 if (!(flags & UMOUNT_NOFOLLOW))
36 lookup_flags |= LOOKUP_FOLLOW;
37 ret = kern_path(name, lookup_flags, &path);
38 if (ret)
39 return ret;
40 return path_umount(&path, flags);
41}
8fb9f73e 42
db63f1e3
CH
43int __init init_chdir(const char *filename)
44{
45 struct path path;
46 int error;
47
48 error = kern_path(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
49 if (error)
50 return error;
51 error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
52 if (!error)
53 set_fs_pwd(current->fs, &path);
54 path_put(&path);
55 return error;
56}
57
4b7ca501
CH
58int __init init_chroot(const char *filename)
59{
60 struct path path;
61 int error;
62
63 error = kern_path(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
64 if (error)
65 return error;
66 error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
67 if (error)
68 goto dput_and_out;
69 error = -EPERM;
70 if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT))
71 goto dput_and_out;
72 error = security_path_chroot(&path);
73 if (error)
74 goto dput_and_out;
75 set_fs_root(current->fs, &path);
76dput_and_out:
77 path_put(&path);
78 return error;
79}
80
b873498f
CH
81int __init init_chown(const char *filename, uid_t user, gid_t group, int flags)
82{
83 int lookup_flags = (flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
84 struct path path;
85 int error;
86
87 error = kern_path(filename, lookup_flags, &path);
88 if (error)
89 return error;
90 error = mnt_want_write(path.mnt);
91 if (!error) {
92 error = chown_common(&path, user, group);
93 mnt_drop_write(path.mnt);
94 }
95 path_put(&path);
96 return error;
97}
98
1097742e
CH
99int __init init_chmod(const char *filename, umode_t mode)
100{
101 struct path path;
102 int error;
103
104 error = kern_path(filename, LOOKUP_FOLLOW, &path);
105 if (error)
106 return error;
107 error = chmod_common(&path, mode);
108 path_put(&path);
109 return error;
110}
111
eb9d7d39
CH
112int __init init_eaccess(const char *filename)
113{
114 struct path path;
115 int error;
116
117 error = kern_path(filename, LOOKUP_FOLLOW, &path);
118 if (error)
119 return error;
120 error = inode_permission(d_inode(path.dentry), MAY_ACCESS);
121 path_put(&path);
122 return error;
123}
124
716308a5
CH
125int __init init_stat(const char *filename, struct kstat *stat, int flags)
126{
127 int lookup_flags = (flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
128 struct path path;
129 int error;
130
131 error = kern_path(filename, lookup_flags, &path);
132 if (error)
133 return error;
134 error = vfs_getattr(&path, stat, STATX_BASIC_STATS,
135 flags | AT_NO_AUTOMOUNT);
136 path_put(&path);
137 return error;
138}
139
5fee64fc
CH
140int __init init_mknod(const char *filename, umode_t mode, unsigned int dev)
141{
142 struct dentry *dentry;
143 struct path path;
144 int error;
145
146 if (S_ISFIFO(mode) || S_ISSOCK(mode))
147 dev = 0;
148 else if (!(S_ISBLK(mode) || S_ISCHR(mode)))
149 return -EINVAL;
150
151 dentry = kern_path_create(AT_FDCWD, filename, &path, 0);
152 if (IS_ERR(dentry))
153 return PTR_ERR(dentry);
154
155 if (!IS_POSIXACL(path.dentry->d_inode))
156 mode &= ~current_umask();
157 error = security_path_mknod(&path, dentry, mode, dev);
158 if (!error)
159 error = vfs_mknod(path.dentry->d_inode, dentry, mode,
160 new_decode_dev(dev));
161 done_path_create(&path, dentry);
162 return error;
163}
164
812931d6
CH
165int __init init_link(const char *oldname, const char *newname)
166{
167 struct dentry *new_dentry;
168 struct path old_path, new_path;
169 int error;
170
171 error = kern_path(oldname, 0, &old_path);
172 if (error)
173 return error;
174
175 new_dentry = kern_path_create(AT_FDCWD, newname, &new_path, 0);
176 error = PTR_ERR(new_dentry);
177 if (IS_ERR(new_dentry))
178 goto out;
179
180 error = -EXDEV;
181 if (old_path.mnt != new_path.mnt)
182 goto out_dput;
183 error = may_linkat(&old_path);
184 if (unlikely(error))
185 goto out_dput;
186 error = security_path_link(old_path.dentry, &new_path, new_dentry);
187 if (error)
188 goto out_dput;
189 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry,
190 NULL);
191out_dput:
192 done_path_create(&new_path, new_dentry);
193out:
194 path_put(&old_path);
195 return error;
196}
197
cd3acb6a
CH
198int __init init_symlink(const char *oldname, const char *newname)
199{
200 struct dentry *dentry;
201 struct path path;
202 int error;
203
204 dentry = kern_path_create(AT_FDCWD, newname, &path, 0);
205 if (IS_ERR(dentry))
206 return PTR_ERR(dentry);
207 error = security_path_symlink(&path, dentry, oldname);
208 if (!error)
209 error = vfs_symlink(path.dentry->d_inode, dentry, oldname);
210 done_path_create(&path, dentry);
211 return error;
212}
213
8fb9f73e
CH
214int __init init_unlink(const char *pathname)
215{
216 return do_unlinkat(AT_FDCWD, getname_kernel(pathname));
217}
20cce026 218
83ff98c3
CH
219int __init init_mkdir(const char *pathname, umode_t mode)
220{
221 struct dentry *dentry;
222 struct path path;
223 int error;
224
225 dentry = kern_path_create(AT_FDCWD, pathname, &path, LOOKUP_DIRECTORY);
226 if (IS_ERR(dentry))
227 return PTR_ERR(dentry);
228 if (!IS_POSIXACL(path.dentry->d_inode))
229 mode &= ~current_umask();
230 error = security_path_mkdir(&path, dentry, mode);
231 if (!error)
232 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
233 done_path_create(&path, dentry);
234 return error;
235}
236
20cce026
CH
237int __init init_rmdir(const char *pathname)
238{
239 return do_rmdir(AT_FDCWD, getname_kernel(pathname));
240}
235e5793
CH
241
242int __init init_utimes(char *filename, struct timespec64 *ts)
243{
244 struct path path;
245 int error;
246
247 error = kern_path(filename, 0, &path);
248 if (error)
249 return error;
250 error = vfs_utimes(&path, ts);
251 path_put(&path);
252 return error;
253}