zpool.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. * @gfp The GFP flags to use when allocating the pool.
  113. * @ops The optional ops callback.
  114. *
  115. * This creates a new zpool of the specified type. The gfp flags will be
  116. * used when allocating memory, if the implementation supports it. If the
  117. * ops param is NULL, then the created zpool will not be shrinkable.
  118. *
  119. * Implementations must guarantee this to be thread-safe.
  120. *
  121. * Returns: New zpool on success, NULL on failure.
  122. */
  123. struct zpool *zpool_create_pool(char *type, gfp_t gfp, struct zpool_ops *ops)
  124. {
  125. struct zpool_driver *driver;
  126. struct zpool *zpool;
  127. pr_info("creating pool type %s\n", type);
  128. driver = zpool_get_driver(type);
  129. if (!driver) {
  130. request_module("zpool-%s", type);
  131. driver = zpool_get_driver(type);
  132. }
  133. if (!driver) {
  134. pr_err("no driver for type %s\n", type);
  135. return NULL;
  136. }
  137. zpool = kmalloc(sizeof(*zpool), gfp);
  138. if (!zpool) {
  139. pr_err("couldn't create zpool - out of memory\n");
  140. zpool_put_driver(driver);
  141. return NULL;
  142. }
  143. zpool->type = driver->type;
  144. zpool->driver = driver;
  145. zpool->pool = driver->create(gfp, ops);
  146. zpool->ops = ops;
  147. if (!zpool->pool) {
  148. pr_err("couldn't create %s pool\n", type);
  149. zpool_put_driver(driver);
  150. kfree(zpool);
  151. return NULL;
  152. }
  153. pr_info("created %s pool\n", type);
  154. spin_lock(&pools_lock);
  155. list_add(&zpool->list, &pools_head);
  156. spin_unlock(&pools_lock);
  157. return zpool;
  158. }
  159. /**
  160. * zpool_destroy_pool() - Destroy a zpool
  161. * @pool The zpool to destroy.
  162. *
  163. * Implementations must guarantee this to be thread-safe,
  164. * however only when destroying different pools. The same
  165. * pool should only be destroyed once, and should not be used
  166. * after it is destroyed.
  167. *
  168. * This destroys an existing zpool. The zpool should not be in use.
  169. */
  170. void zpool_destroy_pool(struct zpool *zpool)
  171. {
  172. pr_info("destroying pool type %s\n", zpool->type);
  173. spin_lock(&pools_lock);
  174. list_del(&zpool->list);
  175. spin_unlock(&pools_lock);
  176. zpool->driver->destroy(zpool->pool);
  177. zpool_put_driver(zpool->driver);
  178. kfree(zpool);
  179. }
  180. /**
  181. * zpool_get_type() - Get the type of the zpool
  182. * @pool The zpool to check
  183. *
  184. * This returns the type of the pool.
  185. *
  186. * Implementations must guarantee this to be thread-safe.
  187. *
  188. * Returns: The type of zpool.
  189. */
  190. char *zpool_get_type(struct zpool *zpool)
  191. {
  192. return zpool->type;
  193. }
  194. /**
  195. * zpool_malloc() - Allocate memory
  196. * @pool The zpool to allocate from.
  197. * @size The amount of memory to allocate.
  198. * @gfp The GFP flags to use when allocating memory.
  199. * @handle Pointer to the handle to set
  200. *
  201. * This allocates the requested amount of memory from the pool.
  202. * The gfp flags will be used when allocating memory, if the
  203. * implementation supports it. The provided @handle will be
  204. * set to the allocated object handle.
  205. *
  206. * Implementations must guarantee this to be thread-safe.
  207. *
  208. * Returns: 0 on success, negative value on error.
  209. */
  210. int zpool_malloc(struct zpool *zpool, size_t size, gfp_t gfp,
  211. unsigned long *handle)
  212. {
  213. return zpool->driver->malloc(zpool->pool, size, gfp, handle);
  214. }
  215. /**
  216. * zpool_free() - Free previously allocated memory
  217. * @pool The zpool that allocated the memory.
  218. * @handle The handle to the memory to free.
  219. *
  220. * This frees previously allocated memory. This does not guarantee
  221. * that the pool will actually free memory, only that the memory
  222. * in the pool will become available for use by the pool.
  223. *
  224. * Implementations must guarantee this to be thread-safe,
  225. * however only when freeing different handles. The same
  226. * handle should only be freed once, and should not be used
  227. * after freeing.
  228. */
  229. void zpool_free(struct zpool *zpool, unsigned long handle)
  230. {
  231. zpool->driver->free(zpool->pool, handle);
  232. }
  233. /**
  234. * zpool_shrink() - Shrink the pool size
  235. * @pool The zpool to shrink.
  236. * @pages The number of pages to shrink the pool.
  237. * @reclaimed The number of pages successfully evicted.
  238. *
  239. * This attempts to shrink the actual memory size of the pool
  240. * by evicting currently used handle(s). If the pool was
  241. * created with no zpool_ops, or the evict call fails for any
  242. * of the handles, this will fail. If non-NULL, the @reclaimed
  243. * parameter will be set to the number of pages reclaimed,
  244. * which may be more than the number of pages requested.
  245. *
  246. * Implementations must guarantee this to be thread-safe.
  247. *
  248. * Returns: 0 on success, negative value on error/failure.
  249. */
  250. int zpool_shrink(struct zpool *zpool, unsigned int pages,
  251. unsigned int *reclaimed)
  252. {
  253. return zpool->driver->shrink(zpool->pool, pages, reclaimed);
  254. }
  255. /**
  256. * zpool_map_handle() - Map a previously allocated handle into memory
  257. * @pool The zpool that the handle was allocated from
  258. * @handle The handle to map
  259. * @mm How the memory should be mapped
  260. *
  261. * This maps a previously allocated handle into memory. The @mm
  262. * param indicates to the implementation how the memory will be
  263. * used, i.e. read-only, write-only, read-write. If the
  264. * implementation does not support it, the memory will be treated
  265. * as read-write.
  266. *
  267. * This may hold locks, disable interrupts, and/or preemption,
  268. * and the zpool_unmap_handle() must be called to undo those
  269. * actions. The code that uses the mapped handle should complete
  270. * its operatons on the mapped handle memory quickly and unmap
  271. * as soon as possible. As the implementation may use per-cpu
  272. * data, multiple handles should not be mapped concurrently on
  273. * any cpu.
  274. *
  275. * Returns: A pointer to the handle's mapped memory area.
  276. */
  277. void *zpool_map_handle(struct zpool *zpool, unsigned long handle,
  278. enum zpool_mapmode mapmode)
  279. {
  280. return zpool->driver->map(zpool->pool, handle, mapmode);
  281. }
  282. /**
  283. * zpool_unmap_handle() - Unmap a previously mapped handle
  284. * @pool The zpool that the handle was allocated from
  285. * @handle The handle to unmap
  286. *
  287. * This unmaps a previously mapped handle. Any locks or other
  288. * actions that the implementation took in zpool_map_handle()
  289. * will be undone here. The memory area returned from
  290. * zpool_map_handle() should no longer be used after this.
  291. */
  292. void zpool_unmap_handle(struct zpool *zpool, unsigned long handle)
  293. {
  294. zpool->driver->unmap(zpool->pool, handle);
  295. }
  296. /**
  297. * zpool_get_total_size() - The total size of the pool
  298. * @pool The zpool to check
  299. *
  300. * This returns the total size in bytes of the pool.
  301. *
  302. * Returns: Total size of the zpool in bytes.
  303. */
  304. u64 zpool_get_total_size(struct zpool *zpool)
  305. {
  306. return zpool->driver->total_size(zpool->pool);
  307. }
  308. static int __init init_zpool(void)
  309. {
  310. pr_info("loaded\n");
  311. return 0;
  312. }
  313. static void __exit exit_zpool(void)
  314. {
  315. pr_info("unloaded\n");
  316. }
  317. module_init(init_zpool);
  318. module_exit(exit_zpool);
  319. MODULE_LICENSE("GPL");
  320. MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");
  321. MODULE_DESCRIPTION("Common API for compressed memory storage");