1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992 Linus Torvalds
8 #include <linux/export.h>
10 #include <linux/errno.h>
11 #include <linux/file.h>
12 #include <linux/highuid.h>
14 #include <linux/namei.h>
15 #include <linux/security.h>
16 #include <linux/cred.h>
17 #include <linux/syscalls.h>
18 #include <linux/pagemap.h>
19 #include <linux/compat.h>
21 #include <linux/uaccess.h>
22 #include <asm/unistd.h>
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
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.
35 void generic_fillattr(struct inode *inode, struct kstat *stat)
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;
44 stat->size = i_size_read(inode);
45 stat->atime = inode->i_atime;
46 stat->mtime = inode->i_mtime;
47 stat->ctime = inode->i_ctime;
48 stat->blksize = i_blocksize(inode);
49 stat->blocks = inode->i_blocks;
51 EXPORT_SYMBOL(generic_fillattr);
54 * vfs_getattr_nosec - getattr without security checks
55 * @path: file to get attributes from
56 * @stat: structure to return attributes in
57 * @request_mask: STATX_xxx flags indicating what the caller wants
58 * @query_flags: Query mode (KSTAT_QUERY_FLAGS)
60 * Get attributes without calling security_inode_getattr.
62 * Currently the only caller other than vfs_getattr is internal to the
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.
66 int vfs_getattr_nosec(const struct path *path, struct kstat *stat,
67 u32 request_mask, unsigned int query_flags)
69 struct inode *inode = d_backing_inode(path->dentry);
71 memset(stat, 0, sizeof(*stat));
72 stat->result_mask |= STATX_BASIC_STATS;
73 request_mask &= STATX_ALL;
74 query_flags &= KSTAT_QUERY_FLAGS;
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;
82 if (inode->i_op->getattr)
83 return inode->i_op->getattr(path, stat, request_mask,
86 generic_fillattr(inode, stat);
89 EXPORT_SYMBOL(vfs_getattr_nosec);
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)
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.
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.
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.
110 * 0 will be returned on success, and a -ve error code if unsuccessful.
112 int vfs_getattr(const struct path *path, struct kstat *stat,
113 u32 request_mask, unsigned int query_flags)
117 retval = security_inode_getattr(path);
120 return vfs_getattr_nosec(path, stat, request_mask, query_flags);
122 EXPORT_SYMBOL(vfs_getattr);
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)
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.
134 * 0 will be returned on success, and a -ve error code if unsuccessful.
136 int vfs_statx_fd(unsigned int fd, struct kstat *stat,
137 u32 request_mask, unsigned int query_flags)
142 if (query_flags & ~KSTAT_QUERY_FLAGS)
147 error = vfs_getattr(&f.file->f_path, stat,
148 request_mask, query_flags);
153 EXPORT_SYMBOL(vfs_statx_fd);
155 inline unsigned vfs_stat_set_lookup_flags(unsigned *lookup_flags, int flags)
157 if ((flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT |
158 AT_EMPTY_PATH | KSTAT_QUERY_FLAGS)) != 0)
161 *lookup_flags = LOOKUP_FOLLOW | LOOKUP_AUTOMOUNT;
162 if (flags & AT_SYMLINK_NOFOLLOW)
163 *lookup_flags &= ~LOOKUP_FOLLOW;
164 if (flags & AT_NO_AUTOMOUNT)
165 *lookup_flags &= ~LOOKUP_AUTOMOUNT;
166 if (flags & AT_EMPTY_PATH)
167 *lookup_flags |= LOOKUP_EMPTY;
173 * vfs_statx - Get basic and extra attributes by filename
174 * @dfd: A file descriptor representing the base dir for a relative filename
175 * @filename: The name of the file of interest
176 * @flags: Flags to control the query
177 * @stat: The result structure to fill in.
178 * @request_mask: STATX_xxx flags indicating what the caller wants
180 * This function is a wrapper around vfs_getattr(). The main difference is
181 * that it uses a filename and base directory to determine the file location.
182 * Additionally, the use of AT_SYMLINK_NOFOLLOW in flags will prevent a symlink
183 * at the given name from being referenced.
185 * 0 will be returned on success, and a -ve error code if unsuccessful.
187 int vfs_statx(int dfd, const char __user *filename, int flags,
188 struct kstat *stat, u32 request_mask)
192 unsigned lookup_flags;
194 if (vfs_stat_set_lookup_flags(&lookup_flags, flags))
197 error = user_path_at(dfd, filename, lookup_flags, &path);
201 error = vfs_getattr(&path, stat, request_mask, flags);
203 if (retry_estale(error, lookup_flags)) {
204 lookup_flags |= LOOKUP_REVAL;
210 EXPORT_SYMBOL(vfs_statx);
213 #ifdef __ARCH_WANT_OLD_STAT
216 * For backward compatibility? Maybe this should be moved
217 * into arch/i386 instead?
219 static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
221 static int warncount = 5;
222 struct __old_kernel_stat tmp;
226 printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
228 } else if (warncount < 0) {
229 /* it's laughable, but... */
233 memset(&tmp, 0, sizeof(struct __old_kernel_stat));
234 tmp.st_dev = old_encode_dev(stat->dev);
235 tmp.st_ino = stat->ino;
236 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
238 tmp.st_mode = stat->mode;
239 tmp.st_nlink = stat->nlink;
240 if (tmp.st_nlink != stat->nlink)
242 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
243 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
244 tmp.st_rdev = old_encode_dev(stat->rdev);
245 #if BITS_PER_LONG == 32
246 if (stat->size > MAX_NON_LFS)
249 tmp.st_size = stat->size;
250 tmp.st_atime = stat->atime.tv_sec;
251 tmp.st_mtime = stat->mtime.tv_sec;
252 tmp.st_ctime = stat->ctime.tv_sec;
253 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
256 SYSCALL_DEFINE2(stat, const char __user *, filename,
257 struct __old_kernel_stat __user *, statbuf)
262 error = vfs_stat(filename, &stat);
266 return cp_old_stat(&stat, statbuf);
269 SYSCALL_DEFINE2(lstat, const char __user *, filename,
270 struct __old_kernel_stat __user *, statbuf)
275 error = vfs_lstat(filename, &stat);
279 return cp_old_stat(&stat, statbuf);
282 SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
285 int error = vfs_fstat(fd, &stat);
288 error = cp_old_stat(&stat, statbuf);
293 #endif /* __ARCH_WANT_OLD_STAT */
295 #ifdef __ARCH_WANT_NEW_STAT
297 #if BITS_PER_LONG == 32
298 # define choose_32_64(a,b) a
300 # define choose_32_64(a,b) b
303 #define valid_dev(x) choose_32_64(old_valid_dev(x),true)
304 #define encode_dev(x) choose_32_64(old_encode_dev,new_encode_dev)(x)
306 #ifndef INIT_STRUCT_STAT_PADDING
307 # define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
310 static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
314 if (!valid_dev(stat->dev) || !valid_dev(stat->rdev))
316 #if BITS_PER_LONG == 32
317 if (stat->size > MAX_NON_LFS)
321 INIT_STRUCT_STAT_PADDING(tmp);
322 tmp.st_dev = encode_dev(stat->dev);
323 tmp.st_ino = stat->ino;
324 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
326 tmp.st_mode = stat->mode;
327 tmp.st_nlink = stat->nlink;
328 if (tmp.st_nlink != stat->nlink)
330 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
331 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
332 tmp.st_rdev = encode_dev(stat->rdev);
333 tmp.st_size = stat->size;
334 tmp.st_atime = stat->atime.tv_sec;
335 tmp.st_mtime = stat->mtime.tv_sec;
336 tmp.st_ctime = stat->ctime.tv_sec;
337 #ifdef STAT_HAVE_NSEC
338 tmp.st_atime_nsec = stat->atime.tv_nsec;
339 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
340 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
342 tmp.st_blocks = stat->blocks;
343 tmp.st_blksize = stat->blksize;
344 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
347 SYSCALL_DEFINE2(newstat, const char __user *, filename,
348 struct stat __user *, statbuf)
351 int error = vfs_stat(filename, &stat);
355 return cp_new_stat(&stat, statbuf);
358 SYSCALL_DEFINE2(newlstat, const char __user *, filename,
359 struct stat __user *, statbuf)
364 error = vfs_lstat(filename, &stat);
368 return cp_new_stat(&stat, statbuf);
371 #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
372 SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
373 struct stat __user *, statbuf, int, flag)
378 error = vfs_fstatat(dfd, filename, &stat, flag);
381 return cp_new_stat(&stat, statbuf);
385 SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
388 int error = vfs_fstat(fd, &stat);
391 error = cp_new_stat(&stat, statbuf);
397 static int do_readlinkat(int dfd, const char __user *pathname,
398 char __user *buf, int bufsiz)
403 unsigned int lookup_flags = LOOKUP_EMPTY;
409 error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
411 struct inode *inode = d_backing_inode(path.dentry);
413 error = empty ? -ENOENT : -EINVAL;
415 * AFS mountpoints allow readlink(2) but are not symlinks
417 if (d_is_symlink(path.dentry) || inode->i_op->readlink) {
418 error = security_inode_readlink(path.dentry);
421 error = vfs_readlink(path.dentry, buf, bufsiz);
425 if (retry_estale(error, lookup_flags)) {
426 lookup_flags |= LOOKUP_REVAL;
433 SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
434 char __user *, buf, int, bufsiz)
436 return do_readlinkat(dfd, pathname, buf, bufsiz);
439 SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
442 return do_readlinkat(AT_FDCWD, path, buf, bufsiz);
446 /* ---------- LFS-64 ----------- */
447 #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
449 #ifndef INIT_STRUCT_STAT64_PADDING
450 # define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
453 static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
457 INIT_STRUCT_STAT64_PADDING(tmp);
459 /* mips has weird padding, so we don't get 64 bits there */
460 tmp.st_dev = new_encode_dev(stat->dev);
461 tmp.st_rdev = new_encode_dev(stat->rdev);
463 tmp.st_dev = huge_encode_dev(stat->dev);
464 tmp.st_rdev = huge_encode_dev(stat->rdev);
466 tmp.st_ino = stat->ino;
467 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
469 #ifdef STAT64_HAS_BROKEN_ST_INO
470 tmp.__st_ino = stat->ino;
472 tmp.st_mode = stat->mode;
473 tmp.st_nlink = stat->nlink;
474 tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
475 tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
476 tmp.st_atime = stat->atime.tv_sec;
477 tmp.st_atime_nsec = stat->atime.tv_nsec;
478 tmp.st_mtime = stat->mtime.tv_sec;
479 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
480 tmp.st_ctime = stat->ctime.tv_sec;
481 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
482 tmp.st_size = stat->size;
483 tmp.st_blocks = stat->blocks;
484 tmp.st_blksize = stat->blksize;
485 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
488 SYSCALL_DEFINE2(stat64, const char __user *, filename,
489 struct stat64 __user *, statbuf)
492 int error = vfs_stat(filename, &stat);
495 error = cp_new_stat64(&stat, statbuf);
500 SYSCALL_DEFINE2(lstat64, const char __user *, filename,
501 struct stat64 __user *, statbuf)
504 int error = vfs_lstat(filename, &stat);
507 error = cp_new_stat64(&stat, statbuf);
512 SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
515 int error = vfs_fstat(fd, &stat);
518 error = cp_new_stat64(&stat, statbuf);
523 SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
524 struct stat64 __user *, statbuf, int, flag)
529 error = vfs_fstatat(dfd, filename, &stat, flag);
532 return cp_new_stat64(&stat, statbuf);
534 #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
536 noinline_for_stack int
537 cp_statx(const struct kstat *stat, struct statx __user *buffer)
541 memset(&tmp, 0, sizeof(tmp));
543 tmp.stx_mask = stat->result_mask;
544 tmp.stx_blksize = stat->blksize;
545 tmp.stx_attributes = stat->attributes;
546 tmp.stx_nlink = stat->nlink;
547 tmp.stx_uid = from_kuid_munged(current_user_ns(), stat->uid);
548 tmp.stx_gid = from_kgid_munged(current_user_ns(), stat->gid);
549 tmp.stx_mode = stat->mode;
550 tmp.stx_ino = stat->ino;
551 tmp.stx_size = stat->size;
552 tmp.stx_blocks = stat->blocks;
553 tmp.stx_attributes_mask = stat->attributes_mask;
554 tmp.stx_atime.tv_sec = stat->atime.tv_sec;
555 tmp.stx_atime.tv_nsec = stat->atime.tv_nsec;
556 tmp.stx_btime.tv_sec = stat->btime.tv_sec;
557 tmp.stx_btime.tv_nsec = stat->btime.tv_nsec;
558 tmp.stx_ctime.tv_sec = stat->ctime.tv_sec;
559 tmp.stx_ctime.tv_nsec = stat->ctime.tv_nsec;
560 tmp.stx_mtime.tv_sec = stat->mtime.tv_sec;
561 tmp.stx_mtime.tv_nsec = stat->mtime.tv_nsec;
562 tmp.stx_rdev_major = MAJOR(stat->rdev);
563 tmp.stx_rdev_minor = MINOR(stat->rdev);
564 tmp.stx_dev_major = MAJOR(stat->dev);
565 tmp.stx_dev_minor = MINOR(stat->dev);
567 return copy_to_user(buffer, &tmp, sizeof(tmp)) ? -EFAULT : 0;
571 * sys_statx - System call to get enhanced stats
572 * @dfd: Base directory to pathwalk from *or* fd to stat.
573 * @filename: File to stat or "" with AT_EMPTY_PATH
574 * @flags: AT_* flags to control pathwalk.
575 * @mask: Parts of statx struct actually required.
576 * @buffer: Result buffer.
578 * Note that fstat() can be emulated by setting dfd to the fd of interest,
579 * supplying "" as the filename and setting AT_EMPTY_PATH in the flags.
581 SYSCALL_DEFINE5(statx,
582 int, dfd, const char __user *, filename, unsigned, flags,
584 struct statx __user *, buffer)
589 if (mask & STATX__RESERVED)
591 if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE)
594 error = vfs_statx(dfd, filename, flags, &stat, mask);
598 return cp_statx(&stat, buffer);
602 static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
604 struct compat_stat tmp;
606 if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
609 memset(&tmp, 0, sizeof(tmp));
610 tmp.st_dev = old_encode_dev(stat->dev);
611 tmp.st_ino = stat->ino;
612 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
614 tmp.st_mode = stat->mode;
615 tmp.st_nlink = stat->nlink;
616 if (tmp.st_nlink != stat->nlink)
618 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
619 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
620 tmp.st_rdev = old_encode_dev(stat->rdev);
621 if ((u64) stat->size > MAX_NON_LFS)
623 tmp.st_size = stat->size;
624 tmp.st_atime = stat->atime.tv_sec;
625 tmp.st_atime_nsec = stat->atime.tv_nsec;
626 tmp.st_mtime = stat->mtime.tv_sec;
627 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
628 tmp.st_ctime = stat->ctime.tv_sec;
629 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
630 tmp.st_blocks = stat->blocks;
631 tmp.st_blksize = stat->blksize;
632 return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
635 COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename,
636 struct compat_stat __user *, statbuf)
641 error = vfs_stat(filename, &stat);
644 return cp_compat_stat(&stat, statbuf);
647 COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename,
648 struct compat_stat __user *, statbuf)
653 error = vfs_lstat(filename, &stat);
656 return cp_compat_stat(&stat, statbuf);
659 #ifndef __ARCH_WANT_STAT64
660 COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd,
661 const char __user *, filename,
662 struct compat_stat __user *, statbuf, int, flag)
667 error = vfs_fstatat(dfd, filename, &stat, flag);
670 return cp_compat_stat(&stat, statbuf);
674 COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd,
675 struct compat_stat __user *, statbuf)
678 int error = vfs_fstat(fd, &stat);
681 error = cp_compat_stat(&stat, statbuf);
686 /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
687 void __inode_add_bytes(struct inode *inode, loff_t bytes)
689 inode->i_blocks += bytes >> 9;
691 inode->i_bytes += bytes;
692 if (inode->i_bytes >= 512) {
694 inode->i_bytes -= 512;
697 EXPORT_SYMBOL(__inode_add_bytes);
699 void inode_add_bytes(struct inode *inode, loff_t bytes)
701 spin_lock(&inode->i_lock);
702 __inode_add_bytes(inode, bytes);
703 spin_unlock(&inode->i_lock);
706 EXPORT_SYMBOL(inode_add_bytes);
708 void __inode_sub_bytes(struct inode *inode, loff_t bytes)
710 inode->i_blocks -= bytes >> 9;
712 if (inode->i_bytes < bytes) {
714 inode->i_bytes += 512;
716 inode->i_bytes -= bytes;
719 EXPORT_SYMBOL(__inode_sub_bytes);
721 void inode_sub_bytes(struct inode *inode, loff_t bytes)
723 spin_lock(&inode->i_lock);
724 __inode_sub_bytes(inode, bytes);
725 spin_unlock(&inode->i_lock);
728 EXPORT_SYMBOL(inode_sub_bytes);
730 loff_t inode_get_bytes(struct inode *inode)
734 spin_lock(&inode->i_lock);
735 ret = __inode_get_bytes(inode);
736 spin_unlock(&inode->i_lock);
740 EXPORT_SYMBOL(inode_get_bytes);
742 void inode_set_bytes(struct inode *inode, loff_t bytes)
744 /* Caller is here responsible for sufficient locking
745 * (ie. inode->i_lock) */
746 inode->i_blocks = bytes >> 9;
747 inode->i_bytes = bytes & 511;
750 EXPORT_SYMBOL(inode_set_bytes);