null_blk.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. #include <linux/module.h>
  2. #include <linux/moduleparam.h>
  3. #include <linux/sched.h>
  4. #include <linux/fs.h>
  5. #include <linux/blkdev.h>
  6. #include <linux/init.h>
  7. #include <linux/slab.h>
  8. #include <linux/blk-mq.h>
  9. #include <linux/hrtimer.h>
  10. #include <linux/lightnvm.h>
  11. struct nullb_cmd {
  12. struct list_head list;
  13. struct llist_node ll_list;
  14. struct call_single_data csd;
  15. struct request *rq;
  16. struct bio *bio;
  17. unsigned int tag;
  18. struct nullb_queue *nq;
  19. struct hrtimer timer;
  20. };
  21. struct nullb_queue {
  22. unsigned long *tag_map;
  23. wait_queue_head_t wait;
  24. unsigned int queue_depth;
  25. struct nullb_cmd *cmds;
  26. };
  27. struct nullb {
  28. struct list_head list;
  29. unsigned int index;
  30. struct request_queue *q;
  31. struct gendisk *disk;
  32. struct nvm_dev *ndev;
  33. struct blk_mq_tag_set tag_set;
  34. struct hrtimer timer;
  35. unsigned int queue_depth;
  36. spinlock_t lock;
  37. struct nullb_queue *queues;
  38. unsigned int nr_queues;
  39. char disk_name[DISK_NAME_LEN];
  40. };
  41. static LIST_HEAD(nullb_list);
  42. static struct mutex lock;
  43. static int null_major;
  44. static int nullb_indexes;
  45. static struct kmem_cache *ppa_cache;
  46. enum {
  47. NULL_IRQ_NONE = 0,
  48. NULL_IRQ_SOFTIRQ = 1,
  49. NULL_IRQ_TIMER = 2,
  50. };
  51. enum {
  52. NULL_Q_BIO = 0,
  53. NULL_Q_RQ = 1,
  54. NULL_Q_MQ = 2,
  55. };
  56. static int submit_queues;
  57. module_param(submit_queues, int, S_IRUGO);
  58. MODULE_PARM_DESC(submit_queues, "Number of submission queues");
  59. static int home_node = NUMA_NO_NODE;
  60. module_param(home_node, int, S_IRUGO);
  61. MODULE_PARM_DESC(home_node, "Home node for the device");
  62. static int queue_mode = NULL_Q_MQ;
  63. static int null_param_store_val(const char *str, int *val, int min, int max)
  64. {
  65. int ret, new_val;
  66. ret = kstrtoint(str, 10, &new_val);
  67. if (ret)
  68. return -EINVAL;
  69. if (new_val < min || new_val > max)
  70. return -EINVAL;
  71. *val = new_val;
  72. return 0;
  73. }
  74. static int null_set_queue_mode(const char *str, const struct kernel_param *kp)
  75. {
  76. return null_param_store_val(str, &queue_mode, NULL_Q_BIO, NULL_Q_MQ);
  77. }
  78. static const struct kernel_param_ops null_queue_mode_param_ops = {
  79. .set = null_set_queue_mode,
  80. .get = param_get_int,
  81. };
  82. device_param_cb(queue_mode, &null_queue_mode_param_ops, &queue_mode, S_IRUGO);
  83. MODULE_PARM_DESC(queue_mode, "Block interface to use (0=bio,1=rq,2=multiqueue)");
  84. static int gb = 250;
  85. module_param(gb, int, S_IRUGO);
  86. MODULE_PARM_DESC(gb, "Size in GB");
  87. static int bs = 512;
  88. module_param(bs, int, S_IRUGO);
  89. MODULE_PARM_DESC(bs, "Block size (in bytes)");
  90. static int nr_devices = 2;
  91. module_param(nr_devices, int, S_IRUGO);
  92. MODULE_PARM_DESC(nr_devices, "Number of devices to register");
  93. static bool use_lightnvm;
  94. module_param(use_lightnvm, bool, S_IRUGO);
  95. MODULE_PARM_DESC(use_lightnvm, "Register as a LightNVM device");
  96. static bool blocking;
  97. module_param(blocking, bool, S_IRUGO);
  98. MODULE_PARM_DESC(blocking, "Register as a blocking blk-mq driver device");
  99. static int irqmode = NULL_IRQ_SOFTIRQ;
  100. static int null_set_irqmode(const char *str, const struct kernel_param *kp)
  101. {
  102. return null_param_store_val(str, &irqmode, NULL_IRQ_NONE,
  103. NULL_IRQ_TIMER);
  104. }
  105. static const struct kernel_param_ops null_irqmode_param_ops = {
  106. .set = null_set_irqmode,
  107. .get = param_get_int,
  108. };
  109. device_param_cb(irqmode, &null_irqmode_param_ops, &irqmode, S_IRUGO);
  110. MODULE_PARM_DESC(irqmode, "IRQ completion handler. 0-none, 1-softirq, 2-timer");
  111. static unsigned long completion_nsec = 10000;
  112. module_param(completion_nsec, ulong, S_IRUGO);
  113. MODULE_PARM_DESC(completion_nsec, "Time in ns to complete a request in hardware. Default: 10,000ns");
  114. static int hw_queue_depth = 64;
  115. module_param(hw_queue_depth, int, S_IRUGO);
  116. MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 64");
  117. static bool use_per_node_hctx = false;
  118. module_param(use_per_node_hctx, bool, S_IRUGO);
  119. MODULE_PARM_DESC(use_per_node_hctx, "Use per-node allocation for hardware context queues. Default: false");
  120. static void put_tag(struct nullb_queue *nq, unsigned int tag)
  121. {
  122. clear_bit_unlock(tag, nq->tag_map);
  123. if (waitqueue_active(&nq->wait))
  124. wake_up(&nq->wait);
  125. }
  126. static unsigned int get_tag(struct nullb_queue *nq)
  127. {
  128. unsigned int tag;
  129. do {
  130. tag = find_first_zero_bit(nq->tag_map, nq->queue_depth);
  131. if (tag >= nq->queue_depth)
  132. return -1U;
  133. } while (test_and_set_bit_lock(tag, nq->tag_map));
  134. return tag;
  135. }
  136. static void free_cmd(struct nullb_cmd *cmd)
  137. {
  138. put_tag(cmd->nq, cmd->tag);
  139. }
  140. static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer);
  141. static struct nullb_cmd *__alloc_cmd(struct nullb_queue *nq)
  142. {
  143. struct nullb_cmd *cmd;
  144. unsigned int tag;
  145. tag = get_tag(nq);
  146. if (tag != -1U) {
  147. cmd = &nq->cmds[tag];
  148. cmd->tag = tag;
  149. cmd->nq = nq;
  150. if (irqmode == NULL_IRQ_TIMER) {
  151. hrtimer_init(&cmd->timer, CLOCK_MONOTONIC,
  152. HRTIMER_MODE_REL);
  153. cmd->timer.function = null_cmd_timer_expired;
  154. }
  155. return cmd;
  156. }
  157. return NULL;
  158. }
  159. static struct nullb_cmd *alloc_cmd(struct nullb_queue *nq, int can_wait)
  160. {
  161. struct nullb_cmd *cmd;
  162. DEFINE_WAIT(wait);
  163. cmd = __alloc_cmd(nq);
  164. if (cmd || !can_wait)
  165. return cmd;
  166. do {
  167. prepare_to_wait(&nq->wait, &wait, TASK_UNINTERRUPTIBLE);
  168. cmd = __alloc_cmd(nq);
  169. if (cmd)
  170. break;
  171. io_schedule();
  172. } while (1);
  173. finish_wait(&nq->wait, &wait);
  174. return cmd;
  175. }
  176. static void end_cmd(struct nullb_cmd *cmd)
  177. {
  178. struct request_queue *q = NULL;
  179. if (cmd->rq)
  180. q = cmd->rq->q;
  181. switch (queue_mode) {
  182. case NULL_Q_MQ:
  183. blk_mq_end_request(cmd->rq, BLK_STS_OK);
  184. return;
  185. case NULL_Q_RQ:
  186. INIT_LIST_HEAD(&cmd->rq->queuelist);
  187. blk_end_request_all(cmd->rq, BLK_STS_OK);
  188. break;
  189. case NULL_Q_BIO:
  190. bio_endio(cmd->bio);
  191. break;
  192. }
  193. free_cmd(cmd);
  194. /* Restart queue if needed, as we are freeing a tag */
  195. if (queue_mode == NULL_Q_RQ && blk_queue_stopped(q)) {
  196. unsigned long flags;
  197. spin_lock_irqsave(q->queue_lock, flags);
  198. blk_start_queue_async(q);
  199. spin_unlock_irqrestore(q->queue_lock, flags);
  200. }
  201. }
  202. static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer)
  203. {
  204. end_cmd(container_of(timer, struct nullb_cmd, timer));
  205. return HRTIMER_NORESTART;
  206. }
  207. static void null_cmd_end_timer(struct nullb_cmd *cmd)
  208. {
  209. ktime_t kt = completion_nsec;
  210. hrtimer_start(&cmd->timer, kt, HRTIMER_MODE_REL);
  211. }
  212. static void null_softirq_done_fn(struct request *rq)
  213. {
  214. if (queue_mode == NULL_Q_MQ)
  215. end_cmd(blk_mq_rq_to_pdu(rq));
  216. else
  217. end_cmd(rq->special);
  218. }
  219. static inline void null_handle_cmd(struct nullb_cmd *cmd)
  220. {
  221. /* Complete IO by inline, softirq or timer */
  222. switch (irqmode) {
  223. case NULL_IRQ_SOFTIRQ:
  224. switch (queue_mode) {
  225. case NULL_Q_MQ:
  226. blk_mq_complete_request(cmd->rq);
  227. break;
  228. case NULL_Q_RQ:
  229. blk_complete_request(cmd->rq);
  230. break;
  231. case NULL_Q_BIO:
  232. /*
  233. * XXX: no proper submitting cpu information available.
  234. */
  235. end_cmd(cmd);
  236. break;
  237. }
  238. break;
  239. case NULL_IRQ_NONE:
  240. end_cmd(cmd);
  241. break;
  242. case NULL_IRQ_TIMER:
  243. null_cmd_end_timer(cmd);
  244. break;
  245. }
  246. }
  247. static struct nullb_queue *nullb_to_queue(struct nullb *nullb)
  248. {
  249. int index = 0;
  250. if (nullb->nr_queues != 1)
  251. index = raw_smp_processor_id() / ((nr_cpu_ids + nullb->nr_queues - 1) / nullb->nr_queues);
  252. return &nullb->queues[index];
  253. }
  254. static blk_qc_t null_queue_bio(struct request_queue *q, struct bio *bio)
  255. {
  256. struct nullb *nullb = q->queuedata;
  257. struct nullb_queue *nq = nullb_to_queue(nullb);
  258. struct nullb_cmd *cmd;
  259. cmd = alloc_cmd(nq, 1);
  260. cmd->bio = bio;
  261. null_handle_cmd(cmd);
  262. return BLK_QC_T_NONE;
  263. }
  264. static int null_rq_prep_fn(struct request_queue *q, struct request *req)
  265. {
  266. struct nullb *nullb = q->queuedata;
  267. struct nullb_queue *nq = nullb_to_queue(nullb);
  268. struct nullb_cmd *cmd;
  269. cmd = alloc_cmd(nq, 0);
  270. if (cmd) {
  271. cmd->rq = req;
  272. req->special = cmd;
  273. return BLKPREP_OK;
  274. }
  275. blk_stop_queue(q);
  276. return BLKPREP_DEFER;
  277. }
  278. static void null_request_fn(struct request_queue *q)
  279. {
  280. struct request *rq;
  281. while ((rq = blk_fetch_request(q)) != NULL) {
  282. struct nullb_cmd *cmd = rq->special;
  283. spin_unlock_irq(q->queue_lock);
  284. null_handle_cmd(cmd);
  285. spin_lock_irq(q->queue_lock);
  286. }
  287. }
  288. static blk_status_t null_queue_rq(struct blk_mq_hw_ctx *hctx,
  289. const struct blk_mq_queue_data *bd)
  290. {
  291. struct nullb_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
  292. might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
  293. if (irqmode == NULL_IRQ_TIMER) {
  294. hrtimer_init(&cmd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  295. cmd->timer.function = null_cmd_timer_expired;
  296. }
  297. cmd->rq = bd->rq;
  298. cmd->nq = hctx->driver_data;
  299. blk_mq_start_request(bd->rq);
  300. null_handle_cmd(cmd);
  301. return BLK_STS_OK;
  302. }
  303. static void null_init_queue(struct nullb *nullb, struct nullb_queue *nq)
  304. {
  305. BUG_ON(!nullb);
  306. BUG_ON(!nq);
  307. init_waitqueue_head(&nq->wait);
  308. nq->queue_depth = nullb->queue_depth;
  309. }
  310. static int null_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
  311. unsigned int index)
  312. {
  313. struct nullb *nullb = data;
  314. struct nullb_queue *nq = &nullb->queues[index];
  315. hctx->driver_data = nq;
  316. null_init_queue(nullb, nq);
  317. nullb->nr_queues++;
  318. return 0;
  319. }
  320. static const struct blk_mq_ops null_mq_ops = {
  321. .queue_rq = null_queue_rq,
  322. .init_hctx = null_init_hctx,
  323. .complete = null_softirq_done_fn,
  324. };
  325. static void cleanup_queue(struct nullb_queue *nq)
  326. {
  327. kfree(nq->tag_map);
  328. kfree(nq->cmds);
  329. }
  330. static void cleanup_queues(struct nullb *nullb)
  331. {
  332. int i;
  333. for (i = 0; i < nullb->nr_queues; i++)
  334. cleanup_queue(&nullb->queues[i]);
  335. kfree(nullb->queues);
  336. }
  337. #ifdef CONFIG_NVM
  338. static void null_lnvm_end_io(struct request *rq, blk_status_t status)
  339. {
  340. struct nvm_rq *rqd = rq->end_io_data;
  341. /* XXX: lighnvm core seems to expect NVM_RSP_* values here.. */
  342. rqd->error = status ? -EIO : 0;
  343. nvm_end_io(rqd);
  344. blk_put_request(rq);
  345. }
  346. static int null_lnvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
  347. {
  348. struct request_queue *q = dev->q;
  349. struct request *rq;
  350. struct bio *bio = rqd->bio;
  351. rq = blk_mq_alloc_request(q,
  352. op_is_write(bio_op(bio)) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
  353. if (IS_ERR(rq))
  354. return -ENOMEM;
  355. blk_init_request_from_bio(rq, bio);
  356. rq->end_io_data = rqd;
  357. blk_execute_rq_nowait(q, NULL, rq, 0, null_lnvm_end_io);
  358. return 0;
  359. }
  360. static int null_lnvm_id(struct nvm_dev *dev, struct nvm_id *id)
  361. {
  362. sector_t size = gb * 1024 * 1024 * 1024ULL;
  363. sector_t blksize;
  364. struct nvm_id_group *grp;
  365. id->ver_id = 0x1;
  366. id->vmnt = 0;
  367. id->cap = 0x2;
  368. id->dom = 0x1;
  369. id->ppaf.blk_offset = 0;
  370. id->ppaf.blk_len = 16;
  371. id->ppaf.pg_offset = 16;
  372. id->ppaf.pg_len = 16;
  373. id->ppaf.sect_offset = 32;
  374. id->ppaf.sect_len = 8;
  375. id->ppaf.pln_offset = 40;
  376. id->ppaf.pln_len = 8;
  377. id->ppaf.lun_offset = 48;
  378. id->ppaf.lun_len = 8;
  379. id->ppaf.ch_offset = 56;
  380. id->ppaf.ch_len = 8;
  381. sector_div(size, bs); /* convert size to pages */
  382. size >>= 8; /* concert size to pgs pr blk */
  383. grp = &id->grp;
  384. grp->mtype = 0;
  385. grp->fmtype = 0;
  386. grp->num_ch = 1;
  387. grp->num_pg = 256;
  388. blksize = size;
  389. size >>= 16;
  390. grp->num_lun = size + 1;
  391. sector_div(blksize, grp->num_lun);
  392. grp->num_blk = blksize;
  393. grp->num_pln = 1;
  394. grp->fpg_sz = bs;
  395. grp->csecs = bs;
  396. grp->trdt = 25000;
  397. grp->trdm = 25000;
  398. grp->tprt = 500000;
  399. grp->tprm = 500000;
  400. grp->tbet = 1500000;
  401. grp->tbem = 1500000;
  402. grp->mpos = 0x010101; /* single plane rwe */
  403. grp->cpar = hw_queue_depth;
  404. return 0;
  405. }
  406. static void *null_lnvm_create_dma_pool(struct nvm_dev *dev, char *name)
  407. {
  408. mempool_t *virtmem_pool;
  409. virtmem_pool = mempool_create_slab_pool(64, ppa_cache);
  410. if (!virtmem_pool) {
  411. pr_err("null_blk: Unable to create virtual memory pool\n");
  412. return NULL;
  413. }
  414. return virtmem_pool;
  415. }
  416. static void null_lnvm_destroy_dma_pool(void *pool)
  417. {
  418. mempool_destroy(pool);
  419. }
  420. static void *null_lnvm_dev_dma_alloc(struct nvm_dev *dev, void *pool,
  421. gfp_t mem_flags, dma_addr_t *dma_handler)
  422. {
  423. return mempool_alloc(pool, mem_flags);
  424. }
  425. static void null_lnvm_dev_dma_free(void *pool, void *entry,
  426. dma_addr_t dma_handler)
  427. {
  428. mempool_free(entry, pool);
  429. }
  430. static struct nvm_dev_ops null_lnvm_dev_ops = {
  431. .identity = null_lnvm_id,
  432. .submit_io = null_lnvm_submit_io,
  433. .create_dma_pool = null_lnvm_create_dma_pool,
  434. .destroy_dma_pool = null_lnvm_destroy_dma_pool,
  435. .dev_dma_alloc = null_lnvm_dev_dma_alloc,
  436. .dev_dma_free = null_lnvm_dev_dma_free,
  437. /* Simulate nvme protocol restriction */
  438. .max_phys_sect = 64,
  439. };
  440. static int null_nvm_register(struct nullb *nullb)
  441. {
  442. struct nvm_dev *dev;
  443. int rv;
  444. dev = nvm_alloc_dev(0);
  445. if (!dev)
  446. return -ENOMEM;
  447. dev->q = nullb->q;
  448. memcpy(dev->name, nullb->disk_name, DISK_NAME_LEN);
  449. dev->ops = &null_lnvm_dev_ops;
  450. rv = nvm_register(dev);
  451. if (rv) {
  452. kfree(dev);
  453. return rv;
  454. }
  455. nullb->ndev = dev;
  456. return 0;
  457. }
  458. static void null_nvm_unregister(struct nullb *nullb)
  459. {
  460. nvm_unregister(nullb->ndev);
  461. }
  462. #else
  463. static int null_nvm_register(struct nullb *nullb)
  464. {
  465. pr_err("null_blk: CONFIG_NVM needs to be enabled for LightNVM\n");
  466. return -EINVAL;
  467. }
  468. static void null_nvm_unregister(struct nullb *nullb) {}
  469. #endif /* CONFIG_NVM */
  470. static void null_del_dev(struct nullb *nullb)
  471. {
  472. list_del_init(&nullb->list);
  473. if (use_lightnvm)
  474. null_nvm_unregister(nullb);
  475. else
  476. del_gendisk(nullb->disk);
  477. blk_cleanup_queue(nullb->q);
  478. if (queue_mode == NULL_Q_MQ)
  479. blk_mq_free_tag_set(&nullb->tag_set);
  480. if (!use_lightnvm)
  481. put_disk(nullb->disk);
  482. cleanup_queues(nullb);
  483. kfree(nullb);
  484. }
  485. static int null_open(struct block_device *bdev, fmode_t mode)
  486. {
  487. return 0;
  488. }
  489. static void null_release(struct gendisk *disk, fmode_t mode)
  490. {
  491. }
  492. static const struct block_device_operations null_fops = {
  493. .owner = THIS_MODULE,
  494. .open = null_open,
  495. .release = null_release,
  496. };
  497. static int setup_commands(struct nullb_queue *nq)
  498. {
  499. struct nullb_cmd *cmd;
  500. int i, tag_size;
  501. nq->cmds = kzalloc(nq->queue_depth * sizeof(*cmd), GFP_KERNEL);
  502. if (!nq->cmds)
  503. return -ENOMEM;
  504. tag_size = ALIGN(nq->queue_depth, BITS_PER_LONG) / BITS_PER_LONG;
  505. nq->tag_map = kzalloc(tag_size * sizeof(unsigned long), GFP_KERNEL);
  506. if (!nq->tag_map) {
  507. kfree(nq->cmds);
  508. return -ENOMEM;
  509. }
  510. for (i = 0; i < nq->queue_depth; i++) {
  511. cmd = &nq->cmds[i];
  512. INIT_LIST_HEAD(&cmd->list);
  513. cmd->ll_list.next = NULL;
  514. cmd->tag = -1U;
  515. }
  516. return 0;
  517. }
  518. static int setup_queues(struct nullb *nullb)
  519. {
  520. nullb->queues = kzalloc(submit_queues * sizeof(struct nullb_queue),
  521. GFP_KERNEL);
  522. if (!nullb->queues)
  523. return -ENOMEM;
  524. nullb->nr_queues = 0;
  525. nullb->queue_depth = hw_queue_depth;
  526. return 0;
  527. }
  528. static int init_driver_queues(struct nullb *nullb)
  529. {
  530. struct nullb_queue *nq;
  531. int i, ret = 0;
  532. for (i = 0; i < submit_queues; i++) {
  533. nq = &nullb->queues[i];
  534. null_init_queue(nullb, nq);
  535. ret = setup_commands(nq);
  536. if (ret)
  537. return ret;
  538. nullb->nr_queues++;
  539. }
  540. return 0;
  541. }
  542. static int null_gendisk_register(struct nullb *nullb)
  543. {
  544. struct gendisk *disk;
  545. sector_t size;
  546. disk = nullb->disk = alloc_disk_node(1, home_node);
  547. if (!disk)
  548. return -ENOMEM;
  549. size = gb * 1024 * 1024 * 1024ULL;
  550. set_capacity(disk, size >> 9);
  551. disk->flags |= GENHD_FL_EXT_DEVT | GENHD_FL_SUPPRESS_PARTITION_INFO;
  552. disk->major = null_major;
  553. disk->first_minor = nullb->index;
  554. disk->fops = &null_fops;
  555. disk->private_data = nullb;
  556. disk->queue = nullb->q;
  557. strncpy(disk->disk_name, nullb->disk_name, DISK_NAME_LEN);
  558. add_disk(disk);
  559. return 0;
  560. }
  561. static int null_add_dev(void)
  562. {
  563. struct nullb *nullb;
  564. int rv;
  565. nullb = kzalloc_node(sizeof(*nullb), GFP_KERNEL, home_node);
  566. if (!nullb) {
  567. rv = -ENOMEM;
  568. goto out;
  569. }
  570. spin_lock_init(&nullb->lock);
  571. if (queue_mode == NULL_Q_MQ && use_per_node_hctx)
  572. submit_queues = nr_online_nodes;
  573. rv = setup_queues(nullb);
  574. if (rv)
  575. goto out_free_nullb;
  576. if (queue_mode == NULL_Q_MQ) {
  577. nullb->tag_set.ops = &null_mq_ops;
  578. nullb->tag_set.nr_hw_queues = submit_queues;
  579. nullb->tag_set.queue_depth = hw_queue_depth;
  580. nullb->tag_set.numa_node = home_node;
  581. nullb->tag_set.cmd_size = sizeof(struct nullb_cmd);
  582. nullb->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
  583. nullb->tag_set.driver_data = nullb;
  584. if (blocking)
  585. nullb->tag_set.flags |= BLK_MQ_F_BLOCKING;
  586. rv = blk_mq_alloc_tag_set(&nullb->tag_set);
  587. if (rv)
  588. goto out_cleanup_queues;
  589. nullb->q = blk_mq_init_queue(&nullb->tag_set);
  590. if (IS_ERR(nullb->q)) {
  591. rv = -ENOMEM;
  592. goto out_cleanup_tags;
  593. }
  594. } else if (queue_mode == NULL_Q_BIO) {
  595. nullb->q = blk_alloc_queue_node(GFP_KERNEL, home_node);
  596. if (!nullb->q) {
  597. rv = -ENOMEM;
  598. goto out_cleanup_queues;
  599. }
  600. blk_queue_make_request(nullb->q, null_queue_bio);
  601. rv = init_driver_queues(nullb);
  602. if (rv)
  603. goto out_cleanup_blk_queue;
  604. } else {
  605. nullb->q = blk_init_queue_node(null_request_fn, &nullb->lock, home_node);
  606. if (!nullb->q) {
  607. rv = -ENOMEM;
  608. goto out_cleanup_queues;
  609. }
  610. blk_queue_prep_rq(nullb->q, null_rq_prep_fn);
  611. blk_queue_softirq_done(nullb->q, null_softirq_done_fn);
  612. rv = init_driver_queues(nullb);
  613. if (rv)
  614. goto out_cleanup_blk_queue;
  615. }
  616. nullb->q->queuedata = nullb;
  617. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, nullb->q);
  618. queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, nullb->q);
  619. mutex_lock(&lock);
  620. nullb->index = nullb_indexes++;
  621. mutex_unlock(&lock);
  622. blk_queue_logical_block_size(nullb->q, bs);
  623. blk_queue_physical_block_size(nullb->q, bs);
  624. sprintf(nullb->disk_name, "nullb%d", nullb->index);
  625. if (use_lightnvm)
  626. rv = null_nvm_register(nullb);
  627. else
  628. rv = null_gendisk_register(nullb);
  629. if (rv)
  630. goto out_cleanup_blk_queue;
  631. mutex_lock(&lock);
  632. list_add_tail(&nullb->list, &nullb_list);
  633. mutex_unlock(&lock);
  634. return 0;
  635. out_cleanup_blk_queue:
  636. blk_cleanup_queue(nullb->q);
  637. out_cleanup_tags:
  638. if (queue_mode == NULL_Q_MQ)
  639. blk_mq_free_tag_set(&nullb->tag_set);
  640. out_cleanup_queues:
  641. cleanup_queues(nullb);
  642. out_free_nullb:
  643. kfree(nullb);
  644. out:
  645. return rv;
  646. }
  647. static int __init null_init(void)
  648. {
  649. int ret = 0;
  650. unsigned int i;
  651. struct nullb *nullb;
  652. if (bs > PAGE_SIZE) {
  653. pr_warn("null_blk: invalid block size\n");
  654. pr_warn("null_blk: defaults block size to %lu\n", PAGE_SIZE);
  655. bs = PAGE_SIZE;
  656. }
  657. if (use_lightnvm && bs != 4096) {
  658. pr_warn("null_blk: LightNVM only supports 4k block size\n");
  659. pr_warn("null_blk: defaults block size to 4k\n");
  660. bs = 4096;
  661. }
  662. if (use_lightnvm && queue_mode != NULL_Q_MQ) {
  663. pr_warn("null_blk: LightNVM only supported for blk-mq\n");
  664. pr_warn("null_blk: defaults queue mode to blk-mq\n");
  665. queue_mode = NULL_Q_MQ;
  666. }
  667. if (queue_mode == NULL_Q_MQ && use_per_node_hctx) {
  668. if (submit_queues < nr_online_nodes) {
  669. pr_warn("null_blk: submit_queues param is set to %u.",
  670. nr_online_nodes);
  671. submit_queues = nr_online_nodes;
  672. }
  673. } else if (submit_queues > nr_cpu_ids)
  674. submit_queues = nr_cpu_ids;
  675. else if (!submit_queues)
  676. submit_queues = 1;
  677. mutex_init(&lock);
  678. null_major = register_blkdev(0, "nullb");
  679. if (null_major < 0)
  680. return null_major;
  681. if (use_lightnvm) {
  682. ppa_cache = kmem_cache_create("ppa_cache", 64 * sizeof(u64),
  683. 0, 0, NULL);
  684. if (!ppa_cache) {
  685. pr_err("null_blk: unable to create ppa cache\n");
  686. ret = -ENOMEM;
  687. goto err_ppa;
  688. }
  689. }
  690. for (i = 0; i < nr_devices; i++) {
  691. ret = null_add_dev();
  692. if (ret)
  693. goto err_dev;
  694. }
  695. pr_info("null: module loaded\n");
  696. return 0;
  697. err_dev:
  698. while (!list_empty(&nullb_list)) {
  699. nullb = list_entry(nullb_list.next, struct nullb, list);
  700. null_del_dev(nullb);
  701. }
  702. kmem_cache_destroy(ppa_cache);
  703. err_ppa:
  704. unregister_blkdev(null_major, "nullb");
  705. return ret;
  706. }
  707. static void __exit null_exit(void)
  708. {
  709. struct nullb *nullb;
  710. unregister_blkdev(null_major, "nullb");
  711. mutex_lock(&lock);
  712. while (!list_empty(&nullb_list)) {
  713. nullb = list_entry(nullb_list.next, struct nullb, list);
  714. null_del_dev(nullb);
  715. }
  716. mutex_unlock(&lock);
  717. kmem_cache_destroy(ppa_cache);
  718. }
  719. module_init(null_init);
  720. module_exit(null_exit);
  721. MODULE_AUTHOR("Jens Axboe <jaxboe@fusionio.com>");
  722. MODULE_LICENSE("GPL");