zram_drv.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
  21. #define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT)
  22. #define ZRAM_LOGICAL_BLOCK_SHIFT 12
  23. #define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT)
  24. #define ZRAM_SECTOR_PER_LOGICAL_BLOCK \
  25. (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT))
  26. /*
  27. * The lower ZRAM_FLAG_SHIFT bits of table.value is for
  28. * object size (excluding header), the higher bits is for
  29. * zram_pageflags.
  30. *
  31. * zram is mainly used for memory efficiency so we want to keep memory
  32. * footprint small so we can squeeze size and flags into a field.
  33. * The lower ZRAM_FLAG_SHIFT bits is for object size (excluding header),
  34. * the higher bits is for zram_pageflags.
  35. */
  36. #define ZRAM_FLAG_SHIFT 24
  37. /* Flags for zram pages (table[page_no].value) */
  38. enum zram_pageflags {
  39. /* zram slot is locked */
  40. ZRAM_LOCK = ZRAM_FLAG_SHIFT,
  41. ZRAM_SAME, /* Page consists the same element */
  42. ZRAM_WB, /* page is stored on backing_device */
  43. ZRAM_HUGE, /* Incompressible page */
  44. __NR_ZRAM_PAGEFLAGS,
  45. };
  46. /*-- Data structures */
  47. /* Allocated for each disk page */
  48. struct zram_table_entry {
  49. union {
  50. unsigned long handle;
  51. unsigned long element;
  52. };
  53. unsigned long value;
  54. #ifdef CONFIG_ZRAM_MEMORY_TRACKING
  55. ktime_t ac_time;
  56. #endif
  57. };
  58. struct zram_stats {
  59. atomic64_t compr_data_size; /* compressed size of pages stored */
  60. atomic64_t num_reads; /* failed + successful */
  61. atomic64_t num_writes; /* --do-- */
  62. atomic64_t failed_reads; /* can happen when memory is too low */
  63. atomic64_t failed_writes; /* can happen when memory is too low */
  64. atomic64_t invalid_io; /* non-page-aligned I/O requests */
  65. atomic64_t notify_free; /* no. of swap slot free notifications */
  66. atomic64_t same_pages; /* no. of same element filled pages */
  67. atomic64_t huge_pages; /* no. of huge pages */
  68. atomic64_t pages_stored; /* no. of pages currently stored */
  69. atomic_long_t max_used_pages; /* no. of maximum pages stored */
  70. atomic64_t writestall; /* no. of write slow paths */
  71. atomic64_t miss_free; /* no. of missed free */
  72. };
  73. struct zram {
  74. struct zram_table_entry *table;
  75. struct zs_pool *mem_pool;
  76. struct zcomp *comp;
  77. struct gendisk *disk;
  78. /* Prevent concurrent execution of device init */
  79. struct rw_semaphore init_lock;
  80. /*
  81. * the number of pages zram can consume for storing compressed data
  82. */
  83. unsigned long limit_pages;
  84. struct zram_stats stats;
  85. /*
  86. * This is the limit on amount of *uncompressed* worth of data
  87. * we can store in a disk.
  88. */
  89. u64 disksize; /* bytes */
  90. char compressor[CRYPTO_MAX_ALG_NAME];
  91. /*
  92. * zram is claimed so open request will be failed
  93. */
  94. bool claim; /* Protected by bdev->bd_mutex */
  95. #ifdef CONFIG_ZRAM_WRITEBACK
  96. struct file *backing_dev;
  97. struct block_device *bdev;
  98. unsigned int old_block_size;
  99. unsigned long *bitmap;
  100. unsigned long nr_pages;
  101. #endif
  102. #ifdef CONFIG_ZRAM_MEMORY_TRACKING
  103. struct dentry *debugfs_dir;
  104. #endif
  105. };
  106. #endif