zpool.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * zpool memory storage api
  3. *
  4. * Copyright (C) 2014 Dan Streetman
  5. *
  6. * This is a common frontend for memory storage pool implementations.
  7. * Typically, this is used to store compressed memory.
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/list.h>
  11. #include <linux/types.h>
  12. #include <linux/mm.h>
  13. #include <linux/slab.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/module.h>
  16. #include <linux/zpool.h>
  17. struct zpool {
  18. char *type;
  19. struct zpool_driver *driver;
  20. void *pool;
  21. struct zpool_ops *ops;
  22. struct list_head list;
  23. };
  24. static LIST_HEAD(drivers_head);
  25. static DEFINE_SPINLOCK(drivers_lock);
  26. static LIST_HEAD(pools_head);
  27. static DEFINE_SPINLOCK(pools_lock);
  28. /**
  29. * zpool_register_driver() - register a zpool implementation.
  30. * @driver: driver to register
  31. */
  32. void zpool_register_driver(struct zpool_driver *driver)
  33. {
  34. spin_lock(&drivers_lock);
  35. atomic_set(&driver->refcount, 0);
  36. list_add(&driver->list, &drivers_head);
  37. spin_unlock(&drivers_lock);
  38. }
  39. EXPORT_SYMBOL(zpool_register_driver);
  40. /**
  41. * zpool_unregister_driver() - unregister a zpool implementation.
  42. * @driver: driver to unregister.
  43. *
  44. * Module usage counting is used to prevent using a driver
  45. * while/after unloading, so if this is called from module
  46. * exit function, this should never fail; if called from
  47. * other than the module exit function, and this returns
  48. * failure, the driver is in use and must remain available.
  49. */
  50. int zpool_unregister_driver(struct zpool_driver *driver)
  51. {
  52. int ret = 0, refcount;
  53. spin_lock(&drivers_lock);
  54. refcount = atomic_read(&driver->refcount);
  55. WARN_ON(refcount < 0);
  56. if (refcount > 0)
  57. ret = -EBUSY;
  58. else
  59. list_del(&driver->list);
  60. spin_unlock(&drivers_lock);
  61. return ret;
  62. }
  63. EXPORT_SYMBOL(zpool_unregister_driver);
  64. /**
  65. * zpool_evict() - evict callback from a zpool implementation.
  66. * @pool: pool to evict from.
  67. * @handle: handle to evict.
  68. *
  69. * This can be used by zpool implementations to call the
  70. * user's evict zpool_ops struct evict callback.
  71. */
  72. int zpool_evict(void *pool, unsigned long handle)
  73. {
  74. struct zpool *zpool;
  75. spin_lock(&pools_lock);
  76. list_for_each_entry(zpool, &pools_head, list) {
  77. if (zpool->pool == pool) {
  78. spin_unlock(&pools_lock);
  79. if (!zpool->ops || !zpool->ops->evict)
  80. return -EINVAL;
  81. return zpool->ops->evict(zpool, handle);
  82. }
  83. }
  84. spin_unlock(&pools_lock);
  85. return -ENOENT;
  86. }
  87. EXPORT_SYMBOL(zpool_evict);
  88. static struct zpool_driver *zpool_get_driver(char *type)
  89. {
  90. struct zpool_driver *driver;
  91. spin_lock(&drivers_lock);
  92. list_for_each_entry(driver, &drivers_head, list) {
  93. if (!strcmp(driver->type, type)) {
  94. bool got = try_module_get(driver->owner);
  95. if (got)
  96. atomic_inc(&driver->refcount);
  97. spin_unlock(&drivers_lock);
  98. return got ? driver : NULL;
  99. }
  100. }
  101. spin_unlock(&drivers_lock);
  102. return NULL;
  103. }
  104. static void zpool_put_driver(struct zpool_driver *driver)
  105. {
  106. atomic_dec(&driver->refcount);
  107. module_put(driver->owner);
  108. }
  109. /**
  110. * zpool_create_pool() - Create a new zpool
  111. * @type The type of the zpool to create (e.g. zbud, zsmalloc)
  112. * @name The name of the zpool (e.g. zram0, zswap)
  113. * @gfp The GFP flags to use when allocating the pool.
  114. * @ops The optional ops callback.
  115. *
  116. * This creates a new zpool of the specified type. The gfp flags will be
  117. * used when allocating memory, if the implementation supports it. If the
  118. * ops param is NULL, then the created zpool will not be shrinkable.
  119. *
  120. * Implementations must guarantee this to be thread-safe.
  121. *
  122. * Returns: New zpool on success, NULL on failure.
  123. */
  124. struct zpool *zpool_create_pool(char *type, char *name, gfp_t gfp,
  125. struct zpool_ops *ops)
  126. {
  127. struct zpool_driver *driver;
  128. struct zpool *zpool;
  129. pr_info("creating pool type %s\n", type);
  130. driver = zpool_get_driver(type);
  131. if (!driver) {
  132. request_module("zpool-%s", type);
  133. driver = zpool_get_driver(type);
  134. }
  135. if (!driver) {
  136. pr_err("no driver for type %s\n", type);
  137. return NULL;
  138. }
  139. zpool = kmalloc(sizeof(*zpool), gfp);
  140. if (!zpool) {
  141. pr_err("couldn't create zpool - out of memory\n");
  142. zpool_put_driver(driver);
  143. return NULL;
  144. }
  145. zpool->type = driver->type;
  146. zpool->driver = driver;
  147. zpool->pool = driver->create(name, gfp, ops);
  148. zpool->ops = ops;
  149. if (!zpool->pool) {
  150. pr_err("couldn't create %s pool\n", type);
  151. zpool_put_driver(driver);
  152. kfree(zpool);
  153. return NULL;
  154. }
  155. pr_info("created %s pool\n", type);
  156. spin_lock(&pools_lock);
  157. list_add(&zpool->list, &pools_head);
  158. spin_unlock(&pools_lock);
  159. return zpool;
  160. }
  161. /**
  162. * zpool_destroy_pool() - Destroy a zpool
  163. * @pool The zpool to destroy.
  164. *
  165. * Implementations must guarantee this to be thread-safe,
  166. * however only when destroying different pools. The same
  167. * pool should only be destroyed once, and should not be used
  168. * after it is destroyed.
  169. *
  170. * This destroys an existing zpool. The zpool should not be in use.
  171. */
  172. void zpool_destroy_pool(struct zpool *zpool)
  173. {
  174. pr_info("destroying pool type %s\n", zpool->type);
  175. spin_lock(&pools_lock);
  176. list_del(&zpool->list);
  177. spin_unlock(&pools_lock);
  178. zpool->driver->destroy(zpool->pool);
  179. zpool_put_driver(zpool->driver);
  180. kfree(zpool);
  181. }
  182. /**
  183. * zpool_get_type() - Get the type of the zpool
  184. * @pool The zpool to check
  185. *
  186. * This returns the type of the pool.
  187. *
  188. * Implementations must guarantee this to be thread-safe.
  189. *
  190. * Returns: The type of zpool.
  191. */
  192. char *zpool_get_type(struct zpool *zpool)
  193. {
  194. return zpool->type;
  195. }
  196. /**
  197. * zpool_malloc() - Allocate memory
  198. * @pool The zpool to allocate from.
  199. * @size The amount of memory to allocate.
  200. * @gfp The GFP flags to use when allocating memory.
  201. * @handle Pointer to the handle to set
  202. *
  203. * This allocates the requested amount of memory from the pool.
  204. * The gfp flags will be used when allocating memory, if the
  205. * implementation supports it. The provided @handle will be
  206. * set to the allocated object handle.
  207. *
  208. * Implementations must guarantee this to be thread-safe.
  209. *
  210. * Returns: 0 on success, negative value on error.
  211. */
  212. int zpool_malloc(struct zpool *zpool, size_t size, gfp_t gfp,
  213. unsigned long *handle)
  214. {
  215. return zpool->driver->malloc(zpool->pool, size, gfp, handle);
  216. }
  217. /**
  218. * zpool_free() - Free previously allocated memory
  219. * @pool The zpool that allocated the memory.
  220. * @handle The handle to the memory to free.
  221. *
  222. * This frees previously allocated memory. This does not guarantee
  223. * that the pool will actually free memory, only that the memory
  224. * in the pool will become available for use by the pool.
  225. *
  226. * Implementations must guarantee this to be thread-safe,
  227. * however only when freeing different handles. The same
  228. * handle should only be freed once, and should not be used
  229. * after freeing.
  230. */
  231. void zpool_free(struct zpool *zpool, unsigned long handle)
  232. {
  233. zpool->driver->free(zpool->pool, handle);
  234. }
  235. /**
  236. * zpool_shrink() - Shrink the pool size
  237. * @pool The zpool to shrink.
  238. * @pages The number of pages to shrink the pool.
  239. * @reclaimed The number of pages successfully evicted.
  240. *
  241. * This attempts to shrink the actual memory size of the pool
  242. * by evicting currently used handle(s). If the pool was
  243. * created with no zpool_ops, or the evict call fails for any
  244. * of the handles, this will fail. If non-NULL, the @reclaimed
  245. * parameter will be set to the number of pages reclaimed,
  246. * which may be more than the number of pages requested.
  247. *
  248. * Implementations must guarantee this to be thread-safe.
  249. *
  250. * Returns: 0 on success, negative value on error/failure.
  251. */
  252. int zpool_shrink(struct zpool *zpool, unsigned int pages,
  253. unsigned int *reclaimed)
  254. {
  255. return zpool->driver->shrink(zpool->pool, pages, reclaimed);
  256. }
  257. /**
  258. * zpool_map_handle() - Map a previously allocated handle into memory
  259. * @pool The zpool that the handle was allocated from
  260. * @handle The handle to map
  261. * @mm How the memory should be mapped
  262. *
  263. * This maps a previously allocated handle into memory. The @mm
  264. * param indicates to the implementation how the memory will be
  265. * used, i.e. read-only, write-only, read-write. If the
  266. * implementation does not support it, the memory will be treated
  267. * as read-write.
  268. *
  269. * This may hold locks, disable interrupts, and/or preemption,
  270. * and the zpool_unmap_handle() must be called to undo those
  271. * actions. The code that uses the mapped handle should complete
  272. * its operatons on the mapped handle memory quickly and unmap
  273. * as soon as possible. As the implementation may use per-cpu
  274. * data, multiple handles should not be mapped concurrently on
  275. * any cpu.
  276. *
  277. * Returns: A pointer to the handle's mapped memory area.
  278. */
  279. void *zpool_map_handle(struct zpool *zpool, unsigned long handle,
  280. enum zpool_mapmode mapmode)
  281. {
  282. return zpool->driver->map(zpool->pool, handle, mapmode);
  283. }
  284. /**
  285. * zpool_unmap_handle() - Unmap a previously mapped handle
  286. * @pool The zpool that the handle was allocated from
  287. * @handle The handle to unmap
  288. *
  289. * This unmaps a previously mapped handle. Any locks or other
  290. * actions that the implementation took in zpool_map_handle()
  291. * will be undone here. The memory area returned from
  292. * zpool_map_handle() should no longer be used after this.
  293. */
  294. void zpool_unmap_handle(struct zpool *zpool, unsigned long handle)
  295. {
  296. zpool->driver->unmap(zpool->pool, handle);
  297. }
  298. /**
  299. * zpool_get_total_size() - The total size of the pool
  300. * @pool The zpool to check
  301. *
  302. * This returns the total size in bytes of the pool.
  303. *
  304. * Returns: Total size of the zpool in bytes.
  305. */
  306. u64 zpool_get_total_size(struct zpool *zpool)
  307. {
  308. return zpool->driver->total_size(zpool->pool);
  309. }
  310. static int __init init_zpool(void)
  311. {
  312. pr_info("loaded\n");
  313. return 0;
  314. }
  315. static void __exit exit_zpool(void)
  316. {
  317. pr_info("unloaded\n");
  318. }
  319. module_init(init_zpool);
  320. module_exit(exit_zpool);
  321. MODULE_LICENSE("GPL");
  322. MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");
  323. MODULE_DESCRIPTION("Common API for compressed memory storage");