2 # SPDX-License-Identifier: GPL-2.0-or-later
3 # Copyright (c) 2019, Oracle and/or its affiliates. All Rights Reserved.
7 # Regression test to ensure that dquots are attached to the inode when we're
8 # performing unwritten extent conversion after a directio write and the extent
9 # mapping btree splits. On an unpatched kernel, the quota accounting will be
12 # This test accompanies the commit 2815a16d7ff623 "xfs: attach dquots and
13 # reserve quota blocks during unwritten conversion".
16 seqres=$RESULT_DIR/$seq
17 echo "QA output created by $seq"
21 status=1 # failure is the default!
22 trap "_cleanup; exit \$status" 0 1 2 3 15
30 # get standard environment, filters and checks
35 # real QA test starts here
39 _require_xfs_io_command "falloc"
44 cat > $tmp.awk << ENDL
46 if (\$1 == qa_user && \$2 != blocks)
47 printf("%s: quota blocks %dKiB, expected %dKiB!\n", qa_user, \$2, blocks);
51 # Make sure that the quota blocks accounting for qa_user on the scratch fs
52 # matches the stat blocks counter for the only file on the scratch fs that
53 # is owned by qa_user.
54 check_quota_accounting()
56 # repquota rounds the raw numbers up to the nearest 1k when reporting
57 # space usage. xfs_io stat always reports space usage in 512b units,
58 # so use an awk script to round this number up to the nearest 1k, just
60 $XFS_IO_PROG -c stat $testfile > $tmp.out
61 cat $tmp.out >> $seqres.full
62 local stat_blocks=$(grep 'stat.blocks' $tmp.out | \
63 awk '{printf("%d\n", ($3 + 1) / 2);}')
65 _report_quota_blocks $SCRATCH_MNT > $tmp.out
66 cat $tmp.out >> $seqres.full
67 $AWK_PROG -v qa_user=$qa_user -v blocks=$stat_blocks -f $tmp.awk $tmp.out
70 _scratch_mkfs > $seqres.full
72 # This test must have user quota enabled
73 _qmount_option usrquota
74 _qmount >> $seqres.full
76 testfile=$SCRATCH_MNT/test-$seq
78 chown $qa_user $testfile
80 # Preallocate a file with just enough space that when we write every other
81 # block of the file, the extent mapping tree will expand to a two-block tree.
82 # Each tree block has a 56-byte header, and each mapping consumes 16 bytes.
83 meta_blksz=$(_get_block_size $SCRATCH_MNT)
84 file_blksz=$(_get_file_block_size $SCRATCH_MNT)
86 mappings_per_bmbt_block=$(( (meta_blksz - 56) / 16))
87 extents_needed=$((mappings_per_bmbt_block * 2))
88 sz=$((extents_needed * file_blksz))
90 $XFS_IO_PROG -f -c "falloc 0 $sz" $testfile >> $seqres.full
91 check_quota_accounting
93 # Cycle the mount to detach dquots and expand the bmbt to a single block.
94 _qmount >> $seqres.full
95 for ((i = 0; i < mappings_per_bmbt_block; i += 2)); do
96 offset=$((i * file_blksz))
97 $XFS_IO_PROG -d -c "pwrite $offset $file_blksz" $testfile >> $seqres.full
99 check_quota_accounting
101 # Cycle the mount to detach dquots and expand the bmbt to multiple blocks.
102 # A buggy kernel will forget to attach the dquots before the bmbt split and
103 # this will cause us to lose a block in the quota accounting.
104 _qmount >> $seqres.full
105 for ((i = mappings_per_bmbt_block; i < extents_needed; i += 2)); do
106 offset=$((i * file_blksz))
107 $XFS_IO_PROG -d -c "pwrite $offset $file_blksz" $testfile >> $seqres.full
109 check_quota_accounting
111 # Remove the test file, which (if the quota accounting is incorrect) will
112 # also trigger assertions when we try to free more blocks from the dquot than
113 # were accounted to the dquot. Only do this if assertions aren't going to be
114 # fatal, since the check_quota_accounting above should be enough to fail the
115 # test when the kernel is buggy.
116 bug_on_assert="/sys/fs/xfs/debug/bug_on_assert"
117 if [ -f $bug_on_assert ] && grep -q "0" $bug_on_assert; then
121 echo Silence is golden.