mnt_idmapping: add missing helpers
[linux-block.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
2d985f8c 8#include <linux/blkdev.h>
630d9c47 9#include <linux/export.h>
1da177e4
LT
10#include <linux/mm.h>
11#include <linux/errno.h>
12#include <linux/file.h>
1da177e4
LT
13#include <linux/highuid.h>
14#include <linux/fs.h>
15#include <linux/namei.h>
16#include <linux/security.h>
5b825c3a 17#include <linux/cred.h>
1da177e4 18#include <linux/syscalls.h>
ba52de12 19#include <linux/pagemap.h>
ac565de3 20#include <linux/compat.h>
1da177e4 21
7c0f6ba6 22#include <linux/uaccess.h>
1da177e4
LT
23#include <asm/unistd.h>
24
3934e36f 25#include "internal.h"
fa2fcf4f 26#include "mount.h"
3934e36f 27
a528d35e
DH
28/**
29 * generic_fillattr - Fill in the basic attributes from the inode struct
0d56a451
CB
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
a528d35e
DH
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.
0d56a451
CB
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.
a528d35e 43 */
0d56a451
CB
44void generic_fillattr(struct user_namespace *mnt_userns, struct inode *inode,
45 struct kstat *stat)
1da177e4
LT
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;
0d56a451
CB
51 stat->uid = i_uid_into_mnt(mnt_userns, inode);
52 stat->gid = i_gid_into_mnt(mnt_userns, inode);
1da177e4 53 stat->rdev = inode->i_rdev;
3ddcd056 54 stat->size = i_size_read(inode);
1da177e4
LT
55 stat->atime = inode->i_atime;
56 stat->mtime = inode->i_mtime;
57 stat->ctime = inode->i_ctime;
93407472 58 stat->blksize = i_blocksize(inode);
3ddcd056 59 stat->blocks = inode->i_blocks;
a528d35e 60}
1da177e4
LT
61EXPORT_SYMBOL(generic_fillattr);
62
4f911138
AG
63/**
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 */
71void 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}
79EXPORT_SYMBOL(generic_fill_statx_attr);
80
b7a6ec52
BF
81/**
82 * vfs_getattr_nosec - getattr without security checks
83 * @path: file to get attributes from
84 * @stat: structure to return attributes in
a528d35e 85 * @request_mask: STATX_xxx flags indicating what the caller wants
f2d077ff 86 * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
b7a6ec52
BF
87 *
88 * Get attributes without calling security_inode_getattr.
89 *
90 * Currently the only caller other than vfs_getattr is internal to the
a528d35e
DH
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.
b7a6ec52 93 */
a528d35e
DH
94int vfs_getattr_nosec(const struct path *path, struct kstat *stat,
95 u32 request_mask, unsigned int query_flags)
1da177e4 96{
549c7297 97 struct user_namespace *mnt_userns;
bb668734 98 struct inode *inode = d_backing_inode(path->dentry);
1da177e4 99
a528d35e
DH
100 memset(stat, 0, sizeof(*stat));
101 stat->result_mask |= STATX_BASIC_STATS;
f2d077ff 102 query_flags &= AT_STATX_SYNC_TYPE;
801e5237
CH
103
104 /* allow the fs to override these if it really wants to */
761e28fa
MS
105 /* SB_NOATIME means filesystem supplies dummy atime value */
106 if (inode->i_sb->s_flags & SB_NOATIME)
801e5237 107 stat->result_mask &= ~STATX_ATIME;
5afa7e8b
TT
108
109 /*
110 * Note: If you add another clause to set an attribute flag, please
111 * update attributes_mask below.
112 */
801e5237
CH
113 if (IS_AUTOMOUNT(inode))
114 stat->attributes |= STATX_ATTR_AUTOMOUNT;
115
712b2698
IW
116 if (IS_DAX(inode))
117 stat->attributes |= STATX_ATTR_DAX;
118
5afa7e8b
TT
119 stat->attributes_mask |= (STATX_ATTR_AUTOMOUNT |
120 STATX_ATTR_DAX);
121
549c7297 122 mnt_userns = mnt_user_ns(path->mnt);
1da177e4 123 if (inode->i_op->getattr)
549c7297
CB
124 return inode->i_op->getattr(mnt_userns, path, stat,
125 request_mask, query_flags);
1da177e4 126
549c7297 127 generic_fillattr(mnt_userns, inode, stat);
1da177e4
LT
128 return 0;
129}
b7a6ec52
BF
130EXPORT_SYMBOL(vfs_getattr_nosec);
131
a528d35e
DH
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
f2d077ff 137 * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
a528d35e
DH
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 */
153int vfs_getattr(const struct path *path, struct kstat *stat,
154 u32 request_mask, unsigned int query_flags)
b7a6ec52
BF
155{
156 int retval;
157
3f7036a0 158 retval = security_inode_getattr(path);
b7a6ec52
BF
159 if (retval)
160 return retval;
a528d35e 161 return vfs_getattr_nosec(path, stat, request_mask, query_flags);
b7a6ec52 162}
1da177e4
LT
163EXPORT_SYMBOL(vfs_getattr);
164
a528d35e 165/**
da9aa5d9 166 * vfs_fstat - Get the basic attributes by file descriptor
a528d35e
DH
167 * @fd: The file descriptor referring to the file of interest
168 * @stat: The result structure to fill in.
a528d35e
DH
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 */
da9aa5d9 175int vfs_fstat(int fd, struct kstat *stat)
1da177e4 176{
8c7493aa 177 struct fd f;
da9aa5d9 178 int error;
8c7493aa
EB
179
180 f = fdget_raw(fd);
da9aa5d9
CH
181 if (!f.file)
182 return -EBADF;
183 error = vfs_getattr(&f.file->f_path, stat, STATX_BASIC_STATS, 0);
184 fdput(f);
1da177e4
LT
185 return error;
186}
1da177e4 187
1b6fe6e0
SR
188int 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
a528d35e
DH
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 *
a528d35e
DH
215 * 0 will be returned on success, and a -ve error code if unsuccessful.
216 */
1b6fe6e0 217static int vfs_statx(int dfd, struct filename *filename, int flags,
a528d35e 218 struct kstat *stat, u32 request_mask)
0112fc22 219{
2eae7a18 220 struct path path;
1b6fe6e0 221 unsigned int lookup_flags = getname_statx_lookup_flags(flags);
b3f05150 222 int error;
0112fc22 223
b3f05150 224 if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT | AT_EMPTY_PATH |
f2d077ff 225 AT_STATX_SYNC_TYPE))
a528d35e 226 return -EINVAL;
b3f05150 227
836fb7e7 228retry:
1b6fe6e0 229 error = filename_lookup(dfd, filename, lookup_flags, &path, NULL);
2eae7a18
CH
230 if (error)
231 goto out;
232
a528d35e 233 error = vfs_getattr(&path, stat, request_mask, flags);
2d985f8c 234
fa2fcf4f
MS
235 stat->mnt_id = real_mount(path.mnt)->mnt_id;
236 stat->result_mask |= STATX_MNT_ID;
2d985f8c 237
80340fe3
MS
238 if (path.mnt->mnt_root == path.dentry)
239 stat->attributes |= STATX_ATTR_MOUNT_ROOT;
240 stat->attributes_mask |= STATX_ATTR_MOUNT_ROOT;
2d985f8c
EB
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
2eae7a18 250 path_put(&path);
836fb7e7
JL
251 if (retry_estale(error, lookup_flags)) {
252 lookup_flags |= LOOKUP_REVAL;
253 goto retry;
254 }
0112fc22
OD
255out:
256 return error;
257}
2eae7a18 258
09f1bde4
CH
259int vfs_fstatat(int dfd, const char __user *filename,
260 struct kstat *stat, int flags)
261{
1b6fe6e0
SR
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;
09f1bde4 271}
0112fc22 272
1da177e4
LT
273#ifdef __ARCH_WANT_OLD_STAT
274
275/*
276 * For backward compatibility? Maybe this should be moved
277 * into arch/i386 instead?
278 */
279static 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;
a528d35e 283
1da177e4
LT
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;
afefdbb2
DH
296 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
297 return -EOVERFLOW;
1da177e4
LT
298 tmp.st_mode = stat->mode;
299 tmp.st_nlink = stat->nlink;
300 if (tmp.st_nlink != stat->nlink)
301 return -EOVERFLOW;
a7c1938e
EB
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));
1da177e4
LT
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;
a528d35e 308#endif
1da177e4
LT
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
c7887325
DH
316SYSCALL_DEFINE2(stat, const char __user *, filename,
317 struct __old_kernel_stat __user *, statbuf)
1da177e4
LT
318{
319 struct kstat stat;
2eae7a18 320 int error;
1da177e4 321
2eae7a18
CH
322 error = vfs_stat(filename, &stat);
323 if (error)
324 return error;
1da177e4 325
2eae7a18 326 return cp_old_stat(&stat, statbuf);
1da177e4 327}
257ac264 328
c7887325
DH
329SYSCALL_DEFINE2(lstat, const char __user *, filename,
330 struct __old_kernel_stat __user *, statbuf)
1da177e4
LT
331{
332 struct kstat stat;
2eae7a18 333 int error;
1da177e4 334
2eae7a18
CH
335 error = vfs_lstat(filename, &stat);
336 if (error)
337 return error;
1da177e4 338
2eae7a18 339 return cp_old_stat(&stat, statbuf);
1da177e4 340}
257ac264
HC
341
342SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
1da177e4
LT
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
82b355d1
AB
355#ifdef __ARCH_WANT_NEW_STAT
356
a52dd971
LT
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
8529f613
LT
363#ifndef INIT_STRUCT_STAT_PADDING
364# define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
365#endif
366
1da177e4
LT
367static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
368{
369 struct stat tmp;
370
932aba1e
MP
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))
1da177e4 374 return -EOVERFLOW;
a52dd971
LT
375#if BITS_PER_LONG == 32
376 if (stat->size > MAX_NON_LFS)
1da177e4
LT
377 return -EOVERFLOW;
378#endif
379
8529f613 380 INIT_STRUCT_STAT_PADDING(tmp);
932aba1e 381 tmp.st_dev = new_encode_dev(stat->dev);
1da177e4 382 tmp.st_ino = stat->ino;
afefdbb2
DH
383 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
384 return -EOVERFLOW;
1da177e4
LT
385 tmp.st_mode = stat->mode;
386 tmp.st_nlink = stat->nlink;
387 if (tmp.st_nlink != stat->nlink)
388 return -EOVERFLOW;
a7c1938e
EB
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));
932aba1e 391 tmp.st_rdev = new_encode_dev(stat->rdev);
1da177e4
LT
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
c7887325
DH
406SYSCALL_DEFINE2(newstat, const char __user *, filename,
407 struct stat __user *, statbuf)
5590ff0d
UD
408{
409 struct kstat stat;
2eae7a18 410 int error = vfs_stat(filename, &stat);
5590ff0d 411
2eae7a18
CH
412 if (error)
413 return error;
414 return cp_new_stat(&stat, statbuf);
5590ff0d
UD
415}
416
c7887325
DH
417SYSCALL_DEFINE2(newlstat, const char __user *, filename,
418 struct stat __user *, statbuf)
1da177e4
LT
419{
420 struct kstat stat;
2eae7a18 421 int error;
1da177e4 422
2eae7a18
CH
423 error = vfs_lstat(filename, &stat);
424 if (error)
425 return error;
1da177e4 426
2eae7a18 427 return cp_new_stat(&stat, statbuf);
1da177e4 428}
5590ff0d 429
2833c28a 430#if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
c7887325 431SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
6559eed8 432 struct stat __user *, statbuf, int, flag)
1da177e4
LT
433{
434 struct kstat stat;
0112fc22 435 int error;
1da177e4 436
0112fc22
OD
437 error = vfs_fstatat(dfd, filename, &stat, flag);
438 if (error)
439 return error;
440 return cp_new_stat(&stat, statbuf);
1da177e4 441}
cff2b760 442#endif
5590ff0d 443
257ac264 444SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
1da177e4
LT
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}
82b355d1 454#endif
1da177e4 455
2dae0248
DB
456static int do_readlinkat(int dfd, const char __user *pathname,
457 char __user *buf, int bufsiz)
1da177e4 458{
2d8f3038 459 struct path path;
1da177e4 460 int error;
1fa1e7f6 461 int empty = 0;
7955119e 462 unsigned int lookup_flags = LOOKUP_EMPTY;
1da177e4
LT
463
464 if (bufsiz <= 0)
465 return -EINVAL;
466
7955119e
JL
467retry:
468 error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
1da177e4 469 if (!error) {
bb668734 470 struct inode *inode = d_backing_inode(path.dentry);
1da177e4 471
1fa1e7f6 472 error = empty ? -ENOENT : -EINVAL;
fd4a0edf
MS
473 /*
474 * AFS mountpoints allow readlink(2) but are not symlinks
475 */
476 if (d_is_symlink(path.dentry) || inode->i_op->readlink) {
2d8f3038 477 error = security_inode_readlink(path.dentry);
1da177e4 478 if (!error) {
68ac1234 479 touch_atime(&path);
fd4a0edf 480 error = vfs_readlink(path.dentry, buf, bufsiz);
1da177e4
LT
481 }
482 }
2d8f3038 483 path_put(&path);
7955119e
JL
484 if (retry_estale(error, lookup_flags)) {
485 lookup_flags |= LOOKUP_REVAL;
486 goto retry;
487 }
1da177e4
LT
488 }
489 return error;
490}
491
2dae0248
DB
492SYSCALL_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
002c8976
HC
498SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
499 int, bufsiz)
5590ff0d 500{
2dae0248 501 return do_readlinkat(AT_FDCWD, path, buf, bufsiz);
5590ff0d
UD
502}
503
1da177e4
LT
504
505/* ---------- LFS-64 ----------- */
0753f70f 506#if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
1da177e4 507
8529f613
LT
508#ifndef INIT_STRUCT_STAT64_PADDING
509# define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
510#endif
511
1da177e4
LT
512static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
513{
514 struct stat64 tmp;
515
8529f613 516 INIT_STRUCT_STAT64_PADDING(tmp);
1da177e4
LT
517#ifdef CONFIG_MIPS
518 /* mips has weird padding, so we don't get 64 bits there */
1da177e4
LT
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;
afefdbb2
DH
526 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
527 return -EOVERFLOW;
1da177e4
LT
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;
a7c1938e
EB
533 tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
534 tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
1da177e4
LT
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
c7887325
DH
547SYSCALL_DEFINE2(stat64, const char __user *, filename,
548 struct stat64 __user *, statbuf)
1da177e4
LT
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}
257ac264 558
c7887325
DH
559SYSCALL_DEFINE2(lstat64, const char __user *, filename,
560 struct stat64 __user *, statbuf)
1da177e4
LT
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}
257ac264
HC
570
571SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
1da177e4
LT
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
c7887325 582SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
6559eed8 583 struct stat64 __user *, statbuf, int, flag)
cff2b760
UD
584{
585 struct kstat stat;
0112fc22 586 int error;
cff2b760 587
0112fc22
OD
588 error = vfs_fstatat(dfd, filename, &stat, flag);
589 if (error)
590 return error;
591 return cp_new_stat64(&stat, statbuf);
cff2b760 592}
0753f70f 593#endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
1da177e4 594
6f88cc17 595static noinline_for_stack int
64bd7204 596cp_statx(const struct kstat *stat, struct statx __user *buffer)
a528d35e 597{
64bd7204
EB
598 struct statx tmp;
599
600 memset(&tmp, 0, sizeof(tmp));
601
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;
3209f68b 612 tmp.stx_attributes_mask = stat->attributes_mask;
64bd7204
EB
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);
fa2fcf4f 625 tmp.stx_mnt_id = stat->mnt_id;
825cf206
EB
626 tmp.stx_dio_mem_align = stat->dio_mem_align;
627 tmp.stx_dio_offset_align = stat->dio_offset_align;
64bd7204
EB
628
629 return copy_to_user(buffer, &tmp, sizeof(tmp)) ? -EFAULT : 0;
a528d35e
DH
630}
631
1b6fe6e0 632int do_statx(int dfd, struct filename *filename, unsigned int flags,
0018784f
BM
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
a528d35e
DH
650/**
651 * sys_statx - System call to get enhanced stats
652 * @dfd: Base directory to pathwalk from *or* fd to stat.
1e2f82d1 653 * @filename: File to stat or "" with AT_EMPTY_PATH
a528d35e
DH
654 * @flags: AT_* flags to control pathwalk.
655 * @mask: Parts of statx struct actually required.
656 * @buffer: Result buffer.
657 *
1e2f82d1
DH
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.
a528d35e
DH
660 */
661SYSCALL_DEFINE5(statx,
662 int, dfd, const char __user *, filename, unsigned, flags,
663 unsigned int, mask,
664 struct statx __user *, buffer)
665{
1b6fe6e0
SR
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;
a528d35e
DH
674}
675
f18ed30d 676#if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_STAT)
ac565de3
AV
677static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
678{
679 struct compat_stat tmp;
680
932aba1e
MP
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))
ac565de3
AV
684 return -EOVERFLOW;
685
686 memset(&tmp, 0, sizeof(tmp));
932aba1e 687 tmp.st_dev = new_encode_dev(stat->dev);
ac565de3
AV
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));
932aba1e 697 tmp.st_rdev = new_encode_dev(stat->rdev);
ac565de3
AV
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
712COMPAT_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
724COMPAT_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
737COMPAT_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
751COMPAT_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
b462707e
DM
763/* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
764void __inode_add_bytes(struct inode *inode, loff_t bytes)
1da177e4 765{
1da177e4
LT
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 }
b462707e 773}
eb315d2a 774EXPORT_SYMBOL(__inode_add_bytes);
b462707e
DM
775
776void inode_add_bytes(struct inode *inode, loff_t bytes)
777{
778 spin_lock(&inode->i_lock);
779 __inode_add_bytes(inode, bytes);
1da177e4
LT
780 spin_unlock(&inode->i_lock);
781}
782
783EXPORT_SYMBOL(inode_add_bytes);
784
1c8924eb 785void __inode_sub_bytes(struct inode *inode, loff_t bytes)
1da177e4 786{
1da177e4
LT
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;
1c8924eb
JK
794}
795
796EXPORT_SYMBOL(__inode_sub_bytes);
797
798void inode_sub_bytes(struct inode *inode, loff_t bytes)
799{
800 spin_lock(&inode->i_lock);
801 __inode_sub_bytes(inode, bytes);
1da177e4
LT
802 spin_unlock(&inode->i_lock);
803}
804
805EXPORT_SYMBOL(inode_sub_bytes);
806
807loff_t inode_get_bytes(struct inode *inode)
808{
809 loff_t ret;
810
811 spin_lock(&inode->i_lock);
f4a8116a 812 ret = __inode_get_bytes(inode);
1da177e4
LT
813 spin_unlock(&inode->i_lock);
814 return ret;
815}
816
817EXPORT_SYMBOL(inode_get_bytes);
818
819void 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
827EXPORT_SYMBOL(inode_set_bytes);