162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci *  linux/fs/open.c
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci *  Copyright (C) 1991, 1992  Linus Torvalds
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/string.h>
962306a36Sopenharmony_ci#include <linux/mm.h>
1062306a36Sopenharmony_ci#include <linux/file.h>
1162306a36Sopenharmony_ci#include <linux/fdtable.h>
1262306a36Sopenharmony_ci#include <linux/fsnotify.h>
1362306a36Sopenharmony_ci#include <linux/module.h>
1462306a36Sopenharmony_ci#include <linux/tty.h>
1562306a36Sopenharmony_ci#include <linux/namei.h>
1662306a36Sopenharmony_ci#include <linux/backing-dev.h>
1762306a36Sopenharmony_ci#include <linux/capability.h>
1862306a36Sopenharmony_ci#include <linux/securebits.h>
1962306a36Sopenharmony_ci#include <linux/security.h>
2062306a36Sopenharmony_ci#include <linux/mount.h>
2162306a36Sopenharmony_ci#include <linux/fcntl.h>
2262306a36Sopenharmony_ci#include <linux/slab.h>
2362306a36Sopenharmony_ci#include <linux/uaccess.h>
2462306a36Sopenharmony_ci#include <linux/fs.h>
2562306a36Sopenharmony_ci#include <linux/personality.h>
2662306a36Sopenharmony_ci#include <linux/pagemap.h>
2762306a36Sopenharmony_ci#include <linux/syscalls.h>
2862306a36Sopenharmony_ci#include <linux/rcupdate.h>
2962306a36Sopenharmony_ci#include <linux/audit.h>
3062306a36Sopenharmony_ci#include <linux/falloc.h>
3162306a36Sopenharmony_ci#include <linux/fs_struct.h>
3262306a36Sopenharmony_ci#include <linux/ima.h>
3362306a36Sopenharmony_ci#include <linux/dnotify.h>
3462306a36Sopenharmony_ci#include <linux/compat.h>
3562306a36Sopenharmony_ci#include <linux/mnt_idmapping.h>
3662306a36Sopenharmony_ci#include <linux/filelock.h>
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci#include "internal.h"
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ciint do_truncate(struct mnt_idmap *idmap, struct dentry *dentry,
4162306a36Sopenharmony_ci		loff_t length, unsigned int time_attrs, struct file *filp)
4262306a36Sopenharmony_ci{
4362306a36Sopenharmony_ci	int ret;
4462306a36Sopenharmony_ci	struct iattr newattrs;
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci	/* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
4762306a36Sopenharmony_ci	if (length < 0)
4862306a36Sopenharmony_ci		return -EINVAL;
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci	newattrs.ia_size = length;
5162306a36Sopenharmony_ci	newattrs.ia_valid = ATTR_SIZE | time_attrs;
5262306a36Sopenharmony_ci	if (filp) {
5362306a36Sopenharmony_ci		newattrs.ia_file = filp;
5462306a36Sopenharmony_ci		newattrs.ia_valid |= ATTR_FILE;
5562306a36Sopenharmony_ci	}
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci	/* Remove suid, sgid, and file capabilities on truncate too */
5862306a36Sopenharmony_ci	ret = dentry_needs_remove_privs(idmap, dentry);
5962306a36Sopenharmony_ci	if (ret < 0)
6062306a36Sopenharmony_ci		return ret;
6162306a36Sopenharmony_ci	if (ret)
6262306a36Sopenharmony_ci		newattrs.ia_valid |= ret | ATTR_FORCE;
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci	inode_lock(dentry->d_inode);
6562306a36Sopenharmony_ci	/* Note any delegations or leases have already been broken: */
6662306a36Sopenharmony_ci	ret = notify_change(idmap, dentry, &newattrs, NULL);
6762306a36Sopenharmony_ci	inode_unlock(dentry->d_inode);
6862306a36Sopenharmony_ci	return ret;
6962306a36Sopenharmony_ci}
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_cilong vfs_truncate(const struct path *path, loff_t length)
7262306a36Sopenharmony_ci{
7362306a36Sopenharmony_ci	struct mnt_idmap *idmap;
7462306a36Sopenharmony_ci	struct inode *inode;
7562306a36Sopenharmony_ci	long error;
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	inode = path->dentry->d_inode;
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	/* For directories it's -EISDIR, for other non-regulars - -EINVAL */
8062306a36Sopenharmony_ci	if (S_ISDIR(inode->i_mode))
8162306a36Sopenharmony_ci		return -EISDIR;
8262306a36Sopenharmony_ci	if (!S_ISREG(inode->i_mode))
8362306a36Sopenharmony_ci		return -EINVAL;
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci	error = mnt_want_write(path->mnt);
8662306a36Sopenharmony_ci	if (error)
8762306a36Sopenharmony_ci		goto out;
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci	idmap = mnt_idmap(path->mnt);
9062306a36Sopenharmony_ci	error = inode_permission(idmap, inode, MAY_WRITE);
9162306a36Sopenharmony_ci	if (error)
9262306a36Sopenharmony_ci		goto mnt_drop_write_and_out;
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	error = -EPERM;
9562306a36Sopenharmony_ci	if (IS_APPEND(inode))
9662306a36Sopenharmony_ci		goto mnt_drop_write_and_out;
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	error = get_write_access(inode);
9962306a36Sopenharmony_ci	if (error)
10062306a36Sopenharmony_ci		goto mnt_drop_write_and_out;
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci	/*
10362306a36Sopenharmony_ci	 * Make sure that there are no leases.  get_write_access() protects
10462306a36Sopenharmony_ci	 * against the truncate racing with a lease-granting setlease().
10562306a36Sopenharmony_ci	 */
10662306a36Sopenharmony_ci	error = break_lease(inode, O_WRONLY);
10762306a36Sopenharmony_ci	if (error)
10862306a36Sopenharmony_ci		goto put_write_and_out;
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci	error = security_path_truncate(path);
11162306a36Sopenharmony_ci	if (!error)
11262306a36Sopenharmony_ci		error = do_truncate(idmap, path->dentry, length, 0, NULL);
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ciput_write_and_out:
11562306a36Sopenharmony_ci	put_write_access(inode);
11662306a36Sopenharmony_cimnt_drop_write_and_out:
11762306a36Sopenharmony_ci	mnt_drop_write(path->mnt);
11862306a36Sopenharmony_ciout:
11962306a36Sopenharmony_ci	return error;
12062306a36Sopenharmony_ci}
12162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(vfs_truncate);
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_cilong do_sys_truncate(const char __user *pathname, loff_t length)
12462306a36Sopenharmony_ci{
12562306a36Sopenharmony_ci	unsigned int lookup_flags = LOOKUP_FOLLOW;
12662306a36Sopenharmony_ci	struct path path;
12762306a36Sopenharmony_ci	int error;
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	if (length < 0)	/* sorry, but loff_t says... */
13062306a36Sopenharmony_ci		return -EINVAL;
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ciretry:
13362306a36Sopenharmony_ci	error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
13462306a36Sopenharmony_ci	if (!error) {
13562306a36Sopenharmony_ci		error = vfs_truncate(&path, length);
13662306a36Sopenharmony_ci		path_put(&path);
13762306a36Sopenharmony_ci	}
13862306a36Sopenharmony_ci	if (retry_estale(error, lookup_flags)) {
13962306a36Sopenharmony_ci		lookup_flags |= LOOKUP_REVAL;
14062306a36Sopenharmony_ci		goto retry;
14162306a36Sopenharmony_ci	}
14262306a36Sopenharmony_ci	return error;
14362306a36Sopenharmony_ci}
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ciSYSCALL_DEFINE2(truncate, const char __user *, path, long, length)
14662306a36Sopenharmony_ci{
14762306a36Sopenharmony_ci	return do_sys_truncate(path, length);
14862306a36Sopenharmony_ci}
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci#ifdef CONFIG_COMPAT
15162306a36Sopenharmony_ciCOMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length)
15262306a36Sopenharmony_ci{
15362306a36Sopenharmony_ci	return do_sys_truncate(path, length);
15462306a36Sopenharmony_ci}
15562306a36Sopenharmony_ci#endif
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_cilong do_sys_ftruncate(unsigned int fd, loff_t length, int small)
15862306a36Sopenharmony_ci{
15962306a36Sopenharmony_ci	struct inode *inode;
16062306a36Sopenharmony_ci	struct dentry *dentry;
16162306a36Sopenharmony_ci	struct fd f;
16262306a36Sopenharmony_ci	int error;
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci	error = -EINVAL;
16562306a36Sopenharmony_ci	if (length < 0)
16662306a36Sopenharmony_ci		goto out;
16762306a36Sopenharmony_ci	error = -EBADF;
16862306a36Sopenharmony_ci	f = fdget(fd);
16962306a36Sopenharmony_ci	if (!f.file)
17062306a36Sopenharmony_ci		goto out;
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci	/* explicitly opened as large or we are on 64-bit box */
17362306a36Sopenharmony_ci	if (f.file->f_flags & O_LARGEFILE)
17462306a36Sopenharmony_ci		small = 0;
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci	dentry = f.file->f_path.dentry;
17762306a36Sopenharmony_ci	inode = dentry->d_inode;
17862306a36Sopenharmony_ci	error = -EINVAL;
17962306a36Sopenharmony_ci	if (!S_ISREG(inode->i_mode) || !(f.file->f_mode & FMODE_WRITE))
18062306a36Sopenharmony_ci		goto out_putf;
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci	error = -EINVAL;
18362306a36Sopenharmony_ci	/* Cannot ftruncate over 2^31 bytes without large file support */
18462306a36Sopenharmony_ci	if (small && length > MAX_NON_LFS)
18562306a36Sopenharmony_ci		goto out_putf;
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci	error = -EPERM;
18862306a36Sopenharmony_ci	/* Check IS_APPEND on real upper inode */
18962306a36Sopenharmony_ci	if (IS_APPEND(file_inode(f.file)))
19062306a36Sopenharmony_ci		goto out_putf;
19162306a36Sopenharmony_ci	sb_start_write(inode->i_sb);
19262306a36Sopenharmony_ci	error = security_file_truncate(f.file);
19362306a36Sopenharmony_ci	if (!error)
19462306a36Sopenharmony_ci		error = do_truncate(file_mnt_idmap(f.file), dentry, length,
19562306a36Sopenharmony_ci				    ATTR_MTIME | ATTR_CTIME, f.file);
19662306a36Sopenharmony_ci	sb_end_write(inode->i_sb);
19762306a36Sopenharmony_ciout_putf:
19862306a36Sopenharmony_ci	fdput(f);
19962306a36Sopenharmony_ciout:
20062306a36Sopenharmony_ci	return error;
20162306a36Sopenharmony_ci}
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ciSYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length)
20462306a36Sopenharmony_ci{
20562306a36Sopenharmony_ci	return do_sys_ftruncate(fd, length, 1);
20662306a36Sopenharmony_ci}
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci#ifdef CONFIG_COMPAT
20962306a36Sopenharmony_ciCOMPAT_SYSCALL_DEFINE2(ftruncate, unsigned int, fd, compat_ulong_t, length)
21062306a36Sopenharmony_ci{
21162306a36Sopenharmony_ci	return do_sys_ftruncate(fd, length, 1);
21262306a36Sopenharmony_ci}
21362306a36Sopenharmony_ci#endif
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci/* LFS versions of truncate are only needed on 32 bit machines */
21662306a36Sopenharmony_ci#if BITS_PER_LONG == 32
21762306a36Sopenharmony_ciSYSCALL_DEFINE2(truncate64, const char __user *, path, loff_t, length)
21862306a36Sopenharmony_ci{
21962306a36Sopenharmony_ci	return do_sys_truncate(path, length);
22062306a36Sopenharmony_ci}
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ciSYSCALL_DEFINE2(ftruncate64, unsigned int, fd, loff_t, length)
22362306a36Sopenharmony_ci{
22462306a36Sopenharmony_ci	return do_sys_ftruncate(fd, length, 0);
22562306a36Sopenharmony_ci}
22662306a36Sopenharmony_ci#endif /* BITS_PER_LONG == 32 */
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci#if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_TRUNCATE64)
22962306a36Sopenharmony_ciCOMPAT_SYSCALL_DEFINE3(truncate64, const char __user *, pathname,
23062306a36Sopenharmony_ci		       compat_arg_u64_dual(length))
23162306a36Sopenharmony_ci{
23262306a36Sopenharmony_ci	return ksys_truncate(pathname, compat_arg_u64_glue(length));
23362306a36Sopenharmony_ci}
23462306a36Sopenharmony_ci#endif
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ci#if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_FTRUNCATE64)
23762306a36Sopenharmony_ciCOMPAT_SYSCALL_DEFINE3(ftruncate64, unsigned int, fd,
23862306a36Sopenharmony_ci		       compat_arg_u64_dual(length))
23962306a36Sopenharmony_ci{
24062306a36Sopenharmony_ci	return ksys_ftruncate(fd, compat_arg_u64_glue(length));
24162306a36Sopenharmony_ci}
24262306a36Sopenharmony_ci#endif
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ciint vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
24562306a36Sopenharmony_ci{
24662306a36Sopenharmony_ci	struct inode *inode = file_inode(file);
24762306a36Sopenharmony_ci	long ret;
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci	if (offset < 0 || len <= 0)
25062306a36Sopenharmony_ci		return -EINVAL;
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ci	/* Return error if mode is not supported */
25362306a36Sopenharmony_ci	if (mode & ~FALLOC_FL_SUPPORTED_MASK)
25462306a36Sopenharmony_ci		return -EOPNOTSUPP;
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci	/* Punch hole and zero range are mutually exclusive */
25762306a36Sopenharmony_ci	if ((mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) ==
25862306a36Sopenharmony_ci	    (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE))
25962306a36Sopenharmony_ci		return -EOPNOTSUPP;
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci	/* Punch hole must have keep size set */
26262306a36Sopenharmony_ci	if ((mode & FALLOC_FL_PUNCH_HOLE) &&
26362306a36Sopenharmony_ci	    !(mode & FALLOC_FL_KEEP_SIZE))
26462306a36Sopenharmony_ci		return -EOPNOTSUPP;
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci	/* Collapse range should only be used exclusively. */
26762306a36Sopenharmony_ci	if ((mode & FALLOC_FL_COLLAPSE_RANGE) &&
26862306a36Sopenharmony_ci	    (mode & ~FALLOC_FL_COLLAPSE_RANGE))
26962306a36Sopenharmony_ci		return -EINVAL;
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_ci	/* Insert range should only be used exclusively. */
27262306a36Sopenharmony_ci	if ((mode & FALLOC_FL_INSERT_RANGE) &&
27362306a36Sopenharmony_ci	    (mode & ~FALLOC_FL_INSERT_RANGE))
27462306a36Sopenharmony_ci		return -EINVAL;
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci	/* Unshare range should only be used with allocate mode. */
27762306a36Sopenharmony_ci	if ((mode & FALLOC_FL_UNSHARE_RANGE) &&
27862306a36Sopenharmony_ci	    (mode & ~(FALLOC_FL_UNSHARE_RANGE | FALLOC_FL_KEEP_SIZE)))
27962306a36Sopenharmony_ci		return -EINVAL;
28062306a36Sopenharmony_ci
28162306a36Sopenharmony_ci	if (!(file->f_mode & FMODE_WRITE))
28262306a36Sopenharmony_ci		return -EBADF;
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci	/*
28562306a36Sopenharmony_ci	 * We can only allow pure fallocate on append only files
28662306a36Sopenharmony_ci	 */
28762306a36Sopenharmony_ci	if ((mode & ~FALLOC_FL_KEEP_SIZE) && IS_APPEND(inode))
28862306a36Sopenharmony_ci		return -EPERM;
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci	if (IS_IMMUTABLE(inode))
29162306a36Sopenharmony_ci		return -EPERM;
29262306a36Sopenharmony_ci
29362306a36Sopenharmony_ci	/*
29462306a36Sopenharmony_ci	 * We cannot allow any fallocate operation on an active swapfile
29562306a36Sopenharmony_ci	 */
29662306a36Sopenharmony_ci	if (IS_SWAPFILE(inode))
29762306a36Sopenharmony_ci		return -ETXTBSY;
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_ci	/*
30062306a36Sopenharmony_ci	 * Revalidate the write permissions, in case security policy has
30162306a36Sopenharmony_ci	 * changed since the files were opened.
30262306a36Sopenharmony_ci	 */
30362306a36Sopenharmony_ci	ret = security_file_permission(file, MAY_WRITE);
30462306a36Sopenharmony_ci	if (ret)
30562306a36Sopenharmony_ci		return ret;
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_ci	if (S_ISFIFO(inode->i_mode))
30862306a36Sopenharmony_ci		return -ESPIPE;
30962306a36Sopenharmony_ci
31062306a36Sopenharmony_ci	if (S_ISDIR(inode->i_mode))
31162306a36Sopenharmony_ci		return -EISDIR;
31262306a36Sopenharmony_ci
31362306a36Sopenharmony_ci	if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
31462306a36Sopenharmony_ci		return -ENODEV;
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_ci	/* Check for wrap through zero too */
31762306a36Sopenharmony_ci	if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
31862306a36Sopenharmony_ci		return -EFBIG;
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_ci	if (!file->f_op->fallocate)
32162306a36Sopenharmony_ci		return -EOPNOTSUPP;
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_ci	file_start_write(file);
32462306a36Sopenharmony_ci	ret = file->f_op->fallocate(file, mode, offset, len);
32562306a36Sopenharmony_ci
32662306a36Sopenharmony_ci	/*
32762306a36Sopenharmony_ci	 * Create inotify and fanotify events.
32862306a36Sopenharmony_ci	 *
32962306a36Sopenharmony_ci	 * To keep the logic simple always create events if fallocate succeeds.
33062306a36Sopenharmony_ci	 * This implies that events are even created if the file size remains
33162306a36Sopenharmony_ci	 * unchanged, e.g. when using flag FALLOC_FL_KEEP_SIZE.
33262306a36Sopenharmony_ci	 */
33362306a36Sopenharmony_ci	if (ret == 0)
33462306a36Sopenharmony_ci		fsnotify_modify(file);
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ci	file_end_write(file);
33762306a36Sopenharmony_ci	return ret;
33862306a36Sopenharmony_ci}
33962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(vfs_fallocate);
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ciint ksys_fallocate(int fd, int mode, loff_t offset, loff_t len)
34262306a36Sopenharmony_ci{
34362306a36Sopenharmony_ci	struct fd f = fdget(fd);
34462306a36Sopenharmony_ci	int error = -EBADF;
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci	if (f.file) {
34762306a36Sopenharmony_ci		error = vfs_fallocate(f.file, mode, offset, len);
34862306a36Sopenharmony_ci		fdput(f);
34962306a36Sopenharmony_ci	}
35062306a36Sopenharmony_ci	return error;
35162306a36Sopenharmony_ci}
35262306a36Sopenharmony_ci
35362306a36Sopenharmony_ciSYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len)
35462306a36Sopenharmony_ci{
35562306a36Sopenharmony_ci	return ksys_fallocate(fd, mode, offset, len);
35662306a36Sopenharmony_ci}
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_ci#if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_FALLOCATE)
35962306a36Sopenharmony_ciCOMPAT_SYSCALL_DEFINE6(fallocate, int, fd, int, mode, compat_arg_u64_dual(offset),
36062306a36Sopenharmony_ci		       compat_arg_u64_dual(len))
36162306a36Sopenharmony_ci{
36262306a36Sopenharmony_ci	return ksys_fallocate(fd, mode, compat_arg_u64_glue(offset),
36362306a36Sopenharmony_ci			      compat_arg_u64_glue(len));
36462306a36Sopenharmony_ci}
36562306a36Sopenharmony_ci#endif
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_ci/*
36862306a36Sopenharmony_ci * access() needs to use the real uid/gid, not the effective uid/gid.
36962306a36Sopenharmony_ci * We do this by temporarily clearing all FS-related capabilities and
37062306a36Sopenharmony_ci * switching the fsuid/fsgid around to the real ones.
37162306a36Sopenharmony_ci *
37262306a36Sopenharmony_ci * Creating new credentials is expensive, so we try to skip doing it,
37362306a36Sopenharmony_ci * which we can if the result would match what we already got.
37462306a36Sopenharmony_ci */
37562306a36Sopenharmony_cistatic bool access_need_override_creds(int flags)
37662306a36Sopenharmony_ci{
37762306a36Sopenharmony_ci	const struct cred *cred;
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_ci	if (flags & AT_EACCESS)
38062306a36Sopenharmony_ci		return false;
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_ci	cred = current_cred();
38362306a36Sopenharmony_ci	if (!uid_eq(cred->fsuid, cred->uid) ||
38462306a36Sopenharmony_ci	    !gid_eq(cred->fsgid, cred->gid))
38562306a36Sopenharmony_ci		return true;
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_ci	if (!issecure(SECURE_NO_SETUID_FIXUP)) {
38862306a36Sopenharmony_ci		kuid_t root_uid = make_kuid(cred->user_ns, 0);
38962306a36Sopenharmony_ci		if (!uid_eq(cred->uid, root_uid)) {
39062306a36Sopenharmony_ci			if (!cap_isclear(cred->cap_effective))
39162306a36Sopenharmony_ci				return true;
39262306a36Sopenharmony_ci		} else {
39362306a36Sopenharmony_ci			if (!cap_isidentical(cred->cap_effective,
39462306a36Sopenharmony_ci			    cred->cap_permitted))
39562306a36Sopenharmony_ci				return true;
39662306a36Sopenharmony_ci		}
39762306a36Sopenharmony_ci	}
39862306a36Sopenharmony_ci
39962306a36Sopenharmony_ci	return false;
40062306a36Sopenharmony_ci}
40162306a36Sopenharmony_ci
40262306a36Sopenharmony_cistatic const struct cred *access_override_creds(void)
40362306a36Sopenharmony_ci{
40462306a36Sopenharmony_ci	const struct cred *old_cred;
40562306a36Sopenharmony_ci	struct cred *override_cred;
40662306a36Sopenharmony_ci
40762306a36Sopenharmony_ci	override_cred = prepare_creds();
40862306a36Sopenharmony_ci	if (!override_cred)
40962306a36Sopenharmony_ci		return NULL;
41062306a36Sopenharmony_ci
41162306a36Sopenharmony_ci	/*
41262306a36Sopenharmony_ci	 * XXX access_need_override_creds performs checks in hopes of skipping
41362306a36Sopenharmony_ci	 * this work. Make sure it stays in sync if making any changes in this
41462306a36Sopenharmony_ci	 * routine.
41562306a36Sopenharmony_ci	 */
41662306a36Sopenharmony_ci
41762306a36Sopenharmony_ci	override_cred->fsuid = override_cred->uid;
41862306a36Sopenharmony_ci	override_cred->fsgid = override_cred->gid;
41962306a36Sopenharmony_ci
42062306a36Sopenharmony_ci	if (!issecure(SECURE_NO_SETUID_FIXUP)) {
42162306a36Sopenharmony_ci		/* Clear the capabilities if we switch to a non-root user */
42262306a36Sopenharmony_ci		kuid_t root_uid = make_kuid(override_cred->user_ns, 0);
42362306a36Sopenharmony_ci		if (!uid_eq(override_cred->uid, root_uid))
42462306a36Sopenharmony_ci			cap_clear(override_cred->cap_effective);
42562306a36Sopenharmony_ci		else
42662306a36Sopenharmony_ci			override_cred->cap_effective =
42762306a36Sopenharmony_ci				override_cred->cap_permitted;
42862306a36Sopenharmony_ci	}
42962306a36Sopenharmony_ci
43062306a36Sopenharmony_ci	/*
43162306a36Sopenharmony_ci	 * The new set of credentials can *only* be used in
43262306a36Sopenharmony_ci	 * task-synchronous circumstances, and does not need
43362306a36Sopenharmony_ci	 * RCU freeing, unless somebody then takes a separate
43462306a36Sopenharmony_ci	 * reference to it.
43562306a36Sopenharmony_ci	 *
43662306a36Sopenharmony_ci	 * NOTE! This is _only_ true because this credential
43762306a36Sopenharmony_ci	 * is used purely for override_creds() that installs
43862306a36Sopenharmony_ci	 * it as the subjective cred. Other threads will be
43962306a36Sopenharmony_ci	 * accessing ->real_cred, not the subjective cred.
44062306a36Sopenharmony_ci	 *
44162306a36Sopenharmony_ci	 * If somebody _does_ make a copy of this (using the
44262306a36Sopenharmony_ci	 * 'get_current_cred()' function), that will clear the
44362306a36Sopenharmony_ci	 * non_rcu field, because now that other user may be
44462306a36Sopenharmony_ci	 * expecting RCU freeing. But normal thread-synchronous
44562306a36Sopenharmony_ci	 * cred accesses will keep things non-RCY.
44662306a36Sopenharmony_ci	 */
44762306a36Sopenharmony_ci	override_cred->non_rcu = 1;
44862306a36Sopenharmony_ci
44962306a36Sopenharmony_ci	old_cred = override_creds(override_cred);
45062306a36Sopenharmony_ci
45162306a36Sopenharmony_ci	/* override_cred() gets its own ref */
45262306a36Sopenharmony_ci	put_cred(override_cred);
45362306a36Sopenharmony_ci
45462306a36Sopenharmony_ci	return old_cred;
45562306a36Sopenharmony_ci}
45662306a36Sopenharmony_ci
45762306a36Sopenharmony_cistatic long do_faccessat(int dfd, const char __user *filename, int mode, int flags)
45862306a36Sopenharmony_ci{
45962306a36Sopenharmony_ci	struct path path;
46062306a36Sopenharmony_ci	struct inode *inode;
46162306a36Sopenharmony_ci	int res;
46262306a36Sopenharmony_ci	unsigned int lookup_flags = LOOKUP_FOLLOW;
46362306a36Sopenharmony_ci	const struct cred *old_cred = NULL;
46462306a36Sopenharmony_ci
46562306a36Sopenharmony_ci	if (mode & ~S_IRWXO)	/* where's F_OK, X_OK, W_OK, R_OK? */
46662306a36Sopenharmony_ci		return -EINVAL;
46762306a36Sopenharmony_ci
46862306a36Sopenharmony_ci	if (flags & ~(AT_EACCESS | AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH))
46962306a36Sopenharmony_ci		return -EINVAL;
47062306a36Sopenharmony_ci
47162306a36Sopenharmony_ci	if (flags & AT_SYMLINK_NOFOLLOW)
47262306a36Sopenharmony_ci		lookup_flags &= ~LOOKUP_FOLLOW;
47362306a36Sopenharmony_ci	if (flags & AT_EMPTY_PATH)
47462306a36Sopenharmony_ci		lookup_flags |= LOOKUP_EMPTY;
47562306a36Sopenharmony_ci
47662306a36Sopenharmony_ci	if (access_need_override_creds(flags)) {
47762306a36Sopenharmony_ci		old_cred = access_override_creds();
47862306a36Sopenharmony_ci		if (!old_cred)
47962306a36Sopenharmony_ci			return -ENOMEM;
48062306a36Sopenharmony_ci	}
48162306a36Sopenharmony_ci
48262306a36Sopenharmony_ciretry:
48362306a36Sopenharmony_ci	res = user_path_at(dfd, filename, lookup_flags, &path);
48462306a36Sopenharmony_ci	if (res)
48562306a36Sopenharmony_ci		goto out;
48662306a36Sopenharmony_ci
48762306a36Sopenharmony_ci	inode = d_backing_inode(path.dentry);
48862306a36Sopenharmony_ci
48962306a36Sopenharmony_ci	if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
49062306a36Sopenharmony_ci		/*
49162306a36Sopenharmony_ci		 * MAY_EXEC on regular files is denied if the fs is mounted
49262306a36Sopenharmony_ci		 * with the "noexec" flag.
49362306a36Sopenharmony_ci		 */
49462306a36Sopenharmony_ci		res = -EACCES;
49562306a36Sopenharmony_ci		if (path_noexec(&path))
49662306a36Sopenharmony_ci			goto out_path_release;
49762306a36Sopenharmony_ci	}
49862306a36Sopenharmony_ci
49962306a36Sopenharmony_ci	res = inode_permission(mnt_idmap(path.mnt), inode, mode | MAY_ACCESS);
50062306a36Sopenharmony_ci	/* SuS v2 requires we report a read only fs too */
50162306a36Sopenharmony_ci	if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
50262306a36Sopenharmony_ci		goto out_path_release;
50362306a36Sopenharmony_ci	/*
50462306a36Sopenharmony_ci	 * This is a rare case where using __mnt_is_readonly()
50562306a36Sopenharmony_ci	 * is OK without a mnt_want/drop_write() pair.  Since
50662306a36Sopenharmony_ci	 * no actual write to the fs is performed here, we do
50762306a36Sopenharmony_ci	 * not need to telegraph to that to anyone.
50862306a36Sopenharmony_ci	 *
50962306a36Sopenharmony_ci	 * By doing this, we accept that this access is
51062306a36Sopenharmony_ci	 * inherently racy and know that the fs may change
51162306a36Sopenharmony_ci	 * state before we even see this result.
51262306a36Sopenharmony_ci	 */
51362306a36Sopenharmony_ci	if (__mnt_is_readonly(path.mnt))
51462306a36Sopenharmony_ci		res = -EROFS;
51562306a36Sopenharmony_ci
51662306a36Sopenharmony_ciout_path_release:
51762306a36Sopenharmony_ci	path_put(&path);
51862306a36Sopenharmony_ci	if (retry_estale(res, lookup_flags)) {
51962306a36Sopenharmony_ci		lookup_flags |= LOOKUP_REVAL;
52062306a36Sopenharmony_ci		goto retry;
52162306a36Sopenharmony_ci	}
52262306a36Sopenharmony_ciout:
52362306a36Sopenharmony_ci	if (old_cred)
52462306a36Sopenharmony_ci		revert_creds(old_cred);
52562306a36Sopenharmony_ci
52662306a36Sopenharmony_ci	return res;
52762306a36Sopenharmony_ci}
52862306a36Sopenharmony_ci
52962306a36Sopenharmony_ciSYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
53062306a36Sopenharmony_ci{
53162306a36Sopenharmony_ci	return do_faccessat(dfd, filename, mode, 0);
53262306a36Sopenharmony_ci}
53362306a36Sopenharmony_ci
53462306a36Sopenharmony_ciSYSCALL_DEFINE4(faccessat2, int, dfd, const char __user *, filename, int, mode,
53562306a36Sopenharmony_ci		int, flags)
53662306a36Sopenharmony_ci{
53762306a36Sopenharmony_ci	return do_faccessat(dfd, filename, mode, flags);
53862306a36Sopenharmony_ci}
53962306a36Sopenharmony_ci
54062306a36Sopenharmony_ciSYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
54162306a36Sopenharmony_ci{
54262306a36Sopenharmony_ci	return do_faccessat(AT_FDCWD, filename, mode, 0);
54362306a36Sopenharmony_ci}
54462306a36Sopenharmony_ci
54562306a36Sopenharmony_ciSYSCALL_DEFINE1(chdir, const char __user *, filename)
54662306a36Sopenharmony_ci{
54762306a36Sopenharmony_ci	struct path path;
54862306a36Sopenharmony_ci	int error;
54962306a36Sopenharmony_ci	unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
55062306a36Sopenharmony_ciretry:
55162306a36Sopenharmony_ci	error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
55262306a36Sopenharmony_ci	if (error)
55362306a36Sopenharmony_ci		goto out;
55462306a36Sopenharmony_ci
55562306a36Sopenharmony_ci	error = path_permission(&path, MAY_EXEC | MAY_CHDIR);
55662306a36Sopenharmony_ci	if (error)
55762306a36Sopenharmony_ci		goto dput_and_out;
55862306a36Sopenharmony_ci
55962306a36Sopenharmony_ci	set_fs_pwd(current->fs, &path);
56062306a36Sopenharmony_ci
56162306a36Sopenharmony_cidput_and_out:
56262306a36Sopenharmony_ci	path_put(&path);
56362306a36Sopenharmony_ci	if (retry_estale(error, lookup_flags)) {
56462306a36Sopenharmony_ci		lookup_flags |= LOOKUP_REVAL;
56562306a36Sopenharmony_ci		goto retry;
56662306a36Sopenharmony_ci	}
56762306a36Sopenharmony_ciout:
56862306a36Sopenharmony_ci	return error;
56962306a36Sopenharmony_ci}
57062306a36Sopenharmony_ci
57162306a36Sopenharmony_ciSYSCALL_DEFINE1(fchdir, unsigned int, fd)
57262306a36Sopenharmony_ci{
57362306a36Sopenharmony_ci	struct fd f = fdget_raw(fd);
57462306a36Sopenharmony_ci	int error;
57562306a36Sopenharmony_ci
57662306a36Sopenharmony_ci	error = -EBADF;
57762306a36Sopenharmony_ci	if (!f.file)
57862306a36Sopenharmony_ci		goto out;
57962306a36Sopenharmony_ci
58062306a36Sopenharmony_ci	error = -ENOTDIR;
58162306a36Sopenharmony_ci	if (!d_can_lookup(f.file->f_path.dentry))
58262306a36Sopenharmony_ci		goto out_putf;
58362306a36Sopenharmony_ci
58462306a36Sopenharmony_ci	error = file_permission(f.file, MAY_EXEC | MAY_CHDIR);
58562306a36Sopenharmony_ci	if (!error)
58662306a36Sopenharmony_ci		set_fs_pwd(current->fs, &f.file->f_path);
58762306a36Sopenharmony_ciout_putf:
58862306a36Sopenharmony_ci	fdput(f);
58962306a36Sopenharmony_ciout:
59062306a36Sopenharmony_ci	return error;
59162306a36Sopenharmony_ci}
59262306a36Sopenharmony_ci
59362306a36Sopenharmony_ciSYSCALL_DEFINE1(chroot, const char __user *, filename)
59462306a36Sopenharmony_ci{
59562306a36Sopenharmony_ci	struct path path;
59662306a36Sopenharmony_ci	int error;
59762306a36Sopenharmony_ci	unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
59862306a36Sopenharmony_ciretry:
59962306a36Sopenharmony_ci	error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
60062306a36Sopenharmony_ci	if (error)
60162306a36Sopenharmony_ci		goto out;
60262306a36Sopenharmony_ci
60362306a36Sopenharmony_ci	error = path_permission(&path, MAY_EXEC | MAY_CHDIR);
60462306a36Sopenharmony_ci	if (error)
60562306a36Sopenharmony_ci		goto dput_and_out;
60662306a36Sopenharmony_ci
60762306a36Sopenharmony_ci	error = -EPERM;
60862306a36Sopenharmony_ci	if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT))
60962306a36Sopenharmony_ci		goto dput_and_out;
61062306a36Sopenharmony_ci	error = security_path_chroot(&path);
61162306a36Sopenharmony_ci	if (error)
61262306a36Sopenharmony_ci		goto dput_and_out;
61362306a36Sopenharmony_ci
61462306a36Sopenharmony_ci	set_fs_root(current->fs, &path);
61562306a36Sopenharmony_ci	error = 0;
61662306a36Sopenharmony_cidput_and_out:
61762306a36Sopenharmony_ci	path_put(&path);
61862306a36Sopenharmony_ci	if (retry_estale(error, lookup_flags)) {
61962306a36Sopenharmony_ci		lookup_flags |= LOOKUP_REVAL;
62062306a36Sopenharmony_ci		goto retry;
62162306a36Sopenharmony_ci	}
62262306a36Sopenharmony_ciout:
62362306a36Sopenharmony_ci	return error;
62462306a36Sopenharmony_ci}
62562306a36Sopenharmony_ci
62662306a36Sopenharmony_ciint chmod_common(const struct path *path, umode_t mode)
62762306a36Sopenharmony_ci{
62862306a36Sopenharmony_ci	struct inode *inode = path->dentry->d_inode;
62962306a36Sopenharmony_ci	struct inode *delegated_inode = NULL;
63062306a36Sopenharmony_ci	struct iattr newattrs;
63162306a36Sopenharmony_ci	int error;
63262306a36Sopenharmony_ci
63362306a36Sopenharmony_ci	error = mnt_want_write(path->mnt);
63462306a36Sopenharmony_ci	if (error)
63562306a36Sopenharmony_ci		return error;
63662306a36Sopenharmony_ciretry_deleg:
63762306a36Sopenharmony_ci	inode_lock(inode);
63862306a36Sopenharmony_ci	error = security_path_chmod(path, mode);
63962306a36Sopenharmony_ci	if (error)
64062306a36Sopenharmony_ci		goto out_unlock;
64162306a36Sopenharmony_ci	newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
64262306a36Sopenharmony_ci	newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
64362306a36Sopenharmony_ci	error = notify_change(mnt_idmap(path->mnt), path->dentry,
64462306a36Sopenharmony_ci			      &newattrs, &delegated_inode);
64562306a36Sopenharmony_ciout_unlock:
64662306a36Sopenharmony_ci	inode_unlock(inode);
64762306a36Sopenharmony_ci	if (delegated_inode) {
64862306a36Sopenharmony_ci		error = break_deleg_wait(&delegated_inode);
64962306a36Sopenharmony_ci		if (!error)
65062306a36Sopenharmony_ci			goto retry_deleg;
65162306a36Sopenharmony_ci	}
65262306a36Sopenharmony_ci	mnt_drop_write(path->mnt);
65362306a36Sopenharmony_ci	return error;
65462306a36Sopenharmony_ci}
65562306a36Sopenharmony_ci
65662306a36Sopenharmony_ciint vfs_fchmod(struct file *file, umode_t mode)
65762306a36Sopenharmony_ci{
65862306a36Sopenharmony_ci	audit_file(file);
65962306a36Sopenharmony_ci	return chmod_common(&file->f_path, mode);
66062306a36Sopenharmony_ci}
66162306a36Sopenharmony_ci
66262306a36Sopenharmony_ciSYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode)
66362306a36Sopenharmony_ci{
66462306a36Sopenharmony_ci	struct fd f = fdget(fd);
66562306a36Sopenharmony_ci	int err = -EBADF;
66662306a36Sopenharmony_ci
66762306a36Sopenharmony_ci	if (f.file) {
66862306a36Sopenharmony_ci		err = vfs_fchmod(f.file, mode);
66962306a36Sopenharmony_ci		fdput(f);
67062306a36Sopenharmony_ci	}
67162306a36Sopenharmony_ci	return err;
67262306a36Sopenharmony_ci}
67362306a36Sopenharmony_ci
67462306a36Sopenharmony_cistatic int do_fchmodat(int dfd, const char __user *filename, umode_t mode,
67562306a36Sopenharmony_ci		       unsigned int flags)
67662306a36Sopenharmony_ci{
67762306a36Sopenharmony_ci	struct path path;
67862306a36Sopenharmony_ci	int error;
67962306a36Sopenharmony_ci	unsigned int lookup_flags;
68062306a36Sopenharmony_ci
68162306a36Sopenharmony_ci	if (unlikely(flags & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)))
68262306a36Sopenharmony_ci		return -EINVAL;
68362306a36Sopenharmony_ci
68462306a36Sopenharmony_ci	lookup_flags = (flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
68562306a36Sopenharmony_ci	if (flags & AT_EMPTY_PATH)
68662306a36Sopenharmony_ci		lookup_flags |= LOOKUP_EMPTY;
68762306a36Sopenharmony_ci
68862306a36Sopenharmony_ciretry:
68962306a36Sopenharmony_ci	error = user_path_at(dfd, filename, lookup_flags, &path);
69062306a36Sopenharmony_ci	if (!error) {
69162306a36Sopenharmony_ci		error = chmod_common(&path, mode);
69262306a36Sopenharmony_ci		path_put(&path);
69362306a36Sopenharmony_ci		if (retry_estale(error, lookup_flags)) {
69462306a36Sopenharmony_ci			lookup_flags |= LOOKUP_REVAL;
69562306a36Sopenharmony_ci			goto retry;
69662306a36Sopenharmony_ci		}
69762306a36Sopenharmony_ci	}
69862306a36Sopenharmony_ci	return error;
69962306a36Sopenharmony_ci}
70062306a36Sopenharmony_ci
70162306a36Sopenharmony_ciSYSCALL_DEFINE4(fchmodat2, int, dfd, const char __user *, filename,
70262306a36Sopenharmony_ci		umode_t, mode, unsigned int, flags)
70362306a36Sopenharmony_ci{
70462306a36Sopenharmony_ci	return do_fchmodat(dfd, filename, mode, flags);
70562306a36Sopenharmony_ci}
70662306a36Sopenharmony_ci
70762306a36Sopenharmony_ciSYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename,
70862306a36Sopenharmony_ci		umode_t, mode)
70962306a36Sopenharmony_ci{
71062306a36Sopenharmony_ci	return do_fchmodat(dfd, filename, mode, 0);
71162306a36Sopenharmony_ci}
71262306a36Sopenharmony_ci
71362306a36Sopenharmony_ciSYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode)
71462306a36Sopenharmony_ci{
71562306a36Sopenharmony_ci	return do_fchmodat(AT_FDCWD, filename, mode, 0);
71662306a36Sopenharmony_ci}
71762306a36Sopenharmony_ci
71862306a36Sopenharmony_ci/*
71962306a36Sopenharmony_ci * Check whether @kuid is valid and if so generate and set vfsuid_t in
72062306a36Sopenharmony_ci * ia_vfsuid.
72162306a36Sopenharmony_ci *
72262306a36Sopenharmony_ci * Return: true if @kuid is valid, false if not.
72362306a36Sopenharmony_ci */
72462306a36Sopenharmony_cistatic inline bool setattr_vfsuid(struct iattr *attr, kuid_t kuid)
72562306a36Sopenharmony_ci{
72662306a36Sopenharmony_ci	if (!uid_valid(kuid))
72762306a36Sopenharmony_ci		return false;
72862306a36Sopenharmony_ci	attr->ia_valid |= ATTR_UID;
72962306a36Sopenharmony_ci	attr->ia_vfsuid = VFSUIDT_INIT(kuid);
73062306a36Sopenharmony_ci	return true;
73162306a36Sopenharmony_ci}
73262306a36Sopenharmony_ci
73362306a36Sopenharmony_ci/*
73462306a36Sopenharmony_ci * Check whether @kgid is valid and if so generate and set vfsgid_t in
73562306a36Sopenharmony_ci * ia_vfsgid.
73662306a36Sopenharmony_ci *
73762306a36Sopenharmony_ci * Return: true if @kgid is valid, false if not.
73862306a36Sopenharmony_ci */
73962306a36Sopenharmony_cistatic inline bool setattr_vfsgid(struct iattr *attr, kgid_t kgid)
74062306a36Sopenharmony_ci{
74162306a36Sopenharmony_ci	if (!gid_valid(kgid))
74262306a36Sopenharmony_ci		return false;
74362306a36Sopenharmony_ci	attr->ia_valid |= ATTR_GID;
74462306a36Sopenharmony_ci	attr->ia_vfsgid = VFSGIDT_INIT(kgid);
74562306a36Sopenharmony_ci	return true;
74662306a36Sopenharmony_ci}
74762306a36Sopenharmony_ci
74862306a36Sopenharmony_ciint chown_common(const struct path *path, uid_t user, gid_t group)
74962306a36Sopenharmony_ci{
75062306a36Sopenharmony_ci	struct mnt_idmap *idmap;
75162306a36Sopenharmony_ci	struct user_namespace *fs_userns;
75262306a36Sopenharmony_ci	struct inode *inode = path->dentry->d_inode;
75362306a36Sopenharmony_ci	struct inode *delegated_inode = NULL;
75462306a36Sopenharmony_ci	int error;
75562306a36Sopenharmony_ci	struct iattr newattrs;
75662306a36Sopenharmony_ci	kuid_t uid;
75762306a36Sopenharmony_ci	kgid_t gid;
75862306a36Sopenharmony_ci
75962306a36Sopenharmony_ci	uid = make_kuid(current_user_ns(), user);
76062306a36Sopenharmony_ci	gid = make_kgid(current_user_ns(), group);
76162306a36Sopenharmony_ci
76262306a36Sopenharmony_ci	idmap = mnt_idmap(path->mnt);
76362306a36Sopenharmony_ci	fs_userns = i_user_ns(inode);
76462306a36Sopenharmony_ci
76562306a36Sopenharmony_ciretry_deleg:
76662306a36Sopenharmony_ci	newattrs.ia_vfsuid = INVALID_VFSUID;
76762306a36Sopenharmony_ci	newattrs.ia_vfsgid = INVALID_VFSGID;
76862306a36Sopenharmony_ci	newattrs.ia_valid =  ATTR_CTIME;
76962306a36Sopenharmony_ci	if ((user != (uid_t)-1) && !setattr_vfsuid(&newattrs, uid))
77062306a36Sopenharmony_ci		return -EINVAL;
77162306a36Sopenharmony_ci	if ((group != (gid_t)-1) && !setattr_vfsgid(&newattrs, gid))
77262306a36Sopenharmony_ci		return -EINVAL;
77362306a36Sopenharmony_ci	inode_lock(inode);
77462306a36Sopenharmony_ci	if (!S_ISDIR(inode->i_mode))
77562306a36Sopenharmony_ci		newattrs.ia_valid |= ATTR_KILL_SUID | ATTR_KILL_PRIV |
77662306a36Sopenharmony_ci				     setattr_should_drop_sgid(idmap, inode);
77762306a36Sopenharmony_ci	/* Continue to send actual fs values, not the mount values. */
77862306a36Sopenharmony_ci	error = security_path_chown(
77962306a36Sopenharmony_ci		path,
78062306a36Sopenharmony_ci		from_vfsuid(idmap, fs_userns, newattrs.ia_vfsuid),
78162306a36Sopenharmony_ci		from_vfsgid(idmap, fs_userns, newattrs.ia_vfsgid));
78262306a36Sopenharmony_ci	if (!error)
78362306a36Sopenharmony_ci		error = notify_change(idmap, path->dentry, &newattrs,
78462306a36Sopenharmony_ci				      &delegated_inode);
78562306a36Sopenharmony_ci	inode_unlock(inode);
78662306a36Sopenharmony_ci	if (delegated_inode) {
78762306a36Sopenharmony_ci		error = break_deleg_wait(&delegated_inode);
78862306a36Sopenharmony_ci		if (!error)
78962306a36Sopenharmony_ci			goto retry_deleg;
79062306a36Sopenharmony_ci	}
79162306a36Sopenharmony_ci	return error;
79262306a36Sopenharmony_ci}
79362306a36Sopenharmony_ci
79462306a36Sopenharmony_ciint do_fchownat(int dfd, const char __user *filename, uid_t user, gid_t group,
79562306a36Sopenharmony_ci		int flag)
79662306a36Sopenharmony_ci{
79762306a36Sopenharmony_ci	struct path path;
79862306a36Sopenharmony_ci	int error = -EINVAL;
79962306a36Sopenharmony_ci	int lookup_flags;
80062306a36Sopenharmony_ci
80162306a36Sopenharmony_ci	if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
80262306a36Sopenharmony_ci		goto out;
80362306a36Sopenharmony_ci
80462306a36Sopenharmony_ci	lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
80562306a36Sopenharmony_ci	if (flag & AT_EMPTY_PATH)
80662306a36Sopenharmony_ci		lookup_flags |= LOOKUP_EMPTY;
80762306a36Sopenharmony_ciretry:
80862306a36Sopenharmony_ci	error = user_path_at(dfd, filename, lookup_flags, &path);
80962306a36Sopenharmony_ci	if (error)
81062306a36Sopenharmony_ci		goto out;
81162306a36Sopenharmony_ci	error = mnt_want_write(path.mnt);
81262306a36Sopenharmony_ci	if (error)
81362306a36Sopenharmony_ci		goto out_release;
81462306a36Sopenharmony_ci	error = chown_common(&path, user, group);
81562306a36Sopenharmony_ci	mnt_drop_write(path.mnt);
81662306a36Sopenharmony_ciout_release:
81762306a36Sopenharmony_ci	path_put(&path);
81862306a36Sopenharmony_ci	if (retry_estale(error, lookup_flags)) {
81962306a36Sopenharmony_ci		lookup_flags |= LOOKUP_REVAL;
82062306a36Sopenharmony_ci		goto retry;
82162306a36Sopenharmony_ci	}
82262306a36Sopenharmony_ciout:
82362306a36Sopenharmony_ci	return error;
82462306a36Sopenharmony_ci}
82562306a36Sopenharmony_ci
82662306a36Sopenharmony_ciSYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
82762306a36Sopenharmony_ci		gid_t, group, int, flag)
82862306a36Sopenharmony_ci{
82962306a36Sopenharmony_ci	return do_fchownat(dfd, filename, user, group, flag);
83062306a36Sopenharmony_ci}
83162306a36Sopenharmony_ci
83262306a36Sopenharmony_ciSYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
83362306a36Sopenharmony_ci{
83462306a36Sopenharmony_ci	return do_fchownat(AT_FDCWD, filename, user, group, 0);
83562306a36Sopenharmony_ci}
83662306a36Sopenharmony_ci
83762306a36Sopenharmony_ciSYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
83862306a36Sopenharmony_ci{
83962306a36Sopenharmony_ci	return do_fchownat(AT_FDCWD, filename, user, group,
84062306a36Sopenharmony_ci			   AT_SYMLINK_NOFOLLOW);
84162306a36Sopenharmony_ci}
84262306a36Sopenharmony_ci
84362306a36Sopenharmony_ciint vfs_fchown(struct file *file, uid_t user, gid_t group)
84462306a36Sopenharmony_ci{
84562306a36Sopenharmony_ci	int error;
84662306a36Sopenharmony_ci
84762306a36Sopenharmony_ci	error = mnt_want_write_file(file);
84862306a36Sopenharmony_ci	if (error)
84962306a36Sopenharmony_ci		return error;
85062306a36Sopenharmony_ci	audit_file(file);
85162306a36Sopenharmony_ci	error = chown_common(&file->f_path, user, group);
85262306a36Sopenharmony_ci	mnt_drop_write_file(file);
85362306a36Sopenharmony_ci	return error;
85462306a36Sopenharmony_ci}
85562306a36Sopenharmony_ci
85662306a36Sopenharmony_ciint ksys_fchown(unsigned int fd, uid_t user, gid_t group)
85762306a36Sopenharmony_ci{
85862306a36Sopenharmony_ci	struct fd f = fdget(fd);
85962306a36Sopenharmony_ci	int error = -EBADF;
86062306a36Sopenharmony_ci
86162306a36Sopenharmony_ci	if (f.file) {
86262306a36Sopenharmony_ci		error = vfs_fchown(f.file, user, group);
86362306a36Sopenharmony_ci		fdput(f);
86462306a36Sopenharmony_ci	}
86562306a36Sopenharmony_ci	return error;
86662306a36Sopenharmony_ci}
86762306a36Sopenharmony_ci
86862306a36Sopenharmony_ciSYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
86962306a36Sopenharmony_ci{
87062306a36Sopenharmony_ci	return ksys_fchown(fd, user, group);
87162306a36Sopenharmony_ci}
87262306a36Sopenharmony_ci
87362306a36Sopenharmony_cistatic int do_dentry_open(struct file *f,
87462306a36Sopenharmony_ci			  struct inode *inode,
87562306a36Sopenharmony_ci			  int (*open)(struct inode *, struct file *))
87662306a36Sopenharmony_ci{
87762306a36Sopenharmony_ci	static const struct file_operations empty_fops = {};
87862306a36Sopenharmony_ci	int error;
87962306a36Sopenharmony_ci
88062306a36Sopenharmony_ci	path_get(&f->f_path);
88162306a36Sopenharmony_ci	f->f_inode = inode;
88262306a36Sopenharmony_ci	f->f_mapping = inode->i_mapping;
88362306a36Sopenharmony_ci	f->f_wb_err = filemap_sample_wb_err(f->f_mapping);
88462306a36Sopenharmony_ci	f->f_sb_err = file_sample_sb_err(f);
88562306a36Sopenharmony_ci
88662306a36Sopenharmony_ci	if (unlikely(f->f_flags & O_PATH)) {
88762306a36Sopenharmony_ci		f->f_mode = FMODE_PATH | FMODE_OPENED;
88862306a36Sopenharmony_ci		f->f_op = &empty_fops;
88962306a36Sopenharmony_ci		return 0;
89062306a36Sopenharmony_ci	}
89162306a36Sopenharmony_ci
89262306a36Sopenharmony_ci	if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
89362306a36Sopenharmony_ci		i_readcount_inc(inode);
89462306a36Sopenharmony_ci	} else if (f->f_mode & FMODE_WRITE && !special_file(inode->i_mode)) {
89562306a36Sopenharmony_ci		error = get_write_access(inode);
89662306a36Sopenharmony_ci		if (unlikely(error))
89762306a36Sopenharmony_ci			goto cleanup_file;
89862306a36Sopenharmony_ci		error = __mnt_want_write(f->f_path.mnt);
89962306a36Sopenharmony_ci		if (unlikely(error)) {
90062306a36Sopenharmony_ci			put_write_access(inode);
90162306a36Sopenharmony_ci			goto cleanup_file;
90262306a36Sopenharmony_ci		}
90362306a36Sopenharmony_ci		f->f_mode |= FMODE_WRITER;
90462306a36Sopenharmony_ci	}
90562306a36Sopenharmony_ci
90662306a36Sopenharmony_ci	/* POSIX.1-2008/SUSv4 Section XSI 2.9.7 */
90762306a36Sopenharmony_ci	if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))
90862306a36Sopenharmony_ci		f->f_mode |= FMODE_ATOMIC_POS;
90962306a36Sopenharmony_ci
91062306a36Sopenharmony_ci	f->f_op = fops_get(inode->i_fop);
91162306a36Sopenharmony_ci	if (WARN_ON(!f->f_op)) {
91262306a36Sopenharmony_ci		error = -ENODEV;
91362306a36Sopenharmony_ci		goto cleanup_all;
91462306a36Sopenharmony_ci	}
91562306a36Sopenharmony_ci
91662306a36Sopenharmony_ci	error = security_file_open(f);
91762306a36Sopenharmony_ci	if (error)
91862306a36Sopenharmony_ci		goto cleanup_all;
91962306a36Sopenharmony_ci
92062306a36Sopenharmony_ci	error = break_lease(file_inode(f), f->f_flags);
92162306a36Sopenharmony_ci	if (error)
92262306a36Sopenharmony_ci		goto cleanup_all;
92362306a36Sopenharmony_ci
92462306a36Sopenharmony_ci	/* normally all 3 are set; ->open() can clear them if needed */
92562306a36Sopenharmony_ci	f->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
92662306a36Sopenharmony_ci	if (!open)
92762306a36Sopenharmony_ci		open = f->f_op->open;
92862306a36Sopenharmony_ci	if (open) {
92962306a36Sopenharmony_ci		error = open(inode, f);
93062306a36Sopenharmony_ci		if (error)
93162306a36Sopenharmony_ci			goto cleanup_all;
93262306a36Sopenharmony_ci	}
93362306a36Sopenharmony_ci	f->f_mode |= FMODE_OPENED;
93462306a36Sopenharmony_ci	if ((f->f_mode & FMODE_READ) &&
93562306a36Sopenharmony_ci	     likely(f->f_op->read || f->f_op->read_iter))
93662306a36Sopenharmony_ci		f->f_mode |= FMODE_CAN_READ;
93762306a36Sopenharmony_ci	if ((f->f_mode & FMODE_WRITE) &&
93862306a36Sopenharmony_ci	     likely(f->f_op->write || f->f_op->write_iter))
93962306a36Sopenharmony_ci		f->f_mode |= FMODE_CAN_WRITE;
94062306a36Sopenharmony_ci	if ((f->f_mode & FMODE_LSEEK) && !f->f_op->llseek)
94162306a36Sopenharmony_ci		f->f_mode &= ~FMODE_LSEEK;
94262306a36Sopenharmony_ci	if (f->f_mapping->a_ops && f->f_mapping->a_ops->direct_IO)
94362306a36Sopenharmony_ci		f->f_mode |= FMODE_CAN_ODIRECT;
94462306a36Sopenharmony_ci
94562306a36Sopenharmony_ci	f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
94662306a36Sopenharmony_ci	f->f_iocb_flags = iocb_flags(f);
94762306a36Sopenharmony_ci
94862306a36Sopenharmony_ci	file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
94962306a36Sopenharmony_ci
95062306a36Sopenharmony_ci	if ((f->f_flags & O_DIRECT) && !(f->f_mode & FMODE_CAN_ODIRECT))
95162306a36Sopenharmony_ci		return -EINVAL;
95262306a36Sopenharmony_ci
95362306a36Sopenharmony_ci	/*
95462306a36Sopenharmony_ci	 * XXX: Huge page cache doesn't support writing yet. Drop all page
95562306a36Sopenharmony_ci	 * cache for this file before processing writes.
95662306a36Sopenharmony_ci	 */
95762306a36Sopenharmony_ci	if (f->f_mode & FMODE_WRITE) {
95862306a36Sopenharmony_ci		/*
95962306a36Sopenharmony_ci		 * Paired with smp_mb() in collapse_file() to ensure nr_thps
96062306a36Sopenharmony_ci		 * is up to date and the update to i_writecount by
96162306a36Sopenharmony_ci		 * get_write_access() is visible. Ensures subsequent insertion
96262306a36Sopenharmony_ci		 * of THPs into the page cache will fail.
96362306a36Sopenharmony_ci		 */
96462306a36Sopenharmony_ci		smp_mb();
96562306a36Sopenharmony_ci		if (filemap_nr_thps(inode->i_mapping)) {
96662306a36Sopenharmony_ci			struct address_space *mapping = inode->i_mapping;
96762306a36Sopenharmony_ci
96862306a36Sopenharmony_ci			filemap_invalidate_lock(inode->i_mapping);
96962306a36Sopenharmony_ci			/*
97062306a36Sopenharmony_ci			 * unmap_mapping_range just need to be called once
97162306a36Sopenharmony_ci			 * here, because the private pages is not need to be
97262306a36Sopenharmony_ci			 * unmapped mapping (e.g. data segment of dynamic
97362306a36Sopenharmony_ci			 * shared libraries here).
97462306a36Sopenharmony_ci			 */
97562306a36Sopenharmony_ci			unmap_mapping_range(mapping, 0, 0, 0);
97662306a36Sopenharmony_ci			truncate_inode_pages(mapping, 0);
97762306a36Sopenharmony_ci			filemap_invalidate_unlock(inode->i_mapping);
97862306a36Sopenharmony_ci		}
97962306a36Sopenharmony_ci	}
98062306a36Sopenharmony_ci
98162306a36Sopenharmony_ci	/*
98262306a36Sopenharmony_ci	 * Once we return a file with FMODE_OPENED, __fput() will call
98362306a36Sopenharmony_ci	 * fsnotify_close(), so we need fsnotify_open() here for symmetry.
98462306a36Sopenharmony_ci	 */
98562306a36Sopenharmony_ci	fsnotify_open(f);
98662306a36Sopenharmony_ci	return 0;
98762306a36Sopenharmony_ci
98862306a36Sopenharmony_cicleanup_all:
98962306a36Sopenharmony_ci	if (WARN_ON_ONCE(error > 0))
99062306a36Sopenharmony_ci		error = -EINVAL;
99162306a36Sopenharmony_ci	fops_put(f->f_op);
99262306a36Sopenharmony_ci	put_file_access(f);
99362306a36Sopenharmony_cicleanup_file:
99462306a36Sopenharmony_ci	path_put(&f->f_path);
99562306a36Sopenharmony_ci	f->f_path.mnt = NULL;
99662306a36Sopenharmony_ci	f->f_path.dentry = NULL;
99762306a36Sopenharmony_ci	f->f_inode = NULL;
99862306a36Sopenharmony_ci	return error;
99962306a36Sopenharmony_ci}
100062306a36Sopenharmony_ci
100162306a36Sopenharmony_ci/**
100262306a36Sopenharmony_ci * finish_open - finish opening a file
100362306a36Sopenharmony_ci * @file: file pointer
100462306a36Sopenharmony_ci * @dentry: pointer to dentry
100562306a36Sopenharmony_ci * @open: open callback
100662306a36Sopenharmony_ci *
100762306a36Sopenharmony_ci * This can be used to finish opening a file passed to i_op->atomic_open().
100862306a36Sopenharmony_ci *
100962306a36Sopenharmony_ci * If the open callback is set to NULL, then the standard f_op->open()
101062306a36Sopenharmony_ci * filesystem callback is substituted.
101162306a36Sopenharmony_ci *
101262306a36Sopenharmony_ci * NB: the dentry reference is _not_ consumed.  If, for example, the dentry is
101362306a36Sopenharmony_ci * the return value of d_splice_alias(), then the caller needs to perform dput()
101462306a36Sopenharmony_ci * on it after finish_open().
101562306a36Sopenharmony_ci *
101662306a36Sopenharmony_ci * Returns zero on success or -errno if the open failed.
101762306a36Sopenharmony_ci */
101862306a36Sopenharmony_ciint finish_open(struct file *file, struct dentry *dentry,
101962306a36Sopenharmony_ci		int (*open)(struct inode *, struct file *))
102062306a36Sopenharmony_ci{
102162306a36Sopenharmony_ci	BUG_ON(file->f_mode & FMODE_OPENED); /* once it's opened, it's opened */
102262306a36Sopenharmony_ci
102362306a36Sopenharmony_ci	file->f_path.dentry = dentry;
102462306a36Sopenharmony_ci	return do_dentry_open(file, d_backing_inode(dentry), open);
102562306a36Sopenharmony_ci}
102662306a36Sopenharmony_ciEXPORT_SYMBOL(finish_open);
102762306a36Sopenharmony_ci
102862306a36Sopenharmony_ci/**
102962306a36Sopenharmony_ci * finish_no_open - finish ->atomic_open() without opening the file
103062306a36Sopenharmony_ci *
103162306a36Sopenharmony_ci * @file: file pointer
103262306a36Sopenharmony_ci * @dentry: dentry or NULL (as returned from ->lookup())
103362306a36Sopenharmony_ci *
103462306a36Sopenharmony_ci * This can be used to set the result of a successful lookup in ->atomic_open().
103562306a36Sopenharmony_ci *
103662306a36Sopenharmony_ci * NB: unlike finish_open() this function does consume the dentry reference and
103762306a36Sopenharmony_ci * the caller need not dput() it.
103862306a36Sopenharmony_ci *
103962306a36Sopenharmony_ci * Returns "0" which must be the return value of ->atomic_open() after having
104062306a36Sopenharmony_ci * called this function.
104162306a36Sopenharmony_ci */
104262306a36Sopenharmony_ciint finish_no_open(struct file *file, struct dentry *dentry)
104362306a36Sopenharmony_ci{
104462306a36Sopenharmony_ci	file->f_path.dentry = dentry;
104562306a36Sopenharmony_ci	return 0;
104662306a36Sopenharmony_ci}
104762306a36Sopenharmony_ciEXPORT_SYMBOL(finish_no_open);
104862306a36Sopenharmony_ci
104962306a36Sopenharmony_cichar *file_path(struct file *filp, char *buf, int buflen)
105062306a36Sopenharmony_ci{
105162306a36Sopenharmony_ci	return d_path(&filp->f_path, buf, buflen);
105262306a36Sopenharmony_ci}
105362306a36Sopenharmony_ciEXPORT_SYMBOL(file_path);
105462306a36Sopenharmony_ci
105562306a36Sopenharmony_ci/**
105662306a36Sopenharmony_ci * vfs_open - open the file at the given path
105762306a36Sopenharmony_ci * @path: path to open
105862306a36Sopenharmony_ci * @file: newly allocated file with f_flag initialized
105962306a36Sopenharmony_ci */
106062306a36Sopenharmony_ciint vfs_open(const struct path *path, struct file *file)
106162306a36Sopenharmony_ci{
106262306a36Sopenharmony_ci	file->f_path = *path;
106362306a36Sopenharmony_ci	return do_dentry_open(file, d_backing_inode(path->dentry), NULL);
106462306a36Sopenharmony_ci}
106562306a36Sopenharmony_ci
106662306a36Sopenharmony_cistruct file *dentry_open(const struct path *path, int flags,
106762306a36Sopenharmony_ci			 const struct cred *cred)
106862306a36Sopenharmony_ci{
106962306a36Sopenharmony_ci	int error;
107062306a36Sopenharmony_ci	struct file *f;
107162306a36Sopenharmony_ci
107262306a36Sopenharmony_ci	/* We must always pass in a valid mount pointer. */
107362306a36Sopenharmony_ci	BUG_ON(!path->mnt);
107462306a36Sopenharmony_ci
107562306a36Sopenharmony_ci	f = alloc_empty_file(flags, cred);
107662306a36Sopenharmony_ci	if (!IS_ERR(f)) {
107762306a36Sopenharmony_ci		error = vfs_open(path, f);
107862306a36Sopenharmony_ci		if (error) {
107962306a36Sopenharmony_ci			fput(f);
108062306a36Sopenharmony_ci			f = ERR_PTR(error);
108162306a36Sopenharmony_ci		}
108262306a36Sopenharmony_ci	}
108362306a36Sopenharmony_ci	return f;
108462306a36Sopenharmony_ci}
108562306a36Sopenharmony_ciEXPORT_SYMBOL(dentry_open);
108662306a36Sopenharmony_ci
108762306a36Sopenharmony_ci/**
108862306a36Sopenharmony_ci * dentry_create - Create and open a file
108962306a36Sopenharmony_ci * @path: path to create
109062306a36Sopenharmony_ci * @flags: O_ flags
109162306a36Sopenharmony_ci * @mode: mode bits for new file
109262306a36Sopenharmony_ci * @cred: credentials to use
109362306a36Sopenharmony_ci *
109462306a36Sopenharmony_ci * Caller must hold the parent directory's lock, and have prepared
109562306a36Sopenharmony_ci * a negative dentry, placed in @path->dentry, for the new file.
109662306a36Sopenharmony_ci *
109762306a36Sopenharmony_ci * Caller sets @path->mnt to the vfsmount of the filesystem where
109862306a36Sopenharmony_ci * the new file is to be created. The parent directory and the
109962306a36Sopenharmony_ci * negative dentry must reside on the same filesystem instance.
110062306a36Sopenharmony_ci *
110162306a36Sopenharmony_ci * On success, returns a "struct file *". Otherwise a ERR_PTR
110262306a36Sopenharmony_ci * is returned.
110362306a36Sopenharmony_ci */
110462306a36Sopenharmony_cistruct file *dentry_create(const struct path *path, int flags, umode_t mode,
110562306a36Sopenharmony_ci			   const struct cred *cred)
110662306a36Sopenharmony_ci{
110762306a36Sopenharmony_ci	struct file *f;
110862306a36Sopenharmony_ci	int error;
110962306a36Sopenharmony_ci
111062306a36Sopenharmony_ci	f = alloc_empty_file(flags, cred);
111162306a36Sopenharmony_ci	if (IS_ERR(f))
111262306a36Sopenharmony_ci		return f;
111362306a36Sopenharmony_ci
111462306a36Sopenharmony_ci	error = vfs_create(mnt_idmap(path->mnt),
111562306a36Sopenharmony_ci			   d_inode(path->dentry->d_parent),
111662306a36Sopenharmony_ci			   path->dentry, mode, true);
111762306a36Sopenharmony_ci	if (!error)
111862306a36Sopenharmony_ci		error = vfs_open(path, f);
111962306a36Sopenharmony_ci
112062306a36Sopenharmony_ci	if (unlikely(error)) {
112162306a36Sopenharmony_ci		fput(f);
112262306a36Sopenharmony_ci		return ERR_PTR(error);
112362306a36Sopenharmony_ci	}
112462306a36Sopenharmony_ci	return f;
112562306a36Sopenharmony_ci}
112662306a36Sopenharmony_ciEXPORT_SYMBOL(dentry_create);
112762306a36Sopenharmony_ci
112862306a36Sopenharmony_ci/**
112962306a36Sopenharmony_ci * kernel_file_open - open a file for kernel internal use
113062306a36Sopenharmony_ci * @path:	path of the file to open
113162306a36Sopenharmony_ci * @flags:	open flags
113262306a36Sopenharmony_ci * @inode:	the inode
113362306a36Sopenharmony_ci * @cred:	credentials for open
113462306a36Sopenharmony_ci *
113562306a36Sopenharmony_ci * Open a file for use by in-kernel consumers. The file is not accounted
113662306a36Sopenharmony_ci * against nr_files and must not be installed into the file descriptor
113762306a36Sopenharmony_ci * table.
113862306a36Sopenharmony_ci *
113962306a36Sopenharmony_ci * Return: Opened file on success, an error pointer on failure.
114062306a36Sopenharmony_ci */
114162306a36Sopenharmony_cistruct file *kernel_file_open(const struct path *path, int flags,
114262306a36Sopenharmony_ci				struct inode *inode, const struct cred *cred)
114362306a36Sopenharmony_ci{
114462306a36Sopenharmony_ci	struct file *f;
114562306a36Sopenharmony_ci	int error;
114662306a36Sopenharmony_ci
114762306a36Sopenharmony_ci	f = alloc_empty_file_noaccount(flags, cred);
114862306a36Sopenharmony_ci	if (IS_ERR(f))
114962306a36Sopenharmony_ci		return f;
115062306a36Sopenharmony_ci
115162306a36Sopenharmony_ci	f->f_path = *path;
115262306a36Sopenharmony_ci	error = do_dentry_open(f, inode, NULL);
115362306a36Sopenharmony_ci	if (error) {
115462306a36Sopenharmony_ci		fput(f);
115562306a36Sopenharmony_ci		f = ERR_PTR(error);
115662306a36Sopenharmony_ci	}
115762306a36Sopenharmony_ci	return f;
115862306a36Sopenharmony_ci}
115962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(kernel_file_open);
116062306a36Sopenharmony_ci
116162306a36Sopenharmony_ci/**
116262306a36Sopenharmony_ci * backing_file_open - open a backing file for kernel internal use
116362306a36Sopenharmony_ci * @path:	path of the file to open
116462306a36Sopenharmony_ci * @flags:	open flags
116562306a36Sopenharmony_ci * @real_path:	path of the backing file
116662306a36Sopenharmony_ci * @cred:	credentials for open
116762306a36Sopenharmony_ci *
116862306a36Sopenharmony_ci * Open a backing file for a stackable filesystem (e.g., overlayfs).
116962306a36Sopenharmony_ci * @path may be on the stackable filesystem and backing inode on the
117062306a36Sopenharmony_ci * underlying filesystem. In this case, we want to be able to return
117162306a36Sopenharmony_ci * the @real_path of the backing inode. This is done by embedding the
117262306a36Sopenharmony_ci * returned file into a container structure that also stores the path of
117362306a36Sopenharmony_ci * the backing inode on the underlying filesystem, which can be
117462306a36Sopenharmony_ci * retrieved using backing_file_real_path().
117562306a36Sopenharmony_ci */
117662306a36Sopenharmony_cistruct file *backing_file_open(const struct path *path, int flags,
117762306a36Sopenharmony_ci			       const struct path *real_path,
117862306a36Sopenharmony_ci			       const struct cred *cred)
117962306a36Sopenharmony_ci{
118062306a36Sopenharmony_ci	struct file *f;
118162306a36Sopenharmony_ci	int error;
118262306a36Sopenharmony_ci
118362306a36Sopenharmony_ci	f = alloc_empty_backing_file(flags, cred);
118462306a36Sopenharmony_ci	if (IS_ERR(f))
118562306a36Sopenharmony_ci		return f;
118662306a36Sopenharmony_ci
118762306a36Sopenharmony_ci	f->f_path = *path;
118862306a36Sopenharmony_ci	path_get(real_path);
118962306a36Sopenharmony_ci	*backing_file_real_path(f) = *real_path;
119062306a36Sopenharmony_ci	error = do_dentry_open(f, d_inode(real_path->dentry), NULL);
119162306a36Sopenharmony_ci	if (error) {
119262306a36Sopenharmony_ci		fput(f);
119362306a36Sopenharmony_ci		f = ERR_PTR(error);
119462306a36Sopenharmony_ci	}
119562306a36Sopenharmony_ci
119662306a36Sopenharmony_ci	return f;
119762306a36Sopenharmony_ci}
119862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(backing_file_open);
119962306a36Sopenharmony_ci
120062306a36Sopenharmony_ci#define WILL_CREATE(flags)	(flags & (O_CREAT | __O_TMPFILE))
120162306a36Sopenharmony_ci#define O_PATH_FLAGS		(O_DIRECTORY | O_NOFOLLOW | O_PATH | O_CLOEXEC)
120262306a36Sopenharmony_ci
120362306a36Sopenharmony_ciinline struct open_how build_open_how(int flags, umode_t mode)
120462306a36Sopenharmony_ci{
120562306a36Sopenharmony_ci	struct open_how how = {
120662306a36Sopenharmony_ci		.flags = flags & VALID_OPEN_FLAGS,
120762306a36Sopenharmony_ci		.mode = mode & S_IALLUGO,
120862306a36Sopenharmony_ci	};
120962306a36Sopenharmony_ci
121062306a36Sopenharmony_ci	/* O_PATH beats everything else. */
121162306a36Sopenharmony_ci	if (how.flags & O_PATH)
121262306a36Sopenharmony_ci		how.flags &= O_PATH_FLAGS;
121362306a36Sopenharmony_ci	/* Modes should only be set for create-like flags. */
121462306a36Sopenharmony_ci	if (!WILL_CREATE(how.flags))
121562306a36Sopenharmony_ci		how.mode = 0;
121662306a36Sopenharmony_ci	return how;
121762306a36Sopenharmony_ci}
121862306a36Sopenharmony_ci
121962306a36Sopenharmony_ciinline int build_open_flags(const struct open_how *how, struct open_flags *op)
122062306a36Sopenharmony_ci{
122162306a36Sopenharmony_ci	u64 flags = how->flags;
122262306a36Sopenharmony_ci	u64 strip = __FMODE_NONOTIFY | O_CLOEXEC;
122362306a36Sopenharmony_ci	int lookup_flags = 0;
122462306a36Sopenharmony_ci	int acc_mode = ACC_MODE(flags);
122562306a36Sopenharmony_ci
122662306a36Sopenharmony_ci	BUILD_BUG_ON_MSG(upper_32_bits(VALID_OPEN_FLAGS),
122762306a36Sopenharmony_ci			 "struct open_flags doesn't yet handle flags > 32 bits");
122862306a36Sopenharmony_ci
122962306a36Sopenharmony_ci	/*
123062306a36Sopenharmony_ci	 * Strip flags that either shouldn't be set by userspace like
123162306a36Sopenharmony_ci	 * FMODE_NONOTIFY or that aren't relevant in determining struct
123262306a36Sopenharmony_ci	 * open_flags like O_CLOEXEC.
123362306a36Sopenharmony_ci	 */
123462306a36Sopenharmony_ci	flags &= ~strip;
123562306a36Sopenharmony_ci
123662306a36Sopenharmony_ci	/*
123762306a36Sopenharmony_ci	 * Older syscalls implicitly clear all of the invalid flags or argument
123862306a36Sopenharmony_ci	 * values before calling build_open_flags(), but openat2(2) checks all
123962306a36Sopenharmony_ci	 * of its arguments.
124062306a36Sopenharmony_ci	 */
124162306a36Sopenharmony_ci	if (flags & ~VALID_OPEN_FLAGS)
124262306a36Sopenharmony_ci		return -EINVAL;
124362306a36Sopenharmony_ci	if (how->resolve & ~VALID_RESOLVE_FLAGS)
124462306a36Sopenharmony_ci		return -EINVAL;
124562306a36Sopenharmony_ci
124662306a36Sopenharmony_ci	/* Scoping flags are mutually exclusive. */
124762306a36Sopenharmony_ci	if ((how->resolve & RESOLVE_BENEATH) && (how->resolve & RESOLVE_IN_ROOT))
124862306a36Sopenharmony_ci		return -EINVAL;
124962306a36Sopenharmony_ci
125062306a36Sopenharmony_ci	/* Deal with the mode. */
125162306a36Sopenharmony_ci	if (WILL_CREATE(flags)) {
125262306a36Sopenharmony_ci		if (how->mode & ~S_IALLUGO)
125362306a36Sopenharmony_ci			return -EINVAL;
125462306a36Sopenharmony_ci		op->mode = how->mode | S_IFREG;
125562306a36Sopenharmony_ci	} else {
125662306a36Sopenharmony_ci		if (how->mode != 0)
125762306a36Sopenharmony_ci			return -EINVAL;
125862306a36Sopenharmony_ci		op->mode = 0;
125962306a36Sopenharmony_ci	}
126062306a36Sopenharmony_ci
126162306a36Sopenharmony_ci	/*
126262306a36Sopenharmony_ci	 * Block bugs where O_DIRECTORY | O_CREAT created regular files.
126362306a36Sopenharmony_ci	 * Note, that blocking O_DIRECTORY | O_CREAT here also protects
126462306a36Sopenharmony_ci	 * O_TMPFILE below which requires O_DIRECTORY being raised.
126562306a36Sopenharmony_ci	 */
126662306a36Sopenharmony_ci	if ((flags & (O_DIRECTORY | O_CREAT)) == (O_DIRECTORY | O_CREAT))
126762306a36Sopenharmony_ci		return -EINVAL;
126862306a36Sopenharmony_ci
126962306a36Sopenharmony_ci	/* Now handle the creative implementation of O_TMPFILE. */
127062306a36Sopenharmony_ci	if (flags & __O_TMPFILE) {
127162306a36Sopenharmony_ci		/*
127262306a36Sopenharmony_ci		 * In order to ensure programs get explicit errors when trying
127362306a36Sopenharmony_ci		 * to use O_TMPFILE on old kernels we enforce that O_DIRECTORY
127462306a36Sopenharmony_ci		 * is raised alongside __O_TMPFILE.
127562306a36Sopenharmony_ci		 */
127662306a36Sopenharmony_ci		if (!(flags & O_DIRECTORY))
127762306a36Sopenharmony_ci			return -EINVAL;
127862306a36Sopenharmony_ci		if (!(acc_mode & MAY_WRITE))
127962306a36Sopenharmony_ci			return -EINVAL;
128062306a36Sopenharmony_ci	}
128162306a36Sopenharmony_ci	if (flags & O_PATH) {
128262306a36Sopenharmony_ci		/* O_PATH only permits certain other flags to be set. */
128362306a36Sopenharmony_ci		if (flags & ~O_PATH_FLAGS)
128462306a36Sopenharmony_ci			return -EINVAL;
128562306a36Sopenharmony_ci		acc_mode = 0;
128662306a36Sopenharmony_ci	}
128762306a36Sopenharmony_ci
128862306a36Sopenharmony_ci	/*
128962306a36Sopenharmony_ci	 * O_SYNC is implemented as __O_SYNC|O_DSYNC.  As many places only
129062306a36Sopenharmony_ci	 * check for O_DSYNC if the need any syncing at all we enforce it's
129162306a36Sopenharmony_ci	 * always set instead of having to deal with possibly weird behaviour
129262306a36Sopenharmony_ci	 * for malicious applications setting only __O_SYNC.
129362306a36Sopenharmony_ci	 */
129462306a36Sopenharmony_ci	if (flags & __O_SYNC)
129562306a36Sopenharmony_ci		flags |= O_DSYNC;
129662306a36Sopenharmony_ci
129762306a36Sopenharmony_ci	op->open_flag = flags;
129862306a36Sopenharmony_ci
129962306a36Sopenharmony_ci	/* O_TRUNC implies we need access checks for write permissions */
130062306a36Sopenharmony_ci	if (flags & O_TRUNC)
130162306a36Sopenharmony_ci		acc_mode |= MAY_WRITE;
130262306a36Sopenharmony_ci
130362306a36Sopenharmony_ci	/* Allow the LSM permission hook to distinguish append
130462306a36Sopenharmony_ci	   access from general write access. */
130562306a36Sopenharmony_ci	if (flags & O_APPEND)
130662306a36Sopenharmony_ci		acc_mode |= MAY_APPEND;
130762306a36Sopenharmony_ci
130862306a36Sopenharmony_ci	op->acc_mode = acc_mode;
130962306a36Sopenharmony_ci
131062306a36Sopenharmony_ci	op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
131162306a36Sopenharmony_ci
131262306a36Sopenharmony_ci	if (flags & O_CREAT) {
131362306a36Sopenharmony_ci		op->intent |= LOOKUP_CREATE;
131462306a36Sopenharmony_ci		if (flags & O_EXCL) {
131562306a36Sopenharmony_ci			op->intent |= LOOKUP_EXCL;
131662306a36Sopenharmony_ci			flags |= O_NOFOLLOW;
131762306a36Sopenharmony_ci		}
131862306a36Sopenharmony_ci	}
131962306a36Sopenharmony_ci
132062306a36Sopenharmony_ci	if (flags & O_DIRECTORY)
132162306a36Sopenharmony_ci		lookup_flags |= LOOKUP_DIRECTORY;
132262306a36Sopenharmony_ci	if (!(flags & O_NOFOLLOW))
132362306a36Sopenharmony_ci		lookup_flags |= LOOKUP_FOLLOW;
132462306a36Sopenharmony_ci
132562306a36Sopenharmony_ci	if (how->resolve & RESOLVE_NO_XDEV)
132662306a36Sopenharmony_ci		lookup_flags |= LOOKUP_NO_XDEV;
132762306a36Sopenharmony_ci	if (how->resolve & RESOLVE_NO_MAGICLINKS)
132862306a36Sopenharmony_ci		lookup_flags |= LOOKUP_NO_MAGICLINKS;
132962306a36Sopenharmony_ci	if (how->resolve & RESOLVE_NO_SYMLINKS)
133062306a36Sopenharmony_ci		lookup_flags |= LOOKUP_NO_SYMLINKS;
133162306a36Sopenharmony_ci	if (how->resolve & RESOLVE_BENEATH)
133262306a36Sopenharmony_ci		lookup_flags |= LOOKUP_BENEATH;
133362306a36Sopenharmony_ci	if (how->resolve & RESOLVE_IN_ROOT)
133462306a36Sopenharmony_ci		lookup_flags |= LOOKUP_IN_ROOT;
133562306a36Sopenharmony_ci	if (how->resolve & RESOLVE_CACHED) {
133662306a36Sopenharmony_ci		/* Don't bother even trying for create/truncate/tmpfile open */
133762306a36Sopenharmony_ci		if (flags & (O_TRUNC | O_CREAT | __O_TMPFILE))
133862306a36Sopenharmony_ci			return -EAGAIN;
133962306a36Sopenharmony_ci		lookup_flags |= LOOKUP_CACHED;
134062306a36Sopenharmony_ci	}
134162306a36Sopenharmony_ci
134262306a36Sopenharmony_ci	op->lookup_flags = lookup_flags;
134362306a36Sopenharmony_ci	return 0;
134462306a36Sopenharmony_ci}
134562306a36Sopenharmony_ci
134662306a36Sopenharmony_ci/**
134762306a36Sopenharmony_ci * file_open_name - open file and return file pointer
134862306a36Sopenharmony_ci *
134962306a36Sopenharmony_ci * @name:	struct filename containing path to open
135062306a36Sopenharmony_ci * @flags:	open flags as per the open(2) second argument
135162306a36Sopenharmony_ci * @mode:	mode for the new file if O_CREAT is set, else ignored
135262306a36Sopenharmony_ci *
135362306a36Sopenharmony_ci * This is the helper to open a file from kernelspace if you really
135462306a36Sopenharmony_ci * have to.  But in generally you should not do this, so please move
135562306a36Sopenharmony_ci * along, nothing to see here..
135662306a36Sopenharmony_ci */
135762306a36Sopenharmony_cistruct file *file_open_name(struct filename *name, int flags, umode_t mode)
135862306a36Sopenharmony_ci{
135962306a36Sopenharmony_ci	struct open_flags op;
136062306a36Sopenharmony_ci	struct open_how how = build_open_how(flags, mode);
136162306a36Sopenharmony_ci	int err = build_open_flags(&how, &op);
136262306a36Sopenharmony_ci	if (err)
136362306a36Sopenharmony_ci		return ERR_PTR(err);
136462306a36Sopenharmony_ci	return do_filp_open(AT_FDCWD, name, &op);
136562306a36Sopenharmony_ci}
136662306a36Sopenharmony_ci
136762306a36Sopenharmony_ci/**
136862306a36Sopenharmony_ci * filp_open - open file and return file pointer
136962306a36Sopenharmony_ci *
137062306a36Sopenharmony_ci * @filename:	path to open
137162306a36Sopenharmony_ci * @flags:	open flags as per the open(2) second argument
137262306a36Sopenharmony_ci * @mode:	mode for the new file if O_CREAT is set, else ignored
137362306a36Sopenharmony_ci *
137462306a36Sopenharmony_ci * This is the helper to open a file from kernelspace if you really
137562306a36Sopenharmony_ci * have to.  But in generally you should not do this, so please move
137662306a36Sopenharmony_ci * along, nothing to see here..
137762306a36Sopenharmony_ci */
137862306a36Sopenharmony_cistruct file *filp_open(const char *filename, int flags, umode_t mode)
137962306a36Sopenharmony_ci{
138062306a36Sopenharmony_ci	struct filename *name = getname_kernel(filename);
138162306a36Sopenharmony_ci	struct file *file = ERR_CAST(name);
138262306a36Sopenharmony_ci
138362306a36Sopenharmony_ci	if (!IS_ERR(name)) {
138462306a36Sopenharmony_ci		file = file_open_name(name, flags, mode);
138562306a36Sopenharmony_ci		putname(name);
138662306a36Sopenharmony_ci	}
138762306a36Sopenharmony_ci	return file;
138862306a36Sopenharmony_ci}
138962306a36Sopenharmony_ciEXPORT_SYMBOL(filp_open);
139062306a36Sopenharmony_ci
139162306a36Sopenharmony_cistruct file *file_open_root(const struct path *root,
139262306a36Sopenharmony_ci			    const char *filename, int flags, umode_t mode)
139362306a36Sopenharmony_ci{
139462306a36Sopenharmony_ci	struct open_flags op;
139562306a36Sopenharmony_ci	struct open_how how = build_open_how(flags, mode);
139662306a36Sopenharmony_ci	int err = build_open_flags(&how, &op);
139762306a36Sopenharmony_ci	if (err)
139862306a36Sopenharmony_ci		return ERR_PTR(err);
139962306a36Sopenharmony_ci	return do_file_open_root(root, filename, &op);
140062306a36Sopenharmony_ci}
140162306a36Sopenharmony_ciEXPORT_SYMBOL(file_open_root);
140262306a36Sopenharmony_ci
140362306a36Sopenharmony_cistatic long do_sys_openat2(int dfd, const char __user *filename,
140462306a36Sopenharmony_ci			   struct open_how *how)
140562306a36Sopenharmony_ci{
140662306a36Sopenharmony_ci	struct open_flags op;
140762306a36Sopenharmony_ci	int fd = build_open_flags(how, &op);
140862306a36Sopenharmony_ci	struct filename *tmp;
140962306a36Sopenharmony_ci
141062306a36Sopenharmony_ci	if (fd)
141162306a36Sopenharmony_ci		return fd;
141262306a36Sopenharmony_ci
141362306a36Sopenharmony_ci	tmp = getname(filename);
141462306a36Sopenharmony_ci	if (IS_ERR(tmp))
141562306a36Sopenharmony_ci		return PTR_ERR(tmp);
141662306a36Sopenharmony_ci
141762306a36Sopenharmony_ci	fd = get_unused_fd_flags(how->flags);
141862306a36Sopenharmony_ci	if (fd >= 0) {
141962306a36Sopenharmony_ci		struct file *f = do_filp_open(dfd, tmp, &op);
142062306a36Sopenharmony_ci		if (IS_ERR(f)) {
142162306a36Sopenharmony_ci			put_unused_fd(fd);
142262306a36Sopenharmony_ci			fd = PTR_ERR(f);
142362306a36Sopenharmony_ci		} else {
142462306a36Sopenharmony_ci			fd_install(fd, f);
142562306a36Sopenharmony_ci		}
142662306a36Sopenharmony_ci	}
142762306a36Sopenharmony_ci	putname(tmp);
142862306a36Sopenharmony_ci	return fd;
142962306a36Sopenharmony_ci}
143062306a36Sopenharmony_ci
143162306a36Sopenharmony_cilong do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
143262306a36Sopenharmony_ci{
143362306a36Sopenharmony_ci	struct open_how how = build_open_how(flags, mode);
143462306a36Sopenharmony_ci	return do_sys_openat2(dfd, filename, &how);
143562306a36Sopenharmony_ci}
143662306a36Sopenharmony_ci
143762306a36Sopenharmony_ci
143862306a36Sopenharmony_ciSYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
143962306a36Sopenharmony_ci{
144062306a36Sopenharmony_ci	if (force_o_largefile())
144162306a36Sopenharmony_ci		flags |= O_LARGEFILE;
144262306a36Sopenharmony_ci	return do_sys_open(AT_FDCWD, filename, flags, mode);
144362306a36Sopenharmony_ci}
144462306a36Sopenharmony_ci
144562306a36Sopenharmony_ciSYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
144662306a36Sopenharmony_ci		umode_t, mode)
144762306a36Sopenharmony_ci{
144862306a36Sopenharmony_ci	if (force_o_largefile())
144962306a36Sopenharmony_ci		flags |= O_LARGEFILE;
145062306a36Sopenharmony_ci	return do_sys_open(dfd, filename, flags, mode);
145162306a36Sopenharmony_ci}
145262306a36Sopenharmony_ci
145362306a36Sopenharmony_ciSYSCALL_DEFINE4(openat2, int, dfd, const char __user *, filename,
145462306a36Sopenharmony_ci		struct open_how __user *, how, size_t, usize)
145562306a36Sopenharmony_ci{
145662306a36Sopenharmony_ci	int err;
145762306a36Sopenharmony_ci	struct open_how tmp;
145862306a36Sopenharmony_ci
145962306a36Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct open_how) < OPEN_HOW_SIZE_VER0);
146062306a36Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct open_how) != OPEN_HOW_SIZE_LATEST);
146162306a36Sopenharmony_ci
146262306a36Sopenharmony_ci	if (unlikely(usize < OPEN_HOW_SIZE_VER0))
146362306a36Sopenharmony_ci		return -EINVAL;
146462306a36Sopenharmony_ci
146562306a36Sopenharmony_ci	err = copy_struct_from_user(&tmp, sizeof(tmp), how, usize);
146662306a36Sopenharmony_ci	if (err)
146762306a36Sopenharmony_ci		return err;
146862306a36Sopenharmony_ci
146962306a36Sopenharmony_ci	audit_openat2_how(&tmp);
147062306a36Sopenharmony_ci
147162306a36Sopenharmony_ci	/* O_LARGEFILE is only allowed for non-O_PATH. */
147262306a36Sopenharmony_ci	if (!(tmp.flags & O_PATH) && force_o_largefile())
147362306a36Sopenharmony_ci		tmp.flags |= O_LARGEFILE;
147462306a36Sopenharmony_ci
147562306a36Sopenharmony_ci	return do_sys_openat2(dfd, filename, &tmp);
147662306a36Sopenharmony_ci}
147762306a36Sopenharmony_ci
147862306a36Sopenharmony_ci#ifdef CONFIG_COMPAT
147962306a36Sopenharmony_ci/*
148062306a36Sopenharmony_ci * Exactly like sys_open(), except that it doesn't set the
148162306a36Sopenharmony_ci * O_LARGEFILE flag.
148262306a36Sopenharmony_ci */
148362306a36Sopenharmony_ciCOMPAT_SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
148462306a36Sopenharmony_ci{
148562306a36Sopenharmony_ci	return do_sys_open(AT_FDCWD, filename, flags, mode);
148662306a36Sopenharmony_ci}
148762306a36Sopenharmony_ci
148862306a36Sopenharmony_ci/*
148962306a36Sopenharmony_ci * Exactly like sys_openat(), except that it doesn't set the
149062306a36Sopenharmony_ci * O_LARGEFILE flag.
149162306a36Sopenharmony_ci */
149262306a36Sopenharmony_ciCOMPAT_SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, umode_t, mode)
149362306a36Sopenharmony_ci{
149462306a36Sopenharmony_ci	return do_sys_open(dfd, filename, flags, mode);
149562306a36Sopenharmony_ci}
149662306a36Sopenharmony_ci#endif
149762306a36Sopenharmony_ci
149862306a36Sopenharmony_ci#ifndef __alpha__
149962306a36Sopenharmony_ci
150062306a36Sopenharmony_ci/*
150162306a36Sopenharmony_ci * For backward compatibility?  Maybe this should be moved
150262306a36Sopenharmony_ci * into arch/i386 instead?
150362306a36Sopenharmony_ci */
150462306a36Sopenharmony_ciSYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)
150562306a36Sopenharmony_ci{
150662306a36Sopenharmony_ci	int flags = O_CREAT | O_WRONLY | O_TRUNC;
150762306a36Sopenharmony_ci
150862306a36Sopenharmony_ci	if (force_o_largefile())
150962306a36Sopenharmony_ci		flags |= O_LARGEFILE;
151062306a36Sopenharmony_ci	return do_sys_open(AT_FDCWD, pathname, flags, mode);
151162306a36Sopenharmony_ci}
151262306a36Sopenharmony_ci#endif
151362306a36Sopenharmony_ci
151462306a36Sopenharmony_ci/*
151562306a36Sopenharmony_ci * "id" is the POSIX thread ID. We use the
151662306a36Sopenharmony_ci * files pointer for this..
151762306a36Sopenharmony_ci */
151862306a36Sopenharmony_cistatic int filp_flush(struct file *filp, fl_owner_t id)
151962306a36Sopenharmony_ci{
152062306a36Sopenharmony_ci	int retval = 0;
152162306a36Sopenharmony_ci
152262306a36Sopenharmony_ci	if (CHECK_DATA_CORRUPTION(file_count(filp) == 0,
152362306a36Sopenharmony_ci			"VFS: Close: file count is 0 (f_op=%ps)",
152462306a36Sopenharmony_ci			filp->f_op)) {
152562306a36Sopenharmony_ci		return 0;
152662306a36Sopenharmony_ci	}
152762306a36Sopenharmony_ci
152862306a36Sopenharmony_ci	if (filp->f_op->flush)
152962306a36Sopenharmony_ci		retval = filp->f_op->flush(filp, id);
153062306a36Sopenharmony_ci
153162306a36Sopenharmony_ci	if (likely(!(filp->f_mode & FMODE_PATH))) {
153262306a36Sopenharmony_ci		dnotify_flush(filp, id);
153362306a36Sopenharmony_ci		locks_remove_posix(filp, id);
153462306a36Sopenharmony_ci	}
153562306a36Sopenharmony_ci	return retval;
153662306a36Sopenharmony_ci}
153762306a36Sopenharmony_ci
153862306a36Sopenharmony_ciint filp_close(struct file *filp, fl_owner_t id)
153962306a36Sopenharmony_ci{
154062306a36Sopenharmony_ci	int retval;
154162306a36Sopenharmony_ci
154262306a36Sopenharmony_ci	retval = filp_flush(filp, id);
154362306a36Sopenharmony_ci	fput(filp);
154462306a36Sopenharmony_ci
154562306a36Sopenharmony_ci	return retval;
154662306a36Sopenharmony_ci}
154762306a36Sopenharmony_ciEXPORT_SYMBOL(filp_close);
154862306a36Sopenharmony_ci
154962306a36Sopenharmony_ci/*
155062306a36Sopenharmony_ci * Careful here! We test whether the file pointer is NULL before
155162306a36Sopenharmony_ci * releasing the fd. This ensures that one clone task can't release
155262306a36Sopenharmony_ci * an fd while another clone is opening it.
155362306a36Sopenharmony_ci */
155462306a36Sopenharmony_ciSYSCALL_DEFINE1(close, unsigned int, fd)
155562306a36Sopenharmony_ci{
155662306a36Sopenharmony_ci	int retval;
155762306a36Sopenharmony_ci	struct file *file;
155862306a36Sopenharmony_ci
155962306a36Sopenharmony_ci	file = close_fd_get_file(fd);
156062306a36Sopenharmony_ci	if (!file)
156162306a36Sopenharmony_ci		return -EBADF;
156262306a36Sopenharmony_ci
156362306a36Sopenharmony_ci	retval = filp_flush(file, current->files);
156462306a36Sopenharmony_ci
156562306a36Sopenharmony_ci	/*
156662306a36Sopenharmony_ci	 * We're returning to user space. Don't bother
156762306a36Sopenharmony_ci	 * with any delayed fput() cases.
156862306a36Sopenharmony_ci	 */
156962306a36Sopenharmony_ci	__fput_sync(file);
157062306a36Sopenharmony_ci
157162306a36Sopenharmony_ci	/* can't restart close syscall because file table entry was cleared */
157262306a36Sopenharmony_ci	if (unlikely(retval == -ERESTARTSYS ||
157362306a36Sopenharmony_ci		     retval == -ERESTARTNOINTR ||
157462306a36Sopenharmony_ci		     retval == -ERESTARTNOHAND ||
157562306a36Sopenharmony_ci		     retval == -ERESTART_RESTARTBLOCK))
157662306a36Sopenharmony_ci		retval = -EINTR;
157762306a36Sopenharmony_ci
157862306a36Sopenharmony_ci	return retval;
157962306a36Sopenharmony_ci}
158062306a36Sopenharmony_ci
158162306a36Sopenharmony_ci/**
158262306a36Sopenharmony_ci * sys_close_range() - Close all file descriptors in a given range.
158362306a36Sopenharmony_ci *
158462306a36Sopenharmony_ci * @fd:     starting file descriptor to close
158562306a36Sopenharmony_ci * @max_fd: last file descriptor to close
158662306a36Sopenharmony_ci * @flags:  reserved for future extensions
158762306a36Sopenharmony_ci *
158862306a36Sopenharmony_ci * This closes a range of file descriptors. All file descriptors
158962306a36Sopenharmony_ci * from @fd up to and including @max_fd are closed.
159062306a36Sopenharmony_ci * Currently, errors to close a given file descriptor are ignored.
159162306a36Sopenharmony_ci */
159262306a36Sopenharmony_ciSYSCALL_DEFINE3(close_range, unsigned int, fd, unsigned int, max_fd,
159362306a36Sopenharmony_ci		unsigned int, flags)
159462306a36Sopenharmony_ci{
159562306a36Sopenharmony_ci	return __close_range(fd, max_fd, flags);
159662306a36Sopenharmony_ci}
159762306a36Sopenharmony_ci
159862306a36Sopenharmony_ci/*
159962306a36Sopenharmony_ci * This routine simulates a hangup on the tty, to arrange that users
160062306a36Sopenharmony_ci * are given clean terminals at login time.
160162306a36Sopenharmony_ci */
160262306a36Sopenharmony_ciSYSCALL_DEFINE0(vhangup)
160362306a36Sopenharmony_ci{
160462306a36Sopenharmony_ci	if (capable(CAP_SYS_TTY_CONFIG)) {
160562306a36Sopenharmony_ci		tty_vhangup_self();
160662306a36Sopenharmony_ci		return 0;
160762306a36Sopenharmony_ci	}
160862306a36Sopenharmony_ci	return -EPERM;
160962306a36Sopenharmony_ci}
161062306a36Sopenharmony_ci
161162306a36Sopenharmony_ci/*
161262306a36Sopenharmony_ci * Called when an inode is about to be open.
161362306a36Sopenharmony_ci * We use this to disallow opening large files on 32bit systems if
161462306a36Sopenharmony_ci * the caller didn't specify O_LARGEFILE.  On 64bit systems we force
161562306a36Sopenharmony_ci * on this flag in sys_open.
161662306a36Sopenharmony_ci */
161762306a36Sopenharmony_ciint generic_file_open(struct inode * inode, struct file * filp)
161862306a36Sopenharmony_ci{
161962306a36Sopenharmony_ci	if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
162062306a36Sopenharmony_ci		return -EOVERFLOW;
162162306a36Sopenharmony_ci	return 0;
162262306a36Sopenharmony_ci}
162362306a36Sopenharmony_ci
162462306a36Sopenharmony_ciEXPORT_SYMBOL(generic_file_open);
162562306a36Sopenharmony_ci
162662306a36Sopenharmony_ci/*
162762306a36Sopenharmony_ci * This is used by subsystems that don't want seekable
162862306a36Sopenharmony_ci * file descriptors. The function is not supposed to ever fail, the only
162962306a36Sopenharmony_ci * reason it returns an 'int' and not 'void' is so that it can be plugged
163062306a36Sopenharmony_ci * directly into file_operations structure.
163162306a36Sopenharmony_ci */
163262306a36Sopenharmony_ciint nonseekable_open(struct inode *inode, struct file *filp)
163362306a36Sopenharmony_ci{
163462306a36Sopenharmony_ci	filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
163562306a36Sopenharmony_ci	return 0;
163662306a36Sopenharmony_ci}
163762306a36Sopenharmony_ci
163862306a36Sopenharmony_ciEXPORT_SYMBOL(nonseekable_open);
163962306a36Sopenharmony_ci
164062306a36Sopenharmony_ci/*
164162306a36Sopenharmony_ci * stream_open is used by subsystems that want stream-like file descriptors.
164262306a36Sopenharmony_ci * Such file descriptors are not seekable and don't have notion of position
164362306a36Sopenharmony_ci * (file.f_pos is always 0 and ppos passed to .read()/.write() is always NULL).
164462306a36Sopenharmony_ci * Contrary to file descriptors of other regular files, .read() and .write()
164562306a36Sopenharmony_ci * can run simultaneously.
164662306a36Sopenharmony_ci *
164762306a36Sopenharmony_ci * stream_open never fails and is marked to return int so that it could be
164862306a36Sopenharmony_ci * directly used as file_operations.open .
164962306a36Sopenharmony_ci */
165062306a36Sopenharmony_ciint stream_open(struct inode *inode, struct file *filp)
165162306a36Sopenharmony_ci{
165262306a36Sopenharmony_ci	filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE | FMODE_ATOMIC_POS);
165362306a36Sopenharmony_ci	filp->f_mode |= FMODE_STREAM;
165462306a36Sopenharmony_ci	return 0;
165562306a36Sopenharmony_ci}
165662306a36Sopenharmony_ci
165762306a36Sopenharmony_ciEXPORT_SYMBOL(stream_open);
1658