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