]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/open.c | |
3 | * | |
4 | * Copyright (C) 1991, 1992 Linus Torvalds | |
5 | */ | |
6 | ||
7 | #include <linux/string.h> | |
8 | #include <linux/mm.h> | |
1da177e4 | 9 | #include <linux/file.h> |
9f3acc31 | 10 | #include <linux/fdtable.h> |
0eeca283 | 11 | #include <linux/fsnotify.h> |
1da177e4 | 12 | #include <linux/module.h> |
1da177e4 LT |
13 | #include <linux/tty.h> |
14 | #include <linux/namei.h> | |
15 | #include <linux/backing-dev.h> | |
16f7e0fe | 16 | #include <linux/capability.h> |
086f7316 | 17 | #include <linux/securebits.h> |
1da177e4 LT |
18 | #include <linux/security.h> |
19 | #include <linux/mount.h> | |
5590ff0d | 20 | #include <linux/fcntl.h> |
5a0e3ad6 | 21 | #include <linux/slab.h> |
1da177e4 LT |
22 | #include <asm/uaccess.h> |
23 | #include <linux/fs.h> | |
ef3daeda | 24 | #include <linux/personality.h> |
1da177e4 LT |
25 | #include <linux/pagemap.h> |
26 | #include <linux/syscalls.h> | |
ab2af1f5 | 27 | #include <linux/rcupdate.h> |
73241ccc | 28 | #include <linux/audit.h> |
97ac7350 | 29 | #include <linux/falloc.h> |
5ad4e53b | 30 | #include <linux/fs_struct.h> |
b65a9cfc | 31 | #include <linux/ima.h> |
2dfc1cae | 32 | #include <linux/dnotify.h> |
3f6d078d | 33 | #include <linux/compat.h> |
1da177e4 | 34 | |
e81e3f4d EP |
35 | #include "internal.h" |
36 | ||
4a30131e N |
37 | int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs, |
38 | struct file *filp) | |
1da177e4 | 39 | { |
939a9421 | 40 | int ret; |
1da177e4 LT |
41 | struct iattr newattrs; |
42 | ||
43 | /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */ | |
44 | if (length < 0) | |
45 | return -EINVAL; | |
46 | ||
47 | newattrs.ia_size = length; | |
4a30131e | 48 | newattrs.ia_valid = ATTR_SIZE | time_attrs; |
cc4e69de MS |
49 | if (filp) { |
50 | newattrs.ia_file = filp; | |
51 | newattrs.ia_valid |= ATTR_FILE; | |
52 | } | |
1da177e4 | 53 | |
7b82dc0e | 54 | /* Remove suid/sgid on truncate too */ |
939a9421 AW |
55 | ret = should_remove_suid(dentry); |
56 | if (ret) | |
57 | newattrs.ia_valid |= ret | ATTR_FORCE; | |
7b82dc0e | 58 | |
1b1dcc1b | 59 | mutex_lock(&dentry->d_inode->i_mutex); |
939a9421 | 60 | ret = notify_change(dentry, &newattrs); |
1b1dcc1b | 61 | mutex_unlock(&dentry->d_inode->i_mutex); |
939a9421 | 62 | return ret; |
1da177e4 LT |
63 | } |
64 | ||
a02de960 | 65 | long vfs_truncate(struct path *path, loff_t length) |
1da177e4 | 66 | { |
2d8f3038 | 67 | struct inode *inode; |
a02de960 | 68 | long error; |
1da177e4 | 69 | |
a02de960 | 70 | inode = path->dentry->d_inode; |
1da177e4 LT |
71 | |
72 | /* For directories it's -EISDIR, for other non-regulars - -EINVAL */ | |
1da177e4 | 73 | if (S_ISDIR(inode->i_mode)) |
a02de960 | 74 | return -EISDIR; |
1da177e4 | 75 | if (!S_ISREG(inode->i_mode)) |
a02de960 | 76 | return -EINVAL; |
1da177e4 | 77 | |
a02de960 | 78 | error = mnt_want_write(path->mnt); |
1da177e4 | 79 | if (error) |
a02de960 | 80 | goto out; |
1da177e4 | 81 | |
256984a8 | 82 | error = inode_permission(inode, MAY_WRITE); |
9ac9b847 DH |
83 | if (error) |
84 | goto mnt_drop_write_and_out; | |
1da177e4 LT |
85 | |
86 | error = -EPERM; | |
c82e42da | 87 | if (IS_APPEND(inode)) |
9ac9b847 | 88 | goto mnt_drop_write_and_out; |
1da177e4 | 89 | |
9700382c | 90 | error = get_write_access(inode); |
1da177e4 | 91 | if (error) |
9ac9b847 | 92 | goto mnt_drop_write_and_out; |
1da177e4 | 93 | |
9700382c | 94 | /* |
95 | * Make sure that there are no leases. get_write_access() protects | |
96 | * against the truncate racing with a lease-granting setlease(). | |
97 | */ | |
8737c930 | 98 | error = break_lease(inode, O_WRONLY); |
1da177e4 | 99 | if (error) |
9700382c | 100 | goto put_write_and_out; |
1da177e4 LT |
101 | |
102 | error = locks_verify_truncate(inode, NULL, length); | |
be6d3e56 | 103 | if (!error) |
a02de960 | 104 | error = security_path_truncate(path); |
907f4554 | 105 | if (!error) |
a02de960 | 106 | error = do_truncate(path->dentry, length, 0, NULL); |
1da177e4 | 107 | |
9700382c | 108 | put_write_and_out: |
109 | put_write_access(inode); | |
9ac9b847 | 110 | mnt_drop_write_and_out: |
a02de960 | 111 | mnt_drop_write(path->mnt); |
1da177e4 LT |
112 | out: |
113 | return error; | |
114 | } | |
a02de960 DH |
115 | EXPORT_SYMBOL_GPL(vfs_truncate); |
116 | ||
117 | static long do_sys_truncate(const char __user *pathname, loff_t length) | |
118 | { | |
48f7530d | 119 | unsigned int lookup_flags = LOOKUP_FOLLOW; |
a02de960 DH |
120 | struct path path; |
121 | int error; | |
122 | ||
123 | if (length < 0) /* sorry, but loff_t says... */ | |
124 | return -EINVAL; | |
125 | ||
48f7530d JL |
126 | retry: |
127 | error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path); | |
a02de960 DH |
128 | if (!error) { |
129 | error = vfs_truncate(&path, length); | |
130 | path_put(&path); | |
131 | } | |
48f7530d JL |
132 | if (retry_estale(error, lookup_flags)) { |
133 | lookup_flags |= LOOKUP_REVAL; | |
134 | goto retry; | |
135 | } | |
a02de960 DH |
136 | return error; |
137 | } | |
1da177e4 | 138 | |
4fd8da8d | 139 | SYSCALL_DEFINE2(truncate, const char __user *, path, long, length) |
1da177e4 | 140 | { |
4fd8da8d | 141 | return do_sys_truncate(path, length); |
1da177e4 LT |
142 | } |
143 | ||
3f6d078d AV |
144 | #ifdef CONFIG_COMPAT |
145 | COMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length) | |
146 | { | |
147 | return do_sys_truncate(path, length); | |
148 | } | |
149 | #endif | |
150 | ||
b01ec0ef | 151 | static long do_sys_ftruncate(unsigned int fd, loff_t length, int small) |
1da177e4 | 152 | { |
bf2965d5 | 153 | struct inode *inode; |
1da177e4 | 154 | struct dentry *dentry; |
2903ff01 | 155 | struct fd f; |
1da177e4 LT |
156 | int error; |
157 | ||
158 | error = -EINVAL; | |
159 | if (length < 0) | |
160 | goto out; | |
161 | error = -EBADF; | |
2903ff01 AV |
162 | f = fdget(fd); |
163 | if (!f.file) | |
1da177e4 LT |
164 | goto out; |
165 | ||
166 | /* explicitly opened as large or we are on 64-bit box */ | |
2903ff01 | 167 | if (f.file->f_flags & O_LARGEFILE) |
1da177e4 LT |
168 | small = 0; |
169 | ||
2903ff01 | 170 | dentry = f.file->f_path.dentry; |
1da177e4 LT |
171 | inode = dentry->d_inode; |
172 | error = -EINVAL; | |
2903ff01 | 173 | if (!S_ISREG(inode->i_mode) || !(f.file->f_mode & FMODE_WRITE)) |
1da177e4 LT |
174 | goto out_putf; |
175 | ||
176 | error = -EINVAL; | |
177 | /* Cannot ftruncate over 2^31 bytes without large file support */ | |
178 | if (small && length > MAX_NON_LFS) | |
179 | goto out_putf; | |
180 | ||
181 | error = -EPERM; | |
182 | if (IS_APPEND(inode)) | |
183 | goto out_putf; | |
184 | ||
14da9200 | 185 | sb_start_write(inode->i_sb); |
2903ff01 | 186 | error = locks_verify_truncate(inode, f.file, length); |
be6d3e56 | 187 | if (!error) |
2903ff01 | 188 | error = security_path_truncate(&f.file->f_path); |
1da177e4 | 189 | if (!error) |
2903ff01 | 190 | error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, f.file); |
14da9200 | 191 | sb_end_write(inode->i_sb); |
1da177e4 | 192 | out_putf: |
2903ff01 | 193 | fdput(f); |
1da177e4 LT |
194 | out: |
195 | return error; | |
196 | } | |
197 | ||
bdc480e3 | 198 | SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length) |
1da177e4 | 199 | { |
0a489cb3 | 200 | long ret = do_sys_ftruncate(fd, length, 1); |
385910f2 | 201 | /* avoid REGPARM breakage on x86: */ |
54a01510 | 202 | asmlinkage_protect(2, ret, fd, length); |
0a489cb3 | 203 | return ret; |
1da177e4 LT |
204 | } |
205 | ||
3f6d078d AV |
206 | #ifdef CONFIG_COMPAT |
207 | COMPAT_SYSCALL_DEFINE2(ftruncate, unsigned int, fd, compat_ulong_t, length) | |
208 | { | |
209 | return do_sys_ftruncate(fd, length, 1); | |
210 | } | |
211 | #endif | |
212 | ||
1da177e4 LT |
213 | /* LFS versions of truncate are only needed on 32 bit machines */ |
214 | #if BITS_PER_LONG == 32 | |
6673e0c3 | 215 | SYSCALL_DEFINE(truncate64)(const char __user * path, loff_t length) |
1da177e4 LT |
216 | { |
217 | return do_sys_truncate(path, length); | |
218 | } | |
6673e0c3 HC |
219 | #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS |
220 | asmlinkage long SyS_truncate64(long path, loff_t length) | |
221 | { | |
222 | return SYSC_truncate64((const char __user *) path, length); | |
223 | } | |
224 | SYSCALL_ALIAS(sys_truncate64, SyS_truncate64); | |
225 | #endif | |
1da177e4 | 226 | |
6673e0c3 | 227 | SYSCALL_DEFINE(ftruncate64)(unsigned int fd, loff_t length) |
1da177e4 | 228 | { |
0a489cb3 | 229 | long ret = do_sys_ftruncate(fd, length, 0); |
385910f2 | 230 | /* avoid REGPARM breakage on x86: */ |
54a01510 | 231 | asmlinkage_protect(2, ret, fd, length); |
0a489cb3 | 232 | return ret; |
1da177e4 | 233 | } |
6673e0c3 HC |
234 | #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS |
235 | asmlinkage long SyS_ftruncate64(long fd, loff_t length) | |
236 | { | |
237 | return SYSC_ftruncate64((unsigned int) fd, length); | |
238 | } | |
239 | SYSCALL_ALIAS(sys_ftruncate64, SyS_ftruncate64); | |
1da177e4 | 240 | #endif |
6673e0c3 | 241 | #endif /* BITS_PER_LONG == 32 */ |
1da177e4 | 242 | |
3e63cbb1 AJ |
243 | |
244 | int do_fallocate(struct file *file, int mode, loff_t offset, loff_t len) | |
97ac7350 | 245 | { |
496ad9aa | 246 | struct inode *inode = file_inode(file); |
3e63cbb1 | 247 | long ret; |
97ac7350 AA |
248 | |
249 | if (offset < 0 || len <= 0) | |
3e63cbb1 | 250 | return -EINVAL; |
97ac7350 AA |
251 | |
252 | /* Return error if mode is not supported */ | |
79124f18 JB |
253 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) |
254 | return -EOPNOTSUPP; | |
255 | ||
256 | /* Punch hole must have keep size set */ | |
257 | if ((mode & FALLOC_FL_PUNCH_HOLE) && | |
258 | !(mode & FALLOC_FL_KEEP_SIZE)) | |
3e63cbb1 | 259 | return -EOPNOTSUPP; |
97ac7350 | 260 | |
97ac7350 | 261 | if (!(file->f_mode & FMODE_WRITE)) |
3e63cbb1 | 262 | return -EBADF; |
1ca551c6 MS |
263 | |
264 | /* It's not possible punch hole on append only file */ | |
265 | if (mode & FALLOC_FL_PUNCH_HOLE && IS_APPEND(inode)) | |
266 | return -EPERM; | |
267 | ||
268 | if (IS_IMMUTABLE(inode)) | |
269 | return -EPERM; | |
270 | ||
97ac7350 AA |
271 | /* |
272 | * Revalidate the write permissions, in case security policy has | |
273 | * changed since the files were opened. | |
274 | */ | |
275 | ret = security_file_permission(file, MAY_WRITE); | |
276 | if (ret) | |
3e63cbb1 | 277 | return ret; |
97ac7350 | 278 | |
97ac7350 | 279 | if (S_ISFIFO(inode->i_mode)) |
3e63cbb1 | 280 | return -ESPIPE; |
97ac7350 | 281 | |
97ac7350 AA |
282 | /* |
283 | * Let individual file system decide if it supports preallocation | |
284 | * for directories or not. | |
285 | */ | |
286 | if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode)) | |
3e63cbb1 | 287 | return -ENODEV; |
97ac7350 | 288 | |
97ac7350 AA |
289 | /* Check for wrap through zero too */ |
290 | if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0)) | |
3e63cbb1 | 291 | return -EFBIG; |
97ac7350 | 292 | |
2fe17c10 | 293 | if (!file->f_op->fallocate) |
3e63cbb1 | 294 | return -EOPNOTSUPP; |
97ac7350 | 295 | |
14da9200 JK |
296 | sb_start_write(inode->i_sb); |
297 | ret = file->f_op->fallocate(file, mode, offset, len); | |
298 | sb_end_write(inode->i_sb); | |
299 | return ret; | |
3e63cbb1 AJ |
300 | } |
301 | ||
302 | SYSCALL_DEFINE(fallocate)(int fd, int mode, loff_t offset, loff_t len) | |
303 | { | |
2903ff01 | 304 | struct fd f = fdget(fd); |
3e63cbb1 AJ |
305 | int error = -EBADF; |
306 | ||
2903ff01 AV |
307 | if (f.file) { |
308 | error = do_fallocate(f.file, mode, offset, len); | |
309 | fdput(f); | |
3e63cbb1 | 310 | } |
3e63cbb1 | 311 | return error; |
97ac7350 | 312 | } |
3e63cbb1 | 313 | |
6673e0c3 HC |
314 | #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS |
315 | asmlinkage long SyS_fallocate(long fd, long mode, loff_t offset, loff_t len) | |
316 | { | |
317 | return SYSC_fallocate((int)fd, (int)mode, offset, len); | |
318 | } | |
319 | SYSCALL_ALIAS(sys_fallocate, SyS_fallocate); | |
320 | #endif | |
97ac7350 | 321 | |
1da177e4 LT |
322 | /* |
323 | * access() needs to use the real uid/gid, not the effective uid/gid. | |
324 | * We do this by temporarily clearing all FS-related capabilities and | |
325 | * switching the fsuid/fsgid around to the real ones. | |
326 | */ | |
6559eed8 | 327 | SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode) |
1da177e4 | 328 | { |
d84f4f99 DH |
329 | const struct cred *old_cred; |
330 | struct cred *override_cred; | |
2d8f3038 | 331 | struct path path; |
256984a8 | 332 | struct inode *inode; |
1da177e4 | 333 | int res; |
87fa5595 | 334 | unsigned int lookup_flags = LOOKUP_FOLLOW; |
1da177e4 LT |
335 | |
336 | if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */ | |
337 | return -EINVAL; | |
338 | ||
d84f4f99 DH |
339 | override_cred = prepare_creds(); |
340 | if (!override_cred) | |
341 | return -ENOMEM; | |
1da177e4 | 342 | |
d84f4f99 DH |
343 | override_cred->fsuid = override_cred->uid; |
344 | override_cred->fsgid = override_cred->gid; | |
1da177e4 | 345 | |
086f7316 | 346 | if (!issecure(SECURE_NO_SETUID_FIXUP)) { |
1cdcbec1 | 347 | /* Clear the capabilities if we switch to a non-root user */ |
18815a18 EB |
348 | kuid_t root_uid = make_kuid(override_cred->user_ns, 0); |
349 | if (!uid_eq(override_cred->uid, root_uid)) | |
d84f4f99 | 350 | cap_clear(override_cred->cap_effective); |
086f7316 | 351 | else |
d84f4f99 DH |
352 | override_cred->cap_effective = |
353 | override_cred->cap_permitted; | |
086f7316 | 354 | } |
1da177e4 | 355 | |
d84f4f99 | 356 | old_cred = override_creds(override_cred); |
87fa5595 JL |
357 | retry: |
358 | res = user_path_at(dfd, filename, lookup_flags, &path); | |
6902d925 DH |
359 | if (res) |
360 | goto out; | |
361 | ||
2d8f3038 | 362 | inode = path.dentry->d_inode; |
256984a8 AV |
363 | |
364 | if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) { | |
30524472 AV |
365 | /* |
366 | * MAY_EXEC on regular files is denied if the fs is mounted | |
367 | * with the "noexec" flag. | |
368 | */ | |
369 | res = -EACCES; | |
2d8f3038 | 370 | if (path.mnt->mnt_flags & MNT_NOEXEC) |
30524472 AV |
371 | goto out_path_release; |
372 | } | |
373 | ||
256984a8 | 374 | res = inode_permission(inode, mode | MAY_ACCESS); |
6902d925 | 375 | /* SuS v2 requires we report a read only fs too */ |
256984a8 | 376 | if (res || !(mode & S_IWOTH) || special_file(inode->i_mode)) |
6902d925 | 377 | goto out_path_release; |
2f676cbc DH |
378 | /* |
379 | * This is a rare case where using __mnt_is_readonly() | |
380 | * is OK without a mnt_want/drop_write() pair. Since | |
381 | * no actual write to the fs is performed here, we do | |
382 | * not need to telegraph to that to anyone. | |
383 | * | |
384 | * By doing this, we accept that this access is | |
385 | * inherently racy and know that the fs may change | |
386 | * state before we even see this result. | |
387 | */ | |
2d8f3038 | 388 | if (__mnt_is_readonly(path.mnt)) |
6902d925 | 389 | res = -EROFS; |
1da177e4 | 390 | |
6902d925 | 391 | out_path_release: |
2d8f3038 | 392 | path_put(&path); |
87fa5595 JL |
393 | if (retry_estale(res, lookup_flags)) { |
394 | lookup_flags |= LOOKUP_REVAL; | |
395 | goto retry; | |
396 | } | |
6902d925 | 397 | out: |
d84f4f99 DH |
398 | revert_creds(old_cred); |
399 | put_cred(override_cred); | |
1da177e4 LT |
400 | return res; |
401 | } | |
402 | ||
ca013e94 | 403 | SYSCALL_DEFINE2(access, const char __user *, filename, int, mode) |
5590ff0d UD |
404 | { |
405 | return sys_faccessat(AT_FDCWD, filename, mode); | |
406 | } | |
407 | ||
3cdad428 | 408 | SYSCALL_DEFINE1(chdir, const char __user *, filename) |
1da177e4 | 409 | { |
2d8f3038 | 410 | struct path path; |
1da177e4 | 411 | int error; |
0291c0a5 JL |
412 | unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY; |
413 | retry: | |
414 | error = user_path_at(AT_FDCWD, filename, lookup_flags, &path); | |
1da177e4 LT |
415 | if (error) |
416 | goto out; | |
417 | ||
9cfcac81 | 418 | error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR); |
1da177e4 LT |
419 | if (error) |
420 | goto dput_and_out; | |
421 | ||
2d8f3038 | 422 | set_fs_pwd(current->fs, &path); |
1da177e4 LT |
423 | |
424 | dput_and_out: | |
2d8f3038 | 425 | path_put(&path); |
0291c0a5 JL |
426 | if (retry_estale(error, lookup_flags)) { |
427 | lookup_flags |= LOOKUP_REVAL; | |
428 | goto retry; | |
429 | } | |
1da177e4 LT |
430 | out: |
431 | return error; | |
432 | } | |
433 | ||
3cdad428 | 434 | SYSCALL_DEFINE1(fchdir, unsigned int, fd) |
1da177e4 | 435 | { |
2903ff01 | 436 | struct fd f = fdget_raw(fd); |
1da177e4 | 437 | struct inode *inode; |
2903ff01 | 438 | int error = -EBADF; |
1da177e4 LT |
439 | |
440 | error = -EBADF; | |
2903ff01 | 441 | if (!f.file) |
1da177e4 LT |
442 | goto out; |
443 | ||
496ad9aa | 444 | inode = file_inode(f.file); |
1da177e4 LT |
445 | |
446 | error = -ENOTDIR; | |
447 | if (!S_ISDIR(inode->i_mode)) | |
448 | goto out_putf; | |
449 | ||
9cfcac81 | 450 | error = inode_permission(inode, MAY_EXEC | MAY_CHDIR); |
1da177e4 | 451 | if (!error) |
2903ff01 | 452 | set_fs_pwd(current->fs, &f.file->f_path); |
1da177e4 | 453 | out_putf: |
2903ff01 | 454 | fdput(f); |
1da177e4 LT |
455 | out: |
456 | return error; | |
457 | } | |
458 | ||
3480b257 | 459 | SYSCALL_DEFINE1(chroot, const char __user *, filename) |
1da177e4 | 460 | { |
2d8f3038 | 461 | struct path path; |
1da177e4 | 462 | int error; |
2771261e JL |
463 | unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY; |
464 | retry: | |
465 | error = user_path_at(AT_FDCWD, filename, lookup_flags, &path); | |
1da177e4 LT |
466 | if (error) |
467 | goto out; | |
468 | ||
9cfcac81 | 469 | error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR); |
1da177e4 LT |
470 | if (error) |
471 | goto dput_and_out; | |
472 | ||
473 | error = -EPERM; | |
a85fb273 | 474 | if (!nsown_capable(CAP_SYS_CHROOT)) |
1da177e4 | 475 | goto dput_and_out; |
8b8efb44 TH |
476 | error = security_path_chroot(&path); |
477 | if (error) | |
478 | goto dput_and_out; | |
1da177e4 | 479 | |
2d8f3038 | 480 | set_fs_root(current->fs, &path); |
1da177e4 LT |
481 | error = 0; |
482 | dput_and_out: | |
2d8f3038 | 483 | path_put(&path); |
2771261e JL |
484 | if (retry_estale(error, lookup_flags)) { |
485 | lookup_flags |= LOOKUP_REVAL; | |
486 | goto retry; | |
487 | } | |
1da177e4 LT |
488 | out: |
489 | return error; | |
490 | } | |
491 | ||
e57712eb | 492 | static int chmod_common(struct path *path, umode_t mode) |
1da177e4 | 493 | { |
e57712eb | 494 | struct inode *inode = path->dentry->d_inode; |
1da177e4 | 495 | struct iattr newattrs; |
e57712eb | 496 | int error; |
1da177e4 | 497 | |
e57712eb AV |
498 | error = mnt_want_write(path->mnt); |
499 | if (error) | |
500 | return error; | |
fe542cf5 | 501 | mutex_lock(&inode->i_mutex); |
cdcf116d | 502 | error = security_path_chmod(path, mode); |
e57712eb | 503 | if (error) |
fe542cf5 | 504 | goto out_unlock; |
1da177e4 LT |
505 | newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO); |
506 | newattrs.ia_valid = ATTR_MODE | ATTR_CTIME; | |
e57712eb | 507 | error = notify_change(path->dentry, &newattrs); |
fe542cf5 | 508 | out_unlock: |
1b1dcc1b | 509 | mutex_unlock(&inode->i_mutex); |
e57712eb AV |
510 | mnt_drop_write(path->mnt); |
511 | return error; | |
512 | } | |
513 | ||
49f0a076 | 514 | SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode) |
e57712eb AV |
515 | { |
516 | struct file * file; | |
517 | int err = -EBADF; | |
518 | ||
519 | file = fget(fd); | |
520 | if (file) { | |
bfcec708 | 521 | audit_inode(NULL, file->f_path.dentry, 0); |
e57712eb AV |
522 | err = chmod_common(&file->f_path, mode); |
523 | fput(file); | |
524 | } | |
1da177e4 LT |
525 | return err; |
526 | } | |
527 | ||
49f0a076 | 528 | SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, umode_t, mode) |
1da177e4 | 529 | { |
2d8f3038 | 530 | struct path path; |
1da177e4 | 531 | int error; |
14ff690c JL |
532 | unsigned int lookup_flags = LOOKUP_FOLLOW; |
533 | retry: | |
534 | error = user_path_at(dfd, filename, lookup_flags, &path); | |
e57712eb AV |
535 | if (!error) { |
536 | error = chmod_common(&path, mode); | |
537 | path_put(&path); | |
14ff690c JL |
538 | if (retry_estale(error, lookup_flags)) { |
539 | lookup_flags |= LOOKUP_REVAL; | |
540 | goto retry; | |
541 | } | |
e57712eb | 542 | } |
1da177e4 LT |
543 | return error; |
544 | } | |
545 | ||
49f0a076 | 546 | SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode) |
5590ff0d UD |
547 | { |
548 | return sys_fchmodat(AT_FDCWD, filename, mode); | |
549 | } | |
550 | ||
fe542cf5 | 551 | static int chown_common(struct path *path, uid_t user, gid_t group) |
1da177e4 | 552 | { |
fe542cf5 | 553 | struct inode *inode = path->dentry->d_inode; |
1da177e4 LT |
554 | int error; |
555 | struct iattr newattrs; | |
52137abe EB |
556 | kuid_t uid; |
557 | kgid_t gid; | |
558 | ||
559 | uid = make_kuid(current_user_ns(), user); | |
560 | gid = make_kgid(current_user_ns(), group); | |
1da177e4 | 561 | |
1da177e4 LT |
562 | newattrs.ia_valid = ATTR_CTIME; |
563 | if (user != (uid_t) -1) { | |
52137abe EB |
564 | if (!uid_valid(uid)) |
565 | return -EINVAL; | |
1da177e4 | 566 | newattrs.ia_valid |= ATTR_UID; |
52137abe | 567 | newattrs.ia_uid = uid; |
1da177e4 LT |
568 | } |
569 | if (group != (gid_t) -1) { | |
52137abe EB |
570 | if (!gid_valid(gid)) |
571 | return -EINVAL; | |
1da177e4 | 572 | newattrs.ia_valid |= ATTR_GID; |
52137abe | 573 | newattrs.ia_gid = gid; |
1da177e4 LT |
574 | } |
575 | if (!S_ISDIR(inode->i_mode)) | |
b5376771 SH |
576 | newattrs.ia_valid |= |
577 | ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV; | |
1b1dcc1b | 578 | mutex_lock(&inode->i_mutex); |
d2b31ca6 | 579 | error = security_path_chown(path, uid, gid); |
fe542cf5 TH |
580 | if (!error) |
581 | error = notify_change(path->dentry, &newattrs); | |
1b1dcc1b | 582 | mutex_unlock(&inode->i_mutex); |
beb29e05 | 583 | |
1da177e4 LT |
584 | return error; |
585 | } | |
586 | ||
6559eed8 HC |
587 | SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user, |
588 | gid_t, group, int, flag) | |
5590ff0d | 589 | { |
2d8f3038 | 590 | struct path path; |
5590ff0d | 591 | int error = -EINVAL; |
65cfc672 | 592 | int lookup_flags; |
5590ff0d | 593 | |
65cfc672 | 594 | if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0) |
5590ff0d UD |
595 | goto out; |
596 | ||
65cfc672 AV |
597 | lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW; |
598 | if (flag & AT_EMPTY_PATH) | |
599 | lookup_flags |= LOOKUP_EMPTY; | |
99a5df37 | 600 | retry: |
65cfc672 | 601 | error = user_path_at(dfd, filename, lookup_flags, &path); |
6902d925 DH |
602 | if (error) |
603 | goto out; | |
2d8f3038 | 604 | error = mnt_want_write(path.mnt); |
2af482a7 DH |
605 | if (error) |
606 | goto out_release; | |
fe542cf5 | 607 | error = chown_common(&path, user, group); |
2d8f3038 | 608 | mnt_drop_write(path.mnt); |
2af482a7 | 609 | out_release: |
2d8f3038 | 610 | path_put(&path); |
99a5df37 JL |
611 | if (retry_estale(error, lookup_flags)) { |
612 | lookup_flags |= LOOKUP_REVAL; | |
613 | goto retry; | |
614 | } | |
5590ff0d UD |
615 | out: |
616 | return error; | |
617 | } | |
618 | ||
55e4def0 | 619 | SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group) |
1da177e4 | 620 | { |
55e4def0 DH |
621 | return sys_fchownat(AT_FDCWD, filename, user, group, 0); |
622 | } | |
1da177e4 | 623 | |
55e4def0 DH |
624 | SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group) |
625 | { | |
626 | return sys_fchownat(AT_FDCWD, filename, user, group, | |
627 | AT_SYMLINK_NOFOLLOW); | |
1da177e4 LT |
628 | } |
629 | ||
ca013e94 | 630 | SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group) |
1da177e4 | 631 | { |
2903ff01 | 632 | struct fd f = fdget(fd); |
1da177e4 LT |
633 | int error = -EBADF; |
634 | ||
2903ff01 | 635 | if (!f.file) |
6902d925 DH |
636 | goto out; |
637 | ||
2903ff01 | 638 | error = mnt_want_write_file(f.file); |
2af482a7 DH |
639 | if (error) |
640 | goto out_fput; | |
bfcec708 | 641 | audit_inode(NULL, f.file->f_path.dentry, 0); |
2903ff01 AV |
642 | error = chown_common(&f.file->f_path, user, group); |
643 | mnt_drop_write_file(f.file); | |
2af482a7 | 644 | out_fput: |
2903ff01 | 645 | fdput(f); |
6902d925 | 646 | out: |
1da177e4 LT |
647 | return error; |
648 | } | |
649 | ||
4a3fd211 DH |
650 | /* |
651 | * You have to be very careful that these write | |
652 | * counts get cleaned up in error cases and | |
653 | * upon __fput(). This should probably never | |
654 | * be called outside of __dentry_open(). | |
655 | */ | |
656 | static inline int __get_file_write_access(struct inode *inode, | |
657 | struct vfsmount *mnt) | |
658 | { | |
659 | int error; | |
660 | error = get_write_access(inode); | |
661 | if (error) | |
662 | return error; | |
663 | /* | |
664 | * Do not take mount writer counts on | |
665 | * special files since no writes to | |
666 | * the mount itself will occur. | |
667 | */ | |
668 | if (!special_file(inode->i_mode)) { | |
669 | /* | |
670 | * Balanced in __fput() | |
671 | */ | |
eb04c282 | 672 | error = __mnt_want_write(mnt); |
4a3fd211 DH |
673 | if (error) |
674 | put_write_access(inode); | |
675 | } | |
676 | return error; | |
677 | } | |
678 | ||
90ad1a8e MS |
679 | int open_check_o_direct(struct file *f) |
680 | { | |
681 | /* NB: we're sure to have correct a_ops only after f_op->open */ | |
682 | if (f->f_flags & O_DIRECT) { | |
683 | if (!f->f_mapping->a_ops || | |
684 | ((!f->f_mapping->a_ops->direct_IO) && | |
685 | (!f->f_mapping->a_ops->get_xip_mem))) { | |
686 | return -EINVAL; | |
687 | } | |
688 | } | |
689 | return 0; | |
690 | } | |
691 | ||
02e5180d | 692 | static int do_dentry_open(struct file *f, |
96b7e579 AV |
693 | int (*open)(struct inode *, struct file *), |
694 | const struct cred *cred) | |
1da177e4 | 695 | { |
1abf0c71 | 696 | static const struct file_operations empty_fops = {}; |
1da177e4 LT |
697 | struct inode *inode; |
698 | int error; | |
699 | ||
5300990c | 700 | f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK | |
a1a5b3d9 | 701 | FMODE_PREAD | FMODE_PWRITE; |
1abf0c71 AV |
702 | |
703 | if (unlikely(f->f_flags & O_PATH)) | |
704 | f->f_mode = FMODE_PATH; | |
705 | ||
b5bcdda3 | 706 | path_get(&f->f_path); |
dd37978c | 707 | inode = f->f_inode = f->f_path.dentry->d_inode; |
1da177e4 | 708 | if (f->f_mode & FMODE_WRITE) { |
02e5180d | 709 | error = __get_file_write_access(inode, f->f_path.mnt); |
1da177e4 LT |
710 | if (error) |
711 | goto cleanup_file; | |
ad775f5a DH |
712 | if (!special_file(inode->i_mode)) |
713 | file_take_write(f); | |
1da177e4 LT |
714 | } |
715 | ||
716 | f->f_mapping = inode->i_mapping; | |
ee2ffa0d | 717 | file_sb_list_add(f, inode->i_sb); |
1da177e4 | 718 | |
1abf0c71 AV |
719 | if (unlikely(f->f_mode & FMODE_PATH)) { |
720 | f->f_op = &empty_fops; | |
96b7e579 | 721 | return 0; |
1abf0c71 AV |
722 | } |
723 | ||
724 | f->f_op = fops_get(inode->i_fop); | |
725 | ||
83d49856 | 726 | error = security_file_open(f, cred); |
788e7dd4 YN |
727 | if (error) |
728 | goto cleanup_all; | |
729 | ||
f3c7691e BF |
730 | error = break_lease(inode, f->f_flags); |
731 | if (error) | |
732 | goto cleanup_all; | |
733 | ||
834f2a4a TM |
734 | if (!open && f->f_op) |
735 | open = f->f_op->open; | |
736 | if (open) { | |
737 | error = open(inode, f); | |
1da177e4 LT |
738 | if (error) |
739 | goto cleanup_all; | |
740 | } | |
890275b5 MZ |
741 | if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) |
742 | i_readcount_inc(inode); | |
834f2a4a | 743 | |
1da177e4 LT |
744 | f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC); |
745 | ||
746 | file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping); | |
747 | ||
96b7e579 | 748 | return 0; |
1da177e4 LT |
749 | |
750 | cleanup_all: | |
751 | fops_put(f->f_op); | |
c3c4f694 | 752 | file_sb_list_del(f); |
4a3fd211 | 753 | if (f->f_mode & FMODE_WRITE) { |
1da177e4 | 754 | put_write_access(inode); |
ad775f5a DH |
755 | if (!special_file(inode->i_mode)) { |
756 | /* | |
757 | * We don't consider this a real | |
758 | * mnt_want/drop_write() pair | |
759 | * because it all happenend right | |
760 | * here, so just reset the state. | |
761 | */ | |
762 | file_reset_write(f); | |
fe7c8051 | 763 | __mnt_drop_write(f->f_path.mnt); |
ad775f5a | 764 | } |
4a3fd211 | 765 | } |
1da177e4 | 766 | cleanup_file: |
02e5180d AV |
767 | path_put(&f->f_path); |
768 | f->f_path.mnt = NULL; | |
769 | f->f_path.dentry = NULL; | |
dd37978c | 770 | f->f_inode = NULL; |
96b7e579 | 771 | return error; |
1da177e4 LT |
772 | } |
773 | ||
d18e9008 MS |
774 | /** |
775 | * finish_open - finish opening a file | |
776 | * @od: opaque open data | |
777 | * @dentry: pointer to dentry | |
778 | * @open: open callback | |
779 | * | |
780 | * This can be used to finish opening a file passed to i_op->atomic_open(). | |
781 | * | |
782 | * If the open callback is set to NULL, then the standard f_op->open() | |
783 | * filesystem callback is substituted. | |
784 | */ | |
30d90494 AV |
785 | int finish_open(struct file *file, struct dentry *dentry, |
786 | int (*open)(struct inode *, struct file *), | |
787 | int *opened) | |
d18e9008 | 788 | { |
96b7e579 | 789 | int error; |
3d8a00d2 | 790 | BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */ |
d18e9008 | 791 | |
b5bcdda3 | 792 | file->f_path.dentry = dentry; |
02e5180d | 793 | error = do_dentry_open(file, open, current_cred()); |
96b7e579 | 794 | if (!error) |
47237687 | 795 | *opened |= FILE_OPENED; |
d18e9008 | 796 | |
96b7e579 | 797 | return error; |
d18e9008 MS |
798 | } |
799 | EXPORT_SYMBOL(finish_open); | |
800 | ||
801 | /** | |
802 | * finish_no_open - finish ->atomic_open() without opening the file | |
803 | * | |
804 | * @od: opaque open data | |
805 | * @dentry: dentry or NULL (as returned from ->lookup()) | |
806 | * | |
807 | * This can be used to set the result of a successful lookup in ->atomic_open(). | |
808 | * The filesystem's atomic_open() method shall return NULL after calling this. | |
809 | */ | |
e45198a6 | 810 | int finish_no_open(struct file *file, struct dentry *dentry) |
d18e9008 | 811 | { |
30d90494 | 812 | file->f_path.dentry = dentry; |
e45198a6 | 813 | return 1; |
d18e9008 MS |
814 | } |
815 | EXPORT_SYMBOL(finish_no_open); | |
816 | ||
765927b2 | 817 | struct file *dentry_open(const struct path *path, int flags, |
745ca247 | 818 | const struct cred *cred) |
a1a5b3d9 PS |
819 | { |
820 | int error; | |
821 | struct file *f; | |
822 | ||
e0e81739 DH |
823 | validate_creds(cred); |
824 | ||
c212f9aa | 825 | /* We must always pass in a valid mount pointer. */ |
765927b2 | 826 | BUG_ON(!path->mnt); |
322ee5b3 | 827 | |
a1a5b3d9 | 828 | f = get_empty_filp(); |
1afc99be AV |
829 | if (!IS_ERR(f)) { |
830 | f->f_flags = flags; | |
831 | f->f_path = *path; | |
832 | error = do_dentry_open(f, NULL, cred); | |
833 | if (!error) { | |
834 | /* from now on we need fput() to dispose of f */ | |
835 | error = open_check_o_direct(f); | |
836 | if (error) { | |
837 | fput(f); | |
838 | f = ERR_PTR(error); | |
839 | } | |
840 | } else { | |
841 | put_filp(f); | |
2a027e7a AV |
842 | f = ERR_PTR(error); |
843 | } | |
2a027e7a AV |
844 | } |
845 | return f; | |
a1a5b3d9 | 846 | } |
1da177e4 LT |
847 | EXPORT_SYMBOL(dentry_open); |
848 | ||
a218d0fd | 849 | static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op) |
47c805dc AV |
850 | { |
851 | int lookup_flags = 0; | |
852 | int acc_mode; | |
853 | ||
e68726ff MS |
854 | if (flags & O_CREAT) |
855 | op->mode = (mode & S_IALLUGO) | S_IFREG; | |
856 | else | |
857 | op->mode = 0; | |
47c805dc AV |
858 | |
859 | /* Must never be set by userspace */ | |
c6f3d811 | 860 | flags &= ~FMODE_NONOTIFY & ~O_CLOEXEC; |
47c805dc AV |
861 | |
862 | /* | |
863 | * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only | |
864 | * check for O_DSYNC if the need any syncing at all we enforce it's | |
865 | * always set instead of having to deal with possibly weird behaviour | |
866 | * for malicious applications setting only __O_SYNC. | |
867 | */ | |
868 | if (flags & __O_SYNC) | |
869 | flags |= O_DSYNC; | |
870 | ||
1abf0c71 AV |
871 | /* |
872 | * If we have O_PATH in the open flag. Then we | |
873 | * cannot have anything other than the below set of flags | |
874 | */ | |
875 | if (flags & O_PATH) { | |
876 | flags &= O_DIRECTORY | O_NOFOLLOW | O_PATH; | |
877 | acc_mode = 0; | |
878 | } else { | |
879 | acc_mode = MAY_OPEN | ACC_MODE(flags); | |
880 | } | |
47c805dc | 881 | |
1abf0c71 | 882 | op->open_flag = flags; |
47c805dc AV |
883 | |
884 | /* O_TRUNC implies we need access checks for write permissions */ | |
885 | if (flags & O_TRUNC) | |
886 | acc_mode |= MAY_WRITE; | |
887 | ||
888 | /* Allow the LSM permission hook to distinguish append | |
889 | access from general write access. */ | |
890 | if (flags & O_APPEND) | |
891 | acc_mode |= MAY_APPEND; | |
892 | ||
893 | op->acc_mode = acc_mode; | |
894 | ||
1abf0c71 AV |
895 | op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN; |
896 | ||
47c805dc AV |
897 | if (flags & O_CREAT) { |
898 | op->intent |= LOOKUP_CREATE; | |
899 | if (flags & O_EXCL) | |
900 | op->intent |= LOOKUP_EXCL; | |
901 | } | |
902 | ||
903 | if (flags & O_DIRECTORY) | |
904 | lookup_flags |= LOOKUP_DIRECTORY; | |
905 | if (!(flags & O_NOFOLLOW)) | |
906 | lookup_flags |= LOOKUP_FOLLOW; | |
907 | return lookup_flags; | |
908 | } | |
909 | ||
669abf4e JL |
910 | /** |
911 | * file_open_name - open file and return file pointer | |
912 | * | |
913 | * @name: struct filename containing path to open | |
914 | * @flags: open flags as per the open(2) second argument | |
915 | * @mode: mode for the new file if O_CREAT is set, else ignored | |
916 | * | |
917 | * This is the helper to open a file from kernelspace if you really | |
918 | * have to. But in generally you should not do this, so please move | |
919 | * along, nothing to see here.. | |
920 | */ | |
921 | struct file *file_open_name(struct filename *name, int flags, umode_t mode) | |
922 | { | |
923 | struct open_flags op; | |
924 | int lookup = build_open_flags(flags, mode, &op); | |
925 | return do_filp_open(AT_FDCWD, name, &op, lookup); | |
926 | } | |
927 | ||
47c805dc AV |
928 | /** |
929 | * filp_open - open file and return file pointer | |
930 | * | |
931 | * @filename: path to open | |
932 | * @flags: open flags as per the open(2) second argument | |
933 | * @mode: mode for the new file if O_CREAT is set, else ignored | |
934 | * | |
935 | * This is the helper to open a file from kernelspace if you really | |
936 | * have to. But in generally you should not do this, so please move | |
937 | * along, nothing to see here.. | |
938 | */ | |
a218d0fd | 939 | struct file *filp_open(const char *filename, int flags, umode_t mode) |
47c805dc | 940 | { |
669abf4e JL |
941 | struct filename name = {.name = filename}; |
942 | return file_open_name(&name, flags, mode); | |
47c805dc AV |
943 | } |
944 | EXPORT_SYMBOL(filp_open); | |
945 | ||
73d049a4 AV |
946 | struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt, |
947 | const char *filename, int flags) | |
948 | { | |
949 | struct open_flags op; | |
950 | int lookup = build_open_flags(flags, 0, &op); | |
951 | if (flags & O_CREAT) | |
952 | return ERR_PTR(-EINVAL); | |
953 | if (!filename && (flags & O_DIRECTORY)) | |
954 | if (!dentry->d_inode->i_op->lookup) | |
955 | return ERR_PTR(-ENOTDIR); | |
956 | return do_file_open_root(dentry, mnt, filename, &op, lookup); | |
957 | } | |
958 | EXPORT_SYMBOL(file_open_root); | |
959 | ||
a218d0fd | 960 | long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode) |
1da177e4 | 961 | { |
47c805dc AV |
962 | struct open_flags op; |
963 | int lookup = build_open_flags(flags, mode, &op); | |
91a27b2a | 964 | struct filename *tmp = getname(filename); |
e922efc3 | 965 | int fd = PTR_ERR(tmp); |
1da177e4 | 966 | |
1da177e4 | 967 | if (!IS_ERR(tmp)) { |
f23513e8 | 968 | fd = get_unused_fd_flags(flags); |
1da177e4 | 969 | if (fd >= 0) { |
669abf4e | 970 | struct file *f = do_filp_open(dfd, tmp, &op, lookup); |
fed2fc18 TN |
971 | if (IS_ERR(f)) { |
972 | put_unused_fd(fd); | |
973 | fd = PTR_ERR(f); | |
974 | } else { | |
2a12a9d7 | 975 | fsnotify_open(f); |
fed2fc18 TN |
976 | fd_install(fd, f); |
977 | } | |
1da177e4 | 978 | } |
1da177e4 LT |
979 | putname(tmp); |
980 | } | |
981 | return fd; | |
1da177e4 | 982 | } |
e922efc3 | 983 | |
a218d0fd | 984 | SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode) |
e922efc3 | 985 | { |
385910f2 LT |
986 | long ret; |
987 | ||
e922efc3 MS |
988 | if (force_o_largefile()) |
989 | flags |= O_LARGEFILE; | |
990 | ||
385910f2 LT |
991 | ret = do_sys_open(AT_FDCWD, filename, flags, mode); |
992 | /* avoid REGPARM breakage on x86: */ | |
54a01510 | 993 | asmlinkage_protect(3, ret, filename, flags, mode); |
385910f2 | 994 | return ret; |
e922efc3 | 995 | } |
1da177e4 | 996 | |
6559eed8 | 997 | SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, |
a218d0fd | 998 | umode_t, mode) |
5590ff0d | 999 | { |
385910f2 LT |
1000 | long ret; |
1001 | ||
5590ff0d UD |
1002 | if (force_o_largefile()) |
1003 | flags |= O_LARGEFILE; | |
1004 | ||
385910f2 LT |
1005 | ret = do_sys_open(dfd, filename, flags, mode); |
1006 | /* avoid REGPARM breakage on x86: */ | |
54a01510 | 1007 | asmlinkage_protect(4, ret, dfd, filename, flags, mode); |
385910f2 | 1008 | return ret; |
5590ff0d | 1009 | } |
5590ff0d | 1010 | |
1da177e4 LT |
1011 | #ifndef __alpha__ |
1012 | ||
1013 | /* | |
1014 | * For backward compatibility? Maybe this should be moved | |
1015 | * into arch/i386 instead? | |
1016 | */ | |
a218d0fd | 1017 | SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode) |
1da177e4 LT |
1018 | { |
1019 | return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode); | |
1020 | } | |
1021 | ||
1022 | #endif | |
1023 | ||
1024 | /* | |
1025 | * "id" is the POSIX thread ID. We use the | |
1026 | * files pointer for this.. | |
1027 | */ | |
1028 | int filp_close(struct file *filp, fl_owner_t id) | |
1029 | { | |
45778ca8 | 1030 | int retval = 0; |
1da177e4 LT |
1031 | |
1032 | if (!file_count(filp)) { | |
1033 | printk(KERN_ERR "VFS: Close: file count is 0\n"); | |
45778ca8 | 1034 | return 0; |
1da177e4 LT |
1035 | } |
1036 | ||
45778ca8 | 1037 | if (filp->f_op && filp->f_op->flush) |
75e1fcc0 | 1038 | retval = filp->f_op->flush(filp, id); |
1da177e4 | 1039 | |
1abf0c71 AV |
1040 | if (likely(!(filp->f_mode & FMODE_PATH))) { |
1041 | dnotify_flush(filp, id); | |
1042 | locks_remove_posix(filp, id); | |
1043 | } | |
1da177e4 LT |
1044 | fput(filp); |
1045 | return retval; | |
1046 | } | |
1047 | ||
1048 | EXPORT_SYMBOL(filp_close); | |
1049 | ||
1050 | /* | |
1051 | * Careful here! We test whether the file pointer is NULL before | |
1052 | * releasing the fd. This ensures that one clone task can't release | |
1053 | * an fd while another clone is opening it. | |
1054 | */ | |
ca013e94 | 1055 | SYSCALL_DEFINE1(close, unsigned int, fd) |
1da177e4 | 1056 | { |
483ce1d4 | 1057 | int retval = __close_fd(current->files, fd); |
ee731f4f EP |
1058 | |
1059 | /* can't restart close syscall because file table entry was cleared */ | |
1060 | if (unlikely(retval == -ERESTARTSYS || | |
1061 | retval == -ERESTARTNOINTR || | |
1062 | retval == -ERESTARTNOHAND || | |
1063 | retval == -ERESTART_RESTARTBLOCK)) | |
1064 | retval = -EINTR; | |
1065 | ||
1066 | return retval; | |
1da177e4 | 1067 | } |
1da177e4 LT |
1068 | EXPORT_SYMBOL(sys_close); |
1069 | ||
1070 | /* | |
1071 | * This routine simulates a hangup on the tty, to arrange that users | |
1072 | * are given clean terminals at login time. | |
1073 | */ | |
ca013e94 | 1074 | SYSCALL_DEFINE0(vhangup) |
1da177e4 LT |
1075 | { |
1076 | if (capable(CAP_SYS_TTY_CONFIG)) { | |
2cb5998b | 1077 | tty_vhangup_self(); |
1da177e4 LT |
1078 | return 0; |
1079 | } | |
1080 | return -EPERM; | |
1081 | } | |
1082 | ||
1083 | /* | |
1084 | * Called when an inode is about to be open. | |
1085 | * We use this to disallow opening large files on 32bit systems if | |
1086 | * the caller didn't specify O_LARGEFILE. On 64bit systems we force | |
1087 | * on this flag in sys_open. | |
1088 | */ | |
1089 | int generic_file_open(struct inode * inode, struct file * filp) | |
1090 | { | |
1091 | if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS) | |
a9c62a18 | 1092 | return -EOVERFLOW; |
1da177e4 LT |
1093 | return 0; |
1094 | } | |
1095 | ||
1096 | EXPORT_SYMBOL(generic_file_open); | |
1097 | ||
1098 | /* | |
1099 | * This is used by subsystems that don't want seekable | |
06b1e104 DT |
1100 | * file descriptors. The function is not supposed to ever fail, the only |
1101 | * reason it returns an 'int' and not 'void' is so that it can be plugged | |
1102 | * directly into file_operations structure. | |
1da177e4 LT |
1103 | */ |
1104 | int nonseekable_open(struct inode *inode, struct file *filp) | |
1105 | { | |
1106 | filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE); | |
1107 | return 0; | |
1108 | } | |
1109 | ||
1110 | EXPORT_SYMBOL(nonseekable_open); |