1 #include <linux/syscalls.h>
2 #include <linux/export.h>
4 #include <linux/file.h>
5 #include <linux/mount.h>
6 #include <linux/namei.h>
7 #include <linux/statfs.h>
8 #include <linux/security.h>
9 #include <linux/uaccess.h>
10 #include <linux/compat.h>
13 static int flags_by_mnt(int mnt_flags)
17 if (mnt_flags & MNT_READONLY)
19 if (mnt_flags & MNT_NOSUID)
21 if (mnt_flags & MNT_NODEV)
23 if (mnt_flags & MNT_NOEXEC)
25 if (mnt_flags & MNT_NOATIME)
27 if (mnt_flags & MNT_NODIRATIME)
28 flags |= ST_NODIRATIME;
29 if (mnt_flags & MNT_RELATIME)
34 static int flags_by_sb(int s_flags)
37 if (s_flags & MS_SYNCHRONOUS)
38 flags |= ST_SYNCHRONOUS;
39 if (s_flags & MS_MANDLOCK)
44 static int calculate_f_flags(struct vfsmount *mnt)
46 return ST_VALID | flags_by_mnt(mnt->mnt_flags) |
47 flags_by_sb(mnt->mnt_sb->s_flags);
50 static int statfs_by_dentry(struct dentry *dentry, struct kstatfs *buf)
54 if (!dentry->d_sb->s_op->statfs)
57 memset(buf, 0, sizeof(*buf));
58 retval = security_sb_statfs(dentry);
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;
67 int vfs_statfs(const struct path *path, struct kstatfs *buf)
71 error = statfs_by_dentry(path->dentry, buf);
73 buf->f_flags = calculate_f_flags(path->mnt);
76 EXPORT_SYMBOL(vfs_statfs);
78 int user_statfs(const char __user *pathname, struct kstatfs *st)
82 unsigned int lookup_flags = LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT;
84 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
86 error = vfs_statfs(&path, st);
88 if (retry_estale(error, lookup_flags)) {
89 lookup_flags |= LOOKUP_REVAL;
96 int fd_statfs(int fd, struct kstatfs *st)
98 struct fd f = fdget_raw(fd);
101 error = vfs_statfs(&f.file->f_path, st);
107 static int do_statfs_native(struct kstatfs *st, struct statfs __user *p)
111 if (sizeof(buf) == sizeof(*st))
112 memcpy(&buf, st, sizeof(*st));
114 if (sizeof buf.f_blocks == 4) {
115 if ((st->f_blocks | st->f_bfree | st->f_bavail |
116 st->f_bsize | st->f_frsize) &
117 0xffffffff00000000ULL)
120 * f_files and f_ffree may be -1; it's okay to stuff
123 if (st->f_files != -1 &&
124 (st->f_files & 0xffffffff00000000ULL))
126 if (st->f_ffree != -1 &&
127 (st->f_ffree & 0xffffffff00000000ULL))
131 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));
144 if (copy_to_user(p, &buf, sizeof(buf)))
149 static int do_statfs64(struct kstatfs *st, struct statfs64 __user *p)
152 if (sizeof(buf) == sizeof(*st))
153 memcpy(&buf, st, sizeof(*st));
155 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));
168 if (copy_to_user(p, &buf, sizeof(buf)))
173 SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct statfs __user *, buf)
176 int error = user_statfs(pathname, &st);
178 error = do_statfs_native(&st, buf);
182 SYSCALL_DEFINE3(statfs64, const char __user *, pathname, size_t, sz, struct statfs64 __user *, buf)
186 if (sz != sizeof(*buf))
188 error = user_statfs(pathname, &st);
190 error = do_statfs64(&st, buf);
194 SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct statfs __user *, buf)
197 int error = fd_statfs(fd, &st);
199 error = do_statfs_native(&st, buf);
203 SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, size_t, sz, struct statfs64 __user *, buf)
208 if (sz != sizeof(*buf))
211 error = fd_statfs(fd, &st);
213 error = do_statfs64(&st, buf);
217 int vfs_ustat(dev_t dev, struct kstatfs *sbuf)
219 struct super_block *s = user_get_super(dev);
224 err = statfs_by_dentry(s->s_root, sbuf);
229 SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf)
233 int err = vfs_ustat(new_decode_dev(dev), &sbuf);
237 memset(&tmp,0,sizeof(struct ustat));
238 tmp.f_tfree = sbuf.f_bfree;
239 tmp.f_tinode = sbuf.f_ffree;
241 return copy_to_user(ubuf, &tmp, sizeof(struct ustat)) ? -EFAULT : 0;
245 static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
247 struct compat_statfs buf;
248 if (sizeof ubuf->f_blocks == 4) {
249 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail |
250 kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
252 /* f_files and f_ffree may be -1; it's okay
253 * to stuff that into 32 bits */
254 if (kbuf->f_files != 0xffffffffffffffffULL
255 && (kbuf->f_files & 0xffffffff00000000ULL))
257 if (kbuf->f_ffree != 0xffffffffffffffffULL
258 && (kbuf->f_ffree & 0xffffffff00000000ULL))
261 memset(&buf, 0, sizeof(struct compat_statfs));
262 buf.f_type = kbuf->f_type;
263 buf.f_bsize = kbuf->f_bsize;
264 buf.f_blocks = kbuf->f_blocks;
265 buf.f_bfree = kbuf->f_bfree;
266 buf.f_bavail = kbuf->f_bavail;
267 buf.f_files = kbuf->f_files;
268 buf.f_ffree = kbuf->f_ffree;
269 buf.f_namelen = kbuf->f_namelen;
270 buf.f_fsid.val[0] = kbuf->f_fsid.val[0];
271 buf.f_fsid.val[1] = kbuf->f_fsid.val[1];
272 buf.f_frsize = kbuf->f_frsize;
273 buf.f_flags = kbuf->f_flags;
274 if (copy_to_user(ubuf, &buf, sizeof(struct compat_statfs)))
280 * The following statfs calls are copies of code from fs/statfs.c and
281 * should be checked against those from time to time
283 COMPAT_SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct compat_statfs __user *, buf)
286 int error = user_statfs(pathname, &tmp);
288 error = put_compat_statfs(buf, &tmp);
292 COMPAT_SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct compat_statfs __user *, buf)
295 int error = fd_statfs(fd, &tmp);
297 error = put_compat_statfs(buf, &tmp);
301 static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
303 struct compat_statfs64 buf;
304 if (sizeof(ubuf->f_bsize) == 4) {
305 if ((kbuf->f_type | kbuf->f_bsize | kbuf->f_namelen |
306 kbuf->f_frsize | kbuf->f_flags) & 0xffffffff00000000ULL)
308 /* f_files and f_ffree may be -1; it's okay
309 * to stuff that into 32 bits */
310 if (kbuf->f_files != 0xffffffffffffffffULL
311 && (kbuf->f_files & 0xffffffff00000000ULL))
313 if (kbuf->f_ffree != 0xffffffffffffffffULL
314 && (kbuf->f_ffree & 0xffffffff00000000ULL))
317 memset(&buf, 0, sizeof(struct compat_statfs64));
318 buf.f_type = kbuf->f_type;
319 buf.f_bsize = kbuf->f_bsize;
320 buf.f_blocks = kbuf->f_blocks;
321 buf.f_bfree = kbuf->f_bfree;
322 buf.f_bavail = kbuf->f_bavail;
323 buf.f_files = kbuf->f_files;
324 buf.f_ffree = kbuf->f_ffree;
325 buf.f_namelen = kbuf->f_namelen;
326 buf.f_fsid.val[0] = kbuf->f_fsid.val[0];
327 buf.f_fsid.val[1] = kbuf->f_fsid.val[1];
328 buf.f_frsize = kbuf->f_frsize;
329 buf.f_flags = kbuf->f_flags;
330 if (copy_to_user(ubuf, &buf, sizeof(struct compat_statfs64)))
335 COMPAT_SYSCALL_DEFINE3(statfs64, const char __user *, pathname, compat_size_t, sz, struct compat_statfs64 __user *, buf)
340 if (sz != sizeof(*buf))
343 error = user_statfs(pathname, &tmp);
345 error = put_compat_statfs64(buf, &tmp);
349 COMPAT_SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, compat_size_t, sz, struct compat_statfs64 __user *, buf)
354 if (sz != sizeof(*buf))
357 error = fd_statfs(fd, &tmp);
359 error = put_compat_statfs64(buf, &tmp);
364 * This is a copy of sys_ustat, just dealing with a structure layout.
365 * Given how simple this syscall is that apporach is more maintainable
366 * than the various conversion hacks.
368 COMPAT_SYSCALL_DEFINE2(ustat, unsigned, dev, struct compat_ustat __user *, u)
370 struct compat_ustat tmp;
372 int err = vfs_ustat(new_decode_dev(dev), &sbuf);
376 memset(&tmp, 0, sizeof(struct compat_ustat));
377 tmp.f_tfree = sbuf.f_bfree;
378 tmp.f_tinode = sbuf.f_ffree;
379 if (copy_to_user(u, &tmp, sizeof(struct compat_ustat)))