fs/ecryptfs/file.c: introduce missing free
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mtd / tests / mtd_stresstest.c
blob3854afec56d0575b65d00c824d898c2347d69529
1 /*
2 * Copyright (C) 2006-2008 Nokia Corporation
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
13 * You should have received a copy of the GNU General Public License along with
14 * this program; see the file COPYING. If not, write to the Free Software
15 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 * Test random reads, writes and erases on MTD device.
19 * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/err.h>
26 #include <linux/mtd/mtd.h>
27 #include <linux/slab.h>
28 #include <linux/sched.h>
29 #include <linux/vmalloc.h>
31 #define PRINT_PREF KERN_INFO "mtd_stresstest: "
33 static int dev;
34 module_param(dev, int, S_IRUGO);
35 MODULE_PARM_DESC(dev, "MTD device number to use");
37 static int count = 10000;
38 module_param(count, int, S_IRUGO);
39 MODULE_PARM_DESC(count, "Number of operations to do (default is 10000)");
41 static struct mtd_info *mtd;
42 static unsigned char *writebuf;
43 static unsigned char *readbuf;
44 static unsigned char *bbt;
45 static int *offsets;
47 static int pgsize;
48 static int bufsize;
49 static int ebcnt;
50 static int pgcnt;
51 static unsigned long next = 1;
53 static inline unsigned int simple_rand(void)
55 next = next * 1103515245 + 12345;
56 return (unsigned int)((next / 65536) % 32768);
59 static inline void simple_srand(unsigned long seed)
61 next = seed;
64 static int rand_eb(void)
66 int eb;
68 again:
69 if (ebcnt < 32768)
70 eb = simple_rand();
71 else
72 eb = (simple_rand() << 15) | simple_rand();
73 /* Read or write up 2 eraseblocks at a time - hence 'ebcnt - 1' */
74 eb %= (ebcnt - 1);
75 if (bbt[eb])
76 goto again;
77 return eb;
80 static int rand_offs(void)
82 int offs;
84 if (bufsize < 32768)
85 offs = simple_rand();
86 else
87 offs = (simple_rand() << 15) | simple_rand();
88 offs %= bufsize;
89 return offs;
92 static int rand_len(int offs)
94 int len;
96 if (bufsize < 32768)
97 len = simple_rand();
98 else
99 len = (simple_rand() << 15) | simple_rand();
100 len %= (bufsize - offs);
101 return len;
104 static int erase_eraseblock(int ebnum)
106 int err;
107 struct erase_info ei;
108 loff_t addr = ebnum * mtd->erasesize;
110 memset(&ei, 0, sizeof(struct erase_info));
111 ei.mtd = mtd;
112 ei.addr = addr;
113 ei.len = mtd->erasesize;
115 err = mtd->erase(mtd, &ei);
116 if (unlikely(err)) {
117 printk(PRINT_PREF "error %d while erasing EB %d\n", err, ebnum);
118 return err;
121 if (unlikely(ei.state == MTD_ERASE_FAILED)) {
122 printk(PRINT_PREF "some erase error occurred at EB %d\n",
123 ebnum);
124 return -EIO;
127 return 0;
130 static int is_block_bad(int ebnum)
132 loff_t addr = ebnum * mtd->erasesize;
133 int ret;
135 ret = mtd->block_isbad(mtd, addr);
136 if (ret)
137 printk(PRINT_PREF "block %d is bad\n", ebnum);
138 return ret;
141 static int do_read(void)
143 size_t read = 0;
144 int eb = rand_eb();
145 int offs = rand_offs();
146 int len = rand_len(offs), err;
147 loff_t addr;
149 if (bbt[eb + 1]) {
150 if (offs >= mtd->erasesize)
151 offs -= mtd->erasesize;
152 if (offs + len > mtd->erasesize)
153 len = mtd->erasesize - offs;
155 addr = eb * mtd->erasesize + offs;
156 err = mtd->read(mtd, addr, len, &read, readbuf);
157 if (err == -EUCLEAN)
158 err = 0;
159 if (unlikely(err || read != len)) {
160 printk(PRINT_PREF "error: read failed at 0x%llx\n",
161 (long long)addr);
162 if (!err)
163 err = -EINVAL;
164 return err;
166 return 0;
169 static int do_write(void)
171 int eb = rand_eb(), offs, err, len;
172 size_t written = 0;
173 loff_t addr;
175 offs = offsets[eb];
176 if (offs >= mtd->erasesize) {
177 err = erase_eraseblock(eb);
178 if (err)
179 return err;
180 offs = offsets[eb] = 0;
182 len = rand_len(offs);
183 len = ((len + pgsize - 1) / pgsize) * pgsize;
184 if (offs + len > mtd->erasesize) {
185 if (bbt[eb + 1])
186 len = mtd->erasesize - offs;
187 else {
188 err = erase_eraseblock(eb + 1);
189 if (err)
190 return err;
191 offsets[eb + 1] = 0;
194 addr = eb * mtd->erasesize + offs;
195 err = mtd->write(mtd, addr, len, &written, writebuf);
196 if (unlikely(err || written != len)) {
197 printk(PRINT_PREF "error: write failed at 0x%llx\n",
198 (long long)addr);
199 if (!err)
200 err = -EINVAL;
201 return err;
203 offs += len;
204 while (offs > mtd->erasesize) {
205 offsets[eb++] = mtd->erasesize;
206 offs -= mtd->erasesize;
208 offsets[eb] = offs;
209 return 0;
212 static int do_operation(void)
214 if (simple_rand() & 1)
215 return do_read();
216 else
217 return do_write();
220 static int scan_for_bad_eraseblocks(void)
222 int i, bad = 0;
224 bbt = kmalloc(ebcnt, GFP_KERNEL);
225 if (!bbt) {
226 printk(PRINT_PREF "error: cannot allocate memory\n");
227 return -ENOMEM;
229 memset(bbt, 0 , ebcnt);
231 /* NOR flash does not implement block_isbad */
232 if (mtd->block_isbad == NULL)
233 return 0;
235 printk(PRINT_PREF "scanning for bad eraseblocks\n");
236 for (i = 0; i < ebcnt; ++i) {
237 bbt[i] = is_block_bad(i) ? 1 : 0;
238 if (bbt[i])
239 bad += 1;
240 cond_resched();
242 printk(PRINT_PREF "scanned %d eraseblocks, %d are bad\n", i, bad);
243 return 0;
246 static int __init mtd_stresstest_init(void)
248 int err;
249 int i, op;
250 uint64_t tmp;
252 printk(KERN_INFO "\n");
253 printk(KERN_INFO "=================================================\n");
254 printk(PRINT_PREF "MTD device: %d\n", dev);
256 mtd = get_mtd_device(NULL, dev);
257 if (IS_ERR(mtd)) {
258 err = PTR_ERR(mtd);
259 printk(PRINT_PREF "error: cannot get MTD device\n");
260 return err;
263 if (mtd->writesize == 1) {
264 printk(PRINT_PREF "not NAND flash, assume page size is 512 "
265 "bytes.\n");
266 pgsize = 512;
267 } else
268 pgsize = mtd->writesize;
270 tmp = mtd->size;
271 do_div(tmp, mtd->erasesize);
272 ebcnt = tmp;
273 pgcnt = mtd->erasesize / pgsize;
275 printk(PRINT_PREF "MTD device size %llu, eraseblock size %u, "
276 "page size %u, count of eraseblocks %u, pages per "
277 "eraseblock %u, OOB size %u\n",
278 (unsigned long long)mtd->size, mtd->erasesize,
279 pgsize, ebcnt, pgcnt, mtd->oobsize);
281 /* Read or write up 2 eraseblocks at a time */
282 bufsize = mtd->erasesize * 2;
284 err = -ENOMEM;
285 readbuf = vmalloc(bufsize);
286 writebuf = vmalloc(bufsize);
287 offsets = kmalloc(ebcnt * sizeof(int), GFP_KERNEL);
288 if (!readbuf || !writebuf || !offsets) {
289 printk(PRINT_PREF "error: cannot allocate memory\n");
290 goto out;
292 for (i = 0; i < ebcnt; i++)
293 offsets[i] = mtd->erasesize;
294 simple_srand(current->pid);
295 for (i = 0; i < bufsize; i++)
296 writebuf[i] = simple_rand();
298 err = scan_for_bad_eraseblocks();
299 if (err)
300 goto out;
302 /* Do operations */
303 printk(PRINT_PREF "doing operations\n");
304 for (op = 0; op < count; op++) {
305 if ((op & 1023) == 0)
306 printk(PRINT_PREF "%d operations done\n", op);
307 err = do_operation();
308 if (err)
309 goto out;
310 cond_resched();
312 printk(PRINT_PREF "finished, %d operations done\n", op);
314 out:
315 kfree(offsets);
316 kfree(bbt);
317 vfree(writebuf);
318 vfree(readbuf);
319 put_mtd_device(mtd);
320 if (err)
321 printk(PRINT_PREF "error %d occurred\n", err);
322 printk(KERN_INFO "=================================================\n");
323 return err;
325 module_init(mtd_stresstest_init);
327 static void __exit mtd_stresstest_exit(void)
329 return;
331 module_exit(mtd_stresstest_exit);
333 MODULE_DESCRIPTION("Stress test module");
334 MODULE_AUTHOR("Adrian Hunter");
335 MODULE_LICENSE("GPL");