blob: 09de99fbb7e0e5e5874b2d5f662ad6b51408e8c3 [file] [log] [blame]
Nitin Gupta306b0c92009-09-22 10:26:53 +05301/*
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05302 * Compressed RAM block device
Nitin Gupta306b0c92009-09-22 10:26:53 +05303 *
Nitin Gupta1130ebb2010-01-28 21:21:35 +05304 * Copyright (C) 2008, 2009, 2010 Nitin Gupta
Nitin Gupta306b0c92009-09-22 10:26:53 +05305 *
6 * This code is released using a dual license strategy: BSD/GPL
7 * You can choose the licence that better fits your requirements.
8 *
9 * Released under the terms of 3-clause BSD License
10 * Released under the terms of GNU General Public License Version 2.0
11 *
12 * Project home: http://compcache.googlecode.com
13 */
14
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053015#define KMSG_COMPONENT "zram"
Nitin Gupta306b0c92009-09-22 10:26:53 +053016#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
17
Robert Jenningsb1f5b812011-01-28 08:59:26 -060018#ifdef CONFIG_ZRAM_DEBUG
19#define DEBUG
20#endif
21
Nitin Gupta306b0c92009-09-22 10:26:53 +053022#include <linux/module.h>
23#include <linux/kernel.h>
Randy Dunlap8946a082010-06-23 20:27:09 -070024#include <linux/bio.h>
Nitin Gupta306b0c92009-09-22 10:26:53 +053025#include <linux/bitops.h>
26#include <linux/blkdev.h>
27#include <linux/buffer_head.h>
28#include <linux/device.h>
29#include <linux/genhd.h>
30#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Nitin Gupta306b0c92009-09-22 10:26:53 +053032#include <linux/lzo.h>
Nitin Gupta306b0c92009-09-22 10:26:53 +053033#include <linux/string.h>
Nitin Gupta306b0c92009-09-22 10:26:53 +053034#include <linux/vmalloc.h>
Nitin Gupta306b0c92009-09-22 10:26:53 +053035
Nitin Gupta16a4bfb2010-06-01 13:31:24 +053036#include "zram_drv.h"
Nitin Gupta306b0c92009-09-22 10:26:53 +053037
38/* Globals */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053039static int zram_major;
Noah Watkins43801f62011-07-20 17:05:57 -060040struct zram *zram_devices;
Nitin Gupta306b0c92009-09-22 10:26:53 +053041
Nitin Gupta306b0c92009-09-22 10:26:53 +053042/* Module params (documentation at end) */
Noah Watkinsefd54f42011-07-20 17:06:08 -060043unsigned int zram_num_devices;
Nitin Gupta33863c22010-08-09 22:56:47 +053044
45static void zram_stat_inc(u32 *v)
46{
47 *v = *v + 1;
48}
49
50static void zram_stat_dec(u32 *v)
51{
52 *v = *v - 1;
53}
54
55static void zram_stat64_add(struct zram *zram, u64 *v, u64 inc)
56{
57 spin_lock(&zram->stat64_lock);
58 *v = *v + inc;
59 spin_unlock(&zram->stat64_lock);
60}
61
62static void zram_stat64_sub(struct zram *zram, u64 *v, u64 dec)
63{
64 spin_lock(&zram->stat64_lock);
65 *v = *v - dec;
66 spin_unlock(&zram->stat64_lock);
67}
68
69static void zram_stat64_inc(struct zram *zram, u64 *v)
70{
71 zram_stat64_add(zram, v, 1);
72}
Nitin Gupta306b0c92009-09-22 10:26:53 +053073
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053074static int zram_test_flag(struct zram *zram, u32 index,
75 enum zram_pageflags flag)
Nitin Gupta306b0c92009-09-22 10:26:53 +053076{
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053077 return zram->table[index].flags & BIT(flag);
Nitin Gupta306b0c92009-09-22 10:26:53 +053078}
79
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053080static void zram_set_flag(struct zram *zram, u32 index,
81 enum zram_pageflags flag)
Nitin Gupta306b0c92009-09-22 10:26:53 +053082{
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053083 zram->table[index].flags |= BIT(flag);
Nitin Gupta306b0c92009-09-22 10:26:53 +053084}
85
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053086static void zram_clear_flag(struct zram *zram, u32 index,
87 enum zram_pageflags flag)
Nitin Gupta306b0c92009-09-22 10:26:53 +053088{
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053089 zram->table[index].flags &= ~BIT(flag);
Nitin Gupta306b0c92009-09-22 10:26:53 +053090}
91
92static int page_zero_filled(void *ptr)
93{
94 unsigned int pos;
95 unsigned long *page;
96
97 page = (unsigned long *)ptr;
98
99 for (pos = 0; pos != PAGE_SIZE / sizeof(*page); pos++) {
100 if (page[pos])
101 return 0;
102 }
103
104 return 1;
105}
106
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530107static void zram_set_disksize(struct zram *zram, size_t totalram_bytes)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530108{
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530109 if (!zram->disksize) {
Nitin Gupta306b0c92009-09-22 10:26:53 +0530110 pr_info(
111 "disk size not provided. You can use disksize_kb module "
112 "param to specify size.\nUsing default: (%u%% of RAM).\n",
113 default_disksize_perc_ram
114 );
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530115 zram->disksize = default_disksize_perc_ram *
Nitin Gupta306b0c92009-09-22 10:26:53 +0530116 (totalram_bytes / 100);
117 }
118
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530119 if (zram->disksize > 2 * (totalram_bytes)) {
Nitin Gupta306b0c92009-09-22 10:26:53 +0530120 pr_info(
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530121 "There is little point creating a zram of greater than "
Nitin Gupta306b0c92009-09-22 10:26:53 +0530122 "twice the size of memory since we expect a 2:1 compression "
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530123 "ratio. Note that zram uses about 0.1%% of the size of "
124 "the disk when not in use so a huge zram is "
Nitin Gupta306b0c92009-09-22 10:26:53 +0530125 "wasteful.\n"
126 "\tMemory Size: %zu kB\n"
Nitin Gupta33863c22010-08-09 22:56:47 +0530127 "\tSize you selected: %llu kB\n"
Nitin Gupta306b0c92009-09-22 10:26:53 +0530128 "Continuing anyway ...\n",
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530129 totalram_bytes >> 10, zram->disksize
Nitin Gupta306b0c92009-09-22 10:26:53 +0530130 );
131 }
132
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530133 zram->disksize &= PAGE_MASK;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530134}
135
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530136static void zram_free_page(struct zram *zram, size_t index)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530137{
138 u32 clen;
139 void *obj;
140
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530141 struct page *page = zram->table[index].page;
142 u32 offset = zram->table[index].offset;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530143
144 if (unlikely(!page)) {
Nitin Gupta2e882282010-01-28 21:13:41 +0530145 /*
146 * No memory is allocated for zero filled pages.
147 * Simply clear zero page flag.
148 */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530149 if (zram_test_flag(zram, index, ZRAM_ZERO)) {
150 zram_clear_flag(zram, index, ZRAM_ZERO);
151 zram_stat_dec(&zram->stats.pages_zero);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530152 }
153 return;
154 }
155
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530156 if (unlikely(zram_test_flag(zram, index, ZRAM_UNCOMPRESSED))) {
Nitin Gupta306b0c92009-09-22 10:26:53 +0530157 clen = PAGE_SIZE;
158 __free_page(page);
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530159 zram_clear_flag(zram, index, ZRAM_UNCOMPRESSED);
160 zram_stat_dec(&zram->stats.pages_expand);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530161 goto out;
162 }
163
164 obj = kmap_atomic(page, KM_USER0) + offset;
165 clen = xv_get_object_size(obj) - sizeof(struct zobj_header);
166 kunmap_atomic(obj, KM_USER0);
167
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530168 xv_free(zram->mem_pool, page, offset);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530169 if (clen <= PAGE_SIZE / 2)
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530170 zram_stat_dec(&zram->stats.good_compress);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530171
172out:
Nitin Gupta33863c22010-08-09 22:56:47 +0530173 zram_stat64_sub(zram, &zram->stats.compr_size, clen);
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530174 zram_stat_dec(&zram->stats.pages_stored);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530175
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530176 zram->table[index].page = NULL;
177 zram->table[index].offset = 0;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530178}
179
Jerome Marchand924bd882011-06-10 15:28:48 +0200180static void handle_zero_page(struct bio_vec *bvec)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530181{
Jerome Marchand924bd882011-06-10 15:28:48 +0200182 struct page *page = bvec->bv_page;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530183 void *user_mem;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530184
185 user_mem = kmap_atomic(page, KM_USER0);
Jerome Marchand924bd882011-06-10 15:28:48 +0200186 memset(user_mem + bvec->bv_offset, 0, bvec->bv_len);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530187 kunmap_atomic(user_mem, KM_USER0);
188
Nitin Gupta30fb8a72009-12-12 11:44:46 +0530189 flush_dcache_page(page);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530190}
191
Jerome Marchand924bd882011-06-10 15:28:48 +0200192static void handle_uncompressed_page(struct zram *zram, struct bio_vec *bvec,
193 u32 index, int offset)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530194{
Jerome Marchand924bd882011-06-10 15:28:48 +0200195 struct page *page = bvec->bv_page;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530196 unsigned char *user_mem, *cmem;
197
Nitin Gupta306b0c92009-09-22 10:26:53 +0530198 user_mem = kmap_atomic(page, KM_USER0);
Jerome Marchand6a587e832011-06-10 15:28:46 +0200199 cmem = kmap_atomic(zram->table[index].page, KM_USER1);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530200
Jerome Marchand924bd882011-06-10 15:28:48 +0200201 memcpy(user_mem + bvec->bv_offset, cmem + offset, bvec->bv_len);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530202 kunmap_atomic(cmem, KM_USER1);
Jerome Marchanddffbb442011-07-13 17:20:05 +0200203 kunmap_atomic(user_mem, KM_USER0);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530204
Nitin Gupta30fb8a72009-12-12 11:44:46 +0530205 flush_dcache_page(page);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530206}
207
Jerome Marchand924bd882011-06-10 15:28:48 +0200208static inline int is_partial_io(struct bio_vec *bvec)
209{
210 return bvec->bv_len != PAGE_SIZE;
211}
212
Jerome Marchand8c921b22011-06-10 15:28:47 +0200213static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
Jerome Marchand924bd882011-06-10 15:28:48 +0200214 u32 index, int offset, struct bio *bio)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530215{
Jerome Marchand8c921b22011-06-10 15:28:47 +0200216 int ret;
217 size_t clen;
218 struct page *page;
219 struct zobj_header *zheader;
Jerome Marchand924bd882011-06-10 15:28:48 +0200220 unsigned char *user_mem, *cmem, *uncmem = NULL;
Nitin Guptaa1dd52a2010-06-01 13:31:23 +0530221
Jerome Marchand8c921b22011-06-10 15:28:47 +0200222 page = bvec->bv_page;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530223
Jerome Marchand8c921b22011-06-10 15:28:47 +0200224 if (zram_test_flag(zram, index, ZRAM_ZERO)) {
Jerome Marchand924bd882011-06-10 15:28:48 +0200225 handle_zero_page(bvec);
Jerome Marchand8c921b22011-06-10 15:28:47 +0200226 return 0;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530227 }
228
Jerome Marchand8c921b22011-06-10 15:28:47 +0200229 /* Requested page is not present in compressed area */
230 if (unlikely(!zram->table[index].page)) {
231 pr_debug("Read before write: sector=%lu, size=%u",
232 (ulong)(bio->bi_sector), bio->bi_size);
Jerome Marchand924bd882011-06-10 15:28:48 +0200233 handle_zero_page(bvec);
Jerome Marchand8c921b22011-06-10 15:28:47 +0200234 return 0;
235 }
Nitin Gupta306b0c92009-09-22 10:26:53 +0530236
Jerome Marchand8c921b22011-06-10 15:28:47 +0200237 /* Page is stored uncompressed since it's incompressible */
238 if (unlikely(zram_test_flag(zram, index, ZRAM_UNCOMPRESSED))) {
Jerome Marchand924bd882011-06-10 15:28:48 +0200239 handle_uncompressed_page(zram, bvec, index, offset);
Jerome Marchand8c921b22011-06-10 15:28:47 +0200240 return 0;
241 }
242
Jerome Marchand924bd882011-06-10 15:28:48 +0200243 if (is_partial_io(bvec)) {
244 /* Use a temporary buffer to decompress the page */
245 uncmem = kmalloc(PAGE_SIZE, GFP_KERNEL);
246 if (!uncmem) {
247 pr_info("Error allocating temp memory!\n");
248 return -ENOMEM;
249 }
250 }
251
Jerome Marchand8c921b22011-06-10 15:28:47 +0200252 user_mem = kmap_atomic(page, KM_USER0);
Jerome Marchand924bd882011-06-10 15:28:48 +0200253 if (!is_partial_io(bvec))
254 uncmem = user_mem;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200255 clen = PAGE_SIZE;
256
257 cmem = kmap_atomic(zram->table[index].page, KM_USER1) +
258 zram->table[index].offset;
259
260 ret = lzo1x_decompress_safe(cmem + sizeof(*zheader),
261 xv_get_object_size(cmem) - sizeof(*zheader),
Jerome Marchand924bd882011-06-10 15:28:48 +0200262 uncmem, &clen);
263
264 if (is_partial_io(bvec)) {
265 memcpy(user_mem + bvec->bv_offset, uncmem + offset,
266 bvec->bv_len);
267 kfree(uncmem);
268 }
Jerome Marchand8c921b22011-06-10 15:28:47 +0200269
Jerome Marchand8c921b22011-06-10 15:28:47 +0200270 kunmap_atomic(cmem, KM_USER1);
Jerome Marchanddffbb442011-07-13 17:20:05 +0200271 kunmap_atomic(user_mem, KM_USER0);
Jerome Marchand8c921b22011-06-10 15:28:47 +0200272
273 /* Should NEVER happen. Return bio error if it does. */
274 if (unlikely(ret != LZO_E_OK)) {
275 pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
276 zram_stat64_inc(zram, &zram->stats.failed_reads);
277 return ret;
278 }
279
280 flush_dcache_page(page);
281
282 return 0;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530283}
284
Jerome Marchand924bd882011-06-10 15:28:48 +0200285static int zram_read_before_write(struct zram *zram, char *mem, u32 index)
Jerome Marchand8c921b22011-06-10 15:28:47 +0200286{
287 int ret;
Jerome Marchand924bd882011-06-10 15:28:48 +0200288 size_t clen = PAGE_SIZE;
289 struct zobj_header *zheader;
290 unsigned char *cmem;
291
292 if (zram_test_flag(zram, index, ZRAM_ZERO) ||
293 !zram->table[index].page) {
294 memset(mem, 0, PAGE_SIZE);
295 return 0;
296 }
297
298 cmem = kmap_atomic(zram->table[index].page, KM_USER0) +
299 zram->table[index].offset;
300
301 /* Page is stored uncompressed since it's incompressible */
302 if (unlikely(zram_test_flag(zram, index, ZRAM_UNCOMPRESSED))) {
303 memcpy(mem, cmem, PAGE_SIZE);
304 kunmap_atomic(cmem, KM_USER0);
305 return 0;
306 }
307
308 ret = lzo1x_decompress_safe(cmem + sizeof(*zheader),
309 xv_get_object_size(cmem) - sizeof(*zheader),
310 mem, &clen);
311 kunmap_atomic(cmem, KM_USER0);
312
313 /* Should NEVER happen. Return bio error if it does. */
314 if (unlikely(ret != LZO_E_OK)) {
315 pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
316 zram_stat64_inc(zram, &zram->stats.failed_reads);
317 return ret;
318 }
319
320 return 0;
321}
322
323static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
324 int offset)
325{
326 int ret;
327 u32 store_offset;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200328 size_t clen;
329 struct zobj_header *zheader;
330 struct page *page, *page_store;
Jerome Marchand924bd882011-06-10 15:28:48 +0200331 unsigned char *user_mem, *cmem, *src, *uncmem = NULL;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200332
333 page = bvec->bv_page;
334 src = zram->compress_buffer;
335
Jerome Marchand924bd882011-06-10 15:28:48 +0200336 if (is_partial_io(bvec)) {
337 /*
338 * This is a partial IO. We need to read the full page
339 * before to write the changes.
340 */
341 uncmem = kmalloc(PAGE_SIZE, GFP_KERNEL);
342 if (!uncmem) {
343 pr_info("Error allocating temp memory!\n");
344 ret = -ENOMEM;
345 goto out;
346 }
347 ret = zram_read_before_write(zram, uncmem, index);
348 if (ret) {
349 kfree(uncmem);
350 goto out;
351 }
352 }
353
Jerome Marchand8c921b22011-06-10 15:28:47 +0200354 /*
355 * System overwrites unused sectors. Free memory associated
356 * with this sector now.
357 */
358 if (zram->table[index].page ||
359 zram_test_flag(zram, index, ZRAM_ZERO))
360 zram_free_page(zram, index);
361
Jerome Marchand8c921b22011-06-10 15:28:47 +0200362 user_mem = kmap_atomic(page, KM_USER0);
Jerome Marchand924bd882011-06-10 15:28:48 +0200363
364 if (is_partial_io(bvec))
365 memcpy(uncmem + offset, user_mem + bvec->bv_offset,
366 bvec->bv_len);
367 else
368 uncmem = user_mem;
369
370 if (page_zero_filled(uncmem)) {
Jerome Marchand8c921b22011-06-10 15:28:47 +0200371 kunmap_atomic(user_mem, KM_USER0);
Jerome Marchand924bd882011-06-10 15:28:48 +0200372 if (is_partial_io(bvec))
373 kfree(uncmem);
Jerome Marchand8c921b22011-06-10 15:28:47 +0200374 zram_stat_inc(&zram->stats.pages_zero);
375 zram_set_flag(zram, index, ZRAM_ZERO);
Jerome Marchand924bd882011-06-10 15:28:48 +0200376 ret = 0;
377 goto out;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200378 }
379
Jerome Marchand924bd882011-06-10 15:28:48 +0200380 ret = lzo1x_1_compress(uncmem, PAGE_SIZE, src, &clen,
Jerome Marchand8c921b22011-06-10 15:28:47 +0200381 zram->compress_workmem);
382
383 kunmap_atomic(user_mem, KM_USER0);
Jerome Marchand924bd882011-06-10 15:28:48 +0200384 if (is_partial_io(bvec))
385 kfree(uncmem);
Jerome Marchand8c921b22011-06-10 15:28:47 +0200386
387 if (unlikely(ret != LZO_E_OK)) {
Jerome Marchand8c921b22011-06-10 15:28:47 +0200388 pr_err("Compression failed! err=%d\n", ret);
Jerome Marchand924bd882011-06-10 15:28:48 +0200389 goto out;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200390 }
391
392 /*
393 * Page is incompressible. Store it as-is (uncompressed)
394 * since we do not want to return too many disk write
395 * errors which has side effect of hanging the system.
396 */
397 if (unlikely(clen > max_zpage_size)) {
398 clen = PAGE_SIZE;
399 page_store = alloc_page(GFP_NOIO | __GFP_HIGHMEM);
400 if (unlikely(!page_store)) {
Jerome Marchand8c921b22011-06-10 15:28:47 +0200401 pr_info("Error allocating memory for "
402 "incompressible page: %u\n", index);
Jerome Marchand924bd882011-06-10 15:28:48 +0200403 ret = -ENOMEM;
404 goto out;
405 }
Jerome Marchand8c921b22011-06-10 15:28:47 +0200406
Jerome Marchand924bd882011-06-10 15:28:48 +0200407 store_offset = 0;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200408 zram_set_flag(zram, index, ZRAM_UNCOMPRESSED);
409 zram_stat_inc(&zram->stats.pages_expand);
410 zram->table[index].page = page_store;
411 src = kmap_atomic(page, KM_USER0);
412 goto memstore;
413 }
414
415 if (xv_malloc(zram->mem_pool, clen + sizeof(*zheader),
Jerome Marchand924bd882011-06-10 15:28:48 +0200416 &zram->table[index].page, &store_offset,
Jerome Marchand8c921b22011-06-10 15:28:47 +0200417 GFP_NOIO | __GFP_HIGHMEM)) {
Jerome Marchand8c921b22011-06-10 15:28:47 +0200418 pr_info("Error allocating memory for compressed "
419 "page: %u, size=%zu\n", index, clen);
Jerome Marchand924bd882011-06-10 15:28:48 +0200420 ret = -ENOMEM;
421 goto out;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200422 }
423
424memstore:
Jerome Marchand924bd882011-06-10 15:28:48 +0200425 zram->table[index].offset = store_offset;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200426
427 cmem = kmap_atomic(zram->table[index].page, KM_USER1) +
428 zram->table[index].offset;
429
430#if 0
431 /* Back-reference needed for memory defragmentation */
432 if (!zram_test_flag(zram, index, ZRAM_UNCOMPRESSED)) {
433 zheader = (struct zobj_header *)cmem;
434 zheader->table_idx = index;
435 cmem += sizeof(*zheader);
436 }
437#endif
438
439 memcpy(cmem, src, clen);
440
441 kunmap_atomic(cmem, KM_USER1);
442 if (unlikely(zram_test_flag(zram, index, ZRAM_UNCOMPRESSED)))
443 kunmap_atomic(src, KM_USER0);
444
445 /* Update stats */
446 zram_stat64_add(zram, &zram->stats.compr_size, clen);
447 zram_stat_inc(&zram->stats.pages_stored);
448 if (clen <= PAGE_SIZE / 2)
449 zram_stat_inc(&zram->stats.good_compress);
450
Jerome Marchand8c921b22011-06-10 15:28:47 +0200451 return 0;
Jerome Marchand924bd882011-06-10 15:28:48 +0200452
453out:
454 if (ret)
455 zram_stat64_inc(zram, &zram->stats.failed_writes);
456 return ret;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200457}
458
459static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
Jerome Marchand924bd882011-06-10 15:28:48 +0200460 int offset, struct bio *bio, int rw)
Jerome Marchand8c921b22011-06-10 15:28:47 +0200461{
Jerome Marchandc5bde232011-06-10 15:28:49 +0200462 int ret;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200463
Jerome Marchandc5bde232011-06-10 15:28:49 +0200464 if (rw == READ) {
465 down_read(&zram->lock);
466 ret = zram_bvec_read(zram, bvec, index, offset, bio);
467 up_read(&zram->lock);
468 } else {
469 down_write(&zram->lock);
470 ret = zram_bvec_write(zram, bvec, index, offset);
471 up_write(&zram->lock);
472 }
473
474 return ret;
Jerome Marchand924bd882011-06-10 15:28:48 +0200475}
476
477static void update_position(u32 *index, int *offset, struct bio_vec *bvec)
478{
479 if (*offset + bvec->bv_len >= PAGE_SIZE)
480 (*index)++;
481 *offset = (*offset + bvec->bv_len) % PAGE_SIZE;
Jerome Marchand8c921b22011-06-10 15:28:47 +0200482}
483
484static void __zram_make_request(struct zram *zram, struct bio *bio, int rw)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530485{
Jerome Marchand924bd882011-06-10 15:28:48 +0200486 int i, offset;
Nitin Guptaa1dd52a2010-06-01 13:31:23 +0530487 u32 index;
488 struct bio_vec *bvec;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530489
Jerome Marchand8c921b22011-06-10 15:28:47 +0200490 switch (rw) {
491 case READ:
492 zram_stat64_inc(zram, &zram->stats.num_reads);
493 break;
494 case WRITE:
495 zram_stat64_inc(zram, &zram->stats.num_writes);
496 break;
497 }
498
Nitin Gupta306b0c92009-09-22 10:26:53 +0530499 index = bio->bi_sector >> SECTORS_PER_PAGE_SHIFT;
Jerome Marchand924bd882011-06-10 15:28:48 +0200500 offset = (bio->bi_sector & (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530501
Nitin Guptaa1dd52a2010-06-01 13:31:23 +0530502 bio_for_each_segment(bvec, bio, i) {
Jerome Marchand924bd882011-06-10 15:28:48 +0200503 int max_transfer_size = PAGE_SIZE - offset;
504
505 if (bvec->bv_len > max_transfer_size) {
506 /*
507 * zram_bvec_rw() can only make operation on a single
508 * zram page. Split the bio vector.
509 */
510 struct bio_vec bv;
511
512 bv.bv_page = bvec->bv_page;
513 bv.bv_len = max_transfer_size;
514 bv.bv_offset = bvec->bv_offset;
515
516 if (zram_bvec_rw(zram, &bv, index, offset, bio, rw) < 0)
517 goto out;
518
519 bv.bv_len = bvec->bv_len - max_transfer_size;
520 bv.bv_offset += max_transfer_size;
521 if (zram_bvec_rw(zram, &bv, index+1, 0, bio, rw) < 0)
522 goto out;
523 } else
524 if (zram_bvec_rw(zram, bvec, index, offset, bio, rw)
525 < 0)
526 goto out;
527
528 update_position(&index, &offset, bvec);
Nitin Guptaa1dd52a2010-06-01 13:31:23 +0530529 }
Nitin Gupta306b0c92009-09-22 10:26:53 +0530530
531 set_bit(BIO_UPTODATE, &bio->bi_flags);
532 bio_endio(bio, 0);
Nitin Gupta7d7854b2011-01-22 07:36:15 -0500533 return;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530534
535out:
Nitin Gupta306b0c92009-09-22 10:26:53 +0530536 bio_io_error(bio);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530537}
538
Nitin Gupta306b0c92009-09-22 10:26:53 +0530539/*
Jerome Marchand924bd882011-06-10 15:28:48 +0200540 * Check if request is within bounds and aligned on zram logical blocks.
Nitin Gupta306b0c92009-09-22 10:26:53 +0530541 */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530542static inline int valid_io_request(struct zram *zram, struct bio *bio)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530543{
544 if (unlikely(
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530545 (bio->bi_sector >= (zram->disksize >> SECTOR_SHIFT)) ||
Jerome Marchand924bd882011-06-10 15:28:48 +0200546 (bio->bi_sector & (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)) ||
547 (bio->bi_size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))) {
Nitin Gupta306b0c92009-09-22 10:26:53 +0530548
549 return 0;
550 }
551
Nitin Guptaa1dd52a2010-06-01 13:31:23 +0530552 /* I/O request is valid */
Nitin Gupta306b0c92009-09-22 10:26:53 +0530553 return 1;
554}
555
556/*
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530557 * Handler function for all zram I/O requests.
Nitin Gupta306b0c92009-09-22 10:26:53 +0530558 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200559static void zram_make_request(struct request_queue *queue, struct bio *bio)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530560{
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530561 struct zram *zram = queue->queuedata;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530562
Jerome Marchand0900bea2011-09-06 15:02:11 +0200563 if (unlikely(!zram->init_done) && zram_init_device(zram))
564 goto error;
565
566 down_read(&zram->init_lock);
567 if (unlikely(!zram->init_done))
568 goto error_unlock;
569
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530570 if (!valid_io_request(zram, bio)) {
571 zram_stat64_inc(zram, &zram->stats.invalid_io);
Jerome Marchand0900bea2011-09-06 15:02:11 +0200572 goto error_unlock;
Jerome Marchand6642a672011-02-17 17:11:49 +0100573 }
574
Jerome Marchand8c921b22011-06-10 15:28:47 +0200575 __zram_make_request(zram, bio, bio_data_dir(bio));
Jerome Marchand0900bea2011-09-06 15:02:11 +0200576 up_read(&zram->init_lock);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530577
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -0700578 return;
Jerome Marchand0900bea2011-09-06 15:02:11 +0200579
580error_unlock:
581 up_read(&zram->init_lock);
582error:
583 bio_io_error(bio);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530584}
585
Jerome Marchand0900bea2011-09-06 15:02:11 +0200586void __zram_reset_device(struct zram *zram)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530587{
Nitin Gupta97a06382010-05-13 14:24:21 +0530588 size_t index;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530589
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530590 zram->init_done = 0;
Nitin Gupta7eef7532010-01-28 21:13:38 +0530591
Nitin Gupta306b0c92009-09-22 10:26:53 +0530592 /* Free various per-device buffers */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530593 kfree(zram->compress_workmem);
594 free_pages((unsigned long)zram->compress_buffer, 1);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530595
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530596 zram->compress_workmem = NULL;
597 zram->compress_buffer = NULL;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530598
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530599 /* Free all pages that are still in this zram device */
600 for (index = 0; index < zram->disksize >> PAGE_SHIFT; index++) {
Nitin Gupta306b0c92009-09-22 10:26:53 +0530601 struct page *page;
602 u16 offset;
603
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530604 page = zram->table[index].page;
605 offset = zram->table[index].offset;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530606
607 if (!page)
608 continue;
609
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530610 if (unlikely(zram_test_flag(zram, index, ZRAM_UNCOMPRESSED)))
Nitin Gupta306b0c92009-09-22 10:26:53 +0530611 __free_page(page);
612 else
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530613 xv_free(zram->mem_pool, page, offset);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530614 }
615
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530616 vfree(zram->table);
617 zram->table = NULL;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530618
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530619 xv_destroy_pool(zram->mem_pool);
620 zram->mem_pool = NULL;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530621
Nitin Gupta306b0c92009-09-22 10:26:53 +0530622 /* Reset stats */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530623 memset(&zram->stats, 0, sizeof(zram->stats));
Nitin Gupta306b0c92009-09-22 10:26:53 +0530624
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530625 zram->disksize = 0;
Jerome Marchand0900bea2011-09-06 15:02:11 +0200626}
627
628void zram_reset_device(struct zram *zram)
629{
630 down_write(&zram->init_lock);
631 __zram_reset_device(zram);
632 up_write(&zram->init_lock);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530633}
634
Nitin Gupta33863c22010-08-09 22:56:47 +0530635int zram_init_device(struct zram *zram)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530636{
637 int ret;
638 size_t num_pages;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530639
Jerome Marchand0900bea2011-09-06 15:02:11 +0200640 down_write(&zram->init_lock);
Nitin Gupta484875a2010-08-09 22:56:48 +0530641
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530642 if (zram->init_done) {
Jerome Marchand0900bea2011-09-06 15:02:11 +0200643 up_write(&zram->init_lock);
Nitin Gupta484875a2010-08-09 22:56:48 +0530644 return 0;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530645 }
646
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530647 zram_set_disksize(zram, totalram_pages << PAGE_SHIFT);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530648
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530649 zram->compress_workmem = kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
650 if (!zram->compress_workmem) {
Nitin Gupta306b0c92009-09-22 10:26:53 +0530651 pr_err("Error allocating compressor working memory!\n");
652 ret = -ENOMEM;
Jerome Marchand5a18c532011-09-06 15:02:12 +0200653 goto fail_no_table;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530654 }
655
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530656 zram->compress_buffer = (void *)__get_free_pages(__GFP_ZERO, 1);
657 if (!zram->compress_buffer) {
Nitin Gupta306b0c92009-09-22 10:26:53 +0530658 pr_err("Error allocating compressor buffer space\n");
659 ret = -ENOMEM;
Jerome Marchand5a18c532011-09-06 15:02:12 +0200660 goto fail_no_table;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530661 }
662
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530663 num_pages = zram->disksize >> PAGE_SHIFT;
Joe Perches5b84cc72010-11-04 20:07:59 -0700664 zram->table = vzalloc(num_pages * sizeof(*zram->table));
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530665 if (!zram->table) {
666 pr_err("Error allocating zram address table\n");
Nitin Gupta306b0c92009-09-22 10:26:53 +0530667 ret = -ENOMEM;
Jerome Marchand5a18c532011-09-06 15:02:12 +0200668 goto fail_no_table;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530669 }
Nitin Gupta306b0c92009-09-22 10:26:53 +0530670
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530671 set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530672
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530673 /* zram devices sort of resembles non-rotational disks */
674 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530675
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530676 zram->mem_pool = xv_create_pool();
677 if (!zram->mem_pool) {
Nitin Gupta306b0c92009-09-22 10:26:53 +0530678 pr_err("Error creating memory pool\n");
679 ret = -ENOMEM;
680 goto fail;
681 }
682
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530683 zram->init_done = 1;
Jerome Marchand0900bea2011-09-06 15:02:11 +0200684 up_write(&zram->init_lock);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530685
686 pr_debug("Initialization done!\n");
687 return 0;
688
Jerome Marchand5a18c532011-09-06 15:02:12 +0200689fail_no_table:
690 /* To prevent accessing table entries during cleanup */
691 zram->disksize = 0;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530692fail:
Jerome Marchand0900bea2011-09-06 15:02:11 +0200693 __zram_reset_device(zram);
694 up_write(&zram->init_lock);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530695 pr_err("Initialization failed: err=%d\n", ret);
696 return ret;
697}
698
Nitin Gupta2ccbec02011-09-09 19:01:00 -0400699static void zram_slot_free_notify(struct block_device *bdev,
700 unsigned long index)
Nitin Gupta107c1612010-05-17 11:02:44 +0530701{
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530702 struct zram *zram;
Nitin Gupta107c1612010-05-17 11:02:44 +0530703
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530704 zram = bdev->bd_disk->private_data;
705 zram_free_page(zram, index);
706 zram_stat64_inc(zram, &zram->stats.notify_free);
Nitin Gupta107c1612010-05-17 11:02:44 +0530707}
708
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530709static const struct block_device_operations zram_devops = {
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530710 .swap_slot_free_notify = zram_slot_free_notify,
Nitin Gupta107c1612010-05-17 11:02:44 +0530711 .owner = THIS_MODULE
Nitin Gupta306b0c92009-09-22 10:26:53 +0530712};
713
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530714static int create_device(struct zram *zram, int device_id)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530715{
Nitin Guptade1a21a2010-01-28 21:13:40 +0530716 int ret = 0;
717
Jerome Marchandc5bde232011-06-10 15:28:49 +0200718 init_rwsem(&zram->lock);
Jerome Marchand0900bea2011-09-06 15:02:11 +0200719 init_rwsem(&zram->init_lock);
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530720 spin_lock_init(&zram->stat64_lock);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530721
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530722 zram->queue = blk_alloc_queue(GFP_KERNEL);
723 if (!zram->queue) {
Nitin Gupta306b0c92009-09-22 10:26:53 +0530724 pr_err("Error allocating disk queue for device %d\n",
725 device_id);
Nitin Guptade1a21a2010-01-28 21:13:40 +0530726 ret = -ENOMEM;
727 goto out;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530728 }
729
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530730 blk_queue_make_request(zram->queue, zram_make_request);
731 zram->queue->queuedata = zram;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530732
733 /* gendisk structure */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530734 zram->disk = alloc_disk(1);
735 if (!zram->disk) {
736 blk_cleanup_queue(zram->queue);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530737 pr_warning("Error allocating disk structure for device %d\n",
738 device_id);
Nitin Guptade1a21a2010-01-28 21:13:40 +0530739 ret = -ENOMEM;
740 goto out;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530741 }
742
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530743 zram->disk->major = zram_major;
744 zram->disk->first_minor = device_id;
745 zram->disk->fops = &zram_devops;
746 zram->disk->queue = zram->queue;
747 zram->disk->private_data = zram;
748 snprintf(zram->disk->disk_name, 16, "zram%d", device_id);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530749
Nitin Gupta33863c22010-08-09 22:56:47 +0530750 /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530751 set_capacity(zram->disk, 0);
Nitin Gupta5d83d5a2010-01-28 21:13:39 +0530752
Nitin Guptaa1dd52a2010-06-01 13:31:23 +0530753 /*
754 * To ensure that we always get PAGE_SIZE aligned
755 * and n*PAGE_SIZED sized I/O requests.
756 */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530757 blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE);
Robert Jennings7b19b8d2011-01-28 08:58:17 -0600758 blk_queue_logical_block_size(zram->disk->queue,
759 ZRAM_LOGICAL_BLOCK_SIZE);
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530760 blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
761 blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
Nitin Gupta5d83d5a2010-01-28 21:13:39 +0530762
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530763 add_disk(zram->disk);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530764
Nitin Gupta33863c22010-08-09 22:56:47 +0530765 ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj,
766 &zram_disk_attr_group);
767 if (ret < 0) {
768 pr_warning("Error creating sysfs group");
769 goto out;
770 }
Nitin Gupta33863c22010-08-09 22:56:47 +0530771
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530772 zram->init_done = 0;
Nitin Guptade1a21a2010-01-28 21:13:40 +0530773
774out:
775 return ret;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530776}
777
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530778static void destroy_device(struct zram *zram)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530779{
Nitin Gupta33863c22010-08-09 22:56:47 +0530780 sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
781 &zram_disk_attr_group);
Nitin Gupta33863c22010-08-09 22:56:47 +0530782
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530783 if (zram->disk) {
784 del_gendisk(zram->disk);
785 put_disk(zram->disk);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530786 }
787
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530788 if (zram->queue)
789 blk_cleanup_queue(zram->queue);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530790}
791
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530792static int __init zram_init(void)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530793{
Nitin Guptade1a21a2010-01-28 21:13:40 +0530794 int ret, dev_id;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530795
Noah Watkinsefd54f42011-07-20 17:06:08 -0600796 if (zram_num_devices > max_num_devices) {
Nitin Gupta306b0c92009-09-22 10:26:53 +0530797 pr_warning("Invalid value for num_devices: %u\n",
Noah Watkinsefd54f42011-07-20 17:06:08 -0600798 zram_num_devices);
Nitin Guptade1a21a2010-01-28 21:13:40 +0530799 ret = -EINVAL;
800 goto out;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530801 }
802
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530803 zram_major = register_blkdev(0, "zram");
804 if (zram_major <= 0) {
Nitin Gupta306b0c92009-09-22 10:26:53 +0530805 pr_warning("Unable to get major number\n");
Nitin Guptade1a21a2010-01-28 21:13:40 +0530806 ret = -EBUSY;
807 goto out;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530808 }
809
Noah Watkinsefd54f42011-07-20 17:06:08 -0600810 if (!zram_num_devices) {
Nitin Gupta306b0c92009-09-22 10:26:53 +0530811 pr_info("num_devices not specified. Using default: 1\n");
Noah Watkinsefd54f42011-07-20 17:06:08 -0600812 zram_num_devices = 1;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530813 }
814
815 /* Allocate the device array and initialize each one */
Noah Watkinsefd54f42011-07-20 17:06:08 -0600816 pr_info("Creating %u devices ...\n", zram_num_devices);
817 zram_devices = kzalloc(zram_num_devices * sizeof(struct zram), GFP_KERNEL);
Noah Watkins43801f62011-07-20 17:05:57 -0600818 if (!zram_devices) {
Nitin Guptade1a21a2010-01-28 21:13:40 +0530819 ret = -ENOMEM;
820 goto unregister;
821 }
Nitin Gupta306b0c92009-09-22 10:26:53 +0530822
Noah Watkinsefd54f42011-07-20 17:06:08 -0600823 for (dev_id = 0; dev_id < zram_num_devices; dev_id++) {
Noah Watkins43801f62011-07-20 17:05:57 -0600824 ret = create_device(&zram_devices[dev_id], dev_id);
Nitin Guptade1a21a2010-01-28 21:13:40 +0530825 if (ret)
Minchan Kim3bf040c2010-01-11 16:15:53 +0900826 goto free_devices;
Nitin Guptade1a21a2010-01-28 21:13:40 +0530827 }
828
Nitin Gupta306b0c92009-09-22 10:26:53 +0530829 return 0;
Nitin Guptade1a21a2010-01-28 21:13:40 +0530830
Minchan Kim3bf040c2010-01-11 16:15:53 +0900831free_devices:
Nitin Guptade1a21a2010-01-28 21:13:40 +0530832 while (dev_id)
Noah Watkins43801f62011-07-20 17:05:57 -0600833 destroy_device(&zram_devices[--dev_id]);
834 kfree(zram_devices);
Nitin Guptade1a21a2010-01-28 21:13:40 +0530835unregister:
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530836 unregister_blkdev(zram_major, "zram");
Nitin Guptade1a21a2010-01-28 21:13:40 +0530837out:
Nitin Gupta306b0c92009-09-22 10:26:53 +0530838 return ret;
839}
840
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530841static void __exit zram_exit(void)
Nitin Gupta306b0c92009-09-22 10:26:53 +0530842{
843 int i;
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530844 struct zram *zram;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530845
Noah Watkinsefd54f42011-07-20 17:06:08 -0600846 for (i = 0; i < zram_num_devices; i++) {
Noah Watkins43801f62011-07-20 17:05:57 -0600847 zram = &zram_devices[i];
Nitin Gupta306b0c92009-09-22 10:26:53 +0530848
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530849 destroy_device(zram);
850 if (zram->init_done)
Nitin Gupta33863c22010-08-09 22:56:47 +0530851 zram_reset_device(zram);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530852 }
853
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530854 unregister_blkdev(zram_major, "zram");
Nitin Gupta306b0c92009-09-22 10:26:53 +0530855
Noah Watkins43801f62011-07-20 17:05:57 -0600856 kfree(zram_devices);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530857 pr_debug("Cleanup done!\n");
858}
859
Noah Watkinsefd54f42011-07-20 17:06:08 -0600860module_param(zram_num_devices, uint, 0);
861MODULE_PARM_DESC(zram_num_devices, "Number of zram devices");
Nitin Gupta306b0c92009-09-22 10:26:53 +0530862
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530863module_init(zram_init);
864module_exit(zram_exit);
Nitin Gupta306b0c92009-09-22 10:26:53 +0530865
866MODULE_LICENSE("Dual BSD/GPL");
867MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530868MODULE_DESCRIPTION("Compressed RAM Block Device");