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
40 _require_xfs_io_command "falloc"
45 cat > $tmp.awk << ENDL
47 if (\$1 == qa_user && \$2 != blocks)
48 printf("%s: quota blocks %dKiB, expected %dKiB!\n", qa_user, \$2, blocks);
52 # Make sure that the quota blocks accounting for qa_user on the scratch fs
53 # matches the stat blocks counter for the only file on the scratch fs that
54 # is owned by qa_user.
55 check_quota_accounting()
57 # repquota rounds the raw numbers up to the nearest 1k when reporting
58 # space usage. xfs_io stat always reports space usage in 512b units,
59 # so use an awk script to round this number up to the nearest 1k, just
61 $XFS_IO_PROG -c stat $testfile > $tmp.out
62 cat $tmp.out >> $seqres.full
63 local stat_blocks=$(grep 'stat.blocks' $tmp.out | \
64 awk '{printf("%d\n", ($3 + 1) / 2);}')
66 _report_quota_blocks $SCRATCH_MNT > $tmp.out
67 cat $tmp.out >> $seqres.full
68 $AWK_PROG -v qa_user=$qa_user -v blocks=$stat_blocks -f $tmp.awk $tmp.out
71 _scratch_mkfs > $seqres.full
73 # This test must have user quota enabled
74 _qmount_option usrquota
75 _qmount >> $seqres.full
77 testfile=$SCRATCH_MNT/test-$seq
79 chown $qa_user $testfile
81 # Preallocate a file with just enough space that when we write every other
82 # block of the file, the extent mapping tree will expand to a two-block tree.
83 # Each tree block has a 56-byte header, and each mapping consumes 16 bytes.
84 meta_blksz=$(_get_block_size $SCRATCH_MNT)
85 file_blksz=$(_get_file_block_size $SCRATCH_MNT)
87 mappings_per_bmbt_block=$(( (meta_blksz - 56) / 16))
88 extents_needed=$((mappings_per_bmbt_block * 2))
89 sz=$((extents_needed * file_blksz))
91 $XFS_IO_PROG -f -c "falloc 0 $sz" $testfile >> $seqres.full
92 check_quota_accounting
94 # Cycle the mount to detach dquots and expand the bmbt to a single block.
95 _qmount >> $seqres.full
96 for ((i = 0; i < mappings_per_bmbt_block; i += 2)); do
97 offset=$((i * file_blksz))
98 $XFS_IO_PROG -d -c "pwrite $offset $file_blksz" $testfile >> $seqres.full
100 check_quota_accounting
102 # Cycle the mount to detach dquots and expand the bmbt to multiple blocks.
103 # A buggy kernel will forget to attach the dquots before the bmbt split and
104 # this will cause us to lose a block in the quota accounting.
105 _qmount >> $seqres.full
106 for ((i = mappings_per_bmbt_block; i < extents_needed; i += 2)); do
107 offset=$((i * file_blksz))
108 $XFS_IO_PROG -d -c "pwrite $offset $file_blksz" $testfile >> $seqres.full
110 check_quota_accounting
112 # Remove the test file, which (if the quota accounting is incorrect) will
113 # also trigger assertions when we try to free more blocks from the dquot than
114 # were accounted to the dquot. Only do this if assertions aren't going to be
115 # fatal, since the check_quota_accounting above should be enough to fail the
116 # test when the kernel is buggy.
117 bug_on_assert="/sys/fs/xfs/debug/bug_on_assert"
118 if [ -f $bug_on_assert ] && grep -q "0" $bug_on_assert; then
122 echo Silence is golden.