Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Add configfs and memory store: Kyungchan Koh <kkc6196@fb.com> and |
| 3 | * Shaohua Li <shli@fb.com> |
| 4 | */ |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 5 | #include <linux/module.h> |
Matias Bjørling | fc1bc35 | 2013-12-21 00:11:01 +0100 | [diff] [blame] | 6 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 7 | #include <linux/moduleparam.h> |
| 8 | #include <linux/sched.h> |
| 9 | #include <linux/fs.h> |
| 10 | #include <linux/blkdev.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/blk-mq.h> |
| 14 | #include <linux/hrtimer.h> |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 15 | #include <linux/lightnvm.h> |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 16 | #include <linux/configfs.h> |
Shaohua Li | 2f54a61 | 2017-08-14 15:05:00 -0700 | [diff] [blame] | 17 | #include <linux/badblocks.h> |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 18 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 19 | #define SECTOR_SHIFT 9 |
| 20 | #define PAGE_SECTORS_SHIFT (PAGE_SHIFT - SECTOR_SHIFT) |
| 21 | #define PAGE_SECTORS (1 << PAGE_SECTORS_SHIFT) |
| 22 | #define SECTOR_SIZE (1 << SECTOR_SHIFT) |
| 23 | #define SECTOR_MASK (PAGE_SECTORS - 1) |
| 24 | |
| 25 | #define FREE_BATCH 16 |
| 26 | |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 27 | #define TICKS_PER_SEC 50ULL |
| 28 | #define TIMER_INTERVAL (NSEC_PER_SEC / TICKS_PER_SEC) |
| 29 | |
| 30 | static inline u64 mb_per_tick(int mbps) |
| 31 | { |
| 32 | return (1 << 20) / TICKS_PER_SEC * ((u64) mbps); |
| 33 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 34 | |
| 35 | struct nullb_cmd { |
| 36 | struct list_head list; |
| 37 | struct llist_node ll_list; |
Ying Huang | 966a967 | 2017-08-08 12:30:00 +0800 | [diff] [blame] | 38 | call_single_data_t csd; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 39 | struct request *rq; |
| 40 | struct bio *bio; |
| 41 | unsigned int tag; |
| 42 | struct nullb_queue *nq; |
Paolo Valente | 3c395a9 | 2015-12-01 11:48:17 +0100 | [diff] [blame] | 43 | struct hrtimer timer; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 44 | blk_status_t error; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | struct nullb_queue { |
| 48 | unsigned long *tag_map; |
| 49 | wait_queue_head_t wait; |
| 50 | unsigned int queue_depth; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 51 | struct nullb_device *dev; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 52 | |
| 53 | struct nullb_cmd *cmds; |
| 54 | }; |
| 55 | |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 56 | /* |
| 57 | * Status flags for nullb_device. |
| 58 | * |
| 59 | * CONFIGURED: Device has been configured and turned on. Cannot reconfigure. |
| 60 | * UP: Device is currently on and visible in userspace. |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 61 | * THROTTLED: Device is being throttled. |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 62 | * CACHE: Device is using a write-back cache. |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 63 | */ |
| 64 | enum nullb_device_flags { |
| 65 | NULLB_DEV_FL_CONFIGURED = 0, |
| 66 | NULLB_DEV_FL_UP = 1, |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 67 | NULLB_DEV_FL_THROTTLED = 2, |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 68 | NULLB_DEV_FL_CACHE = 3, |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 71 | /* |
| 72 | * nullb_page is a page in memory for nullb devices. |
| 73 | * |
| 74 | * @page: The page holding the data. |
| 75 | * @bitmap: The bitmap represents which sector in the page has data. |
| 76 | * Each bit represents one block size. For example, sector 8 |
| 77 | * will use the 7th bit |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 78 | * The highest 2 bits of bitmap are for special purpose. LOCK means the cache |
| 79 | * page is being flushing to storage. FREE means the cache page is freed and |
| 80 | * should be skipped from flushing to storage. Please see |
| 81 | * null_make_cache_space |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 82 | */ |
| 83 | struct nullb_page { |
| 84 | struct page *page; |
| 85 | unsigned long bitmap; |
| 86 | }; |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 87 | #define NULLB_PAGE_LOCK (sizeof(unsigned long) * 8 - 1) |
| 88 | #define NULLB_PAGE_FREE (sizeof(unsigned long) * 8 - 2) |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 89 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 90 | struct nullb_device { |
| 91 | struct nullb *nullb; |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 92 | struct config_item item; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 93 | struct radix_tree_root data; /* data stored in the disk */ |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 94 | struct radix_tree_root cache; /* disk cache data */ |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 95 | unsigned long flags; /* device flags */ |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 96 | unsigned int curr_cache; |
Shaohua Li | 2f54a61 | 2017-08-14 15:05:00 -0700 | [diff] [blame] | 97 | struct badblocks badblocks; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 98 | |
| 99 | unsigned long size; /* device size in MB */ |
| 100 | unsigned long completion_nsec; /* time in ns to complete a request */ |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 101 | unsigned long cache_size; /* disk cache size in MB */ |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 102 | unsigned int submit_queues; /* number of submission queues */ |
| 103 | unsigned int home_node; /* home node for the device */ |
| 104 | unsigned int queue_mode; /* block interface */ |
| 105 | unsigned int blocksize; /* block size */ |
| 106 | unsigned int irqmode; /* IRQ completion handler */ |
| 107 | unsigned int hw_queue_depth; /* queue depth */ |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 108 | unsigned int index; /* index of the disk, only valid with a disk */ |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 109 | unsigned int mbps; /* Bandwidth throttle cap (in MB/s) */ |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 110 | bool use_lightnvm; /* register as a LightNVM device */ |
| 111 | bool blocking; /* blocking blk-mq device */ |
| 112 | bool use_per_node_hctx; /* use per-node allocation for hardware context */ |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 113 | bool power; /* power on/off the device */ |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 114 | bool memory_backed; /* if data is stored in memory */ |
Shaohua Li | 306eb6b | 2017-08-14 15:04:57 -0700 | [diff] [blame] | 115 | bool discard; /* if support discard */ |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 118 | struct nullb { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 119 | struct nullb_device *dev; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 120 | struct list_head list; |
| 121 | unsigned int index; |
| 122 | struct request_queue *q; |
| 123 | struct gendisk *disk; |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 124 | struct nvm_dev *ndev; |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 125 | struct blk_mq_tag_set *tag_set; |
| 126 | struct blk_mq_tag_set __tag_set; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 127 | unsigned int queue_depth; |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 128 | atomic_long_t cur_bytes; |
| 129 | struct hrtimer bw_timer; |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 130 | unsigned long cache_flush_pos; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 131 | spinlock_t lock; |
| 132 | |
| 133 | struct nullb_queue *queues; |
| 134 | unsigned int nr_queues; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 135 | char disk_name[DISK_NAME_LEN]; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | static LIST_HEAD(nullb_list); |
| 139 | static struct mutex lock; |
| 140 | static int null_major; |
Shaohua Li | 94bc02e | 2017-08-14 15:04:55 -0700 | [diff] [blame] | 141 | static DEFINE_IDA(nullb_indexes); |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 142 | static struct kmem_cache *ppa_cache; |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 143 | static struct blk_mq_tag_set tag_set; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 144 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 145 | enum { |
| 146 | NULL_IRQ_NONE = 0, |
| 147 | NULL_IRQ_SOFTIRQ = 1, |
| 148 | NULL_IRQ_TIMER = 2, |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 149 | }; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 150 | |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 151 | enum { |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 152 | NULL_Q_BIO = 0, |
| 153 | NULL_Q_RQ = 1, |
| 154 | NULL_Q_MQ = 2, |
| 155 | }; |
| 156 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 157 | static int g_submit_queues = 1; |
| 158 | module_param_named(submit_queues, g_submit_queues, int, S_IRUGO); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 159 | MODULE_PARM_DESC(submit_queues, "Number of submission queues"); |
| 160 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 161 | static int g_home_node = NUMA_NO_NODE; |
| 162 | module_param_named(home_node, g_home_node, int, S_IRUGO); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 163 | MODULE_PARM_DESC(home_node, "Home node for the device"); |
| 164 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 165 | static int g_queue_mode = NULL_Q_MQ; |
Matias Bjorling | 709c866 | 2014-11-26 14:45:48 -0700 | [diff] [blame] | 166 | |
| 167 | static int null_param_store_val(const char *str, int *val, int min, int max) |
| 168 | { |
| 169 | int ret, new_val; |
| 170 | |
| 171 | ret = kstrtoint(str, 10, &new_val); |
| 172 | if (ret) |
| 173 | return -EINVAL; |
| 174 | |
| 175 | if (new_val < min || new_val > max) |
| 176 | return -EINVAL; |
| 177 | |
| 178 | *val = new_val; |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static int null_set_queue_mode(const char *str, const struct kernel_param *kp) |
| 183 | { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 184 | return null_param_store_val(str, &g_queue_mode, NULL_Q_BIO, NULL_Q_MQ); |
Matias Bjorling | 709c866 | 2014-11-26 14:45:48 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 187 | static const struct kernel_param_ops null_queue_mode_param_ops = { |
Matias Bjorling | 709c866 | 2014-11-26 14:45:48 -0700 | [diff] [blame] | 188 | .set = null_set_queue_mode, |
| 189 | .get = param_get_int, |
| 190 | }; |
| 191 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 192 | device_param_cb(queue_mode, &null_queue_mode_param_ops, &g_queue_mode, S_IRUGO); |
Mike Snitzer | 54ae81c | 2014-06-11 17:13:50 -0400 | [diff] [blame] | 193 | MODULE_PARM_DESC(queue_mode, "Block interface to use (0=bio,1=rq,2=multiqueue)"); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 194 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 195 | static int g_gb = 250; |
| 196 | module_param_named(gb, g_gb, int, S_IRUGO); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 197 | MODULE_PARM_DESC(gb, "Size in GB"); |
| 198 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 199 | static int g_bs = 512; |
| 200 | module_param_named(bs, g_bs, int, S_IRUGO); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 201 | MODULE_PARM_DESC(bs, "Block size (in bytes)"); |
| 202 | |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 203 | static int nr_devices = 1; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 204 | module_param(nr_devices, int, S_IRUGO); |
| 205 | MODULE_PARM_DESC(nr_devices, "Number of devices to register"); |
| 206 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 207 | static bool g_use_lightnvm; |
| 208 | module_param_named(use_lightnvm, g_use_lightnvm, bool, S_IRUGO); |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 209 | MODULE_PARM_DESC(use_lightnvm, "Register as a LightNVM device"); |
| 210 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 211 | static bool g_blocking; |
| 212 | module_param_named(blocking, g_blocking, bool, S_IRUGO); |
Jens Axboe | db5bcf8 | 2017-03-30 13:44:26 -0600 | [diff] [blame] | 213 | MODULE_PARM_DESC(blocking, "Register as a blocking blk-mq driver device"); |
| 214 | |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 215 | static bool shared_tags; |
| 216 | module_param(shared_tags, bool, S_IRUGO); |
| 217 | MODULE_PARM_DESC(shared_tags, "Share tag set between devices for blk-mq"); |
| 218 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 219 | static int g_irqmode = NULL_IRQ_SOFTIRQ; |
Matias Bjorling | 709c866 | 2014-11-26 14:45:48 -0700 | [diff] [blame] | 220 | |
| 221 | static int null_set_irqmode(const char *str, const struct kernel_param *kp) |
| 222 | { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 223 | return null_param_store_val(str, &g_irqmode, NULL_IRQ_NONE, |
Matias Bjorling | 709c866 | 2014-11-26 14:45:48 -0700 | [diff] [blame] | 224 | NULL_IRQ_TIMER); |
| 225 | } |
| 226 | |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 227 | static const struct kernel_param_ops null_irqmode_param_ops = { |
Matias Bjorling | 709c866 | 2014-11-26 14:45:48 -0700 | [diff] [blame] | 228 | .set = null_set_irqmode, |
| 229 | .get = param_get_int, |
| 230 | }; |
| 231 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 232 | device_param_cb(irqmode, &null_irqmode_param_ops, &g_irqmode, S_IRUGO); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 233 | MODULE_PARM_DESC(irqmode, "IRQ completion handler. 0-none, 1-softirq, 2-timer"); |
| 234 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 235 | static unsigned long g_completion_nsec = 10000; |
| 236 | module_param_named(completion_nsec, g_completion_nsec, ulong, S_IRUGO); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 237 | MODULE_PARM_DESC(completion_nsec, "Time in ns to complete a request in hardware. Default: 10,000ns"); |
| 238 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 239 | static int g_hw_queue_depth = 64; |
| 240 | module_param_named(hw_queue_depth, g_hw_queue_depth, int, S_IRUGO); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 241 | MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 64"); |
| 242 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 243 | static bool g_use_per_node_hctx; |
| 244 | module_param_named(use_per_node_hctx, g_use_per_node_hctx, bool, S_IRUGO); |
Matias Bjørling | 2000524 | 2013-12-21 00:11:00 +0100 | [diff] [blame] | 245 | MODULE_PARM_DESC(use_per_node_hctx, "Use per-node allocation for hardware context queues. Default: false"); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 246 | |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 247 | static struct nullb_device *null_alloc_dev(void); |
| 248 | static void null_free_dev(struct nullb_device *dev); |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 249 | static void null_del_dev(struct nullb *nullb); |
| 250 | static int null_add_dev(struct nullb_device *dev); |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 251 | static void null_free_device_storage(struct nullb_device *dev, bool is_cache); |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 252 | |
| 253 | static inline struct nullb_device *to_nullb_device(struct config_item *item) |
| 254 | { |
| 255 | return item ? container_of(item, struct nullb_device, item) : NULL; |
| 256 | } |
| 257 | |
| 258 | static inline ssize_t nullb_device_uint_attr_show(unsigned int val, char *page) |
| 259 | { |
| 260 | return snprintf(page, PAGE_SIZE, "%u\n", val); |
| 261 | } |
| 262 | |
| 263 | static inline ssize_t nullb_device_ulong_attr_show(unsigned long val, |
| 264 | char *page) |
| 265 | { |
| 266 | return snprintf(page, PAGE_SIZE, "%lu\n", val); |
| 267 | } |
| 268 | |
| 269 | static inline ssize_t nullb_device_bool_attr_show(bool val, char *page) |
| 270 | { |
| 271 | return snprintf(page, PAGE_SIZE, "%u\n", val); |
| 272 | } |
| 273 | |
| 274 | static ssize_t nullb_device_uint_attr_store(unsigned int *val, |
| 275 | const char *page, size_t count) |
| 276 | { |
| 277 | unsigned int tmp; |
| 278 | int result; |
| 279 | |
| 280 | result = kstrtouint(page, 0, &tmp); |
| 281 | if (result) |
| 282 | return result; |
| 283 | |
| 284 | *val = tmp; |
| 285 | return count; |
| 286 | } |
| 287 | |
| 288 | static ssize_t nullb_device_ulong_attr_store(unsigned long *val, |
| 289 | const char *page, size_t count) |
| 290 | { |
| 291 | int result; |
| 292 | unsigned long tmp; |
| 293 | |
| 294 | result = kstrtoul(page, 0, &tmp); |
| 295 | if (result) |
| 296 | return result; |
| 297 | |
| 298 | *val = tmp; |
| 299 | return count; |
| 300 | } |
| 301 | |
| 302 | static ssize_t nullb_device_bool_attr_store(bool *val, const char *page, |
| 303 | size_t count) |
| 304 | { |
| 305 | bool tmp; |
| 306 | int result; |
| 307 | |
| 308 | result = kstrtobool(page, &tmp); |
| 309 | if (result) |
| 310 | return result; |
| 311 | |
| 312 | *val = tmp; |
| 313 | return count; |
| 314 | } |
| 315 | |
| 316 | /* The following macro should only be used with TYPE = {uint, ulong, bool}. */ |
| 317 | #define NULLB_DEVICE_ATTR(NAME, TYPE) \ |
| 318 | static ssize_t \ |
| 319 | nullb_device_##NAME##_show(struct config_item *item, char *page) \ |
| 320 | { \ |
| 321 | return nullb_device_##TYPE##_attr_show( \ |
| 322 | to_nullb_device(item)->NAME, page); \ |
| 323 | } \ |
| 324 | static ssize_t \ |
| 325 | nullb_device_##NAME##_store(struct config_item *item, const char *page, \ |
| 326 | size_t count) \ |
| 327 | { \ |
| 328 | if (test_bit(NULLB_DEV_FL_CONFIGURED, &to_nullb_device(item)->flags)) \ |
| 329 | return -EBUSY; \ |
| 330 | return nullb_device_##TYPE##_attr_store( \ |
| 331 | &to_nullb_device(item)->NAME, page, count); \ |
| 332 | } \ |
| 333 | CONFIGFS_ATTR(nullb_device_, NAME); |
| 334 | |
| 335 | NULLB_DEVICE_ATTR(size, ulong); |
| 336 | NULLB_DEVICE_ATTR(completion_nsec, ulong); |
| 337 | NULLB_DEVICE_ATTR(submit_queues, uint); |
| 338 | NULLB_DEVICE_ATTR(home_node, uint); |
| 339 | NULLB_DEVICE_ATTR(queue_mode, uint); |
| 340 | NULLB_DEVICE_ATTR(blocksize, uint); |
| 341 | NULLB_DEVICE_ATTR(irqmode, uint); |
| 342 | NULLB_DEVICE_ATTR(hw_queue_depth, uint); |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 343 | NULLB_DEVICE_ATTR(index, uint); |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 344 | NULLB_DEVICE_ATTR(use_lightnvm, bool); |
| 345 | NULLB_DEVICE_ATTR(blocking, bool); |
| 346 | NULLB_DEVICE_ATTR(use_per_node_hctx, bool); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 347 | NULLB_DEVICE_ATTR(memory_backed, bool); |
Shaohua Li | 306eb6b | 2017-08-14 15:04:57 -0700 | [diff] [blame] | 348 | NULLB_DEVICE_ATTR(discard, bool); |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 349 | NULLB_DEVICE_ATTR(mbps, uint); |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 350 | NULLB_DEVICE_ATTR(cache_size, ulong); |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 351 | |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 352 | static ssize_t nullb_device_power_show(struct config_item *item, char *page) |
| 353 | { |
| 354 | return nullb_device_bool_attr_show(to_nullb_device(item)->power, page); |
| 355 | } |
| 356 | |
| 357 | static ssize_t nullb_device_power_store(struct config_item *item, |
| 358 | const char *page, size_t count) |
| 359 | { |
| 360 | struct nullb_device *dev = to_nullb_device(item); |
| 361 | bool newp = false; |
| 362 | ssize_t ret; |
| 363 | |
| 364 | ret = nullb_device_bool_attr_store(&newp, page, count); |
| 365 | if (ret < 0) |
| 366 | return ret; |
| 367 | |
| 368 | if (!dev->power && newp) { |
| 369 | if (test_and_set_bit(NULLB_DEV_FL_UP, &dev->flags)) |
| 370 | return count; |
| 371 | if (null_add_dev(dev)) { |
| 372 | clear_bit(NULLB_DEV_FL_UP, &dev->flags); |
| 373 | return -ENOMEM; |
| 374 | } |
| 375 | |
| 376 | set_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags); |
| 377 | dev->power = newp; |
Jens Axboe | b3c3051 | 2017-08-28 15:06:31 -0600 | [diff] [blame] | 378 | } else if (dev->power && !newp) { |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 379 | mutex_lock(&lock); |
| 380 | dev->power = newp; |
| 381 | null_del_dev(dev->nullb); |
| 382 | mutex_unlock(&lock); |
| 383 | clear_bit(NULLB_DEV_FL_UP, &dev->flags); |
| 384 | } |
| 385 | |
| 386 | return count; |
| 387 | } |
| 388 | |
| 389 | CONFIGFS_ATTR(nullb_device_, power); |
| 390 | |
Shaohua Li | 2f54a61 | 2017-08-14 15:05:00 -0700 | [diff] [blame] | 391 | static ssize_t nullb_device_badblocks_show(struct config_item *item, char *page) |
| 392 | { |
| 393 | struct nullb_device *t_dev = to_nullb_device(item); |
| 394 | |
| 395 | return badblocks_show(&t_dev->badblocks, page, 0); |
| 396 | } |
| 397 | |
| 398 | static ssize_t nullb_device_badblocks_store(struct config_item *item, |
| 399 | const char *page, size_t count) |
| 400 | { |
| 401 | struct nullb_device *t_dev = to_nullb_device(item); |
| 402 | char *orig, *buf, *tmp; |
| 403 | u64 start, end; |
| 404 | int ret; |
| 405 | |
| 406 | orig = kstrndup(page, count, GFP_KERNEL); |
| 407 | if (!orig) |
| 408 | return -ENOMEM; |
| 409 | |
| 410 | buf = strstrip(orig); |
| 411 | |
| 412 | ret = -EINVAL; |
| 413 | if (buf[0] != '+' && buf[0] != '-') |
| 414 | goto out; |
| 415 | tmp = strchr(&buf[1], '-'); |
| 416 | if (!tmp) |
| 417 | goto out; |
| 418 | *tmp = '\0'; |
| 419 | ret = kstrtoull(buf + 1, 0, &start); |
| 420 | if (ret) |
| 421 | goto out; |
| 422 | ret = kstrtoull(tmp + 1, 0, &end); |
| 423 | if (ret) |
| 424 | goto out; |
| 425 | ret = -EINVAL; |
| 426 | if (start > end) |
| 427 | goto out; |
| 428 | /* enable badblocks */ |
| 429 | cmpxchg(&t_dev->badblocks.shift, -1, 0); |
| 430 | if (buf[0] == '+') |
| 431 | ret = badblocks_set(&t_dev->badblocks, start, |
| 432 | end - start + 1, 1); |
| 433 | else |
| 434 | ret = badblocks_clear(&t_dev->badblocks, start, |
| 435 | end - start + 1); |
| 436 | if (ret == 0) |
| 437 | ret = count; |
| 438 | out: |
| 439 | kfree(orig); |
| 440 | return ret; |
| 441 | } |
| 442 | CONFIGFS_ATTR(nullb_device_, badblocks); |
| 443 | |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 444 | static struct configfs_attribute *nullb_device_attrs[] = { |
| 445 | &nullb_device_attr_size, |
| 446 | &nullb_device_attr_completion_nsec, |
| 447 | &nullb_device_attr_submit_queues, |
| 448 | &nullb_device_attr_home_node, |
| 449 | &nullb_device_attr_queue_mode, |
| 450 | &nullb_device_attr_blocksize, |
| 451 | &nullb_device_attr_irqmode, |
| 452 | &nullb_device_attr_hw_queue_depth, |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 453 | &nullb_device_attr_index, |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 454 | &nullb_device_attr_use_lightnvm, |
| 455 | &nullb_device_attr_blocking, |
| 456 | &nullb_device_attr_use_per_node_hctx, |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 457 | &nullb_device_attr_power, |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 458 | &nullb_device_attr_memory_backed, |
Shaohua Li | 306eb6b | 2017-08-14 15:04:57 -0700 | [diff] [blame] | 459 | &nullb_device_attr_discard, |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 460 | &nullb_device_attr_mbps, |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 461 | &nullb_device_attr_cache_size, |
Shaohua Li | 2f54a61 | 2017-08-14 15:05:00 -0700 | [diff] [blame] | 462 | &nullb_device_attr_badblocks, |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 463 | NULL, |
| 464 | }; |
| 465 | |
| 466 | static void nullb_device_release(struct config_item *item) |
| 467 | { |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 468 | struct nullb_device *dev = to_nullb_device(item); |
| 469 | |
Shaohua Li | 2f54a61 | 2017-08-14 15:05:00 -0700 | [diff] [blame] | 470 | badblocks_exit(&dev->badblocks); |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 471 | null_free_device_storage(dev, false); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 472 | null_free_dev(dev); |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | static struct configfs_item_operations nullb_device_ops = { |
| 476 | .release = nullb_device_release, |
| 477 | }; |
| 478 | |
Bhumika Goyal | e1919df | 2017-10-16 17:18:49 +0200 | [diff] [blame^] | 479 | static const struct config_item_type nullb_device_type = { |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 480 | .ct_item_ops = &nullb_device_ops, |
| 481 | .ct_attrs = nullb_device_attrs, |
| 482 | .ct_owner = THIS_MODULE, |
| 483 | }; |
| 484 | |
| 485 | static struct |
| 486 | config_item *nullb_group_make_item(struct config_group *group, const char *name) |
| 487 | { |
| 488 | struct nullb_device *dev; |
| 489 | |
| 490 | dev = null_alloc_dev(); |
| 491 | if (!dev) |
| 492 | return ERR_PTR(-ENOMEM); |
| 493 | |
| 494 | config_item_init_type_name(&dev->item, name, &nullb_device_type); |
| 495 | |
| 496 | return &dev->item; |
| 497 | } |
| 498 | |
| 499 | static void |
| 500 | nullb_group_drop_item(struct config_group *group, struct config_item *item) |
| 501 | { |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 502 | struct nullb_device *dev = to_nullb_device(item); |
| 503 | |
| 504 | if (test_and_clear_bit(NULLB_DEV_FL_UP, &dev->flags)) { |
| 505 | mutex_lock(&lock); |
| 506 | dev->power = false; |
| 507 | null_del_dev(dev->nullb); |
| 508 | mutex_unlock(&lock); |
| 509 | } |
| 510 | |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 511 | config_item_put(item); |
| 512 | } |
| 513 | |
| 514 | static ssize_t memb_group_features_show(struct config_item *item, char *page) |
| 515 | { |
Shaohua Li | 2f54a61 | 2017-08-14 15:05:00 -0700 | [diff] [blame] | 516 | return snprintf(page, PAGE_SIZE, "memory_backed,discard,bandwidth,cache,badblocks\n"); |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | CONFIGFS_ATTR_RO(memb_group_, features); |
| 520 | |
| 521 | static struct configfs_attribute *nullb_group_attrs[] = { |
| 522 | &memb_group_attr_features, |
| 523 | NULL, |
| 524 | }; |
| 525 | |
| 526 | static struct configfs_group_operations nullb_group_ops = { |
| 527 | .make_item = nullb_group_make_item, |
| 528 | .drop_item = nullb_group_drop_item, |
| 529 | }; |
| 530 | |
Bhumika Goyal | e1919df | 2017-10-16 17:18:49 +0200 | [diff] [blame^] | 531 | static const struct config_item_type nullb_group_type = { |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 532 | .ct_group_ops = &nullb_group_ops, |
| 533 | .ct_attrs = nullb_group_attrs, |
| 534 | .ct_owner = THIS_MODULE, |
| 535 | }; |
| 536 | |
| 537 | static struct configfs_subsystem nullb_subsys = { |
| 538 | .su_group = { |
| 539 | .cg_item = { |
| 540 | .ci_namebuf = "nullb", |
| 541 | .ci_type = &nullb_group_type, |
| 542 | }, |
| 543 | }, |
| 544 | }; |
| 545 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 546 | static inline int null_cache_active(struct nullb *nullb) |
| 547 | { |
| 548 | return test_bit(NULLB_DEV_FL_CACHE, &nullb->dev->flags); |
| 549 | } |
| 550 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 551 | static struct nullb_device *null_alloc_dev(void) |
| 552 | { |
| 553 | struct nullb_device *dev; |
| 554 | |
| 555 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 556 | if (!dev) |
| 557 | return NULL; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 558 | INIT_RADIX_TREE(&dev->data, GFP_ATOMIC); |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 559 | INIT_RADIX_TREE(&dev->cache, GFP_ATOMIC); |
Shaohua Li | 2f54a61 | 2017-08-14 15:05:00 -0700 | [diff] [blame] | 560 | if (badblocks_init(&dev->badblocks, 0)) { |
| 561 | kfree(dev); |
| 562 | return NULL; |
| 563 | } |
| 564 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 565 | dev->size = g_gb * 1024; |
| 566 | dev->completion_nsec = g_completion_nsec; |
| 567 | dev->submit_queues = g_submit_queues; |
| 568 | dev->home_node = g_home_node; |
| 569 | dev->queue_mode = g_queue_mode; |
| 570 | dev->blocksize = g_bs; |
| 571 | dev->irqmode = g_irqmode; |
| 572 | dev->hw_queue_depth = g_hw_queue_depth; |
| 573 | dev->use_lightnvm = g_use_lightnvm; |
| 574 | dev->blocking = g_blocking; |
| 575 | dev->use_per_node_hctx = g_use_per_node_hctx; |
| 576 | return dev; |
| 577 | } |
| 578 | |
| 579 | static void null_free_dev(struct nullb_device *dev) |
| 580 | { |
| 581 | kfree(dev); |
| 582 | } |
| 583 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 584 | static void put_tag(struct nullb_queue *nq, unsigned int tag) |
| 585 | { |
| 586 | clear_bit_unlock(tag, nq->tag_map); |
| 587 | |
| 588 | if (waitqueue_active(&nq->wait)) |
| 589 | wake_up(&nq->wait); |
| 590 | } |
| 591 | |
| 592 | static unsigned int get_tag(struct nullb_queue *nq) |
| 593 | { |
| 594 | unsigned int tag; |
| 595 | |
| 596 | do { |
| 597 | tag = find_first_zero_bit(nq->tag_map, nq->queue_depth); |
| 598 | if (tag >= nq->queue_depth) |
| 599 | return -1U; |
| 600 | } while (test_and_set_bit_lock(tag, nq->tag_map)); |
| 601 | |
| 602 | return tag; |
| 603 | } |
| 604 | |
| 605 | static void free_cmd(struct nullb_cmd *cmd) |
| 606 | { |
| 607 | put_tag(cmd->nq, cmd->tag); |
| 608 | } |
| 609 | |
Paolo Valente | 3c395a9 | 2015-12-01 11:48:17 +0100 | [diff] [blame] | 610 | static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer); |
| 611 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 612 | static struct nullb_cmd *__alloc_cmd(struct nullb_queue *nq) |
| 613 | { |
| 614 | struct nullb_cmd *cmd; |
| 615 | unsigned int tag; |
| 616 | |
| 617 | tag = get_tag(nq); |
| 618 | if (tag != -1U) { |
| 619 | cmd = &nq->cmds[tag]; |
| 620 | cmd->tag = tag; |
| 621 | cmd->nq = nq; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 622 | if (nq->dev->irqmode == NULL_IRQ_TIMER) { |
Paolo Valente | 3c395a9 | 2015-12-01 11:48:17 +0100 | [diff] [blame] | 623 | hrtimer_init(&cmd->timer, CLOCK_MONOTONIC, |
| 624 | HRTIMER_MODE_REL); |
| 625 | cmd->timer.function = null_cmd_timer_expired; |
| 626 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 627 | return cmd; |
| 628 | } |
| 629 | |
| 630 | return NULL; |
| 631 | } |
| 632 | |
| 633 | static struct nullb_cmd *alloc_cmd(struct nullb_queue *nq, int can_wait) |
| 634 | { |
| 635 | struct nullb_cmd *cmd; |
| 636 | DEFINE_WAIT(wait); |
| 637 | |
| 638 | cmd = __alloc_cmd(nq); |
| 639 | if (cmd || !can_wait) |
| 640 | return cmd; |
| 641 | |
| 642 | do { |
| 643 | prepare_to_wait(&nq->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 644 | cmd = __alloc_cmd(nq); |
| 645 | if (cmd) |
| 646 | break; |
| 647 | |
| 648 | io_schedule(); |
| 649 | } while (1); |
| 650 | |
| 651 | finish_wait(&nq->wait, &wait); |
| 652 | return cmd; |
| 653 | } |
| 654 | |
| 655 | static void end_cmd(struct nullb_cmd *cmd) |
| 656 | { |
Arianna Avanzini | cf8ecc5 | 2015-12-01 11:48:18 +0100 | [diff] [blame] | 657 | struct request_queue *q = NULL; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 658 | int queue_mode = cmd->nq->dev->queue_mode; |
Arianna Avanzini | cf8ecc5 | 2015-12-01 11:48:18 +0100 | [diff] [blame] | 659 | |
Mike Krinkin | e827120 | 2015-12-15 12:56:40 +0300 | [diff] [blame] | 660 | if (cmd->rq) |
| 661 | q = cmd->rq->q; |
| 662 | |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 663 | switch (queue_mode) { |
| 664 | case NULL_Q_MQ: |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 665 | blk_mq_end_request(cmd->rq, cmd->error); |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 666 | return; |
| 667 | case NULL_Q_RQ: |
| 668 | INIT_LIST_HEAD(&cmd->rq->queuelist); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 669 | blk_end_request_all(cmd->rq, cmd->error); |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 670 | break; |
| 671 | case NULL_Q_BIO: |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 672 | cmd->bio->bi_status = cmd->error; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 673 | bio_endio(cmd->bio); |
Jens Axboe | 48cc661 | 2015-12-28 13:02:47 -0700 | [diff] [blame] | 674 | break; |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 675 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 676 | |
Jens Axboe | 48cc661 | 2015-12-28 13:02:47 -0700 | [diff] [blame] | 677 | free_cmd(cmd); |
| 678 | |
Arianna Avanzini | cf8ecc5 | 2015-12-01 11:48:18 +0100 | [diff] [blame] | 679 | /* Restart queue if needed, as we are freeing a tag */ |
Jens Axboe | 48cc661 | 2015-12-28 13:02:47 -0700 | [diff] [blame] | 680 | if (queue_mode == NULL_Q_RQ && blk_queue_stopped(q)) { |
Arianna Avanzini | cf8ecc5 | 2015-12-01 11:48:18 +0100 | [diff] [blame] | 681 | unsigned long flags; |
| 682 | |
| 683 | spin_lock_irqsave(q->queue_lock, flags); |
Jens Axboe | 48cc661 | 2015-12-28 13:02:47 -0700 | [diff] [blame] | 684 | blk_start_queue_async(q); |
Arianna Avanzini | cf8ecc5 | 2015-12-01 11:48:18 +0100 | [diff] [blame] | 685 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 686 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer) |
| 690 | { |
Arianna Avanzini | cf8ecc5 | 2015-12-01 11:48:18 +0100 | [diff] [blame] | 691 | end_cmd(container_of(timer, struct nullb_cmd, timer)); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 692 | |
| 693 | return HRTIMER_NORESTART; |
| 694 | } |
| 695 | |
| 696 | static void null_cmd_end_timer(struct nullb_cmd *cmd) |
| 697 | { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 698 | ktime_t kt = cmd->nq->dev->completion_nsec; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 699 | |
Paolo Valente | 3c395a9 | 2015-12-01 11:48:17 +0100 | [diff] [blame] | 700 | hrtimer_start(&cmd->timer, kt, HRTIMER_MODE_REL); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | static void null_softirq_done_fn(struct request *rq) |
| 704 | { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 705 | struct nullb *nullb = rq->q->queuedata; |
| 706 | |
| 707 | if (nullb->dev->queue_mode == NULL_Q_MQ) |
Jens Axboe | d891fa7 | 2014-06-16 11:40:25 -0600 | [diff] [blame] | 708 | end_cmd(blk_mq_rq_to_pdu(rq)); |
| 709 | else |
| 710 | end_cmd(rq->special); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 711 | } |
| 712 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 713 | static struct nullb_page *null_alloc_page(gfp_t gfp_flags) |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 714 | { |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 715 | struct nullb_page *t_page; |
| 716 | |
| 717 | t_page = kmalloc(sizeof(struct nullb_page), gfp_flags); |
| 718 | if (!t_page) |
| 719 | goto out; |
| 720 | |
| 721 | t_page->page = alloc_pages(gfp_flags, 0); |
| 722 | if (!t_page->page) |
| 723 | goto out_freepage; |
| 724 | |
| 725 | t_page->bitmap = 0; |
| 726 | return t_page; |
| 727 | out_freepage: |
| 728 | kfree(t_page); |
| 729 | out: |
| 730 | return NULL; |
| 731 | } |
| 732 | |
| 733 | static void null_free_page(struct nullb_page *t_page) |
| 734 | { |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 735 | __set_bit(NULLB_PAGE_FREE, &t_page->bitmap); |
| 736 | if (test_bit(NULLB_PAGE_LOCK, &t_page->bitmap)) |
| 737 | return; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 738 | __free_page(t_page->page); |
| 739 | kfree(t_page); |
| 740 | } |
| 741 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 742 | static void null_free_sector(struct nullb *nullb, sector_t sector, |
| 743 | bool is_cache) |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 744 | { |
| 745 | unsigned int sector_bit; |
| 746 | u64 idx; |
| 747 | struct nullb_page *t_page, *ret; |
| 748 | struct radix_tree_root *root; |
| 749 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 750 | root = is_cache ? &nullb->dev->cache : &nullb->dev->data; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 751 | idx = sector >> PAGE_SECTORS_SHIFT; |
| 752 | sector_bit = (sector & SECTOR_MASK); |
| 753 | |
| 754 | t_page = radix_tree_lookup(root, idx); |
| 755 | if (t_page) { |
| 756 | __clear_bit(sector_bit, &t_page->bitmap); |
| 757 | |
| 758 | if (!t_page->bitmap) { |
| 759 | ret = radix_tree_delete_item(root, idx, t_page); |
| 760 | WARN_ON(ret != t_page); |
| 761 | null_free_page(ret); |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 762 | if (is_cache) |
| 763 | nullb->dev->curr_cache -= PAGE_SIZE; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | static struct nullb_page *null_radix_tree_insert(struct nullb *nullb, u64 idx, |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 769 | struct nullb_page *t_page, bool is_cache) |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 770 | { |
| 771 | struct radix_tree_root *root; |
| 772 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 773 | root = is_cache ? &nullb->dev->cache : &nullb->dev->data; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 774 | |
| 775 | if (radix_tree_insert(root, idx, t_page)) { |
| 776 | null_free_page(t_page); |
| 777 | t_page = radix_tree_lookup(root, idx); |
| 778 | WARN_ON(!t_page || t_page->page->index != idx); |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 779 | } else if (is_cache) |
| 780 | nullb->dev->curr_cache += PAGE_SIZE; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 781 | |
| 782 | return t_page; |
| 783 | } |
| 784 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 785 | static void null_free_device_storage(struct nullb_device *dev, bool is_cache) |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 786 | { |
| 787 | unsigned long pos = 0; |
| 788 | int nr_pages; |
| 789 | struct nullb_page *ret, *t_pages[FREE_BATCH]; |
| 790 | struct radix_tree_root *root; |
| 791 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 792 | root = is_cache ? &dev->cache : &dev->data; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 793 | |
| 794 | do { |
| 795 | int i; |
| 796 | |
| 797 | nr_pages = radix_tree_gang_lookup(root, |
| 798 | (void **)t_pages, pos, FREE_BATCH); |
| 799 | |
| 800 | for (i = 0; i < nr_pages; i++) { |
| 801 | pos = t_pages[i]->page->index; |
| 802 | ret = radix_tree_delete_item(root, pos, t_pages[i]); |
| 803 | WARN_ON(ret != t_pages[i]); |
| 804 | null_free_page(ret); |
| 805 | } |
| 806 | |
| 807 | pos++; |
| 808 | } while (nr_pages == FREE_BATCH); |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 809 | |
| 810 | if (is_cache) |
| 811 | dev->curr_cache = 0; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 812 | } |
| 813 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 814 | static struct nullb_page *__null_lookup_page(struct nullb *nullb, |
| 815 | sector_t sector, bool for_write, bool is_cache) |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 816 | { |
| 817 | unsigned int sector_bit; |
| 818 | u64 idx; |
| 819 | struct nullb_page *t_page; |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 820 | struct radix_tree_root *root; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 821 | |
| 822 | idx = sector >> PAGE_SECTORS_SHIFT; |
| 823 | sector_bit = (sector & SECTOR_MASK); |
| 824 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 825 | root = is_cache ? &nullb->dev->cache : &nullb->dev->data; |
| 826 | t_page = radix_tree_lookup(root, idx); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 827 | WARN_ON(t_page && t_page->page->index != idx); |
| 828 | |
| 829 | if (t_page && (for_write || test_bit(sector_bit, &t_page->bitmap))) |
| 830 | return t_page; |
| 831 | |
| 832 | return NULL; |
| 833 | } |
| 834 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 835 | static struct nullb_page *null_lookup_page(struct nullb *nullb, |
| 836 | sector_t sector, bool for_write, bool ignore_cache) |
| 837 | { |
| 838 | struct nullb_page *page = NULL; |
| 839 | |
| 840 | if (!ignore_cache) |
| 841 | page = __null_lookup_page(nullb, sector, for_write, true); |
| 842 | if (page) |
| 843 | return page; |
| 844 | return __null_lookup_page(nullb, sector, for_write, false); |
| 845 | } |
| 846 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 847 | static struct nullb_page *null_insert_page(struct nullb *nullb, |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 848 | sector_t sector, bool ignore_cache) |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 849 | { |
| 850 | u64 idx; |
| 851 | struct nullb_page *t_page; |
| 852 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 853 | t_page = null_lookup_page(nullb, sector, true, ignore_cache); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 854 | if (t_page) |
| 855 | return t_page; |
| 856 | |
| 857 | spin_unlock_irq(&nullb->lock); |
| 858 | |
| 859 | t_page = null_alloc_page(GFP_NOIO); |
| 860 | if (!t_page) |
| 861 | goto out_lock; |
| 862 | |
| 863 | if (radix_tree_preload(GFP_NOIO)) |
| 864 | goto out_freepage; |
| 865 | |
| 866 | spin_lock_irq(&nullb->lock); |
| 867 | idx = sector >> PAGE_SECTORS_SHIFT; |
| 868 | t_page->page->index = idx; |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 869 | t_page = null_radix_tree_insert(nullb, idx, t_page, !ignore_cache); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 870 | radix_tree_preload_end(); |
| 871 | |
| 872 | return t_page; |
| 873 | out_freepage: |
| 874 | null_free_page(t_page); |
| 875 | out_lock: |
| 876 | spin_lock_irq(&nullb->lock); |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 877 | return null_lookup_page(nullb, sector, true, ignore_cache); |
| 878 | } |
| 879 | |
| 880 | static int null_flush_cache_page(struct nullb *nullb, struct nullb_page *c_page) |
| 881 | { |
| 882 | int i; |
| 883 | unsigned int offset; |
| 884 | u64 idx; |
| 885 | struct nullb_page *t_page, *ret; |
| 886 | void *dst, *src; |
| 887 | |
| 888 | idx = c_page->page->index; |
| 889 | |
| 890 | t_page = null_insert_page(nullb, idx << PAGE_SECTORS_SHIFT, true); |
| 891 | |
| 892 | __clear_bit(NULLB_PAGE_LOCK, &c_page->bitmap); |
| 893 | if (test_bit(NULLB_PAGE_FREE, &c_page->bitmap)) { |
| 894 | null_free_page(c_page); |
| 895 | if (t_page && t_page->bitmap == 0) { |
| 896 | ret = radix_tree_delete_item(&nullb->dev->data, |
| 897 | idx, t_page); |
| 898 | null_free_page(t_page); |
| 899 | } |
| 900 | return 0; |
| 901 | } |
| 902 | |
| 903 | if (!t_page) |
| 904 | return -ENOMEM; |
| 905 | |
| 906 | src = kmap_atomic(c_page->page); |
| 907 | dst = kmap_atomic(t_page->page); |
| 908 | |
| 909 | for (i = 0; i < PAGE_SECTORS; |
| 910 | i += (nullb->dev->blocksize >> SECTOR_SHIFT)) { |
| 911 | if (test_bit(i, &c_page->bitmap)) { |
| 912 | offset = (i << SECTOR_SHIFT); |
| 913 | memcpy(dst + offset, src + offset, |
| 914 | nullb->dev->blocksize); |
| 915 | __set_bit(i, &t_page->bitmap); |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | kunmap_atomic(dst); |
| 920 | kunmap_atomic(src); |
| 921 | |
| 922 | ret = radix_tree_delete_item(&nullb->dev->cache, idx, c_page); |
| 923 | null_free_page(ret); |
| 924 | nullb->dev->curr_cache -= PAGE_SIZE; |
| 925 | |
| 926 | return 0; |
| 927 | } |
| 928 | |
| 929 | static int null_make_cache_space(struct nullb *nullb, unsigned long n) |
| 930 | { |
| 931 | int i, err, nr_pages; |
| 932 | struct nullb_page *c_pages[FREE_BATCH]; |
| 933 | unsigned long flushed = 0, one_round; |
| 934 | |
| 935 | again: |
| 936 | if ((nullb->dev->cache_size * 1024 * 1024) > |
| 937 | nullb->dev->curr_cache + n || nullb->dev->curr_cache == 0) |
| 938 | return 0; |
| 939 | |
| 940 | nr_pages = radix_tree_gang_lookup(&nullb->dev->cache, |
| 941 | (void **)c_pages, nullb->cache_flush_pos, FREE_BATCH); |
| 942 | /* |
| 943 | * nullb_flush_cache_page could unlock before using the c_pages. To |
| 944 | * avoid race, we don't allow page free |
| 945 | */ |
| 946 | for (i = 0; i < nr_pages; i++) { |
| 947 | nullb->cache_flush_pos = c_pages[i]->page->index; |
| 948 | /* |
| 949 | * We found the page which is being flushed to disk by other |
| 950 | * threads |
| 951 | */ |
| 952 | if (test_bit(NULLB_PAGE_LOCK, &c_pages[i]->bitmap)) |
| 953 | c_pages[i] = NULL; |
| 954 | else |
| 955 | __set_bit(NULLB_PAGE_LOCK, &c_pages[i]->bitmap); |
| 956 | } |
| 957 | |
| 958 | one_round = 0; |
| 959 | for (i = 0; i < nr_pages; i++) { |
| 960 | if (c_pages[i] == NULL) |
| 961 | continue; |
| 962 | err = null_flush_cache_page(nullb, c_pages[i]); |
| 963 | if (err) |
| 964 | return err; |
| 965 | one_round++; |
| 966 | } |
| 967 | flushed += one_round << PAGE_SHIFT; |
| 968 | |
| 969 | if (n > flushed) { |
| 970 | if (nr_pages == 0) |
| 971 | nullb->cache_flush_pos = 0; |
| 972 | if (one_round == 0) { |
| 973 | /* give other threads a chance */ |
| 974 | spin_unlock_irq(&nullb->lock); |
| 975 | spin_lock_irq(&nullb->lock); |
| 976 | } |
| 977 | goto again; |
| 978 | } |
| 979 | return 0; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | static int copy_to_nullb(struct nullb *nullb, struct page *source, |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 983 | unsigned int off, sector_t sector, size_t n, bool is_fua) |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 984 | { |
| 985 | size_t temp, count = 0; |
| 986 | unsigned int offset; |
| 987 | struct nullb_page *t_page; |
| 988 | void *dst, *src; |
| 989 | |
| 990 | while (count < n) { |
| 991 | temp = min_t(size_t, nullb->dev->blocksize, n - count); |
| 992 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 993 | if (null_cache_active(nullb) && !is_fua) |
| 994 | null_make_cache_space(nullb, PAGE_SIZE); |
| 995 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 996 | offset = (sector & SECTOR_MASK) << SECTOR_SHIFT; |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 997 | t_page = null_insert_page(nullb, sector, |
| 998 | !null_cache_active(nullb) || is_fua); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 999 | if (!t_page) |
| 1000 | return -ENOSPC; |
| 1001 | |
| 1002 | src = kmap_atomic(source); |
| 1003 | dst = kmap_atomic(t_page->page); |
| 1004 | memcpy(dst + offset, src + off + count, temp); |
| 1005 | kunmap_atomic(dst); |
| 1006 | kunmap_atomic(src); |
| 1007 | |
| 1008 | __set_bit(sector & SECTOR_MASK, &t_page->bitmap); |
| 1009 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1010 | if (is_fua) |
| 1011 | null_free_sector(nullb, sector, true); |
| 1012 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1013 | count += temp; |
| 1014 | sector += temp >> SECTOR_SHIFT; |
| 1015 | } |
| 1016 | return 0; |
| 1017 | } |
| 1018 | |
| 1019 | static int copy_from_nullb(struct nullb *nullb, struct page *dest, |
| 1020 | unsigned int off, sector_t sector, size_t n) |
| 1021 | { |
| 1022 | size_t temp, count = 0; |
| 1023 | unsigned int offset; |
| 1024 | struct nullb_page *t_page; |
| 1025 | void *dst, *src; |
| 1026 | |
| 1027 | while (count < n) { |
| 1028 | temp = min_t(size_t, nullb->dev->blocksize, n - count); |
| 1029 | |
| 1030 | offset = (sector & SECTOR_MASK) << SECTOR_SHIFT; |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1031 | t_page = null_lookup_page(nullb, sector, false, |
| 1032 | !null_cache_active(nullb)); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1033 | |
| 1034 | dst = kmap_atomic(dest); |
| 1035 | if (!t_page) { |
| 1036 | memset(dst + off + count, 0, temp); |
| 1037 | goto next; |
| 1038 | } |
| 1039 | src = kmap_atomic(t_page->page); |
| 1040 | memcpy(dst + off + count, src + offset, temp); |
| 1041 | kunmap_atomic(src); |
| 1042 | next: |
| 1043 | kunmap_atomic(dst); |
| 1044 | |
| 1045 | count += temp; |
| 1046 | sector += temp >> SECTOR_SHIFT; |
| 1047 | } |
| 1048 | return 0; |
| 1049 | } |
| 1050 | |
Shaohua Li | 306eb6b | 2017-08-14 15:04:57 -0700 | [diff] [blame] | 1051 | static void null_handle_discard(struct nullb *nullb, sector_t sector, size_t n) |
| 1052 | { |
| 1053 | size_t temp; |
| 1054 | |
| 1055 | spin_lock_irq(&nullb->lock); |
| 1056 | while (n > 0) { |
| 1057 | temp = min_t(size_t, n, nullb->dev->blocksize); |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1058 | null_free_sector(nullb, sector, false); |
| 1059 | if (null_cache_active(nullb)) |
| 1060 | null_free_sector(nullb, sector, true); |
Shaohua Li | 306eb6b | 2017-08-14 15:04:57 -0700 | [diff] [blame] | 1061 | sector += temp >> SECTOR_SHIFT; |
| 1062 | n -= temp; |
| 1063 | } |
| 1064 | spin_unlock_irq(&nullb->lock); |
| 1065 | } |
| 1066 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1067 | static int null_handle_flush(struct nullb *nullb) |
| 1068 | { |
| 1069 | int err; |
| 1070 | |
| 1071 | if (!null_cache_active(nullb)) |
| 1072 | return 0; |
| 1073 | |
| 1074 | spin_lock_irq(&nullb->lock); |
| 1075 | while (true) { |
| 1076 | err = null_make_cache_space(nullb, |
| 1077 | nullb->dev->cache_size * 1024 * 1024); |
| 1078 | if (err || nullb->dev->curr_cache == 0) |
| 1079 | break; |
| 1080 | } |
| 1081 | |
| 1082 | WARN_ON(!radix_tree_empty(&nullb->dev->cache)); |
| 1083 | spin_unlock_irq(&nullb->lock); |
| 1084 | return err; |
| 1085 | } |
| 1086 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1087 | static int null_transfer(struct nullb *nullb, struct page *page, |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1088 | unsigned int len, unsigned int off, bool is_write, sector_t sector, |
| 1089 | bool is_fua) |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1090 | { |
| 1091 | int err = 0; |
| 1092 | |
| 1093 | if (!is_write) { |
| 1094 | err = copy_from_nullb(nullb, page, off, sector, len); |
| 1095 | flush_dcache_page(page); |
| 1096 | } else { |
| 1097 | flush_dcache_page(page); |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1098 | err = copy_to_nullb(nullb, page, off, sector, len, is_fua); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | return err; |
| 1102 | } |
| 1103 | |
| 1104 | static int null_handle_rq(struct nullb_cmd *cmd) |
| 1105 | { |
| 1106 | struct request *rq = cmd->rq; |
| 1107 | struct nullb *nullb = cmd->nq->dev->nullb; |
| 1108 | int err; |
| 1109 | unsigned int len; |
| 1110 | sector_t sector; |
| 1111 | struct req_iterator iter; |
| 1112 | struct bio_vec bvec; |
| 1113 | |
| 1114 | sector = blk_rq_pos(rq); |
| 1115 | |
Shaohua Li | 306eb6b | 2017-08-14 15:04:57 -0700 | [diff] [blame] | 1116 | if (req_op(rq) == REQ_OP_DISCARD) { |
| 1117 | null_handle_discard(nullb, sector, blk_rq_bytes(rq)); |
| 1118 | return 0; |
| 1119 | } |
| 1120 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1121 | spin_lock_irq(&nullb->lock); |
| 1122 | rq_for_each_segment(bvec, rq, iter) { |
| 1123 | len = bvec.bv_len; |
| 1124 | err = null_transfer(nullb, bvec.bv_page, len, bvec.bv_offset, |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1125 | op_is_write(req_op(rq)), sector, |
| 1126 | req_op(rq) & REQ_FUA); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1127 | if (err) { |
| 1128 | spin_unlock_irq(&nullb->lock); |
| 1129 | return err; |
| 1130 | } |
| 1131 | sector += len >> SECTOR_SHIFT; |
| 1132 | } |
| 1133 | spin_unlock_irq(&nullb->lock); |
| 1134 | |
| 1135 | return 0; |
| 1136 | } |
| 1137 | |
| 1138 | static int null_handle_bio(struct nullb_cmd *cmd) |
| 1139 | { |
| 1140 | struct bio *bio = cmd->bio; |
| 1141 | struct nullb *nullb = cmd->nq->dev->nullb; |
| 1142 | int err; |
| 1143 | unsigned int len; |
| 1144 | sector_t sector; |
| 1145 | struct bio_vec bvec; |
| 1146 | struct bvec_iter iter; |
| 1147 | |
| 1148 | sector = bio->bi_iter.bi_sector; |
| 1149 | |
Shaohua Li | 306eb6b | 2017-08-14 15:04:57 -0700 | [diff] [blame] | 1150 | if (bio_op(bio) == REQ_OP_DISCARD) { |
| 1151 | null_handle_discard(nullb, sector, |
| 1152 | bio_sectors(bio) << SECTOR_SHIFT); |
| 1153 | return 0; |
| 1154 | } |
| 1155 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1156 | spin_lock_irq(&nullb->lock); |
| 1157 | bio_for_each_segment(bvec, bio, iter) { |
| 1158 | len = bvec.bv_len; |
| 1159 | err = null_transfer(nullb, bvec.bv_page, len, bvec.bv_offset, |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1160 | op_is_write(bio_op(bio)), sector, |
| 1161 | bio_op(bio) & REQ_FUA); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1162 | if (err) { |
| 1163 | spin_unlock_irq(&nullb->lock); |
| 1164 | return err; |
| 1165 | } |
| 1166 | sector += len >> SECTOR_SHIFT; |
| 1167 | } |
| 1168 | spin_unlock_irq(&nullb->lock); |
| 1169 | return 0; |
| 1170 | } |
| 1171 | |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 1172 | static void null_stop_queue(struct nullb *nullb) |
| 1173 | { |
| 1174 | struct request_queue *q = nullb->q; |
| 1175 | |
| 1176 | if (nullb->dev->queue_mode == NULL_Q_MQ) |
| 1177 | blk_mq_stop_hw_queues(q); |
| 1178 | else { |
| 1179 | spin_lock_irq(q->queue_lock); |
| 1180 | blk_stop_queue(q); |
| 1181 | spin_unlock_irq(q->queue_lock); |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | static void null_restart_queue_async(struct nullb *nullb) |
| 1186 | { |
| 1187 | struct request_queue *q = nullb->q; |
| 1188 | unsigned long flags; |
| 1189 | |
| 1190 | if (nullb->dev->queue_mode == NULL_Q_MQ) |
| 1191 | blk_mq_start_stopped_hw_queues(q, true); |
| 1192 | else { |
| 1193 | spin_lock_irqsave(q->queue_lock, flags); |
| 1194 | blk_start_queue_async(q); |
| 1195 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 1196 | } |
| 1197 | } |
| 1198 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1199 | static blk_status_t null_handle_cmd(struct nullb_cmd *cmd) |
| 1200 | { |
| 1201 | struct nullb_device *dev = cmd->nq->dev; |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 1202 | struct nullb *nullb = dev->nullb; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1203 | int err = 0; |
| 1204 | |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 1205 | if (test_bit(NULLB_DEV_FL_THROTTLED, &dev->flags)) { |
| 1206 | struct request *rq = cmd->rq; |
| 1207 | |
| 1208 | if (!hrtimer_active(&nullb->bw_timer)) |
| 1209 | hrtimer_restart(&nullb->bw_timer); |
| 1210 | |
| 1211 | if (atomic_long_sub_return(blk_rq_bytes(rq), |
| 1212 | &nullb->cur_bytes) < 0) { |
| 1213 | null_stop_queue(nullb); |
| 1214 | /* race with timer */ |
| 1215 | if (atomic_long_read(&nullb->cur_bytes) > 0) |
| 1216 | null_restart_queue_async(nullb); |
| 1217 | if (dev->queue_mode == NULL_Q_RQ) { |
| 1218 | struct request_queue *q = nullb->q; |
| 1219 | |
| 1220 | spin_lock_irq(q->queue_lock); |
| 1221 | rq->rq_flags |= RQF_DONTPREP; |
| 1222 | blk_requeue_request(q, rq); |
| 1223 | spin_unlock_irq(q->queue_lock); |
| 1224 | return BLK_STS_OK; |
| 1225 | } else |
| 1226 | /* requeue request */ |
| 1227 | return BLK_STS_RESOURCE; |
| 1228 | } |
| 1229 | } |
| 1230 | |
Shaohua Li | 2f54a61 | 2017-08-14 15:05:00 -0700 | [diff] [blame] | 1231 | if (nullb->dev->badblocks.shift != -1) { |
| 1232 | int bad_sectors; |
| 1233 | sector_t sector, size, first_bad; |
| 1234 | bool is_flush = true; |
| 1235 | |
| 1236 | if (dev->queue_mode == NULL_Q_BIO && |
| 1237 | bio_op(cmd->bio) != REQ_OP_FLUSH) { |
| 1238 | is_flush = false; |
| 1239 | sector = cmd->bio->bi_iter.bi_sector; |
| 1240 | size = bio_sectors(cmd->bio); |
| 1241 | } |
| 1242 | if (dev->queue_mode != NULL_Q_BIO && |
| 1243 | req_op(cmd->rq) != REQ_OP_FLUSH) { |
| 1244 | is_flush = false; |
| 1245 | sector = blk_rq_pos(cmd->rq); |
| 1246 | size = blk_rq_sectors(cmd->rq); |
| 1247 | } |
| 1248 | if (!is_flush && badblocks_check(&nullb->dev->badblocks, sector, |
| 1249 | size, &first_bad, &bad_sectors)) { |
| 1250 | cmd->error = BLK_STS_IOERR; |
| 1251 | goto out; |
| 1252 | } |
| 1253 | } |
| 1254 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1255 | if (dev->memory_backed) { |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1256 | if (dev->queue_mode == NULL_Q_BIO) { |
| 1257 | if (bio_op(cmd->bio) == REQ_OP_FLUSH) |
| 1258 | err = null_handle_flush(nullb); |
| 1259 | else |
| 1260 | err = null_handle_bio(cmd); |
| 1261 | } else { |
| 1262 | if (req_op(cmd->rq) == REQ_OP_FLUSH) |
| 1263 | err = null_handle_flush(nullb); |
| 1264 | else |
| 1265 | err = null_handle_rq(cmd); |
| 1266 | } |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1267 | } |
| 1268 | cmd->error = errno_to_blk_status(err); |
Shaohua Li | 2f54a61 | 2017-08-14 15:05:00 -0700 | [diff] [blame] | 1269 | out: |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1270 | /* Complete IO by inline, softirq or timer */ |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1271 | switch (dev->irqmode) { |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 1272 | case NULL_IRQ_SOFTIRQ: |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1273 | switch (dev->queue_mode) { |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 1274 | case NULL_Q_MQ: |
Christoph Hellwig | 08e0029 | 2017-04-20 16:03:09 +0200 | [diff] [blame] | 1275 | blk_mq_complete_request(cmd->rq); |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 1276 | break; |
| 1277 | case NULL_Q_RQ: |
| 1278 | blk_complete_request(cmd->rq); |
| 1279 | break; |
| 1280 | case NULL_Q_BIO: |
| 1281 | /* |
| 1282 | * XXX: no proper submitting cpu information available. |
| 1283 | */ |
| 1284 | end_cmd(cmd); |
| 1285 | break; |
| 1286 | } |
| 1287 | break; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1288 | case NULL_IRQ_NONE: |
| 1289 | end_cmd(cmd); |
| 1290 | break; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1291 | case NULL_IRQ_TIMER: |
| 1292 | null_cmd_end_timer(cmd); |
| 1293 | break; |
| 1294 | } |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1295 | return BLK_STS_OK; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1296 | } |
| 1297 | |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 1298 | static enum hrtimer_restart nullb_bwtimer_fn(struct hrtimer *timer) |
| 1299 | { |
| 1300 | struct nullb *nullb = container_of(timer, struct nullb, bw_timer); |
| 1301 | ktime_t timer_interval = ktime_set(0, TIMER_INTERVAL); |
| 1302 | unsigned int mbps = nullb->dev->mbps; |
| 1303 | |
| 1304 | if (atomic_long_read(&nullb->cur_bytes) == mb_per_tick(mbps)) |
| 1305 | return HRTIMER_NORESTART; |
| 1306 | |
| 1307 | atomic_long_set(&nullb->cur_bytes, mb_per_tick(mbps)); |
| 1308 | null_restart_queue_async(nullb); |
| 1309 | |
| 1310 | hrtimer_forward_now(&nullb->bw_timer, timer_interval); |
| 1311 | |
| 1312 | return HRTIMER_RESTART; |
| 1313 | } |
| 1314 | |
| 1315 | static void nullb_setup_bwtimer(struct nullb *nullb) |
| 1316 | { |
| 1317 | ktime_t timer_interval = ktime_set(0, TIMER_INTERVAL); |
| 1318 | |
| 1319 | hrtimer_init(&nullb->bw_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 1320 | nullb->bw_timer.function = nullb_bwtimer_fn; |
| 1321 | atomic_long_set(&nullb->cur_bytes, mb_per_tick(nullb->dev->mbps)); |
| 1322 | hrtimer_start(&nullb->bw_timer, timer_interval, HRTIMER_MODE_REL); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | static struct nullb_queue *nullb_to_queue(struct nullb *nullb) |
| 1326 | { |
| 1327 | int index = 0; |
| 1328 | |
| 1329 | if (nullb->nr_queues != 1) |
| 1330 | index = raw_smp_processor_id() / ((nr_cpu_ids + nullb->nr_queues - 1) / nullb->nr_queues); |
| 1331 | |
| 1332 | return &nullb->queues[index]; |
| 1333 | } |
| 1334 | |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 1335 | static blk_qc_t null_queue_bio(struct request_queue *q, struct bio *bio) |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1336 | { |
| 1337 | struct nullb *nullb = q->queuedata; |
| 1338 | struct nullb_queue *nq = nullb_to_queue(nullb); |
| 1339 | struct nullb_cmd *cmd; |
| 1340 | |
| 1341 | cmd = alloc_cmd(nq, 1); |
| 1342 | cmd->bio = bio; |
| 1343 | |
| 1344 | null_handle_cmd(cmd); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 1345 | return BLK_QC_T_NONE; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | static int null_rq_prep_fn(struct request_queue *q, struct request *req) |
| 1349 | { |
| 1350 | struct nullb *nullb = q->queuedata; |
| 1351 | struct nullb_queue *nq = nullb_to_queue(nullb); |
| 1352 | struct nullb_cmd *cmd; |
| 1353 | |
| 1354 | cmd = alloc_cmd(nq, 0); |
| 1355 | if (cmd) { |
| 1356 | cmd->rq = req; |
| 1357 | req->special = cmd; |
| 1358 | return BLKPREP_OK; |
| 1359 | } |
Akinobu Mita | 8b70f45 | 2015-06-02 08:35:10 +0900 | [diff] [blame] | 1360 | blk_stop_queue(q); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1361 | |
| 1362 | return BLKPREP_DEFER; |
| 1363 | } |
| 1364 | |
| 1365 | static void null_request_fn(struct request_queue *q) |
| 1366 | { |
| 1367 | struct request *rq; |
| 1368 | |
| 1369 | while ((rq = blk_fetch_request(q)) != NULL) { |
| 1370 | struct nullb_cmd *cmd = rq->special; |
| 1371 | |
| 1372 | spin_unlock_irq(q->queue_lock); |
| 1373 | null_handle_cmd(cmd); |
| 1374 | spin_lock_irq(q->queue_lock); |
| 1375 | } |
| 1376 | } |
| 1377 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 1378 | static blk_status_t null_queue_rq(struct blk_mq_hw_ctx *hctx, |
Jens Axboe | 74c4505 | 2014-10-29 11:14:52 -0600 | [diff] [blame] | 1379 | const struct blk_mq_queue_data *bd) |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1380 | { |
Jens Axboe | 74c4505 | 2014-10-29 11:14:52 -0600 | [diff] [blame] | 1381 | struct nullb_cmd *cmd = blk_mq_rq_to_pdu(bd->rq); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1382 | struct nullb_queue *nq = hctx->driver_data; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1383 | |
Jens Axboe | db5bcf8 | 2017-03-30 13:44:26 -0600 | [diff] [blame] | 1384 | might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING); |
| 1385 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1386 | if (nq->dev->irqmode == NULL_IRQ_TIMER) { |
Paolo Valente | 3c395a9 | 2015-12-01 11:48:17 +0100 | [diff] [blame] | 1387 | hrtimer_init(&cmd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 1388 | cmd->timer.function = null_cmd_timer_expired; |
| 1389 | } |
Jens Axboe | 74c4505 | 2014-10-29 11:14:52 -0600 | [diff] [blame] | 1390 | cmd->rq = bd->rq; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1391 | cmd->nq = nq; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1392 | |
Jens Axboe | 74c4505 | 2014-10-29 11:14:52 -0600 | [diff] [blame] | 1393 | blk_mq_start_request(bd->rq); |
Christoph Hellwig | e249007 | 2014-09-13 16:40:09 -0700 | [diff] [blame] | 1394 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1395 | return null_handle_cmd(cmd); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1396 | } |
| 1397 | |
Eric Biggers | f363b08 | 2017-03-30 13:39:16 -0700 | [diff] [blame] | 1398 | static const struct blk_mq_ops null_mq_ops = { |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1399 | .queue_rq = null_queue_rq, |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 1400 | .complete = null_softirq_done_fn, |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1401 | }; |
| 1402 | |
Matias Bjørling | de65d2d | 2015-08-31 14:17:18 +0200 | [diff] [blame] | 1403 | static void cleanup_queue(struct nullb_queue *nq) |
| 1404 | { |
| 1405 | kfree(nq->tag_map); |
| 1406 | kfree(nq->cmds); |
| 1407 | } |
| 1408 | |
| 1409 | static void cleanup_queues(struct nullb *nullb) |
| 1410 | { |
| 1411 | int i; |
| 1412 | |
| 1413 | for (i = 0; i < nullb->nr_queues; i++) |
| 1414 | cleanup_queue(&nullb->queues[i]); |
| 1415 | |
| 1416 | kfree(nullb->queues); |
| 1417 | } |
| 1418 | |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1419 | #ifdef CONFIG_NVM |
| 1420 | |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1421 | static void null_lnvm_end_io(struct request *rq, blk_status_t status) |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1422 | { |
| 1423 | struct nvm_rq *rqd = rq->end_io_data; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1424 | |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1425 | /* XXX: lighnvm core seems to expect NVM_RSP_* values here.. */ |
| 1426 | rqd->error = status ? -EIO : 0; |
Matias Bjørling | 06894ef | 2017-01-31 13:17:17 +0100 | [diff] [blame] | 1427 | nvm_end_io(rqd); |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1428 | |
| 1429 | blk_put_request(rq); |
| 1430 | } |
| 1431 | |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 1432 | static int null_lnvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd) |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1433 | { |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 1434 | struct request_queue *q = dev->q; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1435 | struct request *rq; |
| 1436 | struct bio *bio = rqd->bio; |
| 1437 | |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 1438 | rq = blk_mq_alloc_request(q, |
| 1439 | op_is_write(bio_op(bio)) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0); |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1440 | if (IS_ERR(rq)) |
| 1441 | return -ENOMEM; |
| 1442 | |
Bart Van Assche | 2644a3c | 2017-04-19 14:01:25 -0700 | [diff] [blame] | 1443 | blk_init_request_from_bio(rq, bio); |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1444 | |
| 1445 | rq->end_io_data = rqd; |
| 1446 | |
| 1447 | blk_execute_rq_nowait(q, NULL, rq, 0, null_lnvm_end_io); |
| 1448 | |
| 1449 | return 0; |
| 1450 | } |
| 1451 | |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 1452 | static int null_lnvm_id(struct nvm_dev *dev, struct nvm_id *id) |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1453 | { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1454 | struct nullb *nullb = dev->q->queuedata; |
| 1455 | sector_t size = (sector_t)nullb->dev->size * 1024 * 1024ULL; |
Matias Bjørling | 5b40db9 | 2015-11-19 12:50:09 +0100 | [diff] [blame] | 1456 | sector_t blksize; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1457 | struct nvm_id_group *grp; |
| 1458 | |
| 1459 | id->ver_id = 0x1; |
| 1460 | id->vmnt = 0; |
Matias Bjørling | bf64318 | 2016-02-04 15:13:27 +0100 | [diff] [blame] | 1461 | id->cap = 0x2; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1462 | id->dom = 0x1; |
Matias Bjørling | 5b40db9 | 2015-11-19 12:50:09 +0100 | [diff] [blame] | 1463 | |
| 1464 | id->ppaf.blk_offset = 0; |
| 1465 | id->ppaf.blk_len = 16; |
| 1466 | id->ppaf.pg_offset = 16; |
| 1467 | id->ppaf.pg_len = 16; |
| 1468 | id->ppaf.sect_offset = 32; |
| 1469 | id->ppaf.sect_len = 8; |
| 1470 | id->ppaf.pln_offset = 40; |
| 1471 | id->ppaf.pln_len = 8; |
| 1472 | id->ppaf.lun_offset = 48; |
| 1473 | id->ppaf.lun_len = 8; |
| 1474 | id->ppaf.ch_offset = 56; |
| 1475 | id->ppaf.ch_len = 8; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1476 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1477 | sector_div(size, nullb->dev->blocksize); /* convert size to pages */ |
Arnd Bergmann | e93d12a | 2016-01-13 23:04:08 +0100 | [diff] [blame] | 1478 | size >>= 8; /* concert size to pgs pr blk */ |
Matias Bjørling | 19bd6fe | 2017-01-31 13:17:15 +0100 | [diff] [blame] | 1479 | grp = &id->grp; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1480 | grp->mtype = 0; |
Matias Bjørling | 5b40db9 | 2015-11-19 12:50:09 +0100 | [diff] [blame] | 1481 | grp->fmtype = 0; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1482 | grp->num_ch = 1; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1483 | grp->num_pg = 256; |
Matias Bjørling | 5b40db9 | 2015-11-19 12:50:09 +0100 | [diff] [blame] | 1484 | blksize = size; |
Arnd Bergmann | e93d12a | 2016-01-13 23:04:08 +0100 | [diff] [blame] | 1485 | size >>= 16; |
Matias Bjørling | 5b40db9 | 2015-11-19 12:50:09 +0100 | [diff] [blame] | 1486 | grp->num_lun = size + 1; |
Arnd Bergmann | e93d12a | 2016-01-13 23:04:08 +0100 | [diff] [blame] | 1487 | sector_div(blksize, grp->num_lun); |
Matias Bjørling | 5b40db9 | 2015-11-19 12:50:09 +0100 | [diff] [blame] | 1488 | grp->num_blk = blksize; |
| 1489 | grp->num_pln = 1; |
| 1490 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1491 | grp->fpg_sz = nullb->dev->blocksize; |
| 1492 | grp->csecs = nullb->dev->blocksize; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1493 | grp->trdt = 25000; |
| 1494 | grp->trdm = 25000; |
| 1495 | grp->tprt = 500000; |
| 1496 | grp->tprm = 500000; |
| 1497 | grp->tbet = 1500000; |
| 1498 | grp->tbem = 1500000; |
| 1499 | grp->mpos = 0x010101; /* single plane rwe */ |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1500 | grp->cpar = nullb->dev->hw_queue_depth; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1501 | |
| 1502 | return 0; |
| 1503 | } |
| 1504 | |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 1505 | static void *null_lnvm_create_dma_pool(struct nvm_dev *dev, char *name) |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1506 | { |
| 1507 | mempool_t *virtmem_pool; |
| 1508 | |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 1509 | virtmem_pool = mempool_create_slab_pool(64, ppa_cache); |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1510 | if (!virtmem_pool) { |
| 1511 | pr_err("null_blk: Unable to create virtual memory pool\n"); |
| 1512 | return NULL; |
| 1513 | } |
| 1514 | |
| 1515 | return virtmem_pool; |
| 1516 | } |
| 1517 | |
| 1518 | static void null_lnvm_destroy_dma_pool(void *pool) |
| 1519 | { |
| 1520 | mempool_destroy(pool); |
| 1521 | } |
| 1522 | |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 1523 | static void *null_lnvm_dev_dma_alloc(struct nvm_dev *dev, void *pool, |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1524 | gfp_t mem_flags, dma_addr_t *dma_handler) |
| 1525 | { |
| 1526 | return mempool_alloc(pool, mem_flags); |
| 1527 | } |
| 1528 | |
| 1529 | static void null_lnvm_dev_dma_free(void *pool, void *entry, |
| 1530 | dma_addr_t dma_handler) |
| 1531 | { |
| 1532 | mempool_free(entry, pool); |
| 1533 | } |
| 1534 | |
| 1535 | static struct nvm_dev_ops null_lnvm_dev_ops = { |
| 1536 | .identity = null_lnvm_id, |
| 1537 | .submit_io = null_lnvm_submit_io, |
| 1538 | |
| 1539 | .create_dma_pool = null_lnvm_create_dma_pool, |
| 1540 | .destroy_dma_pool = null_lnvm_destroy_dma_pool, |
| 1541 | .dev_dma_alloc = null_lnvm_dev_dma_alloc, |
| 1542 | .dev_dma_free = null_lnvm_dev_dma_free, |
| 1543 | |
| 1544 | /* Simulate nvme protocol restriction */ |
| 1545 | .max_phys_sect = 64, |
| 1546 | }; |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1547 | |
| 1548 | static int null_nvm_register(struct nullb *nullb) |
| 1549 | { |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 1550 | struct nvm_dev *dev; |
| 1551 | int rv; |
| 1552 | |
| 1553 | dev = nvm_alloc_dev(0); |
| 1554 | if (!dev) |
| 1555 | return -ENOMEM; |
| 1556 | |
| 1557 | dev->q = nullb->q; |
| 1558 | memcpy(dev->name, nullb->disk_name, DISK_NAME_LEN); |
| 1559 | dev->ops = &null_lnvm_dev_ops; |
| 1560 | |
| 1561 | rv = nvm_register(dev); |
| 1562 | if (rv) { |
| 1563 | kfree(dev); |
| 1564 | return rv; |
| 1565 | } |
| 1566 | nullb->ndev = dev; |
| 1567 | return 0; |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | static void null_nvm_unregister(struct nullb *nullb) |
| 1571 | { |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 1572 | nvm_unregister(nullb->ndev); |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1573 | } |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1574 | #else |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1575 | static int null_nvm_register(struct nullb *nullb) |
| 1576 | { |
Yasuaki Ishimatsu | 92153d3 | 2016-11-16 08:26:11 -0700 | [diff] [blame] | 1577 | pr_err("null_blk: CONFIG_NVM needs to be enabled for LightNVM\n"); |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1578 | return -EINVAL; |
| 1579 | } |
| 1580 | static void null_nvm_unregister(struct nullb *nullb) {} |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1581 | #endif /* CONFIG_NVM */ |
| 1582 | |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1583 | static void null_del_dev(struct nullb *nullb) |
| 1584 | { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1585 | struct nullb_device *dev = nullb->dev; |
| 1586 | |
Shaohua Li | 94bc02e | 2017-08-14 15:04:55 -0700 | [diff] [blame] | 1587 | ida_simple_remove(&nullb_indexes, nullb->index); |
| 1588 | |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1589 | list_del_init(&nullb->list); |
| 1590 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1591 | if (dev->use_lightnvm) |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1592 | null_nvm_unregister(nullb); |
| 1593 | else |
| 1594 | del_gendisk(nullb->disk); |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 1595 | |
| 1596 | if (test_bit(NULLB_DEV_FL_THROTTLED, &nullb->dev->flags)) { |
| 1597 | hrtimer_cancel(&nullb->bw_timer); |
| 1598 | atomic_long_set(&nullb->cur_bytes, LONG_MAX); |
| 1599 | null_restart_queue_async(nullb); |
| 1600 | } |
| 1601 | |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1602 | blk_cleanup_queue(nullb->q); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1603 | if (dev->queue_mode == NULL_Q_MQ && |
| 1604 | nullb->tag_set == &nullb->__tag_set) |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1605 | blk_mq_free_tag_set(nullb->tag_set); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1606 | if (!dev->use_lightnvm) |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1607 | put_disk(nullb->disk); |
| 1608 | cleanup_queues(nullb); |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1609 | if (null_cache_active(nullb)) |
| 1610 | null_free_device_storage(nullb->dev, true); |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1611 | kfree(nullb); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1612 | dev->nullb = NULL; |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1613 | } |
| 1614 | |
Shaohua Li | 306eb6b | 2017-08-14 15:04:57 -0700 | [diff] [blame] | 1615 | static void null_config_discard(struct nullb *nullb) |
| 1616 | { |
| 1617 | if (nullb->dev->discard == false) |
| 1618 | return; |
| 1619 | nullb->q->limits.discard_granularity = nullb->dev->blocksize; |
| 1620 | nullb->q->limits.discard_alignment = nullb->dev->blocksize; |
| 1621 | blk_queue_max_discard_sectors(nullb->q, UINT_MAX >> 9); |
| 1622 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, nullb->q); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | static int null_open(struct block_device *bdev, fmode_t mode) |
| 1626 | { |
| 1627 | return 0; |
| 1628 | } |
| 1629 | |
| 1630 | static void null_release(struct gendisk *disk, fmode_t mode) |
| 1631 | { |
| 1632 | } |
| 1633 | |
| 1634 | static const struct block_device_operations null_fops = { |
| 1635 | .owner = THIS_MODULE, |
| 1636 | .open = null_open, |
| 1637 | .release = null_release, |
| 1638 | }; |
| 1639 | |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1640 | static void null_init_queue(struct nullb *nullb, struct nullb_queue *nq) |
| 1641 | { |
| 1642 | BUG_ON(!nullb); |
| 1643 | BUG_ON(!nq); |
| 1644 | |
| 1645 | init_waitqueue_head(&nq->wait); |
| 1646 | nq->queue_depth = nullb->queue_depth; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1647 | nq->dev = nullb->dev; |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1648 | } |
| 1649 | |
| 1650 | static void null_init_queues(struct nullb *nullb) |
| 1651 | { |
| 1652 | struct request_queue *q = nullb->q; |
| 1653 | struct blk_mq_hw_ctx *hctx; |
| 1654 | struct nullb_queue *nq; |
| 1655 | int i; |
| 1656 | |
| 1657 | queue_for_each_hw_ctx(q, hctx, i) { |
| 1658 | if (!hctx->nr_ctx || !hctx->tags) |
| 1659 | continue; |
| 1660 | nq = &nullb->queues[i]; |
| 1661 | hctx->driver_data = nq; |
| 1662 | null_init_queue(nullb, nq); |
| 1663 | nullb->nr_queues++; |
| 1664 | } |
| 1665 | } |
| 1666 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1667 | static int setup_commands(struct nullb_queue *nq) |
| 1668 | { |
| 1669 | struct nullb_cmd *cmd; |
| 1670 | int i, tag_size; |
| 1671 | |
| 1672 | nq->cmds = kzalloc(nq->queue_depth * sizeof(*cmd), GFP_KERNEL); |
| 1673 | if (!nq->cmds) |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 1674 | return -ENOMEM; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1675 | |
| 1676 | tag_size = ALIGN(nq->queue_depth, BITS_PER_LONG) / BITS_PER_LONG; |
| 1677 | nq->tag_map = kzalloc(tag_size * sizeof(unsigned long), GFP_KERNEL); |
| 1678 | if (!nq->tag_map) { |
| 1679 | kfree(nq->cmds); |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 1680 | return -ENOMEM; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | for (i = 0; i < nq->queue_depth; i++) { |
| 1684 | cmd = &nq->cmds[i]; |
| 1685 | INIT_LIST_HEAD(&cmd->list); |
| 1686 | cmd->ll_list.next = NULL; |
| 1687 | cmd->tag = -1U; |
| 1688 | } |
| 1689 | |
| 1690 | return 0; |
| 1691 | } |
| 1692 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1693 | static int setup_queues(struct nullb *nullb) |
| 1694 | { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1695 | nullb->queues = kzalloc(nullb->dev->submit_queues * |
| 1696 | sizeof(struct nullb_queue), GFP_KERNEL); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1697 | if (!nullb->queues) |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 1698 | return -ENOMEM; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1699 | |
| 1700 | nullb->nr_queues = 0; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1701 | nullb->queue_depth = nullb->dev->hw_queue_depth; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1702 | |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 1703 | return 0; |
| 1704 | } |
| 1705 | |
| 1706 | static int init_driver_queues(struct nullb *nullb) |
| 1707 | { |
| 1708 | struct nullb_queue *nq; |
| 1709 | int i, ret = 0; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1710 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1711 | for (i = 0; i < nullb->dev->submit_queues; i++) { |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1712 | nq = &nullb->queues[i]; |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 1713 | |
| 1714 | null_init_queue(nullb, nq); |
| 1715 | |
| 1716 | ret = setup_commands(nq); |
| 1717 | if (ret) |
Jan Kara | 31f9690 | 2014-10-22 15:34:21 +0200 | [diff] [blame] | 1718 | return ret; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1719 | nullb->nr_queues++; |
| 1720 | } |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 1721 | return 0; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1722 | } |
| 1723 | |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1724 | static int null_gendisk_register(struct nullb *nullb) |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1725 | { |
| 1726 | struct gendisk *disk; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1727 | sector_t size; |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1728 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1729 | disk = nullb->disk = alloc_disk_node(1, nullb->dev->home_node); |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1730 | if (!disk) |
| 1731 | return -ENOMEM; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1732 | size = (sector_t)nullb->dev->size * 1024 * 1024ULL; |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1733 | set_capacity(disk, size >> 9); |
| 1734 | |
| 1735 | disk->flags |= GENHD_FL_EXT_DEVT | GENHD_FL_SUPPRESS_PARTITION_INFO; |
| 1736 | disk->major = null_major; |
| 1737 | disk->first_minor = nullb->index; |
| 1738 | disk->fops = &null_fops; |
| 1739 | disk->private_data = nullb; |
| 1740 | disk->queue = nullb->q; |
| 1741 | strncpy(disk->disk_name, nullb->disk_name, DISK_NAME_LEN); |
| 1742 | |
| 1743 | add_disk(disk); |
| 1744 | return 0; |
| 1745 | } |
| 1746 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1747 | static int null_init_tag_set(struct nullb *nullb, struct blk_mq_tag_set *set) |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1748 | { |
| 1749 | set->ops = &null_mq_ops; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1750 | set->nr_hw_queues = nullb ? nullb->dev->submit_queues : |
| 1751 | g_submit_queues; |
| 1752 | set->queue_depth = nullb ? nullb->dev->hw_queue_depth : |
| 1753 | g_hw_queue_depth; |
| 1754 | set->numa_node = nullb ? nullb->dev->home_node : g_home_node; |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1755 | set->cmd_size = sizeof(struct nullb_cmd); |
| 1756 | set->flags = BLK_MQ_F_SHOULD_MERGE; |
| 1757 | set->driver_data = NULL; |
| 1758 | |
Shaohua Li | 0d06a42 | 2017-08-25 13:46:25 -0700 | [diff] [blame] | 1759 | if ((nullb && nullb->dev->blocking) || g_blocking) |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1760 | set->flags |= BLK_MQ_F_BLOCKING; |
| 1761 | |
| 1762 | return blk_mq_alloc_tag_set(set); |
| 1763 | } |
| 1764 | |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 1765 | static void null_validate_conf(struct nullb_device *dev) |
| 1766 | { |
| 1767 | dev->blocksize = round_down(dev->blocksize, 512); |
| 1768 | dev->blocksize = clamp_t(unsigned int, dev->blocksize, 512, 4096); |
| 1769 | if (dev->use_lightnvm && dev->blocksize != 4096) |
| 1770 | dev->blocksize = 4096; |
| 1771 | |
| 1772 | if (dev->use_lightnvm && dev->queue_mode != NULL_Q_MQ) |
| 1773 | dev->queue_mode = NULL_Q_MQ; |
| 1774 | |
| 1775 | if (dev->queue_mode == NULL_Q_MQ && dev->use_per_node_hctx) { |
| 1776 | if (dev->submit_queues != nr_online_nodes) |
| 1777 | dev->submit_queues = nr_online_nodes; |
| 1778 | } else if (dev->submit_queues > nr_cpu_ids) |
| 1779 | dev->submit_queues = nr_cpu_ids; |
| 1780 | else if (dev->submit_queues == 0) |
| 1781 | dev->submit_queues = 1; |
| 1782 | |
| 1783 | dev->queue_mode = min_t(unsigned int, dev->queue_mode, NULL_Q_MQ); |
| 1784 | dev->irqmode = min_t(unsigned int, dev->irqmode, NULL_IRQ_TIMER); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1785 | |
| 1786 | /* Do memory allocation, so set blocking */ |
| 1787 | if (dev->memory_backed) |
| 1788 | dev->blocking = true; |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1789 | else /* cache is meaningless */ |
| 1790 | dev->cache_size = 0; |
| 1791 | dev->cache_size = min_t(unsigned long, ULONG_MAX / 1024 / 1024, |
| 1792 | dev->cache_size); |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 1793 | dev->mbps = min_t(unsigned int, 1024 * 40, dev->mbps); |
| 1794 | /* can not stop a queue */ |
| 1795 | if (dev->queue_mode == NULL_Q_BIO) |
| 1796 | dev->mbps = 0; |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 1797 | } |
| 1798 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1799 | static int null_add_dev(struct nullb_device *dev) |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1800 | { |
| 1801 | struct nullb *nullb; |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1802 | int rv; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1803 | |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 1804 | null_validate_conf(dev); |
| 1805 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1806 | nullb = kzalloc_node(sizeof(*nullb), GFP_KERNEL, dev->home_node); |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1807 | if (!nullb) { |
| 1808 | rv = -ENOMEM; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1809 | goto out; |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1810 | } |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1811 | nullb->dev = dev; |
| 1812 | dev->nullb = nullb; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1813 | |
| 1814 | spin_lock_init(&nullb->lock); |
| 1815 | |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1816 | rv = setup_queues(nullb); |
| 1817 | if (rv) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1818 | goto out_free_nullb; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1819 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1820 | if (dev->queue_mode == NULL_Q_MQ) { |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1821 | if (shared_tags) { |
| 1822 | nullb->tag_set = &tag_set; |
| 1823 | rv = 0; |
| 1824 | } else { |
| 1825 | nullb->tag_set = &nullb->__tag_set; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1826 | rv = null_init_tag_set(nullb, nullb->tag_set); |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1827 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1828 | |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1829 | if (rv) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1830 | goto out_cleanup_queues; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1831 | |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1832 | nullb->q = blk_mq_init_queue(nullb->tag_set); |
Ming Lei | 35b489d | 2015-01-02 14:25:27 +0000 | [diff] [blame] | 1833 | if (IS_ERR(nullb->q)) { |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1834 | rv = -ENOMEM; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1835 | goto out_cleanup_tags; |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1836 | } |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1837 | null_init_queues(nullb); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1838 | } else if (dev->queue_mode == NULL_Q_BIO) { |
| 1839 | nullb->q = blk_alloc_queue_node(GFP_KERNEL, dev->home_node); |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1840 | if (!nullb->q) { |
| 1841 | rv = -ENOMEM; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1842 | goto out_cleanup_queues; |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1843 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1844 | blk_queue_make_request(nullb->q, null_queue_bio); |
Jan Kara | 31f9690 | 2014-10-22 15:34:21 +0200 | [diff] [blame] | 1845 | rv = init_driver_queues(nullb); |
| 1846 | if (rv) |
| 1847 | goto out_cleanup_blk_queue; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1848 | } else { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1849 | nullb->q = blk_init_queue_node(null_request_fn, &nullb->lock, |
| 1850 | dev->home_node); |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1851 | if (!nullb->q) { |
| 1852 | rv = -ENOMEM; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1853 | goto out_cleanup_queues; |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1854 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1855 | blk_queue_prep_rq(nullb->q, null_rq_prep_fn); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1856 | blk_queue_softirq_done(nullb->q, null_softirq_done_fn); |
Jan Kara | 31f9690 | 2014-10-22 15:34:21 +0200 | [diff] [blame] | 1857 | rv = init_driver_queues(nullb); |
| 1858 | if (rv) |
| 1859 | goto out_cleanup_blk_queue; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1860 | } |
| 1861 | |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 1862 | if (dev->mbps) { |
| 1863 | set_bit(NULLB_DEV_FL_THROTTLED, &dev->flags); |
| 1864 | nullb_setup_bwtimer(nullb); |
| 1865 | } |
| 1866 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1867 | if (dev->cache_size > 0) { |
| 1868 | set_bit(NULLB_DEV_FL_CACHE, &nullb->dev->flags); |
| 1869 | blk_queue_write_cache(nullb->q, true, true); |
| 1870 | blk_queue_flush_queueable(nullb->q, true); |
| 1871 | } |
| 1872 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1873 | nullb->q->queuedata = nullb; |
| 1874 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, nullb->q); |
Mike Snitzer | b277da0 | 2014-10-04 10:55:32 -0600 | [diff] [blame] | 1875 | queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, nullb->q); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1876 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1877 | mutex_lock(&lock); |
Shaohua Li | 94bc02e | 2017-08-14 15:04:55 -0700 | [diff] [blame] | 1878 | nullb->index = ida_simple_get(&nullb_indexes, 0, 0, GFP_KERNEL); |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 1879 | dev->index = nullb->index; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1880 | mutex_unlock(&lock); |
| 1881 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1882 | blk_queue_logical_block_size(nullb->q, dev->blocksize); |
| 1883 | blk_queue_physical_block_size(nullb->q, dev->blocksize); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1884 | |
Shaohua Li | 306eb6b | 2017-08-14 15:04:57 -0700 | [diff] [blame] | 1885 | null_config_discard(nullb); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1886 | |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1887 | sprintf(nullb->disk_name, "nullb%d", nullb->index); |
| 1888 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1889 | if (dev->use_lightnvm) |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1890 | rv = null_nvm_register(nullb); |
| 1891 | else |
| 1892 | rv = null_gendisk_register(nullb); |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1893 | |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1894 | if (rv) |
| 1895 | goto out_cleanup_blk_queue; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1896 | |
Matias Bjørling | a514379 | 2016-02-11 14:49:13 +0100 | [diff] [blame] | 1897 | mutex_lock(&lock); |
| 1898 | list_add_tail(&nullb->list, &nullb_list); |
| 1899 | mutex_unlock(&lock); |
Wenwei Tao | 3681c85 | 2016-03-05 00:27:04 +0800 | [diff] [blame] | 1900 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1901 | return 0; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1902 | out_cleanup_blk_queue: |
| 1903 | blk_cleanup_queue(nullb->q); |
| 1904 | out_cleanup_tags: |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1905 | if (dev->queue_mode == NULL_Q_MQ && nullb->tag_set == &nullb->__tag_set) |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1906 | blk_mq_free_tag_set(nullb->tag_set); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1907 | out_cleanup_queues: |
| 1908 | cleanup_queues(nullb); |
| 1909 | out_free_nullb: |
| 1910 | kfree(nullb); |
| 1911 | out: |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1912 | return rv; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | static int __init null_init(void) |
| 1916 | { |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 1917 | int ret = 0; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1918 | unsigned int i; |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 1919 | struct nullb *nullb; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1920 | struct nullb_device *dev; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1921 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1922 | /* check for nullb_page.bitmap */ |
| 1923 | if (sizeof(unsigned long) * 8 - 2 < (PAGE_SIZE >> SECTOR_SHIFT)) |
| 1924 | return -EINVAL; |
| 1925 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1926 | if (g_bs > PAGE_SIZE) { |
Raghavendra K T | 9967d8a | 2014-01-21 16:59:59 +0530 | [diff] [blame] | 1927 | pr_warn("null_blk: invalid block size\n"); |
| 1928 | pr_warn("null_blk: defaults block size to %lu\n", PAGE_SIZE); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1929 | g_bs = PAGE_SIZE; |
Raghavendra K T | 9967d8a | 2014-01-21 16:59:59 +0530 | [diff] [blame] | 1930 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1931 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1932 | if (g_use_lightnvm && g_bs != 4096) { |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 1933 | pr_warn("null_blk: LightNVM only supports 4k block size\n"); |
| 1934 | pr_warn("null_blk: defaults block size to 4k\n"); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1935 | g_bs = 4096; |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 1936 | } |
| 1937 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1938 | if (g_use_lightnvm && g_queue_mode != NULL_Q_MQ) { |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1939 | pr_warn("null_blk: LightNVM only supported for blk-mq\n"); |
| 1940 | pr_warn("null_blk: defaults queue mode to blk-mq\n"); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1941 | g_queue_mode = NULL_Q_MQ; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1942 | } |
| 1943 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1944 | if (g_queue_mode == NULL_Q_MQ && g_use_per_node_hctx) { |
| 1945 | if (g_submit_queues != nr_online_nodes) { |
weiping zhang | 558ab300 | 2017-08-03 00:26:39 +0800 | [diff] [blame] | 1946 | pr_warn("null_blk: submit_queues param is set to %u.\n", |
Matias Bjorling | d15ee6b | 2013-12-18 13:41:44 +0100 | [diff] [blame] | 1947 | nr_online_nodes); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1948 | g_submit_queues = nr_online_nodes; |
Matias Bjørling | fc1bc35 | 2013-12-21 00:11:01 +0100 | [diff] [blame] | 1949 | } |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1950 | } else if (g_submit_queues > nr_cpu_ids) |
| 1951 | g_submit_queues = nr_cpu_ids; |
| 1952 | else if (g_submit_queues <= 0) |
| 1953 | g_submit_queues = 1; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1954 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1955 | if (g_queue_mode == NULL_Q_MQ && shared_tags) { |
| 1956 | ret = null_init_tag_set(NULL, &tag_set); |
Max Gurtovoy | db2d153 | 2017-07-06 18:00:07 +0300 | [diff] [blame] | 1957 | if (ret) |
| 1958 | return ret; |
| 1959 | } |
| 1960 | |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 1961 | config_group_init(&nullb_subsys.su_group); |
| 1962 | mutex_init(&nullb_subsys.su_mutex); |
| 1963 | |
| 1964 | ret = configfs_register_subsystem(&nullb_subsys); |
| 1965 | if (ret) |
| 1966 | goto err_tagset; |
| 1967 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1968 | mutex_init(&lock); |
| 1969 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1970 | null_major = register_blkdev(0, "nullb"); |
Max Gurtovoy | db2d153 | 2017-07-06 18:00:07 +0300 | [diff] [blame] | 1971 | if (null_major < 0) { |
| 1972 | ret = null_major; |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 1973 | goto err_conf; |
Max Gurtovoy | db2d153 | 2017-07-06 18:00:07 +0300 | [diff] [blame] | 1974 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1975 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1976 | if (g_use_lightnvm) { |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 1977 | ppa_cache = kmem_cache_create("ppa_cache", 64 * sizeof(u64), |
| 1978 | 0, 0, NULL); |
| 1979 | if (!ppa_cache) { |
| 1980 | pr_err("null_blk: unable to create ppa cache\n"); |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 1981 | ret = -ENOMEM; |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 1982 | goto err_ppa; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1983 | } |
| 1984 | } |
| 1985 | |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 1986 | for (i = 0; i < nr_devices; i++) { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1987 | dev = null_alloc_dev(); |
| 1988 | if (!dev) |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 1989 | goto err_dev; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1990 | ret = null_add_dev(dev); |
| 1991 | if (ret) { |
| 1992 | null_free_dev(dev); |
| 1993 | goto err_dev; |
| 1994 | } |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 1995 | } |
| 1996 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1997 | pr_info("null: module loaded\n"); |
| 1998 | return 0; |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 1999 | |
| 2000 | err_dev: |
| 2001 | while (!list_empty(&nullb_list)) { |
| 2002 | nullb = list_entry(nullb_list.next, struct nullb, list); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 2003 | dev = nullb->dev; |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 2004 | null_del_dev(nullb); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 2005 | null_free_dev(dev); |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 2006 | } |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 2007 | kmem_cache_destroy(ppa_cache); |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 2008 | err_ppa: |
| 2009 | unregister_blkdev(null_major, "nullb"); |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 2010 | err_conf: |
| 2011 | configfs_unregister_subsystem(&nullb_subsys); |
Max Gurtovoy | db2d153 | 2017-07-06 18:00:07 +0300 | [diff] [blame] | 2012 | err_tagset: |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 2013 | if (g_queue_mode == NULL_Q_MQ && shared_tags) |
Max Gurtovoy | db2d153 | 2017-07-06 18:00:07 +0300 | [diff] [blame] | 2014 | blk_mq_free_tag_set(&tag_set); |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 2015 | return ret; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 2016 | } |
| 2017 | |
| 2018 | static void __exit null_exit(void) |
| 2019 | { |
| 2020 | struct nullb *nullb; |
| 2021 | |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 2022 | configfs_unregister_subsystem(&nullb_subsys); |
| 2023 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 2024 | unregister_blkdev(null_major, "nullb"); |
| 2025 | |
| 2026 | mutex_lock(&lock); |
| 2027 | while (!list_empty(&nullb_list)) { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 2028 | struct nullb_device *dev; |
| 2029 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 2030 | nullb = list_entry(nullb_list.next, struct nullb, list); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 2031 | dev = nullb->dev; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 2032 | null_del_dev(nullb); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 2033 | null_free_dev(dev); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 2034 | } |
| 2035 | mutex_unlock(&lock); |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 2036 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 2037 | if (g_queue_mode == NULL_Q_MQ && shared_tags) |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 2038 | blk_mq_free_tag_set(&tag_set); |
| 2039 | |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 2040 | kmem_cache_destroy(ppa_cache); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | module_init(null_init); |
| 2044 | module_exit(null_exit); |
| 2045 | |
Jens Axboe | 231b3db | 2017-08-25 12:53:15 -0600 | [diff] [blame] | 2046 | MODULE_AUTHOR("Jens Axboe <axboe@kernel.dk>"); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 2047 | MODULE_LICENSE("GPL"); |