Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/fs/stat.c |
| 4 | * |
| 5 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 6 | */ |
| 7 | |
Paul Gortmaker | 630d9c4 | 2011-11-16 23:57:37 -0500 | [diff] [blame] | 8 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/mm.h> |
| 10 | #include <linux/errno.h> |
| 11 | #include <linux/file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/highuid.h> |
| 13 | #include <linux/fs.h> |
| 14 | #include <linux/namei.h> |
| 15 | #include <linux/security.h> |
Ingo Molnar | 5b825c3 | 2017-02-02 17:54:15 +0100 | [diff] [blame] | 16 | #include <linux/cred.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/syscalls.h> |
Theodore Ts'o | ba52de1 | 2006-09-27 01:50:49 -0700 | [diff] [blame] | 18 | #include <linux/pagemap.h> |
Al Viro | ac565de | 2017-04-08 18:13:00 -0400 | [diff] [blame] | 19 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 21 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <asm/unistd.h> |
| 23 | |
Jens Axboe | 3934e36 | 2019-12-14 13:26:33 -0700 | [diff] [blame] | 24 | #include "internal.h" |
| 25 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 26 | /** |
| 27 | * generic_fillattr - Fill in the basic attributes from the inode struct |
| 28 | * @inode: Inode to use as the source |
| 29 | * @stat: Where to fill in the attributes |
| 30 | * |
| 31 | * Fill in the basic attributes in the kstat structure from data that's to be |
| 32 | * found on the VFS inode structure. This is the default if no getattr inode |
| 33 | * operation is supplied. |
| 34 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | void generic_fillattr(struct inode *inode, struct kstat *stat) |
| 36 | { |
| 37 | stat->dev = inode->i_sb->s_dev; |
| 38 | stat->ino = inode->i_ino; |
| 39 | stat->mode = inode->i_mode; |
| 40 | stat->nlink = inode->i_nlink; |
| 41 | stat->uid = inode->i_uid; |
| 42 | stat->gid = inode->i_gid; |
| 43 | stat->rdev = inode->i_rdev; |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 44 | stat->size = i_size_read(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | stat->atime = inode->i_atime; |
| 46 | stat->mtime = inode->i_mtime; |
| 47 | stat->ctime = inode->i_ctime; |
Fabian Frederick | 9340747 | 2017-02-27 14:28:32 -0800 | [diff] [blame] | 48 | stat->blksize = i_blocksize(inode); |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 49 | stat->blocks = inode->i_blocks; |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 50 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | EXPORT_SYMBOL(generic_fillattr); |
| 52 | |
J. Bruce Fields | b7a6ec5 | 2013-10-02 17:01:18 -0400 | [diff] [blame] | 53 | /** |
| 54 | * vfs_getattr_nosec - getattr without security checks |
| 55 | * @path: file to get attributes from |
| 56 | * @stat: structure to return attributes in |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 57 | * @request_mask: STATX_xxx flags indicating what the caller wants |
| 58 | * @query_flags: Query mode (KSTAT_QUERY_FLAGS) |
J. Bruce Fields | b7a6ec5 | 2013-10-02 17:01:18 -0400 | [diff] [blame] | 59 | * |
| 60 | * Get attributes without calling security_inode_getattr. |
| 61 | * |
| 62 | * Currently the only caller other than vfs_getattr is internal to the |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 63 | * filehandle lookup code, which uses only the inode number and returns no |
| 64 | * attributes to any user. Any other code probably wants vfs_getattr. |
J. Bruce Fields | b7a6ec5 | 2013-10-02 17:01:18 -0400 | [diff] [blame] | 65 | */ |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 66 | int vfs_getattr_nosec(const struct path *path, struct kstat *stat, |
| 67 | u32 request_mask, unsigned int query_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
David Howells | bb668734 | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 69 | struct inode *inode = d_backing_inode(path->dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 71 | memset(stat, 0, sizeof(*stat)); |
| 72 | stat->result_mask |= STATX_BASIC_STATS; |
| 73 | request_mask &= STATX_ALL; |
| 74 | query_flags &= KSTAT_QUERY_FLAGS; |
Christoph Hellwig | 801e523 | 2019-01-21 16:23:26 +0100 | [diff] [blame] | 75 | |
| 76 | /* allow the fs to override these if it really wants to */ |
| 77 | if (IS_NOATIME(inode)) |
| 78 | stat->result_mask &= ~STATX_ATIME; |
| 79 | if (IS_AUTOMOUNT(inode)) |
| 80 | stat->attributes |= STATX_ATTR_AUTOMOUNT; |
| 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | if (inode->i_op->getattr) |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 83 | return inode->i_op->getattr(path, stat, request_mask, |
| 84 | query_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | generic_fillattr(inode, stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | return 0; |
| 88 | } |
J. Bruce Fields | b7a6ec5 | 2013-10-02 17:01:18 -0400 | [diff] [blame] | 89 | EXPORT_SYMBOL(vfs_getattr_nosec); |
| 90 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 91 | /* |
| 92 | * vfs_getattr - Get the enhanced basic attributes of a file |
| 93 | * @path: The file of interest |
| 94 | * @stat: Where to return the statistics |
| 95 | * @request_mask: STATX_xxx flags indicating what the caller wants |
| 96 | * @query_flags: Query mode (KSTAT_QUERY_FLAGS) |
| 97 | * |
| 98 | * Ask the filesystem for a file's attributes. The caller must indicate in |
| 99 | * request_mask and query_flags to indicate what they want. |
| 100 | * |
| 101 | * If the file is remote, the filesystem can be forced to update the attributes |
| 102 | * from the backing store by passing AT_STATX_FORCE_SYNC in query_flags or can |
| 103 | * suppress the update by passing AT_STATX_DONT_SYNC. |
| 104 | * |
| 105 | * Bits must have been set in request_mask to indicate which attributes the |
| 106 | * caller wants retrieving. Any such attribute not requested may be returned |
| 107 | * anyway, but the value may be approximate, and, if remote, may not have been |
| 108 | * synchronised with the server. |
| 109 | * |
| 110 | * 0 will be returned on success, and a -ve error code if unsuccessful. |
| 111 | */ |
| 112 | int vfs_getattr(const struct path *path, struct kstat *stat, |
| 113 | u32 request_mask, unsigned int query_flags) |
J. Bruce Fields | b7a6ec5 | 2013-10-02 17:01:18 -0400 | [diff] [blame] | 114 | { |
| 115 | int retval; |
| 116 | |
Al Viro | 3f7036a | 2015-03-08 19:28:30 -0400 | [diff] [blame] | 117 | retval = security_inode_getattr(path); |
J. Bruce Fields | b7a6ec5 | 2013-10-02 17:01:18 -0400 | [diff] [blame] | 118 | if (retval) |
| 119 | return retval; |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 120 | return vfs_getattr_nosec(path, stat, request_mask, query_flags); |
J. Bruce Fields | b7a6ec5 | 2013-10-02 17:01:18 -0400 | [diff] [blame] | 121 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | EXPORT_SYMBOL(vfs_getattr); |
| 123 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 124 | /** |
| 125 | * vfs_statx_fd - Get the enhanced basic attributes by file descriptor |
| 126 | * @fd: The file descriptor referring to the file of interest |
| 127 | * @stat: The result structure to fill in. |
| 128 | * @request_mask: STATX_xxx flags indicating what the caller wants |
| 129 | * @query_flags: Query mode (KSTAT_QUERY_FLAGS) |
| 130 | * |
| 131 | * This function is a wrapper around vfs_getattr(). The main difference is |
| 132 | * that it uses a file descriptor to determine the file location. |
| 133 | * |
| 134 | * 0 will be returned on success, and a -ve error code if unsuccessful. |
| 135 | */ |
| 136 | int vfs_statx_fd(unsigned int fd, struct kstat *stat, |
| 137 | u32 request_mask, unsigned int query_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
Eric Biggers | 8c7493a | 2017-03-31 18:31:32 +0100 | [diff] [blame] | 139 | struct fd f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | int error = -EBADF; |
| 141 | |
Eric Biggers | 8c7493a | 2017-03-31 18:31:32 +0100 | [diff] [blame] | 142 | if (query_flags & ~KSTAT_QUERY_FLAGS) |
| 143 | return -EINVAL; |
| 144 | |
| 145 | f = fdget_raw(fd); |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 146 | if (f.file) { |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 147 | error = vfs_getattr(&f.file->f_path, stat, |
| 148 | request_mask, query_flags); |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 149 | fdput(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
| 151 | return error; |
| 152 | } |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 153 | EXPORT_SYMBOL(vfs_statx_fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
Jens Axboe | 3934e36 | 2019-12-14 13:26:33 -0700 | [diff] [blame] | 155 | inline unsigned vfs_stat_set_lookup_flags(unsigned *lookup_flags, int flags) |
| 156 | { |
| 157 | if ((flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT | |
| 158 | AT_EMPTY_PATH | KSTAT_QUERY_FLAGS)) != 0) |
| 159 | return -EINVAL; |
| 160 | |
| 161 | *lookup_flags = LOOKUP_FOLLOW | LOOKUP_AUTOMOUNT; |
| 162 | if (flags & AT_SYMLINK_NOFOLLOW) |
| 163 | *lookup_flags &= ~LOOKUP_FOLLOW; |
| 164 | if (flags & AT_NO_AUTOMOUNT) |
| 165 | *lookup_flags &= ~LOOKUP_AUTOMOUNT; |
| 166 | if (flags & AT_EMPTY_PATH) |
| 167 | *lookup_flags |= LOOKUP_EMPTY; |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 172 | /** |
| 173 | * vfs_statx - Get basic and extra attributes by filename |
| 174 | * @dfd: A file descriptor representing the base dir for a relative filename |
| 175 | * @filename: The name of the file of interest |
| 176 | * @flags: Flags to control the query |
| 177 | * @stat: The result structure to fill in. |
| 178 | * @request_mask: STATX_xxx flags indicating what the caller wants |
| 179 | * |
| 180 | * This function is a wrapper around vfs_getattr(). The main difference is |
| 181 | * that it uses a filename and base directory to determine the file location. |
| 182 | * Additionally, the use of AT_SYMLINK_NOFOLLOW in flags will prevent a symlink |
| 183 | * at the given name from being referenced. |
| 184 | * |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 185 | * 0 will be returned on success, and a -ve error code if unsuccessful. |
| 186 | */ |
| 187 | int vfs_statx(int dfd, const char __user *filename, int flags, |
| 188 | struct kstat *stat, u32 request_mask) |
Oleg Drokin | 0112fc2 | 2009-04-08 20:05:42 +0400 | [diff] [blame] | 189 | { |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 190 | struct path path; |
Oleg Drokin | 0112fc2 | 2009-04-08 20:05:42 +0400 | [diff] [blame] | 191 | int error = -EINVAL; |
Jens Axboe | 3934e36 | 2019-12-14 13:26:33 -0700 | [diff] [blame] | 192 | unsigned lookup_flags; |
Oleg Drokin | 0112fc2 | 2009-04-08 20:05:42 +0400 | [diff] [blame] | 193 | |
Jens Axboe | 3934e36 | 2019-12-14 13:26:33 -0700 | [diff] [blame] | 194 | if (vfs_stat_set_lookup_flags(&lookup_flags, flags)) |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 195 | return -EINVAL; |
Jeff Layton | 836fb7e | 2012-12-11 12:10:05 -0500 | [diff] [blame] | 196 | retry: |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 197 | error = user_path_at(dfd, filename, lookup_flags, &path); |
| 198 | if (error) |
| 199 | goto out; |
| 200 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 201 | error = vfs_getattr(&path, stat, request_mask, flags); |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 202 | path_put(&path); |
Jeff Layton | 836fb7e | 2012-12-11 12:10:05 -0500 | [diff] [blame] | 203 | if (retry_estale(error, lookup_flags)) { |
| 204 | lookup_flags |= LOOKUP_REVAL; |
| 205 | goto retry; |
| 206 | } |
Oleg Drokin | 0112fc2 | 2009-04-08 20:05:42 +0400 | [diff] [blame] | 207 | out: |
| 208 | return error; |
| 209 | } |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 210 | EXPORT_SYMBOL(vfs_statx); |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 211 | |
Oleg Drokin | 0112fc2 | 2009-04-08 20:05:42 +0400 | [diff] [blame] | 212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | #ifdef __ARCH_WANT_OLD_STAT |
| 214 | |
| 215 | /* |
| 216 | * For backward compatibility? Maybe this should be moved |
| 217 | * into arch/i386 instead? |
| 218 | */ |
| 219 | static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf) |
| 220 | { |
| 221 | static int warncount = 5; |
| 222 | struct __old_kernel_stat tmp; |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | if (warncount > 0) { |
| 225 | warncount--; |
| 226 | printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n", |
| 227 | current->comm); |
| 228 | } else if (warncount < 0) { |
| 229 | /* it's laughable, but... */ |
| 230 | warncount = 0; |
| 231 | } |
| 232 | |
| 233 | memset(&tmp, 0, sizeof(struct __old_kernel_stat)); |
| 234 | tmp.st_dev = old_encode_dev(stat->dev); |
| 235 | tmp.st_ino = stat->ino; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 236 | if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino) |
| 237 | return -EOVERFLOW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | tmp.st_mode = stat->mode; |
| 239 | tmp.st_nlink = stat->nlink; |
| 240 | if (tmp.st_nlink != stat->nlink) |
| 241 | return -EOVERFLOW; |
Eric W. Biederman | a7c1938 | 2012-02-09 09:10:30 -0800 | [diff] [blame] | 242 | SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid)); |
| 243 | SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | tmp.st_rdev = old_encode_dev(stat->rdev); |
| 245 | #if BITS_PER_LONG == 32 |
| 246 | if (stat->size > MAX_NON_LFS) |
| 247 | return -EOVERFLOW; |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 248 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | tmp.st_size = stat->size; |
| 250 | tmp.st_atime = stat->atime.tv_sec; |
| 251 | tmp.st_mtime = stat->mtime.tv_sec; |
| 252 | tmp.st_ctime = stat->ctime.tv_sec; |
| 253 | return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0; |
| 254 | } |
| 255 | |
David Howells | c788732 | 2010-08-11 11:26:22 +0100 | [diff] [blame] | 256 | SYSCALL_DEFINE2(stat, const char __user *, filename, |
| 257 | struct __old_kernel_stat __user *, statbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | { |
| 259 | struct kstat stat; |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 260 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 262 | error = vfs_stat(filename, &stat); |
| 263 | if (error) |
| 264 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 266 | return cp_old_stat(&stat, statbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | } |
Heiko Carstens | 257ac26 | 2009-01-14 14:14:13 +0100 | [diff] [blame] | 268 | |
David Howells | c788732 | 2010-08-11 11:26:22 +0100 | [diff] [blame] | 269 | SYSCALL_DEFINE2(lstat, const char __user *, filename, |
| 270 | struct __old_kernel_stat __user *, statbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | { |
| 272 | struct kstat stat; |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 273 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 275 | error = vfs_lstat(filename, &stat); |
| 276 | if (error) |
| 277 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 279 | return cp_old_stat(&stat, statbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | } |
Heiko Carstens | 257ac26 | 2009-01-14 14:14:13 +0100 | [diff] [blame] | 281 | |
| 282 | SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | { |
| 284 | struct kstat stat; |
| 285 | int error = vfs_fstat(fd, &stat); |
| 286 | |
| 287 | if (!error) |
| 288 | error = cp_old_stat(&stat, statbuf); |
| 289 | |
| 290 | return error; |
| 291 | } |
| 292 | |
| 293 | #endif /* __ARCH_WANT_OLD_STAT */ |
| 294 | |
Arnd Bergmann | 82b355d | 2018-04-13 11:50:12 +0200 | [diff] [blame] | 295 | #ifdef __ARCH_WANT_NEW_STAT |
| 296 | |
Linus Torvalds | a52dd97 | 2012-05-06 17:47:30 -0700 | [diff] [blame] | 297 | #if BITS_PER_LONG == 32 |
| 298 | # define choose_32_64(a,b) a |
| 299 | #else |
| 300 | # define choose_32_64(a,b) b |
| 301 | #endif |
| 302 | |
Yaowei Bai | 4c416f4 | 2016-01-15 16:58:01 -0800 | [diff] [blame] | 303 | #define valid_dev(x) choose_32_64(old_valid_dev(x),true) |
Linus Torvalds | a52dd97 | 2012-05-06 17:47:30 -0700 | [diff] [blame] | 304 | #define encode_dev(x) choose_32_64(old_encode_dev,new_encode_dev)(x) |
| 305 | |
Linus Torvalds | 8529f61 | 2012-05-06 18:02:40 -0700 | [diff] [blame] | 306 | #ifndef INIT_STRUCT_STAT_PADDING |
| 307 | # define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st)) |
| 308 | #endif |
| 309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf) |
| 311 | { |
| 312 | struct stat tmp; |
| 313 | |
Linus Torvalds | a52dd97 | 2012-05-06 17:47:30 -0700 | [diff] [blame] | 314 | if (!valid_dev(stat->dev) || !valid_dev(stat->rdev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | return -EOVERFLOW; |
Linus Torvalds | a52dd97 | 2012-05-06 17:47:30 -0700 | [diff] [blame] | 316 | #if BITS_PER_LONG == 32 |
| 317 | if (stat->size > MAX_NON_LFS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | return -EOVERFLOW; |
| 319 | #endif |
| 320 | |
Linus Torvalds | 8529f61 | 2012-05-06 18:02:40 -0700 | [diff] [blame] | 321 | INIT_STRUCT_STAT_PADDING(tmp); |
Linus Torvalds | a52dd97 | 2012-05-06 17:47:30 -0700 | [diff] [blame] | 322 | tmp.st_dev = encode_dev(stat->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | tmp.st_ino = stat->ino; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 324 | if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino) |
| 325 | return -EOVERFLOW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | tmp.st_mode = stat->mode; |
| 327 | tmp.st_nlink = stat->nlink; |
| 328 | if (tmp.st_nlink != stat->nlink) |
| 329 | return -EOVERFLOW; |
Eric W. Biederman | a7c1938 | 2012-02-09 09:10:30 -0800 | [diff] [blame] | 330 | SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid)); |
| 331 | SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid)); |
Linus Torvalds | a52dd97 | 2012-05-06 17:47:30 -0700 | [diff] [blame] | 332 | tmp.st_rdev = encode_dev(stat->rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | tmp.st_size = stat->size; |
| 334 | tmp.st_atime = stat->atime.tv_sec; |
| 335 | tmp.st_mtime = stat->mtime.tv_sec; |
| 336 | tmp.st_ctime = stat->ctime.tv_sec; |
| 337 | #ifdef STAT_HAVE_NSEC |
| 338 | tmp.st_atime_nsec = stat->atime.tv_nsec; |
| 339 | tmp.st_mtime_nsec = stat->mtime.tv_nsec; |
| 340 | tmp.st_ctime_nsec = stat->ctime.tv_nsec; |
| 341 | #endif |
| 342 | tmp.st_blocks = stat->blocks; |
| 343 | tmp.st_blksize = stat->blksize; |
| 344 | return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0; |
| 345 | } |
| 346 | |
David Howells | c788732 | 2010-08-11 11:26:22 +0100 | [diff] [blame] | 347 | SYSCALL_DEFINE2(newstat, const char __user *, filename, |
| 348 | struct stat __user *, statbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | { |
| 350 | struct kstat stat; |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 351 | int error = vfs_stat(filename, &stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 353 | if (error) |
| 354 | return error; |
| 355 | return cp_new_stat(&stat, statbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | } |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 357 | |
David Howells | c788732 | 2010-08-11 11:26:22 +0100 | [diff] [blame] | 358 | SYSCALL_DEFINE2(newlstat, const char __user *, filename, |
| 359 | struct stat __user *, statbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | { |
| 361 | struct kstat stat; |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 362 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 364 | error = vfs_lstat(filename, &stat); |
| 365 | if (error) |
| 366 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 368 | return cp_new_stat(&stat, statbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | } |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 370 | |
Andreas Schwab | 2833c28 | 2006-04-27 15:46:42 +0200 | [diff] [blame] | 371 | #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT) |
David Howells | c788732 | 2010-08-11 11:26:22 +0100 | [diff] [blame] | 372 | SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename, |
Heiko Carstens | 6559eed8 | 2009-01-14 14:14:32 +0100 | [diff] [blame] | 373 | struct stat __user *, statbuf, int, flag) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 374 | { |
| 375 | struct kstat stat; |
Oleg Drokin | 0112fc2 | 2009-04-08 20:05:42 +0400 | [diff] [blame] | 376 | int error; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 377 | |
Oleg Drokin | 0112fc2 | 2009-04-08 20:05:42 +0400 | [diff] [blame] | 378 | error = vfs_fstatat(dfd, filename, &stat, flag); |
| 379 | if (error) |
| 380 | return error; |
| 381 | return cp_new_stat(&stat, statbuf); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 382 | } |
Ulrich Drepper | cff2b76 | 2006-02-11 17:55:47 -0800 | [diff] [blame] | 383 | #endif |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 384 | |
Heiko Carstens | 257ac26 | 2009-01-14 14:14:13 +0100 | [diff] [blame] | 385 | SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | { |
| 387 | struct kstat stat; |
| 388 | int error = vfs_fstat(fd, &stat); |
| 389 | |
| 390 | if (!error) |
| 391 | error = cp_new_stat(&stat, statbuf); |
| 392 | |
| 393 | return error; |
| 394 | } |
Arnd Bergmann | 82b355d | 2018-04-13 11:50:12 +0200 | [diff] [blame] | 395 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
Dominik Brodowski | 2dae024 | 2018-03-11 11:34:27 +0100 | [diff] [blame] | 397 | static int do_readlinkat(int dfd, const char __user *pathname, |
| 398 | char __user *buf, int bufsiz) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 400 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | int error; |
Andy Whitcroft | 1fa1e7f | 2011-11-02 09:44:39 +0100 | [diff] [blame] | 402 | int empty = 0; |
Jeff Layton | 7955119 | 2012-12-11 12:10:06 -0500 | [diff] [blame] | 403 | unsigned int lookup_flags = LOOKUP_EMPTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
| 405 | if (bufsiz <= 0) |
| 406 | return -EINVAL; |
| 407 | |
Jeff Layton | 7955119 | 2012-12-11 12:10:06 -0500 | [diff] [blame] | 408 | retry: |
| 409 | error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | if (!error) { |
David Howells | bb668734 | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 411 | struct inode *inode = d_backing_inode(path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
Andy Whitcroft | 1fa1e7f | 2011-11-02 09:44:39 +0100 | [diff] [blame] | 413 | error = empty ? -ENOENT : -EINVAL; |
Miklos Szeredi | fd4a0edf | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 414 | /* |
| 415 | * AFS mountpoints allow readlink(2) but are not symlinks |
| 416 | */ |
| 417 | if (d_is_symlink(path.dentry) || inode->i_op->readlink) { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 418 | error = security_inode_readlink(path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | if (!error) { |
Al Viro | 68ac123 | 2012-03-15 08:21:57 -0400 | [diff] [blame] | 420 | touch_atime(&path); |
Miklos Szeredi | fd4a0edf | 2016-12-09 16:45:04 +0100 | [diff] [blame] | 421 | error = vfs_readlink(path.dentry, buf, bufsiz); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | } |
| 423 | } |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 424 | path_put(&path); |
Jeff Layton | 7955119 | 2012-12-11 12:10:06 -0500 | [diff] [blame] | 425 | if (retry_estale(error, lookup_flags)) { |
| 426 | lookup_flags |= LOOKUP_REVAL; |
| 427 | goto retry; |
| 428 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | } |
| 430 | return error; |
| 431 | } |
| 432 | |
Dominik Brodowski | 2dae024 | 2018-03-11 11:34:27 +0100 | [diff] [blame] | 433 | SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname, |
| 434 | char __user *, buf, int, bufsiz) |
| 435 | { |
| 436 | return do_readlinkat(dfd, pathname, buf, bufsiz); |
| 437 | } |
| 438 | |
Heiko Carstens | 002c897 | 2009-01-14 14:14:18 +0100 | [diff] [blame] | 439 | SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf, |
| 440 | int, bufsiz) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 441 | { |
Dominik Brodowski | 2dae024 | 2018-03-11 11:34:27 +0100 | [diff] [blame] | 442 | return do_readlinkat(AT_FDCWD, path, buf, bufsiz); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 443 | } |
| 444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
| 446 | /* ---------- LFS-64 ----------- */ |
Catalin Marinas | 0753f70 | 2012-03-19 15:13:51 +0000 | [diff] [blame] | 447 | #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | |
Linus Torvalds | 8529f61 | 2012-05-06 18:02:40 -0700 | [diff] [blame] | 449 | #ifndef INIT_STRUCT_STAT64_PADDING |
| 450 | # define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st)) |
| 451 | #endif |
| 452 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf) |
| 454 | { |
| 455 | struct stat64 tmp; |
| 456 | |
Linus Torvalds | 8529f61 | 2012-05-06 18:02:40 -0700 | [diff] [blame] | 457 | INIT_STRUCT_STAT64_PADDING(tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | #ifdef CONFIG_MIPS |
| 459 | /* mips has weird padding, so we don't get 64 bits there */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | tmp.st_dev = new_encode_dev(stat->dev); |
| 461 | tmp.st_rdev = new_encode_dev(stat->rdev); |
| 462 | #else |
| 463 | tmp.st_dev = huge_encode_dev(stat->dev); |
| 464 | tmp.st_rdev = huge_encode_dev(stat->rdev); |
| 465 | #endif |
| 466 | tmp.st_ino = stat->ino; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 467 | if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino) |
| 468 | return -EOVERFLOW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | #ifdef STAT64_HAS_BROKEN_ST_INO |
| 470 | tmp.__st_ino = stat->ino; |
| 471 | #endif |
| 472 | tmp.st_mode = stat->mode; |
| 473 | tmp.st_nlink = stat->nlink; |
Eric W. Biederman | a7c1938 | 2012-02-09 09:10:30 -0800 | [diff] [blame] | 474 | tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid); |
| 475 | tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | tmp.st_atime = stat->atime.tv_sec; |
| 477 | tmp.st_atime_nsec = stat->atime.tv_nsec; |
| 478 | tmp.st_mtime = stat->mtime.tv_sec; |
| 479 | tmp.st_mtime_nsec = stat->mtime.tv_nsec; |
| 480 | tmp.st_ctime = stat->ctime.tv_sec; |
| 481 | tmp.st_ctime_nsec = stat->ctime.tv_nsec; |
| 482 | tmp.st_size = stat->size; |
| 483 | tmp.st_blocks = stat->blocks; |
| 484 | tmp.st_blksize = stat->blksize; |
| 485 | return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0; |
| 486 | } |
| 487 | |
David Howells | c788732 | 2010-08-11 11:26:22 +0100 | [diff] [blame] | 488 | SYSCALL_DEFINE2(stat64, const char __user *, filename, |
| 489 | struct stat64 __user *, statbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | { |
| 491 | struct kstat stat; |
| 492 | int error = vfs_stat(filename, &stat); |
| 493 | |
| 494 | if (!error) |
| 495 | error = cp_new_stat64(&stat, statbuf); |
| 496 | |
| 497 | return error; |
| 498 | } |
Heiko Carstens | 257ac26 | 2009-01-14 14:14:13 +0100 | [diff] [blame] | 499 | |
David Howells | c788732 | 2010-08-11 11:26:22 +0100 | [diff] [blame] | 500 | SYSCALL_DEFINE2(lstat64, const char __user *, filename, |
| 501 | struct stat64 __user *, statbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | { |
| 503 | struct kstat stat; |
| 504 | int error = vfs_lstat(filename, &stat); |
| 505 | |
| 506 | if (!error) |
| 507 | error = cp_new_stat64(&stat, statbuf); |
| 508 | |
| 509 | return error; |
| 510 | } |
Heiko Carstens | 257ac26 | 2009-01-14 14:14:13 +0100 | [diff] [blame] | 511 | |
| 512 | SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | { |
| 514 | struct kstat stat; |
| 515 | int error = vfs_fstat(fd, &stat); |
| 516 | |
| 517 | if (!error) |
| 518 | error = cp_new_stat64(&stat, statbuf); |
| 519 | |
| 520 | return error; |
| 521 | } |
| 522 | |
David Howells | c788732 | 2010-08-11 11:26:22 +0100 | [diff] [blame] | 523 | SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename, |
Heiko Carstens | 6559eed8 | 2009-01-14 14:14:32 +0100 | [diff] [blame] | 524 | struct stat64 __user *, statbuf, int, flag) |
Ulrich Drepper | cff2b76 | 2006-02-11 17:55:47 -0800 | [diff] [blame] | 525 | { |
| 526 | struct kstat stat; |
Oleg Drokin | 0112fc2 | 2009-04-08 20:05:42 +0400 | [diff] [blame] | 527 | int error; |
Ulrich Drepper | cff2b76 | 2006-02-11 17:55:47 -0800 | [diff] [blame] | 528 | |
Oleg Drokin | 0112fc2 | 2009-04-08 20:05:42 +0400 | [diff] [blame] | 529 | error = vfs_fstatat(dfd, filename, &stat, flag); |
| 530 | if (error) |
| 531 | return error; |
| 532 | return cp_new_stat64(&stat, statbuf); |
Ulrich Drepper | cff2b76 | 2006-02-11 17:55:47 -0800 | [diff] [blame] | 533 | } |
Catalin Marinas | 0753f70 | 2012-03-19 15:13:51 +0000 | [diff] [blame] | 534 | #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
Jens Axboe | 3934e36 | 2019-12-14 13:26:33 -0700 | [diff] [blame] | 536 | noinline_for_stack int |
Eric Biggers | 64bd720 | 2017-03-31 18:31:48 +0100 | [diff] [blame] | 537 | cp_statx(const struct kstat *stat, struct statx __user *buffer) |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 538 | { |
Eric Biggers | 64bd720 | 2017-03-31 18:31:48 +0100 | [diff] [blame] | 539 | struct statx tmp; |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 540 | |
Eric Biggers | 64bd720 | 2017-03-31 18:31:48 +0100 | [diff] [blame] | 541 | memset(&tmp, 0, sizeof(tmp)); |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 542 | |
Eric Biggers | 64bd720 | 2017-03-31 18:31:48 +0100 | [diff] [blame] | 543 | tmp.stx_mask = stat->result_mask; |
| 544 | tmp.stx_blksize = stat->blksize; |
| 545 | tmp.stx_attributes = stat->attributes; |
| 546 | tmp.stx_nlink = stat->nlink; |
| 547 | tmp.stx_uid = from_kuid_munged(current_user_ns(), stat->uid); |
| 548 | tmp.stx_gid = from_kgid_munged(current_user_ns(), stat->gid); |
| 549 | tmp.stx_mode = stat->mode; |
| 550 | tmp.stx_ino = stat->ino; |
| 551 | tmp.stx_size = stat->size; |
| 552 | tmp.stx_blocks = stat->blocks; |
David Howells | 3209f68 | 2017-03-31 18:32:17 +0100 | [diff] [blame] | 553 | tmp.stx_attributes_mask = stat->attributes_mask; |
Eric Biggers | 64bd720 | 2017-03-31 18:31:48 +0100 | [diff] [blame] | 554 | tmp.stx_atime.tv_sec = stat->atime.tv_sec; |
| 555 | tmp.stx_atime.tv_nsec = stat->atime.tv_nsec; |
| 556 | tmp.stx_btime.tv_sec = stat->btime.tv_sec; |
| 557 | tmp.stx_btime.tv_nsec = stat->btime.tv_nsec; |
| 558 | tmp.stx_ctime.tv_sec = stat->ctime.tv_sec; |
| 559 | tmp.stx_ctime.tv_nsec = stat->ctime.tv_nsec; |
| 560 | tmp.stx_mtime.tv_sec = stat->mtime.tv_sec; |
| 561 | tmp.stx_mtime.tv_nsec = stat->mtime.tv_nsec; |
| 562 | tmp.stx_rdev_major = MAJOR(stat->rdev); |
| 563 | tmp.stx_rdev_minor = MINOR(stat->rdev); |
| 564 | tmp.stx_dev_major = MAJOR(stat->dev); |
| 565 | tmp.stx_dev_minor = MINOR(stat->dev); |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 566 | |
Eric Biggers | 64bd720 | 2017-03-31 18:31:48 +0100 | [diff] [blame] | 567 | return copy_to_user(buffer, &tmp, sizeof(tmp)) ? -EFAULT : 0; |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /** |
| 571 | * sys_statx - System call to get enhanced stats |
| 572 | * @dfd: Base directory to pathwalk from *or* fd to stat. |
David Howells | 1e2f82d | 2017-04-26 22:15:55 +0100 | [diff] [blame] | 573 | * @filename: File to stat or "" with AT_EMPTY_PATH |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 574 | * @flags: AT_* flags to control pathwalk. |
| 575 | * @mask: Parts of statx struct actually required. |
| 576 | * @buffer: Result buffer. |
| 577 | * |
David Howells | 1e2f82d | 2017-04-26 22:15:55 +0100 | [diff] [blame] | 578 | * Note that fstat() can be emulated by setting dfd to the fd of interest, |
| 579 | * supplying "" as the filename and setting AT_EMPTY_PATH in the flags. |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 580 | */ |
| 581 | SYSCALL_DEFINE5(statx, |
| 582 | int, dfd, const char __user *, filename, unsigned, flags, |
| 583 | unsigned int, mask, |
| 584 | struct statx __user *, buffer) |
| 585 | { |
| 586 | struct kstat stat; |
| 587 | int error; |
| 588 | |
David Howells | 47071ae | 2017-03-31 18:32:10 +0100 | [diff] [blame] | 589 | if (mask & STATX__RESERVED) |
| 590 | return -EINVAL; |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 591 | if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE) |
| 592 | return -EINVAL; |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 593 | |
David Howells | 1e2f82d | 2017-04-26 22:15:55 +0100 | [diff] [blame] | 594 | error = vfs_statx(dfd, filename, flags, &stat, mask); |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 595 | if (error) |
| 596 | return error; |
Eric Biggers | 64bd720 | 2017-03-31 18:31:48 +0100 | [diff] [blame] | 597 | |
| 598 | return cp_statx(&stat, buffer); |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Al Viro | ac565de | 2017-04-08 18:13:00 -0400 | [diff] [blame] | 601 | #ifdef CONFIG_COMPAT |
| 602 | static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf) |
| 603 | { |
| 604 | struct compat_stat tmp; |
| 605 | |
| 606 | if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev)) |
| 607 | return -EOVERFLOW; |
| 608 | |
| 609 | memset(&tmp, 0, sizeof(tmp)); |
| 610 | tmp.st_dev = old_encode_dev(stat->dev); |
| 611 | tmp.st_ino = stat->ino; |
| 612 | if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino) |
| 613 | return -EOVERFLOW; |
| 614 | tmp.st_mode = stat->mode; |
| 615 | tmp.st_nlink = stat->nlink; |
| 616 | if (tmp.st_nlink != stat->nlink) |
| 617 | return -EOVERFLOW; |
| 618 | SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid)); |
| 619 | SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid)); |
| 620 | tmp.st_rdev = old_encode_dev(stat->rdev); |
| 621 | if ((u64) stat->size > MAX_NON_LFS) |
| 622 | return -EOVERFLOW; |
| 623 | tmp.st_size = stat->size; |
| 624 | tmp.st_atime = stat->atime.tv_sec; |
| 625 | tmp.st_atime_nsec = stat->atime.tv_nsec; |
| 626 | tmp.st_mtime = stat->mtime.tv_sec; |
| 627 | tmp.st_mtime_nsec = stat->mtime.tv_nsec; |
| 628 | tmp.st_ctime = stat->ctime.tv_sec; |
| 629 | tmp.st_ctime_nsec = stat->ctime.tv_nsec; |
| 630 | tmp.st_blocks = stat->blocks; |
| 631 | tmp.st_blksize = stat->blksize; |
| 632 | return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0; |
| 633 | } |
| 634 | |
| 635 | COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename, |
| 636 | struct compat_stat __user *, statbuf) |
| 637 | { |
| 638 | struct kstat stat; |
| 639 | int error; |
| 640 | |
| 641 | error = vfs_stat(filename, &stat); |
| 642 | if (error) |
| 643 | return error; |
| 644 | return cp_compat_stat(&stat, statbuf); |
| 645 | } |
| 646 | |
| 647 | COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename, |
| 648 | struct compat_stat __user *, statbuf) |
| 649 | { |
| 650 | struct kstat stat; |
| 651 | int error; |
| 652 | |
| 653 | error = vfs_lstat(filename, &stat); |
| 654 | if (error) |
| 655 | return error; |
| 656 | return cp_compat_stat(&stat, statbuf); |
| 657 | } |
| 658 | |
| 659 | #ifndef __ARCH_WANT_STAT64 |
| 660 | COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd, |
| 661 | const char __user *, filename, |
| 662 | struct compat_stat __user *, statbuf, int, flag) |
| 663 | { |
| 664 | struct kstat stat; |
| 665 | int error; |
| 666 | |
| 667 | error = vfs_fstatat(dfd, filename, &stat, flag); |
| 668 | if (error) |
| 669 | return error; |
| 670 | return cp_compat_stat(&stat, statbuf); |
| 671 | } |
| 672 | #endif |
| 673 | |
| 674 | COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd, |
| 675 | struct compat_stat __user *, statbuf) |
| 676 | { |
| 677 | struct kstat stat; |
| 678 | int error = vfs_fstat(fd, &stat); |
| 679 | |
| 680 | if (!error) |
| 681 | error = cp_compat_stat(&stat, statbuf); |
| 682 | return error; |
| 683 | } |
| 684 | #endif |
| 685 | |
Dmitry Monakhov | b462707 | 2009-12-14 15:21:12 +0300 | [diff] [blame] | 686 | /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */ |
| 687 | void __inode_add_bytes(struct inode *inode, loff_t bytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | inode->i_blocks += bytes >> 9; |
| 690 | bytes &= 511; |
| 691 | inode->i_bytes += bytes; |
| 692 | if (inode->i_bytes >= 512) { |
| 693 | inode->i_blocks++; |
| 694 | inode->i_bytes -= 512; |
| 695 | } |
Dmitry Monakhov | b462707 | 2009-12-14 15:21:12 +0300 | [diff] [blame] | 696 | } |
Al Viro | eb315d2 | 2017-06-08 21:15:03 -0400 | [diff] [blame] | 697 | EXPORT_SYMBOL(__inode_add_bytes); |
Dmitry Monakhov | b462707 | 2009-12-14 15:21:12 +0300 | [diff] [blame] | 698 | |
| 699 | void inode_add_bytes(struct inode *inode, loff_t bytes) |
| 700 | { |
| 701 | spin_lock(&inode->i_lock); |
| 702 | __inode_add_bytes(inode, bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | spin_unlock(&inode->i_lock); |
| 704 | } |
| 705 | |
| 706 | EXPORT_SYMBOL(inode_add_bytes); |
| 707 | |
Jan Kara | 1c8924e | 2013-08-17 09:32:32 -0400 | [diff] [blame] | 708 | void __inode_sub_bytes(struct inode *inode, loff_t bytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | inode->i_blocks -= bytes >> 9; |
| 711 | bytes &= 511; |
| 712 | if (inode->i_bytes < bytes) { |
| 713 | inode->i_blocks--; |
| 714 | inode->i_bytes += 512; |
| 715 | } |
| 716 | inode->i_bytes -= bytes; |
Jan Kara | 1c8924e | 2013-08-17 09:32:32 -0400 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | EXPORT_SYMBOL(__inode_sub_bytes); |
| 720 | |
| 721 | void inode_sub_bytes(struct inode *inode, loff_t bytes) |
| 722 | { |
| 723 | spin_lock(&inode->i_lock); |
| 724 | __inode_sub_bytes(inode, bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | spin_unlock(&inode->i_lock); |
| 726 | } |
| 727 | |
| 728 | EXPORT_SYMBOL(inode_sub_bytes); |
| 729 | |
| 730 | loff_t inode_get_bytes(struct inode *inode) |
| 731 | { |
| 732 | loff_t ret; |
| 733 | |
| 734 | spin_lock(&inode->i_lock); |
Jan Kara | f4a8116a | 2017-08-08 09:54:36 +0200 | [diff] [blame] | 735 | ret = __inode_get_bytes(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | spin_unlock(&inode->i_lock); |
| 737 | return ret; |
| 738 | } |
| 739 | |
| 740 | EXPORT_SYMBOL(inode_get_bytes); |
| 741 | |
| 742 | void inode_set_bytes(struct inode *inode, loff_t bytes) |
| 743 | { |
| 744 | /* Caller is here responsible for sufficient locking |
| 745 | * (ie. inode->i_lock) */ |
| 746 | inode->i_blocks = bytes >> 9; |
| 747 | inode->i_bytes = bytes & 511; |
| 748 | } |
| 749 | |
| 750 | EXPORT_SYMBOL(inode_set_bytes); |