]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/open.c
fsnotify: include data in should_send calls
[net-next-2.6.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>
1da177e4 33
e81e3f4d
EP
34#include "internal.h"
35
4a30131e
N
36int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
37 struct file *filp)
1da177e4 38{
939a9421 39 int ret;
1da177e4
LT
40 struct iattr newattrs;
41
42 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
43 if (length < 0)
44 return -EINVAL;
45
46 newattrs.ia_size = length;
4a30131e 47 newattrs.ia_valid = ATTR_SIZE | time_attrs;
cc4e69de
MS
48 if (filp) {
49 newattrs.ia_file = filp;
50 newattrs.ia_valid |= ATTR_FILE;
51 }
1da177e4 52
7b82dc0e 53 /* Remove suid/sgid on truncate too */
939a9421
AW
54 ret = should_remove_suid(dentry);
55 if (ret)
56 newattrs.ia_valid |= ret | ATTR_FORCE;
7b82dc0e 57
1b1dcc1b 58 mutex_lock(&dentry->d_inode->i_mutex);
939a9421 59 ret = notify_change(dentry, &newattrs);
1b1dcc1b 60 mutex_unlock(&dentry->d_inode->i_mutex);
939a9421 61 return ret;
1da177e4
LT
62}
63
2d8f3038 64static long do_sys_truncate(const char __user *pathname, loff_t length)
1da177e4 65{
2d8f3038
AV
66 struct path path;
67 struct inode *inode;
1da177e4
LT
68 int error;
69
70 error = -EINVAL;
71 if (length < 0) /* sorry, but loff_t says... */
72 goto out;
73
2d8f3038 74 error = user_path(pathname, &path);
1da177e4
LT
75 if (error)
76 goto out;
2d8f3038 77 inode = path.dentry->d_inode;
1da177e4
LT
78
79 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
80 error = -EISDIR;
81 if (S_ISDIR(inode->i_mode))
82 goto dput_and_out;
83
84 error = -EINVAL;
85 if (!S_ISREG(inode->i_mode))
86 goto dput_and_out;
87
2d8f3038 88 error = mnt_want_write(path.mnt);
1da177e4
LT
89 if (error)
90 goto dput_and_out;
91
256984a8 92 error = inode_permission(inode, MAY_WRITE);
9ac9b847
DH
93 if (error)
94 goto mnt_drop_write_and_out;
1da177e4
LT
95
96 error = -EPERM;
c82e42da 97 if (IS_APPEND(inode))
9ac9b847 98 goto mnt_drop_write_and_out;
1da177e4 99
9700382c 100 error = get_write_access(inode);
1da177e4 101 if (error)
9ac9b847 102 goto mnt_drop_write_and_out;
1da177e4 103
9700382c 104 /*
105 * Make sure that there are no leases. get_write_access() protects
106 * against the truncate racing with a lease-granting setlease().
107 */
8737c930 108 error = break_lease(inode, O_WRONLY);
1da177e4 109 if (error)
9700382c 110 goto put_write_and_out;
1da177e4
LT
111
112 error = locks_verify_truncate(inode, NULL, length);
be6d3e56
KT
113 if (!error)
114 error = security_path_truncate(&path, length, 0);
907f4554 115 if (!error)
2d8f3038 116 error = do_truncate(path.dentry, length, 0, NULL);
1da177e4 117
9700382c 118put_write_and_out:
119 put_write_access(inode);
9ac9b847 120mnt_drop_write_and_out:
2d8f3038 121 mnt_drop_write(path.mnt);
1da177e4 122dput_and_out:
2d8f3038 123 path_put(&path);
1da177e4
LT
124out:
125 return error;
126}
127
4fd8da8d 128SYSCALL_DEFINE2(truncate, const char __user *, path, long, length)
1da177e4 129{
4fd8da8d 130 return do_sys_truncate(path, length);
1da177e4
LT
131}
132
b01ec0ef 133static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
1da177e4
LT
134{
135 struct inode * inode;
136 struct dentry *dentry;
137 struct file * file;
138 int error;
139
140 error = -EINVAL;
141 if (length < 0)
142 goto out;
143 error = -EBADF;
144 file = fget(fd);
145 if (!file)
146 goto out;
147
148 /* explicitly opened as large or we are on 64-bit box */
149 if (file->f_flags & O_LARGEFILE)
150 small = 0;
151
0f7fc9e4 152 dentry = file->f_path.dentry;
1da177e4
LT
153 inode = dentry->d_inode;
154 error = -EINVAL;
155 if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
156 goto out_putf;
157
158 error = -EINVAL;
159 /* Cannot ftruncate over 2^31 bytes without large file support */
160 if (small && length > MAX_NON_LFS)
161 goto out_putf;
162
163 error = -EPERM;
164 if (IS_APPEND(inode))
165 goto out_putf;
166
167 error = locks_verify_truncate(inode, file, length);
be6d3e56
KT
168 if (!error)
169 error = security_path_truncate(&file->f_path, length,
170 ATTR_MTIME|ATTR_CTIME);
1da177e4 171 if (!error)
6e656be8 172 error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, file);
1da177e4
LT
173out_putf:
174 fput(file);
175out:
176 return error;
177}
178
bdc480e3 179SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length)
1da177e4 180{
0a489cb3 181 long ret = do_sys_ftruncate(fd, length, 1);
385910f2 182 /* avoid REGPARM breakage on x86: */
54a01510 183 asmlinkage_protect(2, ret, fd, length);
0a489cb3 184 return ret;
1da177e4
LT
185}
186
187/* LFS versions of truncate are only needed on 32 bit machines */
188#if BITS_PER_LONG == 32
6673e0c3 189SYSCALL_DEFINE(truncate64)(const char __user * path, loff_t length)
1da177e4
LT
190{
191 return do_sys_truncate(path, length);
192}
6673e0c3
HC
193#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
194asmlinkage long SyS_truncate64(long path, loff_t length)
195{
196 return SYSC_truncate64((const char __user *) path, length);
197}
198SYSCALL_ALIAS(sys_truncate64, SyS_truncate64);
199#endif
1da177e4 200
6673e0c3 201SYSCALL_DEFINE(ftruncate64)(unsigned int fd, loff_t length)
1da177e4 202{
0a489cb3 203 long ret = do_sys_ftruncate(fd, length, 0);
385910f2 204 /* avoid REGPARM breakage on x86: */
54a01510 205 asmlinkage_protect(2, ret, fd, length);
0a489cb3 206 return ret;
1da177e4 207}
6673e0c3
HC
208#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
209asmlinkage long SyS_ftruncate64(long fd, loff_t length)
210{
211 return SYSC_ftruncate64((unsigned int) fd, length);
212}
213SYSCALL_ALIAS(sys_ftruncate64, SyS_ftruncate64);
1da177e4 214#endif
6673e0c3 215#endif /* BITS_PER_LONG == 32 */
1da177e4 216
3e63cbb1
AJ
217
218int do_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
97ac7350 219{
3e63cbb1
AJ
220 struct inode *inode = file->f_path.dentry->d_inode;
221 long ret;
97ac7350
AA
222
223 if (offset < 0 || len <= 0)
3e63cbb1 224 return -EINVAL;
97ac7350
AA
225
226 /* Return error if mode is not supported */
97ac7350 227 if (mode && !(mode & FALLOC_FL_KEEP_SIZE))
3e63cbb1 228 return -EOPNOTSUPP;
97ac7350 229
97ac7350 230 if (!(file->f_mode & FMODE_WRITE))
3e63cbb1 231 return -EBADF;
97ac7350
AA
232 /*
233 * Revalidate the write permissions, in case security policy has
234 * changed since the files were opened.
235 */
236 ret = security_file_permission(file, MAY_WRITE);
237 if (ret)
3e63cbb1 238 return ret;
97ac7350 239
97ac7350 240 if (S_ISFIFO(inode->i_mode))
3e63cbb1 241 return -ESPIPE;
97ac7350 242
97ac7350
AA
243 /*
244 * Let individual file system decide if it supports preallocation
245 * for directories or not.
246 */
247 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
3e63cbb1 248 return -ENODEV;
97ac7350 249
97ac7350
AA
250 /* Check for wrap through zero too */
251 if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
3e63cbb1 252 return -EFBIG;
97ac7350 253
3e63cbb1
AJ
254 if (!inode->i_op->fallocate)
255 return -EOPNOTSUPP;
97ac7350 256
3e63cbb1
AJ
257 return inode->i_op->fallocate(inode, mode, offset, len);
258}
259
260SYSCALL_DEFINE(fallocate)(int fd, int mode, loff_t offset, loff_t len)
261{
262 struct file *file;
263 int error = -EBADF;
264
265 file = fget(fd);
266 if (file) {
267 error = do_fallocate(file, mode, offset, len);
268 fput(file);
269 }
270
271 return error;
97ac7350 272}
3e63cbb1 273
6673e0c3
HC
274#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
275asmlinkage long SyS_fallocate(long fd, long mode, loff_t offset, loff_t len)
276{
277 return SYSC_fallocate((int)fd, (int)mode, offset, len);
278}
279SYSCALL_ALIAS(sys_fallocate, SyS_fallocate);
280#endif
97ac7350 281
1da177e4
LT
282/*
283 * access() needs to use the real uid/gid, not the effective uid/gid.
284 * We do this by temporarily clearing all FS-related capabilities and
285 * switching the fsuid/fsgid around to the real ones.
286 */
6559eed8 287SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
1da177e4 288{
d84f4f99
DH
289 const struct cred *old_cred;
290 struct cred *override_cred;
2d8f3038 291 struct path path;
256984a8 292 struct inode *inode;
1da177e4
LT
293 int res;
294
295 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
296 return -EINVAL;
297
d84f4f99
DH
298 override_cred = prepare_creds();
299 if (!override_cred)
300 return -ENOMEM;
1da177e4 301
d84f4f99
DH
302 override_cred->fsuid = override_cred->uid;
303 override_cred->fsgid = override_cred->gid;
1da177e4 304
086f7316 305 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
1cdcbec1 306 /* Clear the capabilities if we switch to a non-root user */
d84f4f99
DH
307 if (override_cred->uid)
308 cap_clear(override_cred->cap_effective);
086f7316 309 else
d84f4f99
DH
310 override_cred->cap_effective =
311 override_cred->cap_permitted;
086f7316 312 }
1da177e4 313
d84f4f99
DH
314 old_cred = override_creds(override_cred);
315
2d8f3038 316 res = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path);
6902d925
DH
317 if (res)
318 goto out;
319
2d8f3038 320 inode = path.dentry->d_inode;
256984a8
AV
321
322 if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
30524472
AV
323 /*
324 * MAY_EXEC on regular files is denied if the fs is mounted
325 * with the "noexec" flag.
326 */
327 res = -EACCES;
2d8f3038 328 if (path.mnt->mnt_flags & MNT_NOEXEC)
30524472
AV
329 goto out_path_release;
330 }
331
256984a8 332 res = inode_permission(inode, mode | MAY_ACCESS);
6902d925 333 /* SuS v2 requires we report a read only fs too */
256984a8 334 if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
6902d925 335 goto out_path_release;
2f676cbc
DH
336 /*
337 * This is a rare case where using __mnt_is_readonly()
338 * is OK without a mnt_want/drop_write() pair. Since
339 * no actual write to the fs is performed here, we do
340 * not need to telegraph to that to anyone.
341 *
342 * By doing this, we accept that this access is
343 * inherently racy and know that the fs may change
344 * state before we even see this result.
345 */
2d8f3038 346 if (__mnt_is_readonly(path.mnt))
6902d925 347 res = -EROFS;
1da177e4 348
6902d925 349out_path_release:
2d8f3038 350 path_put(&path);
6902d925 351out:
d84f4f99
DH
352 revert_creds(old_cred);
353 put_cred(override_cred);
1da177e4
LT
354 return res;
355}
356
ca013e94 357SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
5590ff0d
UD
358{
359 return sys_faccessat(AT_FDCWD, filename, mode);
360}
361
3cdad428 362SYSCALL_DEFINE1(chdir, const char __user *, filename)
1da177e4 363{
2d8f3038 364 struct path path;
1da177e4
LT
365 int error;
366
2d8f3038 367 error = user_path_dir(filename, &path);
1da177e4
LT
368 if (error)
369 goto out;
370
2d8f3038 371 error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_ACCESS);
1da177e4
LT
372 if (error)
373 goto dput_and_out;
374
2d8f3038 375 set_fs_pwd(current->fs, &path);
1da177e4
LT
376
377dput_and_out:
2d8f3038 378 path_put(&path);
1da177e4
LT
379out:
380 return error;
381}
382
3cdad428 383SYSCALL_DEFINE1(fchdir, unsigned int, fd)
1da177e4
LT
384{
385 struct file *file;
1da177e4 386 struct inode *inode;
1da177e4
LT
387 int error;
388
389 error = -EBADF;
390 file = fget(fd);
391 if (!file)
392 goto out;
393
ac748a09 394 inode = file->f_path.dentry->d_inode;
1da177e4
LT
395
396 error = -ENOTDIR;
397 if (!S_ISDIR(inode->i_mode))
398 goto out_putf;
399
256984a8 400 error = inode_permission(inode, MAY_EXEC | MAY_ACCESS);
1da177e4 401 if (!error)
ac748a09 402 set_fs_pwd(current->fs, &file->f_path);
1da177e4
LT
403out_putf:
404 fput(file);
405out:
406 return error;
407}
408
3480b257 409SYSCALL_DEFINE1(chroot, const char __user *, filename)
1da177e4 410{
2d8f3038 411 struct path path;
1da177e4
LT
412 int error;
413
2d8f3038 414 error = user_path_dir(filename, &path);
1da177e4
LT
415 if (error)
416 goto out;
417
2d8f3038 418 error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_ACCESS);
1da177e4
LT
419 if (error)
420 goto dput_and_out;
421
422 error = -EPERM;
423 if (!capable(CAP_SYS_CHROOT))
424 goto dput_and_out;
8b8efb44
TH
425 error = security_path_chroot(&path);
426 if (error)
427 goto dput_and_out;
1da177e4 428
2d8f3038 429 set_fs_root(current->fs, &path);
1da177e4
LT
430 error = 0;
431dput_and_out:
2d8f3038 432 path_put(&path);
1da177e4
LT
433out:
434 return error;
435}
436
a26eab24 437SYSCALL_DEFINE2(fchmod, unsigned int, fd, mode_t, mode)
1da177e4
LT
438{
439 struct inode * inode;
440 struct dentry * dentry;
441 struct file * file;
442 int err = -EBADF;
443 struct iattr newattrs;
444
445 file = fget(fd);
446 if (!file)
447 goto out;
448
0f7fc9e4 449 dentry = file->f_path.dentry;
1da177e4
LT
450 inode = dentry->d_inode;
451
5a190ae6 452 audit_inode(NULL, dentry);
73241ccc 453
96029c4e 454 err = mnt_want_write_file(file);
2af482a7 455 if (err)
1da177e4 456 goto out_putf;
fe542cf5 457 mutex_lock(&inode->i_mutex);
89eda068
TH
458 err = security_path_chmod(dentry, file->f_vfsmnt, mode);
459 if (err)
fe542cf5 460 goto out_unlock;
1da177e4
LT
461 if (mode == (mode_t) -1)
462 mode = inode->i_mode;
463 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
464 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
465 err = notify_change(dentry, &newattrs);
fe542cf5 466out_unlock:
1b1dcc1b 467 mutex_unlock(&inode->i_mutex);
2af482a7 468 mnt_drop_write(file->f_path.mnt);
1da177e4
LT
469out_putf:
470 fput(file);
471out:
472 return err;
473}
474
6559eed8 475SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, mode_t, mode)
1da177e4 476{
2d8f3038
AV
477 struct path path;
478 struct inode *inode;
1da177e4
LT
479 int error;
480 struct iattr newattrs;
481
2d8f3038 482 error = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path);
1da177e4
LT
483 if (error)
484 goto out;
2d8f3038 485 inode = path.dentry->d_inode;
1da177e4 486
2d8f3038 487 error = mnt_want_write(path.mnt);
2af482a7 488 if (error)
1da177e4 489 goto dput_and_out;
fe542cf5 490 mutex_lock(&inode->i_mutex);
89eda068
TH
491 error = security_path_chmod(path.dentry, path.mnt, mode);
492 if (error)
fe542cf5 493 goto out_unlock;
1da177e4
LT
494 if (mode == (mode_t) -1)
495 mode = inode->i_mode;
496 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
497 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
2d8f3038 498 error = notify_change(path.dentry, &newattrs);
fe542cf5 499out_unlock:
1b1dcc1b 500 mutex_unlock(&inode->i_mutex);
2d8f3038 501 mnt_drop_write(path.mnt);
1da177e4 502dput_and_out:
2d8f3038 503 path_put(&path);
1da177e4
LT
504out:
505 return error;
506}
507
a26eab24 508SYSCALL_DEFINE2(chmod, const char __user *, filename, mode_t, mode)
5590ff0d
UD
509{
510 return sys_fchmodat(AT_FDCWD, filename, mode);
511}
512
fe542cf5 513static int chown_common(struct path *path, uid_t user, gid_t group)
1da177e4 514{
fe542cf5 515 struct inode *inode = path->dentry->d_inode;
1da177e4
LT
516 int error;
517 struct iattr newattrs;
518
1da177e4
LT
519 newattrs.ia_valid = ATTR_CTIME;
520 if (user != (uid_t) -1) {
521 newattrs.ia_valid |= ATTR_UID;
522 newattrs.ia_uid = user;
523 }
524 if (group != (gid_t) -1) {
525 newattrs.ia_valid |= ATTR_GID;
526 newattrs.ia_gid = group;
527 }
528 if (!S_ISDIR(inode->i_mode))
b5376771
SH
529 newattrs.ia_valid |=
530 ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
1b1dcc1b 531 mutex_lock(&inode->i_mutex);
fe542cf5
TH
532 error = security_path_chown(path, user, group);
533 if (!error)
534 error = notify_change(path->dentry, &newattrs);
1b1dcc1b 535 mutex_unlock(&inode->i_mutex);
beb29e05 536
1da177e4
LT
537 return error;
538}
539
ca013e94 540SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
1da177e4 541{
2d8f3038 542 struct path path;
1da177e4
LT
543 int error;
544
2d8f3038 545 error = user_path(filename, &path);
6902d925
DH
546 if (error)
547 goto out;
2d8f3038 548 error = mnt_want_write(path.mnt);
2af482a7
DH
549 if (error)
550 goto out_release;
fe542cf5 551 error = chown_common(&path, user, group);
2d8f3038 552 mnt_drop_write(path.mnt);
2af482a7 553out_release:
2d8f3038 554 path_put(&path);
6902d925 555out:
1da177e4
LT
556 return error;
557}
558
6559eed8
HC
559SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
560 gid_t, group, int, flag)
5590ff0d 561{
2d8f3038 562 struct path path;
5590ff0d
UD
563 int error = -EINVAL;
564 int follow;
565
566 if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
567 goto out;
568
569 follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
2d8f3038 570 error = user_path_at(dfd, filename, follow, &path);
6902d925
DH
571 if (error)
572 goto out;
2d8f3038 573 error = mnt_want_write(path.mnt);
2af482a7
DH
574 if (error)
575 goto out_release;
fe542cf5 576 error = chown_common(&path, user, group);
2d8f3038 577 mnt_drop_write(path.mnt);
2af482a7 578out_release:
2d8f3038 579 path_put(&path);
5590ff0d
UD
580out:
581 return error;
582}
583
ca013e94 584SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
1da177e4 585{
2d8f3038 586 struct path path;
1da177e4
LT
587 int error;
588
2d8f3038 589 error = user_lpath(filename, &path);
6902d925
DH
590 if (error)
591 goto out;
2d8f3038 592 error = mnt_want_write(path.mnt);
2af482a7
DH
593 if (error)
594 goto out_release;
fe542cf5 595 error = chown_common(&path, user, group);
2d8f3038 596 mnt_drop_write(path.mnt);
2af482a7 597out_release:
2d8f3038 598 path_put(&path);
6902d925 599out:
1da177e4
LT
600 return error;
601}
602
ca013e94 603SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
1da177e4
LT
604{
605 struct file * file;
606 int error = -EBADF;
6902d925 607 struct dentry * dentry;
1da177e4
LT
608
609 file = fget(fd);
6902d925
DH
610 if (!file)
611 goto out;
612
96029c4e 613 error = mnt_want_write_file(file);
2af482a7
DH
614 if (error)
615 goto out_fput;
0f7fc9e4 616 dentry = file->f_path.dentry;
5a190ae6 617 audit_inode(NULL, dentry);
fe542cf5 618 error = chown_common(&file->f_path, user, group);
2af482a7
DH
619 mnt_drop_write(file->f_path.mnt);
620out_fput:
6902d925
DH
621 fput(file);
622out:
1da177e4
LT
623 return error;
624}
625
4a3fd211
DH
626/*
627 * You have to be very careful that these write
628 * counts get cleaned up in error cases and
629 * upon __fput(). This should probably never
630 * be called outside of __dentry_open().
631 */
632static inline int __get_file_write_access(struct inode *inode,
633 struct vfsmount *mnt)
634{
635 int error;
636 error = get_write_access(inode);
637 if (error)
638 return error;
639 /*
640 * Do not take mount writer counts on
641 * special files since no writes to
642 * the mount itself will occur.
643 */
644 if (!special_file(inode->i_mode)) {
645 /*
646 * Balanced in __fput()
647 */
648 error = mnt_want_write(mnt);
649 if (error)
650 put_write_access(inode);
651 }
652 return error;
653}
654
a1a5b3d9 655static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
482928d5 656 struct file *f,
745ca247
DH
657 int (*open)(struct inode *, struct file *),
658 const struct cred *cred)
1da177e4 659{
1da177e4
LT
660 struct inode *inode;
661 int error;
662
5300990c 663 f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
a1a5b3d9 664 FMODE_PREAD | FMODE_PWRITE;
1da177e4
LT
665 inode = dentry->d_inode;
666 if (f->f_mode & FMODE_WRITE) {
4a3fd211 667 error = __get_file_write_access(inode, mnt);
1da177e4
LT
668 if (error)
669 goto cleanup_file;
ad775f5a
DH
670 if (!special_file(inode->i_mode))
671 file_take_write(f);
1da177e4
LT
672 }
673
674 f->f_mapping = inode->i_mapping;
0f7fc9e4
JJS
675 f->f_path.dentry = dentry;
676 f->f_path.mnt = mnt;
1da177e4
LT
677 f->f_pos = 0;
678 f->f_op = fops_get(inode->i_fop);
679 file_move(f, &inode->i_sb->s_files);
680
745ca247 681 error = security_dentry_open(f, cred);
788e7dd4
YN
682 if (error)
683 goto cleanup_all;
684
834f2a4a
TM
685 if (!open && f->f_op)
686 open = f->f_op->open;
687 if (open) {
688 error = open(inode, f);
1da177e4
LT
689 if (error)
690 goto cleanup_all;
691 }
b65a9cfc 692 ima_counts_get(f);
834f2a4a 693
1da177e4
LT
694 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
695
696 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
697
698 /* NB: we're sure to have correct a_ops only after f_op->open */
699 if (f->f_flags & O_DIRECT) {
ceffc078
CO
700 if (!f->f_mapping->a_ops ||
701 ((!f->f_mapping->a_ops->direct_IO) &&
70688e4d 702 (!f->f_mapping->a_ops->get_xip_mem))) {
1da177e4
LT
703 fput(f);
704 f = ERR_PTR(-EINVAL);
705 }
706 }
707
708 return f;
709
710cleanup_all:
711 fops_put(f->f_op);
4a3fd211 712 if (f->f_mode & FMODE_WRITE) {
1da177e4 713 put_write_access(inode);
ad775f5a
DH
714 if (!special_file(inode->i_mode)) {
715 /*
716 * We don't consider this a real
717 * mnt_want/drop_write() pair
718 * because it all happenend right
719 * here, so just reset the state.
720 */
721 file_reset_write(f);
4a3fd211 722 mnt_drop_write(mnt);
ad775f5a 723 }
4a3fd211 724 }
1da177e4 725 file_kill(f);
0f7fc9e4
JJS
726 f->f_path.dentry = NULL;
727 f->f_path.mnt = NULL;
1da177e4
LT
728cleanup_file:
729 put_filp(f);
1da177e4
LT
730 dput(dentry);
731 mntput(mnt);
732 return ERR_PTR(error);
733}
734
834f2a4a
TM
735/**
736 * lookup_instantiate_filp - instantiates the open intent filp
737 * @nd: pointer to nameidata
738 * @dentry: pointer to dentry
739 * @open: open callback
740 *
741 * Helper for filesystems that want to use lookup open intents and pass back
742 * a fully instantiated struct file to the caller.
743 * This function is meant to be called from within a filesystem's
744 * lookup method.
9a56c213
OD
745 * Beware of calling it for non-regular files! Those ->open methods might block
746 * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo,
747 * leading to a deadlock, as nobody can open that fifo anymore, because
748 * another process to open fifo will block on locked parent when doing lookup).
834f2a4a
TM
749 * Note that in case of error, nd->intent.open.file is destroyed, but the
750 * path information remains valid.
751 * If the open callback is set to NULL, then the standard f_op->open()
752 * filesystem callback is substituted.
753 */
754struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
755 int (*open)(struct inode *, struct file *))
756{
745ca247
DH
757 const struct cred *cred = current_cred();
758
834f2a4a
TM
759 if (IS_ERR(nd->intent.open.file))
760 goto out;
761 if (IS_ERR(dentry))
762 goto out_err;
4ac91378 763 nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->path.mnt),
834f2a4a 764 nd->intent.open.file,
745ca247 765 open, cred);
834f2a4a
TM
766out:
767 return nd->intent.open.file;
768out_err:
769 release_open_intent(nd);
770 nd->intent.open.file = (struct file *)dentry;
771 goto out;
772}
773EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
774
775/**
776 * nameidata_to_filp - convert a nameidata to an open filp.
777 * @nd: pointer to nameidata
778 * @flags: open flags
779 *
780 * Note that this function destroys the original nameidata
781 */
482928d5 782struct file *nameidata_to_filp(struct nameidata *nd)
834f2a4a 783{
745ca247 784 const struct cred *cred = current_cred();
834f2a4a
TM
785 struct file *filp;
786
787 /* Pick up the filp from the open intent */
788 filp = nd->intent.open.file;
789 /* Has the filesystem initialised the file for us? */
0f7fc9e4 790 if (filp->f_path.dentry == NULL)
482928d5 791 filp = __dentry_open(nd->path.dentry, nd->path.mnt, filp,
745ca247 792 NULL, cred);
834f2a4a 793 else
1d957f9b 794 path_put(&nd->path);
834f2a4a
TM
795 return filp;
796}
797
6fdcc216
PS
798/*
799 * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
800 * error.
801 */
745ca247
DH
802struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags,
803 const struct cred *cred)
a1a5b3d9
PS
804{
805 int error;
806 struct file *f;
807
e0e81739
DH
808 validate_creds(cred);
809
322ee5b3
CH
810 /*
811 * We must always pass in a valid mount pointer. Historically
812 * callers got away with not passing it, but we must enforce this at
813 * the earliest possible point now to avoid strange problems deep in the
814 * filesystem stack.
815 */
816 if (!mnt) {
817 printk(KERN_WARNING "%s called with NULL vfsmount\n", __func__);
818 dump_stack();
819 return ERR_PTR(-EINVAL);
820 }
821
a1a5b3d9
PS
822 error = -ENFILE;
823 f = get_empty_filp();
6fdcc216
PS
824 if (f == NULL) {
825 dput(dentry);
826 mntput(mnt);
a1a5b3d9 827 return ERR_PTR(error);
6fdcc216 828 }
a1a5b3d9 829
482928d5
AV
830 f->f_flags = flags;
831 return __dentry_open(dentry, mnt, f, NULL, cred);
a1a5b3d9 832}
1da177e4
LT
833EXPORT_SYMBOL(dentry_open);
834
b01ec0ef 835static void __put_unused_fd(struct files_struct *files, unsigned int fd)
1da177e4 836{
badf1662
DS
837 struct fdtable *fdt = files_fdtable(files);
838 __FD_CLR(fd, fdt->open_fds);
0c9e63fd
ED
839 if (fd < files->next_fd)
840 files->next_fd = fd;
1da177e4
LT
841}
842
fc9b52cd 843void put_unused_fd(unsigned int fd)
1da177e4
LT
844{
845 struct files_struct *files = current->files;
846 spin_lock(&files->file_lock);
847 __put_unused_fd(files, fd);
848 spin_unlock(&files->file_lock);
849}
850
851EXPORT_SYMBOL(put_unused_fd);
852
853/*
5590ff0d 854 * Install a file pointer in the fd array.
1da177e4
LT
855 *
856 * The VFS is full of places where we drop the files lock between
857 * setting the open_fds bitmap and installing the file in the file
858 * array. At any such point, we are vulnerable to a dup2() race
859 * installing a file in the array before us. We need to detect this and
860 * fput() the struct file we are about to overwrite in this case.
861 *
862 * It should never happen - if we allow dup2() do it, _really_ bad things
863 * will follow.
864 */
865
fc9b52cd 866void fd_install(unsigned int fd, struct file *file)
1da177e4
LT
867{
868 struct files_struct *files = current->files;
badf1662 869 struct fdtable *fdt;
1da177e4 870 spin_lock(&files->file_lock);
badf1662 871 fdt = files_fdtable(files);
ab2af1f5
DS
872 BUG_ON(fdt->fd[fd] != NULL);
873 rcu_assign_pointer(fdt->fd[fd], file);
1da177e4
LT
874 spin_unlock(&files->file_lock);
875}
876
877EXPORT_SYMBOL(fd_install);
878
5590ff0d 879long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
1da177e4 880{
e922efc3
MS
881 char *tmp = getname(filename);
882 int fd = PTR_ERR(tmp);
1da177e4 883
1da177e4 884 if (!IS_ERR(tmp)) {
f23513e8 885 fd = get_unused_fd_flags(flags);
1da177e4 886 if (fd >= 0) {
6e8341a1 887 struct file *f = do_filp_open(dfd, tmp, flags, mode, 0);
fed2fc18
TN
888 if (IS_ERR(f)) {
889 put_unused_fd(fd);
890 fd = PTR_ERR(f);
891 } else {
0f7fc9e4 892 fsnotify_open(f->f_path.dentry);
fed2fc18
TN
893 fd_install(fd, f);
894 }
1da177e4 895 }
1da177e4
LT
896 putname(tmp);
897 }
898 return fd;
1da177e4 899}
e922efc3 900
ca013e94 901SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, int, mode)
e922efc3 902{
385910f2
LT
903 long ret;
904
e922efc3
MS
905 if (force_o_largefile())
906 flags |= O_LARGEFILE;
907
385910f2
LT
908 ret = do_sys_open(AT_FDCWD, filename, flags, mode);
909 /* avoid REGPARM breakage on x86: */
54a01510 910 asmlinkage_protect(3, ret, filename, flags, mode);
385910f2 911 return ret;
e922efc3 912}
1da177e4 913
6559eed8
HC
914SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
915 int, mode)
5590ff0d 916{
385910f2
LT
917 long ret;
918
5590ff0d
UD
919 if (force_o_largefile())
920 flags |= O_LARGEFILE;
921
385910f2
LT
922 ret = do_sys_open(dfd, filename, flags, mode);
923 /* avoid REGPARM breakage on x86: */
54a01510 924 asmlinkage_protect(4, ret, dfd, filename, flags, mode);
385910f2 925 return ret;
5590ff0d 926}
5590ff0d 927
1da177e4
LT
928#ifndef __alpha__
929
930/*
931 * For backward compatibility? Maybe this should be moved
932 * into arch/i386 instead?
933 */
002c8976 934SYSCALL_DEFINE2(creat, const char __user *, pathname, int, mode)
1da177e4
LT
935{
936 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
937}
938
939#endif
940
941/*
942 * "id" is the POSIX thread ID. We use the
943 * files pointer for this..
944 */
945int filp_close(struct file *filp, fl_owner_t id)
946{
45778ca8 947 int retval = 0;
1da177e4
LT
948
949 if (!file_count(filp)) {
950 printk(KERN_ERR "VFS: Close: file count is 0\n");
45778ca8 951 return 0;
1da177e4
LT
952 }
953
45778ca8 954 if (filp->f_op && filp->f_op->flush)
75e1fcc0 955 retval = filp->f_op->flush(filp, id);
1da177e4
LT
956
957 dnotify_flush(filp, id);
958 locks_remove_posix(filp, id);
959 fput(filp);
960 return retval;
961}
962
963EXPORT_SYMBOL(filp_close);
964
965/*
966 * Careful here! We test whether the file pointer is NULL before
967 * releasing the fd. This ensures that one clone task can't release
968 * an fd while another clone is opening it.
969 */
ca013e94 970SYSCALL_DEFINE1(close, unsigned int, fd)
1da177e4
LT
971{
972 struct file * filp;
973 struct files_struct *files = current->files;
badf1662 974 struct fdtable *fdt;
ee731f4f 975 int retval;
1da177e4
LT
976
977 spin_lock(&files->file_lock);
badf1662
DS
978 fdt = files_fdtable(files);
979 if (fd >= fdt->max_fds)
1da177e4 980 goto out_unlock;
badf1662 981 filp = fdt->fd[fd];
1da177e4
LT
982 if (!filp)
983 goto out_unlock;
ab2af1f5 984 rcu_assign_pointer(fdt->fd[fd], NULL);
badf1662 985 FD_CLR(fd, fdt->close_on_exec);
1da177e4
LT
986 __put_unused_fd(files, fd);
987 spin_unlock(&files->file_lock);
ee731f4f
EP
988 retval = filp_close(filp, files);
989
990 /* can't restart close syscall because file table entry was cleared */
991 if (unlikely(retval == -ERESTARTSYS ||
992 retval == -ERESTARTNOINTR ||
993 retval == -ERESTARTNOHAND ||
994 retval == -ERESTART_RESTARTBLOCK))
995 retval = -EINTR;
996
997 return retval;
1da177e4
LT
998
999out_unlock:
1000 spin_unlock(&files->file_lock);
1001 return -EBADF;
1002}
1da177e4
LT
1003EXPORT_SYMBOL(sys_close);
1004
1005/*
1006 * This routine simulates a hangup on the tty, to arrange that users
1007 * are given clean terminals at login time.
1008 */
ca013e94 1009SYSCALL_DEFINE0(vhangup)
1da177e4
LT
1010{
1011 if (capable(CAP_SYS_TTY_CONFIG)) {
2cb5998b 1012 tty_vhangup_self();
1da177e4
LT
1013 return 0;
1014 }
1015 return -EPERM;
1016}
1017
1018/*
1019 * Called when an inode is about to be open.
1020 * We use this to disallow opening large files on 32bit systems if
1021 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1022 * on this flag in sys_open.
1023 */
1024int generic_file_open(struct inode * inode, struct file * filp)
1025{
1026 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
a9c62a18 1027 return -EOVERFLOW;
1da177e4
LT
1028 return 0;
1029}
1030
1031EXPORT_SYMBOL(generic_file_open);
1032
1033/*
1034 * This is used by subsystems that don't want seekable
1035 * file descriptors
1036 */
1037int nonseekable_open(struct inode *inode, struct file *filp)
1038{
1039 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1040 return 0;
1041}
1042
1043EXPORT_SYMBOL(nonseekable_open);