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 56 void *zpool_obj_read_begin(struct zpool *zpool, unsigned long handle, 57 void *local_copy); 58 59 void zpool_obj_read_end(struct zpool *zpool, unsigned long handle, 60 void *handle_mem); 61 62 void zpool_obj_write(struct zpool *zpool, unsigned long handle, 63 void *handle_mem, size_t mem_len); 64 65 u64 zpool_get_total_pages(struct zpool *pool); 66 67 68 /** 69 * struct zpool_driver - driver implementation for zpool 70 * @type: name of the driver. 71 * @list: entry in the list of zpool drivers. 72 * @create: create a new pool. 73 * @destroy: destroy a pool. 74 * @malloc: allocate mem from a pool. 75 * @free: free mem from a pool. 76 * @sleep_mapped: whether zpool driver can sleep during map. 77 * @map: map a handle. 78 * @unmap: unmap a handle. 79 * @total_size: get total size of a pool. 80 * 81 * This is created by a zpool implementation and registered 82 * with zpool. 83 */ 84 struct zpool_driver { 85 char *type; 86 struct module *owner; 87 atomic_t refcount; 88 struct list_head list; 89 90 void *(*create)(const char *name, gfp_t gfp); 91 void (*destroy)(void *pool); 92 93 bool malloc_support_movable; 94 int (*malloc)(void *pool, size_t size, gfp_t gfp, 95 unsigned long *handle); 96 void (*free)(void *pool, unsigned long handle); 97 98 bool sleep_mapped; 99 void *(*map)(void *pool, unsigned long handle, 100 enum zpool_mapmode mm); 101 void (*unmap)(void *pool, unsigned long handle); 102 103 void *(*obj_read_begin)(void *pool, unsigned long handle, 104 void *local_copy); 105 void (*obj_read_end)(void *pool, unsigned long handle, 106 void *handle_mem); 107 void (*obj_write)(void *pool, unsigned long handle, 108 void *handle_mem, size_t mem_len); 109 110 u64 (*total_pages)(void *pool); 111 }; 112 113 void zpool_register_driver(struct zpool_driver *driver); 114 115 int zpool_unregister_driver(struct zpool_driver *driver); 116 117 bool zpool_can_sleep_mapped(struct zpool *pool); 118 119 #endif 120