blob: 51bf43076165d76aa0f313a65d526e912a15963e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Dan Streetmanaf8d4172014-08-06 16:08:36 -07002/*
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
15struct zpool;
16
17struct 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 */
32enum 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
Dan Streetman3f0e1312015-09-09 15:35:16 -070040bool zpool_has_pool(char *type);
41
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -080042struct zpool *zpool_create_pool(const char *type, const char *name,
Krzysztof Kozlowski78672772015-09-08 15:05:03 -070043 gfp_t gfp, const struct zpool_ops *ops);
Dan Streetmanaf8d4172014-08-06 16:08:36 -070044
Dan Streetman69e18f42015-11-06 16:29:18 -080045const char *zpool_get_type(struct zpool *pool);
Dan Streetmanaf8d4172014-08-06 16:08:36 -070046
47void zpool_destroy_pool(struct zpool *pool);
48
Hui Zhuc165f252019-09-23 15:39:37 -070049bool zpool_malloc_support_movable(struct zpool *pool);
50
Dan Streetmanaf8d4172014-08-06 16:08:36 -070051int zpool_malloc(struct zpool *pool, size_t size, gfp_t gfp,
52 unsigned long *handle);
53
54void zpool_free(struct zpool *pool, unsigned long handle);
55
56int zpool_shrink(struct zpool *pool, unsigned int pages,
57 unsigned int *reclaimed);
58
59void *zpool_map_handle(struct zpool *pool, unsigned long handle,
60 enum zpool_mapmode mm);
61
62void zpool_unmap_handle(struct zpool *pool, unsigned long handle);
63
64u64 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 * @map: map a handle.
77 * @unmap: unmap a handle.
78 * @total_size: get total size of a pool.
79 *
80 * This is created by a zpool implementation and registered
81 * with zpool.
82 */
83struct zpool_driver {
84 char *type;
85 struct module *owner;
86 atomic_t refcount;
87 struct list_head list;
88
Sergey SENOZHATSKY6f3526d2015-11-06 16:29:21 -080089 void *(*create)(const char *name,
90 gfp_t gfp,
91 const struct zpool_ops *ops,
Dan Streetman479305f2015-06-25 15:00:40 -070092 struct zpool *zpool);
Dan Streetmanaf8d4172014-08-06 16:08:36 -070093 void (*destroy)(void *pool);
94
Hui Zhuc165f252019-09-23 15:39:37 -070095 bool malloc_support_movable;
Dan Streetmanaf8d4172014-08-06 16:08:36 -070096 int (*malloc)(void *pool, size_t size, gfp_t gfp,
97 unsigned long *handle);
98 void (*free)(void *pool, unsigned long handle);
99
100 int (*shrink)(void *pool, unsigned int pages,
101 unsigned int *reclaimed);
102
103 void *(*map)(void *pool, unsigned long handle,
104 enum zpool_mapmode mm);
105 void (*unmap)(void *pool, unsigned long handle);
106
107 u64 (*total_size)(void *pool);
108};
109
110void zpool_register_driver(struct zpool_driver *driver);
111
112int zpool_unregister_driver(struct zpool_driver *driver);
113
Yu Zhao9c3760e2018-01-31 16:19:59 -0800114bool zpool_evictable(struct zpool *pool);
115
Dan Streetmanaf8d4172014-08-06 16:08:36 -0700116#endif