Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/file.h> |
Al Viro | 9f3acc3 | 2008-04-24 07:44:08 -0400 | [diff] [blame] | 10 | #include <linux/fdtable.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 11 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/tty.h> |
| 14 | #include <linux/namei.h> |
| 15 | #include <linux/backing-dev.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 16 | #include <linux/capability.h> |
Andrew G. Morgan | 086f731 | 2008-07-04 09:59:58 -0700 | [diff] [blame] | 17 | #include <linux/securebits.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/security.h> |
| 19 | #include <linux/mount.h> |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 20 | #include <linux/fcntl.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <asm/uaccess.h> |
| 23 | #include <linux/fs.h> |
Yoav Zach | ef3daed | 2005-06-23 00:09:58 -0700 | [diff] [blame] | 24 | #include <linux/personality.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/pagemap.h> |
| 26 | #include <linux/syscalls.h> |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 27 | #include <linux/rcupdate.h> |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 28 | #include <linux/audit.h> |
Amit Arora | 97ac735 | 2007-07-17 21:42:44 -0400 | [diff] [blame] | 29 | #include <linux/falloc.h> |
Al Viro | 5ad4e53 | 2009-03-29 19:50:06 -0400 | [diff] [blame] | 30 | #include <linux/fs_struct.h> |
Al Viro | b65a9cf | 2009-12-16 06:27:40 -0500 | [diff] [blame] | 31 | #include <linux/ima.h> |
Eric Paris | 2dfc1ca | 2009-12-17 20:30:52 -0500 | [diff] [blame] | 32 | #include <linux/dnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Eric Paris | e81e3f4 | 2009-12-04 15:47:36 -0500 | [diff] [blame] | 34 | #include "internal.h" |
| 35 | |
Daniel Rosenberg | e95cb22 | 2016-10-26 16:33:11 -0700 | [diff] [blame] | 36 | int do_truncate2(struct vfsmount *mnt, struct dentry *dentry, loff_t length, |
| 37 | unsigned int time_attrs, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | { |
Amerigo Wang | 939a942 | 2009-08-20 19:29:03 -0700 | [diff] [blame] | 39 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
NeilBrown | 4a30131 | 2006-01-08 01:02:39 -0800 | [diff] [blame] | 47 | newattrs.ia_valid = ATTR_SIZE | time_attrs; |
Miklos Szeredi | cc4e69d | 2005-11-07 00:59:49 -0800 | [diff] [blame] | 48 | if (filp) { |
| 49 | newattrs.ia_file = filp; |
| 50 | newattrs.ia_valid |= ATTR_FILE; |
| 51 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Linus Torvalds | 7b82dc0 | 2007-05-08 20:10:00 -0700 | [diff] [blame] | 53 | /* Remove suid/sgid on truncate too */ |
Amerigo Wang | 939a942 | 2009-08-20 19:29:03 -0700 | [diff] [blame] | 54 | ret = should_remove_suid(dentry); |
| 55 | if (ret) |
| 56 | newattrs.ia_valid |= ret | ATTR_FORCE; |
Linus Torvalds | 7b82dc0 | 2007-05-08 20:10:00 -0700 | [diff] [blame] | 57 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 58 | mutex_lock(&dentry->d_inode->i_mutex); |
Daniel Rosenberg | e95cb22 | 2016-10-26 16:33:11 -0700 | [diff] [blame] | 59 | ret = notify_change2(mnt, dentry, &newattrs); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 60 | mutex_unlock(&dentry->d_inode->i_mutex); |
Amerigo Wang | 939a942 | 2009-08-20 19:29:03 -0700 | [diff] [blame] | 61 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } |
Daniel Rosenberg | e95cb22 | 2016-10-26 16:33:11 -0700 | [diff] [blame] | 63 | int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs, |
| 64 | struct file *filp) |
| 65 | { |
| 66 | return do_truncate2(NULL, dentry, length, time_attrs, filp); |
| 67 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 69 | static long do_sys_truncate(const char __user *pathname, loff_t length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 71 | struct path path; |
| 72 | struct inode *inode; |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 73 | struct vfsmount *mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | int error; |
| 75 | |
| 76 | error = -EINVAL; |
| 77 | if (length < 0) /* sorry, but loff_t says... */ |
| 78 | goto out; |
| 79 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 80 | error = user_path(pathname, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | if (error) |
| 82 | goto out; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 83 | inode = path.dentry->d_inode; |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 84 | mnt = path.mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | /* For directories it's -EISDIR, for other non-regulars - -EINVAL */ |
| 87 | error = -EISDIR; |
| 88 | if (S_ISDIR(inode->i_mode)) |
| 89 | goto dput_and_out; |
| 90 | |
| 91 | error = -EINVAL; |
| 92 | if (!S_ISREG(inode->i_mode)) |
| 93 | goto dput_and_out; |
| 94 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 95 | error = mnt_want_write(path.mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | if (error) |
| 97 | goto dput_and_out; |
| 98 | |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 99 | error = inode_permission2(mnt, inode, MAY_WRITE); |
Dave Hansen | 9ac9b84 | 2008-02-15 14:37:52 -0800 | [diff] [blame] | 100 | if (error) |
| 101 | goto mnt_drop_write_and_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
| 103 | error = -EPERM; |
Miklos Szeredi | c82e42d | 2008-06-24 16:50:12 +0200 | [diff] [blame] | 104 | if (IS_APPEND(inode)) |
Dave Hansen | 9ac9b84 | 2008-02-15 14:37:52 -0800 | [diff] [blame] | 105 | goto mnt_drop_write_and_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | error = get_write_access(inode); |
| 108 | if (error) |
Dave Hansen | 9ac9b84 | 2008-02-15 14:37:52 -0800 | [diff] [blame] | 109 | goto mnt_drop_write_and_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
david m. richter | 9700382 | 2007-07-31 00:39:12 -0700 | [diff] [blame] | 111 | /* |
| 112 | * Make sure that there are no leases. get_write_access() protects |
| 113 | * against the truncate racing with a lease-granting setlease(). |
| 114 | */ |
Al Viro | 8737c93 | 2009-12-24 06:47:55 -0500 | [diff] [blame] | 115 | error = break_lease(inode, O_WRONLY); |
david m. richter | 9700382 | 2007-07-31 00:39:12 -0700 | [diff] [blame] | 116 | if (error) |
| 117 | goto put_write_and_out; |
| 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | error = locks_verify_truncate(inode, NULL, length); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 120 | if (!error) |
Tetsuo Handa | ea0d3ab | 2010-06-02 13:24:43 +0900 | [diff] [blame] | 121 | error = security_path_truncate(&path); |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 122 | if (!error) |
Daniel Rosenberg | e95cb22 | 2016-10-26 16:33:11 -0700 | [diff] [blame] | 123 | error = do_truncate2(mnt, path.dentry, length, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
david m. richter | 9700382 | 2007-07-31 00:39:12 -0700 | [diff] [blame] | 125 | put_write_and_out: |
| 126 | put_write_access(inode); |
Dave Hansen | 9ac9b84 | 2008-02-15 14:37:52 -0800 | [diff] [blame] | 127 | mnt_drop_write_and_out: |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 128 | mnt_drop_write(path.mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | dput_and_out: |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 130 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | out: |
| 132 | return error; |
| 133 | } |
| 134 | |
Heiko Carstens | 4fd8da8 | 2009-09-23 17:49:55 +0200 | [diff] [blame] | 135 | SYSCALL_DEFINE2(truncate, const char __user *, path, long, length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
Heiko Carstens | 4fd8da8 | 2009-09-23 17:49:55 +0200 | [diff] [blame] | 137 | return do_sys_truncate(path, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Matt Mackall | b01ec0e | 2006-01-08 01:05:20 -0800 | [diff] [blame] | 140 | static long do_sys_ftruncate(unsigned int fd, loff_t length, int small) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | { |
| 142 | struct inode * inode; |
| 143 | struct dentry *dentry; |
Daniel Rosenberg | e95cb22 | 2016-10-26 16:33:11 -0700 | [diff] [blame] | 144 | struct vfsmount *mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | struct file * file; |
| 146 | int error; |
| 147 | |
| 148 | error = -EINVAL; |
| 149 | if (length < 0) |
| 150 | goto out; |
| 151 | error = -EBADF; |
| 152 | file = fget(fd); |
| 153 | if (!file) |
| 154 | goto out; |
| 155 | |
| 156 | /* explicitly opened as large or we are on 64-bit box */ |
| 157 | if (file->f_flags & O_LARGEFILE) |
| 158 | small = 0; |
| 159 | |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 160 | dentry = file->f_path.dentry; |
Daniel Rosenberg | e95cb22 | 2016-10-26 16:33:11 -0700 | [diff] [blame] | 161 | mnt = file->f_path.mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | inode = dentry->d_inode; |
| 163 | error = -EINVAL; |
| 164 | if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE)) |
| 165 | goto out_putf; |
| 166 | |
| 167 | error = -EINVAL; |
| 168 | /* Cannot ftruncate over 2^31 bytes without large file support */ |
| 169 | if (small && length > MAX_NON_LFS) |
| 170 | goto out_putf; |
| 171 | |
| 172 | error = -EPERM; |
| 173 | if (IS_APPEND(inode)) |
| 174 | goto out_putf; |
| 175 | |
| 176 | error = locks_verify_truncate(inode, file, length); |
| 177 | if (!error) |
Tetsuo Handa | ea0d3ab | 2010-06-02 13:24:43 +0900 | [diff] [blame] | 178 | error = security_path_truncate(&file->f_path); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 179 | if (!error) |
Daniel Rosenberg | e95cb22 | 2016-10-26 16:33:11 -0700 | [diff] [blame] | 180 | error = do_truncate2(mnt, dentry, length, ATTR_MTIME|ATTR_CTIME, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | out_putf: |
| 182 | fput(file); |
| 183 | out: |
| 184 | return error; |
| 185 | } |
| 186 | |
Heiko Carstens | bdc480e | 2009-01-14 14:14:12 +0100 | [diff] [blame] | 187 | SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | { |
Linus Torvalds | 0a489cb | 2006-04-18 13:02:48 -0700 | [diff] [blame] | 189 | long ret = do_sys_ftruncate(fd, length, 1); |
Linus Torvalds | 385910f | 2006-04-18 13:22:59 -0700 | [diff] [blame] | 190 | /* avoid REGPARM breakage on x86: */ |
Roland McGrath | 54a0151 | 2008-04-10 15:37:38 -0700 | [diff] [blame] | 191 | asmlinkage_protect(2, ret, fd, length); |
Linus Torvalds | 0a489cb | 2006-04-18 13:02:48 -0700 | [diff] [blame] | 192 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /* LFS versions of truncate are only needed on 32 bit machines */ |
| 196 | #if BITS_PER_LONG == 32 |
Heiko Carstens | 6673e0c | 2009-01-14 14:14:02 +0100 | [diff] [blame] | 197 | SYSCALL_DEFINE(truncate64)(const char __user * path, loff_t length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
| 199 | return do_sys_truncate(path, length); |
| 200 | } |
Heiko Carstens | 6673e0c | 2009-01-14 14:14:02 +0100 | [diff] [blame] | 201 | #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS |
| 202 | asmlinkage long SyS_truncate64(long path, loff_t length) |
| 203 | { |
| 204 | return SYSC_truncate64((const char __user *) path, length); |
| 205 | } |
| 206 | SYSCALL_ALIAS(sys_truncate64, SyS_truncate64); |
| 207 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Heiko Carstens | 6673e0c | 2009-01-14 14:14:02 +0100 | [diff] [blame] | 209 | SYSCALL_DEFINE(ftruncate64)(unsigned int fd, loff_t length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { |
Linus Torvalds | 0a489cb | 2006-04-18 13:02:48 -0700 | [diff] [blame] | 211 | long ret = do_sys_ftruncate(fd, length, 0); |
Linus Torvalds | 385910f | 2006-04-18 13:22:59 -0700 | [diff] [blame] | 212 | /* avoid REGPARM breakage on x86: */ |
Roland McGrath | 54a0151 | 2008-04-10 15:37:38 -0700 | [diff] [blame] | 213 | asmlinkage_protect(2, ret, fd, length); |
Linus Torvalds | 0a489cb | 2006-04-18 13:02:48 -0700 | [diff] [blame] | 214 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
Heiko Carstens | 6673e0c | 2009-01-14 14:14:02 +0100 | [diff] [blame] | 216 | #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS |
| 217 | asmlinkage long SyS_ftruncate64(long fd, loff_t length) |
| 218 | { |
| 219 | return SYSC_ftruncate64((unsigned int) fd, length); |
| 220 | } |
| 221 | SYSCALL_ALIAS(sys_ftruncate64, SyS_ftruncate64); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | #endif |
Heiko Carstens | 6673e0c | 2009-01-14 14:14:02 +0100 | [diff] [blame] | 223 | #endif /* BITS_PER_LONG == 32 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
Ankit Jain | 3e63cbb | 2009-06-19 14:28:07 -0400 | [diff] [blame] | 225 | |
| 226 | int do_fallocate(struct file *file, int mode, loff_t offset, loff_t len) |
Amit Arora | 97ac735 | 2007-07-17 21:42:44 -0400 | [diff] [blame] | 227 | { |
Ankit Jain | 3e63cbb | 2009-06-19 14:28:07 -0400 | [diff] [blame] | 228 | struct inode *inode = file->f_path.dentry->d_inode; |
| 229 | long ret; |
Amit Arora | 97ac735 | 2007-07-17 21:42:44 -0400 | [diff] [blame] | 230 | |
| 231 | if (offset < 0 || len <= 0) |
Ankit Jain | 3e63cbb | 2009-06-19 14:28:07 -0400 | [diff] [blame] | 232 | return -EINVAL; |
Amit Arora | 97ac735 | 2007-07-17 21:42:44 -0400 | [diff] [blame] | 233 | |
| 234 | /* Return error if mode is not supported */ |
Josef Bacik | 79124f1 | 2010-11-17 20:46:15 -0500 | [diff] [blame] | 235 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) |
| 236 | return -EOPNOTSUPP; |
| 237 | |
| 238 | /* Punch hole must have keep size set */ |
| 239 | if ((mode & FALLOC_FL_PUNCH_HOLE) && |
| 240 | !(mode & FALLOC_FL_KEEP_SIZE)) |
Ankit Jain | 3e63cbb | 2009-06-19 14:28:07 -0400 | [diff] [blame] | 241 | return -EOPNOTSUPP; |
Amit Arora | 97ac735 | 2007-07-17 21:42:44 -0400 | [diff] [blame] | 242 | |
Amit Arora | 97ac735 | 2007-07-17 21:42:44 -0400 | [diff] [blame] | 243 | if (!(file->f_mode & FMODE_WRITE)) |
Ankit Jain | 3e63cbb | 2009-06-19 14:28:07 -0400 | [diff] [blame] | 244 | return -EBADF; |
Marco Stornelli | 1ca551c | 2011-03-05 11:10:19 +0100 | [diff] [blame] | 245 | |
| 246 | /* It's not possible punch hole on append only file */ |
| 247 | if (mode & FALLOC_FL_PUNCH_HOLE && IS_APPEND(inode)) |
| 248 | return -EPERM; |
| 249 | |
| 250 | if (IS_IMMUTABLE(inode)) |
| 251 | return -EPERM; |
| 252 | |
Amit Arora | 97ac735 | 2007-07-17 21:42:44 -0400 | [diff] [blame] | 253 | /* |
| 254 | * Revalidate the write permissions, in case security policy has |
| 255 | * changed since the files were opened. |
| 256 | */ |
| 257 | ret = security_file_permission(file, MAY_WRITE); |
| 258 | if (ret) |
Ankit Jain | 3e63cbb | 2009-06-19 14:28:07 -0400 | [diff] [blame] | 259 | return ret; |
Amit Arora | 97ac735 | 2007-07-17 21:42:44 -0400 | [diff] [blame] | 260 | |
Amit Arora | 97ac735 | 2007-07-17 21:42:44 -0400 | [diff] [blame] | 261 | if (S_ISFIFO(inode->i_mode)) |
Ankit Jain | 3e63cbb | 2009-06-19 14:28:07 -0400 | [diff] [blame] | 262 | return -ESPIPE; |
Amit Arora | 97ac735 | 2007-07-17 21:42:44 -0400 | [diff] [blame] | 263 | |
Amit Arora | 97ac735 | 2007-07-17 21:42:44 -0400 | [diff] [blame] | 264 | /* |
| 265 | * Let individual file system decide if it supports preallocation |
| 266 | * for directories or not. |
| 267 | */ |
| 268 | if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode)) |
Ankit Jain | 3e63cbb | 2009-06-19 14:28:07 -0400 | [diff] [blame] | 269 | return -ENODEV; |
Amit Arora | 97ac735 | 2007-07-17 21:42:44 -0400 | [diff] [blame] | 270 | |
Amit Arora | 97ac735 | 2007-07-17 21:42:44 -0400 | [diff] [blame] | 271 | /* Check for wrap through zero too */ |
| 272 | if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0)) |
Ankit Jain | 3e63cbb | 2009-06-19 14:28:07 -0400 | [diff] [blame] | 273 | return -EFBIG; |
Amit Arora | 97ac735 | 2007-07-17 21:42:44 -0400 | [diff] [blame] | 274 | |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 275 | if (!file->f_op->fallocate) |
Ankit Jain | 3e63cbb | 2009-06-19 14:28:07 -0400 | [diff] [blame] | 276 | return -EOPNOTSUPP; |
Amit Arora | 97ac735 | 2007-07-17 21:42:44 -0400 | [diff] [blame] | 277 | |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 278 | return file->f_op->fallocate(file, mode, offset, len); |
Amit Arora | 97ac735 | 2007-07-17 21:42:44 -0400 | [diff] [blame] | 279 | } |
Ankit Jain | 3e63cbb | 2009-06-19 14:28:07 -0400 | [diff] [blame] | 280 | |
| 281 | SYSCALL_DEFINE(fallocate)(int fd, int mode, loff_t offset, loff_t len) |
| 282 | { |
| 283 | struct file *file; |
| 284 | int error = -EBADF; |
| 285 | |
| 286 | file = fget(fd); |
| 287 | if (file) { |
| 288 | error = do_fallocate(file, mode, offset, len); |
| 289 | fput(file); |
| 290 | } |
| 291 | |
| 292 | return error; |
| 293 | } |
| 294 | |
Heiko Carstens | 6673e0c | 2009-01-14 14:14:02 +0100 | [diff] [blame] | 295 | #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS |
| 296 | asmlinkage long SyS_fallocate(long fd, long mode, loff_t offset, loff_t len) |
| 297 | { |
| 298 | return SYSC_fallocate((int)fd, (int)mode, offset, len); |
| 299 | } |
| 300 | SYSCALL_ALIAS(sys_fallocate, SyS_fallocate); |
| 301 | #endif |
Amit Arora | 97ac735 | 2007-07-17 21:42:44 -0400 | [diff] [blame] | 302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | /* |
| 304 | * access() needs to use the real uid/gid, not the effective uid/gid. |
| 305 | * We do this by temporarily clearing all FS-related capabilities and |
| 306 | * switching the fsuid/fsgid around to the real ones. |
| 307 | */ |
Heiko Carstens | 6559eed8 | 2009-01-14 14:14:32 +0100 | [diff] [blame] | 308 | SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 310 | const struct cred *old_cred; |
| 311 | struct cred *override_cred; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 312 | struct path path; |
Al Viro | 256984a | 2008-07-22 08:09:30 -0400 | [diff] [blame] | 313 | struct inode *inode; |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 314 | struct vfsmount *mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | int res; |
| 316 | |
| 317 | if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */ |
| 318 | return -EINVAL; |
| 319 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 320 | override_cred = prepare_creds(); |
| 321 | if (!override_cred) |
| 322 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 324 | override_cred->fsuid = override_cred->uid; |
| 325 | override_cred->fsgid = override_cred->gid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
Andrew G. Morgan | 086f731 | 2008-07-04 09:59:58 -0700 | [diff] [blame] | 327 | if (!issecure(SECURE_NO_SETUID_FIXUP)) { |
David Howells | 1cdcbec | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 328 | /* Clear the capabilities if we switch to a non-root user */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 329 | if (override_cred->uid) |
| 330 | cap_clear(override_cred->cap_effective); |
Andrew G. Morgan | 086f731 | 2008-07-04 09:59:58 -0700 | [diff] [blame] | 331 | else |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 332 | override_cred->cap_effective = |
| 333 | override_cred->cap_permitted; |
Andrew G. Morgan | 086f731 | 2008-07-04 09:59:58 -0700 | [diff] [blame] | 334 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 336 | old_cred = override_creds(override_cred); |
| 337 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 338 | res = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 339 | if (res) |
| 340 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 342 | inode = path.dentry->d_inode; |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 343 | mnt = path.mnt; |
Al Viro | 256984a | 2008-07-22 08:09:30 -0400 | [diff] [blame] | 344 | |
| 345 | if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) { |
Al Viro | 3052447 | 2008-07-22 00:02:33 -0400 | [diff] [blame] | 346 | /* |
| 347 | * MAY_EXEC on regular files is denied if the fs is mounted |
| 348 | * with the "noexec" flag. |
| 349 | */ |
| 350 | res = -EACCES; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 351 | if (path.mnt->mnt_flags & MNT_NOEXEC) |
Al Viro | 3052447 | 2008-07-22 00:02:33 -0400 | [diff] [blame] | 352 | goto out_path_release; |
| 353 | } |
| 354 | |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 355 | res = inode_permission2(mnt, inode, mode | MAY_ACCESS); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 356 | /* SuS v2 requires we report a read only fs too */ |
Al Viro | 256984a | 2008-07-22 08:09:30 -0400 | [diff] [blame] | 357 | if (res || !(mode & S_IWOTH) || special_file(inode->i_mode)) |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 358 | goto out_path_release; |
Dave Hansen | 2f676cb | 2008-02-15 14:37:55 -0800 | [diff] [blame] | 359 | /* |
| 360 | * This is a rare case where using __mnt_is_readonly() |
| 361 | * is OK without a mnt_want/drop_write() pair. Since |
| 362 | * no actual write to the fs is performed here, we do |
| 363 | * not need to telegraph to that to anyone. |
| 364 | * |
| 365 | * By doing this, we accept that this access is |
| 366 | * inherently racy and know that the fs may change |
| 367 | * state before we even see this result. |
| 368 | */ |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 369 | if (__mnt_is_readonly(path.mnt)) |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 370 | res = -EROFS; |
| 371 | |
| 372 | out_path_release: |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 373 | path_put(&path); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 374 | out: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 375 | revert_creds(old_cred); |
| 376 | put_cred(override_cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | return res; |
| 378 | } |
| 379 | |
Heiko Carstens | ca013e9 | 2009-01-14 14:14:19 +0100 | [diff] [blame] | 380 | SYSCALL_DEFINE2(access, const char __user *, filename, int, mode) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 381 | { |
| 382 | return sys_faccessat(AT_FDCWD, filename, mode); |
| 383 | } |
| 384 | |
Heiko Carstens | 3cdad42 | 2009-01-14 14:14:22 +0100 | [diff] [blame] | 385 | SYSCALL_DEFINE1(chdir, const char __user *, filename) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 387 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | int error; |
| 389 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 390 | error = user_path_dir(filename, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | if (error) |
| 392 | goto out; |
| 393 | |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 394 | error = inode_permission2(path.mnt, path.dentry->d_inode, MAY_EXEC | MAY_CHDIR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | if (error) |
| 396 | goto dput_and_out; |
| 397 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 398 | set_fs_pwd(current->fs, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
| 400 | dput_and_out: |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 401 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | out: |
| 403 | return error; |
| 404 | } |
| 405 | |
Heiko Carstens | 3cdad42 | 2009-01-14 14:14:22 +0100 | [diff] [blame] | 406 | SYSCALL_DEFINE1(fchdir, unsigned int, fd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { |
| 408 | struct file *file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | struct inode *inode; |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 410 | struct vfsmount *mnt; |
Linus Torvalds | 59ae476 | 2012-07-07 10:17:00 -0700 | [diff] [blame] | 411 | int error, fput_needed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
| 413 | error = -EBADF; |
Linus Torvalds | 59ae476 | 2012-07-07 10:17:00 -0700 | [diff] [blame] | 414 | file = fget_raw_light(fd, &fput_needed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | if (!file) |
| 416 | goto out; |
| 417 | |
Jan Blunck | ac748a0 | 2008-02-14 19:34:39 -0800 | [diff] [blame] | 418 | inode = file->f_path.dentry->d_inode; |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 419 | mnt = file->f_path.mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
| 421 | error = -ENOTDIR; |
| 422 | if (!S_ISDIR(inode->i_mode)) |
| 423 | goto out_putf; |
| 424 | |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 425 | error = inode_permission2(mnt, inode, MAY_EXEC | MAY_CHDIR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | if (!error) |
Jan Blunck | ac748a0 | 2008-02-14 19:34:39 -0800 | [diff] [blame] | 427 | set_fs_pwd(current->fs, &file->f_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | out_putf: |
Linus Torvalds | 59ae476 | 2012-07-07 10:17:00 -0700 | [diff] [blame] | 429 | fput_light(file, fput_needed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | out: |
| 431 | return error; |
| 432 | } |
| 433 | |
Heiko Carstens | 3480b25 | 2009-01-14 14:14:16 +0100 | [diff] [blame] | 434 | SYSCALL_DEFINE1(chroot, const char __user *, filename) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 436 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | int error; |
| 438 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 439 | error = user_path_dir(filename, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | if (error) |
| 441 | goto out; |
| 442 | |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 443 | error = inode_permission2(path.mnt, path.dentry->d_inode, MAY_EXEC | MAY_CHDIR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | if (error) |
| 445 | goto dput_and_out; |
| 446 | |
| 447 | error = -EPERM; |
| 448 | if (!capable(CAP_SYS_CHROOT)) |
| 449 | goto dput_and_out; |
Tetsuo Handa | 8b8efb4 | 2009-10-04 21:49:48 +0900 | [diff] [blame] | 450 | error = security_path_chroot(&path); |
| 451 | if (error) |
| 452 | goto dput_and_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 454 | set_fs_root(current->fs, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | error = 0; |
| 456 | dput_and_out: |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 457 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | out: |
| 459 | return error; |
| 460 | } |
| 461 | |
Al Viro | e57712e | 2011-07-26 04:15:54 -0400 | [diff] [blame] | 462 | static int chmod_common(struct path *path, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | { |
Al Viro | e57712e | 2011-07-26 04:15:54 -0400 | [diff] [blame] | 464 | struct inode *inode = path->dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | struct iattr newattrs; |
Al Viro | e57712e | 2011-07-26 04:15:54 -0400 | [diff] [blame] | 466 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | |
Al Viro | e57712e | 2011-07-26 04:15:54 -0400 | [diff] [blame] | 468 | error = mnt_want_write(path->mnt); |
| 469 | if (error) |
| 470 | return error; |
Tetsuo Handa | fe542cf | 2009-11-22 11:49:55 +0900 | [diff] [blame] | 471 | mutex_lock(&inode->i_mutex); |
Al Viro | cdcf116 | 2011-12-08 10:51:53 -0500 | [diff] [blame] | 472 | error = security_path_chmod(path, mode); |
Al Viro | e57712e | 2011-07-26 04:15:54 -0400 | [diff] [blame] | 473 | if (error) |
Tetsuo Handa | fe542cf | 2009-11-22 11:49:55 +0900 | [diff] [blame] | 474 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO); |
| 476 | newattrs.ia_valid = ATTR_MODE | ATTR_CTIME; |
Daniel Rosenberg | e95cb22 | 2016-10-26 16:33:11 -0700 | [diff] [blame] | 477 | error = notify_change2(path->mnt, path->dentry, &newattrs); |
Tetsuo Handa | fe542cf | 2009-11-22 11:49:55 +0900 | [diff] [blame] | 478 | out_unlock: |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 479 | mutex_unlock(&inode->i_mutex); |
Al Viro | e57712e | 2011-07-26 04:15:54 -0400 | [diff] [blame] | 480 | mnt_drop_write(path->mnt); |
| 481 | return error; |
| 482 | } |
| 483 | |
Al Viro | 49f0a07 | 2011-07-26 04:22:01 -0400 | [diff] [blame] | 484 | SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode) |
Al Viro | e57712e | 2011-07-26 04:15:54 -0400 | [diff] [blame] | 485 | { |
| 486 | struct file * file; |
| 487 | int err = -EBADF; |
| 488 | |
| 489 | file = fget(fd); |
| 490 | if (file) { |
| 491 | audit_inode(NULL, file->f_path.dentry); |
| 492 | err = chmod_common(&file->f_path, mode); |
| 493 | fput(file); |
| 494 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | return err; |
| 496 | } |
| 497 | |
Al Viro | 49f0a07 | 2011-07-26 04:22:01 -0400 | [diff] [blame] | 498 | SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, umode_t, mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 500 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 503 | error = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path); |
Al Viro | e57712e | 2011-07-26 04:15:54 -0400 | [diff] [blame] | 504 | if (!error) { |
| 505 | error = chmod_common(&path, mode); |
| 506 | path_put(&path); |
| 507 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | return error; |
| 509 | } |
| 510 | |
Al Viro | 49f0a07 | 2011-07-26 04:22:01 -0400 | [diff] [blame] | 511 | SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 512 | { |
| 513 | return sys_fchmodat(AT_FDCWD, filename, mode); |
| 514 | } |
| 515 | |
Tetsuo Handa | fe542cf | 2009-11-22 11:49:55 +0900 | [diff] [blame] | 516 | static int chown_common(struct path *path, uid_t user, gid_t group) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | { |
Tetsuo Handa | fe542cf | 2009-11-22 11:49:55 +0900 | [diff] [blame] | 518 | struct inode *inode = path->dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | int error; |
| 520 | struct iattr newattrs; |
| 521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | newattrs.ia_valid = ATTR_CTIME; |
| 523 | if (user != (uid_t) -1) { |
| 524 | newattrs.ia_valid |= ATTR_UID; |
| 525 | newattrs.ia_uid = user; |
| 526 | } |
| 527 | if (group != (gid_t) -1) { |
| 528 | newattrs.ia_valid |= ATTR_GID; |
| 529 | newattrs.ia_gid = group; |
| 530 | } |
| 531 | if (!S_ISDIR(inode->i_mode)) |
Serge E. Hallyn | b537677 | 2007-10-16 23:31:36 -0700 | [diff] [blame] | 532 | newattrs.ia_valid |= |
| 533 | ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV; |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 534 | mutex_lock(&inode->i_mutex); |
Tetsuo Handa | fe542cf | 2009-11-22 11:49:55 +0900 | [diff] [blame] | 535 | error = security_path_chown(path, user, group); |
| 536 | if (!error) |
Daniel Rosenberg | e95cb22 | 2016-10-26 16:33:11 -0700 | [diff] [blame] | 537 | error = notify_change2(path->mnt, path->dentry, &newattrs); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 538 | mutex_unlock(&inode->i_mutex); |
Miklos Szeredi | beb29e0 | 2008-07-01 15:01:29 +0200 | [diff] [blame] | 539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | return error; |
| 541 | } |
| 542 | |
Heiko Carstens | ca013e9 | 2009-01-14 14:14:19 +0100 | [diff] [blame] | 543 | SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 545 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | int error; |
| 547 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 548 | error = user_path(filename, &path); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 549 | if (error) |
| 550 | goto out; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 551 | error = mnt_want_write(path.mnt); |
Dave Hansen | 2af482a | 2008-02-15 14:37:50 -0800 | [diff] [blame] | 552 | if (error) |
| 553 | goto out_release; |
Tetsuo Handa | fe542cf | 2009-11-22 11:49:55 +0900 | [diff] [blame] | 554 | error = chown_common(&path, user, group); |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 555 | mnt_drop_write(path.mnt); |
Dave Hansen | 2af482a | 2008-02-15 14:37:50 -0800 | [diff] [blame] | 556 | out_release: |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 557 | path_put(&path); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 558 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | return error; |
| 560 | } |
| 561 | |
Heiko Carstens | 6559eed8 | 2009-01-14 14:14:32 +0100 | [diff] [blame] | 562 | SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user, |
| 563 | gid_t, group, int, flag) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 564 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 565 | struct path path; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 566 | int error = -EINVAL; |
Al Viro | 65cfc67 | 2011-03-13 15:56:26 -0400 | [diff] [blame] | 567 | int lookup_flags; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 568 | |
Al Viro | 65cfc67 | 2011-03-13 15:56:26 -0400 | [diff] [blame] | 569 | if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 570 | goto out; |
| 571 | |
Al Viro | 65cfc67 | 2011-03-13 15:56:26 -0400 | [diff] [blame] | 572 | lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW; |
| 573 | if (flag & AT_EMPTY_PATH) |
| 574 | lookup_flags |= LOOKUP_EMPTY; |
| 575 | error = user_path_at(dfd, filename, lookup_flags, &path); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 576 | if (error) |
| 577 | goto out; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 578 | error = mnt_want_write(path.mnt); |
Dave Hansen | 2af482a | 2008-02-15 14:37:50 -0800 | [diff] [blame] | 579 | if (error) |
| 580 | goto out_release; |
Tetsuo Handa | fe542cf | 2009-11-22 11:49:55 +0900 | [diff] [blame] | 581 | error = chown_common(&path, user, group); |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 582 | mnt_drop_write(path.mnt); |
Dave Hansen | 2af482a | 2008-02-15 14:37:50 -0800 | [diff] [blame] | 583 | out_release: |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 584 | path_put(&path); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 585 | out: |
| 586 | return error; |
| 587 | } |
| 588 | |
Heiko Carstens | ca013e9 | 2009-01-14 14:14:19 +0100 | [diff] [blame] | 589 | SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 591 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | int error; |
| 593 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 594 | error = user_lpath(filename, &path); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 595 | if (error) |
| 596 | goto out; |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 597 | error = mnt_want_write(path.mnt); |
Dave Hansen | 2af482a | 2008-02-15 14:37:50 -0800 | [diff] [blame] | 598 | if (error) |
| 599 | goto out_release; |
Tetsuo Handa | fe542cf | 2009-11-22 11:49:55 +0900 | [diff] [blame] | 600 | error = chown_common(&path, user, group); |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 601 | mnt_drop_write(path.mnt); |
Dave Hansen | 2af482a | 2008-02-15 14:37:50 -0800 | [diff] [blame] | 602 | out_release: |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 603 | path_put(&path); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 604 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | return error; |
| 606 | } |
| 607 | |
Heiko Carstens | ca013e9 | 2009-01-14 14:14:19 +0100 | [diff] [blame] | 608 | SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | { |
| 610 | struct file * file; |
| 611 | int error = -EBADF; |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 612 | struct dentry * dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | |
| 614 | file = fget(fd); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 615 | if (!file) |
| 616 | goto out; |
| 617 | |
npiggin@suse.de | 96029c4 | 2009-04-26 20:25:55 +1000 | [diff] [blame] | 618 | error = mnt_want_write_file(file); |
Dave Hansen | 2af482a | 2008-02-15 14:37:50 -0800 | [diff] [blame] | 619 | if (error) |
| 620 | goto out_fput; |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 621 | dentry = file->f_path.dentry; |
Al Viro | 5a190ae | 2007-06-07 12:19:32 -0400 | [diff] [blame] | 622 | audit_inode(NULL, dentry); |
Tetsuo Handa | fe542cf | 2009-11-22 11:49:55 +0900 | [diff] [blame] | 623 | error = chown_common(&file->f_path, user, group); |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 624 | mnt_drop_write_file(file); |
Dave Hansen | 2af482a | 2008-02-15 14:37:50 -0800 | [diff] [blame] | 625 | out_fput: |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 626 | fput(file); |
| 627 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | return error; |
| 629 | } |
| 630 | |
Dave Hansen | 4a3fd21 | 2008-02-15 14:37:48 -0800 | [diff] [blame] | 631 | /* |
| 632 | * You have to be very careful that these write |
| 633 | * counts get cleaned up in error cases and |
| 634 | * upon __fput(). This should probably never |
| 635 | * be called outside of __dentry_open(). |
| 636 | */ |
| 637 | static inline int __get_file_write_access(struct inode *inode, |
| 638 | struct vfsmount *mnt) |
| 639 | { |
| 640 | int error; |
| 641 | error = get_write_access(inode); |
| 642 | if (error) |
| 643 | return error; |
| 644 | /* |
| 645 | * Do not take mount writer counts on |
| 646 | * special files since no writes to |
| 647 | * the mount itself will occur. |
| 648 | */ |
| 649 | if (!special_file(inode->i_mode)) { |
| 650 | /* |
| 651 | * Balanced in __fput() |
| 652 | */ |
| 653 | error = mnt_want_write(mnt); |
| 654 | if (error) |
| 655 | put_write_access(inode); |
| 656 | } |
| 657 | return error; |
| 658 | } |
| 659 | |
Miklos Szeredi | d061116 | 2012-05-21 17:30:15 +0200 | [diff] [blame] | 660 | int open_check_o_direct(struct file *f) |
| 661 | { |
| 662 | /* NB: we're sure to have correct a_ops only after f_op->open */ |
| 663 | if (f->f_flags & O_DIRECT) { |
| 664 | if (!f->f_mapping->a_ops || |
| 665 | ((!f->f_mapping->a_ops->direct_IO) && |
| 666 | (!f->f_mapping->a_ops->get_xip_mem))) { |
| 667 | return -EINVAL; |
| 668 | } |
| 669 | } |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | static struct file *do_dentry_open(struct dentry *dentry, struct vfsmount *mnt, |
| 674 | struct file *f, |
| 675 | int (*open)(struct inode *, struct file *), |
| 676 | const struct cred *cred) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | { |
Al Viro | 1abf0c7 | 2011-03-13 03:51:11 -0400 | [diff] [blame] | 678 | static const struct file_operations empty_fops = {}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | struct inode *inode; |
| 680 | int error; |
| 681 | |
Al Viro | 5300990 | 2009-12-19 10:15:07 -0500 | [diff] [blame] | 682 | f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK | |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 683 | FMODE_PREAD | FMODE_PWRITE; |
Al Viro | 1abf0c7 | 2011-03-13 03:51:11 -0400 | [diff] [blame] | 684 | |
| 685 | if (unlikely(f->f_flags & O_PATH)) |
| 686 | f->f_mode = FMODE_PATH; |
| 687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | inode = dentry->d_inode; |
| 689 | if (f->f_mode & FMODE_WRITE) { |
Dave Hansen | 4a3fd21 | 2008-02-15 14:37:48 -0800 | [diff] [blame] | 690 | error = __get_file_write_access(inode, mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | if (error) |
| 692 | goto cleanup_file; |
Dave Hansen | ad775f5 | 2008-02-15 14:38:01 -0800 | [diff] [blame] | 693 | if (!special_file(inode->i_mode)) |
| 694 | file_take_write(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | f->f_mapping = inode->i_mapping; |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 698 | f->f_path.dentry = dentry; |
| 699 | f->f_path.mnt = mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | f->f_pos = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | |
Al Viro | 1abf0c7 | 2011-03-13 03:51:11 -0400 | [diff] [blame] | 702 | if (unlikely(f->f_mode & FMODE_PATH)) { |
| 703 | f->f_op = &empty_fops; |
| 704 | return f; |
| 705 | } |
| 706 | |
Linus Torvalds | f530fe7 | 2017-01-07 19:14:29 +0100 | [diff] [blame] | 707 | if (S_ISREG(inode->i_mode)) |
| 708 | f->f_mode |= FMODE_SPLICE_WRITE | FMODE_SPLICE_READ; |
| 709 | |
| 710 | |
Al Viro | 1abf0c7 | 2011-03-13 03:51:11 -0400 | [diff] [blame] | 711 | f->f_op = fops_get(inode->i_fop); |
| 712 | |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 713 | error = security_dentry_open(f, cred); |
Yuichi Nakamura | 788e7dd | 2007-09-14 09:27:07 +0900 | [diff] [blame] | 714 | if (error) |
| 715 | goto cleanup_all; |
| 716 | |
J. Bruce Fields | f3c7691e | 2011-09-21 10:58:13 -0400 | [diff] [blame] | 717 | error = break_lease(inode, f->f_flags); |
| 718 | if (error) |
| 719 | goto cleanup_all; |
| 720 | |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 721 | if (!open && f->f_op) |
| 722 | open = f->f_op->open; |
| 723 | if (open) { |
| 724 | error = open(inode, f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | if (error) |
| 726 | goto cleanup_all; |
| 727 | } |
Mimi Zohar | 890275b | 2010-11-02 10:13:07 -0400 | [diff] [blame] | 728 | if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) |
| 729 | i_readcount_inc(inode); |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 730 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC); |
| 732 | |
| 733 | file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping); |
| 734 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | return f; |
| 736 | |
| 737 | cleanup_all: |
| 738 | fops_put(f->f_op); |
Dave Hansen | 4a3fd21 | 2008-02-15 14:37:48 -0800 | [diff] [blame] | 739 | if (f->f_mode & FMODE_WRITE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | put_write_access(inode); |
Dave Hansen | ad775f5 | 2008-02-15 14:38:01 -0800 | [diff] [blame] | 741 | if (!special_file(inode->i_mode)) { |
| 742 | /* |
| 743 | * We don't consider this a real |
| 744 | * mnt_want/drop_write() pair |
| 745 | * because it all happenend right |
| 746 | * here, so just reset the state. |
| 747 | */ |
| 748 | file_reset_write(f); |
Dave Hansen | 4a3fd21 | 2008-02-15 14:37:48 -0800 | [diff] [blame] | 749 | mnt_drop_write(mnt); |
Dave Hansen | ad775f5 | 2008-02-15 14:38:01 -0800 | [diff] [blame] | 750 | } |
Dave Hansen | 4a3fd21 | 2008-02-15 14:37:48 -0800 | [diff] [blame] | 751 | } |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 752 | f->f_path.dentry = NULL; |
| 753 | f->f_path.mnt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | cleanup_file: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | dput(dentry); |
| 756 | mntput(mnt); |
| 757 | return ERR_PTR(error); |
| 758 | } |
| 759 | |
Miklos Szeredi | d061116 | 2012-05-21 17:30:15 +0200 | [diff] [blame] | 760 | static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt, |
| 761 | struct file *f, |
| 762 | int (*open)(struct inode *, struct file *), |
| 763 | const struct cred *cred) |
| 764 | { |
| 765 | struct file *res = do_dentry_open(dentry, mnt, f, open, cred); |
| 766 | if (!IS_ERR(res)) { |
| 767 | int error = open_check_o_direct(f); |
| 768 | if (error) { |
| 769 | fput(res); |
| 770 | res = ERR_PTR(error); |
| 771 | } |
Miklos Szeredi | f3aa868 | 2012-05-21 17:30:16 +0200 | [diff] [blame] | 772 | } else { |
| 773 | put_filp(f); |
Miklos Szeredi | d061116 | 2012-05-21 17:30:15 +0200 | [diff] [blame] | 774 | } |
| 775 | return res; |
| 776 | } |
| 777 | |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 778 | /** |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 779 | * finish_open - finish opening a file |
Miklos Szeredi | 90666b3 | 2013-09-16 14:51:55 +0200 | [diff] [blame] | 780 | * @file: file pointer |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 781 | * @dentry: pointer to dentry |
| 782 | * @open: open callback |
Miklos Szeredi | 90666b3 | 2013-09-16 14:51:55 +0200 | [diff] [blame] | 783 | * @opened: state of open |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 784 | * |
| 785 | * This can be used to finish opening a file passed to i_op->atomic_open(). |
| 786 | * |
| 787 | * If the open callback is set to NULL, then the standard f_op->open() |
| 788 | * filesystem callback is substituted. |
Miklos Szeredi | 90666b3 | 2013-09-16 14:51:55 +0200 | [diff] [blame] | 789 | * |
| 790 | * NB: the dentry reference is _not_ consumed. If, for example, the dentry is |
| 791 | * the return value of d_splice_alias(), then the caller needs to perform dput() |
| 792 | * on it after finish_open(). |
| 793 | * |
| 794 | * On successful return @file is a fully instantiated open file. After this, if |
| 795 | * an error occurs in ->atomic_open(), it needs to clean up with fput(). |
| 796 | * |
| 797 | * Returns zero on success or -errno if the open failed. |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 798 | */ |
Al Viro | b2ebdc5 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 799 | int finish_open(struct file *file, struct dentry *dentry, |
| 800 | int (*open)(struct inode *, struct file *), |
| 801 | int *opened) |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 802 | { |
| 803 | struct file *res; |
Al Viro | c313080 | 2012-06-10 05:04:43 -0400 | [diff] [blame] | 804 | BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */ |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 805 | |
Al Viro | b2ebdc5 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 806 | mntget(file->f_path.mnt); |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 807 | dget(dentry); |
| 808 | |
Al Viro | b2ebdc5 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 809 | res = do_dentry_open(dentry, file->f_path.mnt, file, open, current_cred()); |
| 810 | if (!IS_ERR(res)) { |
Al Viro | 8915ef3 | 2012-06-10 05:01:45 -0400 | [diff] [blame] | 811 | *opened |= FILE_OPENED; |
Al Viro | b2ebdc5 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 812 | return 0; |
| 813 | } |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 814 | |
Al Viro | b2ebdc5 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 815 | return PTR_ERR(res); |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 816 | } |
| 817 | EXPORT_SYMBOL(finish_open); |
| 818 | |
| 819 | /** |
| 820 | * finish_no_open - finish ->atomic_open() without opening the file |
| 821 | * |
Miklos Szeredi | 90666b3 | 2013-09-16 14:51:55 +0200 | [diff] [blame] | 822 | * @file: file pointer |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 823 | * @dentry: dentry or NULL (as returned from ->lookup()) |
| 824 | * |
| 825 | * This can be used to set the result of a successful lookup in ->atomic_open(). |
Miklos Szeredi | 90666b3 | 2013-09-16 14:51:55 +0200 | [diff] [blame] | 826 | * |
| 827 | * NB: unlike finish_open() this function does consume the dentry reference and |
| 828 | * the caller need not dput() it. |
| 829 | * |
| 830 | * Returns "1" which must be the return value of ->atomic_open() after having |
| 831 | * called this function. |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 832 | */ |
Al Viro | 57fa6ca | 2012-06-10 06:48:09 -0400 | [diff] [blame] | 833 | int finish_no_open(struct file *file, struct dentry *dentry) |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 834 | { |
Al Viro | b2ebdc5 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 835 | file->f_path.dentry = dentry; |
Al Viro | 57fa6ca | 2012-06-10 06:48:09 -0400 | [diff] [blame] | 836 | return 1; |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 837 | } |
| 838 | EXPORT_SYMBOL(finish_no_open); |
| 839 | |
Peter Staubach | 6fdcc21 | 2005-11-07 00:59:42 -0800 | [diff] [blame] | 840 | /* |
| 841 | * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an |
| 842 | * error. |
| 843 | */ |
David Howells | 745ca24 | 2008-11-14 10:39:22 +1100 | [diff] [blame] | 844 | struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags, |
| 845 | const struct cred *cred) |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 846 | { |
| 847 | int error; |
| 848 | struct file *f; |
| 849 | |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 850 | validate_creds(cred); |
| 851 | |
Tetsuo Handa | c212f9a | 2011-01-19 21:08:41 +0900 | [diff] [blame] | 852 | /* We must always pass in a valid mount pointer. */ |
| 853 | BUG_ON(!mnt); |
Christoph Hellwig | 322ee5b | 2008-02-15 14:37:24 -0800 | [diff] [blame] | 854 | |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 855 | error = -ENFILE; |
| 856 | f = get_empty_filp(); |
Peter Staubach | 6fdcc21 | 2005-11-07 00:59:42 -0800 | [diff] [blame] | 857 | if (f == NULL) { |
| 858 | dput(dentry); |
| 859 | mntput(mnt); |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 860 | return ERR_PTR(error); |
Peter Staubach | 6fdcc21 | 2005-11-07 00:59:42 -0800 | [diff] [blame] | 861 | } |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 862 | |
Al Viro | 482928d | 2009-12-19 10:10:39 -0500 | [diff] [blame] | 863 | f->f_flags = flags; |
| 864 | return __dentry_open(dentry, mnt, f, NULL, cred); |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 865 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | EXPORT_SYMBOL(dentry_open); |
| 867 | |
Matt Mackall | b01ec0e | 2006-01-08 01:05:20 -0800 | [diff] [blame] | 868 | static void __put_unused_fd(struct files_struct *files, unsigned int fd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | { |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 870 | struct fdtable *fdt = files_fdtable(files); |
David Howells | 1dce27c | 2012-02-16 17:49:42 +0000 | [diff] [blame] | 871 | __clear_open_fd(fd, fdt); |
Eric Dumazet | 0c9e63f | 2006-03-23 03:00:12 -0800 | [diff] [blame] | 872 | if (fd < files->next_fd) |
| 873 | files->next_fd = fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Harvey Harrison | fc9b52c | 2008-02-08 04:19:52 -0800 | [diff] [blame] | 876 | void put_unused_fd(unsigned int fd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | { |
| 878 | struct files_struct *files = current->files; |
| 879 | spin_lock(&files->file_lock); |
| 880 | __put_unused_fd(files, fd); |
| 881 | spin_unlock(&files->file_lock); |
| 882 | } |
| 883 | |
| 884 | EXPORT_SYMBOL(put_unused_fd); |
| 885 | |
| 886 | /* |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 887 | * Install a file pointer in the fd array. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | * |
| 889 | * The VFS is full of places where we drop the files lock between |
| 890 | * setting the open_fds bitmap and installing the file in the file |
| 891 | * array. At any such point, we are vulnerable to a dup2() race |
| 892 | * installing a file in the array before us. We need to detect this and |
| 893 | * fput() the struct file we are about to overwrite in this case. |
| 894 | * |
| 895 | * It should never happen - if we allow dup2() do it, _really_ bad things |
| 896 | * will follow. |
| 897 | */ |
| 898 | |
Harvey Harrison | fc9b52c | 2008-02-08 04:19:52 -0800 | [diff] [blame] | 899 | void fd_install(unsigned int fd, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | { |
| 901 | struct files_struct *files = current->files; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 902 | struct fdtable *fdt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | spin_lock(&files->file_lock); |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 904 | fdt = files_fdtable(files); |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 905 | BUG_ON(fdt->fd[fd] != NULL); |
| 906 | rcu_assign_pointer(fdt->fd[fd], file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | spin_unlock(&files->file_lock); |
| 908 | } |
| 909 | |
| 910 | EXPORT_SYMBOL(fd_install); |
| 911 | |
Al Viro | a218d0f | 2011-11-21 14:59:34 -0500 | [diff] [blame] | 912 | static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op) |
Al Viro | 47c805d | 2011-02-23 17:44:09 -0500 | [diff] [blame] | 913 | { |
| 914 | int lookup_flags = 0; |
| 915 | int acc_mode; |
| 916 | |
Andy Lutomirski | 7f52b80 | 2013-08-01 21:07:52 -0700 | [diff] [blame] | 917 | if (flags & (O_CREAT | __O_TMPFILE)) |
Miklos Szeredi | cefc92d | 2012-08-15 13:01:24 +0200 | [diff] [blame] | 918 | op->mode = (mode & S_IALLUGO) | S_IFREG; |
| 919 | else |
| 920 | op->mode = 0; |
Al Viro | 47c805d | 2011-02-23 17:44:09 -0500 | [diff] [blame] | 921 | |
| 922 | /* Must never be set by userspace */ |
| 923 | flags &= ~FMODE_NONOTIFY; |
| 924 | |
| 925 | /* |
| 926 | * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only |
| 927 | * check for O_DSYNC if the need any syncing at all we enforce it's |
| 928 | * always set instead of having to deal with possibly weird behaviour |
| 929 | * for malicious applications setting only __O_SYNC. |
| 930 | */ |
| 931 | if (flags & __O_SYNC) |
| 932 | flags |= O_DSYNC; |
| 933 | |
Al Viro | d50bcfe | 2013-07-13 13:26:37 +0400 | [diff] [blame] | 934 | if (flags & __O_TMPFILE) { |
| 935 | if ((flags & O_TMPFILE_MASK) != O_TMPFILE) |
Al Viro | b0d5430 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 936 | return -EINVAL; |
| 937 | acc_mode = MAY_OPEN | ACC_MODE(flags); |
Al Viro | 815466a | 2013-07-20 03:11:32 +0400 | [diff] [blame] | 938 | if (!(acc_mode & MAY_WRITE)) |
| 939 | return -EINVAL; |
Al Viro | b0d5430 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 940 | } else if (flags & O_PATH) { |
| 941 | /* |
| 942 | * If we have O_PATH in the open flag. Then we |
| 943 | * cannot have anything other than the below set of flags |
| 944 | */ |
Al Viro | 1abf0c7 | 2011-03-13 03:51:11 -0400 | [diff] [blame] | 945 | flags &= O_DIRECTORY | O_NOFOLLOW | O_PATH; |
| 946 | acc_mode = 0; |
| 947 | } else { |
| 948 | acc_mode = MAY_OPEN | ACC_MODE(flags); |
| 949 | } |
Al Viro | 47c805d | 2011-02-23 17:44:09 -0500 | [diff] [blame] | 950 | |
Al Viro | 1abf0c7 | 2011-03-13 03:51:11 -0400 | [diff] [blame] | 951 | op->open_flag = flags; |
Al Viro | 47c805d | 2011-02-23 17:44:09 -0500 | [diff] [blame] | 952 | |
| 953 | /* O_TRUNC implies we need access checks for write permissions */ |
| 954 | if (flags & O_TRUNC) |
| 955 | acc_mode |= MAY_WRITE; |
| 956 | |
| 957 | /* Allow the LSM permission hook to distinguish append |
| 958 | access from general write access. */ |
| 959 | if (flags & O_APPEND) |
| 960 | acc_mode |= MAY_APPEND; |
| 961 | |
| 962 | op->acc_mode = acc_mode; |
| 963 | |
Al Viro | 1abf0c7 | 2011-03-13 03:51:11 -0400 | [diff] [blame] | 964 | op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN; |
| 965 | |
Al Viro | 47c805d | 2011-02-23 17:44:09 -0500 | [diff] [blame] | 966 | if (flags & O_CREAT) { |
| 967 | op->intent |= LOOKUP_CREATE; |
| 968 | if (flags & O_EXCL) |
| 969 | op->intent |= LOOKUP_EXCL; |
| 970 | } |
| 971 | |
| 972 | if (flags & O_DIRECTORY) |
| 973 | lookup_flags |= LOOKUP_DIRECTORY; |
| 974 | if (!(flags & O_NOFOLLOW)) |
| 975 | lookup_flags |= LOOKUP_FOLLOW; |
| 976 | return lookup_flags; |
| 977 | } |
| 978 | |
| 979 | /** |
Jeff Layton | 631423f | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 980 | * file_open_name - open file and return file pointer |
| 981 | * |
| 982 | * @name: struct filename containing path to open |
| 983 | * @flags: open flags as per the open(2) second argument |
| 984 | * @mode: mode for the new file if O_CREAT is set, else ignored |
| 985 | * |
| 986 | * This is the helper to open a file from kernelspace if you really |
| 987 | * have to. But in generally you should not do this, so please move |
| 988 | * along, nothing to see here.. |
| 989 | */ |
| 990 | struct file *file_open_name(struct filename *name, int flags, umode_t mode) |
| 991 | { |
| 992 | struct open_flags op; |
| 993 | int lookup = build_open_flags(flags, mode, &op); |
| 994 | return do_filp_open(AT_FDCWD, name, &op, lookup); |
| 995 | } |
| 996 | |
| 997 | /** |
Al Viro | 47c805d | 2011-02-23 17:44:09 -0500 | [diff] [blame] | 998 | * filp_open - open file and return file pointer |
| 999 | * |
| 1000 | * @filename: path to open |
| 1001 | * @flags: open flags as per the open(2) second argument |
| 1002 | * @mode: mode for the new file if O_CREAT is set, else ignored |
| 1003 | * |
| 1004 | * This is the helper to open a file from kernelspace if you really |
| 1005 | * have to. But in generally you should not do this, so please move |
| 1006 | * along, nothing to see here.. |
| 1007 | */ |
Al Viro | a218d0f | 2011-11-21 14:59:34 -0500 | [diff] [blame] | 1008 | struct file *filp_open(const char *filename, int flags, umode_t mode) |
Al Viro | 47c805d | 2011-02-23 17:44:09 -0500 | [diff] [blame] | 1009 | { |
Jeff Layton | 631423f | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 1010 | struct filename name = {.name = filename}; |
| 1011 | return file_open_name(&name, flags, mode); |
Al Viro | 47c805d | 2011-02-23 17:44:09 -0500 | [diff] [blame] | 1012 | } |
| 1013 | EXPORT_SYMBOL(filp_open); |
| 1014 | |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 1015 | struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt, |
| 1016 | const char *filename, int flags) |
| 1017 | { |
| 1018 | struct open_flags op; |
| 1019 | int lookup = build_open_flags(flags, 0, &op); |
| 1020 | if (flags & O_CREAT) |
| 1021 | return ERR_PTR(-EINVAL); |
| 1022 | if (!filename && (flags & O_DIRECTORY)) |
| 1023 | if (!dentry->d_inode->i_op->lookup) |
| 1024 | return ERR_PTR(-ENOTDIR); |
| 1025 | return do_file_open_root(dentry, mnt, filename, &op, lookup); |
| 1026 | } |
| 1027 | EXPORT_SYMBOL(file_open_root); |
| 1028 | |
Al Viro | a218d0f | 2011-11-21 14:59:34 -0500 | [diff] [blame] | 1029 | long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | { |
Al Viro | 47c805d | 2011-02-23 17:44:09 -0500 | [diff] [blame] | 1031 | struct open_flags op; |
| 1032 | int lookup = build_open_flags(flags, mode, &op); |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 1033 | struct filename *tmp = getname(filename); |
Miklos Szeredi | e922efc | 2005-09-06 15:18:25 -0700 | [diff] [blame] | 1034 | int fd = PTR_ERR(tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | if (!IS_ERR(tmp)) { |
Ulrich Drepper | f23513e | 2007-07-15 23:40:32 -0700 | [diff] [blame] | 1037 | fd = get_unused_fd_flags(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | if (fd >= 0) { |
Jeff Layton | 631423f | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 1039 | struct file *f = do_filp_open(dfd, tmp, &op, lookup); |
Telemaque Ndizihiwe | fed2fc1 | 2005-06-23 00:10:33 -0700 | [diff] [blame] | 1040 | if (IS_ERR(f)) { |
| 1041 | put_unused_fd(fd); |
| 1042 | fd = PTR_ERR(f); |
| 1043 | } else { |
Eric Paris | 2a12a9d | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 1044 | fsnotify_open(f); |
Telemaque Ndizihiwe | fed2fc1 | 2005-06-23 00:10:33 -0700 | [diff] [blame] | 1045 | fd_install(fd, f); |
| 1046 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | putname(tmp); |
| 1049 | } |
| 1050 | return fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | } |
Miklos Szeredi | e922efc | 2005-09-06 15:18:25 -0700 | [diff] [blame] | 1052 | |
Al Viro | a218d0f | 2011-11-21 14:59:34 -0500 | [diff] [blame] | 1053 | SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode) |
Miklos Szeredi | e922efc | 2005-09-06 15:18:25 -0700 | [diff] [blame] | 1054 | { |
Linus Torvalds | 385910f | 2006-04-18 13:22:59 -0700 | [diff] [blame] | 1055 | long ret; |
| 1056 | |
Miklos Szeredi | e922efc | 2005-09-06 15:18:25 -0700 | [diff] [blame] | 1057 | if (force_o_largefile()) |
| 1058 | flags |= O_LARGEFILE; |
| 1059 | |
Linus Torvalds | 385910f | 2006-04-18 13:22:59 -0700 | [diff] [blame] | 1060 | ret = do_sys_open(AT_FDCWD, filename, flags, mode); |
| 1061 | /* avoid REGPARM breakage on x86: */ |
Roland McGrath | 54a0151 | 2008-04-10 15:37:38 -0700 | [diff] [blame] | 1062 | asmlinkage_protect(3, ret, filename, flags, mode); |
Linus Torvalds | 385910f | 2006-04-18 13:22:59 -0700 | [diff] [blame] | 1063 | return ret; |
Miklos Szeredi | e922efc | 2005-09-06 15:18:25 -0700 | [diff] [blame] | 1064 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | |
Heiko Carstens | 6559eed8 | 2009-01-14 14:14:32 +0100 | [diff] [blame] | 1066 | SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, |
Al Viro | a218d0f | 2011-11-21 14:59:34 -0500 | [diff] [blame] | 1067 | umode_t, mode) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1068 | { |
Linus Torvalds | 385910f | 2006-04-18 13:22:59 -0700 | [diff] [blame] | 1069 | long ret; |
| 1070 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1071 | if (force_o_largefile()) |
| 1072 | flags |= O_LARGEFILE; |
| 1073 | |
Linus Torvalds | 385910f | 2006-04-18 13:22:59 -0700 | [diff] [blame] | 1074 | ret = do_sys_open(dfd, filename, flags, mode); |
| 1075 | /* avoid REGPARM breakage on x86: */ |
Roland McGrath | 54a0151 | 2008-04-10 15:37:38 -0700 | [diff] [blame] | 1076 | asmlinkage_protect(4, ret, dfd, filename, flags, mode); |
Linus Torvalds | 385910f | 2006-04-18 13:22:59 -0700 | [diff] [blame] | 1077 | return ret; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1078 | } |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1079 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | #ifndef __alpha__ |
| 1081 | |
| 1082 | /* |
| 1083 | * For backward compatibility? Maybe this should be moved |
| 1084 | * into arch/i386 instead? |
| 1085 | */ |
Al Viro | a218d0f | 2011-11-21 14:59:34 -0500 | [diff] [blame] | 1086 | SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | { |
| 1088 | return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode); |
| 1089 | } |
| 1090 | |
| 1091 | #endif |
| 1092 | |
| 1093 | /* |
| 1094 | * "id" is the POSIX thread ID. We use the |
| 1095 | * files pointer for this.. |
| 1096 | */ |
| 1097 | int filp_close(struct file *filp, fl_owner_t id) |
| 1098 | { |
Christoph Lameter | 45778ca | 2005-06-23 00:10:17 -0700 | [diff] [blame] | 1099 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | |
| 1101 | if (!file_count(filp)) { |
| 1102 | printk(KERN_ERR "VFS: Close: file count is 0\n"); |
Christoph Lameter | 45778ca | 2005-06-23 00:10:17 -0700 | [diff] [blame] | 1103 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
Christoph Lameter | 45778ca | 2005-06-23 00:10:17 -0700 | [diff] [blame] | 1106 | if (filp->f_op && filp->f_op->flush) |
Miklos Szeredi | 75e1fcc | 2006-06-23 02:05:12 -0700 | [diff] [blame] | 1107 | retval = filp->f_op->flush(filp, id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | |
Al Viro | 1abf0c7 | 2011-03-13 03:51:11 -0400 | [diff] [blame] | 1109 | if (likely(!(filp->f_mode & FMODE_PATH))) { |
| 1110 | dnotify_flush(filp, id); |
| 1111 | locks_remove_posix(filp, id); |
| 1112 | } |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 1113 | security_file_close(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | fput(filp); |
| 1115 | return retval; |
| 1116 | } |
| 1117 | |
| 1118 | EXPORT_SYMBOL(filp_close); |
| 1119 | |
| 1120 | /* |
| 1121 | * Careful here! We test whether the file pointer is NULL before |
| 1122 | * releasing the fd. This ensures that one clone task can't release |
| 1123 | * an fd while another clone is opening it. |
| 1124 | */ |
Heiko Carstens | ca013e9 | 2009-01-14 14:14:19 +0100 | [diff] [blame] | 1125 | SYSCALL_DEFINE1(close, unsigned int, fd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | { |
| 1127 | struct file * filp; |
| 1128 | struct files_struct *files = current->files; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 1129 | struct fdtable *fdt; |
Ernie Petrides | ee731f4 | 2006-09-29 02:00:13 -0700 | [diff] [blame] | 1130 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | |
| 1132 | spin_lock(&files->file_lock); |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 1133 | fdt = files_fdtable(files); |
| 1134 | if (fd >= fdt->max_fds) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | goto out_unlock; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 1136 | filp = fdt->fd[fd]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | if (!filp) |
| 1138 | goto out_unlock; |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 1139 | rcu_assign_pointer(fdt->fd[fd], NULL); |
David Howells | 1dce27c | 2012-02-16 17:49:42 +0000 | [diff] [blame] | 1140 | __clear_close_on_exec(fd, fdt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | __put_unused_fd(files, fd); |
| 1142 | spin_unlock(&files->file_lock); |
Ernie Petrides | ee731f4 | 2006-09-29 02:00:13 -0700 | [diff] [blame] | 1143 | retval = filp_close(filp, files); |
| 1144 | |
| 1145 | /* can't restart close syscall because file table entry was cleared */ |
| 1146 | if (unlikely(retval == -ERESTARTSYS || |
| 1147 | retval == -ERESTARTNOINTR || |
| 1148 | retval == -ERESTARTNOHAND || |
| 1149 | retval == -ERESTART_RESTARTBLOCK)) |
| 1150 | retval = -EINTR; |
| 1151 | |
| 1152 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | |
| 1154 | out_unlock: |
| 1155 | spin_unlock(&files->file_lock); |
| 1156 | return -EBADF; |
| 1157 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | EXPORT_SYMBOL(sys_close); |
| 1159 | |
| 1160 | /* |
| 1161 | * This routine simulates a hangup on the tty, to arrange that users |
| 1162 | * are given clean terminals at login time. |
| 1163 | */ |
Heiko Carstens | ca013e9 | 2009-01-14 14:14:19 +0100 | [diff] [blame] | 1164 | SYSCALL_DEFINE0(vhangup) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | { |
| 1166 | if (capable(CAP_SYS_TTY_CONFIG)) { |
Alan Cox | 2cb5998 | 2008-10-13 10:40:30 +0100 | [diff] [blame] | 1167 | tty_vhangup_self(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | return 0; |
| 1169 | } |
| 1170 | return -EPERM; |
| 1171 | } |
| 1172 | |
| 1173 | /* |
| 1174 | * Called when an inode is about to be open. |
| 1175 | * We use this to disallow opening large files on 32bit systems if |
| 1176 | * the caller didn't specify O_LARGEFILE. On 64bit systems we force |
| 1177 | * on this flag in sys_open. |
| 1178 | */ |
| 1179 | int generic_file_open(struct inode * inode, struct file * filp) |
| 1180 | { |
| 1181 | if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS) |
Alan Cox | a9c62a1 | 2007-10-16 23:30:22 -0700 | [diff] [blame] | 1182 | return -EOVERFLOW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | return 0; |
| 1184 | } |
| 1185 | |
| 1186 | EXPORT_SYMBOL(generic_file_open); |
| 1187 | |
| 1188 | /* |
| 1189 | * This is used by subsystems that don't want seekable |
Dmitry Torokhov | 06b1e10 | 2010-08-10 18:01:33 -0700 | [diff] [blame] | 1190 | * file descriptors. The function is not supposed to ever fail, the only |
| 1191 | * reason it returns an 'int' and not 'void' is so that it can be plugged |
| 1192 | * directly into file_operations structure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | */ |
| 1194 | int nonseekable_open(struct inode *inode, struct file *filp) |
| 1195 | { |
| 1196 | filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE); |
| 1197 | return 0; |
| 1198 | } |
| 1199 | |
| 1200 | EXPORT_SYMBOL(nonseekable_open); |