1 #include <linux/syscalls.h>
2 #include <linux/module.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>
11 static int flags_by_mnt(int mnt_flags)
15 if (mnt_flags & MNT_READONLY)
17 if (mnt_flags & MNT_NOSUID)
19 if (mnt_flags & MNT_NODEV)
21 if (mnt_flags & MNT_NOEXEC)
23 if (mnt_flags & MNT_NOATIME)
25 if (mnt_flags & MNT_NODIRATIME)
26 flags |= ST_NODIRATIME;
27 if (mnt_flags & MNT_RELATIME)
32 static int flags_by_sb(int s_flags)
35 if (s_flags & MS_SYNCHRONOUS)
36 flags |= ST_SYNCHRONOUS;
37 if (s_flags & MS_MANDLOCK)
42 static int calculate_f_flags(struct vfsmount *mnt)
44 return ST_VALID | flags_by_mnt(mnt->mnt_flags) |
45 flags_by_sb(mnt->mnt_sb->s_flags);
48 int statfs_by_dentry(struct dentry *dentry, struct kstatfs *buf)
52 if (!dentry->d_sb->s_op->statfs)
55 memset(buf, 0, sizeof(*buf));
56 retval = security_sb_statfs(dentry);
59 retval = dentry->d_sb->s_op->statfs(dentry, buf);
60 if (retval == 0 && buf->f_frsize == 0)
61 buf->f_frsize = buf->f_bsize;
64 EXPORT_SYMBOL_GPL(statfs_by_dentry);
66 int vfs_statfs(struct path *path, struct kstatfs *buf)
70 error = statfs_by_dentry(path->dentry, buf);
72 buf->f_flags = calculate_f_flags(path->mnt);
75 EXPORT_SYMBOL(vfs_statfs);
77 static int do_statfs_native(struct path *path, struct statfs *buf)
82 retval = vfs_statfs(path, &st);
86 if (sizeof(*buf) == sizeof(st))
87 memcpy(buf, &st, sizeof(st));
89 if (sizeof buf->f_blocks == 4) {
90 if ((st.f_blocks | st.f_bfree | st.f_bavail |
91 st.f_bsize | st.f_frsize) &
92 0xffffffff00000000ULL)
95 * f_files and f_ffree may be -1; it's okay to stuff
98 if (st.f_files != -1 &&
99 (st.f_files & 0xffffffff00000000ULL))
101 if (st.f_ffree != -1 &&
102 (st.f_ffree & 0xffffffff00000000ULL))
106 buf->f_type = st.f_type;
107 buf->f_bsize = st.f_bsize;
108 buf->f_blocks = st.f_blocks;
109 buf->f_bfree = st.f_bfree;
110 buf->f_bavail = st.f_bavail;
111 buf->f_files = st.f_files;
112 buf->f_ffree = st.f_ffree;
113 buf->f_fsid = st.f_fsid;
114 buf->f_namelen = st.f_namelen;
115 buf->f_frsize = st.f_frsize;
116 buf->f_flags = st.f_flags;
117 memset(buf->f_spare, 0, sizeof(buf->f_spare));
122 static int do_statfs64(struct path *path, struct statfs64 *buf)
127 retval = vfs_statfs(path, &st);
131 if (sizeof(*buf) == sizeof(st))
132 memcpy(buf, &st, sizeof(st));
134 buf->f_type = st.f_type;
135 buf->f_bsize = st.f_bsize;
136 buf->f_blocks = st.f_blocks;
137 buf->f_bfree = st.f_bfree;
138 buf->f_bavail = st.f_bavail;
139 buf->f_files = st.f_files;
140 buf->f_ffree = st.f_ffree;
141 buf->f_fsid = st.f_fsid;
142 buf->f_namelen = st.f_namelen;
143 buf->f_frsize = st.f_frsize;
144 buf->f_flags = st.f_flags;
145 memset(buf->f_spare, 0, sizeof(buf->f_spare));
150 SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct statfs __user *, buf)
155 error = user_path(pathname, &path);
158 error = do_statfs_native(&path, &tmp);
159 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
166 SYSCALL_DEFINE3(statfs64, const char __user *, pathname, size_t, sz, struct statfs64 __user *, buf)
171 if (sz != sizeof(*buf))
173 error = user_path(pathname, &path);
176 error = do_statfs64(&path, &tmp);
177 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
184 SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct statfs __user *, buf)
194 error = do_statfs_native(&file->f_path, &tmp);
195 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
202 SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, size_t, sz, struct statfs64 __user *, buf)
208 if (sz != sizeof(*buf))
215 error = do_statfs64(&file->f_path, &tmp);
216 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
223 SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf)
225 struct super_block *s;
230 s = user_get_super(new_decode_dev(dev));
234 err = statfs_by_dentry(s->s_root, &sbuf);
239 memset(&tmp,0,sizeof(struct ustat));
240 tmp.f_tfree = sbuf.f_bfree;
241 tmp.f_tinode = sbuf.f_ffree;
243 return copy_to_user(ubuf, &tmp, sizeof(struct ustat)) ? -EFAULT : 0;