blob: c219e733f55330741962173e994ac8d4e894a70f [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;
Jeff Layton96948fc2012-12-11 12:10:14 -050080 int error;
81 unsigned int lookup_flags = LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT;
82retry:
83 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Al Viroc8b91ac2011-03-12 10:41:39 -050084 if (!error) {
85 error = vfs_statfs(&path, st);
86 path_put(&path);
Jeff Layton96948fc2012-12-11 12:10:14 -050087 if (retry_estale(error, lookup_flags)) {
88 lookup_flags |= LOOKUP_REVAL;
89 goto retry;
90 }
Al Viroc8b91ac2011-03-12 10:41:39 -050091 }
92 return error;
93}
Al Viro7ed1ee62010-03-23 10:37:36 -040094
Al Viroc8b91ac2011-03-12 10:41:39 -050095int fd_statfs(int fd, struct kstatfs *st)
96{
Al Viro2903ff02012-08-28 12:52:22 -040097 struct fd f = fdget(fd);
Al Viroc8b91ac2011-03-12 10:41:39 -050098 int error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -040099 if (f.file) {
100 error = vfs_statfs(&f.file->f_path, st);
101 fdput(f);
Al Viroc8b91ac2011-03-12 10:41:39 -0500102 }
103 return error;
104}
Al Viro7ed1ee62010-03-23 10:37:36 -0400105
Al Viroc8b91ac2011-03-12 10:41:39 -0500106static int do_statfs_native(struct kstatfs *st, struct statfs __user *p)
107{
108 struct statfs buf;
109
110 if (sizeof(buf) == sizeof(*st))
111 memcpy(&buf, st, sizeof(*st));
Al Viro7ed1ee62010-03-23 10:37:36 -0400112 else {
Al Viroc8b91ac2011-03-12 10:41:39 -0500113 if (sizeof buf.f_blocks == 4) {
114 if ((st->f_blocks | st->f_bfree | st->f_bavail |
115 st->f_bsize | st->f_frsize) &
Al Viro7ed1ee62010-03-23 10:37:36 -0400116 0xffffffff00000000ULL)
117 return -EOVERFLOW;
118 /*
119 * f_files and f_ffree may be -1; it's okay to stuff
120 * that into 32 bits
121 */
Al Viroc8b91ac2011-03-12 10:41:39 -0500122 if (st->f_files != -1 &&
123 (st->f_files & 0xffffffff00000000ULL))
Al Viro7ed1ee62010-03-23 10:37:36 -0400124 return -EOVERFLOW;
Al Viroc8b91ac2011-03-12 10:41:39 -0500125 if (st->f_ffree != -1 &&
126 (st->f_ffree & 0xffffffff00000000ULL))
Al Viro7ed1ee62010-03-23 10:37:36 -0400127 return -EOVERFLOW;
128 }
129
Al Viroc8b91ac2011-03-12 10:41:39 -0500130 buf.f_type = st->f_type;
131 buf.f_bsize = st->f_bsize;
132 buf.f_blocks = st->f_blocks;
133 buf.f_bfree = st->f_bfree;
134 buf.f_bavail = st->f_bavail;
135 buf.f_files = st->f_files;
136 buf.f_ffree = st->f_ffree;
137 buf.f_fsid = st->f_fsid;
138 buf.f_namelen = st->f_namelen;
139 buf.f_frsize = st->f_frsize;
140 buf.f_flags = st->f_flags;
141 memset(buf.f_spare, 0, sizeof(buf.f_spare));
Al Viro7ed1ee62010-03-23 10:37:36 -0400142 }
Al Viroc8b91ac2011-03-12 10:41:39 -0500143 if (copy_to_user(p, &buf, sizeof(buf)))
144 return -EFAULT;
Al Viro7ed1ee62010-03-23 10:37:36 -0400145 return 0;
146}
147
Al Viroc8b91ac2011-03-12 10:41:39 -0500148static int do_statfs64(struct kstatfs *st, struct statfs64 __user *p)
Al Viro7ed1ee62010-03-23 10:37:36 -0400149{
Al Viroc8b91ac2011-03-12 10:41:39 -0500150 struct statfs64 buf;
151 if (sizeof(buf) == sizeof(*st))
152 memcpy(&buf, st, sizeof(*st));
Al Viro7ed1ee62010-03-23 10:37:36 -0400153 else {
Al Viroc8b91ac2011-03-12 10:41:39 -0500154 buf.f_type = st->f_type;
155 buf.f_bsize = st->f_bsize;
156 buf.f_blocks = st->f_blocks;
157 buf.f_bfree = st->f_bfree;
158 buf.f_bavail = st->f_bavail;
159 buf.f_files = st->f_files;
160 buf.f_ffree = st->f_ffree;
161 buf.f_fsid = st->f_fsid;
162 buf.f_namelen = st->f_namelen;
163 buf.f_frsize = st->f_frsize;
164 buf.f_flags = st->f_flags;
165 memset(buf.f_spare, 0, sizeof(buf.f_spare));
Al Viro7ed1ee62010-03-23 10:37:36 -0400166 }
Al Viroc8b91ac2011-03-12 10:41:39 -0500167 if (copy_to_user(p, &buf, sizeof(buf)))
168 return -EFAULT;
Al Viro7ed1ee62010-03-23 10:37:36 -0400169 return 0;
170}
171
172SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct statfs __user *, buf)
173{
Al Viroc8b91ac2011-03-12 10:41:39 -0500174 struct kstatfs st;
175 int error = user_statfs(pathname, &st);
176 if (!error)
177 error = do_statfs_native(&st, buf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400178 return error;
179}
180
181SYSCALL_DEFINE3(statfs64, const char __user *, pathname, size_t, sz, struct statfs64 __user *, buf)
182{
Al Viroc8b91ac2011-03-12 10:41:39 -0500183 struct kstatfs st;
184 int error;
Al Viro7ed1ee62010-03-23 10:37:36 -0400185 if (sz != sizeof(*buf))
186 return -EINVAL;
Al Viroc8b91ac2011-03-12 10:41:39 -0500187 error = user_statfs(pathname, &st);
188 if (!error)
189 error = do_statfs64(&st, buf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400190 return error;
191}
192
193SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct statfs __user *, buf)
194{
Al Viroc8b91ac2011-03-12 10:41:39 -0500195 struct kstatfs st;
196 int error = fd_statfs(fd, &st);
197 if (!error)
198 error = do_statfs_native(&st, buf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400199 return error;
200}
201
202SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, size_t, sz, struct statfs64 __user *, buf)
203{
Al Viroc8b91ac2011-03-12 10:41:39 -0500204 struct kstatfs st;
Al Viro7ed1ee62010-03-23 10:37:36 -0400205 int error;
206
207 if (sz != sizeof(*buf))
208 return -EINVAL;
209
Al Viroc8b91ac2011-03-12 10:41:39 -0500210 error = fd_statfs(fd, &st);
211 if (!error)
212 error = do_statfs64(&st, buf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400213 return error;
214}
215
Al Virocf31e702012-01-02 22:28:36 -0500216int vfs_ustat(dev_t dev, struct kstatfs *sbuf)
Al Viro7ed1ee62010-03-23 10:37:36 -0400217{
Al Virocf31e702012-01-02 22:28:36 -0500218 struct super_block *s = user_get_super(dev);
Al Viro7ed1ee62010-03-23 10:37:36 -0400219 int err;
Al Viro7ed1ee62010-03-23 10:37:36 -0400220 if (!s)
221 return -EINVAL;
222
Al Virocf31e702012-01-02 22:28:36 -0500223 err = statfs_by_dentry(s->s_root, sbuf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400224 drop_super(s);
Al Virocf31e702012-01-02 22:28:36 -0500225 return err;
226}
227
228SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf)
229{
230 struct ustat tmp;
231 struct kstatfs sbuf;
232 int err = vfs_ustat(new_decode_dev(dev), &sbuf);
Al Viro7ed1ee62010-03-23 10:37:36 -0400233 if (err)
234 return err;
235
236 memset(&tmp,0,sizeof(struct ustat));
237 tmp.f_tfree = sbuf.f_bfree;
238 tmp.f_tinode = sbuf.f_ffree;
239
240 return copy_to_user(ubuf, &tmp, sizeof(struct ustat)) ? -EFAULT : 0;
241}