blob: 1fd96c5d3895fa5bb18399fdf7d4ee4b6dfe3a8d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040010#include <linux/fdtable.h>
Robert Love0eeca282005-07-12 17:06:03 -040011#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/tty.h>
14#include <linux/namei.h>
15#include <linux/backing-dev.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080016#include <linux/capability.h>
Andrew G. Morgan086f7312008-07-04 09:59:58 -070017#include <linux/securebits.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/security.h>
19#include <linux/mount.h>
Ulrich Drepper5590ff02006-01-18 17:43:53 -080020#include <linux/fcntl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/uaccess.h>
23#include <linux/fs.h>
Yoav Zachef3daed2005-06-23 00:09:58 -070024#include <linux/personality.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/pagemap.h>
26#include <linux/syscalls.h>
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070027#include <linux/rcupdate.h>
Amy Griffis73241cc2005-11-03 16:00:25 +000028#include <linux/audit.h>
Amit Arora97ac7352007-07-17 21:42:44 -040029#include <linux/falloc.h>
Al Viro5ad4e532009-03-29 19:50:06 -040030#include <linux/fs_struct.h>
Al Virob65a9cf2009-12-16 06:27:40 -050031#include <linux/ima.h>
Eric Paris2dfc1ca2009-12-17 20:30:52 -050032#include <linux/dnotify.h>
Al Viro3f6d0782013-02-24 13:49:08 -050033#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Eric Parise81e3f42009-12-04 15:47:36 -050035#include "internal.h"
36
Daniel Rosenberge5eeaaf2016-10-26 16:33:11 -070037int do_truncate2(struct vfsmount *mnt, struct dentry *dentry, loff_t length,
38 unsigned int time_attrs, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Amerigo Wang939a9422009-08-20 19:29:03 -070040 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 struct iattr newattrs;
42
43 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
44 if (length < 0)
45 return -EINVAL;
46
47 newattrs.ia_size = length;
NeilBrown4a301312006-01-08 01:02:39 -080048 newattrs.ia_valid = ATTR_SIZE | time_attrs;
Miklos Szeredicc4e69d2005-11-07 00:59:49 -080049 if (filp) {
50 newattrs.ia_file = filp;
51 newattrs.ia_valid |= ATTR_FILE;
52 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Jan Kara45f147a2015-05-21 16:05:55 +020054 /* Remove suid, sgid, and file capabilities on truncate too */
55 ret = dentry_needs_remove_privs(dentry);
56 if (ret < 0)
57 return ret;
Amerigo Wang939a9422009-08-20 19:29:03 -070058 if (ret)
59 newattrs.ia_valid |= ret | ATTR_FORCE;
Linus Torvalds7b82dc02007-05-08 20:10:00 -070060
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080061 mutex_lock(&dentry->d_inode->i_mutex);
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -040062 /* Note any delegations or leases have already been broken: */
Daniel Rosenberge5eeaaf2016-10-26 16:33:11 -070063 ret = notify_change2(mnt, dentry, &newattrs, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080064 mutex_unlock(&dentry->d_inode->i_mutex);
Amerigo Wang939a9422009-08-20 19:29:03 -070065 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
Daniel Rosenberge5eeaaf2016-10-26 16:33:11 -070067int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
68 struct file *filp)
69{
70 return do_truncate2(NULL, dentry, length, time_attrs, filp);
71}
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
David Howellsa02de962012-12-20 21:52:36 +000073long vfs_truncate(struct path *path, loff_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Al Viro2d8f3032008-07-22 09:59:21 -040075 struct inode *inode;
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -070076 struct vfsmount *mnt;
David Howellsa02de962012-12-20 21:52:36 +000077 long error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
David Howellsa02de962012-12-20 21:52:36 +000079 inode = path->dentry->d_inode;
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -070080 mnt = path->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (S_ISDIR(inode->i_mode))
David Howellsa02de962012-12-20 21:52:36 +000084 return -EISDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (!S_ISREG(inode->i_mode))
David Howellsa02de962012-12-20 21:52:36 +000086 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
David Howellsa02de962012-12-20 21:52:36 +000088 error = mnt_want_write(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 if (error)
David Howellsa02de962012-12-20 21:52:36 +000090 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -070092 error = inode_permission2(mnt, inode, MAY_WRITE);
Dave Hansen9ac9b842008-02-15 14:37:52 -080093 if (error)
94 goto mnt_drop_write_and_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 error = -EPERM;
Miklos Szeredic82e42d2008-06-24 16:50:12 +020097 if (IS_APPEND(inode))
Dave Hansen9ac9b842008-02-15 14:37:52 -080098 goto mnt_drop_write_and_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 error = get_write_access(inode);
101 if (error)
Dave Hansen9ac9b842008-02-15 14:37:52 -0800102 goto mnt_drop_write_and_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
david m. richter97003822007-07-31 00:39:12 -0700104 /*
105 * Make sure that there are no leases. get_write_access() protects
106 * against the truncate racing with a lease-granting setlease().
107 */
Al Viro8737c932009-12-24 06:47:55 -0500108 error = break_lease(inode, O_WRONLY);
david m. richter97003822007-07-31 00:39:12 -0700109 if (error)
110 goto put_write_and_out;
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 error = locks_verify_truncate(inode, NULL, length);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +0900113 if (!error)
David Howellsa02de962012-12-20 21:52:36 +0000114 error = security_path_truncate(path);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500115 if (!error)
Daniel Rosenberge5eeaaf2016-10-26 16:33:11 -0700116 error = do_truncate2(mnt, path->dentry, length, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
david m. richter97003822007-07-31 00:39:12 -0700118put_write_and_out:
119 put_write_access(inode);
Dave Hansen9ac9b842008-02-15 14:37:52 -0800120mnt_drop_write_and_out:
David Howellsa02de962012-12-20 21:52:36 +0000121 mnt_drop_write(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122out:
123 return error;
124}
David Howellsa02de962012-12-20 21:52:36 +0000125EXPORT_SYMBOL_GPL(vfs_truncate);
126
127static long do_sys_truncate(const char __user *pathname, loff_t length)
128{
Jeff Layton48f75302012-12-11 12:10:11 -0500129 unsigned int lookup_flags = LOOKUP_FOLLOW;
David Howellsa02de962012-12-20 21:52:36 +0000130 struct path path;
131 int error;
132
133 if (length < 0) /* sorry, but loff_t says... */
134 return -EINVAL;
135
Jeff Layton48f75302012-12-11 12:10:11 -0500136retry:
137 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
David Howellsa02de962012-12-20 21:52:36 +0000138 if (!error) {
139 error = vfs_truncate(&path, length);
140 path_put(&path);
141 }
Jeff Layton48f75302012-12-11 12:10:11 -0500142 if (retry_estale(error, lookup_flags)) {
143 lookup_flags |= LOOKUP_REVAL;
144 goto retry;
145 }
David Howellsa02de962012-12-20 21:52:36 +0000146 return error;
147}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Heiko Carstens4fd8da82009-09-23 17:49:55 +0200149SYSCALL_DEFINE2(truncate, const char __user *, path, long, length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Heiko Carstens4fd8da82009-09-23 17:49:55 +0200151 return do_sys_truncate(path, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Al Viro3f6d0782013-02-24 13:49:08 -0500154#ifdef CONFIG_COMPAT
155COMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length)
156{
157 return do_sys_truncate(path, length);
158}
159#endif
160
Matt Mackallb01ec0e2006-01-08 01:05:20 -0800161static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Al Virobf2965d2012-08-26 20:13:36 -0400163 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 struct dentry *dentry;
Daniel Rosenberge5eeaaf2016-10-26 16:33:11 -0700165 struct vfsmount *mnt;
Al Viro2903ff02012-08-28 12:52:22 -0400166 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 int error;
168
169 error = -EINVAL;
170 if (length < 0)
171 goto out;
172 error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400173 f = fdget(fd);
174 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 goto out;
176
177 /* explicitly opened as large or we are on 64-bit box */
Al Viro2903ff02012-08-28 12:52:22 -0400178 if (f.file->f_flags & O_LARGEFILE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 small = 0;
180
Al Viro2903ff02012-08-28 12:52:22 -0400181 dentry = f.file->f_path.dentry;
Daniel Rosenberge5eeaaf2016-10-26 16:33:11 -0700182 mnt = f.file->f_path.mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 inode = dentry->d_inode;
184 error = -EINVAL;
Al Viro2903ff02012-08-28 12:52:22 -0400185 if (!S_ISREG(inode->i_mode) || !(f.file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 goto out_putf;
187
188 error = -EINVAL;
189 /* Cannot ftruncate over 2^31 bytes without large file support */
190 if (small && length > MAX_NON_LFS)
191 goto out_putf;
192
193 error = -EPERM;
194 if (IS_APPEND(inode))
195 goto out_putf;
196
Jan Kara14da9202012-06-12 16:20:37 +0200197 sb_start_write(inode->i_sb);
Al Viro2903ff02012-08-28 12:52:22 -0400198 error = locks_verify_truncate(inode, f.file, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (!error)
Al Viro2903ff02012-08-28 12:52:22 -0400200 error = security_path_truncate(&f.file->f_path);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +0900201 if (!error)
Daniel Rosenberge5eeaaf2016-10-26 16:33:11 -0700202 error = do_truncate2(mnt, dentry, length, ATTR_MTIME|ATTR_CTIME, f.file);
Jan Kara14da9202012-06-12 16:20:37 +0200203 sb_end_write(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204out_putf:
Al Viro2903ff02012-08-28 12:52:22 -0400205 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206out:
207 return error;
208}
209
Heiko Carstensbdc480e2009-01-14 14:14:12 +0100210SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Al Viro2cf09662013-01-21 15:25:54 -0500212 return do_sys_ftruncate(fd, length, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Al Viro3f6d0782013-02-24 13:49:08 -0500215#ifdef CONFIG_COMPAT
216COMPAT_SYSCALL_DEFINE2(ftruncate, unsigned int, fd, compat_ulong_t, length)
217{
218 return do_sys_ftruncate(fd, length, 1);
219}
220#endif
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/* LFS versions of truncate are only needed on 32 bit machines */
223#if BITS_PER_LONG == 32
Al Viro4a0fd5b2013-01-21 15:16:58 -0500224SYSCALL_DEFINE2(truncate64, const char __user *, path, loff_t, length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 return do_sys_truncate(path, length);
227}
228
Al Viro4a0fd5b2013-01-21 15:16:58 -0500229SYSCALL_DEFINE2(ftruncate64, unsigned int, fd, loff_t, length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Al Viro2cf09662013-01-21 15:25:54 -0500231 return do_sys_ftruncate(fd, length, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
Heiko Carstens6673e0c2009-01-14 14:14:02 +0100233#endif /* BITS_PER_LONG == 32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400235
Anna Schumaker72c72bd2014-11-07 14:44:25 -0500236int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
Amit Arora97ac7352007-07-17 21:42:44 -0400237{
Al Viro496ad9a2013-01-23 17:07:38 -0500238 struct inode *inode = file_inode(file);
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400239 long ret;
Amit Arora97ac7352007-07-17 21:42:44 -0400240
241 if (offset < 0 || len <= 0)
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400242 return -EINVAL;
Amit Arora97ac7352007-07-17 21:42:44 -0400243
244 /* Return error if mode is not supported */
Namjae Jeondd46c782015-03-25 15:07:05 +1100245 if (mode & ~FALLOC_FL_SUPPORTED_MASK)
Lukas Czerner409332b2014-03-13 19:07:42 +1100246 return -EOPNOTSUPP;
247
248 /* Punch hole and zero range are mutually exclusive */
249 if ((mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) ==
250 (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE))
Josef Bacik79124f182010-11-17 20:46:15 -0500251 return -EOPNOTSUPP;
252
253 /* Punch hole must have keep size set */
254 if ((mode & FALLOC_FL_PUNCH_HOLE) &&
255 !(mode & FALLOC_FL_KEEP_SIZE))
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400256 return -EOPNOTSUPP;
Amit Arora97ac7352007-07-17 21:42:44 -0400257
Namjae Jeon00f5e612014-02-24 10:58:15 +1100258 /* Collapse range should only be used exclusively. */
259 if ((mode & FALLOC_FL_COLLAPSE_RANGE) &&
260 (mode & ~FALLOC_FL_COLLAPSE_RANGE))
261 return -EINVAL;
262
Namjae Jeondd46c782015-03-25 15:07:05 +1100263 /* Insert range should only be used exclusively. */
264 if ((mode & FALLOC_FL_INSERT_RANGE) &&
265 (mode & ~FALLOC_FL_INSERT_RANGE))
266 return -EINVAL;
267
Amit Arora97ac7352007-07-17 21:42:44 -0400268 if (!(file->f_mode & FMODE_WRITE))
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400269 return -EBADF;
Marco Stornelli1ca551c2011-03-05 11:10:19 +0100270
Namjae Jeon00f5e612014-02-24 10:58:15 +1100271 /*
Lukas Czerner8fc61d92014-04-12 09:51:34 -0400272 * We can only allow pure fallocate on append only files
Namjae Jeon00f5e612014-02-24 10:58:15 +1100273 */
Lukas Czerner8fc61d92014-04-12 09:51:34 -0400274 if ((mode & ~FALLOC_FL_KEEP_SIZE) && IS_APPEND(inode))
Marco Stornelli1ca551c2011-03-05 11:10:19 +0100275 return -EPERM;
276
277 if (IS_IMMUTABLE(inode))
278 return -EPERM;
279
Amit Arora97ac7352007-07-17 21:42:44 -0400280 /*
Eric Biggers6d2b6172014-06-24 23:45:08 -0500281 * We cannot allow any fallocate operation on an active swapfile
Lukas Czerner0790b312014-04-12 10:05:37 -0400282 */
283 if (IS_SWAPFILE(inode))
Eric Biggers6d2b6172014-06-24 23:45:08 -0500284 return -ETXTBSY;
Lukas Czerner0790b312014-04-12 10:05:37 -0400285
286 /*
Amit Arora97ac7352007-07-17 21:42:44 -0400287 * Revalidate the write permissions, in case security policy has
288 * changed since the files were opened.
289 */
290 ret = security_file_permission(file, MAY_WRITE);
291 if (ret)
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400292 return ret;
Amit Arora97ac7352007-07-17 21:42:44 -0400293
Amit Arora97ac7352007-07-17 21:42:44 -0400294 if (S_ISFIFO(inode->i_mode))
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400295 return -ESPIPE;
Amit Arora97ac7352007-07-17 21:42:44 -0400296
Amit Arora97ac7352007-07-17 21:42:44 -0400297 /*
298 * Let individual file system decide if it supports preallocation
299 * for directories or not.
300 */
301 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400302 return -ENODEV;
Amit Arora97ac7352007-07-17 21:42:44 -0400303
Amit Arora97ac7352007-07-17 21:42:44 -0400304 /* Check for wrap through zero too */
305 if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400306 return -EFBIG;
Amit Arora97ac7352007-07-17 21:42:44 -0400307
Christoph Hellwig2fe17c12011-01-14 13:07:43 +0100308 if (!file->f_op->fallocate)
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400309 return -EOPNOTSUPP;
Amit Arora97ac7352007-07-17 21:42:44 -0400310
Jan Kara14da9202012-06-12 16:20:37 +0200311 sb_start_write(inode->i_sb);
312 ret = file->f_op->fallocate(file, mode, offset, len);
Heinrich Schuchardt820c12d2014-12-12 16:58:34 -0800313
314 /*
315 * Create inotify and fanotify events.
316 *
317 * To keep the logic simple always create events if fallocate succeeds.
318 * This implies that events are even created if the file size remains
319 * unchanged, e.g. when using flag FALLOC_FL_KEEP_SIZE.
320 */
321 if (ret == 0)
322 fsnotify_modify(file);
323
Jan Kara14da9202012-06-12 16:20:37 +0200324 sb_end_write(inode->i_sb);
325 return ret;
Amit Arora97ac7352007-07-17 21:42:44 -0400326}
Anna Schumaker72c72bd2014-11-07 14:44:25 -0500327EXPORT_SYMBOL_GPL(vfs_fallocate);
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400328
Al Viro4a0fd5b2013-01-21 15:16:58 -0500329SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len)
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400330{
Al Viro2903ff02012-08-28 12:52:22 -0400331 struct fd f = fdget(fd);
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400332 int error = -EBADF;
333
Al Viro2903ff02012-08-28 12:52:22 -0400334 if (f.file) {
Anna Schumaker72c72bd2014-11-07 14:44:25 -0500335 error = vfs_fallocate(f.file, mode, offset, len);
Al Viro2903ff02012-08-28 12:52:22 -0400336 fdput(f);
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400337 }
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400338 return error;
339}
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341/*
342 * access() needs to use the real uid/gid, not the effective uid/gid.
343 * We do this by temporarily clearing all FS-related capabilities and
344 * switching the fsuid/fsgid around to the real ones.
345 */
Heiko Carstens6559eed82009-01-14 14:14:32 +0100346SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
David Howellsd84f4f92008-11-14 10:39:23 +1100348 const struct cred *old_cred;
349 struct cred *override_cred;
Al Viro2d8f3032008-07-22 09:59:21 -0400350 struct path path;
Al Viro256984a2008-07-22 08:09:30 -0400351 struct inode *inode;
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -0700352 struct vfsmount *mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 int res;
Jeff Layton87fa5592012-12-11 12:10:11 -0500354 unsigned int lookup_flags = LOOKUP_FOLLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
357 return -EINVAL;
358
David Howellsd84f4f92008-11-14 10:39:23 +1100359 override_cred = prepare_creds();
360 if (!override_cred)
361 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
David Howellsd84f4f92008-11-14 10:39:23 +1100363 override_cred->fsuid = override_cred->uid;
364 override_cred->fsgid = override_cred->gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Andrew G. Morgan086f7312008-07-04 09:59:58 -0700366 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
David Howells1cdcbec2008-11-14 10:39:14 +1100367 /* Clear the capabilities if we switch to a non-root user */
Eric W. Biederman18815a12012-02-07 16:45:47 -0800368 kuid_t root_uid = make_kuid(override_cred->user_ns, 0);
369 if (!uid_eq(override_cred->uid, root_uid))
David Howellsd84f4f92008-11-14 10:39:23 +1100370 cap_clear(override_cred->cap_effective);
Andrew G. Morgan086f7312008-07-04 09:59:58 -0700371 else
David Howellsd84f4f92008-11-14 10:39:23 +1100372 override_cred->cap_effective =
373 override_cred->cap_permitted;
Andrew G. Morgan086f7312008-07-04 09:59:58 -0700374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
David Howellsd84f4f92008-11-14 10:39:23 +1100376 old_cred = override_creds(override_cred);
Jeff Layton87fa5592012-12-11 12:10:11 -0500377retry:
378 res = user_path_at(dfd, filename, lookup_flags, &path);
Dave Hansen6902d922006-09-30 23:29:01 -0700379 if (res)
380 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
David Howells63afdfc2015-05-06 15:59:00 +0100382 inode = d_backing_inode(path.dentry);
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -0700383 mnt = path.mnt;
Al Viro256984a2008-07-22 08:09:30 -0400384
385 if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
Al Viro30524472008-07-22 00:02:33 -0400386 /*
387 * MAY_EXEC on regular files is denied if the fs is mounted
388 * with the "noexec" flag.
389 */
390 res = -EACCES;
Eric W. Biederman90f85722015-06-29 14:42:03 -0500391 if (path_noexec(&path))
Al Viro30524472008-07-22 00:02:33 -0400392 goto out_path_release;
393 }
394
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -0700395 res = inode_permission2(mnt, inode, mode | MAY_ACCESS);
Dave Hansen6902d922006-09-30 23:29:01 -0700396 /* SuS v2 requires we report a read only fs too */
Al Viro256984a2008-07-22 08:09:30 -0400397 if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
Dave Hansen6902d922006-09-30 23:29:01 -0700398 goto out_path_release;
Dave Hansen2f676cb2008-02-15 14:37:55 -0800399 /*
400 * This is a rare case where using __mnt_is_readonly()
401 * is OK without a mnt_want/drop_write() pair. Since
402 * no actual write to the fs is performed here, we do
403 * not need to telegraph to that to anyone.
404 *
405 * By doing this, we accept that this access is
406 * inherently racy and know that the fs may change
407 * state before we even see this result.
408 */
Al Viro2d8f3032008-07-22 09:59:21 -0400409 if (__mnt_is_readonly(path.mnt))
Dave Hansen6902d922006-09-30 23:29:01 -0700410 res = -EROFS;
411
412out_path_release:
Al Viro2d8f3032008-07-22 09:59:21 -0400413 path_put(&path);
Jeff Layton87fa5592012-12-11 12:10:11 -0500414 if (retry_estale(res, lookup_flags)) {
415 lookup_flags |= LOOKUP_REVAL;
416 goto retry;
417 }
Dave Hansen6902d922006-09-30 23:29:01 -0700418out:
David Howellsd84f4f92008-11-14 10:39:23 +1100419 revert_creds(old_cred);
420 put_cred(override_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return res;
422}
423
Heiko Carstensca013e92009-01-14 14:14:19 +0100424SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800425{
426 return sys_faccessat(AT_FDCWD, filename, mode);
427}
428
Heiko Carstens3cdad422009-01-14 14:14:22 +0100429SYSCALL_DEFINE1(chdir, const char __user *, filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Al Viro2d8f3032008-07-22 09:59:21 -0400431 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 int error;
Jeff Layton0291c0a2012-12-11 12:10:12 -0500433 unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
434retry:
435 error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (error)
437 goto out;
438
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -0700439 error = inode_permission2(path.mnt, path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (error)
441 goto dput_and_out;
442
Al Viro2d8f3032008-07-22 09:59:21 -0400443 set_fs_pwd(current->fs, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445dput_and_out:
Al Viro2d8f3032008-07-22 09:59:21 -0400446 path_put(&path);
Jeff Layton0291c0a2012-12-11 12:10:12 -0500447 if (retry_estale(error, lookup_flags)) {
448 lookup_flags |= LOOKUP_REVAL;
449 goto retry;
450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451out:
452 return error;
453}
454
Heiko Carstens3cdad422009-01-14 14:14:22 +0100455SYSCALL_DEFINE1(fchdir, unsigned int, fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Al Viro2903ff02012-08-28 12:52:22 -0400457 struct fd f = fdget_raw(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 struct inode *inode;
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -0700459 struct vfsmount *mnt;
Al Viro2903ff02012-08-28 12:52:22 -0400460 int error = -EBADF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400463 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 goto out;
465
Al Viro496ad9a2013-01-23 17:07:38 -0500466 inode = file_inode(f.file);
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -0700467 mnt = f.file->f_path.mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 error = -ENOTDIR;
470 if (!S_ISDIR(inode->i_mode))
471 goto out_putf;
472
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -0700473 error = inode_permission2(mnt, inode, MAY_EXEC | MAY_CHDIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (!error)
Al Viro2903ff02012-08-28 12:52:22 -0400475 set_fs_pwd(current->fs, &f.file->f_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476out_putf:
Al Viro2903ff02012-08-28 12:52:22 -0400477 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478out:
479 return error;
480}
481
Heiko Carstens3480b252009-01-14 14:14:16 +0100482SYSCALL_DEFINE1(chroot, const char __user *, filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Al Viro2d8f3032008-07-22 09:59:21 -0400484 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 int error;
Jeff Layton27712612012-12-20 17:08:32 -0500486 unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
487retry:
488 error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (error)
490 goto out;
491
Daniel Rosenbergdb2d40b2016-10-26 16:27:45 -0700492 error = inode_permission2(path.mnt, path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (error)
494 goto dput_and_out;
495
496 error = -EPERM;
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700497 if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 goto dput_and_out;
Tetsuo Handa8b8efb42009-10-04 21:49:48 +0900499 error = security_path_chroot(&path);
500 if (error)
501 goto dput_and_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Al Viro2d8f3032008-07-22 09:59:21 -0400503 set_fs_root(current->fs, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 error = 0;
505dput_and_out:
Al Viro2d8f3032008-07-22 09:59:21 -0400506 path_put(&path);
Jeff Layton27712612012-12-20 17:08:32 -0500507 if (retry_estale(error, lookup_flags)) {
508 lookup_flags |= LOOKUP_REVAL;
509 goto retry;
510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511out:
512 return error;
513}
514
Al Viroe57712e2011-07-26 04:15:54 -0400515static int chmod_common(struct path *path, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Al Viroe57712e2011-07-26 04:15:54 -0400517 struct inode *inode = path->dentry->d_inode;
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -0400518 struct inode *delegated_inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 struct iattr newattrs;
Al Viroe57712e2011-07-26 04:15:54 -0400520 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Al Viroe57712e2011-07-26 04:15:54 -0400522 error = mnt_want_write(path->mnt);
523 if (error)
524 return error;
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -0400525retry_deleg:
Tetsuo Handafe542cf2009-11-22 11:49:55 +0900526 mutex_lock(&inode->i_mutex);
Al Virocdcf1162011-12-08 10:51:53 -0500527 error = security_path_chmod(path, mode);
Al Viroe57712e2011-07-26 04:15:54 -0400528 if (error)
Tetsuo Handafe542cf2009-11-22 11:49:55 +0900529 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
531 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
Daniel Rosenberge5eeaaf2016-10-26 16:33:11 -0700532 error = notify_change2(path->mnt, path->dentry, &newattrs, &delegated_inode);
Tetsuo Handafe542cf2009-11-22 11:49:55 +0900533out_unlock:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800534 mutex_unlock(&inode->i_mutex);
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -0400535 if (delegated_inode) {
536 error = break_deleg_wait(&delegated_inode);
537 if (!error)
538 goto retry_deleg;
539 }
Al Viroe57712e2011-07-26 04:15:54 -0400540 mnt_drop_write(path->mnt);
541 return error;
542}
543
Al Viro49f0a072011-07-26 04:22:01 -0400544SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode)
Al Viroe57712e2011-07-26 04:15:54 -0400545{
Al Viro173c8402013-08-30 12:48:53 -0400546 struct fd f = fdget(fd);
Al Viroe57712e2011-07-26 04:15:54 -0400547 int err = -EBADF;
548
Al Viro173c8402013-08-30 12:48:53 -0400549 if (f.file) {
Al Viro9f45f5b2014-10-31 17:44:57 -0400550 audit_file(f.file);
Al Viro173c8402013-08-30 12:48:53 -0400551 err = chmod_common(&f.file->f_path, mode);
552 fdput(f);
Al Viroe57712e2011-07-26 04:15:54 -0400553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return err;
555}
556
Al Viro49f0a072011-07-26 04:22:01 -0400557SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Al Viro2d8f3032008-07-22 09:59:21 -0400559 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 int error;
Jeff Layton14ff6902012-12-11 12:10:13 -0500561 unsigned int lookup_flags = LOOKUP_FOLLOW;
562retry:
563 error = user_path_at(dfd, filename, lookup_flags, &path);
Al Viroe57712e2011-07-26 04:15:54 -0400564 if (!error) {
565 error = chmod_common(&path, mode);
566 path_put(&path);
Jeff Layton14ff6902012-12-11 12:10:13 -0500567 if (retry_estale(error, lookup_flags)) {
568 lookup_flags |= LOOKUP_REVAL;
569 goto retry;
570 }
Al Viroe57712e2011-07-26 04:15:54 -0400571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return error;
573}
574
Al Viro49f0a072011-07-26 04:22:01 -0400575SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800576{
577 return sys_fchmodat(AT_FDCWD, filename, mode);
578}
579
Tetsuo Handafe542cf2009-11-22 11:49:55 +0900580static int chown_common(struct path *path, uid_t user, gid_t group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Tetsuo Handafe542cf2009-11-22 11:49:55 +0900582 struct inode *inode = path->dentry->d_inode;
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -0400583 struct inode *delegated_inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 int error;
585 struct iattr newattrs;
Eric W. Biederman52137ab2012-03-03 19:52:01 -0800586 kuid_t uid;
587 kgid_t gid;
588
589 uid = make_kuid(current_user_ns(), user);
590 gid = make_kgid(current_user_ns(), group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Andrew Elblec1b89402015-02-23 08:51:24 -0500592retry_deleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 newattrs.ia_valid = ATTR_CTIME;
594 if (user != (uid_t) -1) {
Eric W. Biederman52137ab2012-03-03 19:52:01 -0800595 if (!uid_valid(uid))
596 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 newattrs.ia_valid |= ATTR_UID;
Eric W. Biederman52137ab2012-03-03 19:52:01 -0800598 newattrs.ia_uid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
600 if (group != (gid_t) -1) {
Eric W. Biederman52137ab2012-03-03 19:52:01 -0800601 if (!gid_valid(gid))
602 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 newattrs.ia_valid |= ATTR_GID;
Eric W. Biederman52137ab2012-03-03 19:52:01 -0800604 newattrs.ia_gid = gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606 if (!S_ISDIR(inode->i_mode))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700607 newattrs.ia_valid |=
608 ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800609 mutex_lock(&inode->i_mutex);
Eric W. Biedermand2b31ca2012-06-01 16:14:19 -0600610 error = security_path_chown(path, uid, gid);
Tetsuo Handafe542cf2009-11-22 11:49:55 +0900611 if (!error)
Daniel Rosenberge5eeaaf2016-10-26 16:33:11 -0700612 error = notify_change2(path->mnt, path->dentry, &newattrs, &delegated_inode);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800613 mutex_unlock(&inode->i_mutex);
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -0400614 if (delegated_inode) {
615 error = break_deleg_wait(&delegated_inode);
616 if (!error)
617 goto retry_deleg;
618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return error;
620}
621
Heiko Carstens6559eed82009-01-14 14:14:32 +0100622SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
623 gid_t, group, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800624{
Al Viro2d8f3032008-07-22 09:59:21 -0400625 struct path path;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800626 int error = -EINVAL;
Al Viro65cfc672011-03-13 15:56:26 -0400627 int lookup_flags;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800628
Al Viro65cfc672011-03-13 15:56:26 -0400629 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800630 goto out;
631
Al Viro65cfc672011-03-13 15:56:26 -0400632 lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
633 if (flag & AT_EMPTY_PATH)
634 lookup_flags |= LOOKUP_EMPTY;
Jeff Layton99a5df32012-12-11 12:10:13 -0500635retry:
Al Viro65cfc672011-03-13 15:56:26 -0400636 error = user_path_at(dfd, filename, lookup_flags, &path);
Dave Hansen6902d922006-09-30 23:29:01 -0700637 if (error)
638 goto out;
Al Viro2d8f3032008-07-22 09:59:21 -0400639 error = mnt_want_write(path.mnt);
Dave Hansen2af482a2008-02-15 14:37:50 -0800640 if (error)
641 goto out_release;
Tetsuo Handafe542cf2009-11-22 11:49:55 +0900642 error = chown_common(&path, user, group);
Al Viro2d8f3032008-07-22 09:59:21 -0400643 mnt_drop_write(path.mnt);
Dave Hansen2af482a2008-02-15 14:37:50 -0800644out_release:
Al Viro2d8f3032008-07-22 09:59:21 -0400645 path_put(&path);
Jeff Layton99a5df32012-12-11 12:10:13 -0500646 if (retry_estale(error, lookup_flags)) {
647 lookup_flags |= LOOKUP_REVAL;
648 goto retry;
649 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800650out:
651 return error;
652}
653
David Howells55e4def2012-06-25 12:55:09 +0100654SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
655{
656 return sys_fchownat(AT_FDCWD, filename, user, group, 0);
657}
658
Heiko Carstensca013e92009-01-14 14:14:19 +0100659SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
David Howells55e4def2012-06-25 12:55:09 +0100661 return sys_fchownat(AT_FDCWD, filename, user, group,
662 AT_SYMLINK_NOFOLLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
Heiko Carstensca013e92009-01-14 14:14:19 +0100665SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Al Viro2903ff02012-08-28 12:52:22 -0400667 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 int error = -EBADF;
669
Al Viro2903ff02012-08-28 12:52:22 -0400670 if (!f.file)
Dave Hansen6902d922006-09-30 23:29:01 -0700671 goto out;
672
Al Viro2903ff02012-08-28 12:52:22 -0400673 error = mnt_want_write_file(f.file);
Dave Hansen2af482a2008-02-15 14:37:50 -0800674 if (error)
675 goto out_fput;
Al Viro9f45f5b2014-10-31 17:44:57 -0400676 audit_file(f.file);
Al Viro2903ff02012-08-28 12:52:22 -0400677 error = chown_common(&f.file->f_path, user, group);
678 mnt_drop_write_file(f.file);
Dave Hansen2af482a2008-02-15 14:37:50 -0800679out_fput:
Al Viro2903ff02012-08-28 12:52:22 -0400680 fdput(f);
Dave Hansen6902d922006-09-30 23:29:01 -0700681out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return error;
683}
684
Miklos Szeredi90ad1a82012-05-21 17:30:15 +0200685int open_check_o_direct(struct file *f)
686{
687 /* NB: we're sure to have correct a_ops only after f_op->open */
688 if (f->f_flags & O_DIRECT) {
Matthew Wilcoxe748dcd2015-02-16 15:59:12 -0800689 if (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO)
Miklos Szeredi90ad1a82012-05-21 17:30:15 +0200690 return -EINVAL;
Miklos Szeredi90ad1a82012-05-21 17:30:15 +0200691 }
692 return 0;
693}
694
Al Viro02e51802012-06-10 14:32:45 -0400695static int do_dentry_open(struct file *f,
David Howells4bacc9c2015-06-18 14:32:31 +0100696 struct inode *inode,
Al Viro96b7e572012-06-10 14:22:04 -0400697 int (*open)(struct inode *, struct file *),
698 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Al Viro1abf0c72011-03-13 03:51:11 -0400700 static const struct file_operations empty_fops = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 int error;
702
Al Viro53009902009-12-19 10:15:07 -0500703 f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700704 FMODE_PREAD | FMODE_PWRITE;
Al Viro1abf0c72011-03-13 03:51:11 -0400705
Al Virob5bcdda2012-07-20 23:28:46 +0400706 path_get(&f->f_path);
David Howells4bacc9c2015-06-18 14:32:31 +0100707 f->f_inode = inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 f->f_mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Al Viro3f4d5a02014-03-14 09:43:29 -0400710 if (unlikely(f->f_flags & O_PATH)) {
711 f->f_mode = FMODE_PATH;
Al Viro1abf0c72011-03-13 03:51:11 -0400712 f->f_op = &empty_fops;
Al Viro96b7e572012-06-10 14:22:04 -0400713 return 0;
Al Viro1abf0c72011-03-13 03:51:11 -0400714 }
715
Al Virodd209082014-03-14 10:56:20 -0400716 if (f->f_mode & FMODE_WRITE && !special_file(inode->i_mode)) {
Al Viro0ccb2862014-03-14 10:40:46 -0400717 error = get_write_access(inode);
Al Viro3f4d5a02014-03-14 09:43:29 -0400718 if (unlikely(error))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 goto cleanup_file;
Al Viro0ccb2862014-03-14 10:40:46 -0400720 error = __mnt_want_write(f->f_path.mnt);
Al Viro3f4d5a02014-03-14 09:43:29 -0400721 if (unlikely(error)) {
Al Viro0ccb2862014-03-14 10:40:46 -0400722 put_write_access(inode);
723 goto cleanup_file;
724 }
Al Viro83f936c2014-03-14 12:02:47 -0400725 f->f_mode |= FMODE_WRITER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727
Linus Torvalds9c225f22014-03-03 09:36:58 -0800728 /* POSIX.1-2008/SUSv4 Section XSI 2.9.7 */
729 if (S_ISREG(inode->i_mode))
730 f->f_mode |= FMODE_ATOMIC_POS;
731
Al Viro1abf0c72011-03-13 03:51:11 -0400732 f->f_op = fops_get(inode->i_fop);
Al Viro72c2d532013-09-22 16:27:52 -0400733 if (unlikely(WARN_ON(!f->f_op))) {
734 error = -ENODEV;
735 goto cleanup_all;
736 }
Al Viro1abf0c72011-03-13 03:51:11 -0400737
Eric Paris83d49852012-04-04 13:45:40 -0400738 error = security_file_open(f, cred);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +0900739 if (error)
740 goto cleanup_all;
741
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -0400742 error = break_lease(inode, f->f_flags);
743 if (error)
744 goto cleanup_all;
745
Al Viro72c2d532013-09-22 16:27:52 -0400746 if (!open)
Trond Myklebust834f2a42005-10-18 14:20:16 -0700747 open = f->f_op->open;
748 if (open) {
749 error = open(inode, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (error)
751 goto cleanup_all;
752 }
Mimi Zohar890275b52010-11-02 10:13:07 -0400753 if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
754 i_readcount_inc(inode);
Al Viro293bc982014-02-11 18:37:41 -0500755 if ((f->f_mode & FMODE_READ) &&
Al Viro84363182015-04-04 01:14:53 -0400756 likely(f->f_op->read || f->f_op->read_iter))
Al Viro7f7f25e2014-02-11 17:49:24 -0500757 f->f_mode |= FMODE_CAN_READ;
Al Viro293bc982014-02-11 18:37:41 -0500758 if ((f->f_mode & FMODE_WRITE) &&
Al Viro84363182015-04-04 01:14:53 -0400759 likely(f->f_op->write || f->f_op->write_iter))
Al Viro7f7f25e2014-02-11 17:49:24 -0500760 f->f_mode |= FMODE_CAN_WRITE;
Trond Myklebust834f2a42005-10-18 14:20:16 -0700761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
763
764 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
765
Al Viro96b7e572012-06-10 14:22:04 -0400766 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768cleanup_all:
769 fops_put(f->f_op);
Al Viro83f936c2014-03-14 12:02:47 -0400770 if (f->f_mode & FMODE_WRITER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 put_write_access(inode);
Al Viro83f936c2014-03-14 12:02:47 -0400772 __mnt_drop_write(f->f_path.mnt);
Dave Hansen4a3fd212008-02-15 14:37:48 -0800773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774cleanup_file:
Al Viro02e51802012-06-10 14:32:45 -0400775 path_put(&f->f_path);
776 f->f_path.mnt = NULL;
777 f->f_path.dentry = NULL;
Al Virodd379782013-03-01 19:48:30 -0500778 f->f_inode = NULL;
Al Viro96b7e572012-06-10 14:22:04 -0400779 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
Trond Myklebust834f2a42005-10-18 14:20:16 -0700782/**
Miklos Szeredid18e9002012-06-05 15:10:17 +0200783 * finish_open - finish opening a file
Miklos Szeredi0854d452013-09-16 14:51:55 +0200784 * @file: file pointer
Miklos Szeredid18e9002012-06-05 15:10:17 +0200785 * @dentry: pointer to dentry
786 * @open: open callback
Miklos Szeredi0854d452013-09-16 14:51:55 +0200787 * @opened: state of open
Miklos Szeredid18e9002012-06-05 15:10:17 +0200788 *
789 * This can be used to finish opening a file passed to i_op->atomic_open().
790 *
791 * If the open callback is set to NULL, then the standard f_op->open()
792 * filesystem callback is substituted.
Miklos Szeredi0854d452013-09-16 14:51:55 +0200793 *
794 * NB: the dentry reference is _not_ consumed. If, for example, the dentry is
795 * the return value of d_splice_alias(), then the caller needs to perform dput()
796 * on it after finish_open().
797 *
798 * On successful return @file is a fully instantiated open file. After this, if
799 * an error occurs in ->atomic_open(), it needs to clean up with fput().
800 *
801 * Returns zero on success or -errno if the open failed.
Miklos Szeredid18e9002012-06-05 15:10:17 +0200802 */
Al Viro30d90492012-06-22 12:40:19 +0400803int finish_open(struct file *file, struct dentry *dentry,
804 int (*open)(struct inode *, struct file *),
805 int *opened)
Miklos Szeredid18e9002012-06-05 15:10:17 +0200806{
Al Viro96b7e572012-06-10 14:22:04 -0400807 int error;
Al Viro3d8a00d2012-06-10 05:04:43 -0400808 BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
Miklos Szeredid18e9002012-06-05 15:10:17 +0200809
Al Virob5bcdda2012-07-20 23:28:46 +0400810 file->f_path.dentry = dentry;
David Howells4bacc9c2015-06-18 14:32:31 +0100811 error = do_dentry_open(file, d_backing_inode(dentry), open,
812 current_cred());
Al Viro96b7e572012-06-10 14:22:04 -0400813 if (!error)
Al Viro47237682012-06-10 05:01:45 -0400814 *opened |= FILE_OPENED;
Miklos Szeredid18e9002012-06-05 15:10:17 +0200815
Al Viro96b7e572012-06-10 14:22:04 -0400816 return error;
Miklos Szeredid18e9002012-06-05 15:10:17 +0200817}
818EXPORT_SYMBOL(finish_open);
819
820/**
821 * finish_no_open - finish ->atomic_open() without opening the file
822 *
Miklos Szeredi0854d452013-09-16 14:51:55 +0200823 * @file: file pointer
Miklos Szeredid18e9002012-06-05 15:10:17 +0200824 * @dentry: dentry or NULL (as returned from ->lookup())
825 *
826 * This can be used to set the result of a successful lookup in ->atomic_open().
Miklos Szeredi0854d452013-09-16 14:51:55 +0200827 *
828 * NB: unlike finish_open() this function does consume the dentry reference and
829 * the caller need not dput() it.
830 *
831 * Returns "1" which must be the return value of ->atomic_open() after having
832 * called this function.
Miklos Szeredid18e9002012-06-05 15:10:17 +0200833 */
Al Viroe45198a2012-06-10 06:48:09 -0400834int finish_no_open(struct file *file, struct dentry *dentry)
Miklos Szeredid18e9002012-06-05 15:10:17 +0200835{
Al Viro30d90492012-06-22 12:40:19 +0400836 file->f_path.dentry = dentry;
Al Viroe45198a2012-06-10 06:48:09 -0400837 return 1;
Miklos Szeredid18e9002012-06-05 15:10:17 +0200838}
839EXPORT_SYMBOL(finish_no_open);
840
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +0200841char *file_path(struct file *filp, char *buf, int buflen)
842{
843 return d_path(&filp->f_path, buf, buflen);
844}
845EXPORT_SYMBOL(file_path);
846
David Howells4bacc9c2015-06-18 14:32:31 +0100847/**
848 * vfs_open - open the file at the given path
849 * @path: path to open
850 * @file: newly allocated file with f_flag initialized
851 * @cred: credentials to use
852 */
853int vfs_open(const struct path *path, struct file *file,
854 const struct cred *cred)
855{
Miklos Szeredib0dac612016-05-11 01:16:37 +0200856 struct inode *inode = vfs_select_inode(path->dentry, file->f_flags);
857
858 if (IS_ERR(inode))
859 return PTR_ERR(inode);
David Howells4bacc9c2015-06-18 14:32:31 +0100860
861 file->f_path = *path;
David Howells4bacc9c2015-06-18 14:32:31 +0100862 return do_dentry_open(file, inode, NULL, cred);
863}
864
Al Viro765927b2012-06-26 21:58:53 +0400865struct file *dentry_open(const struct path *path, int flags,
David Howells745ca242008-11-14 10:39:22 +1100866 const struct cred *cred)
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700867{
868 int error;
869 struct file *f;
870
David Howellse0e81732009-09-02 09:13:40 +0100871 validate_creds(cred);
872
Tetsuo Handac212f9a2011-01-19 21:08:41 +0900873 /* We must always pass in a valid mount pointer. */
Al Viro765927b2012-06-26 21:58:53 +0400874 BUG_ON(!path->mnt);
Christoph Hellwig322ee5b2008-02-15 14:37:24 -0800875
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700876 f = get_empty_filp();
Al Viro1afc99b2013-02-14 20:41:04 -0500877 if (!IS_ERR(f)) {
878 f->f_flags = flags;
Miklos Szeredi4aa7c632014-10-24 00:14:35 +0200879 error = vfs_open(path, f, cred);
Al Viro1afc99b2013-02-14 20:41:04 -0500880 if (!error) {
881 /* from now on we need fput() to dispose of f */
882 error = open_check_o_direct(f);
883 if (error) {
884 fput(f);
885 f = ERR_PTR(error);
886 }
887 } else {
888 put_filp(f);
Al Viro2a027e72012-06-10 14:24:38 -0400889 f = ERR_PTR(error);
890 }
Al Viro2a027e72012-06-10 14:24:38 -0400891 }
892 return f;
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700893}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894EXPORT_SYMBOL(dentry_open);
895
Al Viroa218d0f2011-11-21 14:59:34 -0500896static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op)
Al Viro47c805d2011-02-23 17:44:09 -0500897{
898 int lookup_flags = 0;
899 int acc_mode;
900
Christoph Hellwig4043d5b2017-04-27 09:42:25 +0200901 /*
902 * Clear out all open flags we don't know about so that we don't report
903 * them in fcntl(F_GETFD) or similar interfaces.
904 */
905 flags &= VALID_OPEN_FLAGS;
906
Andy Lutomirskie305f482013-08-01 21:07:52 -0700907 if (flags & (O_CREAT | __O_TMPFILE))
Miklos Szeredie68726f2012-08-15 13:01:24 +0200908 op->mode = (mode & S_IALLUGO) | S_IFREG;
909 else
910 op->mode = 0;
Al Viro47c805d2011-02-23 17:44:09 -0500911
912 /* Must never be set by userspace */
Al Viroc6f3d812012-08-26 11:01:04 -0400913 flags &= ~FMODE_NONOTIFY & ~O_CLOEXEC;
Al Viro47c805d2011-02-23 17:44:09 -0500914
915 /*
916 * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
917 * check for O_DSYNC if the need any syncing at all we enforce it's
918 * always set instead of having to deal with possibly weird behaviour
919 * for malicious applications setting only __O_SYNC.
920 */
921 if (flags & __O_SYNC)
922 flags |= O_DSYNC;
923
Al Virobb458c62013-07-13 13:26:37 +0400924 if (flags & __O_TMPFILE) {
925 if ((flags & O_TMPFILE_MASK) != O_TMPFILE)
Al Viro60545d02013-06-07 01:20:27 -0400926 return -EINVAL;
927 acc_mode = MAY_OPEN | ACC_MODE(flags);
Al Viroba57ea62013-07-20 03:11:32 +0400928 if (!(acc_mode & MAY_WRITE))
929 return -EINVAL;
Al Viro60545d02013-06-07 01:20:27 -0400930 } else if (flags & O_PATH) {
931 /*
932 * If we have O_PATH in the open flag. Then we
933 * cannot have anything other than the below set of flags
934 */
Al Viro1abf0c72011-03-13 03:51:11 -0400935 flags &= O_DIRECTORY | O_NOFOLLOW | O_PATH;
936 acc_mode = 0;
937 } else {
938 acc_mode = MAY_OPEN | ACC_MODE(flags);
939 }
Al Viro47c805d2011-02-23 17:44:09 -0500940
Al Viro1abf0c72011-03-13 03:51:11 -0400941 op->open_flag = flags;
Al Viro47c805d2011-02-23 17:44:09 -0500942
943 /* O_TRUNC implies we need access checks for write permissions */
944 if (flags & O_TRUNC)
945 acc_mode |= MAY_WRITE;
946
947 /* Allow the LSM permission hook to distinguish append
948 access from general write access. */
949 if (flags & O_APPEND)
950 acc_mode |= MAY_APPEND;
951
952 op->acc_mode = acc_mode;
953
Al Viro1abf0c72011-03-13 03:51:11 -0400954 op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
955
Al Viro47c805d2011-02-23 17:44:09 -0500956 if (flags & O_CREAT) {
957 op->intent |= LOOKUP_CREATE;
958 if (flags & O_EXCL)
959 op->intent |= LOOKUP_EXCL;
960 }
961
962 if (flags & O_DIRECTORY)
963 lookup_flags |= LOOKUP_DIRECTORY;
964 if (!(flags & O_NOFOLLOW))
965 lookup_flags |= LOOKUP_FOLLOW;
Al Virof9652e12013-06-11 08:23:01 +0400966 op->lookup_flags = lookup_flags;
967 return 0;
Al Viro47c805d2011-02-23 17:44:09 -0500968}
969
970/**
Jeff Layton669abf42012-10-10 16:43:10 -0400971 * file_open_name - open file and return file pointer
972 *
973 * @name: struct filename containing path to open
974 * @flags: open flags as per the open(2) second argument
975 * @mode: mode for the new file if O_CREAT is set, else ignored
976 *
977 * This is the helper to open a file from kernelspace if you really
978 * have to. But in generally you should not do this, so please move
979 * along, nothing to see here..
980 */
981struct file *file_open_name(struct filename *name, int flags, umode_t mode)
982{
983 struct open_flags op;
Al Virof9652e12013-06-11 08:23:01 +0400984 int err = build_open_flags(flags, mode, &op);
985 return err ? ERR_PTR(err) : do_filp_open(AT_FDCWD, name, &op);
Jeff Layton669abf42012-10-10 16:43:10 -0400986}
987
988/**
Al Viro47c805d2011-02-23 17:44:09 -0500989 * filp_open - open file and return file pointer
990 *
991 * @filename: path to open
992 * @flags: open flags as per the open(2) second argument
993 * @mode: mode for the new file if O_CREAT is set, else ignored
994 *
995 * This is the helper to open a file from kernelspace if you really
996 * have to. But in generally you should not do this, so please move
997 * along, nothing to see here..
998 */
Al Viroa218d0f2011-11-21 14:59:34 -0500999struct file *filp_open(const char *filename, int flags, umode_t mode)
Al Viro47c805d2011-02-23 17:44:09 -05001000{
Paul Moore51689102015-01-22 00:00:03 -05001001 struct filename *name = getname_kernel(filename);
1002 struct file *file = ERR_CAST(name);
1003
1004 if (!IS_ERR(name)) {
1005 file = file_open_name(name, flags, mode);
1006 putname(name);
1007 }
1008 return file;
Al Viro47c805d2011-02-23 17:44:09 -05001009}
1010EXPORT_SYMBOL(filp_open);
1011
Al Viro73d049a2011-03-11 12:08:24 -05001012struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt,
Jann Horn74b23f72016-03-22 14:25:36 -07001013 const char *filename, int flags, umode_t mode)
Al Viro73d049a2011-03-11 12:08:24 -05001014{
1015 struct open_flags op;
Jann Horn74b23f72016-03-22 14:25:36 -07001016 int err = build_open_flags(flags, mode, &op);
Al Virof9652e12013-06-11 08:23:01 +04001017 if (err)
1018 return ERR_PTR(err);
Al Virof9652e12013-06-11 08:23:01 +04001019 return do_file_open_root(dentry, mnt, filename, &op);
Al Viro73d049a2011-03-11 12:08:24 -05001020}
1021EXPORT_SYMBOL(file_open_root);
1022
Al Viroa218d0f2011-11-21 14:59:34 -05001023long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Al Viro47c805d2011-02-23 17:44:09 -05001025 struct open_flags op;
Al Virof9652e12013-06-11 08:23:01 +04001026 int fd = build_open_flags(flags, mode, &op);
1027 struct filename *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Al Virof9652e12013-06-11 08:23:01 +04001029 if (fd)
1030 return fd;
1031
1032 tmp = getname(filename);
1033 if (IS_ERR(tmp))
1034 return PTR_ERR(tmp);
1035
1036 fd = get_unused_fd_flags(flags);
1037 if (fd >= 0) {
1038 struct file *f = do_filp_open(dfd, tmp, &op);
1039 if (IS_ERR(f)) {
1040 put_unused_fd(fd);
1041 fd = PTR_ERR(f);
1042 } else {
1043 fsnotify_open(f);
1044 fd_install(fd, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
Al Virof9652e12013-06-11 08:23:01 +04001047 putname(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
Miklos Szeredie922efc2005-09-06 15:18:25 -07001050
Al Viroa218d0f2011-11-21 14:59:34 -05001051SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
Miklos Szeredie922efc2005-09-06 15:18:25 -07001052{
1053 if (force_o_largefile())
1054 flags |= O_LARGEFILE;
1055
Al Viro2cf09662013-01-21 15:25:54 -05001056 return do_sys_open(AT_FDCWD, filename, flags, mode);
Miklos Szeredie922efc2005-09-06 15:18:25 -07001057}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Heiko Carstens6559eed82009-01-14 14:14:32 +01001059SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
Al Viroa218d0f2011-11-21 14:59:34 -05001060 umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001061{
1062 if (force_o_largefile())
1063 flags |= O_LARGEFILE;
1064
Al Viro2cf09662013-01-21 15:25:54 -05001065 return do_sys_open(dfd, filename, flags, mode);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001066}
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068#ifndef __alpha__
1069
1070/*
1071 * For backward compatibility? Maybe this should be moved
1072 * into arch/i386 instead?
1073 */
Al Viroa218d0f2011-11-21 14:59:34 -05001074SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
1076 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
1077}
1078
1079#endif
1080
1081/*
1082 * "id" is the POSIX thread ID. We use the
1083 * files pointer for this..
1084 */
1085int filp_close(struct file *filp, fl_owner_t id)
1086{
Christoph Lameter45778ca2005-06-23 00:10:17 -07001087 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 if (!file_count(filp)) {
1090 printk(KERN_ERR "VFS: Close: file count is 0\n");
Christoph Lameter45778ca2005-06-23 00:10:17 -07001091 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
1093
Al Viro72c2d532013-09-22 16:27:52 -04001094 if (filp->f_op->flush)
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -07001095 retval = filp->f_op->flush(filp, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Al Viro1abf0c72011-03-13 03:51:11 -04001097 if (likely(!(filp->f_mode & FMODE_PATH))) {
1098 dnotify_flush(filp, id);
1099 locks_remove_posix(filp, id);
1100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 fput(filp);
1102 return retval;
1103}
1104
1105EXPORT_SYMBOL(filp_close);
1106
1107/*
1108 * Careful here! We test whether the file pointer is NULL before
1109 * releasing the fd. This ensures that one clone task can't release
1110 * an fd while another clone is opening it.
1111 */
Heiko Carstensca013e92009-01-14 14:14:19 +01001112SYSCALL_DEFINE1(close, unsigned int, fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Al Viro483ce1d2012-08-19 12:04:24 -04001114 int retval = __close_fd(current->files, fd);
Ernie Petridesee731f42006-09-29 02:00:13 -07001115
1116 /* can't restart close syscall because file table entry was cleared */
1117 if (unlikely(retval == -ERESTARTSYS ||
1118 retval == -ERESTARTNOINTR ||
1119 retval == -ERESTARTNOHAND ||
1120 retval == -ERESTART_RESTARTBLOCK))
1121 retval = -EINTR;
1122
1123 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125EXPORT_SYMBOL(sys_close);
1126
1127/*
1128 * This routine simulates a hangup on the tty, to arrange that users
1129 * are given clean terminals at login time.
1130 */
Heiko Carstensca013e92009-01-14 14:14:19 +01001131SYSCALL_DEFINE0(vhangup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
1133 if (capable(CAP_SYS_TTY_CONFIG)) {
Alan Cox2cb59982008-10-13 10:40:30 +01001134 tty_vhangup_self();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return 0;
1136 }
1137 return -EPERM;
1138}
1139
1140/*
1141 * Called when an inode is about to be open.
1142 * We use this to disallow opening large files on 32bit systems if
1143 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1144 * on this flag in sys_open.
1145 */
1146int generic_file_open(struct inode * inode, struct file * filp)
1147{
1148 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
Alan Coxa9c62a12007-10-16 23:30:22 -07001149 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return 0;
1151}
1152
1153EXPORT_SYMBOL(generic_file_open);
1154
1155/*
1156 * This is used by subsystems that don't want seekable
Dmitry Torokhov06b1e102010-08-10 18:01:33 -07001157 * file descriptors. The function is not supposed to ever fail, the only
1158 * reason it returns an 'int' and not 'void' is so that it can be plugged
1159 * directly into file_operations structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 */
1161int nonseekable_open(struct inode *inode, struct file *filp)
1162{
1163 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1164 return 0;
1165}
1166
1167EXPORT_SYMBOL(nonseekable_open);