Skip to content

Commit 25219db

Browse files
committed
xfs: fix fallocate functions when rtextsize is larger than 1
In commit fe341eb, I forgot that xfs_free_file_space isn't strictly a "remove mapped blocks" function. It is actually a function to zero file space by punching out the middle and writing zeroes to the unaligned ends of the specified range. Therefore, putting a rtextsize alignment check in that function is wrong because that breaks unaligned ZERO_RANGE on the realtime volume. Furthermore, xfs_file_fallocate already has alignment checks for the functions require the file range to be aligned to the size of a fundamental allocation unit (which is 1 FSB on the data volume and 1 rt extent on the realtime volume). Create a new helper to check fallocate arguments against the realtiem allocation unit size, fix the fallocate frontend to use it, fix free_file_space to delete the correct range, and remove a now redundant check from insert_file_space. NOTE: The realtime extent size is not required to be a power of two! Fixes: fe341eb ("xfs: ensure that fpunch, fcollapse, and finsert operations are aligned to rt extent size") Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com> Reviewed-by: Chandan Babu R <chandanrlinux@gmail.com>
1 parent 8946455 commit 25219db

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-18
lines changed

fs/xfs/xfs_bmap_util.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -947,11 +947,11 @@ xfs_free_file_space(
947947
endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
948948

949949
/* We can only free complete realtime extents. */
950-
if (XFS_IS_REALTIME_INODE(ip)) {
951-
xfs_extlen_t extsz = xfs_get_extsz_hint(ip);
952-
953-
if ((startoffset_fsb | endoffset_fsb) & (extsz - 1))
954-
return -EINVAL;
950+
if (XFS_IS_REALTIME_INODE(ip) && mp->m_sb.sb_rextsize > 1) {
951+
startoffset_fsb = roundup_64(startoffset_fsb,
952+
mp->m_sb.sb_rextsize);
953+
endoffset_fsb = rounddown_64(endoffset_fsb,
954+
mp->m_sb.sb_rextsize);
955955
}
956956

957957
/*
@@ -1147,14 +1147,6 @@ xfs_insert_file_space(
11471147

11481148
trace_xfs_insert_file_space(ip);
11491149

1150-
/* We can only insert complete realtime extents. */
1151-
if (XFS_IS_REALTIME_INODE(ip)) {
1152-
xfs_extlen_t extsz = xfs_get_extsz_hint(ip);
1153-
1154-
if ((stop_fsb | shift_fsb) & (extsz - 1))
1155-
return -EINVAL;
1156-
}
1157-
11581150
error = xfs_bmap_can_insert_extents(ip, stop_fsb, shift_fsb);
11591151
if (error)
11601152
return error;

fs/xfs/xfs_file.c

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,39 @@
3232

3333
static const struct vm_operations_struct xfs_file_vm_ops;
3434

35+
/*
36+
* Decide if the given file range is aligned to the size of the fundamental
37+
* allocation unit for the file.
38+
*/
39+
static bool
40+
xfs_is_falloc_aligned(
41+
struct xfs_inode *ip,
42+
loff_t pos,
43+
long long int len)
44+
{
45+
struct xfs_mount *mp = ip->i_mount;
46+
uint64_t mask;
47+
48+
if (XFS_IS_REALTIME_INODE(ip)) {
49+
if (!is_power_of_2(mp->m_sb.sb_rextsize)) {
50+
u64 rextbytes;
51+
u32 mod;
52+
53+
rextbytes = XFS_FSB_TO_B(mp, mp->m_sb.sb_rextsize);
54+
div_u64_rem(pos, rextbytes, &mod);
55+
if (mod)
56+
return false;
57+
div_u64_rem(len, rextbytes, &mod);
58+
return mod == 0;
59+
}
60+
mask = XFS_FSB_TO_B(mp, mp->m_sb.sb_rextsize) - 1;
61+
} else {
62+
mask = mp->m_sb.sb_blocksize - 1;
63+
}
64+
65+
return !((pos | len) & mask);
66+
}
67+
3568
int
3669
xfs_update_prealloc_flags(
3770
struct xfs_inode *ip,
@@ -850,9 +883,7 @@ xfs_file_fallocate(
850883
if (error)
851884
goto out_unlock;
852885
} else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
853-
unsigned int blksize_mask = i_blocksize(inode) - 1;
854-
855-
if (offset & blksize_mask || len & blksize_mask) {
886+
if (!xfs_is_falloc_aligned(ip, offset, len)) {
856887
error = -EINVAL;
857888
goto out_unlock;
858889
}
@@ -872,10 +903,9 @@ xfs_file_fallocate(
872903
if (error)
873904
goto out_unlock;
874905
} else if (mode & FALLOC_FL_INSERT_RANGE) {
875-
unsigned int blksize_mask = i_blocksize(inode) - 1;
876906
loff_t isize = i_size_read(inode);
877907

878-
if (offset & blksize_mask || len & blksize_mask) {
908+
if (!xfs_is_falloc_aligned(ip, offset, len)) {
879909
error = -EINVAL;
880910
goto out_unlock;
881911
}

fs/xfs/xfs_linux.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ static inline xfs_dev_t linux_to_xfs_dev_t(dev_t dev)
175175
#define xfs_sort(a,n,s,fn) sort(a,n,s,fn,NULL)
176176
#define xfs_stack_trace() dump_stack()
177177

178+
static inline uint64_t rounddown_64(uint64_t x, uint32_t y)
179+
{
180+
do_div(x, y);
181+
return x * y;
182+
}
183+
178184
static inline uint64_t roundup_64(uint64_t x, uint32_t y)
179185
{
180186
x += y - 1;

0 commit comments

Comments
 (0)