staging: comedi: addi_watchdog: all i/o registers are 32-bit
[pandora-kernel.git] / drivers / staging / zram / zram_drv.c
1 /*
2  * Compressed RAM block device
3  *
4  * Copyright (C) 2008, 2009, 2010  Nitin Gupta
5  *
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
15 #define KMSG_COMPONENT "zram"
16 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
17
18 #ifdef CONFIG_ZRAM_DEBUG
19 #define DEBUG
20 #endif
21
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/bio.h>
25 #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>
31 #include <linux/slab.h>
32 #include <linux/lzo.h>
33 #include <linux/string.h>
34 #include <linux/vmalloc.h>
35
36 #include "zram_drv.h"
37
38 /* Globals */
39 static int zram_major;
40 struct zram *zram_devices;
41
42 /* Module params (documentation at end) */
43 static unsigned int num_devices = 1;
44
45 static void zram_stat64_add(struct zram *zram, u64 *v, u64 inc)
46 {
47         spin_lock(&zram->stat64_lock);
48         *v = *v + inc;
49         spin_unlock(&zram->stat64_lock);
50 }
51
52 static void zram_stat64_sub(struct zram *zram, u64 *v, u64 dec)
53 {
54         spin_lock(&zram->stat64_lock);
55         *v = *v - dec;
56         spin_unlock(&zram->stat64_lock);
57 }
58
59 static void zram_stat64_inc(struct zram *zram, u64 *v)
60 {
61         zram_stat64_add(zram, v, 1);
62 }
63
64 static int zram_test_flag(struct zram *zram, u32 index,
65                         enum zram_pageflags flag)
66 {
67         return zram->table[index].flags & BIT(flag);
68 }
69
70 static void zram_set_flag(struct zram *zram, u32 index,
71                         enum zram_pageflags flag)
72 {
73         zram->table[index].flags |= BIT(flag);
74 }
75
76 static void zram_clear_flag(struct zram *zram, u32 index,
77                         enum zram_pageflags flag)
78 {
79         zram->table[index].flags &= ~BIT(flag);
80 }
81
82 static int page_zero_filled(void *ptr)
83 {
84         unsigned int pos;
85         unsigned long *page;
86
87         page = (unsigned long *)ptr;
88
89         for (pos = 0; pos != PAGE_SIZE / sizeof(*page); pos++) {
90                 if (page[pos])
91                         return 0;
92         }
93
94         return 1;
95 }
96
97 static void zram_set_disksize(struct zram *zram, size_t totalram_bytes)
98 {
99         if (!zram->disksize) {
100                 pr_info(
101                 "disk size not provided. You can use disksize_kb module "
102                 "param to specify size.\nUsing default: (%u%% of RAM).\n",
103                 default_disksize_perc_ram
104                 );
105                 zram->disksize = default_disksize_perc_ram *
106                                         (totalram_bytes / 100);
107         }
108
109         if (zram->disksize > 2 * (totalram_bytes)) {
110                 pr_info(
111                 "There is little point creating a zram of greater than "
112                 "twice the size of memory since we expect a 2:1 compression "
113                 "ratio. Note that zram uses about 0.1%% of the size of "
114                 "the disk when not in use so a huge zram is "
115                 "wasteful.\n"
116                 "\tMemory Size: %zu kB\n"
117                 "\tSize you selected: %llu kB\n"
118                 "Continuing anyway ...\n",
119                 totalram_bytes >> 10, zram->disksize >> 10);
120         }
121
122         zram->disksize &= PAGE_MASK;
123 }
124
125 static void zram_free_page(struct zram *zram, size_t index)
126 {
127         unsigned long handle = zram->table[index].handle;
128         u16 size = zram->table[index].size;
129
130         if (unlikely(!handle)) {
131                 /*
132                  * No memory is allocated for zero filled pages.
133                  * Simply clear zero page flag.
134                  */
135                 if (zram_test_flag(zram, index, ZRAM_ZERO)) {
136                         zram_clear_flag(zram, index, ZRAM_ZERO);
137                         zram->stats.pages_zero--;
138                 }
139                 return;
140         }
141
142         if (unlikely(size > max_zpage_size))
143                 zram->stats.bad_compress--;
144
145         zs_free(zram->mem_pool, handle);
146
147         if (size <= PAGE_SIZE / 2)
148                 zram->stats.good_compress--;
149
150         zram_stat64_sub(zram, &zram->stats.compr_size,
151                         zram->table[index].size);
152         zram->stats.pages_stored--;
153
154         zram->table[index].handle = 0;
155         zram->table[index].size = 0;
156 }
157
158 static void handle_zero_page(struct bio_vec *bvec)
159 {
160         struct page *page = bvec->bv_page;
161         void *user_mem;
162
163         user_mem = kmap_atomic(page);
164         memset(user_mem + bvec->bv_offset, 0, bvec->bv_len);
165         kunmap_atomic(user_mem);
166
167         flush_dcache_page(page);
168 }
169
170 static inline int is_partial_io(struct bio_vec *bvec)
171 {
172         return bvec->bv_len != PAGE_SIZE;
173 }
174
175 static int zram_decompress_page(struct zram *zram, char *mem, u32 index)
176 {
177         int ret = LZO_E_OK;
178         size_t clen = PAGE_SIZE;
179         unsigned char *cmem;
180         unsigned long handle = zram->table[index].handle;
181
182         if (!handle || zram_test_flag(zram, index, ZRAM_ZERO)) {
183                 memset(mem, 0, PAGE_SIZE);
184                 return 0;
185         }
186
187         cmem = zs_map_object(zram->mem_pool, handle, ZS_MM_RO);
188         if (zram->table[index].size == PAGE_SIZE)
189                 memcpy(mem, cmem, PAGE_SIZE);
190         else
191                 ret = lzo1x_decompress_safe(cmem, zram->table[index].size,
192                                                 mem, &clen);
193         zs_unmap_object(zram->mem_pool, handle);
194
195         /* Should NEVER happen. Return bio error if it does. */
196         if (unlikely(ret != LZO_E_OK)) {
197                 pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
198                 zram_stat64_inc(zram, &zram->stats.failed_reads);
199                 return ret;
200         }
201
202         return 0;
203 }
204
205 static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
206                           u32 index, int offset, struct bio *bio)
207 {
208         int ret;
209         struct page *page;
210         unsigned char *user_mem, *uncmem = NULL;
211
212         page = bvec->bv_page;
213
214         if (unlikely(!zram->table[index].handle) ||
215                         zram_test_flag(zram, index, ZRAM_ZERO)) {
216                 handle_zero_page(bvec);
217                 return 0;
218         }
219
220         user_mem = kmap_atomic(page);
221         if (is_partial_io(bvec))
222                 /* Use  a temporary buffer to decompress the page */
223                 uncmem = kmalloc(PAGE_SIZE, GFP_KERNEL);
224         else
225                 uncmem = user_mem;
226
227         if (!uncmem) {
228                 pr_info("Unable to allocate temp memory\n");
229                 ret = -ENOMEM;
230                 goto out_cleanup;
231         }
232
233         ret = zram_decompress_page(zram, uncmem, index);
234         /* Should NEVER happen. Return bio error if it does. */
235         if (unlikely(ret != LZO_E_OK)) {
236                 pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
237                 zram_stat64_inc(zram, &zram->stats.failed_reads);
238                 goto out_cleanup;
239         }
240
241         if (is_partial_io(bvec))
242                 memcpy(user_mem + bvec->bv_offset, uncmem + offset,
243                                 bvec->bv_len);
244
245         flush_dcache_page(page);
246         ret = 0;
247 out_cleanup:
248         kunmap_atomic(user_mem);
249         if (is_partial_io(bvec))
250                 kfree(uncmem);
251         return ret;
252 }
253
254 static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
255                            int offset)
256 {
257         int ret;
258         size_t clen;
259         unsigned long handle;
260         struct page *page;
261         unsigned char *user_mem, *cmem, *src, *uncmem = NULL;
262
263         page = bvec->bv_page;
264         src = zram->compress_buffer;
265
266         if (is_partial_io(bvec)) {
267                 /*
268                  * This is a partial IO. We need to read the full page
269                  * before to write the changes.
270                  */
271                 uncmem = kmalloc(PAGE_SIZE, GFP_KERNEL);
272                 if (!uncmem) {
273                         pr_info("Error allocating temp memory!\n");
274                         ret = -ENOMEM;
275                         goto out;
276                 }
277                 ret = zram_decompress_page(zram, uncmem, index);
278                 if (ret) {
279                         kfree(uncmem);
280                         goto out;
281                 }
282         }
283
284         /*
285          * System overwrites unused sectors. Free memory associated
286          * with this sector now.
287          */
288         if (zram->table[index].handle ||
289             zram_test_flag(zram, index, ZRAM_ZERO))
290                 zram_free_page(zram, index);
291
292         user_mem = kmap_atomic(page);
293
294         if (is_partial_io(bvec))
295                 memcpy(uncmem + offset, user_mem + bvec->bv_offset,
296                        bvec->bv_len);
297         else
298                 uncmem = user_mem;
299
300         if (page_zero_filled(uncmem)) {
301                 kunmap_atomic(user_mem);
302                 if (is_partial_io(bvec))
303                         kfree(uncmem);
304                 zram->stats.pages_zero++;
305                 zram_set_flag(zram, index, ZRAM_ZERO);
306                 ret = 0;
307                 goto out;
308         }
309
310         ret = lzo1x_1_compress(uncmem, PAGE_SIZE, src, &clen,
311                                zram->compress_workmem);
312
313         kunmap_atomic(user_mem);
314         if (is_partial_io(bvec))
315                         kfree(uncmem);
316
317         if (unlikely(ret != LZO_E_OK)) {
318                 pr_err("Compression failed! err=%d\n", ret);
319                 goto out;
320         }
321
322         if (unlikely(clen > max_zpage_size)) {
323                 zram->stats.bad_compress++;
324                 src = uncmem;
325                 clen = PAGE_SIZE;
326         }
327
328         handle = zs_malloc(zram->mem_pool, clen);
329         if (!handle) {
330                 pr_info("Error allocating memory for compressed "
331                         "page: %u, size=%zu\n", index, clen);
332                 ret = -ENOMEM;
333                 goto out;
334         }
335         cmem = zs_map_object(zram->mem_pool, handle, ZS_MM_WO);
336
337         memcpy(cmem, src, clen);
338
339         zs_unmap_object(zram->mem_pool, handle);
340
341         zram->table[index].handle = handle;
342         zram->table[index].size = clen;
343
344         /* Update stats */
345         zram_stat64_add(zram, &zram->stats.compr_size, clen);
346         zram->stats.pages_stored++;
347         if (clen <= PAGE_SIZE / 2)
348                 zram->stats.good_compress++;
349
350         return 0;
351
352 out:
353         if (ret)
354                 zram_stat64_inc(zram, &zram->stats.failed_writes);
355         return ret;
356 }
357
358 static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
359                         int offset, struct bio *bio, int rw)
360 {
361         int ret;
362
363         if (rw == READ) {
364                 down_read(&zram->lock);
365                 ret = zram_bvec_read(zram, bvec, index, offset, bio);
366                 up_read(&zram->lock);
367         } else {
368                 down_write(&zram->lock);
369                 ret = zram_bvec_write(zram, bvec, index, offset);
370                 up_write(&zram->lock);
371         }
372
373         return ret;
374 }
375
376 static void update_position(u32 *index, int *offset, struct bio_vec *bvec)
377 {
378         if (*offset + bvec->bv_len >= PAGE_SIZE)
379                 (*index)++;
380         *offset = (*offset + bvec->bv_len) % PAGE_SIZE;
381 }
382
383 static void __zram_make_request(struct zram *zram, struct bio *bio, int rw)
384 {
385         int i, offset;
386         u32 index;
387         struct bio_vec *bvec;
388
389         switch (rw) {
390         case READ:
391                 zram_stat64_inc(zram, &zram->stats.num_reads);
392                 break;
393         case WRITE:
394                 zram_stat64_inc(zram, &zram->stats.num_writes);
395                 break;
396         }
397
398         index = bio->bi_sector >> SECTORS_PER_PAGE_SHIFT;
399         offset = (bio->bi_sector & (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT;
400
401         bio_for_each_segment(bvec, bio, i) {
402                 int max_transfer_size = PAGE_SIZE - offset;
403
404                 if (bvec->bv_len > max_transfer_size) {
405                         /*
406                          * zram_bvec_rw() can only make operation on a single
407                          * zram page. Split the bio vector.
408                          */
409                         struct bio_vec bv;
410
411                         bv.bv_page = bvec->bv_page;
412                         bv.bv_len = max_transfer_size;
413                         bv.bv_offset = bvec->bv_offset;
414
415                         if (zram_bvec_rw(zram, &bv, index, offset, bio, rw) < 0)
416                                 goto out;
417
418                         bv.bv_len = bvec->bv_len - max_transfer_size;
419                         bv.bv_offset += max_transfer_size;
420                         if (zram_bvec_rw(zram, &bv, index+1, 0, bio, rw) < 0)
421                                 goto out;
422                 } else
423                         if (zram_bvec_rw(zram, bvec, index, offset, bio, rw)
424                             < 0)
425                                 goto out;
426
427                 update_position(&index, &offset, bvec);
428         }
429
430         set_bit(BIO_UPTODATE, &bio->bi_flags);
431         bio_endio(bio, 0);
432         return;
433
434 out:
435         bio_io_error(bio);
436 }
437
438 /*
439  * Check if request is within bounds and aligned on zram logical blocks.
440  */
441 static inline int valid_io_request(struct zram *zram, struct bio *bio)
442 {
443         if (unlikely(
444                 (bio->bi_sector >= (zram->disksize >> SECTOR_SHIFT)) ||
445                 (bio->bi_sector & (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)) ||
446                 (bio->bi_size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))) {
447
448                 return 0;
449         }
450
451         /* I/O request is valid */
452         return 1;
453 }
454
455 /*
456  * Handler function for all zram I/O requests.
457  */
458 static void zram_make_request(struct request_queue *queue, struct bio *bio)
459 {
460         struct zram *zram = queue->queuedata;
461
462         if (unlikely(!zram->init_done) && zram_init_device(zram))
463                 goto error;
464
465         down_read(&zram->init_lock);
466         if (unlikely(!zram->init_done))
467                 goto error_unlock;
468
469         if (!valid_io_request(zram, bio)) {
470                 zram_stat64_inc(zram, &zram->stats.invalid_io);
471                 goto error_unlock;
472         }
473
474         __zram_make_request(zram, bio, bio_data_dir(bio));
475         up_read(&zram->init_lock);
476
477         return;
478
479 error_unlock:
480         up_read(&zram->init_lock);
481 error:
482         bio_io_error(bio);
483 }
484
485 void __zram_reset_device(struct zram *zram)
486 {
487         size_t index;
488
489         zram->init_done = 0;
490
491         /* Free various per-device buffers */
492         kfree(zram->compress_workmem);
493         free_pages((unsigned long)zram->compress_buffer, 1);
494
495         zram->compress_workmem = NULL;
496         zram->compress_buffer = NULL;
497
498         /* Free all pages that are still in this zram device */
499         for (index = 0; index < zram->disksize >> PAGE_SHIFT; index++) {
500                 unsigned long handle = zram->table[index].handle;
501                 if (!handle)
502                         continue;
503
504                 zs_free(zram->mem_pool, handle);
505         }
506
507         vfree(zram->table);
508         zram->table = NULL;
509
510         zs_destroy_pool(zram->mem_pool);
511         zram->mem_pool = NULL;
512
513         /* Reset stats */
514         memset(&zram->stats, 0, sizeof(zram->stats));
515
516         zram->disksize = 0;
517 }
518
519 void zram_reset_device(struct zram *zram)
520 {
521         down_write(&zram->init_lock);
522         __zram_reset_device(zram);
523         up_write(&zram->init_lock);
524 }
525
526 int zram_init_device(struct zram *zram)
527 {
528         int ret;
529         size_t num_pages;
530
531         down_write(&zram->init_lock);
532
533         if (zram->init_done) {
534                 up_write(&zram->init_lock);
535                 return 0;
536         }
537
538         zram_set_disksize(zram, totalram_pages << PAGE_SHIFT);
539
540         zram->compress_workmem = kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
541         if (!zram->compress_workmem) {
542                 pr_err("Error allocating compressor working memory!\n");
543                 ret = -ENOMEM;
544                 goto fail_no_table;
545         }
546
547         zram->compress_buffer =
548                 (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
549         if (!zram->compress_buffer) {
550                 pr_err("Error allocating compressor buffer space\n");
551                 ret = -ENOMEM;
552                 goto fail_no_table;
553         }
554
555         num_pages = zram->disksize >> PAGE_SHIFT;
556         zram->table = vzalloc(num_pages * sizeof(*zram->table));
557         if (!zram->table) {
558                 pr_err("Error allocating zram address table\n");
559                 ret = -ENOMEM;
560                 goto fail_no_table;
561         }
562
563         set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
564
565         /* zram devices sort of resembles non-rotational disks */
566         queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue);
567
568         zram->mem_pool = zs_create_pool("zram", GFP_NOIO | __GFP_HIGHMEM);
569         if (!zram->mem_pool) {
570                 pr_err("Error creating memory pool\n");
571                 ret = -ENOMEM;
572                 goto fail;
573         }
574
575         zram->init_done = 1;
576         up_write(&zram->init_lock);
577
578         pr_debug("Initialization done!\n");
579         return 0;
580
581 fail_no_table:
582         /* To prevent accessing table entries during cleanup */
583         zram->disksize = 0;
584 fail:
585         __zram_reset_device(zram);
586         up_write(&zram->init_lock);
587         pr_err("Initialization failed: err=%d\n", ret);
588         return ret;
589 }
590
591 static void zram_slot_free_notify(struct block_device *bdev,
592                                 unsigned long index)
593 {
594         struct zram *zram;
595
596         zram = bdev->bd_disk->private_data;
597         zram_free_page(zram, index);
598         zram_stat64_inc(zram, &zram->stats.notify_free);
599 }
600
601 static const struct block_device_operations zram_devops = {
602         .swap_slot_free_notify = zram_slot_free_notify,
603         .owner = THIS_MODULE
604 };
605
606 static int create_device(struct zram *zram, int device_id)
607 {
608         int ret = 0;
609
610         init_rwsem(&zram->lock);
611         init_rwsem(&zram->init_lock);
612         spin_lock_init(&zram->stat64_lock);
613
614         zram->queue = blk_alloc_queue(GFP_KERNEL);
615         if (!zram->queue) {
616                 pr_err("Error allocating disk queue for device %d\n",
617                         device_id);
618                 ret = -ENOMEM;
619                 goto out;
620         }
621
622         blk_queue_make_request(zram->queue, zram_make_request);
623         zram->queue->queuedata = zram;
624
625          /* gendisk structure */
626         zram->disk = alloc_disk(1);
627         if (!zram->disk) {
628                 blk_cleanup_queue(zram->queue);
629                 pr_warn("Error allocating disk structure for device %d\n",
630                         device_id);
631                 ret = -ENOMEM;
632                 goto out;
633         }
634
635         zram->disk->major = zram_major;
636         zram->disk->first_minor = device_id;
637         zram->disk->fops = &zram_devops;
638         zram->disk->queue = zram->queue;
639         zram->disk->private_data = zram;
640         snprintf(zram->disk->disk_name, 16, "zram%d", device_id);
641
642         /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */
643         set_capacity(zram->disk, 0);
644
645         /*
646          * To ensure that we always get PAGE_SIZE aligned
647          * and n*PAGE_SIZED sized I/O requests.
648          */
649         blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE);
650         blk_queue_logical_block_size(zram->disk->queue,
651                                         ZRAM_LOGICAL_BLOCK_SIZE);
652         blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
653         blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
654
655         add_disk(zram->disk);
656
657         ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj,
658                                 &zram_disk_attr_group);
659         if (ret < 0) {
660                 pr_warn("Error creating sysfs group");
661                 goto out;
662         }
663
664         zram->init_done = 0;
665
666 out:
667         return ret;
668 }
669
670 static void destroy_device(struct zram *zram)
671 {
672         sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
673                         &zram_disk_attr_group);
674
675         if (zram->disk) {
676                 del_gendisk(zram->disk);
677                 put_disk(zram->disk);
678         }
679
680         if (zram->queue)
681                 blk_cleanup_queue(zram->queue);
682 }
683
684 unsigned int zram_get_num_devices(void)
685 {
686         return num_devices;
687 }
688
689 static int __init zram_init(void)
690 {
691         int ret, dev_id;
692
693         if (num_devices > max_num_devices) {
694                 pr_warn("Invalid value for num_devices: %u\n",
695                                 num_devices);
696                 ret = -EINVAL;
697                 goto out;
698         }
699
700         zram_major = register_blkdev(0, "zram");
701         if (zram_major <= 0) {
702                 pr_warn("Unable to get major number\n");
703                 ret = -EBUSY;
704                 goto out;
705         }
706
707         /* Allocate the device array and initialize each one */
708         zram_devices = kzalloc(num_devices * sizeof(struct zram), GFP_KERNEL);
709         if (!zram_devices) {
710                 ret = -ENOMEM;
711                 goto unregister;
712         }
713
714         for (dev_id = 0; dev_id < num_devices; dev_id++) {
715                 ret = create_device(&zram_devices[dev_id], dev_id);
716                 if (ret)
717                         goto free_devices;
718         }
719
720         pr_info("Created %u device(s) ...\n", num_devices);
721
722         return 0;
723
724 free_devices:
725         while (dev_id)
726                 destroy_device(&zram_devices[--dev_id]);
727         kfree(zram_devices);
728 unregister:
729         unregister_blkdev(zram_major, "zram");
730 out:
731         return ret;
732 }
733
734 static void __exit zram_exit(void)
735 {
736         int i;
737         struct zram *zram;
738
739         for (i = 0; i < num_devices; i++) {
740                 zram = &zram_devices[i];
741
742                 destroy_device(zram);
743                 if (zram->init_done)
744                         zram_reset_device(zram);
745         }
746
747         unregister_blkdev(zram_major, "zram");
748
749         kfree(zram_devices);
750         pr_debug("Cleanup done!\n");
751 }
752
753 module_param(num_devices, uint, 0);
754 MODULE_PARM_DESC(num_devices, "Number of zram devices");
755
756 module_init(zram_init);
757 module_exit(zram_exit);
758
759 MODULE_LICENSE("Dual BSD/GPL");
760 MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
761 MODULE_DESCRIPTION("Compressed RAM Block Device");