1#!/bin/sh
2# Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved.
3#
4# This program is free software; you can redistribute it and/or
5# modify it under the terms of the GNU General Public License as
6# published by the Free Software Foundation; either version 2 of
7# the License, or (at your option) any later version.
8#
9# This program is distributed in the hope that it would be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write the Free Software Foundation,
16# Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17#
18# Author: Alexey Kodanev <alexey.kodanev@oracle.com>
19
20dev_makeswap=-1
21dev_mounted=-1
22
23trap tst_exit INT
24
25zram_cleanup()
26{
27	tst_resm TINFO "zram cleanup"
28	local i=
29	for i in $(seq 0 $dev_makeswap); do
30		swapoff /dev/zram$i
31	done
32
33	for i in $(seq 0 $dev_mounted); do
34		umount /dev/zram$i
35	done
36
37	for i in $(seq 0 $(($dev_num - 1))); do
38		echo 1 > /sys/block/zram${i}/reset
39	done
40
41	rmmod zram > /dev/null 2>&1
42
43	tst_rmdir
44}
45
46zram_load()
47{
48	tst_resm TINFO "create '$dev_num' zram device(s)"
49	modprobe zram num_devices=$dev_num || \
50		tst_brkm TBROK "failed to insert zram module"
51
52	dev_num_created=$(ls /dev/zram* | wc -w)
53
54	if [ "$dev_num_created" -ne "$dev_num" ]; then
55		tst_brkm TFAIL "unexpected num of devices: $dev_num_created"
56	else
57		tst_resm TPASS "test succeeded"
58	fi
59
60	tst_tmpdir
61}
62
63zram_max_streams()
64{
65	tst_kvercmp 3 15 0
66	if [ $? -eq 0 ]; then
67		tst_resm TCONF "device attribute max_comp_streams is"\
68			"introduced since kernel v3.15, the running kernel"\
69			"does not support it"
70		return
71	else
72		tst_resm TINFO "set max_comp_streams to zram device(s)"
73	fi
74
75	local i=0
76	for max_s in $zram_max_streams; do
77		local sys_path="/sys/block/zram${i}/max_comp_streams"
78		echo $max_s > $sys_path || \
79			tst_brkm TFAIL "failed to set '$max_s' to $sys_path"
80		local max_streams=$(cat $sys_path)
81
82		[ "$max_s" -ne "$max_streams" ] && \
83			tst_brkm TFAIL "can't set max_streams '$max_s', get $max_stream"
84
85		i=$(($i + 1))
86		tst_resm TINFO "$sys_path = '$max_streams' ($i/$dev_num)"
87	done
88
89	tst_resm TPASS "test succeeded"
90}
91
92zram_compress_alg()
93{
94	tst_kvercmp 3 15 0
95	if [ $? -eq 0 ]; then
96		tst_resm TCONF "device attribute comp_algorithm is"\
97			"introduced since kernel v3.15, the running kernel"\
98			"does not support it"
99		return
100	else
101		tst_resm TINFO "test that we can set compression algorithm"
102	fi
103
104	local algs=$(cat /sys/block/zram0/comp_algorithm)
105	tst_resm TINFO "supported algs: $algs"
106	local i=0
107	for alg in $zram_algs; do
108		local sys_path="/sys/block/zram${i}/comp_algorithm"
109		echo "$alg" >  $sys_path || \
110			tst_brkm TFAIL "can't set '$alg' to $sys_path"
111		i=$(($i + 1))
112		tst_resm TINFO "$sys_path = '$alg' ($i/$dev_num)"
113	done
114
115	tst_resm TPASS "test succeeded"
116}
117
118zram_set_disksizes()
119{
120	tst_resm TINFO "set disk size to zram device(s)"
121	local i=0
122	for ds in $zram_sizes; do
123		local sys_path="/sys/block/zram${i}/disksize"
124		echo "$ds" >  $sys_path || \
125			tst_brkm TFAIL "can't set '$ds' to $sys_path"
126
127		i=$(($i + 1))
128		tst_resm TINFO "$sys_path = '$ds' ($i/$dev_num)"
129	done
130
131	tst_resm TPASS "test succeeded"
132}
133
134zram_set_memlimit()
135{
136	tst_kvercmp 3 18 0
137	if [ $? -eq 0 ]; then
138		tst_resm TCONF "device attribute mem_limit is"\
139			"introduced since kernel v3.18, the running kernel"\
140			"does not support it"
141		return
142	else
143		tst_resm TINFO "set memory limit to zram device(s)"
144	fi
145
146	local i=0
147	for ds in $zram_mem_limits; do
148		local sys_path="/sys/block/zram${i}/mem_limit"
149		echo "$ds" >  $sys_path || \
150			tst_brkm TFAIL "can't set '$ds' to $sys_path"
151
152		i=$(($i + 1))
153		tst_resm TINFO "$sys_path = '$ds' ($i/$dev_num)"
154	done
155
156	tst_resm TPASS "test succeeded"
157}
158
159zram_makeswap()
160{
161	tst_resm TINFO "make swap with zram device(s)"
162	tst_check_cmds mkswap swapon swapoff
163	local i=0
164	for i in $(seq 0 $(($dev_num - 1))); do
165		mkswap /dev/zram$i > err.log 2>&1
166		if [ $? -ne 0 ]; then
167			cat err.log
168			tst_brkm TFAIL "mkswap /dev/zram$1 failed"
169		fi
170
171		swapon /dev/zram$i > err.log 2>&1
172		if [ $? -ne 0 ]; then
173			cat err.log
174			tst_brkm TFAIL "swapon /dev/zram$1 failed"
175		fi
176
177		tst_resm TINFO "done with /dev/zram$i"
178		dev_makeswap=$i
179	done
180
181	tst_resm TPASS "making zram swap succeeded"
182}
183
184zram_swapoff()
185{
186	tst_check_cmds swapoff
187	local i=
188	for i in $(seq 0 $dev_makeswap); do
189		swapoff /dev/zram$i > err.log 2>&1
190		if [ $? -ne 0 ]; then
191			cat err.log
192			tst_brkm TFAIL "swapoff /dev/zram$i failed"
193		fi
194	done
195	dev_makeswap=-1
196
197	tst_resm TPASS "swapoff completed"
198}
199
200zram_makefs()
201{
202	tst_check_cmds mkfs which
203	local i=0
204	for fs in $zram_filesystems; do
205		# if requested fs not supported default it to ext2
206		which mkfs.$fs > /dev/null 2>&1 || fs=ext2
207
208		tst_resm TINFO "make $fs filesystem on /dev/zram$i"
209		mkfs.$fs /dev/zram$i > err.log 2>&1
210		if [ $? -ne 0 ]; then
211			cat err.log
212			tst_brkm TFAIL "failed to make $fs on /dev/zram$i"
213		fi
214		i=$(($i + 1))
215	done
216
217	tst_resm TPASS "zram_makefs succeeded"
218}
219
220zram_mount()
221{
222	local i=0
223	for i in $(seq 0 $(($dev_num - 1))); do
224		tst_resm TINFO "mount /dev/zram$i"
225		mkdir zram$i
226		mount /dev/zram$i zram$i > /dev/null || \
227			tst_brkm TFAIL "mount /dev/zram$i failed"
228		dev_mounted=$i
229	done
230
231	tst_resm TPASS "mount of zram device(s) succeeded"
232}
233
234modinfo zram > /dev/null 2>&1 ||
235	tst_brkm TCONF "zram not configured in kernel"
236