xref: /openbmc/linux/fs/stat.c (revision 06267eb2)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/stat.c
4  *
5  *  Copyright (C) 1991, 1992  Linus Torvalds
6  */
7 
8 #include <linux/blkdev.h>
9 #include <linux/export.h>
10 #include <linux/mm.h>
11 #include <linux/errno.h>
12 #include <linux/file.h>
13 #include <linux/highuid.h>
14 #include <linux/fs.h>
15 #include <linux/namei.h>
16 #include <linux/security.h>
17 #include <linux/cred.h>
18 #include <linux/syscalls.h>
19 #include <linux/pagemap.h>
20 #include <linux/compat.h>
21 
22 #include <linux/uaccess.h>
23 #include <asm/unistd.h>
24 
25 #include "internal.h"
26 #include "mount.h"
27 
28 /**
29  * generic_fillattr - Fill in the basic attributes from the inode struct
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
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.
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.
43  */
44 void generic_fillattr(struct user_namespace *mnt_userns, struct inode *inode,
45 		      struct kstat *stat)
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;
51 	stat->uid = i_uid_into_mnt(mnt_userns, inode);
52 	stat->gid = i_gid_into_mnt(mnt_userns, inode);
53 	stat->rdev = inode->i_rdev;
54 	stat->size = i_size_read(inode);
55 	stat->atime = inode->i_atime;
56 	stat->mtime = inode->i_mtime;
57 	stat->ctime = inode->i_ctime;
58 	stat->blksize = i_blocksize(inode);
59 	stat->blocks = inode->i_blocks;
60 }
61 EXPORT_SYMBOL(generic_fillattr);
62 
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  */
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 /**
82  * vfs_getattr_nosec - getattr without security checks
83  * @path: file to get attributes from
84  * @stat: structure to return attributes in
85  * @request_mask: STATX_xxx flags indicating what the caller wants
86  * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
87  *
88  * Get attributes without calling security_inode_getattr.
89  *
90  * Currently the only caller other than vfs_getattr is internal to the
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.
93  */
94 int vfs_getattr_nosec(const struct path *path, struct kstat *stat,
95 		      u32 request_mask, unsigned int query_flags)
96 {
97 	struct user_namespace *mnt_userns;
98 	struct inode *inode = d_backing_inode(path->dentry);
99 
100 	memset(stat, 0, sizeof(*stat));
101 	stat->result_mask |= STATX_BASIC_STATS;
102 	query_flags &= AT_STATX_SYNC_TYPE;
103 
104 	/* allow the fs to override these if it really wants to */
105 	/* SB_NOATIME means filesystem supplies dummy atime value */
106 	if (inode->i_sb->s_flags & SB_NOATIME)
107 		stat->result_mask &= ~STATX_ATIME;
108 
109 	/*
110 	 * Note: If you add another clause to set an attribute flag, please
111 	 * update attributes_mask below.
112 	 */
113 	if (IS_AUTOMOUNT(inode))
114 		stat->attributes |= STATX_ATTR_AUTOMOUNT;
115 
116 	if (IS_DAX(inode))
117 		stat->attributes |= STATX_ATTR_DAX;
118 
119 	stat->attributes_mask |= (STATX_ATTR_AUTOMOUNT |
120 				  STATX_ATTR_DAX);
121 
122 	mnt_userns = mnt_user_ns(path->mnt);
123 	if (inode->i_op->getattr)
124 		return inode->i_op->getattr(mnt_userns, path, stat,
125 					    request_mask, query_flags);
126 
127 	generic_fillattr(mnt_userns, inode, stat);
128 	return 0;
129 }
130 EXPORT_SYMBOL(vfs_getattr_nosec);
131 
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
137  * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
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)
155 {
156 	int retval;
157 
158 	retval = security_inode_getattr(path);
159 	if (retval)
160 		return retval;
161 	return vfs_getattr_nosec(path, stat, request_mask, query_flags);
162 }
163 EXPORT_SYMBOL(vfs_getattr);
164 
165 /**
166  * vfs_fstat - Get the basic attributes by file descriptor
167  * @fd: The file descriptor referring to the file of interest
168  * @stat: The result structure to fill in.
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  */
175 int vfs_fstat(int fd, struct kstat *stat)
176 {
177 	struct fd f;
178 	int error;
179 
180 	f = fdget_raw(fd);
181 	if (!f.file)
182 		return -EBADF;
183 	error = vfs_getattr(&f.file->f_path, stat, STATX_BASIC_STATS, 0);
184 	fdput(f);
185 	return error;
186 }
187 
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 
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  *
215  * 0 will be returned on success, and a -ve error code if unsuccessful.
216  */
217 static int vfs_statx(int dfd, struct filename *filename, int flags,
218 	      struct kstat *stat, u32 request_mask)
219 {
220 	struct path path;
221 	unsigned int lookup_flags = getname_statx_lookup_flags(flags);
222 	int error;
223 
224 	if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT | AT_EMPTY_PATH |
225 		      AT_STATX_SYNC_TYPE))
226 		return -EINVAL;
227 
228 retry:
229 	error = filename_lookup(dfd, filename, lookup_flags, &path, NULL);
230 	if (error)
231 		goto out;
232 
233 	error = vfs_getattr(&path, stat, request_mask, flags);
234 
235 	stat->mnt_id = real_mount(path.mnt)->mnt_id;
236 	stat->result_mask |= STATX_MNT_ID;
237 
238 	if (path.mnt->mnt_root == path.dentry)
239 		stat->attributes |= STATX_ATTR_MOUNT_ROOT;
240 	stat->attributes_mask |= STATX_ATTR_MOUNT_ROOT;
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 
250 	path_put(&path);
251 	if (retry_estale(error, lookup_flags)) {
252 		lookup_flags |= LOOKUP_REVAL;
253 		goto retry;
254 	}
255 out:
256 	return error;
257 }
258 
259 int vfs_fstatat(int dfd, const char __user *filename,
260 			      struct kstat *stat, int flags)
261 {
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;
271 }
272 
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;
283 
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;
296 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
297 		return -EOVERFLOW;
298 	tmp.st_mode = stat->mode;
299 	tmp.st_nlink = stat->nlink;
300 	if (tmp.st_nlink != stat->nlink)
301 		return -EOVERFLOW;
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));
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;
308 #endif
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 
316 SYSCALL_DEFINE2(stat, const char __user *, filename,
317 		struct __old_kernel_stat __user *, statbuf)
318 {
319 	struct kstat stat;
320 	int error;
321 
322 	error = vfs_stat(filename, &stat);
323 	if (error)
324 		return error;
325 
326 	return cp_old_stat(&stat, statbuf);
327 }
328 
329 SYSCALL_DEFINE2(lstat, const char __user *, filename,
330 		struct __old_kernel_stat __user *, statbuf)
331 {
332 	struct kstat stat;
333 	int error;
334 
335 	error = vfs_lstat(filename, &stat);
336 	if (error)
337 		return error;
338 
339 	return cp_old_stat(&stat, statbuf);
340 }
341 
342 SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
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 
355 #ifdef __ARCH_WANT_NEW_STAT
356 
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 
363 #ifndef INIT_STRUCT_STAT_PADDING
364 #  define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
365 #endif
366 
367 static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
368 {
369 	struct stat tmp;
370 
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))
374 		return -EOVERFLOW;
375 #if BITS_PER_LONG == 32
376 	if (stat->size > MAX_NON_LFS)
377 		return -EOVERFLOW;
378 #endif
379 
380 	INIT_STRUCT_STAT_PADDING(tmp);
381 	tmp.st_dev = new_encode_dev(stat->dev);
382 	tmp.st_ino = stat->ino;
383 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
384 		return -EOVERFLOW;
385 	tmp.st_mode = stat->mode;
386 	tmp.st_nlink = stat->nlink;
387 	if (tmp.st_nlink != stat->nlink)
388 		return -EOVERFLOW;
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));
391 	tmp.st_rdev = new_encode_dev(stat->rdev);
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 
406 SYSCALL_DEFINE2(newstat, const char __user *, filename,
407 		struct stat __user *, statbuf)
408 {
409 	struct kstat stat;
410 	int error = vfs_stat(filename, &stat);
411 
412 	if (error)
413 		return error;
414 	return cp_new_stat(&stat, statbuf);
415 }
416 
417 SYSCALL_DEFINE2(newlstat, const char __user *, filename,
418 		struct stat __user *, statbuf)
419 {
420 	struct kstat stat;
421 	int error;
422 
423 	error = vfs_lstat(filename, &stat);
424 	if (error)
425 		return error;
426 
427 	return cp_new_stat(&stat, statbuf);
428 }
429 
430 #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
431 SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
432 		struct stat __user *, statbuf, int, flag)
433 {
434 	struct kstat stat;
435 	int error;
436 
437 	error = vfs_fstatat(dfd, filename, &stat, flag);
438 	if (error)
439 		return error;
440 	return cp_new_stat(&stat, statbuf);
441 }
442 #endif
443 
444 SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
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 }
454 #endif
455 
456 static int do_readlinkat(int dfd, const char __user *pathname,
457 			 char __user *buf, int bufsiz)
458 {
459 	struct path path;
460 	int error;
461 	int empty = 0;
462 	unsigned int lookup_flags = LOOKUP_EMPTY;
463 
464 	if (bufsiz <= 0)
465 		return -EINVAL;
466 
467 retry:
468 	error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
469 	if (!error) {
470 		struct inode *inode = d_backing_inode(path.dentry);
471 
472 		error = empty ? -ENOENT : -EINVAL;
473 		/*
474 		 * AFS mountpoints allow readlink(2) but are not symlinks
475 		 */
476 		if (d_is_symlink(path.dentry) || inode->i_op->readlink) {
477 			error = security_inode_readlink(path.dentry);
478 			if (!error) {
479 				touch_atime(&path);
480 				error = vfs_readlink(path.dentry, buf, bufsiz);
481 			}
482 		}
483 		path_put(&path);
484 		if (retry_estale(error, lookup_flags)) {
485 			lookup_flags |= LOOKUP_REVAL;
486 			goto retry;
487 		}
488 	}
489 	return error;
490 }
491 
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 
498 SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
499 		int, bufsiz)
500 {
501 	return do_readlinkat(AT_FDCWD, path, buf, bufsiz);
502 }
503 
504 
505 /* ---------- LFS-64 ----------- */
506 #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
507 
508 #ifndef INIT_STRUCT_STAT64_PADDING
509 #  define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
510 #endif
511 
512 static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
513 {
514 	struct stat64 tmp;
515 
516 	INIT_STRUCT_STAT64_PADDING(tmp);
517 #ifdef CONFIG_MIPS
518 	/* mips has weird padding, so we don't get 64 bits there */
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;
526 	if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
527 		return -EOVERFLOW;
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;
533 	tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
534 	tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
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 
547 SYSCALL_DEFINE2(stat64, const char __user *, filename,
548 		struct stat64 __user *, statbuf)
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 }
558 
559 SYSCALL_DEFINE2(lstat64, const char __user *, filename,
560 		struct stat64 __user *, statbuf)
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 }
570 
571 SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
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 
582 SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
583 		struct stat64 __user *, statbuf, int, flag)
584 {
585 	struct kstat stat;
586 	int error;
587 
588 	error = vfs_fstatat(dfd, filename, &stat, flag);
589 	if (error)
590 		return error;
591 	return cp_new_stat64(&stat, statbuf);
592 }
593 #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
594 
595 static noinline_for_stack int
596 cp_statx(const struct kstat *stat, struct statx __user *buffer)
597 {
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;
612 	tmp.stx_attributes_mask = stat->attributes_mask;
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);
625 	tmp.stx_mnt_id = stat->mnt_id;
626 	tmp.stx_dio_mem_align = stat->dio_mem_align;
627 	tmp.stx_dio_offset_align = stat->dio_offset_align;
628 
629 	return copy_to_user(buffer, &tmp, sizeof(tmp)) ? -EFAULT : 0;
630 }
631 
632 int do_statx(int dfd, struct filename *filename, unsigned int flags,
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 
650 /**
651  * sys_statx - System call to get enhanced stats
652  * @dfd: Base directory to pathwalk from *or* fd to stat.
653  * @filename: File to stat or "" with AT_EMPTY_PATH
654  * @flags: AT_* flags to control pathwalk.
655  * @mask: Parts of statx struct actually required.
656  * @buffer: Result buffer.
657  *
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.
660  */
661 SYSCALL_DEFINE5(statx,
662 		int, dfd, const char __user *, filename, unsigned, flags,
663 		unsigned int, mask,
664 		struct statx __user *, buffer)
665 {
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;
674 }
675 
676 #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_STAT)
677 static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
678 {
679 	struct compat_stat tmp;
680 
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))
684 		return -EOVERFLOW;
685 
686 	memset(&tmp, 0, sizeof(tmp));
687 	tmp.st_dev = new_encode_dev(stat->dev);
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));
697 	tmp.st_rdev = new_encode_dev(stat->rdev);
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 
763 /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
764 void __inode_add_bytes(struct inode *inode, loff_t bytes)
765 {
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 	}
773 }
774 EXPORT_SYMBOL(__inode_add_bytes);
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);
780 	spin_unlock(&inode->i_lock);
781 }
782 
783 EXPORT_SYMBOL(inode_add_bytes);
784 
785 void __inode_sub_bytes(struct inode *inode, loff_t bytes)
786 {
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;
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);
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);
812 	ret = __inode_get_bytes(inode);
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);
828