blob: 34d88589f0e48b0eed32d5f8a0db53a3d5602dd3 [file] [log] [blame]
Garrett Cooperecd667e2011-01-19 01:06:18 -08001/*
2 * zram: generic RAM based compressed R/W block devices
3 * http://lkml.org/lkml/2010/8/9/227
4 *
5 * Copyright (C) 2010 Red Hat, Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 *
14 * Further, this software is distributed without any warranty that it
15 * is free of the rightful claim of any third person regarding
16 * infringement or the like. Any license provided herein, whether
17 * implied or otherwise, applies only to this software file. Patent
18 * licenses, if any, provided herein do not apply to combinations of
19 * this program with other software, or any other product whatsoever.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * 02110-1301, USA.
25 */
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <sys/mman.h>
Caspar Zhang79667fa2012-03-12 14:41:46 +080029#include <errno.h>
Garrett Cooperecd667e2011-01-19 01:06:18 -080030#include <fcntl.h>
31#include <stdio.h>
Garrett Cooperecd667e2011-01-19 01:06:18 -080032#include <string.h>
Caspar Zhang79667fa2012-03-12 14:41:46 +080033#include <unistd.h>
Garrett Cooperecd667e2011-01-19 01:06:18 -080034#include "test.h"
Garrett Cooperecd667e2011-01-19 01:06:18 -080035
36char *TCID = "zram01";
37int TST_TOTAL = 1;
Garrett Cooperecd667e2011-01-19 01:06:18 -080038
39#define PATH_ZRAM "/sys/block/zram0"
Caspar Zhanga4ef1332011-12-23 01:45:32 +080040#define SIZE (512 * 1024 * 1024L)
Garrett Cooperecd667e2011-01-19 01:06:18 -080041#define DEVICE "/dev/zram0"
42
Caspar Zhang79667fa2012-03-12 14:41:46 +080043static int modprobe;
44
Caspar Zhang37b58132011-11-17 12:13:49 +080045static void set_disksize(void);
Caspar Zhang039c3712011-11-17 12:25:22 +080046static void write_device(void);
Caspar Zhang10425462011-12-23 01:34:16 +080047static void verify_device(void);
Caspar Zhang37b58132011-11-17 12:13:49 +080048static void reset(void);
Garrett Cooperecd667e2011-01-19 01:06:18 -080049static void setup(void);
50static void cleanup(void);
51static void print(char *string);
52static void dump_info(void);
53
54int main(int argc, char *argv[])
55{
Caspar Zhang37b58132011-11-17 12:13:49 +080056 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020057 const char *msg;
Garrett Cooperecd667e2011-01-19 01:06:18 -080058
59 msg = parse_opts(argc, argv, NULL, NULL);
60 if (msg != NULL)
61 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
62
63 setup();
64
65 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080066 tst_count = 0;
Garrett Cooperecd667e2011-01-19 01:06:18 -080067
Caspar Zhang37b58132011-11-17 12:13:49 +080068 set_disksize();
Garrett Cooperecd667e2011-01-19 01:06:18 -080069
Caspar Zhang039c3712011-11-17 12:25:22 +080070 write_device();
Garrett Cooperecd667e2011-01-19 01:06:18 -080071 dump_info();
Caspar Zhang10425462011-12-23 01:34:16 +080072 verify_device();
Garrett Cooperecd667e2011-01-19 01:06:18 -080073
Caspar Zhang37b58132011-11-17 12:13:49 +080074 reset();
Garrett Cooperecd667e2011-01-19 01:06:18 -080075 dump_info();
76 }
77 cleanup();
78 tst_exit();
79}
80
Caspar Zhang37b58132011-11-17 12:13:49 +080081static void set_disksize(void)
82{
83 int fd;
84 char size[BUFSIZ];
85
Caspar Zhanga4ef1332011-12-23 01:45:32 +080086 tst_resm(TINFO, "create a zram device with %ld bytes in size.", SIZE);
Caspar Zhang37b58132011-11-17 12:13:49 +080087 fd = open(PATH_ZRAM "/disksize", O_WRONLY);
88 if (fd == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +080089 tst_brkm(TBROK | TERRNO, cleanup, "open %s",
90 PATH_ZRAM "/disksize");
Caspar Zhanga4ef1332011-12-23 01:45:32 +080091 sprintf(size, "%ld", SIZE);
Caspar Zhang37b58132011-11-17 12:13:49 +080092 if (write(fd, size, strlen(size)) != strlen(size))
Wanlong Gao354ebb42012-12-07 10:10:04 +080093 tst_brkm(TBROK | TERRNO, cleanup, "write %s to %s", size,
94 PATH_ZRAM "/disksize");
Caspar Zhang37b58132011-11-17 12:13:49 +080095 close(fd);
96}
97
Caspar Zhang039c3712011-11-17 12:25:22 +080098static void write_device(void)
Caspar Zhang37b58132011-11-17 12:13:49 +080099{
100 int fd;
Caspar Zhang10425462011-12-23 01:34:16 +0800101 char *s;
Caspar Zhang37b58132011-11-17 12:13:49 +0800102
103 tst_resm(TINFO, "map it into memory.");
104 fd = open(DEVICE, O_RDWR);
105 if (fd == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800106 tst_brkm(TBROK | TERRNO, cleanup, "open %s", DEVICE);
107 s = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Caspar Zhang37b58132011-11-17 12:13:49 +0800108 if (s == MAP_FAILED)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800109 tst_brkm(TBROK | TERRNO, cleanup, "mmap");
Caspar Zhang37b58132011-11-17 12:13:49 +0800110
111 tst_resm(TINFO, "write all the memory.");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800112 memset(s, 'a', SIZE - 1);
113 s[SIZE - 1] = '\0';
Caspar Zhang96cdfb72011-11-17 12:27:01 +0800114
115 if (munmap(s, SIZE) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800116 tst_brkm(TBROK | TERRNO, cleanup, "munmap");
Caspar Zhang96cdfb72011-11-17 12:27:01 +0800117
Caspar Zhang37b58132011-11-17 12:13:49 +0800118 close(fd);
119}
120
Caspar Zhang10425462011-12-23 01:34:16 +0800121static void verify_device(void)
122{
Caspar Zhanga4ef1332011-12-23 01:45:32 +0800123 int fd;
124 long i, fail;
Caspar Zhang10425462011-12-23 01:34:16 +0800125 char *s;
126
127 tst_resm(TINFO, "verify contents from device.");
128 fd = open(DEVICE, O_RDONLY);
129 if (fd == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800130 tst_brkm(TBROK | TERRNO, cleanup, "open %s", DEVICE);
Caspar Zhang10425462011-12-23 01:34:16 +0800131 s = mmap(NULL, SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
132 if (s == MAP_FAILED)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800133 tst_brkm(TBROK | TERRNO, cleanup, "2nd mmap");
Caspar Zhang10425462011-12-23 01:34:16 +0800134
135 i = 0;
136 fail = 0;
137 while (s[i] && i < SIZE - 1) {
138 if (s[i] != 'a')
139 fail++;
140 i++;
141 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800142 if (i != SIZE - 1)
Caspar Zhanga4ef1332011-12-23 01:45:32 +0800143 tst_resm(TFAIL, "expect size: %ld, actual size: %ld.",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800144 SIZE - 1, i);
Caspar Zhang10425462011-12-23 01:34:16 +0800145 else if (s[i] != '\0')
146 tst_resm(TFAIL, "zram device seems not null terminated");
147 if (fail)
Caspar Zhanga4ef1332011-12-23 01:45:32 +0800148 tst_resm(TFAIL, "%ld failed bytes found.", fail);
Caspar Zhang10425462011-12-23 01:34:16 +0800149 if (munmap(s, SIZE) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800150 tst_brkm(TBROK | TERRNO, cleanup, "2nd munmap");
Caspar Zhang10425462011-12-23 01:34:16 +0800151
152 close(fd);
153}
154
Caspar Zhang37b58132011-11-17 12:13:49 +0800155static void reset(void)
156{
157 int fd;
158
159 tst_resm(TINFO, "reset it.");
160 fd = open(PATH_ZRAM "/reset", O_WRONLY);
161 if (fd == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800162 tst_brkm(TBROK | TERRNO, cleanup, "open %s",
163 PATH_ZRAM "/reset");
Caspar Zhang37b58132011-11-17 12:13:49 +0800164 if (write(fd, "1", 1) != 1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800165 tst_brkm(TBROK | TERRNO, cleanup, "write 1 to %s",
166 PATH_ZRAM "/reset");
Caspar Zhang37b58132011-11-17 12:13:49 +0800167 close(fd);
168}
169
170static void setup(void)
Garrett Cooperecd667e2011-01-19 01:06:18 -0800171{
Caspar Zhange99867a2011-11-17 12:00:26 +0800172 int retried = 0;
173
Garrett Cooperecd667e2011-01-19 01:06:18 -0800174 tst_require_root(NULL);
175
Caspar Zhange99867a2011-11-17 12:00:26 +0800176retry:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800177 if (access(PATH_ZRAM, R_OK | W_OK | X_OK) == -1) {
Caspar Zhange99867a2011-11-17 12:00:26 +0800178 if (errno == ENOENT) {
179 if (retried)
180 tst_brkm(TCONF, NULL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800181 "system has no zram device.");
Caspar Zhange99867a2011-11-17 12:00:26 +0800182 system("modprobe zram");
183 modprobe = 1;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800184 retried = 1;
Caspar Zhange99867a2011-11-17 12:00:26 +0800185 goto retry;
186 } else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800187 tst_brkm(TBROK | TERRNO, NULL, "access");
Garrett Cooperecd667e2011-01-19 01:06:18 -0800188 }
Caspar Zhange99867a2011-11-17 12:00:26 +0800189
Garrett Cooperecd667e2011-01-19 01:06:18 -0800190 tst_sig(FORK, DEF_HANDLER, cleanup);
191 TEST_PAUSE;
192}
193
Caspar Zhang37b58132011-11-17 12:13:49 +0800194static void cleanup(void)
Garrett Cooperecd667e2011-01-19 01:06:18 -0800195{
196 if (modprobe == 1)
197 system("rmmod zram");
Garrett Cooperecd667e2011-01-19 01:06:18 -0800198}
199
Caspar Zhang37b58132011-11-17 12:13:49 +0800200static void print(char *string)
Garrett Cooperecd667e2011-01-19 01:06:18 -0800201{
202 FILE *fp;
203 char buf[BUFSIZ], value[BUFSIZ];
204
205 sprintf(buf, "%s/%s", PATH_ZRAM, string);
206 fp = fopen(buf, "r");
207 if (fp == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800208 tst_brkm(TBROK | TERRNO, cleanup, "fopen %s", buf);
Garrett Cooperecd667e2011-01-19 01:06:18 -0800209
210 if (fgets(value, BUFSIZ, fp) == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800211 tst_brkm(TBROK | TERRNO, cleanup, "fgets %s", buf);
Garrett Cooperecd667e2011-01-19 01:06:18 -0800212 value[strlen(value) - 1] = '\0';
213 fclose(fp);
214
215 tst_resm(TINFO, "%s is %s", buf, value);
216}
217
Caspar Zhang37b58132011-11-17 12:13:49 +0800218static void dump_info(void)
Garrett Cooperecd667e2011-01-19 01:06:18 -0800219{
220 print("initstate");
221 print("compr_data_size");
222 print("orig_data_size");
223 print("disksize");
224 print("mem_used_total");
225 print("num_reads");
226 print("num_writes");
227 print("zero_pages");
228}