blob: f8e832e6f0a2220d0e16163b230ad0f710ee4132 [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 Virocf31e702012-01-02 22:28:36 -050010#include "internal.h"
Al Viro7ed1ee62010-03-23 10:37:36 -040011
Christoph Hellwig365b1812010-07-07 18:53:25 +020012static 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
33static 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
43static 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 Virocf31e702012-01-02 22:28:36 -050049static int statfs_by_dentry(struct dentry *dentry, struct kstatfs *buf)
Al Viro7ed1ee62010-03-23 10:37:36 -040050{
Christoph Hellwigebabe9a2010-07-07 18:53:11 +020051 int retval;
Al Viro7ed1ee62010-03-23 10:37:36 -040052
Christoph Hellwigebabe9a2010-07-07 18:53:11 +020053 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 Viro7ed1ee62010-03-23 10:37:36 -040063 return retval;
64}
65
Christoph Hellwigebabe9a2010-07-07 18:53:11 +020066int vfs_statfs(struct path *path, struct kstatfs *buf)
67{
Christoph Hellwig365b1812010-07-07 18:53:25 +020068 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 Hellwigebabe9a2010-07-07 18:53:11 +020074}
Al Viro7ed1ee62010-03-23 10:37:36 -040075EXPORT_SYMBOL(vfs_statfs);
76
Al Viroc8b91ac2011-03-12 10:41:39 -050077int user_statfs(const char __user *pathname, struct kstatfs *st)
Al Viro7ed1ee62010-03-23 10:37:36 -040078{
Al Viroc8b91ac2011-03-12 10:41:39 -050079 struct path path;
Dan McGee5c8a0fb2011-11-01 18:23:10 -050080 int error = user_path_at(AT_FDCWD, pathname, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
Al Viroc8b91ac2011-03-12 10:41:39 -050081 if (!error) {
82 error = vfs_statfs(&path, st);
83 path_put(&path);
84 }
85 return error;
86}
Al Viro7ed1ee62010-03-23 10:37:36 -040087
Al Viroc8b91ac2011-03-12 10:41:39 -050088int fd_statfs(int fd, struct kstatfs *st)
89{
Al Viro2903ff02012-08-28 12:52:22 -040090 struct fd f = fdget(fd);
Al Viroc8b91ac2011-03-12 10:41:39 -050091 int error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -040092 if (f.file) {
93 error = vfs_statfs(&f.file->f_path, st);
94 fdput(f);
Al Viroc8b91ac2011-03-12 10:41:39 -050095 }
96 return error;
97}
Al Viro7ed1ee62010-03-23 10:37:36 -040098
Al Viroc8b91ac2011-03-12 10:41:39 -050099static int do_statfs_native(struct kstatfs *st, struct statfs __user *p)
100{
101 struct statfs buf;
102
103 if (sizeof(buf) == sizeof(*st))
104 memcpy(&buf, st, sizeof(*st));
Al Viro7ed1ee62010-03-23 10:37:36 -0400105 else {
Al Viroc8b91ac2011-03-12 10:41:39 -0500106 if (sizeof buf.f_blocks == 4) {
107 if ((st->f_blocks | st->f_bfree | st->f_bavail |
108 st->f_bsize | st->f_frsize) &
Al Viro7ed1ee62010-03-23 10:37:36 -0400109 0xffffffff00000000ULL)
110 return -EOVERFLOW;
111 /*
112 * f_files and f_ffree may be -1; it's okay to stuff
113 * that into 32 bits
114 */
Al Viroc8b91ac2011-03-12 10:41:39 -0500115 if (st->f_files != -1 &&
116 (st->f_files & 0xffffffff00000000ULL))
Al Viro7ed1ee62010-03-23 10:37:36 -0400117 return -EOVERFLOW;
Al Viroc8b91ac2011-03-12 10:41:39 -0500118 if (st->f_ffree != -1 &&
119 (st->f_ffree & 0xffffffff00000000ULL))
Al Viro7ed1ee62010-03-23 10:37:36 -0400120 return -EOVERFLOW;
121 }
122
Al Viroc8b91ac2011-03-12 10:41:39 -0500123 buf.f_type = st->f_type;
124 buf.f_bsize = st->f_bsize;
125 buf.f_blocks = st->f_blocks;
126 buf.f_bfree = st->f_bfree;
127 buf.f_bavail = st->f_bavail;
128 buf.f_files = st->f_files;
129 buf.f_ffree = st->f_ffree;
130 buf.f_fsid = st->f_fsid;
131 buf.f_namelen = st->f_namelen;
132 buf.f_frsize = st->f_frsize;
133 buf.f_flags = st->f_flags;
134 memset(buf.f_spare, 0, sizeof(buf.f_spare));
Al Viro7ed1ee62010-03-23 10:37:36 -0400135 }
Al Viroc8b91ac2011-03-12 10:41:39 -0500136 if (copy_to_user(p, &buf, sizeof(buf)))
137 return -EFAULT;
Al Viro7ed1ee62010-03-23 10:37:36 -0400138 return 0;
139}
140
Al Viroc8b91ac2011-03-12 10:41:39 -0500141static int do_statfs64(struct kstatfs *st, struct statfs64 __user *p)
Al Viro7ed1ee62010-03-23 10:37:36 -0400142{
Al Viroc8b91ac2011-03-12 10:41:39 -0500143 struct statfs64 buf;
144 if (sizeof(buf) == sizeof(*st))
145 memcpy(&buf, st, sizeof(*st));
Al Viro7ed1ee62010-03-23 10:37:36 -0400146 else {
Al Viroc8b91ac2011-03-12 10:41:39 -0500147 buf.f_type = st->f_type;
148 buf.f_bsize = st->f_bsize;
149 buf.f_blocks = st->f_blocks;
150 buf.f_bfree = st->f_bfree;
151 buf.f_bavail = st->f_bavail;
152 buf.f_files = st->f_files;
153 buf.f_ffree = st->f_ffree;
154 buf.f_fsid = st->f_fsid;
155 buf.f_namelen = st->f_namelen;
156 buf.f_frsize = st->f_frsize;
157 buf.f_flags = st->f_flags;
158 memset(buf.f_spare, 0, sizeof(buf.f_spare));
Al Viro7ed1ee62010-03-23 10:37:36 -0400159 }
Al Viroc8b91ac2011-03-12 10:41:39 -0500160 if (copy_to_user(p, &buf, sizeof(buf)))
161 return -EFAULT;
Al Viro7ed1ee62010-03-23 10:37:36 -0400162 return 0;
163}
164
165SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct statfs __user *, buf)
166{
Al Viroc8b91ac2011-03-12 10:41:39 -0500167 struct kstatfs st;
168 int error = user_statfs(pathname, &st);
169 if (!error)
170 error = do_statfs_native(&st, buf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400171 return error;
172}
173
174SYSCALL_DEFINE3(statfs64, const char __user *, pathname, size_t, sz, struct statfs64 __user *, buf)
175{
Al Viroc8b91ac2011-03-12 10:41:39 -0500176 struct kstatfs st;
177 int error;
Al Viro7ed1ee62010-03-23 10:37:36 -0400178 if (sz != sizeof(*buf))
179 return -EINVAL;
Al Viroc8b91ac2011-03-12 10:41:39 -0500180 error = user_statfs(pathname, &st);
181 if (!error)
182 error = do_statfs64(&st, buf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400183 return error;
184}
185
186SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct statfs __user *, buf)
187{
Al Viroc8b91ac2011-03-12 10:41:39 -0500188 struct kstatfs st;
189 int error = fd_statfs(fd, &st);
190 if (!error)
191 error = do_statfs_native(&st, buf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400192 return error;
193}
194
195SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, size_t, sz, struct statfs64 __user *, buf)
196{
Al Viroc8b91ac2011-03-12 10:41:39 -0500197 struct kstatfs st;
Al Viro7ed1ee62010-03-23 10:37:36 -0400198 int error;
199
200 if (sz != sizeof(*buf))
201 return -EINVAL;
202
Al Viroc8b91ac2011-03-12 10:41:39 -0500203 error = fd_statfs(fd, &st);
204 if (!error)
205 error = do_statfs64(&st, buf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400206 return error;
207}
208
Al Virocf31e702012-01-02 22:28:36 -0500209int vfs_ustat(dev_t dev, struct kstatfs *sbuf)
Al Viro7ed1ee62010-03-23 10:37:36 -0400210{
Al Virocf31e702012-01-02 22:28:36 -0500211 struct super_block *s = user_get_super(dev);
Al Viro7ed1ee62010-03-23 10:37:36 -0400212 int err;
Al Viro7ed1ee62010-03-23 10:37:36 -0400213 if (!s)
214 return -EINVAL;
215
Al Virocf31e702012-01-02 22:28:36 -0500216 err = statfs_by_dentry(s->s_root, sbuf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400217 drop_super(s);
Al Virocf31e702012-01-02 22:28:36 -0500218 return err;
219}
220
221SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf)
222{
223 struct ustat tmp;
224 struct kstatfs sbuf;
225 int err = vfs_ustat(new_decode_dev(dev), &sbuf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400226 if (err)
227 return err;
228
229 memset(&tmp,0,sizeof(struct ustat));
230 tmp.f_tfree = sbuf.f_bfree;
231 tmp.f_tinode = sbuf.f_ffree;
232
233 return copy_to_user(ubuf, &tmp, sizeof(struct ustat)) ? -EFAULT : 0;
234}