btrfs: convert tests to SPDX license tags
[xfstests-dev.git] / tests / btrfs / 031
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2014, Oracle and/or its affiliates.  All Rights Reserved.
4 #
5 # FS QA Test No. 031
6 #
7 # Testing cross-subvolume sparse copy on btrfs
8 #    - Create two subvolumes, mount one of them
9 #    - Create a file on each (sub/root)volume,
10 #      reflink them on the other volumes
11 #    - Change one original and two reflinked files
12 #    - Move reflinked files between subvolumes
13 #
14 seq=`basename $0`
15 seqres=$RESULT_DIR/$seq
16 echo "QA output created by $seq"
17
18 here=`pwd`
19 tmp=/tmp/$$
20 status=1    # failure is the default!
21 trap "_cleanup; exit \$status" 0 1 2 3 15
22
23 _cleanup()
24 {
25     cd /
26     rm -f $tmp.*
27 }
28
29 # get standard environment, filters and checks
30 . ./common/rc
31 . ./common/filter
32 . ./common/reflink
33
34 # real QA test starts here
35 _supported_fs btrfs
36 _supported_os Linux
37
38 _require_test
39 _require_scratch
40 _require_cp_reflink
41
42 _checksum_files()
43 {
44         for f in file1 file2 file3; do
45                 echo "$f:"
46                 for d in $testdir1 $cross_mount_test_dir $subvol2; do
47                         _md5_checksum $d/$f
48                 done
49         done
50 }
51
52 testdir1=$SCRATCH_MNT/test-$seq-1
53 testdir2=$SCRATCH_MNT/test-$seq-2
54 subvol1=$SCRATCH_MNT/subvol-$seq-1
55 subvol2=$SCRATCH_MNT/subvol-$seq-2
56 cross_mount_test_dir=$TEST_DIR/test-$seq
57
58 rm -f $seqres.full
59
60 _scratch_mkfs > /dev/null 2>&1
61 _scratch_mount
62
63 mkdir $testdir1
64 mkdir $testdir2
65 $BTRFS_UTIL_PROG subvolume create $subvol1 >> $seqres.full
66 $BTRFS_UTIL_PROG subvolume create $subvol2 >> $seqres.full
67 rm -rf $cross_mount_test_dir
68 mkdir $cross_mount_test_dir
69
70 _mount -t btrfs -o subvol=subvol-$seq-1 $SCRATCH_DEV $cross_mount_test_dir
71
72 echo "Create initial files"
73 # #testdir1/file1 is very small and will be inlined
74 $XFS_IO_PROG -f -c 'pwrite -S 0x61 0 10' $testdir1/file1 \
75     >> $seqres.full
76 $XFS_IO_PROG -f -c 'pwrite -S 0x62 0 13000' $cross_mount_test_dir/file2 \
77     >> $seqres.full
78 $XFS_IO_PROG -f -c 'pwrite -S 0x63 0 17000' $subvol2/file3 \
79     >> $seqres.full
80
81 echo "Create reflinks to the initial files on other subvolumes"
82 cp --reflink=always $testdir1/file1 $subvol1
83 cp --reflink=always $testdir1/file1 $subvol2
84 cp --reflink=always $subvol1/file2 $testdir1/
85 cp --reflink=always $subvol1/file2 $subvol2
86 cp --reflink=always $subvol2/file3 $testdir1/
87 cp --reflink=always $subvol2/file3 $subvol1
88
89 echo "Verify the reflinks"
90 _verify_reflink $cross_mount_test_dir/file2 $testdir1/file2
91 _verify_reflink $cross_mount_test_dir/file2 $subvol2/file2
92 _verify_reflink $subvol2/file3 $testdir1/file3
93 _verify_reflink $subvol2/file3 $cross_mount_test_dir/file3
94 echo "Verify the file contents:"
95 _checksum_files
96
97 echo -e "---\nOverwrite some files with new content"
98 $XFS_IO_PROG -c 'pwrite -S 0x64 0 20' $testdir1/file1 >> $seqres.full
99 $XFS_IO_PROG -c 'pwrite -S 0x66 0 21000' $subvol2/file2 >> $seqres.full
100 $XFS_IO_PROG -c 'pwrite -S 0x65 5000 5000' $cross_mount_test_dir/file3 \
101     >> $seqres.full
102
103 echo -n "Verify that non-overwritten reflinks "
104 echo "still have the same data blocks"
105 _verify_reflink $testdir1/file2 $cross_mount_test_dir/file2
106 _verify_reflink $testdir1/file3 $subvol2/file3
107 echo "Verify the file contents:"
108 _checksum_files
109
110 echo -e "---\nShuffle files between directories"
111 mv $testdir1/file* $testdir2
112 mv $cross_mount_test_dir/file* $testdir1/
113 mv $subvol2/file* $cross_mount_test_dir/
114 mv $testdir2/file* $subvol2/
115
116 # No _verify_reflink here as data is copied when moving files
117 # between subvols
118 echo "Verify the file contents:"
119 _checksum_files
120
121 $UMOUNT_PROG $cross_mount_test_dir
122
123 # success, all done
124 status=0
125 exit