Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 1 | #include <linux/syscalls.h> |
Paul Gortmaker | 630d9c4 | 2011-11-16 23:57:37 -0500 | [diff] [blame] | 2 | #include <linux/export.h> |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 3 | #include <linux/fs.h> |
| 4 | #include <linux/file.h> |
Christoph Hellwig | 365b181 | 2010-07-07 18:53:25 +0200 | [diff] [blame] | 5 | #include <linux/mount.h> |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 6 | #include <linux/namei.h> |
| 7 | #include <linux/statfs.h> |
| 8 | #include <linux/security.h> |
| 9 | #include <linux/uaccess.h> |
Al Viro | cf31e70 | 2012-01-02 22:28:36 -0500 | [diff] [blame] | 10 | #include "internal.h" |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 11 | |
Christoph Hellwig | 365b181 | 2010-07-07 18:53:25 +0200 | [diff] [blame] | 12 | static int flags_by_mnt(int mnt_flags) |
| 13 | { |
| 14 | int flags = 0; |
| 15 | |
| 16 | if (mnt_flags & MNT_READONLY) |
| 17 | flags |= ST_RDONLY; |
| 18 | if (mnt_flags & MNT_NOSUID) |
| 19 | flags |= ST_NOSUID; |
| 20 | if (mnt_flags & MNT_NODEV) |
| 21 | flags |= ST_NODEV; |
| 22 | if (mnt_flags & MNT_NOEXEC) |
| 23 | flags |= ST_NOEXEC; |
| 24 | if (mnt_flags & MNT_NOATIME) |
| 25 | flags |= ST_NOATIME; |
| 26 | if (mnt_flags & MNT_NODIRATIME) |
| 27 | flags |= ST_NODIRATIME; |
| 28 | if (mnt_flags & MNT_RELATIME) |
| 29 | flags |= ST_RELATIME; |
| 30 | return flags; |
| 31 | } |
| 32 | |
| 33 | static int flags_by_sb(int s_flags) |
| 34 | { |
| 35 | int flags = 0; |
| 36 | if (s_flags & MS_SYNCHRONOUS) |
| 37 | flags |= ST_SYNCHRONOUS; |
| 38 | if (s_flags & MS_MANDLOCK) |
| 39 | flags |= ST_MANDLOCK; |
| 40 | return flags; |
| 41 | } |
| 42 | |
| 43 | static int calculate_f_flags(struct vfsmount *mnt) |
| 44 | { |
| 45 | return ST_VALID | flags_by_mnt(mnt->mnt_flags) | |
| 46 | flags_by_sb(mnt->mnt_sb->s_flags); |
| 47 | } |
| 48 | |
Al Viro | cf31e70 | 2012-01-02 22:28:36 -0500 | [diff] [blame] | 49 | static int statfs_by_dentry(struct dentry *dentry, struct kstatfs *buf) |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 50 | { |
Christoph Hellwig | ebabe9a | 2010-07-07 18:53:11 +0200 | [diff] [blame] | 51 | int retval; |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 52 | |
Christoph Hellwig | ebabe9a | 2010-07-07 18:53:11 +0200 | [diff] [blame] | 53 | if (!dentry->d_sb->s_op->statfs) |
| 54 | return -ENOSYS; |
| 55 | |
| 56 | memset(buf, 0, sizeof(*buf)); |
| 57 | retval = security_sb_statfs(dentry); |
| 58 | if (retval) |
| 59 | return retval; |
| 60 | retval = dentry->d_sb->s_op->statfs(dentry, buf); |
| 61 | if (retval == 0 && buf->f_frsize == 0) |
| 62 | buf->f_frsize = buf->f_bsize; |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 63 | return retval; |
| 64 | } |
| 65 | |
Christoph Hellwig | ebabe9a | 2010-07-07 18:53:11 +0200 | [diff] [blame] | 66 | int vfs_statfs(struct path *path, struct kstatfs *buf) |
| 67 | { |
Christoph Hellwig | 365b181 | 2010-07-07 18:53:25 +0200 | [diff] [blame] | 68 | int error; |
| 69 | |
| 70 | error = statfs_by_dentry(path->dentry, buf); |
| 71 | if (!error) |
| 72 | buf->f_flags = calculate_f_flags(path->mnt); |
| 73 | return error; |
Christoph Hellwig | ebabe9a | 2010-07-07 18:53:11 +0200 | [diff] [blame] | 74 | } |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 75 | EXPORT_SYMBOL(vfs_statfs); |
| 76 | |
Al Viro | c8b91ac | 2011-03-12 10:41:39 -0500 | [diff] [blame] | 77 | int user_statfs(const char __user *pathname, struct kstatfs *st) |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 78 | { |
Al Viro | c8b91ac | 2011-03-12 10:41:39 -0500 | [diff] [blame] | 79 | struct path path; |
Dan McGee | 5c8a0fb | 2011-11-01 18:23:10 -0500 | [diff] [blame] | 80 | int error = user_path_at(AT_FDCWD, pathname, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); |
Al Viro | c8b91ac | 2011-03-12 10:41:39 -0500 | [diff] [blame] | 81 | if (!error) { |
| 82 | error = vfs_statfs(&path, st); |
| 83 | path_put(&path); |
| 84 | } |
| 85 | return error; |
| 86 | } |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 87 | |
Al Viro | c8b91ac | 2011-03-12 10:41:39 -0500 | [diff] [blame] | 88 | int fd_statfs(int fd, struct kstatfs *st) |
| 89 | { |
Al Viro | 0aa2ee5 | 2012-04-21 18:47:27 -0400 | [diff] [blame^] | 90 | int fput_needed; |
| 91 | struct file *file = fget_light(fd, &fput_needed); |
Al Viro | c8b91ac | 2011-03-12 10:41:39 -0500 | [diff] [blame] | 92 | int error = -EBADF; |
| 93 | if (file) { |
| 94 | error = vfs_statfs(&file->f_path, st); |
Al Viro | 0aa2ee5 | 2012-04-21 18:47:27 -0400 | [diff] [blame^] | 95 | fput_light(file, fput_needed); |
Al Viro | c8b91ac | 2011-03-12 10:41:39 -0500 | [diff] [blame] | 96 | } |
| 97 | return error; |
| 98 | } |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 99 | |
Al Viro | c8b91ac | 2011-03-12 10:41:39 -0500 | [diff] [blame] | 100 | static int do_statfs_native(struct kstatfs *st, struct statfs __user *p) |
| 101 | { |
| 102 | struct statfs buf; |
| 103 | |
| 104 | if (sizeof(buf) == sizeof(*st)) |
| 105 | memcpy(&buf, st, sizeof(*st)); |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 106 | else { |
Al Viro | c8b91ac | 2011-03-12 10:41:39 -0500 | [diff] [blame] | 107 | if (sizeof buf.f_blocks == 4) { |
| 108 | if ((st->f_blocks | st->f_bfree | st->f_bavail | |
| 109 | st->f_bsize | st->f_frsize) & |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 110 | 0xffffffff00000000ULL) |
| 111 | return -EOVERFLOW; |
| 112 | /* |
| 113 | * f_files and f_ffree may be -1; it's okay to stuff |
| 114 | * that into 32 bits |
| 115 | */ |
Al Viro | c8b91ac | 2011-03-12 10:41:39 -0500 | [diff] [blame] | 116 | if (st->f_files != -1 && |
| 117 | (st->f_files & 0xffffffff00000000ULL)) |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 118 | return -EOVERFLOW; |
Al Viro | c8b91ac | 2011-03-12 10:41:39 -0500 | [diff] [blame] | 119 | if (st->f_ffree != -1 && |
| 120 | (st->f_ffree & 0xffffffff00000000ULL)) |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 121 | return -EOVERFLOW; |
| 122 | } |
| 123 | |
Al Viro | c8b91ac | 2011-03-12 10:41:39 -0500 | [diff] [blame] | 124 | buf.f_type = st->f_type; |
| 125 | buf.f_bsize = st->f_bsize; |
| 126 | buf.f_blocks = st->f_blocks; |
| 127 | buf.f_bfree = st->f_bfree; |
| 128 | buf.f_bavail = st->f_bavail; |
| 129 | buf.f_files = st->f_files; |
| 130 | buf.f_ffree = st->f_ffree; |
| 131 | buf.f_fsid = st->f_fsid; |
| 132 | buf.f_namelen = st->f_namelen; |
| 133 | buf.f_frsize = st->f_frsize; |
| 134 | buf.f_flags = st->f_flags; |
| 135 | memset(buf.f_spare, 0, sizeof(buf.f_spare)); |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 136 | } |
Al Viro | c8b91ac | 2011-03-12 10:41:39 -0500 | [diff] [blame] | 137 | if (copy_to_user(p, &buf, sizeof(buf))) |
| 138 | return -EFAULT; |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | |
Al Viro | c8b91ac | 2011-03-12 10:41:39 -0500 | [diff] [blame] | 142 | static int do_statfs64(struct kstatfs *st, struct statfs64 __user *p) |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 143 | { |
Al Viro | c8b91ac | 2011-03-12 10:41:39 -0500 | [diff] [blame] | 144 | struct statfs64 buf; |
| 145 | if (sizeof(buf) == sizeof(*st)) |
| 146 | memcpy(&buf, st, sizeof(*st)); |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 147 | else { |
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 | |
| 166 | SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct statfs __user *, buf) |
| 167 | { |
Al Viro | c8b91ac | 2011-03-12 10:41:39 -0500 | [diff] [blame] | 168 | struct kstatfs st; |
| 169 | int error = user_statfs(pathname, &st); |
| 170 | if (!error) |
| 171 | error = do_statfs_native(&st, buf); |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 172 | return error; |
| 173 | } |
| 174 | |
| 175 | SYSCALL_DEFINE3(statfs64, const char __user *, pathname, size_t, sz, struct statfs64 __user *, buf) |
| 176 | { |
Al Viro | c8b91ac | 2011-03-12 10:41:39 -0500 | [diff] [blame] | 177 | struct kstatfs st; |
| 178 | int error; |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 179 | if (sz != sizeof(*buf)) |
| 180 | return -EINVAL; |
Al Viro | c8b91ac | 2011-03-12 10:41:39 -0500 | [diff] [blame] | 181 | error = user_statfs(pathname, &st); |
| 182 | if (!error) |
| 183 | error = do_statfs64(&st, buf); |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 184 | return error; |
| 185 | } |
| 186 | |
| 187 | SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct statfs __user *, buf) |
| 188 | { |
Al Viro | c8b91ac | 2011-03-12 10:41:39 -0500 | [diff] [blame] | 189 | struct kstatfs st; |
| 190 | int error = fd_statfs(fd, &st); |
| 191 | if (!error) |
| 192 | error = do_statfs_native(&st, buf); |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 193 | return error; |
| 194 | } |
| 195 | |
| 196 | SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, size_t, sz, struct statfs64 __user *, buf) |
| 197 | { |
Al Viro | c8b91ac | 2011-03-12 10:41:39 -0500 | [diff] [blame] | 198 | struct kstatfs st; |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 199 | int error; |
| 200 | |
| 201 | if (sz != sizeof(*buf)) |
| 202 | return -EINVAL; |
| 203 | |
Al Viro | c8b91ac | 2011-03-12 10:41:39 -0500 | [diff] [blame] | 204 | error = fd_statfs(fd, &st); |
| 205 | if (!error) |
| 206 | error = do_statfs64(&st, buf); |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 207 | return error; |
| 208 | } |
| 209 | |
Al Viro | cf31e70 | 2012-01-02 22:28:36 -0500 | [diff] [blame] | 210 | int vfs_ustat(dev_t dev, struct kstatfs *sbuf) |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 211 | { |
Al Viro | cf31e70 | 2012-01-02 22:28:36 -0500 | [diff] [blame] | 212 | struct super_block *s = user_get_super(dev); |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 213 | int err; |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 214 | if (!s) |
| 215 | return -EINVAL; |
| 216 | |
Al Viro | cf31e70 | 2012-01-02 22:28:36 -0500 | [diff] [blame] | 217 | err = statfs_by_dentry(s->s_root, sbuf); |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 218 | drop_super(s); |
Al Viro | cf31e70 | 2012-01-02 22:28:36 -0500 | [diff] [blame] | 219 | return err; |
| 220 | } |
| 221 | |
| 222 | SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf) |
| 223 | { |
| 224 | struct ustat tmp; |
| 225 | struct kstatfs sbuf; |
| 226 | int err = vfs_ustat(new_decode_dev(dev), &sbuf); |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 227 | if (err) |
| 228 | return err; |
| 229 | |
| 230 | memset(&tmp,0,sizeof(struct ustat)); |
| 231 | tmp.f_tfree = sbuf.f_bfree; |
| 232 | tmp.f_tinode = sbuf.f_ffree; |
| 233 | |
| 234 | return copy_to_user(ubuf, &tmp, sizeof(struct ustat)) ? -EFAULT : 0; |
| 235 | } |