Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 1 | #include <linux/syscalls.h> |
| 2 | #include <linux/module.h> |
| 3 | #include <linux/fs.h> |
| 4 | #include <linux/file.h> |
| 5 | #include <linux/namei.h> |
| 6 | #include <linux/statfs.h> |
| 7 | #include <linux/security.h> |
| 8 | #include <linux/uaccess.h> |
| 9 | |
Christoph Hellwig | ebabe9a | 2010-07-07 18:53:11 +0200 | [diff] [blame^] | 10 | int statfs_by_dentry(struct dentry *dentry, struct kstatfs *buf) |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 11 | { |
Christoph Hellwig | ebabe9a | 2010-07-07 18:53:11 +0200 | [diff] [blame^] | 12 | int retval; |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 13 | |
Christoph Hellwig | ebabe9a | 2010-07-07 18:53:11 +0200 | [diff] [blame^] | 14 | if (!dentry->d_sb->s_op->statfs) |
| 15 | return -ENOSYS; |
| 16 | |
| 17 | memset(buf, 0, sizeof(*buf)); |
| 18 | retval = security_sb_statfs(dentry); |
| 19 | if (retval) |
| 20 | return retval; |
| 21 | retval = dentry->d_sb->s_op->statfs(dentry, buf); |
| 22 | if (retval == 0 && buf->f_frsize == 0) |
| 23 | buf->f_frsize = buf->f_bsize; |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 24 | return retval; |
| 25 | } |
| 26 | |
Christoph Hellwig | ebabe9a | 2010-07-07 18:53:11 +0200 | [diff] [blame^] | 27 | int vfs_statfs(struct path *path, struct kstatfs *buf) |
| 28 | { |
| 29 | return statfs_by_dentry(path->dentry, buf); |
| 30 | } |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 31 | EXPORT_SYMBOL(vfs_statfs); |
| 32 | |
Christoph Hellwig | ebabe9a | 2010-07-07 18:53:11 +0200 | [diff] [blame^] | 33 | static int do_statfs_native(struct path *path, struct statfs *buf) |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 34 | { |
| 35 | struct kstatfs st; |
| 36 | int retval; |
| 37 | |
Christoph Hellwig | ebabe9a | 2010-07-07 18:53:11 +0200 | [diff] [blame^] | 38 | retval = vfs_statfs(path, &st); |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 39 | if (retval) |
| 40 | return retval; |
| 41 | |
| 42 | if (sizeof(*buf) == sizeof(st)) |
| 43 | memcpy(buf, &st, sizeof(st)); |
| 44 | else { |
| 45 | if (sizeof buf->f_blocks == 4) { |
| 46 | if ((st.f_blocks | st.f_bfree | st.f_bavail | |
| 47 | st.f_bsize | st.f_frsize) & |
| 48 | 0xffffffff00000000ULL) |
| 49 | return -EOVERFLOW; |
| 50 | /* |
| 51 | * f_files and f_ffree may be -1; it's okay to stuff |
| 52 | * that into 32 bits |
| 53 | */ |
| 54 | if (st.f_files != -1 && |
| 55 | (st.f_files & 0xffffffff00000000ULL)) |
| 56 | return -EOVERFLOW; |
| 57 | if (st.f_ffree != -1 && |
| 58 | (st.f_ffree & 0xffffffff00000000ULL)) |
| 59 | return -EOVERFLOW; |
| 60 | } |
| 61 | |
| 62 | buf->f_type = st.f_type; |
| 63 | buf->f_bsize = st.f_bsize; |
| 64 | buf->f_blocks = st.f_blocks; |
| 65 | buf->f_bfree = st.f_bfree; |
| 66 | buf->f_bavail = st.f_bavail; |
| 67 | buf->f_files = st.f_files; |
| 68 | buf->f_ffree = st.f_ffree; |
| 69 | buf->f_fsid = st.f_fsid; |
| 70 | buf->f_namelen = st.f_namelen; |
| 71 | buf->f_frsize = st.f_frsize; |
| 72 | memset(buf->f_spare, 0, sizeof(buf->f_spare)); |
| 73 | } |
| 74 | return 0; |
| 75 | } |
| 76 | |
Christoph Hellwig | ebabe9a | 2010-07-07 18:53:11 +0200 | [diff] [blame^] | 77 | static int do_statfs64(struct path *path, struct statfs64 *buf) |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 78 | { |
| 79 | struct kstatfs st; |
| 80 | int retval; |
| 81 | |
Christoph Hellwig | ebabe9a | 2010-07-07 18:53:11 +0200 | [diff] [blame^] | 82 | retval = vfs_statfs(path, &st); |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 83 | if (retval) |
| 84 | return retval; |
| 85 | |
| 86 | if (sizeof(*buf) == sizeof(st)) |
| 87 | memcpy(buf, &st, sizeof(st)); |
| 88 | else { |
| 89 | buf->f_type = st.f_type; |
| 90 | buf->f_bsize = st.f_bsize; |
| 91 | buf->f_blocks = st.f_blocks; |
| 92 | buf->f_bfree = st.f_bfree; |
| 93 | buf->f_bavail = st.f_bavail; |
| 94 | buf->f_files = st.f_files; |
| 95 | buf->f_ffree = st.f_ffree; |
| 96 | buf->f_fsid = st.f_fsid; |
| 97 | buf->f_namelen = st.f_namelen; |
| 98 | buf->f_frsize = st.f_frsize; |
| 99 | memset(buf->f_spare, 0, sizeof(buf->f_spare)); |
| 100 | } |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct statfs __user *, buf) |
| 105 | { |
| 106 | struct path path; |
| 107 | int error; |
| 108 | |
| 109 | error = user_path(pathname, &path); |
| 110 | if (!error) { |
| 111 | struct statfs tmp; |
Christoph Hellwig | ebabe9a | 2010-07-07 18:53:11 +0200 | [diff] [blame^] | 112 | error = do_statfs_native(&path, &tmp); |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 113 | if (!error && copy_to_user(buf, &tmp, sizeof(tmp))) |
| 114 | error = -EFAULT; |
| 115 | path_put(&path); |
| 116 | } |
| 117 | return error; |
| 118 | } |
| 119 | |
| 120 | SYSCALL_DEFINE3(statfs64, const char __user *, pathname, size_t, sz, struct statfs64 __user *, buf) |
| 121 | { |
| 122 | struct path path; |
| 123 | long error; |
| 124 | |
| 125 | if (sz != sizeof(*buf)) |
| 126 | return -EINVAL; |
| 127 | error = user_path(pathname, &path); |
| 128 | if (!error) { |
| 129 | struct statfs64 tmp; |
Christoph Hellwig | ebabe9a | 2010-07-07 18:53:11 +0200 | [diff] [blame^] | 130 | error = do_statfs64(&path, &tmp); |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 131 | if (!error && copy_to_user(buf, &tmp, sizeof(tmp))) |
| 132 | error = -EFAULT; |
| 133 | path_put(&path); |
| 134 | } |
| 135 | return error; |
| 136 | } |
| 137 | |
| 138 | SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct statfs __user *, buf) |
| 139 | { |
| 140 | struct file *file; |
| 141 | struct statfs tmp; |
| 142 | int error; |
| 143 | |
| 144 | error = -EBADF; |
| 145 | file = fget(fd); |
| 146 | if (!file) |
| 147 | goto out; |
Christoph Hellwig | ebabe9a | 2010-07-07 18:53:11 +0200 | [diff] [blame^] | 148 | error = do_statfs_native(&file->f_path, &tmp); |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 149 | if (!error && copy_to_user(buf, &tmp, sizeof(tmp))) |
| 150 | error = -EFAULT; |
| 151 | fput(file); |
| 152 | out: |
| 153 | return error; |
| 154 | } |
| 155 | |
| 156 | SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, size_t, sz, struct statfs64 __user *, buf) |
| 157 | { |
| 158 | struct file *file; |
| 159 | struct statfs64 tmp; |
| 160 | int error; |
| 161 | |
| 162 | if (sz != sizeof(*buf)) |
| 163 | return -EINVAL; |
| 164 | |
| 165 | error = -EBADF; |
| 166 | file = fget(fd); |
| 167 | if (!file) |
| 168 | goto out; |
Christoph Hellwig | ebabe9a | 2010-07-07 18:53:11 +0200 | [diff] [blame^] | 169 | error = do_statfs64(&file->f_path, &tmp); |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 170 | if (!error && copy_to_user(buf, &tmp, sizeof(tmp))) |
| 171 | error = -EFAULT; |
| 172 | fput(file); |
| 173 | out: |
| 174 | return error; |
| 175 | } |
| 176 | |
| 177 | SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf) |
| 178 | { |
| 179 | struct super_block *s; |
| 180 | struct ustat tmp; |
| 181 | struct kstatfs sbuf; |
| 182 | int err; |
| 183 | |
| 184 | s = user_get_super(new_decode_dev(dev)); |
| 185 | if (!s) |
| 186 | return -EINVAL; |
| 187 | |
Christoph Hellwig | ebabe9a | 2010-07-07 18:53:11 +0200 | [diff] [blame^] | 188 | err = statfs_by_dentry(s->s_root, &sbuf); |
Al Viro | 7ed1ee6 | 2010-03-23 10:37:36 -0400 | [diff] [blame] | 189 | drop_super(s); |
| 190 | if (err) |
| 191 | return err; |
| 192 | |
| 193 | memset(&tmp,0,sizeof(struct ustat)); |
| 194 | tmp.f_tfree = sbuf.f_bfree; |
| 195 | tmp.f_tinode = sbuf.f_ffree; |
| 196 | |
| 197 | return copy_to_user(ubuf, &tmp, sizeof(struct ustat)) ? -EFAULT : 0; |
| 198 | } |