]> git.ipfire.org Git - thirdparty/linux.git/blame - fs/stat.c
statx: hide interfaces no longer used by io_uring
[thirdparty/linux.git] / fs / stat.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * linux/fs/stat.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
630d9c47 8#include <linux/export.h>
1da177e4
LT
9#include <linux/mm.h>
10#include <linux/errno.h>
11#include <linux/file.h>
1da177e4
LT
12#include <linux/highuid.h>
13#include <linux/fs.h>
14#include <linux/namei.h>
15#include <linux/security.h>
5b825c3a 16#include <linux/cred.h>
1da177e4 17#include <linux/syscalls.h>
ba52de12 18#include <linux/pagemap.h>
ac565de3 19#include <linux/compat.h>
1da177e4 20
7c0f6ba6 21#include <linux/uaccess.h>
1da177e4
LT
22#include <asm/unistd.h>
23
3934e36f
JA
24#include "internal.h"
25
a528d35e
DH
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 */
1da177e4
LT
35void 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;
3ddcd056 44 stat->size = i_size_read(inode);
1da177e4
LT
45 stat->atime = inode->i_atime;
46 stat->mtime = inode->i_mtime;
47 stat->ctime = inode->i_ctime;
93407472 48 stat->blksize = i_blocksize(inode);
3ddcd056 49 stat->blocks = inode->i_blocks;
a528d35e 50}
1da177e4
LT
51EXPORT_SYMBOL(generic_fillattr);
52
b7a6ec52
BF
53/**
54 * vfs_getattr_nosec - getattr without security checks
55 * @path: file to get attributes from
56 * @stat: structure to return attributes in
a528d35e
DH
57 * @request_mask: STATX_xxx flags indicating what the caller wants
58 * @query_flags: Query mode (KSTAT_QUERY_FLAGS)
b7a6ec52
BF
59 *
60 * Get attributes without calling security_inode_getattr.
61 *
62 * Currently the only caller other than vfs_getattr is internal to the
a528d35e
DH
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.
b7a6ec52 65 */
a528d35e
DH
66int vfs_getattr_nosec(const struct path *path, struct kstat *stat,
67 u32 request_mask, unsigned int query_flags)
1da177e4 68{
bb668734 69 struct inode *inode = d_backing_inode(path->dentry);
1da177e4 70
a528d35e
DH
71 memset(stat, 0, sizeof(*stat));
72 stat->result_mask |= STATX_BASIC_STATS;
73 request_mask &= STATX_ALL;
74 query_flags &= KSTAT_QUERY_FLAGS;
801e5237
CH
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
1da177e4 82 if (inode->i_op->getattr)
a528d35e
DH
83 return inode->i_op->getattr(path, stat, request_mask,
84 query_flags);
1da177e4
LT
85
86 generic_fillattr(inode, stat);
1da177e4
LT
87 return 0;
88}
b7a6ec52
BF
89EXPORT_SYMBOL(vfs_getattr_nosec);
90
a528d35e
DH
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 */
112int vfs_getattr(const struct path *path, struct kstat *stat,
113 u32 request_mask, unsigned int query_flags)
b7a6ec52
BF
114{
115 int retval;
116
3f7036a0 117 retval = security_inode_getattr(path);
b7a6ec52
BF
118 if (retval)
119 return retval;
a528d35e 120 return vfs_getattr_nosec(path, stat, request_mask, query_flags);
b7a6ec52 121}
1da177e4
LT
122EXPORT_SYMBOL(vfs_getattr);
123
a528d35e
DH
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 */
136int vfs_statx_fd(unsigned int fd, struct kstat *stat,
137 u32 request_mask, unsigned int query_flags)
1da177e4 138{
8c7493aa 139 struct fd f;
1da177e4
LT
140 int error = -EBADF;
141
8c7493aa
EB
142 if (query_flags & ~KSTAT_QUERY_FLAGS)
143 return -EINVAL;
144
145 f = fdget_raw(fd);
2903ff01 146 if (f.file) {
a528d35e
DH
147 error = vfs_getattr(&f.file->f_path, stat,
148 request_mask, query_flags);
2903ff01 149 fdput(f);
1da177e4
LT
150 }
151 return error;
152}
a528d35e 153EXPORT_SYMBOL(vfs_statx_fd);
1da177e4 154
6f88cc17
BM
155static inline unsigned vfs_stat_set_lookup_flags(unsigned *lookup_flags,
156 int flags)
3934e36f
JA
157{
158 if ((flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT |
159 AT_EMPTY_PATH | KSTAT_QUERY_FLAGS)) != 0)
160 return -EINVAL;
161
162 *lookup_flags = LOOKUP_FOLLOW | LOOKUP_AUTOMOUNT;
163 if (flags & AT_SYMLINK_NOFOLLOW)
164 *lookup_flags &= ~LOOKUP_FOLLOW;
165 if (flags & AT_NO_AUTOMOUNT)
166 *lookup_flags &= ~LOOKUP_AUTOMOUNT;
167 if (flags & AT_EMPTY_PATH)
168 *lookup_flags |= LOOKUP_EMPTY;
169
170 return 0;
171}
172
a528d35e
DH
173/**
174 * vfs_statx - Get basic and extra attributes by filename
175 * @dfd: A file descriptor representing the base dir for a relative filename
176 * @filename: The name of the file of interest
177 * @flags: Flags to control the query
178 * @stat: The result structure to fill in.
179 * @request_mask: STATX_xxx flags indicating what the caller wants
180 *
181 * This function is a wrapper around vfs_getattr(). The main difference is
182 * that it uses a filename and base directory to determine the file location.
183 * Additionally, the use of AT_SYMLINK_NOFOLLOW in flags will prevent a symlink
184 * at the given name from being referenced.
185 *
a528d35e
DH
186 * 0 will be returned on success, and a -ve error code if unsuccessful.
187 */
188int vfs_statx(int dfd, const char __user *filename, int flags,
189 struct kstat *stat, u32 request_mask)
0112fc22 190{
2eae7a18 191 struct path path;
0112fc22 192 int error = -EINVAL;
3934e36f 193 unsigned lookup_flags;
0112fc22 194
3934e36f 195 if (vfs_stat_set_lookup_flags(&lookup_flags, flags))
a528d35e 196 return -EINVAL;
836fb7e7 197retry:
2eae7a18
CH
198 error = user_path_at(dfd, filename, lookup_flags, &path);
199 if (error)
200 goto out;
201
a528d35e 202 error = vfs_getattr(&path, stat, request_mask, flags);
2eae7a18 203 path_put(&path);
836fb7e7
JL
204 if (retry_estale(error, lookup_flags)) {
205 lookup_flags |= LOOKUP_REVAL;
206 goto retry;
207 }
0112fc22
OD
208out:
209 return error;
210}
a528d35e 211EXPORT_SYMBOL(vfs_statx);
2eae7a18 212
0112fc22 213
1da177e4
LT
214#ifdef __ARCH_WANT_OLD_STAT
215
216/*
217 * For backward compatibility? Maybe this should be moved
218 * into arch/i386 instead?
219 */
220static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
221{
222 static int warncount = 5;
223 struct __old_kernel_stat tmp;
a528d35e 224
1da177e4
LT
225 if (warncount > 0) {
226 warncount--;
227 printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
228 current->comm);
229 } else if (warncount < 0) {
230 /* it's laughable, but... */
231 warncount = 0;
232 }
233
234 memset(&tmp, 0, sizeof(struct __old_kernel_stat));
235 tmp.st_dev = old_encode_dev(stat->dev);
236 tmp.st_ino = stat->ino;
afefdbb2
DH
237 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
238 return -EOVERFLOW;
1da177e4
LT
239 tmp.st_mode = stat->mode;
240 tmp.st_nlink = stat->nlink;
241 if (tmp.st_nlink != stat->nlink)
242 return -EOVERFLOW;
a7c1938e
EB
243 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
244 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
1da177e4
LT
245 tmp.st_rdev = old_encode_dev(stat->rdev);
246#if BITS_PER_LONG == 32
247 if (stat->size > MAX_NON_LFS)
248 return -EOVERFLOW;
a528d35e 249#endif
1da177e4
LT
250 tmp.st_size = stat->size;
251 tmp.st_atime = stat->atime.tv_sec;
252 tmp.st_mtime = stat->mtime.tv_sec;
253 tmp.st_ctime = stat->ctime.tv_sec;
254 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
255}
256
c7887325
DH
257SYSCALL_DEFINE2(stat, const char __user *, filename,
258 struct __old_kernel_stat __user *, statbuf)
1da177e4
LT
259{
260 struct kstat stat;
2eae7a18 261 int error;
1da177e4 262
2eae7a18
CH
263 error = vfs_stat(filename, &stat);
264 if (error)
265 return error;
1da177e4 266
2eae7a18 267 return cp_old_stat(&stat, statbuf);
1da177e4 268}
257ac264 269
c7887325
DH
270SYSCALL_DEFINE2(lstat, const char __user *, filename,
271 struct __old_kernel_stat __user *, statbuf)
1da177e4
LT
272{
273 struct kstat stat;
2eae7a18 274 int error;
1da177e4 275
2eae7a18
CH
276 error = vfs_lstat(filename, &stat);
277 if (error)
278 return error;
1da177e4 279
2eae7a18 280 return cp_old_stat(&stat, statbuf);
1da177e4 281}
257ac264
HC
282
283SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
1da177e4
LT
284{
285 struct kstat stat;
286 int error = vfs_fstat(fd, &stat);
287
288 if (!error)
289 error = cp_old_stat(&stat, statbuf);
290
291 return error;
292}
293
294#endif /* __ARCH_WANT_OLD_STAT */
295
82b355d1
AB
296#ifdef __ARCH_WANT_NEW_STAT
297
a52dd971
LT
298#if BITS_PER_LONG == 32
299# define choose_32_64(a,b) a
300#else
301# define choose_32_64(a,b) b
302#endif
303
4c416f42 304#define valid_dev(x) choose_32_64(old_valid_dev(x),true)
a52dd971
LT
305#define encode_dev(x) choose_32_64(old_encode_dev,new_encode_dev)(x)
306
8529f613
LT
307#ifndef INIT_STRUCT_STAT_PADDING
308# define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
309#endif
310
1da177e4
LT
311static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
312{
313 struct stat tmp;
314
a52dd971 315 if (!valid_dev(stat->dev) || !valid_dev(stat->rdev))
1da177e4 316 return -EOVERFLOW;
a52dd971
LT
317#if BITS_PER_LONG == 32
318 if (stat->size > MAX_NON_LFS)
1da177e4
LT
319 return -EOVERFLOW;
320#endif
321
8529f613 322 INIT_STRUCT_STAT_PADDING(tmp);
a52dd971 323 tmp.st_dev = encode_dev(stat->dev);
1da177e4 324 tmp.st_ino = stat->ino;
afefdbb2
DH
325 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
326 return -EOVERFLOW;
1da177e4
LT
327 tmp.st_mode = stat->mode;
328 tmp.st_nlink = stat->nlink;
329 if (tmp.st_nlink != stat->nlink)
330 return -EOVERFLOW;
a7c1938e
EB
331 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
332 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
a52dd971 333 tmp.st_rdev = encode_dev(stat->rdev);
1da177e4
LT
334 tmp.st_size = stat->size;
335 tmp.st_atime = stat->atime.tv_sec;
336 tmp.st_mtime = stat->mtime.tv_sec;
337 tmp.st_ctime = stat->ctime.tv_sec;
338#ifdef STAT_HAVE_NSEC
339 tmp.st_atime_nsec = stat->atime.tv_nsec;
340 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
341 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
342#endif
343 tmp.st_blocks = stat->blocks;
344 tmp.st_blksize = stat->blksize;
345 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
346}
347
c7887325
DH
348SYSCALL_DEFINE2(newstat, const char __user *, filename,
349 struct stat __user *, statbuf)
5590ff0d
UD
350{
351 struct kstat stat;
2eae7a18 352 int error = vfs_stat(filename, &stat);
5590ff0d 353
2eae7a18
CH
354 if (error)
355 return error;
356 return cp_new_stat(&stat, statbuf);
5590ff0d
UD
357}
358
c7887325
DH
359SYSCALL_DEFINE2(newlstat, const char __user *, filename,
360 struct stat __user *, statbuf)
1da177e4
LT
361{
362 struct kstat stat;
2eae7a18 363 int error;
1da177e4 364
2eae7a18
CH
365 error = vfs_lstat(filename, &stat);
366 if (error)
367 return error;
1da177e4 368
2eae7a18 369 return cp_new_stat(&stat, statbuf);
1da177e4 370}
5590ff0d 371
2833c28a 372#if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
c7887325 373SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
6559eed8 374 struct stat __user *, statbuf, int, flag)
1da177e4
LT
375{
376 struct kstat stat;
0112fc22 377 int error;
1da177e4 378
0112fc22
OD
379 error = vfs_fstatat(dfd, filename, &stat, flag);
380 if (error)
381 return error;
382 return cp_new_stat(&stat, statbuf);
1da177e4 383}
cff2b760 384#endif
5590ff0d 385
257ac264 386SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
1da177e4
LT
387{
388 struct kstat stat;
389 int error = vfs_fstat(fd, &stat);
390
391 if (!error)
392 error = cp_new_stat(&stat, statbuf);
393
394 return error;
395}
82b355d1 396#endif
1da177e4 397
2dae0248
DB
398static int do_readlinkat(int dfd, const char __user *pathname,
399 char __user *buf, int bufsiz)
1da177e4 400{
2d8f3038 401 struct path path;
1da177e4 402 int error;
1fa1e7f6 403 int empty = 0;
7955119e 404 unsigned int lookup_flags = LOOKUP_EMPTY;
1da177e4
LT
405
406 if (bufsiz <= 0)
407 return -EINVAL;
408
7955119e
JL
409retry:
410 error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
1da177e4 411 if (!error) {
bb668734 412 struct inode *inode = d_backing_inode(path.dentry);
1da177e4 413
1fa1e7f6 414 error = empty ? -ENOENT : -EINVAL;
fd4a0edf
MS
415 /*
416 * AFS mountpoints allow readlink(2) but are not symlinks
417 */
418 if (d_is_symlink(path.dentry) || inode->i_op->readlink) {
2d8f3038 419 error = security_inode_readlink(path.dentry);
1da177e4 420 if (!error) {
68ac1234 421 touch_atime(&path);
fd4a0edf 422 error = vfs_readlink(path.dentry, buf, bufsiz);
1da177e4
LT
423 }
424 }
2d8f3038 425 path_put(&path);
7955119e
JL
426 if (retry_estale(error, lookup_flags)) {
427 lookup_flags |= LOOKUP_REVAL;
428 goto retry;
429 }
1da177e4
LT
430 }
431 return error;
432}
433
2dae0248
DB
434SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
435 char __user *, buf, int, bufsiz)
436{
437 return do_readlinkat(dfd, pathname, buf, bufsiz);
438}
439
002c8976
HC
440SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
441 int, bufsiz)
5590ff0d 442{
2dae0248 443 return do_readlinkat(AT_FDCWD, path, buf, bufsiz);
5590ff0d
UD
444}
445
1da177e4
LT
446
447/* ---------- LFS-64 ----------- */
0753f70f 448#if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
1da177e4 449
8529f613
LT
450#ifndef INIT_STRUCT_STAT64_PADDING
451# define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
452#endif
453
1da177e4
LT
454static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
455{
456 struct stat64 tmp;
457
8529f613 458 INIT_STRUCT_STAT64_PADDING(tmp);
1da177e4
LT
459#ifdef CONFIG_MIPS
460 /* mips has weird padding, so we don't get 64 bits there */
1da177e4
LT
461 tmp.st_dev = new_encode_dev(stat->dev);
462 tmp.st_rdev = new_encode_dev(stat->rdev);
463#else
464 tmp.st_dev = huge_encode_dev(stat->dev);
465 tmp.st_rdev = huge_encode_dev(stat->rdev);
466#endif
467 tmp.st_ino = stat->ino;
afefdbb2
DH
468 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
469 return -EOVERFLOW;
1da177e4
LT
470#ifdef STAT64_HAS_BROKEN_ST_INO
471 tmp.__st_ino = stat->ino;
472#endif
473 tmp.st_mode = stat->mode;
474 tmp.st_nlink = stat->nlink;
a7c1938e
EB
475 tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
476 tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
1da177e4
LT
477 tmp.st_atime = stat->atime.tv_sec;
478 tmp.st_atime_nsec = stat->atime.tv_nsec;
479 tmp.st_mtime = stat->mtime.tv_sec;
480 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
481 tmp.st_ctime = stat->ctime.tv_sec;
482 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
483 tmp.st_size = stat->size;
484 tmp.st_blocks = stat->blocks;
485 tmp.st_blksize = stat->blksize;
486 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
487}
488
c7887325
DH
489SYSCALL_DEFINE2(stat64, const char __user *, filename,
490 struct stat64 __user *, statbuf)
1da177e4
LT
491{
492 struct kstat stat;
493 int error = vfs_stat(filename, &stat);
494
495 if (!error)
496 error = cp_new_stat64(&stat, statbuf);
497
498 return error;
499}
257ac264 500
c7887325
DH
501SYSCALL_DEFINE2(lstat64, const char __user *, filename,
502 struct stat64 __user *, statbuf)
1da177e4
LT
503{
504 struct kstat stat;
505 int error = vfs_lstat(filename, &stat);
506
507 if (!error)
508 error = cp_new_stat64(&stat, statbuf);
509
510 return error;
511}
257ac264
HC
512
513SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
1da177e4
LT
514{
515 struct kstat stat;
516 int error = vfs_fstat(fd, &stat);
517
518 if (!error)
519 error = cp_new_stat64(&stat, statbuf);
520
521 return error;
522}
523
c7887325 524SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
6559eed8 525 struct stat64 __user *, statbuf, int, flag)
cff2b760
UD
526{
527 struct kstat stat;
0112fc22 528 int error;
cff2b760 529
0112fc22
OD
530 error = vfs_fstatat(dfd, filename, &stat, flag);
531 if (error)
532 return error;
533 return cp_new_stat64(&stat, statbuf);
cff2b760 534}
0753f70f 535#endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
1da177e4 536
6f88cc17 537static noinline_for_stack int
64bd7204 538cp_statx(const struct kstat *stat, struct statx __user *buffer)
a528d35e 539{
64bd7204
EB
540 struct statx tmp;
541
542 memset(&tmp, 0, sizeof(tmp));
543
544 tmp.stx_mask = stat->result_mask;
545 tmp.stx_blksize = stat->blksize;
546 tmp.stx_attributes = stat->attributes;
547 tmp.stx_nlink = stat->nlink;
548 tmp.stx_uid = from_kuid_munged(current_user_ns(), stat->uid);
549 tmp.stx_gid = from_kgid_munged(current_user_ns(), stat->gid);
550 tmp.stx_mode = stat->mode;
551 tmp.stx_ino = stat->ino;
552 tmp.stx_size = stat->size;
553 tmp.stx_blocks = stat->blocks;
3209f68b 554 tmp.stx_attributes_mask = stat->attributes_mask;
64bd7204
EB
555 tmp.stx_atime.tv_sec = stat->atime.tv_sec;
556 tmp.stx_atime.tv_nsec = stat->atime.tv_nsec;
557 tmp.stx_btime.tv_sec = stat->btime.tv_sec;
558 tmp.stx_btime.tv_nsec = stat->btime.tv_nsec;
559 tmp.stx_ctime.tv_sec = stat->ctime.tv_sec;
560 tmp.stx_ctime.tv_nsec = stat->ctime.tv_nsec;
561 tmp.stx_mtime.tv_sec = stat->mtime.tv_sec;
562 tmp.stx_mtime.tv_nsec = stat->mtime.tv_nsec;
563 tmp.stx_rdev_major = MAJOR(stat->rdev);
564 tmp.stx_rdev_minor = MINOR(stat->rdev);
565 tmp.stx_dev_major = MAJOR(stat->dev);
566 tmp.stx_dev_minor = MINOR(stat->dev);
567
568 return copy_to_user(buffer, &tmp, sizeof(tmp)) ? -EFAULT : 0;
a528d35e
DH
569}
570
0018784f
BM
571int do_statx(int dfd, const char __user *filename, unsigned flags,
572 unsigned int mask, struct statx __user *buffer)
573{
574 struct kstat stat;
575 int error;
576
577 if (mask & STATX__RESERVED)
578 return -EINVAL;
579 if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE)
580 return -EINVAL;
581
582 error = vfs_statx(dfd, filename, flags, &stat, mask);
583 if (error)
584 return error;
585
586 return cp_statx(&stat, buffer);
587}
588
a528d35e
DH
589/**
590 * sys_statx - System call to get enhanced stats
591 * @dfd: Base directory to pathwalk from *or* fd to stat.
1e2f82d1 592 * @filename: File to stat or "" with AT_EMPTY_PATH
a528d35e
DH
593 * @flags: AT_* flags to control pathwalk.
594 * @mask: Parts of statx struct actually required.
595 * @buffer: Result buffer.
596 *
1e2f82d1
DH
597 * Note that fstat() can be emulated by setting dfd to the fd of interest,
598 * supplying "" as the filename and setting AT_EMPTY_PATH in the flags.
a528d35e
DH
599 */
600SYSCALL_DEFINE5(statx,
601 int, dfd, const char __user *, filename, unsigned, flags,
602 unsigned int, mask,
603 struct statx __user *, buffer)
604{
0018784f 605 return do_statx(dfd, filename, flags, mask, buffer);
a528d35e
DH
606}
607
ac565de3
AV
608#ifdef CONFIG_COMPAT
609static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
610{
611 struct compat_stat tmp;
612
613 if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
614 return -EOVERFLOW;
615
616 memset(&tmp, 0, sizeof(tmp));
617 tmp.st_dev = old_encode_dev(stat->dev);
618 tmp.st_ino = stat->ino;
619 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
620 return -EOVERFLOW;
621 tmp.st_mode = stat->mode;
622 tmp.st_nlink = stat->nlink;
623 if (tmp.st_nlink != stat->nlink)
624 return -EOVERFLOW;
625 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
626 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
627 tmp.st_rdev = old_encode_dev(stat->rdev);
628 if ((u64) stat->size > MAX_NON_LFS)
629 return -EOVERFLOW;
630 tmp.st_size = stat->size;
631 tmp.st_atime = stat->atime.tv_sec;
632 tmp.st_atime_nsec = stat->atime.tv_nsec;
633 tmp.st_mtime = stat->mtime.tv_sec;
634 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
635 tmp.st_ctime = stat->ctime.tv_sec;
636 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
637 tmp.st_blocks = stat->blocks;
638 tmp.st_blksize = stat->blksize;
639 return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
640}
641
642COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename,
643 struct compat_stat __user *, statbuf)
644{
645 struct kstat stat;
646 int error;
647
648 error = vfs_stat(filename, &stat);
649 if (error)
650 return error;
651 return cp_compat_stat(&stat, statbuf);
652}
653
654COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename,
655 struct compat_stat __user *, statbuf)
656{
657 struct kstat stat;
658 int error;
659
660 error = vfs_lstat(filename, &stat);
661 if (error)
662 return error;
663 return cp_compat_stat(&stat, statbuf);
664}
665
666#ifndef __ARCH_WANT_STAT64
667COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd,
668 const char __user *, filename,
669 struct compat_stat __user *, statbuf, int, flag)
670{
671 struct kstat stat;
672 int error;
673
674 error = vfs_fstatat(dfd, filename, &stat, flag);
675 if (error)
676 return error;
677 return cp_compat_stat(&stat, statbuf);
678}
679#endif
680
681COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd,
682 struct compat_stat __user *, statbuf)
683{
684 struct kstat stat;
685 int error = vfs_fstat(fd, &stat);
686
687 if (!error)
688 error = cp_compat_stat(&stat, statbuf);
689 return error;
690}
691#endif
692
b462707e
DM
693/* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
694void __inode_add_bytes(struct inode *inode, loff_t bytes)
1da177e4 695{
1da177e4
LT
696 inode->i_blocks += bytes >> 9;
697 bytes &= 511;
698 inode->i_bytes += bytes;
699 if (inode->i_bytes >= 512) {
700 inode->i_blocks++;
701 inode->i_bytes -= 512;
702 }
b462707e 703}
eb315d2a 704EXPORT_SYMBOL(__inode_add_bytes);
b462707e
DM
705
706void inode_add_bytes(struct inode *inode, loff_t bytes)
707{
708 spin_lock(&inode->i_lock);
709 __inode_add_bytes(inode, bytes);
1da177e4
LT
710 spin_unlock(&inode->i_lock);
711}
712
713EXPORT_SYMBOL(inode_add_bytes);
714
1c8924eb 715void __inode_sub_bytes(struct inode *inode, loff_t bytes)
1da177e4 716{
1da177e4
LT
717 inode->i_blocks -= bytes >> 9;
718 bytes &= 511;
719 if (inode->i_bytes < bytes) {
720 inode->i_blocks--;
721 inode->i_bytes += 512;
722 }
723 inode->i_bytes -= bytes;
1c8924eb
JK
724}
725
726EXPORT_SYMBOL(__inode_sub_bytes);
727
728void inode_sub_bytes(struct inode *inode, loff_t bytes)
729{
730 spin_lock(&inode->i_lock);
731 __inode_sub_bytes(inode, bytes);
1da177e4
LT
732 spin_unlock(&inode->i_lock);
733}
734
735EXPORT_SYMBOL(inode_sub_bytes);
736
737loff_t inode_get_bytes(struct inode *inode)
738{
739 loff_t ret;
740
741 spin_lock(&inode->i_lock);
f4a8116a 742 ret = __inode_get_bytes(inode);
1da177e4
LT
743 spin_unlock(&inode->i_lock);
744 return ret;
745}
746
747EXPORT_SYMBOL(inode_get_bytes);
748
749void inode_set_bytes(struct inode *inode, loff_t bytes)
750{
751 /* Caller is here responsible for sufficient locking
752 * (ie. inode->i_lock) */
753 inode->i_blocks = bytes >> 9;
754 inode->i_bytes = bytes & 511;
755}
756
757EXPORT_SYMBOL(inode_set_bytes);