1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved.
4#
5# Author: Alexey Kodanev <alexey.kodanev@oracle.com>
6# Modified: Naresh Kamboju <naresh.kamboju@linaro.org>
7
8MODULE=0
9dev_makeswap=-1
10dev_mounted=-1
11
12# Kselftest framework requirement - SKIP code is 4.
13ksft_skip=4
14
15trap INT
16
17check_prereqs()
18{
19	local msg="skip all tests:"
20	local uid=$(id -u)
21
22	if [ $uid -ne 0 ]; then
23		echo $msg must be run as root >&2
24		exit $ksft_skip
25	fi
26}
27
28zram_cleanup()
29{
30	echo "zram cleanup"
31	local i=
32	for i in $(seq 0 $dev_makeswap); do
33		swapoff /dev/zram$i
34	done
35
36	for i in $(seq 0 $dev_mounted); do
37		umount /dev/zram$i
38	done
39
40	for i in $(seq 0 $(($dev_num - 1))); do
41		echo 1 > /sys/block/zram${i}/reset
42		rm -rf zram$i
43	done
44
45}
46
47zram_unload()
48{
49	if [ $MODULE -ne 0 ] ; then
50		echo "zram rmmod zram"
51		rmmod zram > /dev/null 2>&1
52	fi
53}
54
55zram_load()
56{
57	# check zram module exists
58	MODULE_PATH=/lib/modules/`uname -r`/kernel/drivers/block/zram/zram.ko
59	if [ -f $MODULE_PATH ]; then
60		MODULE=1
61		echo "create '$dev_num' zram device(s)"
62		modprobe zram num_devices=$dev_num
63		if [ $? -ne 0 ]; then
64			echo "failed to insert zram module"
65			exit 1
66		fi
67
68		dev_num_created=$(ls /dev/zram* | wc -w)
69
70		if [ "$dev_num_created" -ne "$dev_num" ]; then
71			echo "unexpected num of devices: $dev_num_created"
72			ERR_CODE=-1
73		else
74			echo "zram load module successful"
75		fi
76	elif [ -b /dev/zram0 ]; then
77		echo "/dev/zram0 device file found: OK"
78	else
79		echo "ERROR: No zram.ko module or no /dev/zram0 device found"
80		echo "$TCID : CONFIG_ZRAM is not set"
81		exit 1
82	fi
83}
84
85zram_max_streams()
86{
87	echo "set max_comp_streams to zram device(s)"
88
89	local i=0
90	for max_s in $zram_max_streams; do
91		local sys_path="/sys/block/zram${i}/max_comp_streams"
92		echo $max_s > $sys_path || \
93			echo "FAIL failed to set '$max_s' to $sys_path"
94		sleep 1
95		local max_streams=$(cat $sys_path)
96
97		[ "$max_s" -ne "$max_streams" ] && \
98			echo "FAIL can't set max_streams '$max_s', get $max_stream"
99
100		i=$(($i + 1))
101		echo "$sys_path = '$max_streams' ($i/$dev_num)"
102	done
103
104	echo "zram max streams: OK"
105}
106
107zram_compress_alg()
108{
109	echo "test that we can set compression algorithm"
110
111	local algs=$(cat /sys/block/zram0/comp_algorithm)
112	echo "supported algs: $algs"
113	local i=0
114	for alg in $zram_algs; do
115		local sys_path="/sys/block/zram${i}/comp_algorithm"
116		echo "$alg" >	$sys_path || \
117			echo "FAIL can't set '$alg' to $sys_path"
118		i=$(($i + 1))
119		echo "$sys_path = '$alg' ($i/$dev_num)"
120	done
121
122	echo "zram set compression algorithm: OK"
123}
124
125zram_set_disksizes()
126{
127	echo "set disk size to zram device(s)"
128	local i=0
129	for ds in $zram_sizes; do
130		local sys_path="/sys/block/zram${i}/disksize"
131		echo "$ds" >	$sys_path || \
132			echo "FAIL can't set '$ds' to $sys_path"
133
134		i=$(($i + 1))
135		echo "$sys_path = '$ds' ($i/$dev_num)"
136	done
137
138	echo "zram set disksizes: OK"
139}
140
141zram_set_memlimit()
142{
143	echo "set memory limit to zram device(s)"
144
145	local i=0
146	for ds in $zram_mem_limits; do
147		local sys_path="/sys/block/zram${i}/mem_limit"
148		echo "$ds" >	$sys_path || \
149			echo "FAIL can't set '$ds' to $sys_path"
150
151		i=$(($i + 1))
152		echo "$sys_path = '$ds' ($i/$dev_num)"
153	done
154
155	echo "zram set memory limit: OK"
156}
157
158zram_makeswap()
159{
160	echo "make swap with zram device(s)"
161	local i=0
162	for i in $(seq 0 $(($dev_num - 1))); do
163		mkswap /dev/zram$i > err.log 2>&1
164		if [ $? -ne 0 ]; then
165			cat err.log
166			echo "FAIL mkswap /dev/zram$1 failed"
167		fi
168
169		swapon /dev/zram$i > err.log 2>&1
170		if [ $? -ne 0 ]; then
171			cat err.log
172			echo "FAIL swapon /dev/zram$1 failed"
173		fi
174
175		echo "done with /dev/zram$i"
176		dev_makeswap=$i
177	done
178
179	echo "zram making zram mkswap and swapon: OK"
180}
181
182zram_swapoff()
183{
184	local i=
185	for i in $(seq 0 $dev_makeswap); do
186		swapoff /dev/zram$i > err.log 2>&1
187		if [ $? -ne 0 ]; then
188			cat err.log
189			echo "FAIL swapoff /dev/zram$i failed"
190		fi
191	done
192	dev_makeswap=-1
193
194	echo "zram swapoff: OK"
195}
196
197zram_makefs()
198{
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		echo "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			echo "FAIL failed to make $fs on /dev/zram$i"
209		fi
210		i=$(($i + 1))
211		echo "zram mkfs.$fs: OK"
212	done
213}
214
215zram_mount()
216{
217	local i=0
218	for i in $(seq 0 $(($dev_num - 1))); do
219		echo "mount /dev/zram$i"
220		mkdir zram$i
221		mount /dev/zram$i zram$i > /dev/null || \
222			echo "FAIL mount /dev/zram$i failed"
223		dev_mounted=$i
224	done
225
226	echo "zram mount of zram device(s): OK"
227}
228