summaryrefslogtreecommitdiff
path: root/tests/generic/587
blob: ebfeea1d17bbe17d92076af9386ea8327460bbcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#! /bin/bash
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (c) 2019, Oracle and/or its affiliates.  All Rights Reserved.
#
# FS QA Test No. 587
#
# Regression test to ensure that dquots are attached to the inode when we're
# performing unwritten extent conversion after a directio write and the extent
# mapping btree splits.  On an unpatched kernel, the quota accounting will be
# become incorrect.
#
# This test accompanies the commit 2815a16d7ff623 "xfs: attach dquots and
# reserve quota blocks during unwritten conversion".

. ./common/preamble
_begin_fstest auto quick rw prealloc quota

# Import common functions.
. ./common/filter
. ./common/quota

# real QA test starts here
_supported_fs generic
_require_user
_require_quota
_require_xfs_io_command "falloc"
_require_scratch
_require_odirect

cat > $tmp.awk << ENDL
{
if (\$1 == qa_user && \$2 != blocks)
	printf("%s: quota blocks %dKiB, expected %dKiB!\n", qa_user, \$2, blocks);
}
ENDL

# Make sure that the quota blocks accounting for qa_user on the scratch fs
# matches the stat blocks counter for the only file on the scratch fs that
# is owned by qa_user.
check_quota_accounting()
{
	# repquota rounds the raw numbers up to the nearest 1k when reporting
	# space usage.  xfs_io stat always reports space usage in 512b units,
	# so use an awk script to round this number up to the nearest 1k, just
	# like repquota does.
	$XFS_IO_PROG -c stat $testfile > $tmp.out
	cat $tmp.out >> $seqres.full
	local stat_blocks=$(grep 'stat.blocks' $tmp.out | \
		awk '{printf("%d\n", ($3 + 1) / 2);}')

	_report_quota_blocks $SCRATCH_MNT > $tmp.out
	cat $tmp.out >> $seqres.full
	$AWK_PROG -v qa_user=$qa_user -v blocks=$stat_blocks -f $tmp.awk $tmp.out
}

_scratch_mkfs > $seqres.full

# This test must have user quota enabled
_qmount_option usrquota
_qmount >> $seqres.full

testfile=$SCRATCH_MNT/test-$seq
touch $testfile
chown $qa_user $testfile

# Preallocate a file with just enough space that when we write every other
# block of the file, the extent mapping tree will expand to a two-block tree.
# Each tree block has a 56-byte header, and each mapping consumes 16 bytes.
meta_blksz=$(_get_block_size $SCRATCH_MNT)
file_blksz=$(_get_file_block_size $SCRATCH_MNT)

mappings_per_bmbt_block=$(( (meta_blksz - 56) / 16))
extents_needed=$((mappings_per_bmbt_block * 2))
sz=$((extents_needed * file_blksz))

$XFS_IO_PROG -f -c "falloc 0 $sz" $testfile >> $seqres.full
check_quota_accounting

# Cycle the mount to detach dquots and expand the bmbt to a single block.
_qmount >> $seqres.full
for ((i = 0; i < mappings_per_bmbt_block; i += 2)); do
	offset=$((i * file_blksz))
	$XFS_IO_PROG -d -c "pwrite $offset $file_blksz" $testfile >> $seqres.full
done
check_quota_accounting

# Cycle the mount to detach dquots and expand the bmbt to multiple blocks.
# A buggy kernel will forget to attach the dquots before the bmbt split and
# this will cause us to lose a block in the quota accounting.
_qmount >> $seqres.full
for ((i = mappings_per_bmbt_block; i < extents_needed; i += 2)); do
	offset=$((i * file_blksz))
	$XFS_IO_PROG -d -c "pwrite $offset $file_blksz" $testfile >> $seqres.full
done
check_quota_accounting

# Remove the test file, which (if the quota accounting is incorrect) will
# also trigger assertions when we try to free more blocks from the dquot than
# were accounted to the dquot.  Only do this if assertions aren't going to be
# fatal, since the check_quota_accounting above should be enough to fail the
# test when the kernel is buggy.
bug_on_assert="/sys/fs/xfs/debug/bug_on_assert"
if [ -f $bug_on_assert ] && grep -q "0" $bug_on_assert; then
	rm -f $testfile
fi

echo Silence is golden.
# success, all done
status=0
exit