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