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