175317a8a5f7e4f697b36323f56a0b8279543a36
[xfstests-dev.git] / tests / btrfs / 029
1 #! /bin/bash
2 # FS QA Test No. 029
3 #
4 # Check if creating a sparse copy ("reflink") of a file on btrfs
5 # expectedly fails when it's done between different filesystems or
6 # different mount points of the same filesystem.
7 #
8 # For both situations, these actions are executed:
9 #    - Copy a file with the reflink=auto option.
10 #      A normal copy should be created.
11 #    - Copy a file with the reflink=always option. Should result in
12 #      error.
13 #
14 #-----------------------------------------------------------------------
15 # Copyright (c) 2014, Oracle and/or its affiliates.  All Rights Reserved.
16 #
17 # This program is free software; you can redistribute it and/or
18 # modify it under the terms of the GNU General Public License as
19 # published by the Free Software Foundation.
20 #
21 # This program is distributed in the hope that it would be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write the Free Software Foundation,
28 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
29 #-----------------------------------------------------------------------
30
31 seq=`basename $0`
32 seqres=$RESULT_DIR/$seq
33 echo "QA output created by $seq"
34
35 here=`pwd`
36 tmp=/tmp/$$
37 status=1    # failure is the default!
38 trap "_cleanup; exit \$status" 0 1 2 3 15
39
40 _cleanup()
41 {
42     umount $SCRATCH_MNT &>/dev/null
43     cd /
44     rm -f $tmp.*
45 }
46
47 # get standard environment, filters and checks
48 . ./common/rc
49 . ./common/filter
50 . ./common/reflink
51
52 # real QA test starts here
53 _supported_fs btrfs
54 _supported_os Linux
55
56 _require_test
57 _require_scratch
58 _require_cp_reflink
59
60 SOURCE_DIR=$TEST_DIR/test-$seq
61 CROSS_DEV_DIR=$SCRATCH_MNT/test-$seq
62 # mount point & target for twice-mounted device
63 TEST_DIR2=$TEST_DIR/mount2
64 DUAL_MOUNT_DIR=$SCRATCH_MNT/test-bis-$seq
65
66 rm -rf $SOURCE_DIR
67 mkdir $SOURCE_DIR
68
69 rm -f $seqres.full
70
71 _scratch_mkfs > /dev/null 2>&1
72 $XFS_IO_PROG -f -c 'pwrite -S 0x61 0 9000' $SOURCE_DIR/original \
73  >> $seqres.full
74
75 _filter_testdirs()
76 {
77     _filter_test_dir | _filter_scratch
78 }
79
80 _create_reflinks_to()
81 {
82     # auto reflink, should fall back to non-reflink
83     rm -rf $1; mkdir $1
84     echo "reflink=auto:"
85     cp --reflink=auto $SOURCE_DIR/original $1/copy
86     md5sum $SOURCE_DIR/original | _filter_testdirs
87     md5sum $1/copy | _filter_testdirs
88
89     # always reflink, should fail outright
90     rm -rf $1; mkdir $1
91     echo "reflink=always:"
92     cp --reflink=always $SOURCE_DIR/original $1/copyfail > $seqres.full 2>&1 \
93         || echo "cp reflink failed"
94
95     # The failed target actually gets created by cp:
96     ls $1/copyfail | _filter_testdirs
97 }
98
99 echo "test reflinks across different devices"
100 _scratch_mount
101 _create_reflinks_to $CROSS_DEV_DIR
102 _scratch_unmount
103
104 echo "test reflinks across different mountpoints of same device"
105 mount $TEST_DEV $SCRATCH_MNT || _fail "Couldn't double-mount $TEST_DEV"
106 _create_reflinks_to $DUAL_MOUNT_DIR
107 umount $SCRATCH_MNT
108
109 # success, all done
110 status=0
111 exit