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