1#!/bin/sh
2# Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved.
3# Copyright (c) 2019 Petr Vorel <pvorel@suse.cz>
4# Author: Alexey Kodanev <alexey.kodanev@oracle.com>
5
6dev_makeswap=-1
7dev_mounted=-1
8
9TST_NEEDS_TMPDIR=1
10TST_SETUP="zram_load"
11TST_CLEANUP="zram_cleanup"
12. tst_test.sh
13
14zram_cleanup()
15{
16	local i
17
18	for i in $(seq 0 $dev_makeswap); do
19		swapoff /dev/zram$i
20	done
21
22	for i in $(seq 0 $dev_mounted); do
23		umount /dev/zram$i
24	done
25
26	for i in $(seq 0 $(($dev_num - 1))); do
27		echo 1 > /sys/block/zram${i}/reset
28	done
29
30	rmmod zram > /dev/null 2>&1
31}
32
33zram_load()
34{
35	tst_res TINFO "create '$dev_num' zram device(s)"
36	modprobe zram num_devices=$dev_num || \
37		tst_brk TBROK "failed to insert zram module"
38
39	dev_num_created=$(ls /dev/zram* | wc -w)
40
41	if [ "$dev_num_created" -ne "$dev_num" ]; then
42		tst_brk TFAIL "unexpected num of devices: $dev_num_created"
43	else
44		tst_res TPASS "test succeeded"
45	fi
46}
47
48zram_max_streams()
49{
50	if tst_kvcmp -lt "3.15" -o -ge "4.7"; then
51		tst_res TCONF "The device attribute max_comp_streams was"\
52		               "introduced in kernel 3.15 and deprecated in 4.7"
53		return
54	fi
55
56	tst_res TINFO "set max_comp_streams to zram device(s)"
57
58	local i=0
59
60	for max_s in $zram_max_streams; do
61		local sys_path="/sys/block/zram${i}/max_comp_streams"
62		echo $max_s > $sys_path || \
63			tst_brk TFAIL "failed to set '$max_s' to $sys_path"
64		local max_streams=$(cat $sys_path)
65
66		[ "$max_s" -ne "$max_streams" ] && \
67			tst_brk TFAIL "can't set max_streams '$max_s', get $max_stream"
68
69		i=$(($i + 1))
70		tst_res TINFO "$sys_path = '$max_streams' ($i/$dev_num)"
71	done
72
73	tst_res TPASS "test succeeded"
74}
75
76zram_compress_alg()
77{
78	if tst_kvcmp -lt "3.15"; then
79		tst_res TCONF "device attribute comp_algorithm is"\
80			"introduced since kernel v3.15, the running kernel"\
81			"does not support it"
82		return
83	fi
84
85	local i=0
86
87	tst_res TINFO "test that we can set compression algorithm"
88	local algs="$(cat /sys/block/zram0/comp_algorithm)"
89	tst_res TINFO "supported algs: $algs"
90
91	for alg in $zram_algs; do
92		local sys_path="/sys/block/zram${i}/comp_algorithm"
93		echo "$alg" >  $sys_path || \
94			tst_brk TFAIL "can't set '$alg' to $sys_path"
95		i=$(($i + 1))
96		tst_res TINFO "$sys_path = '$alg' ($i/$dev_num)"
97	done
98
99	tst_res TPASS "test succeeded"
100}
101
102zram_set_disksizes()
103{
104	local i=0
105	local ds
106
107	tst_res TINFO "set disk size to zram device(s)"
108	for ds in $zram_sizes; do
109		local sys_path="/sys/block/zram${i}/disksize"
110		echo "$ds" >  $sys_path || \
111			tst_brk TFAIL "can't set '$ds' to $sys_path"
112
113		i=$(($i + 1))
114		tst_res TINFO "$sys_path = '$ds' ($i/$dev_num)"
115	done
116
117	tst_res TPASS "test succeeded"
118}
119
120zram_set_memlimit()
121{
122	if tst_kvcmp -lt "3.18"; then
123		tst_res TCONF "device attribute mem_limit is"\
124			"introduced since kernel v3.18, the running kernel"\
125			"does not support it"
126		return
127	fi
128
129	local i=0
130	local ds
131
132	tst_res TINFO "set memory limit to zram device(s)"
133
134	for ds in $zram_mem_limits; do
135		local sys_path="/sys/block/zram${i}/mem_limit"
136		echo "$ds" >  $sys_path || \
137			tst_brk TFAIL "can't set '$ds' to $sys_path"
138
139		i=$(($i + 1))
140		tst_res TINFO "$sys_path = '$ds' ($i/$dev_num)"
141	done
142
143	tst_res TPASS "test succeeded"
144}
145
146zram_makeswap()
147{
148	tst_res TINFO "make swap with zram device(s)"
149	tst_require_cmds mkswap swapon swapoff
150	local i=0
151
152	for i in $(seq 0 $(($dev_num - 1))); do
153		ROD mkswap /dev/zram$i
154		ROD swapon /dev/zram$i
155		tst_res TINFO "done with /dev/zram$i"
156		dev_makeswap=$i
157	done
158
159	tst_res TPASS "making zram swap succeeded"
160}
161
162zram_swapoff()
163{
164	tst_require_cmds swapoff
165	local i
166
167	for i in $(seq 0 $dev_makeswap); do
168		ROD swapoff /dev/zram$i
169	done
170	dev_makeswap=-1
171
172	tst_res TPASS "swapoff completed"
173}
174
175zram_makefs()
176{
177	tst_require_cmds mkfs
178	local i=0
179
180	for fs in $zram_filesystems; do
181		# if requested fs not supported default it to ext2
182		tst_supported_fs $fs 2> /dev/null || fs=ext2
183
184		tst_res TINFO "make $fs filesystem on /dev/zram$i"
185		mkfs.$fs /dev/zram$i > err.log 2>&1
186		if [ $? -ne 0 ]; then
187			cat err.log
188			tst_brk TFAIL "failed to make $fs on /dev/zram$i"
189		fi
190
191		i=$(($i + 1))
192	done
193
194	tst_res TPASS "zram_makefs succeeded"
195}
196
197zram_mount()
198{
199	local i=0
200
201	for i in $(seq 0 $(($dev_num - 1))); do
202		tst_res TINFO "mount /dev/zram$i"
203		mkdir zram$i
204		ROD mount /dev/zram$i zram$i
205		dev_mounted=$i
206	done
207
208	tst_res TPASS "mount of zram device(s) succeeded"
209}
210
211modinfo zram > /dev/null 2>&1 ||
212	tst_brk TCONF "zram not configured in kernel"
213