1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Brian Behlendorf <behlendorf1@llnl.gov>
3 Date: Mon, 24 Sep 2018 17:11:25 -0700
4 Subject: [PATCH] Fix statfs(2) for 32-bit user space
6 When handling a 32-bit statfs() system call the returned fields,
7 although 64-bit in the kernel, must be limited to 32-bits or an
8 EOVERFLOW error will be returned.
10 This is less of an issue for block counts since the default
11 reported block size in 128KiB. But since it is possible to
12 set a smaller block size, these values will be scaled as
13 needed to fit in a 32-bit unsigned long.
15 Unlike most other filesystems the total possible file counts
16 are more likely to overflow because they are calculated based
17 on the available free space in the pool. In order to prevent
18 this the reported value must be capped at 2^32-1. This is
19 only for statfs(2) reporting, there are no changes to the
22 Reviewed-by: Andreas Dilger <andreas.dilger@whamcloud.com>
23 Reviewed-by: Richard Yao <ryao@gentoo.org>
24 Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
29 config/kernel-in-compat-syscall.m4 | 20 ++++++++++++++++++++
30 config/kernel.m4 | 1 +
31 include/linux/vfs_compat.h | 18 ++++++++++++++++++
32 module/zfs/zfs_vfsops.c | 8 +++-----
33 module/zfs/zpl_super.c | 22 ++++++++++++++++++++++
34 5 files changed, 64 insertions(+), 5 deletions(-)
35 create mode 100644 config/kernel-in-compat-syscall.m4
37 diff --git a/config/kernel-in-compat-syscall.m4 b/config/kernel-in-compat-syscall.m4
39 index 00000000..9fca9da2
41 +++ b/config/kernel-in-compat-syscall.m4
45 +dnl # Added in_compat_syscall() which can be overridden on a per-
46 +dnl # architecture basis. Prior to this is_compat_task() was the
47 +dnl # provided interface.
49 +AC_DEFUN([ZFS_AC_KERNEL_IN_COMPAT_SYSCALL], [
50 + AC_MSG_CHECKING([whether in_compat_syscall() is available])
51 + ZFS_LINUX_TRY_COMPILE([
52 + #include <linux/compat.h>
54 + in_compat_syscall();
57 + AC_DEFINE(HAVE_IN_COMPAT_SYSCALL, 1,
58 + [in_compat_syscall() is available])
63 diff --git a/config/kernel.m4 b/config/kernel.m4
64 index c7ca260c..3777f45c 100644
65 --- a/config/kernel.m4
66 +++ b/config/kernel.m4
67 @@ -129,6 +129,7 @@ AC_DEFUN([ZFS_AC_CONFIG_KERNEL], [
68 ZFS_AC_KERNEL_GLOBAL_PAGE_STATE
69 ZFS_AC_KERNEL_ACL_HAS_REFCOUNT
70 ZFS_AC_KERNEL_USERNS_CAPABILITIES
71 + ZFS_AC_KERNEL_IN_COMPAT_SYSCALL
73 AS_IF([test "$LINUX_OBJ" != "$LINUX"], [
74 KERNELMAKE_PARAMS="$KERNELMAKE_PARAMS O=$LINUX_OBJ"
75 diff --git a/include/linux/vfs_compat.h b/include/linux/vfs_compat.h
76 index c8203bd5..90b3cca7 100644
77 --- a/include/linux/vfs_compat.h
78 +++ b/include/linux/vfs_compat.h
80 #include <sys/taskq.h>
82 #include <linux/backing-dev.h>
83 +#include <linux/compat.h>
87 @@ -626,4 +627,21 @@ inode_set_iversion(struct inode *ip, u64 val)
92 + * Returns true when called in the context of a 32-bit system call.
95 +zpl_is_32bit_api(void)
98 +#ifdef HAVE_IN_COMPAT_SYSCALL
99 + return (in_compat_syscall());
101 + return (is_compat_task());
104 + return (BITS_PER_LONG == 32);
108 #endif /* _ZFS_VFS_H */
109 diff --git a/module/zfs/zfs_vfsops.c b/module/zfs/zfs_vfsops.c
110 index 76113393..bcdfa26b 100644
111 --- a/module/zfs/zfs_vfsops.c
112 +++ b/module/zfs/zfs_vfsops.c
113 @@ -1245,15 +1245,13 @@ zfs_statvfs(struct dentry *dentry, struct kstatfs *statp)
115 zfsvfs_t *zfsvfs = dentry->d_sb->s_fs_info;
116 uint64_t refdbytes, availbytes, usedobjs, availobjs;
122 dmu_objset_space(zfsvfs->z_os,
123 &refdbytes, &availbytes, &usedobjs, &availobjs);
125 - fsid = dmu_objset_fsid_guid(zfsvfs->z_os);
126 + uint64_t fsid = dmu_objset_fsid_guid(zfsvfs->z_os);
128 * The underlying storage pool actually uses multiple block
129 * size. Under Solaris frsize (fragment size) is reported as
130 @@ -1265,7 +1263,7 @@ zfs_statvfs(struct dentry *dentry, struct kstatfs *statp)
132 statp->f_frsize = zfsvfs->z_max_blksz;
133 statp->f_bsize = zfsvfs->z_max_blksz;
134 - bshift = fls(statp->f_bsize) - 1;
135 + uint32_t bshift = fls(statp->f_bsize) - 1;
138 * The following report "total" blocks of various kinds in
139 @@ -1282,7 +1280,7 @@ zfs_statvfs(struct dentry *dentry, struct kstatfs *statp)
140 * static metadata. ZFS doesn't preallocate files, so the best
141 * we can do is report the max that could possibly fit in f_files,
142 * and that minus the number actually used in f_ffree.
143 - * For f_ffree, report the smaller of the number of object available
144 + * For f_ffree, report the smaller of the number of objects available
145 * and the number of blocks (each object will take at least a block).
147 statp->f_ffree = MIN(availobjs, availbytes >> DNODE_SHIFT);
148 diff --git a/module/zfs/zpl_super.c b/module/zfs/zpl_super.c
149 index 5c426b0a..216c7940 100644
150 --- a/module/zfs/zpl_super.c
151 +++ b/module/zfs/zpl_super.c
152 @@ -181,6 +181,28 @@ zpl_statfs(struct dentry *dentry, struct kstatfs *statp)
153 spl_fstrans_unmark(cookie);
154 ASSERT3S(error, <=, 0);
157 + * If required by a 32-bit system call, dynamically scale the
158 + * block size up to 16MiB and decrease the block counts. This
159 + * allows for a maximum size of 64EiB to be reported. The file
160 + * counts must be artificially capped at 2^32-1.
162 + if (unlikely(zpl_is_32bit_api())) {
163 + while (statp->f_blocks > UINT32_MAX &&
164 + statp->f_bsize < SPA_MAXBLOCKSIZE) {
165 + statp->f_frsize <<= 1;
166 + statp->f_bsize <<= 1;
168 + statp->f_blocks >>= 1;
169 + statp->f_bfree >>= 1;
170 + statp->f_bavail >>= 1;
173 + uint64_t usedobjs = statp->f_files - statp->f_ffree;
174 + statp->f_ffree = MIN(statp->f_ffree, UINT32_MAX - usedobjs);
175 + statp->f_files = statp->f_ffree + usedobjs;