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