blob: 4e4623c7a1260eafaf564e87b4812091a9b3eb0e [file] [log] [blame]
Al Viro7ed1ee62010-03-23 10:37:36 -04001#include <linux/syscalls.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -05002#include <linux/export.h>
Al Viro7ed1ee62010-03-23 10:37:36 -04003#include <linux/fs.h>
4#include <linux/file.h>
Christoph Hellwig365b1812010-07-07 18:53:25 +02005#include <linux/mount.h>
Al Viro7ed1ee62010-03-23 10:37:36 -04006#include <linux/namei.h>
7#include <linux/statfs.h>
8#include <linux/security.h>
9#include <linux/uaccess.h>
Al Viro4ada54e2017-04-08 18:08:15 -040010#include <linux/compat.h>
Al Virocf31e702012-01-02 22:28:36 -050011#include "internal.h"
Al Viro7ed1ee62010-03-23 10:37:36 -040012
Christoph Hellwig365b1812010-07-07 18:53:25 +020013static int flags_by_mnt(int mnt_flags)
14{
15 int flags = 0;
16
17 if (mnt_flags & MNT_READONLY)
18 flags |= ST_RDONLY;
19 if (mnt_flags & MNT_NOSUID)
20 flags |= ST_NOSUID;
21 if (mnt_flags & MNT_NODEV)
22 flags |= ST_NODEV;
23 if (mnt_flags & MNT_NOEXEC)
24 flags |= ST_NOEXEC;
25 if (mnt_flags & MNT_NOATIME)
26 flags |= ST_NOATIME;
27 if (mnt_flags & MNT_NODIRATIME)
28 flags |= ST_NODIRATIME;
29 if (mnt_flags & MNT_RELATIME)
30 flags |= ST_RELATIME;
31 return flags;
32}
33
34static int flags_by_sb(int s_flags)
35{
36 int flags = 0;
37 if (s_flags & MS_SYNCHRONOUS)
38 flags |= ST_SYNCHRONOUS;
39 if (s_flags & MS_MANDLOCK)
40 flags |= ST_MANDLOCK;
41 return flags;
42}
43
44static int calculate_f_flags(struct vfsmount *mnt)
45{
46 return ST_VALID | flags_by_mnt(mnt->mnt_flags) |
47 flags_by_sb(mnt->mnt_sb->s_flags);
48}
49
Al Virocf31e702012-01-02 22:28:36 -050050static int statfs_by_dentry(struct dentry *dentry, struct kstatfs *buf)
Al Viro7ed1ee62010-03-23 10:37:36 -040051{
Christoph Hellwigebabe9a2010-07-07 18:53:11 +020052 int retval;
Al Viro7ed1ee62010-03-23 10:37:36 -040053
Christoph Hellwigebabe9a2010-07-07 18:53:11 +020054 if (!dentry->d_sb->s_op->statfs)
55 return -ENOSYS;
56
57 memset(buf, 0, sizeof(*buf));
58 retval = security_sb_statfs(dentry);
59 if (retval)
60 return retval;
61 retval = dentry->d_sb->s_op->statfs(dentry, buf);
62 if (retval == 0 && buf->f_frsize == 0)
63 buf->f_frsize = buf->f_bsize;
Al Viro7ed1ee62010-03-23 10:37:36 -040064 return retval;
65}
66
Al Virof0bb5aa2016-11-20 20:27:12 -050067int vfs_statfs(const struct path *path, struct kstatfs *buf)
Christoph Hellwigebabe9a2010-07-07 18:53:11 +020068{
Christoph Hellwig365b1812010-07-07 18:53:25 +020069 int error;
70
71 error = statfs_by_dentry(path->dentry, buf);
72 if (!error)
73 buf->f_flags = calculate_f_flags(path->mnt);
74 return error;
Christoph Hellwigebabe9a2010-07-07 18:53:11 +020075}
Al Viro7ed1ee62010-03-23 10:37:36 -040076EXPORT_SYMBOL(vfs_statfs);
77
Al Viroc8b91ac2011-03-12 10:41:39 -050078int user_statfs(const char __user *pathname, struct kstatfs *st)
Al Viro7ed1ee62010-03-23 10:37:36 -040079{
Al Viroc8b91ac2011-03-12 10:41:39 -050080 struct path path;
Jeff Layton96948fc2012-12-11 12:10:14 -050081 int error;
82 unsigned int lookup_flags = LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT;
83retry:
84 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Al Viroc8b91ac2011-03-12 10:41:39 -050085 if (!error) {
86 error = vfs_statfs(&path, st);
87 path_put(&path);
Jeff Layton96948fc2012-12-11 12:10:14 -050088 if (retry_estale(error, lookup_flags)) {
89 lookup_flags |= LOOKUP_REVAL;
90 goto retry;
91 }
Al Viroc8b91ac2011-03-12 10:41:39 -050092 }
93 return error;
94}
Al Viro7ed1ee62010-03-23 10:37:36 -040095
Al Viroc8b91ac2011-03-12 10:41:39 -050096int fd_statfs(int fd, struct kstatfs *st)
97{
Linus Torvalds9d057462013-09-30 08:35:10 -070098 struct fd f = fdget_raw(fd);
Al Viroc8b91ac2011-03-12 10:41:39 -050099 int error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400100 if (f.file) {
101 error = vfs_statfs(&f.file->f_path, st);
102 fdput(f);
Al Viroc8b91ac2011-03-12 10:41:39 -0500103 }
104 return error;
105}
Al Viro7ed1ee62010-03-23 10:37:36 -0400106
Al Viroc8b91ac2011-03-12 10:41:39 -0500107static int do_statfs_native(struct kstatfs *st, struct statfs __user *p)
108{
109 struct statfs buf;
110
111 if (sizeof(buf) == sizeof(*st))
112 memcpy(&buf, st, sizeof(*st));
Al Viro7ed1ee62010-03-23 10:37:36 -0400113 else {
Al Viroc8b91ac2011-03-12 10:41:39 -0500114 if (sizeof buf.f_blocks == 4) {
115 if ((st->f_blocks | st->f_bfree | st->f_bavail |
116 st->f_bsize | st->f_frsize) &
Al Viro7ed1ee62010-03-23 10:37:36 -0400117 0xffffffff00000000ULL)
118 return -EOVERFLOW;
119 /*
120 * f_files and f_ffree may be -1; it's okay to stuff
121 * that into 32 bits
122 */
Al Viroc8b91ac2011-03-12 10:41:39 -0500123 if (st->f_files != -1 &&
124 (st->f_files & 0xffffffff00000000ULL))
Al Viro7ed1ee62010-03-23 10:37:36 -0400125 return -EOVERFLOW;
Al Viroc8b91ac2011-03-12 10:41:39 -0500126 if (st->f_ffree != -1 &&
127 (st->f_ffree & 0xffffffff00000000ULL))
Al Viro7ed1ee62010-03-23 10:37:36 -0400128 return -EOVERFLOW;
129 }
130
Al Viroc8b91ac2011-03-12 10:41:39 -0500131 buf.f_type = st->f_type;
132 buf.f_bsize = st->f_bsize;
133 buf.f_blocks = st->f_blocks;
134 buf.f_bfree = st->f_bfree;
135 buf.f_bavail = st->f_bavail;
136 buf.f_files = st->f_files;
137 buf.f_ffree = st->f_ffree;
138 buf.f_fsid = st->f_fsid;
139 buf.f_namelen = st->f_namelen;
140 buf.f_frsize = st->f_frsize;
141 buf.f_flags = st->f_flags;
142 memset(buf.f_spare, 0, sizeof(buf.f_spare));
Al Viro7ed1ee62010-03-23 10:37:36 -0400143 }
Al Viroc8b91ac2011-03-12 10:41:39 -0500144 if (copy_to_user(p, &buf, sizeof(buf)))
145 return -EFAULT;
Al Viro7ed1ee62010-03-23 10:37:36 -0400146 return 0;
147}
148
Al Viroc8b91ac2011-03-12 10:41:39 -0500149static int do_statfs64(struct kstatfs *st, struct statfs64 __user *p)
Al Viro7ed1ee62010-03-23 10:37:36 -0400150{
Al Viroc8b91ac2011-03-12 10:41:39 -0500151 struct statfs64 buf;
152 if (sizeof(buf) == sizeof(*st))
153 memcpy(&buf, st, sizeof(*st));
Al Viro7ed1ee62010-03-23 10:37:36 -0400154 else {
Al Viroc8b91ac2011-03-12 10:41:39 -0500155 buf.f_type = st->f_type;
156 buf.f_bsize = st->f_bsize;
157 buf.f_blocks = st->f_blocks;
158 buf.f_bfree = st->f_bfree;
159 buf.f_bavail = st->f_bavail;
160 buf.f_files = st->f_files;
161 buf.f_ffree = st->f_ffree;
162 buf.f_fsid = st->f_fsid;
163 buf.f_namelen = st->f_namelen;
164 buf.f_frsize = st->f_frsize;
165 buf.f_flags = st->f_flags;
166 memset(buf.f_spare, 0, sizeof(buf.f_spare));
Al Viro7ed1ee62010-03-23 10:37:36 -0400167 }
Al Viroc8b91ac2011-03-12 10:41:39 -0500168 if (copy_to_user(p, &buf, sizeof(buf)))
169 return -EFAULT;
Al Viro7ed1ee62010-03-23 10:37:36 -0400170 return 0;
171}
172
173SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct statfs __user *, buf)
174{
Al Viroc8b91ac2011-03-12 10:41:39 -0500175 struct kstatfs st;
176 int error = user_statfs(pathname, &st);
177 if (!error)
178 error = do_statfs_native(&st, buf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400179 return error;
180}
181
182SYSCALL_DEFINE3(statfs64, const char __user *, pathname, size_t, sz, struct statfs64 __user *, buf)
183{
Al Viroc8b91ac2011-03-12 10:41:39 -0500184 struct kstatfs st;
185 int error;
Al Viro7ed1ee62010-03-23 10:37:36 -0400186 if (sz != sizeof(*buf))
187 return -EINVAL;
Al Viroc8b91ac2011-03-12 10:41:39 -0500188 error = user_statfs(pathname, &st);
189 if (!error)
190 error = do_statfs64(&st, buf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400191 return error;
192}
193
194SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct statfs __user *, buf)
195{
Al Viroc8b91ac2011-03-12 10:41:39 -0500196 struct kstatfs st;
197 int error = fd_statfs(fd, &st);
198 if (!error)
199 error = do_statfs_native(&st, buf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400200 return error;
201}
202
203SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, size_t, sz, struct statfs64 __user *, buf)
204{
Al Viroc8b91ac2011-03-12 10:41:39 -0500205 struct kstatfs st;
Al Viro7ed1ee62010-03-23 10:37:36 -0400206 int error;
207
208 if (sz != sizeof(*buf))
209 return -EINVAL;
210
Al Viroc8b91ac2011-03-12 10:41:39 -0500211 error = fd_statfs(fd, &st);
212 if (!error)
213 error = do_statfs64(&st, buf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400214 return error;
215}
216
Al Virocf31e702012-01-02 22:28:36 -0500217int vfs_ustat(dev_t dev, struct kstatfs *sbuf)
Al Viro7ed1ee62010-03-23 10:37:36 -0400218{
Al Virocf31e702012-01-02 22:28:36 -0500219 struct super_block *s = user_get_super(dev);
Al Viro7ed1ee62010-03-23 10:37:36 -0400220 int err;
Al Viro7ed1ee62010-03-23 10:37:36 -0400221 if (!s)
222 return -EINVAL;
223
Al Virocf31e702012-01-02 22:28:36 -0500224 err = statfs_by_dentry(s->s_root, sbuf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400225 drop_super(s);
Al Virocf31e702012-01-02 22:28:36 -0500226 return err;
227}
228
229SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf)
230{
231 struct ustat tmp;
232 struct kstatfs sbuf;
233 int err = vfs_ustat(new_decode_dev(dev), &sbuf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400234 if (err)
235 return err;
236
237 memset(&tmp,0,sizeof(struct ustat));
238 tmp.f_tfree = sbuf.f_bfree;
239 tmp.f_tinode = sbuf.f_ffree;
240
241 return copy_to_user(ubuf, &tmp, sizeof(struct ustat)) ? -EFAULT : 0;
242}
Al Viro4ada54e2017-04-08 18:08:15 -0400243
244#ifdef CONFIG_COMPAT
245static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
246{
247 if (sizeof ubuf->f_blocks == 4) {
248 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail |
249 kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
250 return -EOVERFLOW;
251 /* f_files and f_ffree may be -1; it's okay
252 * to stuff that into 32 bits */
253 if (kbuf->f_files != 0xffffffffffffffffULL
254 && (kbuf->f_files & 0xffffffff00000000ULL))
255 return -EOVERFLOW;
256 if (kbuf->f_ffree != 0xffffffffffffffffULL
257 && (kbuf->f_ffree & 0xffffffff00000000ULL))
258 return -EOVERFLOW;
259 }
260 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
261 __put_user(kbuf->f_type, &ubuf->f_type) ||
262 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
263 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
264 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
265 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
266 __put_user(kbuf->f_files, &ubuf->f_files) ||
267 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
268 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
269 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
270 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
271 __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
272 __put_user(kbuf->f_flags, &ubuf->f_flags) ||
273 __clear_user(ubuf->f_spare, sizeof(ubuf->f_spare)))
274 return -EFAULT;
275 return 0;
276}
277
278/*
279 * The following statfs calls are copies of code from fs/statfs.c and
280 * should be checked against those from time to time
281 */
282COMPAT_SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct compat_statfs __user *, buf)
283{
284 struct kstatfs tmp;
285 int error = user_statfs(pathname, &tmp);
286 if (!error)
287 error = put_compat_statfs(buf, &tmp);
288 return error;
289}
290
291COMPAT_SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct compat_statfs __user *, buf)
292{
293 struct kstatfs tmp;
294 int error = fd_statfs(fd, &tmp);
295 if (!error)
296 error = put_compat_statfs(buf, &tmp);
297 return error;
298}
299
300static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
301{
302 if (sizeof(ubuf->f_bsize) == 4) {
303 if ((kbuf->f_type | kbuf->f_bsize | kbuf->f_namelen |
304 kbuf->f_frsize | kbuf->f_flags) & 0xffffffff00000000ULL)
305 return -EOVERFLOW;
306 /* f_files and f_ffree may be -1; it's okay
307 * to stuff that into 32 bits */
308 if (kbuf->f_files != 0xffffffffffffffffULL
309 && (kbuf->f_files & 0xffffffff00000000ULL))
310 return -EOVERFLOW;
311 if (kbuf->f_ffree != 0xffffffffffffffffULL
312 && (kbuf->f_ffree & 0xffffffff00000000ULL))
313 return -EOVERFLOW;
314 }
315 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
316 __put_user(kbuf->f_type, &ubuf->f_type) ||
317 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
318 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
319 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
320 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
321 __put_user(kbuf->f_files, &ubuf->f_files) ||
322 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
323 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
324 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
325 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
326 __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
327 __put_user(kbuf->f_flags, &ubuf->f_flags) ||
328 __clear_user(ubuf->f_spare, sizeof(ubuf->f_spare)))
329 return -EFAULT;
330 return 0;
331}
332
333COMPAT_SYSCALL_DEFINE3(statfs64, const char __user *, pathname, compat_size_t, sz, struct compat_statfs64 __user *, buf)
334{
335 struct kstatfs tmp;
336 int error;
337
338 if (sz != sizeof(*buf))
339 return -EINVAL;
340
341 error = user_statfs(pathname, &tmp);
342 if (!error)
343 error = put_compat_statfs64(buf, &tmp);
344 return error;
345}
346
347COMPAT_SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, compat_size_t, sz, struct compat_statfs64 __user *, buf)
348{
349 struct kstatfs tmp;
350 int error;
351
352 if (sz != sizeof(*buf))
353 return -EINVAL;
354
355 error = fd_statfs(fd, &tmp);
356 if (!error)
357 error = put_compat_statfs64(buf, &tmp);
358 return error;
359}
360
361/*
362 * This is a copy of sys_ustat, just dealing with a structure layout.
363 * Given how simple this syscall is that apporach is more maintainable
364 * than the various conversion hacks.
365 */
366COMPAT_SYSCALL_DEFINE2(ustat, unsigned, dev, struct compat_ustat __user *, u)
367{
368 struct compat_ustat tmp;
369 struct kstatfs sbuf;
370 int err = vfs_ustat(new_decode_dev(dev), &sbuf);
371 if (err)
372 return err;
373
374 memset(&tmp, 0, sizeof(struct compat_ustat));
375 tmp.f_tfree = sbuf.f_bfree;
376 tmp.f_tinode = sbuf.f_ffree;
377 if (copy_to_user(u, &tmp, sizeof(struct compat_ustat)))
378 return -EFAULT;
379 return 0;
380}
381#endif