1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/syscalls.h>
3 #include <linux/export.h>
5 #include <linux/file.h>
6 #include <linux/mount.h>
7 #include <linux/namei.h>
8 #include <linux/statfs.h>
9 #include <linux/security.h>
10 #include <linux/uaccess.h>
11 #include <linux/compat.h>
14 static int flags_by_mnt(int mnt_flags)
18 if (mnt_flags & MNT_READONLY)
20 if (mnt_flags & MNT_NOSUID)
22 if (mnt_flags & MNT_NODEV)
24 if (mnt_flags & MNT_NOEXEC)
26 if (mnt_flags & MNT_NOATIME)
28 if (mnt_flags & MNT_NODIRATIME)
29 flags |= ST_NODIRATIME;
30 if (mnt_flags & MNT_RELATIME)
32 if (mnt_flags & MNT_NOSYMFOLLOW)
33 flags |= ST_NOSYMFOLLOW;
37 static int flags_by_sb(int s_flags)
40 if (s_flags & SB_SYNCHRONOUS)
41 flags |= ST_SYNCHRONOUS;
42 if (s_flags & SB_MANDLOCK)
44 if (s_flags & SB_RDONLY)
49 static int calculate_f_flags(struct vfsmount *mnt)
51 return ST_VALID | flags_by_mnt(mnt->mnt_flags) |
52 flags_by_sb(mnt->mnt_sb->s_flags);
55 static int statfs_by_dentry(struct dentry *dentry, struct kstatfs *buf)
59 if (!dentry->d_sb->s_op->statfs)
62 memset(buf, 0, sizeof(*buf));
63 retval = security_sb_statfs(dentry);
66 retval = dentry->d_sb->s_op->statfs(dentry, buf);
67 if (retval == 0 && buf->f_frsize == 0)
68 buf->f_frsize = buf->f_bsize;
72 int vfs_get_fsid(struct dentry *dentry, __kernel_fsid_t *fsid)
77 error = statfs_by_dentry(dentry, &st);
84 EXPORT_SYMBOL(vfs_get_fsid);
86 int vfs_statfs(const struct path *path, struct kstatfs *buf)
90 error = statfs_by_dentry(path->dentry, buf);
92 buf->f_flags = calculate_f_flags(path->mnt);
95 EXPORT_SYMBOL(vfs_statfs);
97 int user_statfs(const char __user *pathname, struct kstatfs *st)
101 unsigned int lookup_flags = LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT;
103 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
105 error = vfs_statfs(&path, st);
107 if (retry_estale(error, lookup_flags)) {
108 lookup_flags |= LOOKUP_REVAL;
115 int fd_statfs(int fd, struct kstatfs *st)
117 struct fd f = fdget_raw(fd);
120 error = vfs_statfs(&f.file->f_path, st);
126 static int do_statfs_native(struct kstatfs *st, struct statfs __user *p)
130 if (sizeof(buf) == sizeof(*st))
131 memcpy(&buf, st, sizeof(*st));
133 if (sizeof buf.f_blocks == 4) {
134 if ((st->f_blocks | st->f_bfree | st->f_bavail |
135 st->f_bsize | st->f_frsize) &
136 0xffffffff00000000ULL)
139 * f_files and f_ffree may be -1; it's okay to stuff
142 if (st->f_files != -1 &&
143 (st->f_files & 0xffffffff00000000ULL))
145 if (st->f_ffree != -1 &&
146 (st->f_ffree & 0xffffffff00000000ULL))
150 buf.f_type = st->f_type;
151 buf.f_bsize = st->f_bsize;
152 buf.f_blocks = st->f_blocks;
153 buf.f_bfree = st->f_bfree;
154 buf.f_bavail = st->f_bavail;
155 buf.f_files = st->f_files;
156 buf.f_ffree = st->f_ffree;
157 buf.f_fsid = st->f_fsid;
158 buf.f_namelen = st->f_namelen;
159 buf.f_frsize = st->f_frsize;
160 buf.f_flags = st->f_flags;
161 memset(buf.f_spare, 0, sizeof(buf.f_spare));
163 if (copy_to_user(p, &buf, sizeof(buf)))
168 static int do_statfs64(struct kstatfs *st, struct statfs64 __user *p)
171 if (sizeof(buf) == sizeof(*st))
172 memcpy(&buf, st, sizeof(*st));
174 buf.f_type = st->f_type;
175 buf.f_bsize = st->f_bsize;
176 buf.f_blocks = st->f_blocks;
177 buf.f_bfree = st->f_bfree;
178 buf.f_bavail = st->f_bavail;
179 buf.f_files = st->f_files;
180 buf.f_ffree = st->f_ffree;
181 buf.f_fsid = st->f_fsid;
182 buf.f_namelen = st->f_namelen;
183 buf.f_frsize = st->f_frsize;
184 buf.f_flags = st->f_flags;
185 memset(buf.f_spare, 0, sizeof(buf.f_spare));
187 if (copy_to_user(p, &buf, sizeof(buf)))
192 SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct statfs __user *, buf)
195 int error = user_statfs(pathname, &st);
197 error = do_statfs_native(&st, buf);
201 SYSCALL_DEFINE3(statfs64, const char __user *, pathname, size_t, sz, struct statfs64 __user *, buf)
205 if (sz != sizeof(*buf))
207 error = user_statfs(pathname, &st);
209 error = do_statfs64(&st, buf);
213 SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct statfs __user *, buf)
216 int error = fd_statfs(fd, &st);
218 error = do_statfs_native(&st, buf);
222 SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, size_t, sz, struct statfs64 __user *, buf)
227 if (sz != sizeof(*buf))
230 error = fd_statfs(fd, &st);
232 error = do_statfs64(&st, buf);
236 static int vfs_ustat(dev_t dev, struct kstatfs *sbuf)
238 struct super_block *s = user_get_super(dev, false);
243 err = statfs_by_dentry(s->s_root, sbuf);
248 SYSCALL_DEFINE2(ustat, unsigned, dev, struct ustat __user *, ubuf)
252 int err = vfs_ustat(new_decode_dev(dev), &sbuf);
256 memset(&tmp,0,sizeof(struct ustat));
257 tmp.f_tfree = sbuf.f_bfree;
258 tmp.f_tinode = sbuf.f_ffree;
260 return copy_to_user(ubuf, &tmp, sizeof(struct ustat)) ? -EFAULT : 0;
264 static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
266 struct compat_statfs buf;
267 if (sizeof ubuf->f_blocks == 4) {
268 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail |
269 kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
271 /* f_files and f_ffree may be -1; it's okay
272 * to stuff that into 32 bits */
273 if (kbuf->f_files != 0xffffffffffffffffULL
274 && (kbuf->f_files & 0xffffffff00000000ULL))
276 if (kbuf->f_ffree != 0xffffffffffffffffULL
277 && (kbuf->f_ffree & 0xffffffff00000000ULL))
280 memset(&buf, 0, sizeof(struct compat_statfs));
281 buf.f_type = kbuf->f_type;
282 buf.f_bsize = kbuf->f_bsize;
283 buf.f_blocks = kbuf->f_blocks;
284 buf.f_bfree = kbuf->f_bfree;
285 buf.f_bavail = kbuf->f_bavail;
286 buf.f_files = kbuf->f_files;
287 buf.f_ffree = kbuf->f_ffree;
288 buf.f_namelen = kbuf->f_namelen;
289 buf.f_fsid.val[0] = kbuf->f_fsid.val[0];
290 buf.f_fsid.val[1] = kbuf->f_fsid.val[1];
291 buf.f_frsize = kbuf->f_frsize;
292 buf.f_flags = kbuf->f_flags;
293 if (copy_to_user(ubuf, &buf, sizeof(struct compat_statfs)))
299 * The following statfs calls are copies of code from fs/statfs.c and
300 * should be checked against those from time to time
302 COMPAT_SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct compat_statfs __user *, buf)
305 int error = user_statfs(pathname, &tmp);
307 error = put_compat_statfs(buf, &tmp);
311 COMPAT_SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct compat_statfs __user *, buf)
314 int error = fd_statfs(fd, &tmp);
316 error = put_compat_statfs(buf, &tmp);
320 static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
322 struct compat_statfs64 buf;
324 if ((kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
327 memset(&buf, 0, sizeof(struct compat_statfs64));
328 buf.f_type = kbuf->f_type;
329 buf.f_bsize = kbuf->f_bsize;
330 buf.f_blocks = kbuf->f_blocks;
331 buf.f_bfree = kbuf->f_bfree;
332 buf.f_bavail = kbuf->f_bavail;
333 buf.f_files = kbuf->f_files;
334 buf.f_ffree = kbuf->f_ffree;
335 buf.f_namelen = kbuf->f_namelen;
336 buf.f_fsid.val[0] = kbuf->f_fsid.val[0];
337 buf.f_fsid.val[1] = kbuf->f_fsid.val[1];
338 buf.f_frsize = kbuf->f_frsize;
339 buf.f_flags = kbuf->f_flags;
340 if (copy_to_user(ubuf, &buf, sizeof(struct compat_statfs64)))
345 int kcompat_sys_statfs64(const char __user * pathname, compat_size_t sz, struct compat_statfs64 __user * buf)
350 if (sz != sizeof(*buf))
353 error = user_statfs(pathname, &tmp);
355 error = put_compat_statfs64(buf, &tmp);
359 COMPAT_SYSCALL_DEFINE3(statfs64, const char __user *, pathname, compat_size_t, sz, struct compat_statfs64 __user *, buf)
361 return kcompat_sys_statfs64(pathname, sz, buf);
364 int kcompat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user * buf)
369 if (sz != sizeof(*buf))
372 error = fd_statfs(fd, &tmp);
374 error = put_compat_statfs64(buf, &tmp);
378 COMPAT_SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, compat_size_t, sz, struct compat_statfs64 __user *, buf)
380 return kcompat_sys_fstatfs64(fd, sz, buf);
384 * This is a copy of sys_ustat, just dealing with a structure layout.
385 * Given how simple this syscall is that apporach is more maintainable
386 * than the various conversion hacks.
388 COMPAT_SYSCALL_DEFINE2(ustat, unsigned, dev, struct compat_ustat __user *, u)
390 struct compat_ustat tmp;
392 int err = vfs_ustat(new_decode_dev(dev), &sbuf);
396 memset(&tmp, 0, sizeof(struct compat_ustat));
397 tmp.f_tfree = sbuf.f_bfree;
398 tmp.f_tinode = sbuf.f_ffree;
399 if (copy_to_user(u, &tmp, sizeof(struct compat_ustat)))