summaryrefslogtreecommitdiff
path: root/tests/btrfs/005
blob: 8d66f0be640dc060a5540cc9ab292a0e42fd0781 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2012 Fujitsu Liu Bo.  All Rights Reserved.
#
# FS QA Test No. btrfs/005
#
# Btrfs Online defragmentation tests
#
. ./common/preamble
_begin_fstest auto defrag compress
cnt=119
filesize=48000

_create_file()
{
	if [ $1 -ne 2 ]; then
		tmpfile="$SCRATCH_MNT/tmp_file"
	else
		mkdir -p $SCRATCH_MNT/tmp_dir
		tmpfile="$SCRATCH_MNT/tmp_dir/tmp_file"
	fi
		
	for i in `seq $cnt -1 0`; do
		dd if=/dev/zero of=$tmpfile bs=4k count=1 \
		 conv=notrunc seek=$i oflag=sync &>/dev/null
	done
	# get md5sum
	md5sum $tmpfile > /tmp/checksum
}

_btrfs_online_defrag()
{
	str=""
	# start = -1 is invalid, should fail
	if [ "$2" = "2" ];then
		str="$str -s -1 -l $((filesize / 2)) "
	elif [ "$2" = "3" ];then
		str="$str -s $((filesize + 1)) -l $((filesize / 2)) "
	# len = -1 is invalid, should fail
	elif [ "$2" = "4" ];then
		str="$str -l -1 "
	elif [ "$2" = "5" ];then
		str="$str -l $((filesize + 1)) "
	elif [ "$2" = "6" ];then
		str="$str -l $((filesize / 2)) "
	fi

	if [ "$3" = "2" ];then
		str="$str -c "
	fi

	if [ "$str" != "" ]; then
		$BTRFS_UTIL_PROG filesystem defragment $str $SCRATCH_MNT/tmp_file >> $seqres.full 2>&1
	else
		if [ "$1" = "1" ];then
			$BTRFS_UTIL_PROG filesystem defragment $SCRATCH_MNT/tmp_file >> $seqres.full 2>&1
		elif [ "$1" = "2" ];then
			$BTRFS_UTIL_PROG filesystem defragment $SCRATCH_MNT/tmp_dir >> $seqres.full 2>&1
		elif [ "$1" = "3" ];then
			$BTRFS_UTIL_PROG filesystem defragment $SCRATCH_MNT >> $seqres.full 2>&1
		fi
	fi
	ret_val=$?
	_scratch_cycle_mount
	# Older defrag returned "20" for success
	# e9393c2 btrfs-progs: defrag return zero on success
	if [ $ret_val -ne 0 -a $ret_val -ne 20 ]; then
		echo "btrfs filesystem defragment failed!"
	fi
}

_checksum()
{
	md5sum -c /tmp/checksum > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "md5 checksum failed!"
	fi
}

_cleanup_defrag()
{
	_scratch_unmount > /dev/null 2>&1
}

_setup_defrag()
{
	_scratch_unmount > /dev/null 2>&1
	_scratch_mkfs > /dev/null 2>&1
	_scratch_mount
	_create_file $1
}

_rundefrag()
{
	_setup_defrag $1
	_btrfs_online_defrag $1 $2 $3
	_checksum
	_cleanup_defrag
	_check_scratch_fs
}

. ./common/filter
. ./common/defrag

_supported_fs btrfs
_require_scratch

_scratch_mkfs >/dev/null 2>&1
_scratch_mount
_require_defrag

echo "defrag object | defragment range | defragment compress"
echo "a single file | default | off"
_rundefrag 1 1 1

echo "a single file | default |  on"
_rundefrag 1 1 2

echo "a single file | start < 0 && 0 < len < file size | off (should fail)"
_rundefrag 1 2 1

echo "a single file | start > file size && 0 < len < file size | off"
_rundefrag 1 3 1

echo "a single file | start = 0 && len < 0 | off (should fail)"
_rundefrag 1 4 1

echo "a single file | start = 0 && len > file size | off"
_rundefrag 1 5 1

echo "a single file | start = 0 && 0 < len < file size | off"
_rundefrag 1 6 1

echo "a directory | default | off"
_rundefrag 2 1 1

echo "a filesystem | default | off"
_rundefrag 3 1 1

status=0
exit