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