generic/611: Use _getfattr instead of GETFATTR_PROG
[xfstests-dev.git] / tests / generic / 587
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0-or-later
3 # Copyright (c) 2019, Oracle and/or its affiliates.  All Rights Reserved.
4 #
5 # FS QA Test No. 587
6 #
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
10 # become incorrect.
11 #
12 # This test accompanies the commit 2815a16d7ff623 "xfs: attach dquots and
13 # reserve quota blocks during unwritten conversion".
14
15 seq=`basename $0`
16 seqres=$RESULT_DIR/$seq
17 echo "QA output created by $seq"
18
19 here=`pwd`
20 tmp=/tmp/$$
21 status=1    # failure is the default!
22 trap "_cleanup; exit \$status" 0 1 2 3 15
23
24 _cleanup()
25 {
26         cd /
27         rm -f $tmp.*
28 }
29
30 # get standard environment, filters and checks
31 . ./common/rc
32 . ./common/filter
33 . ./common/quota
34
35 # real QA test starts here
36 _supported_fs generic
37 _require_user
38 _require_quota
39 _require_xfs_io_command "falloc"
40 _require_scratch
41
42 rm -f $seqres.full
43
44 cat > $tmp.awk << ENDL
45 {
46 if (\$1 == qa_user && \$2 != blocks)
47         printf("%s: quota blocks %dKiB, expected %dKiB!\n", qa_user, \$2, blocks);
48 }
49 ENDL
50
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()
55 {
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
59         # like repquota does.
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);}')
64
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
68 }
69
70 _scratch_mkfs > $seqres.full
71
72 # This test must have user quota enabled
73 _qmount_option usrquota
74 _qmount >> $seqres.full
75
76 testfile=$SCRATCH_MNT/test-$seq
77 touch $testfile
78 chown $qa_user $testfile
79
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)
85
86 mappings_per_bmbt_block=$(( (meta_blksz - 56) / 16))
87 extents_needed=$((mappings_per_bmbt_block * 2))
88 sz=$((extents_needed * file_blksz))
89
90 $XFS_IO_PROG -f -c "falloc 0 $sz" $testfile >> $seqres.full
91 check_quota_accounting
92
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
98 done
99 check_quota_accounting
100
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
108 done
109 check_quota_accounting
110
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
118         rm -f $testfile
119 fi
120
121 echo Silence is golden.
122 # success, all done
123 status=0
124 exit