1 // SPDX-License-Identifier: GPL-2.0-only 1 2 /* 3 * zpool memory storage api 4 * 5 * Copyright (C) 2014 Dan Streetman 6 * 7 * This is a common frontend for memory storag 8 * Typically, this is used to store compressed 9 */ 10 11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 12 13 #include <linux/list.h> 14 #include <linux/types.h> 15 #include <linux/mm.h> 16 #include <linux/slab.h> 17 #include <linux/spinlock.h> 18 #include <linux/module.h> 19 #include <linux/zpool.h> 20 21 struct zpool { 22 struct zpool_driver *driver; 23 void *pool; 24 }; 25 26 static LIST_HEAD(drivers_head); 27 static DEFINE_SPINLOCK(drivers_lock); 28 29 /** 30 * zpool_register_driver() - register a zpool 31 * @driver: driver to register 32 */ 33 void zpool_register_driver(struct zpool_driver 34 { 35 spin_lock(&drivers_lock); 36 atomic_set(&driver->refcount, 0); 37 list_add(&driver->list, &drivers_head) 38 spin_unlock(&drivers_lock); 39 } 40 EXPORT_SYMBOL(zpool_register_driver); 41 42 /** 43 * zpool_unregister_driver() - unregister a zp 44 * @driver: driver to unregister. 45 * 46 * Module usage counting is used to prevent us 47 * while/after unloading, so if this is called 48 * exit function, this should never fail; if c 49 * other than the module exit function, and th 50 * failure, the driver is in use and must rema 51 */ 52 int zpool_unregister_driver(struct zpool_drive 53 { 54 int ret = 0, refcount; 55 56 spin_lock(&drivers_lock); 57 refcount = atomic_read(&driver->refcou 58 WARN_ON(refcount < 0); 59 if (refcount > 0) 60 ret = -EBUSY; 61 else 62 list_del(&driver->list); 63 spin_unlock(&drivers_lock); 64 65 return ret; 66 } 67 EXPORT_SYMBOL(zpool_unregister_driver); 68 69 /* this assumes @type is null-terminated. */ 70 static struct zpool_driver *zpool_get_driver(c 71 { 72 struct zpool_driver *driver; 73 74 spin_lock(&drivers_lock); 75 list_for_each_entry(driver, &drivers_h 76 if (!strcmp(driver->type, type 77 bool got = try_module_ 78 79 if (got) 80 atomic_inc(&dr 81 spin_unlock(&drivers_l 82 return got ? driver : 83 } 84 } 85 86 spin_unlock(&drivers_lock); 87 return NULL; 88 } 89 90 static void zpool_put_driver(struct zpool_driv 91 { 92 atomic_dec(&driver->refcount); 93 module_put(driver->owner); 94 } 95 96 /** 97 * zpool_has_pool() - Check if the pool driver 98 * @type: The type of the zpool to check 99 * 100 * This checks if the @type pool driver is ava 101 * the requested module, if needed, but there 102 * still be loaded and available immediately a 103 * true, the caller should assume the pool is 104 * to handle the @zpool_create_pool() returnin 105 * returns false, the caller should assume the 106 * available; either the requested pool type m 107 * not be loaded, and calling @zpool_create_po 108 * fail. 109 * 110 * The @type string must be null-terminated. 111 * 112 * Returns: true if @type pool is available, f 113 */ 114 bool zpool_has_pool(char *type) 115 { 116 struct zpool_driver *driver = zpool_ge 117 118 if (!driver) { 119 request_module("zpool-%s", typ 120 driver = zpool_get_driver(type 121 } 122 123 if (!driver) 124 return false; 125 126 zpool_put_driver(driver); 127 return true; 128 } 129 EXPORT_SYMBOL(zpool_has_pool); 130 131 /** 132 * zpool_create_pool() - Create a new zpool 133 * @type: The type of the zpool to creat 134 * @name: The name of the zpool (e.g. zr 135 * @gfp: The GFP flags to use when allo 136 * 137 * This creates a new zpool of the specified t 138 * used when allocating memory, if the impleme 139 * ops param is NULL, then the created zpool w 140 * 141 * Implementations must guarantee this to be t 142 * 143 * The @type and @name strings must be null-te 144 * 145 * Returns: New zpool on success, NULL on fail 146 */ 147 struct zpool *zpool_create_pool(const char *ty 148 { 149 struct zpool_driver *driver; 150 struct zpool *zpool; 151 152 pr_debug("creating pool type %s\n", ty 153 154 driver = zpool_get_driver(type); 155 156 if (!driver) { 157 request_module("zpool-%s", typ 158 driver = zpool_get_driver(type 159 } 160 161 if (!driver) { 162 pr_err("no driver for type %s\ 163 return NULL; 164 } 165 166 zpool = kmalloc(sizeof(*zpool), gfp); 167 if (!zpool) { 168 pr_err("couldn't create zpool 169 zpool_put_driver(driver); 170 return NULL; 171 } 172 173 zpool->driver = driver; 174 zpool->pool = driver->create(name, gfp 175 176 if (!zpool->pool) { 177 pr_err("couldn't create %s poo 178 zpool_put_driver(driver); 179 kfree(zpool); 180 return NULL; 181 } 182 183 pr_debug("created pool type %s\n", typ 184 185 return zpool; 186 } 187 188 /** 189 * zpool_destroy_pool() - Destroy a zpool 190 * @zpool: The zpool to destroy. 191 * 192 * Implementations must guarantee this to be t 193 * however only when destroying different pool 194 * pool should only be destroyed once, and sho 195 * after it is destroyed. 196 * 197 * This destroys an existing zpool. The zpool 198 */ 199 void zpool_destroy_pool(struct zpool *zpool) 200 { 201 pr_debug("destroying pool type %s\n", 202 203 zpool->driver->destroy(zpool->pool); 204 zpool_put_driver(zpool->driver); 205 kfree(zpool); 206 } 207 208 /** 209 * zpool_get_type() - Get the type of the zpoo 210 * @zpool: The zpool to check 211 * 212 * This returns the type of the pool. 213 * 214 * Implementations must guarantee this to be t 215 * 216 * Returns: The type of zpool. 217 */ 218 const char *zpool_get_type(struct zpool *zpool 219 { 220 return zpool->driver->type; 221 } 222 223 /** 224 * zpool_malloc_support_movable() - Check if t 225 * allocating movable memory 226 * @zpool: The zpool to check 227 * 228 * This returns if the zpool supports allocati 229 * 230 * Implementations must guarantee this to be t 231 * 232 * Returns: true if the zpool supports allocat 233 */ 234 bool zpool_malloc_support_movable(struct zpool 235 { 236 return zpool->driver->malloc_support_m 237 } 238 239 /** 240 * zpool_malloc() - Allocate memory 241 * @zpool: The zpool to allocate from. 242 * @size: The amount of memory to alloca 243 * @gfp: The GFP flags to use when allo 244 * @handle: Pointer to the handle to set 245 * 246 * This allocates the requested amount of memo 247 * The gfp flags will be used when allocating 248 * implementation supports it. The provided @ 249 * set to the allocated object handle. 250 * 251 * Implementations must guarantee this to be t 252 * 253 * Returns: 0 on success, negative value on er 254 */ 255 int zpool_malloc(struct zpool *zpool, size_t s 256 unsigned long *handle) 257 { 258 return zpool->driver->malloc(zpool->po 259 } 260 261 /** 262 * zpool_free() - Free previously allocated me 263 * @zpool: The zpool that allocated the m 264 * @handle: The handle to the memory to fr 265 * 266 * This frees previously allocated memory. Th 267 * that the pool will actually free memory, on 268 * in the pool will become available for use b 269 * 270 * Implementations must guarantee this to be t 271 * however only when freeing different handles 272 * handle should only be freed once, and shoul 273 * after freeing. 274 */ 275 void zpool_free(struct zpool *zpool, unsigned 276 { 277 zpool->driver->free(zpool->pool, handl 278 } 279 280 /** 281 * zpool_map_handle() - Map a previously alloc 282 * @zpool: The zpool that the handle was 283 * @handle: The handle to map 284 * @mapmode: How the memory should be mappe 285 * 286 * This maps a previously allocated handle int 287 * param indicates to the implementation how t 288 * used, i.e. read-only, write-only, read-writ 289 * implementation does not support it, the mem 290 * as read-write. 291 * 292 * This may hold locks, disable interrupts, an 293 * and the zpool_unmap_handle() must be called 294 * actions. The code that uses the mapped han 295 * its operations on the mapped handle memory 296 * as soon as possible. As the implementation 297 * data, multiple handles should not be mapped 298 * any cpu. 299 * 300 * Returns: A pointer to the handle's mapped m 301 */ 302 void *zpool_map_handle(struct zpool *zpool, un 303 enum zpool_mapmode map 304 { 305 return zpool->driver->map(zpool->pool, 306 } 307 308 /** 309 * zpool_unmap_handle() - Unmap a previously m 310 * @zpool: The zpool that the handle was 311 * @handle: The handle to unmap 312 * 313 * This unmaps a previously mapped handle. An 314 * actions that the implementation took in zpo 315 * will be undone here. The memory area retur 316 * zpool_map_handle() should no longer be used 317 */ 318 void zpool_unmap_handle(struct zpool *zpool, u 319 { 320 zpool->driver->unmap(zpool->pool, hand 321 } 322 323 /** 324 * zpool_get_total_pages() - The total size of 325 * @zpool: The zpool to check 326 * 327 * This returns the total size in pages of the 328 * 329 * Returns: Total size of the zpool in pages. 330 */ 331 u64 zpool_get_total_pages(struct zpool *zpool) 332 { 333 return zpool->driver->total_pages(zpoo 334 } 335 336 /** 337 * zpool_can_sleep_mapped - Test if zpool can 338 * @zpool: The zpool to test 339 * 340 * Some allocators enter non-preemptible conte 341 * disable pagefaults) and exit that context i 342 * we can do with the mapped object. For insta 343 * asynchronous crypto API to decompress such 344 * since those will call into the scheduler. T 345 * we use such an allocator. 346 * 347 * Returns: true if zpool can sleep; false oth 348 */ 349 bool zpool_can_sleep_mapped(struct zpool *zpoo 350 { 351 return zpool->driver->sleep_mapped; 352 } 353 354 MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.or 355 MODULE_DESCRIPTION("Common API for compressed 356
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.