zram_drv.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Compressed RAM block device
  3. *
  4. * Copyright (C) 2008, 2009, 2010 Nitin Gupta
  5. * 2012, 2013 Minchan Kim
  6. *
  7. * This code is released using a dual license strategy: BSD/GPL
  8. * You can choose the licence that better fits your requirements.
  9. *
  10. * Released under the terms of 3-clause BSD License
  11. * Released under the terms of GNU General Public License Version 2.0
  12. *
  13. */
  14. #ifndef _ZRAM_DRV_H_
  15. #define _ZRAM_DRV_H_
  16. #include <linux/rwsem.h>
  17. #include <linux/zsmalloc.h>
  18. #include <linux/crypto.h>
  19. #include "zcomp.h"
  20. /*-- Configurable parameters */
  21. /*
  22. * Pages that compress to size greater than this are stored
  23. * uncompressed in memory.
  24. */
  25. static const size_t max_zpage_size = PAGE_SIZE / 4 * 3;
  26. /*
  27. * NOTE: max_zpage_size must be less than or equal to:
  28. * ZS_MAX_ALLOC_SIZE. Otherwise, zs_malloc() would
  29. * always return failure.
  30. */
  31. /*-- End of configurable params */
  32. #define SECTOR_SHIFT 9
  33. #define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
  34. #define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT)
  35. #define ZRAM_LOGICAL_BLOCK_SHIFT 12
  36. #define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT)
  37. #define ZRAM_SECTOR_PER_LOGICAL_BLOCK \
  38. (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT))
  39. /*
  40. * The lower ZRAM_FLAG_SHIFT bits of table.value is for
  41. * object size (excluding header), the higher bits is for
  42. * zram_pageflags.
  43. *
  44. * zram is mainly used for memory efficiency so we want to keep memory
  45. * footprint small so we can squeeze size and flags into a field.
  46. * The lower ZRAM_FLAG_SHIFT bits is for object size (excluding header),
  47. * the higher bits is for zram_pageflags.
  48. */
  49. #define ZRAM_FLAG_SHIFT 24
  50. /* Flags for zram pages (table[page_no].value) */
  51. enum zram_pageflags {
  52. /* Page consists entirely of zeros */
  53. ZRAM_ZERO = ZRAM_FLAG_SHIFT,
  54. ZRAM_ACCESS, /* page is now accessed */
  55. __NR_ZRAM_PAGEFLAGS,
  56. };
  57. /*-- Data structures */
  58. /* Allocated for each disk page */
  59. struct zram_table_entry {
  60. unsigned long handle;
  61. unsigned long value;
  62. };
  63. struct zram_stats {
  64. atomic64_t compr_data_size; /* compressed size of pages stored */
  65. atomic64_t num_reads; /* failed + successful */
  66. atomic64_t num_writes; /* --do-- */
  67. atomic64_t failed_reads; /* can happen when memory is too low */
  68. atomic64_t failed_writes; /* can happen when memory is too low */
  69. atomic64_t invalid_io; /* non-page-aligned I/O requests */
  70. atomic64_t notify_free; /* no. of swap slot free notifications */
  71. atomic64_t zero_pages; /* no. of zero filled pages */
  72. atomic64_t pages_stored; /* no. of pages currently stored */
  73. atomic_long_t max_used_pages; /* no. of maximum pages stored */
  74. atomic64_t writestall; /* no. of write slow paths */
  75. };
  76. struct zram_meta {
  77. struct zram_table_entry *table;
  78. struct zs_pool *mem_pool;
  79. };
  80. struct zram {
  81. struct zram_meta *meta;
  82. struct zcomp *comp;
  83. struct gendisk *disk;
  84. /* Prevent concurrent execution of device init */
  85. struct rw_semaphore init_lock;
  86. /*
  87. * the number of pages zram can consume for storing compressed data
  88. */
  89. unsigned long limit_pages;
  90. struct zram_stats stats;
  91. atomic_t refcount; /* refcount for zram_meta */
  92. /* wait all IO under all of cpu are done */
  93. wait_queue_head_t io_done;
  94. /*
  95. * This is the limit on amount of *uncompressed* worth of data
  96. * we can store in a disk.
  97. */
  98. u64 disksize; /* bytes */
  99. char compressor[CRYPTO_MAX_ALG_NAME];
  100. /*
  101. * zram is claimed so open request will be failed
  102. */
  103. bool claim; /* Protected by bdev->bd_mutex */
  104. };
  105. #endif