xref: /openbmc/linux/include/linux/zpool.h (revision cbabf03c)
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 struct zpool_ops {
18 	int (*evict)(struct zpool *pool, unsigned long handle);
19 };
20 
21 /*
22  * Control how a handle is mapped.  It will be ignored if the
23  * implementation does not support it.  Its use is optional.
24  * Note that this does not refer to memory protection, it
25  * refers to how the memory will be copied in/out if copying
26  * is necessary during mapping; read-write is the safest as
27  * it copies the existing memory in on map, and copies the
28  * changed memory back out on unmap.  Write-only does not copy
29  * in the memory and should only be used for initialization.
30  * If in doubt, use ZPOOL_MM_DEFAULT which is read-write.
31  */
32 enum zpool_mapmode {
33 	ZPOOL_MM_RW, /* normal read-write mapping */
34 	ZPOOL_MM_RO, /* read-only (no copy-out at unmap time) */
35 	ZPOOL_MM_WO, /* write-only (no copy-in at map time) */
36 
37 	ZPOOL_MM_DEFAULT = ZPOOL_MM_RW
38 };
39 
40 bool zpool_has_pool(char *type);
41 
42 struct zpool *zpool_create_pool(const char *type, const char *name,
43 			gfp_t gfp, const struct zpool_ops *ops);
44 
45 const char *zpool_get_type(struct zpool *pool);
46 
47 void zpool_destroy_pool(struct zpool *pool);
48 
49 bool zpool_malloc_support_movable(struct zpool *pool);
50 
51 int zpool_malloc(struct zpool *pool, size_t size, gfp_t gfp,
52 			unsigned long *handle);
53 
54 void zpool_free(struct zpool *pool, unsigned long handle);
55 
56 int zpool_shrink(struct zpool *pool, unsigned int pages,
57 			unsigned int *reclaimed);
58 
59 void *zpool_map_handle(struct zpool *pool, unsigned long handle,
60 			enum zpool_mapmode mm);
61 
62 void zpool_unmap_handle(struct zpool *pool, unsigned long handle);
63 
64 u64 zpool_get_total_size(struct zpool *pool);
65 
66 
67 /**
68  * struct zpool_driver - driver implementation for zpool
69  * @type:	name of the driver.
70  * @list:	entry in the list of zpool drivers.
71  * @create:	create a new pool.
72  * @destroy:	destroy a pool.
73  * @malloc:	allocate mem from a pool.
74  * @free:	free mem from a pool.
75  * @shrink:	shrink the 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,
91 			gfp_t gfp,
92 			const struct zpool_ops *ops,
93 			struct zpool *zpool);
94 	void (*destroy)(void *pool);
95 
96 	bool malloc_support_movable;
97 	int (*malloc)(void *pool, size_t size, gfp_t gfp,
98 				unsigned long *handle);
99 	void (*free)(void *pool, unsigned long handle);
100 
101 	int (*shrink)(void *pool, unsigned int pages,
102 				unsigned int *reclaimed);
103 
104 	bool sleep_mapped;
105 	void *(*map)(void *pool, unsigned long handle,
106 				enum zpool_mapmode mm);
107 	void (*unmap)(void *pool, unsigned long handle);
108 
109 	u64 (*total_size)(void *pool);
110 };
111 
112 void zpool_register_driver(struct zpool_driver *driver);
113 
114 int zpool_unregister_driver(struct zpool_driver *driver);
115 
116 bool zpool_evictable(struct zpool *pool);
117 bool zpool_can_sleep_mapped(struct zpool *pool);
118 
119 #endif
120