1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992 Linus Torvalds
8 #include <linux/blkdev.h>
9 #include <linux/export.h>
11 #include <linux/errno.h>
12 #include <linux/file.h>
13 #include <linux/highuid.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 #include <linux/iversion.h>
23 #include <linux/uaccess.h>
24 #include <asm/unistd.h>
30 * generic_fillattr - Fill in the basic attributes from the inode struct
31 * @idmap: idmap of the mount the inode was found from
32 * @request_mask: statx request_mask
33 * @inode: Inode to use as the source
34 * @stat: Where to fill in the attributes
36 * Fill in the basic attributes in the kstat structure from data that's to be
37 * found on the VFS inode structure. This is the default if no getattr inode
38 * operation is supplied.
40 * If the inode has been found through an idmapped mount the idmap of
41 * the vfsmount must be passed through @idmap. This function will then
42 * take care to map the inode according to @idmap before filling in the
43 * uid and gid filds. On non-idmapped mounts or if permission checking is to be
44 * performed on the raw inode simply pass @nop_mnt_idmap.
46 void generic_fillattr(struct mnt_idmap *idmap, u32 request_mask,
47 struct inode *inode, struct kstat *stat)
49 vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode);
50 vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode);
52 stat->dev = inode->i_sb->s_dev;
53 stat->ino = inode->i_ino;
54 stat->mode = inode->i_mode;
55 stat->nlink = inode->i_nlink;
56 stat->uid = vfsuid_into_kuid(vfsuid);
57 stat->gid = vfsgid_into_kgid(vfsgid);
58 stat->rdev = inode->i_rdev;
59 stat->size = i_size_read(inode);
60 stat->atime = inode_get_atime(inode);
61 stat->mtime = inode_get_mtime(inode);
62 stat->ctime = inode_get_ctime(inode);
63 stat->blksize = i_blocksize(inode);
64 stat->blocks = inode->i_blocks;
66 if ((request_mask & STATX_CHANGE_COOKIE) && IS_I_VERSION(inode)) {
67 stat->result_mask |= STATX_CHANGE_COOKIE;
68 stat->change_cookie = inode_query_iversion(inode);
72 EXPORT_SYMBOL(generic_fillattr);
75 * generic_fill_statx_attr - Fill in the statx attributes from the inode flags
76 * @inode: Inode to use as the source
77 * @stat: Where to fill in the attribute flags
79 * Fill in the STATX_ATTR_* flags in the kstat structure for properties of the
80 * inode that are published on i_flags and enforced by the VFS.
82 void generic_fill_statx_attr(struct inode *inode, struct kstat *stat)
84 if (inode->i_flags & S_IMMUTABLE)
85 stat->attributes |= STATX_ATTR_IMMUTABLE;
86 if (inode->i_flags & S_APPEND)
87 stat->attributes |= STATX_ATTR_APPEND;
88 stat->attributes_mask |= KSTAT_ATTR_VFS_FLAGS;
90 EXPORT_SYMBOL(generic_fill_statx_attr);
93 * generic_fill_statx_atomic_writes - Fill in atomic writes statx attributes
94 * @stat: Where to fill in the attribute flags
95 * @unit_min: Minimum supported atomic write length in bytes
96 * @unit_max: Maximum supported atomic write length in bytes
98 * Fill in the STATX{_ATTR}_WRITE_ATOMIC flags in the kstat structure from
99 * atomic write unit_min and unit_max values.
101 void generic_fill_statx_atomic_writes(struct kstat *stat,
102 unsigned int unit_min,
103 unsigned int unit_max)
105 /* Confirm that the request type is known */
106 stat->result_mask |= STATX_WRITE_ATOMIC;
108 /* Confirm that the file attribute type is known */
109 stat->attributes_mask |= STATX_ATTR_WRITE_ATOMIC;
112 stat->atomic_write_unit_min = unit_min;
113 stat->atomic_write_unit_max = unit_max;
114 /* Initially only allow 1x segment */
115 stat->atomic_write_segments_max = 1;
117 /* Confirm atomic writes are actually supported */
118 stat->attributes |= STATX_ATTR_WRITE_ATOMIC;
121 EXPORT_SYMBOL_GPL(generic_fill_statx_atomic_writes);
124 * vfs_getattr_nosec - getattr without security checks
125 * @path: file to get attributes from
126 * @stat: structure to return attributes in
127 * @request_mask: STATX_xxx flags indicating what the caller wants
128 * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
130 * Get attributes without calling security_inode_getattr.
132 * Currently the only caller other than vfs_getattr is internal to the
133 * filehandle lookup code, which uses only the inode number and returns no
134 * attributes to any user. Any other code probably wants vfs_getattr.
136 int vfs_getattr_nosec(const struct path *path, struct kstat *stat,
137 u32 request_mask, unsigned int query_flags)
139 struct mnt_idmap *idmap;
140 struct inode *inode = d_backing_inode(path->dentry);
142 memset(stat, 0, sizeof(*stat));
143 stat->result_mask |= STATX_BASIC_STATS;
144 query_flags &= AT_STATX_SYNC_TYPE;
146 /* allow the fs to override these if it really wants to */
147 /* SB_NOATIME means filesystem supplies dummy atime value */
148 if (inode->i_sb->s_flags & SB_NOATIME)
149 stat->result_mask &= ~STATX_ATIME;
152 * Note: If you add another clause to set an attribute flag, please
153 * update attributes_mask below.
155 if (IS_AUTOMOUNT(inode))
156 stat->attributes |= STATX_ATTR_AUTOMOUNT;
159 stat->attributes |= STATX_ATTR_DAX;
161 stat->attributes_mask |= (STATX_ATTR_AUTOMOUNT |
164 idmap = mnt_idmap(path->mnt);
165 if (inode->i_op->getattr)
166 return inode->i_op->getattr(idmap, path, stat,
170 generic_fillattr(idmap, request_mask, inode, stat);
173 EXPORT_SYMBOL(vfs_getattr_nosec);
176 * vfs_getattr - Get the enhanced basic attributes of a file
177 * @path: The file of interest
178 * @stat: Where to return the statistics
179 * @request_mask: STATX_xxx flags indicating what the caller wants
180 * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
182 * Ask the filesystem for a file's attributes. The caller must indicate in
183 * request_mask and query_flags to indicate what they want.
185 * If the file is remote, the filesystem can be forced to update the attributes
186 * from the backing store by passing AT_STATX_FORCE_SYNC in query_flags or can
187 * suppress the update by passing AT_STATX_DONT_SYNC.
189 * Bits must have been set in request_mask to indicate which attributes the
190 * caller wants retrieving. Any such attribute not requested may be returned
191 * anyway, but the value may be approximate, and, if remote, may not have been
192 * synchronised with the server.
194 * 0 will be returned on success, and a -ve error code if unsuccessful.
196 int vfs_getattr(const struct path *path, struct kstat *stat,
197 u32 request_mask, unsigned int query_flags)
201 retval = security_inode_getattr(path);
204 return vfs_getattr_nosec(path, stat, request_mask, query_flags);
206 EXPORT_SYMBOL(vfs_getattr);
209 * vfs_fstat - Get the basic attributes by file descriptor
210 * @fd: The file descriptor referring to the file of interest
211 * @stat: The result structure to fill in.
213 * This function is a wrapper around vfs_getattr(). The main difference is
214 * that it uses a file descriptor to determine the file location.
216 * 0 will be returned on success, and a -ve error code if unsuccessful.
218 int vfs_fstat(int fd, struct kstat *stat)
220 CLASS(fd_raw, f)(fd);
223 return vfs_getattr(&fd_file(f)->f_path, stat, STATX_BASIC_STATS, 0);
226 static int statx_lookup_flags(int flags)
228 int lookup_flags = 0;
230 if (!(flags & AT_SYMLINK_NOFOLLOW))
231 lookup_flags |= LOOKUP_FOLLOW;
232 if (!(flags & AT_NO_AUTOMOUNT))
233 lookup_flags |= LOOKUP_AUTOMOUNT;
238 static int vfs_statx_path(struct path *path, int flags, struct kstat *stat,
241 int error = vfs_getattr(path, stat, request_mask, flags);
243 if (request_mask & STATX_MNT_ID_UNIQUE) {
244 stat->mnt_id = real_mount(path->mnt)->mnt_id_unique;
245 stat->result_mask |= STATX_MNT_ID_UNIQUE;
247 stat->mnt_id = real_mount(path->mnt)->mnt_id;
248 stat->result_mask |= STATX_MNT_ID;
251 if (path_mounted(path))
252 stat->attributes |= STATX_ATTR_MOUNT_ROOT;
253 stat->attributes_mask |= STATX_ATTR_MOUNT_ROOT;
256 * If this is a block device inode, override the filesystem
257 * attributes with the block device specific parameters that need to be
258 * obtained from the bdev backing inode.
260 if (S_ISBLK(stat->mode))
261 bdev_statx(path, stat, request_mask);
266 static int vfs_statx_fd(int fd, int flags, struct kstat *stat,
269 CLASS(fd_raw, f)(fd);
272 return vfs_statx_path(&fd_file(f)->f_path, flags, stat, request_mask);
276 * vfs_statx - Get basic and extra attributes by filename
277 * @dfd: A file descriptor representing the base dir for a relative filename
278 * @filename: The name of the file of interest
279 * @flags: Flags to control the query
280 * @stat: The result structure to fill in.
281 * @request_mask: STATX_xxx flags indicating what the caller wants
283 * This function is a wrapper around vfs_getattr(). The main difference is
284 * that it uses a filename and base directory to determine the file location.
285 * Additionally, the use of AT_SYMLINK_NOFOLLOW in flags will prevent a symlink
286 * at the given name from being referenced.
288 * 0 will be returned on success, and a -ve error code if unsuccessful.
290 static int vfs_statx(int dfd, struct filename *filename, int flags,
291 struct kstat *stat, u32 request_mask)
294 unsigned int lookup_flags = statx_lookup_flags(flags);
297 if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT | AT_EMPTY_PATH |
302 error = filename_lookup(dfd, filename, lookup_flags, &path, NULL);
305 error = vfs_statx_path(&path, flags, stat, request_mask);
307 if (retry_estale(error, lookup_flags)) {
308 lookup_flags |= LOOKUP_REVAL;
314 int vfs_fstatat(int dfd, const char __user *filename,
315 struct kstat *stat, int flags)
318 int statx_flags = flags | AT_NO_AUTOMOUNT;
319 struct filename *name = getname_maybe_null(filename, flags);
321 if (!name && dfd >= 0)
322 return vfs_fstat(dfd, stat);
324 ret = vfs_statx(dfd, name, statx_flags, stat, STATX_BASIC_STATS);
330 #ifdef __ARCH_WANT_OLD_STAT
333 * For backward compatibility? Maybe this should be moved
334 * into arch/i386 instead?
336 static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
338 static int warncount = 5;
339 struct __old_kernel_stat tmp;
343 printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
345 } else if (warncount < 0) {
346 /* it's laughable, but... */
350 memset(&tmp, 0, sizeof(struct __old_kernel_stat));
351 tmp.st_dev = old_encode_dev(stat->dev);
352 tmp.st_ino = stat->ino;
353 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
355 tmp.st_mode = stat->mode;
356 tmp.st_nlink = stat->nlink;
357 if (tmp.st_nlink != stat->nlink)
359 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
360 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
361 tmp.st_rdev = old_encode_dev(stat->rdev);
362 #if BITS_PER_LONG == 32
363 if (stat->size > MAX_NON_LFS)
366 tmp.st_size = stat->size;
367 tmp.st_atime = stat->atime.tv_sec;
368 tmp.st_mtime = stat->mtime.tv_sec;
369 tmp.st_ctime = stat->ctime.tv_sec;
370 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
373 SYSCALL_DEFINE2(stat, const char __user *, filename,
374 struct __old_kernel_stat __user *, statbuf)
379 error = vfs_stat(filename, &stat);
383 return cp_old_stat(&stat, statbuf);
386 SYSCALL_DEFINE2(lstat, const char __user *, filename,
387 struct __old_kernel_stat __user *, statbuf)
392 error = vfs_lstat(filename, &stat);
396 return cp_old_stat(&stat, statbuf);
399 SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
402 int error = vfs_fstat(fd, &stat);
405 error = cp_old_stat(&stat, statbuf);
410 #endif /* __ARCH_WANT_OLD_STAT */
412 #ifdef __ARCH_WANT_NEW_STAT
414 #ifndef INIT_STRUCT_STAT_PADDING
415 # define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
418 static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
422 if (sizeof(tmp.st_dev) < 4 && !old_valid_dev(stat->dev))
424 if (sizeof(tmp.st_rdev) < 4 && !old_valid_dev(stat->rdev))
426 #if BITS_PER_LONG == 32
427 if (stat->size > MAX_NON_LFS)
431 INIT_STRUCT_STAT_PADDING(tmp);
432 tmp.st_dev = new_encode_dev(stat->dev);
433 tmp.st_ino = stat->ino;
434 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
436 tmp.st_mode = stat->mode;
437 tmp.st_nlink = stat->nlink;
438 if (tmp.st_nlink != stat->nlink)
440 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
441 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
442 tmp.st_rdev = new_encode_dev(stat->rdev);
443 tmp.st_size = stat->size;
444 tmp.st_atime = stat->atime.tv_sec;
445 tmp.st_mtime = stat->mtime.tv_sec;
446 tmp.st_ctime = stat->ctime.tv_sec;
447 #ifdef STAT_HAVE_NSEC
448 tmp.st_atime_nsec = stat->atime.tv_nsec;
449 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
450 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
452 tmp.st_blocks = stat->blocks;
453 tmp.st_blksize = stat->blksize;
454 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
457 SYSCALL_DEFINE2(newstat, const char __user *, filename,
458 struct stat __user *, statbuf)
461 int error = vfs_stat(filename, &stat);
465 return cp_new_stat(&stat, statbuf);
468 SYSCALL_DEFINE2(newlstat, const char __user *, filename,
469 struct stat __user *, statbuf)
474 error = vfs_lstat(filename, &stat);
478 return cp_new_stat(&stat, statbuf);
481 #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
482 SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
483 struct stat __user *, statbuf, int, flag)
488 error = vfs_fstatat(dfd, filename, &stat, flag);
491 return cp_new_stat(&stat, statbuf);
495 SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
498 int error = vfs_fstat(fd, &stat);
501 error = cp_new_stat(&stat, statbuf);
507 static int do_readlinkat(int dfd, const char __user *pathname,
508 char __user *buf, int bufsiz)
511 struct filename *name;
513 unsigned int lookup_flags = LOOKUP_EMPTY;
519 name = getname_flags(pathname, lookup_flags);
520 error = filename_lookup(dfd, name, lookup_flags, &path, NULL);
521 if (unlikely(error)) {
527 * AFS mountpoints allow readlink(2) but are not symlinks
529 if (d_is_symlink(path.dentry) ||
530 d_backing_inode(path.dentry)->i_op->readlink) {
531 error = security_inode_readlink(path.dentry);
534 error = vfs_readlink(path.dentry, buf, bufsiz);
537 error = (name->name[0] == '\0') ? -ENOENT : -EINVAL;
541 if (retry_estale(error, lookup_flags)) {
542 lookup_flags |= LOOKUP_REVAL;
548 SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
549 char __user *, buf, int, bufsiz)
551 return do_readlinkat(dfd, pathname, buf, bufsiz);
554 SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
557 return do_readlinkat(AT_FDCWD, path, buf, bufsiz);
561 /* ---------- LFS-64 ----------- */
562 #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
564 #ifndef INIT_STRUCT_STAT64_PADDING
565 # define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
568 static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
572 INIT_STRUCT_STAT64_PADDING(tmp);
574 /* mips has weird padding, so we don't get 64 bits there */
575 tmp.st_dev = new_encode_dev(stat->dev);
576 tmp.st_rdev = new_encode_dev(stat->rdev);
578 tmp.st_dev = huge_encode_dev(stat->dev);
579 tmp.st_rdev = huge_encode_dev(stat->rdev);
581 tmp.st_ino = stat->ino;
582 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
584 #ifdef STAT64_HAS_BROKEN_ST_INO
585 tmp.__st_ino = stat->ino;
587 tmp.st_mode = stat->mode;
588 tmp.st_nlink = stat->nlink;
589 tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
590 tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
591 tmp.st_atime = stat->atime.tv_sec;
592 tmp.st_atime_nsec = stat->atime.tv_nsec;
593 tmp.st_mtime = stat->mtime.tv_sec;
594 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
595 tmp.st_ctime = stat->ctime.tv_sec;
596 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
597 tmp.st_size = stat->size;
598 tmp.st_blocks = stat->blocks;
599 tmp.st_blksize = stat->blksize;
600 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
603 SYSCALL_DEFINE2(stat64, const char __user *, filename,
604 struct stat64 __user *, statbuf)
607 int error = vfs_stat(filename, &stat);
610 error = cp_new_stat64(&stat, statbuf);
615 SYSCALL_DEFINE2(lstat64, const char __user *, filename,
616 struct stat64 __user *, statbuf)
619 int error = vfs_lstat(filename, &stat);
622 error = cp_new_stat64(&stat, statbuf);
627 SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
630 int error = vfs_fstat(fd, &stat);
633 error = cp_new_stat64(&stat, statbuf);
638 SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
639 struct stat64 __user *, statbuf, int, flag)
644 error = vfs_fstatat(dfd, filename, &stat, flag);
647 return cp_new_stat64(&stat, statbuf);
649 #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
651 static noinline_for_stack int
652 cp_statx(const struct kstat *stat, struct statx __user *buffer)
656 memset(&tmp, 0, sizeof(tmp));
658 /* STATX_CHANGE_COOKIE is kernel-only for now */
659 tmp.stx_mask = stat->result_mask & ~STATX_CHANGE_COOKIE;
660 tmp.stx_blksize = stat->blksize;
661 /* STATX_ATTR_CHANGE_MONOTONIC is kernel-only for now */
662 tmp.stx_attributes = stat->attributes & ~STATX_ATTR_CHANGE_MONOTONIC;
663 tmp.stx_nlink = stat->nlink;
664 tmp.stx_uid = from_kuid_munged(current_user_ns(), stat->uid);
665 tmp.stx_gid = from_kgid_munged(current_user_ns(), stat->gid);
666 tmp.stx_mode = stat->mode;
667 tmp.stx_ino = stat->ino;
668 tmp.stx_size = stat->size;
669 tmp.stx_blocks = stat->blocks;
670 tmp.stx_attributes_mask = stat->attributes_mask;
671 tmp.stx_atime.tv_sec = stat->atime.tv_sec;
672 tmp.stx_atime.tv_nsec = stat->atime.tv_nsec;
673 tmp.stx_btime.tv_sec = stat->btime.tv_sec;
674 tmp.stx_btime.tv_nsec = stat->btime.tv_nsec;
675 tmp.stx_ctime.tv_sec = stat->ctime.tv_sec;
676 tmp.stx_ctime.tv_nsec = stat->ctime.tv_nsec;
677 tmp.stx_mtime.tv_sec = stat->mtime.tv_sec;
678 tmp.stx_mtime.tv_nsec = stat->mtime.tv_nsec;
679 tmp.stx_rdev_major = MAJOR(stat->rdev);
680 tmp.stx_rdev_minor = MINOR(stat->rdev);
681 tmp.stx_dev_major = MAJOR(stat->dev);
682 tmp.stx_dev_minor = MINOR(stat->dev);
683 tmp.stx_mnt_id = stat->mnt_id;
684 tmp.stx_dio_mem_align = stat->dio_mem_align;
685 tmp.stx_dio_offset_align = stat->dio_offset_align;
686 tmp.stx_subvol = stat->subvol;
687 tmp.stx_atomic_write_unit_min = stat->atomic_write_unit_min;
688 tmp.stx_atomic_write_unit_max = stat->atomic_write_unit_max;
689 tmp.stx_atomic_write_segments_max = stat->atomic_write_segments_max;
691 return copy_to_user(buffer, &tmp, sizeof(tmp)) ? -EFAULT : 0;
694 int do_statx(int dfd, struct filename *filename, unsigned int flags,
695 unsigned int mask, struct statx __user *buffer)
700 if (mask & STATX__RESERVED)
702 if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE)
706 * STATX_CHANGE_COOKIE is kernel-only for now. Ignore requests
709 mask &= ~STATX_CHANGE_COOKIE;
711 error = vfs_statx(dfd, filename, flags, &stat, mask);
715 return cp_statx(&stat, buffer);
718 int do_statx_fd(int fd, unsigned int flags, unsigned int mask,
719 struct statx __user *buffer)
724 if (mask & STATX__RESERVED)
726 if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE)
730 * STATX_CHANGE_COOKIE is kernel-only for now. Ignore requests
733 mask &= ~STATX_CHANGE_COOKIE;
735 error = vfs_statx_fd(fd, flags, &stat, mask);
739 return cp_statx(&stat, buffer);
743 * sys_statx - System call to get enhanced stats
744 * @dfd: Base directory to pathwalk from *or* fd to stat.
745 * @filename: File to stat or either NULL or "" with AT_EMPTY_PATH
746 * @flags: AT_* flags to control pathwalk.
747 * @mask: Parts of statx struct actually required.
748 * @buffer: Result buffer.
750 * Note that fstat() can be emulated by setting dfd to the fd of interest,
751 * supplying "" (or preferably NULL) as the filename and setting AT_EMPTY_PATH
754 SYSCALL_DEFINE5(statx,
755 int, dfd, const char __user *, filename, unsigned, flags,
757 struct statx __user *, buffer)
760 struct filename *name = getname_maybe_null(filename, flags);
762 if (!name && dfd >= 0)
763 return do_statx_fd(dfd, flags & ~AT_NO_AUTOMOUNT, mask, buffer);
765 ret = do_statx(dfd, name, flags, mask, buffer);
771 #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_STAT)
772 static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
774 struct compat_stat tmp;
776 if (sizeof(tmp.st_dev) < 4 && !old_valid_dev(stat->dev))
778 if (sizeof(tmp.st_rdev) < 4 && !old_valid_dev(stat->rdev))
781 memset(&tmp, 0, sizeof(tmp));
782 tmp.st_dev = new_encode_dev(stat->dev);
783 tmp.st_ino = stat->ino;
784 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
786 tmp.st_mode = stat->mode;
787 tmp.st_nlink = stat->nlink;
788 if (tmp.st_nlink != stat->nlink)
790 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
791 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
792 tmp.st_rdev = new_encode_dev(stat->rdev);
793 if ((u64) stat->size > MAX_NON_LFS)
795 tmp.st_size = stat->size;
796 tmp.st_atime = stat->atime.tv_sec;
797 tmp.st_atime_nsec = stat->atime.tv_nsec;
798 tmp.st_mtime = stat->mtime.tv_sec;
799 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
800 tmp.st_ctime = stat->ctime.tv_sec;
801 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
802 tmp.st_blocks = stat->blocks;
803 tmp.st_blksize = stat->blksize;
804 return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
807 COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename,
808 struct compat_stat __user *, statbuf)
813 error = vfs_stat(filename, &stat);
816 return cp_compat_stat(&stat, statbuf);
819 COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename,
820 struct compat_stat __user *, statbuf)
825 error = vfs_lstat(filename, &stat);
828 return cp_compat_stat(&stat, statbuf);
831 #ifndef __ARCH_WANT_STAT64
832 COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd,
833 const char __user *, filename,
834 struct compat_stat __user *, statbuf, int, flag)
839 error = vfs_fstatat(dfd, filename, &stat, flag);
842 return cp_compat_stat(&stat, statbuf);
846 COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd,
847 struct compat_stat __user *, statbuf)
850 int error = vfs_fstat(fd, &stat);
853 error = cp_compat_stat(&stat, statbuf);
858 /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
859 void __inode_add_bytes(struct inode *inode, loff_t bytes)
861 inode->i_blocks += bytes >> 9;
863 inode->i_bytes += bytes;
864 if (inode->i_bytes >= 512) {
866 inode->i_bytes -= 512;
869 EXPORT_SYMBOL(__inode_add_bytes);
871 void inode_add_bytes(struct inode *inode, loff_t bytes)
873 spin_lock(&inode->i_lock);
874 __inode_add_bytes(inode, bytes);
875 spin_unlock(&inode->i_lock);
878 EXPORT_SYMBOL(inode_add_bytes);
880 void __inode_sub_bytes(struct inode *inode, loff_t bytes)
882 inode->i_blocks -= bytes >> 9;
884 if (inode->i_bytes < bytes) {
886 inode->i_bytes += 512;
888 inode->i_bytes -= bytes;
891 EXPORT_SYMBOL(__inode_sub_bytes);
893 void inode_sub_bytes(struct inode *inode, loff_t bytes)
895 spin_lock(&inode->i_lock);
896 __inode_sub_bytes(inode, bytes);
897 spin_unlock(&inode->i_lock);
900 EXPORT_SYMBOL(inode_sub_bytes);
902 loff_t inode_get_bytes(struct inode *inode)
906 spin_lock(&inode->i_lock);
907 ret = __inode_get_bytes(inode);
908 spin_unlock(&inode->i_lock);
912 EXPORT_SYMBOL(inode_get_bytes);
914 void inode_set_bytes(struct inode *inode, loff_t bytes)
916 /* Caller is here responsible for sufficient locking
917 * (ie. inode->i_lock) */
918 inode->i_blocks = bytes >> 9;
919 inode->i_bytes = bytes & 511;
922 EXPORT_SYMBOL(inode_set_bytes);