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
)
35 static int flags_by_sb(int s_flags
)
38 if (s_flags
& MS_SYNCHRONOUS
)
39 flags
|= ST_SYNCHRONOUS
;
40 if (s_flags
& MS_MANDLOCK
)
42 if (s_flags
& MS_RDONLY
)
47 static int calculate_f_flags(struct vfsmount
*mnt
)
49 return ST_VALID
| flags_by_mnt(mnt
->mnt_flags
) |
50 flags_by_sb(mnt
->mnt_sb
->s_flags
);
53 static int statfs_by_dentry(struct dentry
*dentry
, struct kstatfs
*buf
)
57 if (!dentry
->d_sb
->s_op
->statfs
)
60 memset(buf
, 0, sizeof(*buf
));
61 retval
= security_sb_statfs(dentry
);
64 retval
= dentry
->d_sb
->s_op
->statfs(dentry
, buf
);
65 if (retval
== 0 && buf
->f_frsize
== 0)
66 buf
->f_frsize
= buf
->f_bsize
;
70 int vfs_statfs(const struct path
*path
, struct kstatfs
*buf
)
74 error
= statfs_by_dentry(path
->dentry
, buf
);
76 buf
->f_flags
= calculate_f_flags(path
->mnt
);
79 EXPORT_SYMBOL(vfs_statfs
);
81 int user_statfs(const char __user
*pathname
, struct kstatfs
*st
)
85 unsigned int lookup_flags
= LOOKUP_FOLLOW
|LOOKUP_AUTOMOUNT
;
87 error
= user_path_at(AT_FDCWD
, pathname
, lookup_flags
, &path
);
89 error
= vfs_statfs(&path
, st
);
91 if (retry_estale(error
, lookup_flags
)) {
92 lookup_flags
|= LOOKUP_REVAL
;
99 int fd_statfs(int fd
, struct kstatfs
*st
)
101 struct fd f
= fdget_raw(fd
);
104 error
= vfs_statfs(&f
.file
->f_path
, st
);
110 static int do_statfs_native(struct kstatfs
*st
, struct statfs __user
*p
)
114 if (sizeof(buf
) == sizeof(*st
))
115 memcpy(&buf
, st
, sizeof(*st
));
117 if (sizeof buf
.f_blocks
== 4) {
118 if ((st
->f_blocks
| st
->f_bfree
| st
->f_bavail
|
119 st
->f_bsize
| st
->f_frsize
) &
120 0xffffffff00000000ULL
)
123 * f_files and f_ffree may be -1; it's okay to stuff
126 if (st
->f_files
!= -1 &&
127 (st
->f_files
& 0xffffffff00000000ULL
))
129 if (st
->f_ffree
!= -1 &&
130 (st
->f_ffree
& 0xffffffff00000000ULL
))
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
));
147 if (copy_to_user(p
, &buf
, sizeof(buf
)))
152 static int do_statfs64(struct kstatfs
*st
, struct statfs64 __user
*p
)
155 if (sizeof(buf
) == sizeof(*st
))
156 memcpy(&buf
, st
, sizeof(*st
));
158 buf
.f_type
= st
->f_type
;
159 buf
.f_bsize
= st
->f_bsize
;
160 buf
.f_blocks
= st
->f_blocks
;
161 buf
.f_bfree
= st
->f_bfree
;
162 buf
.f_bavail
= st
->f_bavail
;
163 buf
.f_files
= st
->f_files
;
164 buf
.f_ffree
= st
->f_ffree
;
165 buf
.f_fsid
= st
->f_fsid
;
166 buf
.f_namelen
= st
->f_namelen
;
167 buf
.f_frsize
= st
->f_frsize
;
168 buf
.f_flags
= st
->f_flags
;
169 memset(buf
.f_spare
, 0, sizeof(buf
.f_spare
));
171 if (copy_to_user(p
, &buf
, sizeof(buf
)))
176 SYSCALL_DEFINE2(statfs
, const char __user
*, pathname
, struct statfs __user
*, buf
)
179 int error
= user_statfs(pathname
, &st
);
181 error
= do_statfs_native(&st
, buf
);
185 SYSCALL_DEFINE3(statfs64
, const char __user
*, pathname
, size_t, sz
, struct statfs64 __user
*, buf
)
189 if (sz
!= sizeof(*buf
))
191 error
= user_statfs(pathname
, &st
);
193 error
= do_statfs64(&st
, buf
);
197 SYSCALL_DEFINE2(fstatfs
, unsigned int, fd
, struct statfs __user
*, buf
)
200 int error
= fd_statfs(fd
, &st
);
202 error
= do_statfs_native(&st
, buf
);
206 SYSCALL_DEFINE3(fstatfs64
, unsigned int, fd
, size_t, sz
, struct statfs64 __user
*, buf
)
211 if (sz
!= sizeof(*buf
))
214 error
= fd_statfs(fd
, &st
);
216 error
= do_statfs64(&st
, buf
);
220 static int vfs_ustat(dev_t dev
, struct kstatfs
*sbuf
)
222 struct super_block
*s
= user_get_super(dev
);
227 err
= statfs_by_dentry(s
->s_root
, sbuf
);
232 SYSCALL_DEFINE2(ustat
, unsigned, dev
, struct ustat __user
*, ubuf
)
236 int err
= vfs_ustat(new_decode_dev(dev
), &sbuf
);
240 memset(&tmp
,0,sizeof(struct ustat
));
241 tmp
.f_tfree
= sbuf
.f_bfree
;
242 tmp
.f_tinode
= sbuf
.f_ffree
;
244 return copy_to_user(ubuf
, &tmp
, sizeof(struct ustat
)) ? -EFAULT
: 0;
248 static int put_compat_statfs(struct compat_statfs __user
*ubuf
, struct kstatfs
*kbuf
)
250 struct compat_statfs buf
;
251 if (sizeof ubuf
->f_blocks
== 4) {
252 if ((kbuf
->f_blocks
| kbuf
->f_bfree
| kbuf
->f_bavail
|
253 kbuf
->f_bsize
| kbuf
->f_frsize
) & 0xffffffff00000000ULL
)
255 /* f_files and f_ffree may be -1; it's okay
256 * to stuff that into 32 bits */
257 if (kbuf
->f_files
!= 0xffffffffffffffffULL
258 && (kbuf
->f_files
& 0xffffffff00000000ULL
))
260 if (kbuf
->f_ffree
!= 0xffffffffffffffffULL
261 && (kbuf
->f_ffree
& 0xffffffff00000000ULL
))
264 memset(&buf
, 0, sizeof(struct compat_statfs
));
265 buf
.f_type
= kbuf
->f_type
;
266 buf
.f_bsize
= kbuf
->f_bsize
;
267 buf
.f_blocks
= kbuf
->f_blocks
;
268 buf
.f_bfree
= kbuf
->f_bfree
;
269 buf
.f_bavail
= kbuf
->f_bavail
;
270 buf
.f_files
= kbuf
->f_files
;
271 buf
.f_ffree
= kbuf
->f_ffree
;
272 buf
.f_namelen
= kbuf
->f_namelen
;
273 buf
.f_fsid
.val
[0] = kbuf
->f_fsid
.val
[0];
274 buf
.f_fsid
.val
[1] = kbuf
->f_fsid
.val
[1];
275 buf
.f_frsize
= kbuf
->f_frsize
;
276 buf
.f_flags
= kbuf
->f_flags
;
277 if (copy_to_user(ubuf
, &buf
, sizeof(struct compat_statfs
)))
283 * The following statfs calls are copies of code from fs/statfs.c and
284 * should be checked against those from time to time
286 COMPAT_SYSCALL_DEFINE2(statfs
, const char __user
*, pathname
, struct compat_statfs __user
*, buf
)
289 int error
= user_statfs(pathname
, &tmp
);
291 error
= put_compat_statfs(buf
, &tmp
);
295 COMPAT_SYSCALL_DEFINE2(fstatfs
, unsigned int, fd
, struct compat_statfs __user
*, buf
)
298 int error
= fd_statfs(fd
, &tmp
);
300 error
= put_compat_statfs(buf
, &tmp
);
304 static int put_compat_statfs64(struct compat_statfs64 __user
*ubuf
, struct kstatfs
*kbuf
)
306 struct compat_statfs64 buf
;
307 if (sizeof(ubuf
->f_bsize
) == 4) {
308 if ((kbuf
->f_type
| kbuf
->f_bsize
| kbuf
->f_namelen
|
309 kbuf
->f_frsize
| kbuf
->f_flags
) & 0xffffffff00000000ULL
)
311 /* f_files and f_ffree may be -1; it's okay
312 * to stuff that into 32 bits */
313 if (kbuf
->f_files
!= 0xffffffffffffffffULL
314 && (kbuf
->f_files
& 0xffffffff00000000ULL
))
316 if (kbuf
->f_ffree
!= 0xffffffffffffffffULL
317 && (kbuf
->f_ffree
& 0xffffffff00000000ULL
))
320 memset(&buf
, 0, sizeof(struct compat_statfs64
));
321 buf
.f_type
= kbuf
->f_type
;
322 buf
.f_bsize
= kbuf
->f_bsize
;
323 buf
.f_blocks
= kbuf
->f_blocks
;
324 buf
.f_bfree
= kbuf
->f_bfree
;
325 buf
.f_bavail
= kbuf
->f_bavail
;
326 buf
.f_files
= kbuf
->f_files
;
327 buf
.f_ffree
= kbuf
->f_ffree
;
328 buf
.f_namelen
= kbuf
->f_namelen
;
329 buf
.f_fsid
.val
[0] = kbuf
->f_fsid
.val
[0];
330 buf
.f_fsid
.val
[1] = kbuf
->f_fsid
.val
[1];
331 buf
.f_frsize
= kbuf
->f_frsize
;
332 buf
.f_flags
= kbuf
->f_flags
;
333 if (copy_to_user(ubuf
, &buf
, sizeof(struct compat_statfs64
)))
338 COMPAT_SYSCALL_DEFINE3(statfs64
, const char __user
*, pathname
, compat_size_t
, sz
, struct compat_statfs64 __user
*, buf
)
343 if (sz
!= sizeof(*buf
))
346 error
= user_statfs(pathname
, &tmp
);
348 error
= put_compat_statfs64(buf
, &tmp
);
352 COMPAT_SYSCALL_DEFINE3(fstatfs64
, unsigned int, fd
, compat_size_t
, sz
, struct compat_statfs64 __user
*, buf
)
357 if (sz
!= sizeof(*buf
))
360 error
= fd_statfs(fd
, &tmp
);
362 error
= put_compat_statfs64(buf
, &tmp
);
367 * This is a copy of sys_ustat, just dealing with a structure layout.
368 * Given how simple this syscall is that apporach is more maintainable
369 * than the various conversion hacks.
371 COMPAT_SYSCALL_DEFINE2(ustat
, unsigned, dev
, struct compat_ustat __user
*, u
)
373 struct compat_ustat tmp
;
375 int err
= vfs_ustat(new_decode_dev(dev
), &sbuf
);
379 memset(&tmp
, 0, sizeof(struct compat_ustat
));
380 tmp
.f_tfree
= sbuf
.f_bfree
;
381 tmp
.f_tinode
= sbuf
.f_ffree
;
382 if (copy_to_user(u
, &tmp
, sizeof(struct compat_ustat
)))