btrfs: test correct operation of free objectid related functionality
[xfstests-dev.git] / tests / btrfs / 228
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2020 SUSE Linux Products GmbH. All Rights Reserved.
4 #
5 # FS QA Test 228
6 #
7 # Test correct operation of free objectid related functionality
8 #
9 seq=`basename $0`
10 seqres=$RESULT_DIR/$seq
11 echo "QA output created by $seq"
12
13 here=`pwd`
14 tmp=/tmp/$$
15 status=1        # failure is the default!
16 trap "_cleanup; exit \$status" 0 1 2 3 15
17
18 _cleanup()
19 {
20         cd /
21         rm -f $tmp.*
22 }
23
24 # get standard environment, filters and checks
25 . ./common/rc
26 . ./common/filter
27
28 # remove previous $seqres.full before test
29 rm -f $seqres.full
30
31 # real QA test starts here
32
33 # Modify as appropriate.
34 _supported_fs btrfs
35 _require_scratch
36 _require_btrfs_command inspect-internal dump-tree
37
38 _scratch_mkfs > /dev/null
39 _scratch_mount
40
41 # create a new subvolume to validate its objectid is initialized accordingly,
42 # the expected value is 256 as this is the first free objectid in a new file
43 # system
44 $BTRFS_UTIL_PROG subvolume create $SCRATCH_MNT/newvol >> $seqres.full 2>&1 \
45         || _fail "couldn't create subvol"
46
47 $BTRFS_UTIL_PROG inspect-internal dump-tree -t1 $SCRATCH_DEV \
48         | grep -q "256 ROOT_ITEM"  ||   _fail "First subvol with id 256 doesn't exist"
49
50 # create new file in the new subvolume to validate its objectid is set as
51 # expected
52 touch $SCRATCH_MNT/newvol/file1 || _fail "Cannot create file in new subvol"
53
54 # ensure we have consistent view on-disk
55 sync
56
57 # get output related to the new root's dir entry
58 output_file=$tmp.output
59 $BTRFS_UTIL_PROG inspect-internal dump-tree -t5 $SCRATCH_DEV | grep -A2 "256 DIR_ITEM 1903355334" > $output_file
60
61 # get the objectid of the new root
62 new_root_id=$($AWK_PROG '/location key/{printf $3}' $output_file | tr -d  '(')
63 [ $new_root_id -eq 256 ] || _fail "New root id not equal to 256"
64
65 # the given root should always be item number 2, since it's the only item
66 item_seq=$($AWK_PROG '/item/ {printf $2}' $output_file)
67 [ $item_seq -eq 2 ] || _fail "New root not at item idx 2"
68
69 # now parse the structure of the new subvol's tree
70 $BTRFS_UTIL_PROG inspect-internal dump-tree -t256 $SCRATCH_DEV > $output_file
71
72 # this is the subvol's own ino
73 first_ino=$($AWK_PROG '/item 0/{printf $4}' $output_file | tr -d '(')
74 [ $first_ino -eq 256 ] || _fail "First ino objectid in subvol not 256"
75
76 # this is ino of first file in subvol
77 second_ino=$($AWK_PROG '/item 4/{printf $4}' $output_file | tr -d '(')
78 [ $second_ino -eq 257 ] || _fail "Second ino objectid in subvol not 257"
79
80 # success, all done
81 echo "Silence is golden"
82 status=0
83 exit