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