blob: 030008796479aa135964995e4fe8a1fad91ede37 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/stat.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
Paul Gortmaker630d9c42011-11-16 23:57:37 -05008#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/mm.h>
10#include <linux/errno.h>
11#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/highuid.h>
13#include <linux/fs.h>
14#include <linux/namei.h>
15#include <linux/security.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010016#include <linux/cred.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/syscalls.h>
Theodore Ts'oba52de12006-09-27 01:50:49 -070018#include <linux/pagemap.h>
Al Viroac565de2017-04-08 18:13:00 -040019#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080021#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/unistd.h>
23
Jens Axboe3934e362019-12-14 13:26:33 -070024#include "internal.h"
25
David Howellsa528d352017-01-31 16:46:22 +000026/**
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 Torvalds1da177e2005-04-16 15:20:36 -070035void 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 Torvalds3ddcd052011-08-06 22:45:50 -070044 stat->size = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 stat->atime = inode->i_atime;
46 stat->mtime = inode->i_mtime;
47 stat->ctime = inode->i_ctime;
Fabian Frederick93407472017-02-27 14:28:32 -080048 stat->blksize = i_blocksize(inode);
Linus Torvalds3ddcd052011-08-06 22:45:50 -070049 stat->blocks = inode->i_blocks;
David Howellsa528d352017-01-31 16:46:22 +000050}
Linus Torvalds1da177e2005-04-16 15:20:36 -070051EXPORT_SYMBOL(generic_fillattr);
52
J. Bruce Fieldsb7a6ec52013-10-02 17:01:18 -040053/**
54 * vfs_getattr_nosec - getattr without security checks
55 * @path: file to get attributes from
56 * @stat: structure to return attributes in
David Howellsa528d352017-01-31 16:46:22 +000057 * @request_mask: STATX_xxx flags indicating what the caller wants
58 * @query_flags: Query mode (KSTAT_QUERY_FLAGS)
J. Bruce Fieldsb7a6ec52013-10-02 17:01:18 -040059 *
60 * Get attributes without calling security_inode_getattr.
61 *
62 * Currently the only caller other than vfs_getattr is internal to the
David Howellsa528d352017-01-31 16:46:22 +000063 * 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 Fieldsb7a6ec52013-10-02 17:01:18 -040065 */
David Howellsa528d352017-01-31 16:46:22 +000066int vfs_getattr_nosec(const struct path *path, struct kstat *stat,
67 u32 request_mask, unsigned int query_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
David Howellsbb6687342015-03-17 22:26:21 +000069 struct inode *inode = d_backing_inode(path->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
David Howellsa528d352017-01-31 16:46:22 +000071 memset(stat, 0, sizeof(*stat));
72 stat->result_mask |= STATX_BASIC_STATS;
73 request_mask &= STATX_ALL;
74 query_flags &= KSTAT_QUERY_FLAGS;
Christoph Hellwig801e5232019-01-21 16:23:26 +010075
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 Torvalds1da177e2005-04-16 15:20:36 -070082 if (inode->i_op->getattr)
David Howellsa528d352017-01-31 16:46:22 +000083 return inode->i_op->getattr(path, stat, request_mask,
84 query_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 generic_fillattr(inode, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return 0;
88}
J. Bruce Fieldsb7a6ec52013-10-02 17:01:18 -040089EXPORT_SYMBOL(vfs_getattr_nosec);
90
David Howellsa528d352017-01-31 16:46:22 +000091/*
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 */
112int vfs_getattr(const struct path *path, struct kstat *stat,
113 u32 request_mask, unsigned int query_flags)
J. Bruce Fieldsb7a6ec52013-10-02 17:01:18 -0400114{
115 int retval;
116
Al Viro3f7036a2015-03-08 19:28:30 -0400117 retval = security_inode_getattr(path);
J. Bruce Fieldsb7a6ec52013-10-02 17:01:18 -0400118 if (retval)
119 return retval;
David Howellsa528d352017-01-31 16:46:22 +0000120 return vfs_getattr_nosec(path, stat, request_mask, query_flags);
J. Bruce Fieldsb7a6ec52013-10-02 17:01:18 -0400121}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122EXPORT_SYMBOL(vfs_getattr);
123
David Howellsa528d352017-01-31 16:46:22 +0000124/**
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 */
136int vfs_statx_fd(unsigned int fd, struct kstat *stat,
137 u32 request_mask, unsigned int query_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Eric Biggers8c7493a2017-03-31 18:31:32 +0100139 struct fd f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 int error = -EBADF;
141
Eric Biggers8c7493a2017-03-31 18:31:32 +0100142 if (query_flags & ~KSTAT_QUERY_FLAGS)
143 return -EINVAL;
144
145 f = fdget_raw(fd);
Al Viro2903ff02012-08-28 12:52:22 -0400146 if (f.file) {
David Howellsa528d352017-01-31 16:46:22 +0000147 error = vfs_getattr(&f.file->f_path, stat,
148 request_mask, query_flags);
Al Viro2903ff02012-08-28 12:52:22 -0400149 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151 return error;
152}
David Howellsa528d352017-01-31 16:46:22 +0000153EXPORT_SYMBOL(vfs_statx_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Jens Axboe3934e362019-12-14 13:26:33 -0700155inline 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 Howellsa528d352017-01-31 16:46:22 +0000172/**
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 Howellsa528d352017-01-31 16:46:22 +0000185 * 0 will be returned on success, and a -ve error code if unsuccessful.
186 */
187int vfs_statx(int dfd, const char __user *filename, int flags,
188 struct kstat *stat, u32 request_mask)
Oleg Drokin0112fc22009-04-08 20:05:42 +0400189{
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400190 struct path path;
Oleg Drokin0112fc22009-04-08 20:05:42 +0400191 int error = -EINVAL;
Jens Axboe3934e362019-12-14 13:26:33 -0700192 unsigned lookup_flags;
Oleg Drokin0112fc22009-04-08 20:05:42 +0400193
Jens Axboe3934e362019-12-14 13:26:33 -0700194 if (vfs_stat_set_lookup_flags(&lookup_flags, flags))
David Howellsa528d352017-01-31 16:46:22 +0000195 return -EINVAL;
Jeff Layton836fb7e2012-12-11 12:10:05 -0500196retry:
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400197 error = user_path_at(dfd, filename, lookup_flags, &path);
198 if (error)
199 goto out;
200
David Howellsa528d352017-01-31 16:46:22 +0000201 error = vfs_getattr(&path, stat, request_mask, flags);
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400202 path_put(&path);
Jeff Layton836fb7e2012-12-11 12:10:05 -0500203 if (retry_estale(error, lookup_flags)) {
204 lookup_flags |= LOOKUP_REVAL;
205 goto retry;
206 }
Oleg Drokin0112fc22009-04-08 20:05:42 +0400207out:
208 return error;
209}
David Howellsa528d352017-01-31 16:46:22 +0000210EXPORT_SYMBOL(vfs_statx);
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400211
Oleg Drokin0112fc22009-04-08 20:05:42 +0400212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213#ifdef __ARCH_WANT_OLD_STAT
214
215/*
216 * For backward compatibility? Maybe this should be moved
217 * into arch/i386 instead?
218 */
219static 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 Howellsa528d352017-01-31 16:46:22 +0000223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 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 Howellsafefdbb2006-10-03 01:13:46 -0700236 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
237 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 tmp.st_mode = stat->mode;
239 tmp.st_nlink = stat->nlink;
240 if (tmp.st_nlink != stat->nlink)
241 return -EOVERFLOW;
Eric W. Biedermana7c19382012-02-09 09:10:30 -0800242 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 Torvalds1da177e2005-04-16 15:20:36 -0700244 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 Howellsa528d352017-01-31 16:46:22 +0000248#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 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 Howellsc7887322010-08-11 11:26:22 +0100256SYSCALL_DEFINE2(stat, const char __user *, filename,
257 struct __old_kernel_stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 struct kstat stat;
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400260 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400262 error = vfs_stat(filename, &stat);
263 if (error)
264 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400266 return cp_old_stat(&stat, statbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
Heiko Carstens257ac262009-01-14 14:14:13 +0100268
David Howellsc7887322010-08-11 11:26:22 +0100269SYSCALL_DEFINE2(lstat, const char __user *, filename,
270 struct __old_kernel_stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 struct kstat stat;
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400273 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400275 error = vfs_lstat(filename, &stat);
276 if (error)
277 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400279 return cp_old_stat(&stat, statbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
Heiko Carstens257ac262009-01-14 14:14:13 +0100281
282SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
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 Bergmann82b355d2018-04-13 11:50:12 +0200295#ifdef __ARCH_WANT_NEW_STAT
296
Linus Torvaldsa52dd972012-05-06 17:47:30 -0700297#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 Bai4c416f42016-01-15 16:58:01 -0800303#define valid_dev(x) choose_32_64(old_valid_dev(x),true)
Linus Torvaldsa52dd972012-05-06 17:47:30 -0700304#define encode_dev(x) choose_32_64(old_encode_dev,new_encode_dev)(x)
305
Linus Torvalds8529f612012-05-06 18:02:40 -0700306#ifndef INIT_STRUCT_STAT_PADDING
307# define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
308#endif
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
311{
312 struct stat tmp;
313
Linus Torvaldsa52dd972012-05-06 17:47:30 -0700314 if (!valid_dev(stat->dev) || !valid_dev(stat->rdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return -EOVERFLOW;
Linus Torvaldsa52dd972012-05-06 17:47:30 -0700316#if BITS_PER_LONG == 32
317 if (stat->size > MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return -EOVERFLOW;
319#endif
320
Linus Torvalds8529f612012-05-06 18:02:40 -0700321 INIT_STRUCT_STAT_PADDING(tmp);
Linus Torvaldsa52dd972012-05-06 17:47:30 -0700322 tmp.st_dev = encode_dev(stat->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 tmp.st_ino = stat->ino;
David Howellsafefdbb2006-10-03 01:13:46 -0700324 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
325 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 tmp.st_mode = stat->mode;
327 tmp.st_nlink = stat->nlink;
328 if (tmp.st_nlink != stat->nlink)
329 return -EOVERFLOW;
Eric W. Biedermana7c19382012-02-09 09:10:30 -0800330 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 Torvaldsa52dd972012-05-06 17:47:30 -0700332 tmp.st_rdev = encode_dev(stat->rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 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 Howellsc7887322010-08-11 11:26:22 +0100347SYSCALL_DEFINE2(newstat, const char __user *, filename,
348 struct stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 struct kstat stat;
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400351 int error = vfs_stat(filename, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400353 if (error)
354 return error;
355 return cp_new_stat(&stat, statbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800357
David Howellsc7887322010-08-11 11:26:22 +0100358SYSCALL_DEFINE2(newlstat, const char __user *, filename,
359 struct stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 struct kstat stat;
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400362 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400364 error = vfs_lstat(filename, &stat);
365 if (error)
366 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400368 return cp_new_stat(&stat, statbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800370
Andreas Schwab2833c282006-04-27 15:46:42 +0200371#if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
David Howellsc7887322010-08-11 11:26:22 +0100372SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
Heiko Carstens6559eed82009-01-14 14:14:32 +0100373 struct stat __user *, statbuf, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800374{
375 struct kstat stat;
Oleg Drokin0112fc22009-04-08 20:05:42 +0400376 int error;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800377
Oleg Drokin0112fc22009-04-08 20:05:42 +0400378 error = vfs_fstatat(dfd, filename, &stat, flag);
379 if (error)
380 return error;
381 return cp_new_stat(&stat, statbuf);
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800382}
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800383#endif
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800384
Heiko Carstens257ac262009-01-14 14:14:13 +0100385SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
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 Bergmann82b355d2018-04-13 11:50:12 +0200395#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Dominik Brodowski2dae0242018-03-11 11:34:27 +0100397static int do_readlinkat(int dfd, const char __user *pathname,
398 char __user *buf, int bufsiz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Al Viro2d8f3032008-07-22 09:59:21 -0400400 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 int error;
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +0100402 int empty = 0;
Jeff Layton79551192012-12-11 12:10:06 -0500403 unsigned int lookup_flags = LOOKUP_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 if (bufsiz <= 0)
406 return -EINVAL;
407
Jeff Layton79551192012-12-11 12:10:06 -0500408retry:
409 error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (!error) {
David Howellsbb6687342015-03-17 22:26:21 +0000411 struct inode *inode = d_backing_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +0100413 error = empty ? -ENOENT : -EINVAL;
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +0100414 /*
415 * AFS mountpoints allow readlink(2) but are not symlinks
416 */
417 if (d_is_symlink(path.dentry) || inode->i_op->readlink) {
Al Viro2d8f3032008-07-22 09:59:21 -0400418 error = security_inode_readlink(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (!error) {
Al Viro68ac1232012-03-15 08:21:57 -0400420 touch_atime(&path);
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +0100421 error = vfs_readlink(path.dentry, buf, bufsiz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423 }
Al Viro2d8f3032008-07-22 09:59:21 -0400424 path_put(&path);
Jeff Layton79551192012-12-11 12:10:06 -0500425 if (retry_estale(error, lookup_flags)) {
426 lookup_flags |= LOOKUP_REVAL;
427 goto retry;
428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430 return error;
431}
432
Dominik Brodowski2dae0242018-03-11 11:34:27 +0100433SYSCALL_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 Carstens002c8972009-01-14 14:14:18 +0100439SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
440 int, bufsiz)
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800441{
Dominik Brodowski2dae0242018-03-11 11:34:27 +0100442 return do_readlinkat(AT_FDCWD, path, buf, bufsiz);
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800443}
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446/* ---------- LFS-64 ----------- */
Catalin Marinas0753f702012-03-19 15:13:51 +0000447#if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Linus Torvalds8529f612012-05-06 18:02:40 -0700449#ifndef INIT_STRUCT_STAT64_PADDING
450# define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
451#endif
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
454{
455 struct stat64 tmp;
456
Linus Torvalds8529f612012-05-06 18:02:40 -0700457 INIT_STRUCT_STAT64_PADDING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458#ifdef CONFIG_MIPS
459 /* mips has weird padding, so we don't get 64 bits there */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 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 Howellsafefdbb2006-10-03 01:13:46 -0700467 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
468 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469#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. Biedermana7c19382012-02-09 09:10:30 -0800474 tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
475 tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 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 Howellsc7887322010-08-11 11:26:22 +0100488SYSCALL_DEFINE2(stat64, const char __user *, filename,
489 struct stat64 __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
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 Carstens257ac262009-01-14 14:14:13 +0100499
David Howellsc7887322010-08-11 11:26:22 +0100500SYSCALL_DEFINE2(lstat64, const char __user *, filename,
501 struct stat64 __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
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 Carstens257ac262009-01-14 14:14:13 +0100511
512SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
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 Howellsc7887322010-08-11 11:26:22 +0100523SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
Heiko Carstens6559eed82009-01-14 14:14:32 +0100524 struct stat64 __user *, statbuf, int, flag)
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800525{
526 struct kstat stat;
Oleg Drokin0112fc22009-04-08 20:05:42 +0400527 int error;
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800528
Oleg Drokin0112fc22009-04-08 20:05:42 +0400529 error = vfs_fstatat(dfd, filename, &stat, flag);
530 if (error)
531 return error;
532 return cp_new_stat64(&stat, statbuf);
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800533}
Catalin Marinas0753f702012-03-19 15:13:51 +0000534#endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Jens Axboe3934e362019-12-14 13:26:33 -0700536noinline_for_stack int
Eric Biggers64bd7202017-03-31 18:31:48 +0100537cp_statx(const struct kstat *stat, struct statx __user *buffer)
David Howellsa528d352017-01-31 16:46:22 +0000538{
Eric Biggers64bd7202017-03-31 18:31:48 +0100539 struct statx tmp;
David Howellsa528d352017-01-31 16:46:22 +0000540
Eric Biggers64bd7202017-03-31 18:31:48 +0100541 memset(&tmp, 0, sizeof(tmp));
David Howellsa528d352017-01-31 16:46:22 +0000542
Eric Biggers64bd7202017-03-31 18:31:48 +0100543 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 Howells3209f682017-03-31 18:32:17 +0100553 tmp.stx_attributes_mask = stat->attributes_mask;
Eric Biggers64bd7202017-03-31 18:31:48 +0100554 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 Howellsa528d352017-01-31 16:46:22 +0000566
Eric Biggers64bd7202017-03-31 18:31:48 +0100567 return copy_to_user(buffer, &tmp, sizeof(tmp)) ? -EFAULT : 0;
David Howellsa528d352017-01-31 16:46:22 +0000568}
569
570/**
571 * sys_statx - System call to get enhanced stats
572 * @dfd: Base directory to pathwalk from *or* fd to stat.
David Howells1e2f82d2017-04-26 22:15:55 +0100573 * @filename: File to stat or "" with AT_EMPTY_PATH
David Howellsa528d352017-01-31 16:46:22 +0000574 * @flags: AT_* flags to control pathwalk.
575 * @mask: Parts of statx struct actually required.
576 * @buffer: Result buffer.
577 *
David Howells1e2f82d2017-04-26 22:15:55 +0100578 * 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 Howellsa528d352017-01-31 16:46:22 +0000580 */
581SYSCALL_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 Howells47071ae2017-03-31 18:32:10 +0100589 if (mask & STATX__RESERVED)
590 return -EINVAL;
David Howellsa528d352017-01-31 16:46:22 +0000591 if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE)
592 return -EINVAL;
David Howellsa528d352017-01-31 16:46:22 +0000593
David Howells1e2f82d2017-04-26 22:15:55 +0100594 error = vfs_statx(dfd, filename, flags, &stat, mask);
David Howellsa528d352017-01-31 16:46:22 +0000595 if (error)
596 return error;
Eric Biggers64bd7202017-03-31 18:31:48 +0100597
598 return cp_statx(&stat, buffer);
David Howellsa528d352017-01-31 16:46:22 +0000599}
600
Al Viroac565de2017-04-08 18:13:00 -0400601#ifdef CONFIG_COMPAT
602static 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
635COMPAT_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
647COMPAT_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
660COMPAT_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
674COMPAT_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 Monakhovb4627072009-12-14 15:21:12 +0300686/* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
687void __inode_add_bytes(struct inode *inode, loff_t bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 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 Monakhovb4627072009-12-14 15:21:12 +0300696}
Al Viroeb315d22017-06-08 21:15:03 -0400697EXPORT_SYMBOL(__inode_add_bytes);
Dmitry Monakhovb4627072009-12-14 15:21:12 +0300698
699void inode_add_bytes(struct inode *inode, loff_t bytes)
700{
701 spin_lock(&inode->i_lock);
702 __inode_add_bytes(inode, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 spin_unlock(&inode->i_lock);
704}
705
706EXPORT_SYMBOL(inode_add_bytes);
707
Jan Kara1c8924e2013-08-17 09:32:32 -0400708void __inode_sub_bytes(struct inode *inode, loff_t bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 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 Kara1c8924e2013-08-17 09:32:32 -0400717}
718
719EXPORT_SYMBOL(__inode_sub_bytes);
720
721void inode_sub_bytes(struct inode *inode, loff_t bytes)
722{
723 spin_lock(&inode->i_lock);
724 __inode_sub_bytes(inode, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 spin_unlock(&inode->i_lock);
726}
727
728EXPORT_SYMBOL(inode_sub_bytes);
729
730loff_t inode_get_bytes(struct inode *inode)
731{
732 loff_t ret;
733
734 spin_lock(&inode->i_lock);
Jan Karaf4a8116a2017-08-08 09:54:36 +0200735 ret = __inode_get_bytes(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 spin_unlock(&inode->i_lock);
737 return ret;
738}
739
740EXPORT_SYMBOL(inode_get_bytes);
741
742void 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
750EXPORT_SYMBOL(inode_set_bytes);