^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * zpool memory storage api
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2014 Dan Streetman
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * This is a common frontend for memory storage pool implementations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Typically, this is used to store compressed memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/zpool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct zpool {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct zpool_driver *driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) void *pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) const struct zpool_ops *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) bool evictable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static LIST_HEAD(drivers_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static DEFINE_SPINLOCK(drivers_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static LIST_HEAD(pools_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static DEFINE_SPINLOCK(pools_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * zpool_register_driver() - register a zpool implementation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * @driver: driver to register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) void zpool_register_driver(struct zpool_driver *driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) spin_lock(&drivers_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) atomic_set(&driver->refcount, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) list_add(&driver->list, &drivers_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) spin_unlock(&drivers_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) EXPORT_SYMBOL(zpool_register_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * zpool_unregister_driver() - unregister a zpool implementation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * @driver: driver to unregister.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * Module usage counting is used to prevent using a driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * while/after unloading, so if this is called from module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * exit function, this should never fail; if called from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * other than the module exit function, and this returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * failure, the driver is in use and must remain available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) int zpool_unregister_driver(struct zpool_driver *driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) int ret = 0, refcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) spin_lock(&drivers_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) refcount = atomic_read(&driver->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) WARN_ON(refcount < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (refcount > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) list_del(&driver->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) spin_unlock(&drivers_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) EXPORT_SYMBOL(zpool_unregister_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /* this assumes @type is null-terminated. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static struct zpool_driver *zpool_get_driver(const char *type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct zpool_driver *driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) spin_lock(&drivers_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) list_for_each_entry(driver, &drivers_head, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (!strcmp(driver->type, type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) bool got = try_module_get(driver->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (got)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) atomic_inc(&driver->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) spin_unlock(&drivers_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return got ? driver : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) spin_unlock(&drivers_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static void zpool_put_driver(struct zpool_driver *driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) atomic_dec(&driver->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) module_put(driver->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * zpool_has_pool() - Check if the pool driver is available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * @type: The type of the zpool to check (e.g. zbud, zsmalloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * This checks if the @type pool driver is available. This will try to load
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * the requested module, if needed, but there is no guarantee the module will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * still be loaded and available immediately after calling. If this returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * true, the caller should assume the pool is available, but must be prepared
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * to handle the @zpool_create_pool() returning failure. However if this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * returns false, the caller should assume the requested pool type is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * available; either the requested pool type module does not exist, or could
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * not be loaded, and calling @zpool_create_pool() with the pool type will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * The @type string must be null-terminated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * Returns: true if @type pool is available, false if not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) bool zpool_has_pool(char *type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct zpool_driver *driver = zpool_get_driver(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (!driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) request_module("zpool-%s", type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) driver = zpool_get_driver(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (!driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) zpool_put_driver(driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) EXPORT_SYMBOL(zpool_has_pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * zpool_create_pool() - Create a new zpool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * @type: The type of the zpool to create (e.g. zbud, zsmalloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * @name: The name of the zpool (e.g. zram0, zswap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * @gfp: The GFP flags to use when allocating the pool.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * @ops: The optional ops callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * This creates a new zpool of the specified type. The gfp flags will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * used when allocating memory, if the implementation supports it. If the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * ops param is NULL, then the created zpool will not be evictable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * Implementations must guarantee this to be thread-safe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * The @type and @name strings must be null-terminated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * Returns: New zpool on success, NULL on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct zpool *zpool_create_pool(const char *type, const char *name, gfp_t gfp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) const struct zpool_ops *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct zpool_driver *driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct zpool *zpool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) pr_debug("creating pool type %s\n", type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) driver = zpool_get_driver(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (!driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) request_module("zpool-%s", type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) driver = zpool_get_driver(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (!driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) pr_err("no driver for type %s\n", type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) zpool = kmalloc(sizeof(*zpool), gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (!zpool) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) pr_err("couldn't create zpool - out of memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) zpool_put_driver(driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) zpool->driver = driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) zpool->pool = driver->create(name, gfp, ops, zpool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) zpool->ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) zpool->evictable = driver->shrink && ops && ops->evict;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (!zpool->pool) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) pr_err("couldn't create %s pool\n", type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) zpool_put_driver(driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) kfree(zpool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) pr_debug("created pool type %s\n", type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) spin_lock(&pools_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) list_add(&zpool->list, &pools_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) spin_unlock(&pools_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return zpool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * zpool_destroy_pool() - Destroy a zpool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * @zpool: The zpool to destroy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * Implementations must guarantee this to be thread-safe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * however only when destroying different pools. The same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * pool should only be destroyed once, and should not be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * after it is destroyed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * This destroys an existing zpool. The zpool should not be in use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) void zpool_destroy_pool(struct zpool *zpool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) pr_debug("destroying pool type %s\n", zpool->driver->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) spin_lock(&pools_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) list_del(&zpool->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) spin_unlock(&pools_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) zpool->driver->destroy(zpool->pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) zpool_put_driver(zpool->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) kfree(zpool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * zpool_get_type() - Get the type of the zpool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * @zpool: The zpool to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) * This returns the type of the pool.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * Implementations must guarantee this to be thread-safe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * Returns: The type of zpool.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) const char *zpool_get_type(struct zpool *zpool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return zpool->driver->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * zpool_malloc_support_movable() - Check if the zpool supports
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * allocating movable memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * @zpool: The zpool to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * This returns if the zpool supports allocating movable memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * Implementations must guarantee this to be thread-safe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * Returns: true if the zpool supports allocating movable memory, false if not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) bool zpool_malloc_support_movable(struct zpool *zpool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return zpool->driver->malloc_support_movable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * zpool_malloc() - Allocate memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * @zpool: The zpool to allocate from.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * @size: The amount of memory to allocate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * @gfp: The GFP flags to use when allocating memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * @handle: Pointer to the handle to set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) * This allocates the requested amount of memory from the pool.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * The gfp flags will be used when allocating memory, if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * implementation supports it. The provided @handle will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) * set to the allocated object handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * Implementations must guarantee this to be thread-safe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * Returns: 0 on success, negative value on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) int zpool_malloc(struct zpool *zpool, size_t size, gfp_t gfp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) unsigned long *handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return zpool->driver->malloc(zpool->pool, size, gfp, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * zpool_free() - Free previously allocated memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * @zpool: The zpool that allocated the memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * @handle: The handle to the memory to free.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * This frees previously allocated memory. This does not guarantee
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * that the pool will actually free memory, only that the memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * in the pool will become available for use by the pool.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * Implementations must guarantee this to be thread-safe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * however only when freeing different handles. The same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * handle should only be freed once, and should not be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * after freeing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) void zpool_free(struct zpool *zpool, unsigned long handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) zpool->driver->free(zpool->pool, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * zpool_shrink() - Shrink the pool size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * @zpool: The zpool to shrink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * @pages: The number of pages to shrink the pool.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * @reclaimed: The number of pages successfully evicted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * This attempts to shrink the actual memory size of the pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * by evicting currently used handle(s). If the pool was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * created with no zpool_ops, or the evict call fails for any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * of the handles, this will fail. If non-NULL, the @reclaimed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * parameter will be set to the number of pages reclaimed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * which may be more than the number of pages requested.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * Implementations must guarantee this to be thread-safe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * Returns: 0 on success, negative value on error/failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) int zpool_shrink(struct zpool *zpool, unsigned int pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) unsigned int *reclaimed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) return zpool->driver->shrink ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) zpool->driver->shrink(zpool->pool, pages, reclaimed) : -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * zpool_map_handle() - Map a previously allocated handle into memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * @zpool: The zpool that the handle was allocated from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * @handle: The handle to map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) * @mapmode: How the memory should be mapped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * This maps a previously allocated handle into memory. The @mapmode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * param indicates to the implementation how the memory will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * used, i.e. read-only, write-only, read-write. If the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * implementation does not support it, the memory will be treated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) * as read-write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) * This may hold locks, disable interrupts, and/or preemption,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) * and the zpool_unmap_handle() must be called to undo those
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) * actions. The code that uses the mapped handle should complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) * its operatons on the mapped handle memory quickly and unmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * as soon as possible. As the implementation may use per-cpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * data, multiple handles should not be mapped concurrently on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) * any cpu.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * Returns: A pointer to the handle's mapped memory area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) void *zpool_map_handle(struct zpool *zpool, unsigned long handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) enum zpool_mapmode mapmode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return zpool->driver->map(zpool->pool, handle, mapmode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) * zpool_unmap_handle() - Unmap a previously mapped handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) * @zpool: The zpool that the handle was allocated from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * @handle: The handle to unmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) * This unmaps a previously mapped handle. Any locks or other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * actions that the implementation took in zpool_map_handle()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) * will be undone here. The memory area returned from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * zpool_map_handle() should no longer be used after this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) void zpool_unmap_handle(struct zpool *zpool, unsigned long handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) zpool->driver->unmap(zpool->pool, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) * zpool_get_total_size() - The total size of the pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) * @zpool: The zpool to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) * This returns the total size in bytes of the pool.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) * Returns: Total size of the zpool in bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) u64 zpool_get_total_size(struct zpool *zpool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) return zpool->driver->total_size(zpool->pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) * zpool_evictable() - Test if zpool is potentially evictable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) * @zpool: The zpool to test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) * Zpool is only potentially evictable when it's created with struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * zpool_ops.evict and its driver implements struct zpool_driver.shrink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * However, it doesn't necessarily mean driver will use zpool_ops.evict
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * in its implementation of zpool_driver.shrink. It could do internal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) * defragmentation instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) * Returns: true if potentially evictable; false otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) bool zpool_evictable(struct zpool *zpool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) return zpool->evictable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) MODULE_DESCRIPTION("Common API for compressed memory storage");