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	if tst_kvcmp -lt "3.15" -o -ge "4.7"; then
66		tst_resm TCONF "The device attribute max_comp_streams was"\
67		               "introduced in kernel 3.15 and deprecated in 4.7"
68		return
69	fi
70
71	tst_resm TINFO "set max_comp_streams to zram device(s)"
72
73	local i=0
74	for max_s in $zram_max_streams; do
75		local sys_path="/sys/block/zram${i}/max_comp_streams"
76		echo $max_s > $sys_path || \
77			tst_brkm TFAIL "failed to set '$max_s' to $sys_path"
78		local max_streams=$(cat $sys_path)
79
80		[ "$max_s" -ne "$max_streams" ] && \
81			tst_brkm TFAIL "can't set max_streams '$max_s', get $max_stream"
82
83		i=$(($i + 1))
84		tst_resm TINFO "$sys_path = '$max_streams' ($i/$dev_num)"
85	done
86
87	tst_resm TPASS "test succeeded"
88}
89
90zram_compress_alg()
91{
92	if tst_kvcmp -lt "3.15"; then
93		tst_resm TCONF "device attribute comp_algorithm is"\
94			"introduced since kernel v3.15, the running kernel"\
95			"does not support it"
96		return
97	fi
98
99	tst_resm TINFO "test that we can set compression algorithm"
100
101	local algs=$(cat /sys/block/zram0/comp_algorithm)
102	tst_resm TINFO "supported algs: $algs"
103	local i=0
104	for alg in $zram_algs; do
105		local sys_path="/sys/block/zram${i}/comp_algorithm"
106		echo "$alg" >  $sys_path || \
107			tst_brkm TFAIL "can't set '$alg' to $sys_path"
108		i=$(($i + 1))
109		tst_resm TINFO "$sys_path = '$alg' ($i/$dev_num)"
110	done
111
112	tst_resm TPASS "test succeeded"
113}
114
115zram_set_disksizes()
116{
117	tst_resm TINFO "set disk size to zram device(s)"
118	local i=0
119	for ds in $zram_sizes; do
120		local sys_path="/sys/block/zram${i}/disksize"
121		echo "$ds" >  $sys_path || \
122			tst_brkm TFAIL "can't set '$ds' to $sys_path"
123
124		i=$(($i + 1))
125		tst_resm TINFO "$sys_path = '$ds' ($i/$dev_num)"
126	done
127
128	tst_resm TPASS "test succeeded"
129}
130
131zram_set_memlimit()
132{
133	if tst_kvcmp -lt "3.18"; then
134		tst_resm TCONF "device attribute mem_limit is"\
135			"introduced since kernel v3.18, the running kernel"\
136			"does not support it"
137		return
138	fi
139
140	tst_resm TINFO "set memory limit to zram device(s)"
141
142	local i=0
143	for ds in $zram_mem_limits; do
144		local sys_path="/sys/block/zram${i}/mem_limit"
145		echo "$ds" >  $sys_path || \
146			tst_brkm TFAIL "can't set '$ds' to $sys_path"
147
148		i=$(($i + 1))
149		tst_resm TINFO "$sys_path = '$ds' ($i/$dev_num)"
150	done
151
152	tst_resm TPASS "test succeeded"
153}
154
155zram_makeswap()
156{
157	tst_resm TINFO "make swap with zram device(s)"
158	tst_check_cmds mkswap swapon swapoff
159	local i=0
160	for i in $(seq 0 $(($dev_num - 1))); do
161		mkswap /dev/zram$i > err.log 2>&1
162		if [ $? -ne 0 ]; then
163			cat err.log
164			tst_brkm TFAIL "mkswap /dev/zram$1 failed"
165		fi
166
167		swapon /dev/zram$i > err.log 2>&1
168		if [ $? -ne 0 ]; then
169			cat err.log
170			tst_brkm TFAIL "swapon /dev/zram$1 failed"
171		fi
172
173		tst_resm TINFO "done with /dev/zram$i"
174		dev_makeswap=$i
175	done
176
177	tst_resm TPASS "making zram swap succeeded"
178}
179
180zram_swapoff()
181{
182	tst_check_cmds swapoff
183	local i=
184	for i in $(seq 0 $dev_makeswap); do
185		swapoff /dev/zram$i > err.log 2>&1
186		if [ $? -ne 0 ]; then
187			cat err.log
188			tst_brkm TFAIL "swapoff /dev/zram$i failed"
189		fi
190	done
191	dev_makeswap=-1
192
193	tst_resm TPASS "swapoff completed"
194}
195
196zram_makefs()
197{
198	tst_check_cmds mkfs which
199	local i=0
200	for fs in $zram_filesystems; do
201		# if requested fs not supported default it to ext2
202		which mkfs.$fs > /dev/null 2>&1 || fs=ext2
203
204		tst_resm TINFO "make $fs filesystem on /dev/zram$i"
205		mkfs.$fs /dev/zram$i > err.log 2>&1
206		if [ $? -ne 0 ]; then
207			cat err.log
208			tst_brkm TFAIL "failed to make $fs on /dev/zram$i"
209		fi
210		i=$(($i + 1))
211	done
212
213	tst_resm TPASS "zram_makefs succeeded"
214}
215
216zram_mount()
217{
218	local i=0
219	for i in $(seq 0 $(($dev_num - 1))); do
220		tst_resm TINFO "mount /dev/zram$i"
221		mkdir zram$i
222		mount /dev/zram$i zram$i > /dev/null || \
223			tst_brkm TFAIL "mount /dev/zram$i failed"
224		dev_mounted=$i
225	done
226
227	tst_resm TPASS "mount of zram device(s) succeeded"
228}
229
230modinfo zram > /dev/null 2>&1 ||
231	tst_brkm TCONF "zram not configured in kernel"
232