1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * zpool memory storage api 4 * 5 * Copyright (C) 2014 Dan Streetman 6 * 7 * This is a common frontend for the zswap compressed memory storage 8 * implementations. 9 */ 10 11 #ifndef _ZPOOL_H_ 12 #define _ZPOOL_H_ 13 14 struct zpool; 15 16 /* 17 * Control how a handle is mapped. It will be ignored if the 18 * implementation does not support it. Its use is optional. 19 * Note that this does not refer to memory protection, it 20 * refers to how the memory will be copied in/out if copying 21 * is necessary during mapping; read-write is the safest as 22 * it copies the existing memory in on map, and copies the 23 * changed memory back out on unmap. Write-only does not copy 24 * in the memory and should only be used for initialization. 25 * If in doubt, use ZPOOL_MM_DEFAULT which is read-write. 26 */ 27 enum zpool_mapmode { 28 ZPOOL_MM_RW, /* normal read-write mapping */ 29 ZPOOL_MM_RO, /* read-only (no copy-out at unmap time) */ 30 ZPOOL_MM_WO, /* write-only (no copy-in at map time) */ 31 32 ZPOOL_MM_DEFAULT = ZPOOL_MM_RW 33 }; 34 35 bool zpool_has_pool(char *type); 36 37 struct zpool *zpool_create_pool(const char *type, const char *name, gfp_t gfp); 38 39 const char *zpool_get_type(struct zpool *pool); 40 41 void zpool_destroy_pool(struct zpool *pool); 42 43 bool zpool_malloc_support_movable(struct zpool *pool); 44 45 int zpool_malloc(struct zpool *pool, size_t size, gfp_t gfp, 46 unsigned long *handle); 47 48 void zpool_free(struct zpool *pool, unsigned long handle); 49 50 void *zpool_map_handle(struct zpool *pool, unsigned long handle, 51 enum zpool_mapmode mm); 52 53 void zpool_unmap_handle(struct zpool *pool, unsigned long handle); 54 55 u64 zpool_get_total_pages(struct zpool *pool); 56 57 58 /** 59 * struct zpool_driver - driver implementation for zpool 60 * @type: name of the driver. 61 * @list: entry in the list of zpool drivers. 62 * @create: create a new pool. 63 * @destroy: destroy a pool. 64 * @malloc: allocate mem from a pool. 65 * @free: free mem from a pool. 66 * @sleep_mapped: whether zpool driver can sleep during map. 67 * @map: map a handle. 68 * @unmap: unmap a handle. 69 * @total_size: get total size of a pool. 70 * 71 * This is created by a zpool implementation and registered 72 * with zpool. 73 */ 74 struct zpool_driver { 75 char *type; 76 struct module *owner; 77 atomic_t refcount; 78 struct list_head list; 79 80 void *(*create)(const char *name, gfp_t gfp); 81 void (*destroy)(void *pool); 82 83 bool malloc_support_movable; 84 int (*malloc)(void *pool, size_t size, gfp_t gfp, 85 unsigned long *handle); 86 void (*free)(void *pool, unsigned long handle); 87 88 bool sleep_mapped; 89 void *(*map)(void *pool, unsigned long handle, 90 enum zpool_mapmode mm); 91 void (*unmap)(void *pool, unsigned long handle); 92 93 u64 (*total_pages)(void *pool); 94 }; 95 96 void zpool_register_driver(struct zpool_driver *driver); 97 98 int zpool_unregister_driver(struct zpool_driver *driver); 99 100 bool zpool_can_sleep_mapped(struct zpool *pool); 101 102 #endif 103