^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/statfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) static int flags_by_mnt(int mnt_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) int flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) if (mnt_flags & MNT_READONLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) flags |= ST_RDONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) if (mnt_flags & MNT_NOSUID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) flags |= ST_NOSUID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) if (mnt_flags & MNT_NODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) flags |= ST_NODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) if (mnt_flags & MNT_NOEXEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) flags |= ST_NOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (mnt_flags & MNT_NOATIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) flags |= ST_NOATIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (mnt_flags & MNT_NODIRATIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) flags |= ST_NODIRATIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (mnt_flags & MNT_RELATIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) flags |= ST_RELATIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (mnt_flags & MNT_NOSYMFOLLOW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) flags |= ST_NOSYMFOLLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static int flags_by_sb(int s_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) int flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (s_flags & SB_SYNCHRONOUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) flags |= ST_SYNCHRONOUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (s_flags & SB_MANDLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) flags |= ST_MANDLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (s_flags & SB_RDONLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) flags |= ST_RDONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static int calculate_f_flags(struct vfsmount *mnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return ST_VALID | flags_by_mnt(mnt->mnt_flags) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) flags_by_sb(mnt->mnt_sb->s_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static int statfs_by_dentry(struct dentry *dentry, struct kstatfs *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (!dentry->d_sb->s_op->statfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) memset(buf, 0, sizeof(*buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) retval = security_sb_statfs(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) retval = dentry->d_sb->s_op->statfs(dentry, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (retval == 0 && buf->f_frsize == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) buf->f_frsize = buf->f_bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) int vfs_get_fsid(struct dentry *dentry, __kernel_fsid_t *fsid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct kstatfs st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) error = statfs_by_dentry(dentry, &st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) *fsid = st.f_fsid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) EXPORT_SYMBOL(vfs_get_fsid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) int vfs_statfs(const struct path *path, struct kstatfs *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) error = statfs_by_dentry(path->dentry, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) buf->f_flags = calculate_f_flags(path->mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) EXPORT_SYMBOL(vfs_statfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) int user_statfs(const char __user *pathname, struct kstatfs *st)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct path path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) unsigned int lookup_flags = LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (!error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) error = vfs_statfs(&path, st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) path_put(&path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (retry_estale(error, lookup_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) lookup_flags |= LOOKUP_REVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) int fd_statfs(int fd, struct kstatfs *st)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) struct fd f = fdget_raw(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) int error = -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (f.file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) error = vfs_statfs(&f.file->f_path, st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) fdput(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static int do_statfs_native(struct kstatfs *st, struct statfs __user *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct statfs buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (sizeof(buf) == sizeof(*st))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) memcpy(&buf, st, sizeof(*st));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (sizeof buf.f_blocks == 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if ((st->f_blocks | st->f_bfree | st->f_bavail |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) st->f_bsize | st->f_frsize) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 0xffffffff00000000ULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * f_files and f_ffree may be -1; it's okay to stuff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * that into 32 bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (st->f_files != -1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) (st->f_files & 0xffffffff00000000ULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (st->f_ffree != -1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) (st->f_ffree & 0xffffffff00000000ULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) buf.f_type = st->f_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) buf.f_bsize = st->f_bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) buf.f_blocks = st->f_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) buf.f_bfree = st->f_bfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) buf.f_bavail = st->f_bavail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) buf.f_files = st->f_files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) buf.f_ffree = st->f_ffree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) buf.f_fsid = st->f_fsid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) buf.f_namelen = st->f_namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) buf.f_frsize = st->f_frsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) buf.f_flags = st->f_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) memset(buf.f_spare, 0, sizeof(buf.f_spare));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (copy_to_user(p, &buf, sizeof(buf)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static int do_statfs64(struct kstatfs *st, struct statfs64 __user *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct statfs64 buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (sizeof(buf) == sizeof(*st))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) memcpy(&buf, st, sizeof(*st));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) buf.f_type = st->f_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) buf.f_bsize = st->f_bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) buf.f_blocks = st->f_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) buf.f_bfree = st->f_bfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) buf.f_bavail = st->f_bavail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) buf.f_files = st->f_files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) buf.f_ffree = st->f_ffree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) buf.f_fsid = st->f_fsid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) buf.f_namelen = st->f_namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) buf.f_frsize = st->f_frsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) buf.f_flags = st->f_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) memset(buf.f_spare, 0, sizeof(buf.f_spare));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (copy_to_user(p, &buf, sizeof(buf)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct statfs __user *, buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) struct kstatfs st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) int error = user_statfs(pathname, &st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) error = do_statfs_native(&st, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) SYSCALL_DEFINE3(statfs64, const char __user *, pathname, size_t, sz, struct statfs64 __user *, buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) struct kstatfs st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (sz != sizeof(*buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) error = user_statfs(pathname, &st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) error = do_statfs64(&st, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct statfs __user *, buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct kstatfs st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) int error = fd_statfs(fd, &st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) error = do_statfs_native(&st, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, size_t, sz, struct statfs64 __user *, buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) struct kstatfs st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (sz != sizeof(*buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) error = fd_statfs(fd, &st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) error = do_statfs64(&st, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) static int vfs_ustat(dev_t dev, struct kstatfs *sbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) struct super_block *s = user_get_super(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (!s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) err = statfs_by_dentry(s->s_root, sbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) drop_super(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) struct ustat tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) struct kstatfs sbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) int err = vfs_ustat(new_decode_dev(dev), &sbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) memset(&tmp,0,sizeof(struct ustat));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) tmp.f_tfree = sbuf.f_bfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) tmp.f_tinode = sbuf.f_ffree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return copy_to_user(ubuf, &tmp, sizeof(struct ustat)) ? -EFAULT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct compat_statfs buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (sizeof ubuf->f_blocks == 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) /* f_files and f_ffree may be -1; it's okay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * to stuff that into 32 bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) if (kbuf->f_files != 0xffffffffffffffffULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) && (kbuf->f_files & 0xffffffff00000000ULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (kbuf->f_ffree != 0xffffffffffffffffULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) && (kbuf->f_ffree & 0xffffffff00000000ULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) memset(&buf, 0, sizeof(struct compat_statfs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) buf.f_type = kbuf->f_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) buf.f_bsize = kbuf->f_bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) buf.f_blocks = kbuf->f_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) buf.f_bfree = kbuf->f_bfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) buf.f_bavail = kbuf->f_bavail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) buf.f_files = kbuf->f_files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) buf.f_ffree = kbuf->f_ffree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) buf.f_namelen = kbuf->f_namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) buf.f_fsid.val[0] = kbuf->f_fsid.val[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) buf.f_fsid.val[1] = kbuf->f_fsid.val[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) buf.f_frsize = kbuf->f_frsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) buf.f_flags = kbuf->f_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (copy_to_user(ubuf, &buf, sizeof(struct compat_statfs)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * The following statfs calls are copies of code from fs/statfs.c and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * should be checked against those from time to time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) COMPAT_SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct compat_statfs __user *, buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) struct kstatfs tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) int error = user_statfs(pathname, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) error = put_compat_statfs(buf, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) COMPAT_SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct compat_statfs __user *, buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) struct kstatfs tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) int error = fd_statfs(fd, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) error = put_compat_statfs(buf, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) struct compat_statfs64 buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if ((kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) return -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) memset(&buf, 0, sizeof(struct compat_statfs64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) buf.f_type = kbuf->f_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) buf.f_bsize = kbuf->f_bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) buf.f_blocks = kbuf->f_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) buf.f_bfree = kbuf->f_bfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) buf.f_bavail = kbuf->f_bavail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) buf.f_files = kbuf->f_files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) buf.f_ffree = kbuf->f_ffree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) buf.f_namelen = kbuf->f_namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) buf.f_fsid.val[0] = kbuf->f_fsid.val[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) buf.f_fsid.val[1] = kbuf->f_fsid.val[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) buf.f_frsize = kbuf->f_frsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) buf.f_flags = kbuf->f_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (copy_to_user(ubuf, &buf, sizeof(struct compat_statfs64)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) int kcompat_sys_statfs64(const char __user * pathname, compat_size_t sz, struct compat_statfs64 __user * buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) struct kstatfs tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (sz != sizeof(*buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) error = user_statfs(pathname, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) error = put_compat_statfs64(buf, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) COMPAT_SYSCALL_DEFINE3(statfs64, const char __user *, pathname, compat_size_t, sz, struct compat_statfs64 __user *, buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) return kcompat_sys_statfs64(pathname, sz, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) int kcompat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user * buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) struct kstatfs tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) if (sz != sizeof(*buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) error = fd_statfs(fd, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) error = put_compat_statfs64(buf, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) COMPAT_SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, compat_size_t, sz, struct compat_statfs64 __user *, buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) return kcompat_sys_fstatfs64(fd, sz, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) * This is a copy of sys_ustat, just dealing with a structure layout.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * Given how simple this syscall is that apporach is more maintainable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * than the various conversion hacks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) COMPAT_SYSCALL_DEFINE2(ustat, unsigned, dev, struct compat_ustat __user *, u)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) struct compat_ustat tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) struct kstatfs sbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) int err = vfs_ustat(new_decode_dev(dev), &sbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) memset(&tmp, 0, sizeof(struct compat_ustat));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) tmp.f_tfree = sbuf.f_bfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) tmp.f_tinode = sbuf.f_ffree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (copy_to_user(u, &tmp, sizeof(struct compat_ustat)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) #endif