1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Userspace block device - block device which IO is handled from userspace
5 * Take full use of io_uring passthrough command for communicating with
6 * ublk userspace daemon(ublksrvd) for handling basic IO request.
8 * Copyright 2022 Ming Lei <ming.lei@redhat.com>
10 * (part of code stolen from loop.c)
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/sched.h>
16 #include <linux/pagemap.h>
17 #include <linux/file.h>
18 #include <linux/stat.h>
19 #include <linux/errno.h>
20 #include <linux/major.h>
21 #include <linux/wait.h>
22 #include <linux/blkdev.h>
23 #include <linux/init.h>
24 #include <linux/swap.h>
25 #include <linux/slab.h>
26 #include <linux/compat.h>
27 #include <linux/mutex.h>
28 #include <linux/writeback.h>
29 #include <linux/completion.h>
30 #include <linux/highmem.h>
31 #include <linux/sysfs.h>
32 #include <linux/miscdevice.h>
33 #include <linux/falloc.h>
34 #include <linux/uio.h>
35 #include <linux/ioprio.h>
36 #include <linux/sched/mm.h>
37 #include <linux/uaccess.h>
38 #include <linux/cdev.h>
39 #include <linux/io_uring.h>
40 #include <linux/blk-mq.h>
41 #include <linux/delay.h>
44 #include <linux/task_work.h>
45 #include <uapi/linux/ublk_cmd.h>
47 #define UBLK_MINORS (1U << MINORBITS)
49 /* All UBLK_F_* have to be included into UBLK_F_ALL */
50 #define UBLK_F_ALL (UBLK_F_SUPPORT_ZERO_COPY \
51 | UBLK_F_URING_CMD_COMP_IN_TASK \
52 | UBLK_F_NEED_GET_DATA)
54 /* All UBLK_PARAM_TYPE_* should be included here */
55 #define UBLK_PARAM_TYPE_ALL (UBLK_PARAM_TYPE_BASIC | UBLK_PARAM_TYPE_DISCARD)
58 struct callback_head work;
61 struct ublk_uring_cmd_pdu {
66 * io command is active: sqe cmd is received, and its cqe isn't done
68 * If the flag is set, the io command is owned by ublk driver, and waited
69 * for incoming blk-mq request from the ublk block device.
71 * If the flag is cleared, the io command will be completed, and owned by
74 #define UBLK_IO_FLAG_ACTIVE 0x01
77 * IO command is completed via cqe, and it is being handled by ublksrv, and
80 * Basically exclusively with UBLK_IO_FLAG_ACTIVE, so can be served for
83 #define UBLK_IO_FLAG_OWNED_BY_SRV 0x02
86 * IO command is aborted, so this flag is set in case of
87 * !UBLK_IO_FLAG_ACTIVE.
89 * After this flag is observed, any pending or new incoming request
90 * associated with this io command will be failed immediately
92 #define UBLK_IO_FLAG_ABORTED 0x04
95 * UBLK_IO_FLAG_NEED_GET_DATA is set because IO command requires
96 * get data buffer address from ublksrv.
98 * Then, bio data could be copied into this data buffer for a WRITE request
99 * after the IO command is issued again and UBLK_IO_FLAG_NEED_GET_DATA is unset.
101 #define UBLK_IO_FLAG_NEED_GET_DATA 0x08
104 /* userspace buffer address from io cmd */
109 struct io_uring_cmd *cmd;
117 struct task_struct *ubq_daemon;
120 unsigned long io_addr; /* mapped vm address */
121 unsigned int max_io_sz;
122 bool abort_work_pending;
123 unsigned short nr_io_ready; /* how many ios setup */
124 struct ublk_device *dev;
125 struct ublk_io ios[0];
128 #define UBLK_DAEMON_MONITOR_PERIOD (5 * HZ)
131 struct gendisk *ub_disk;
135 unsigned short queue_size;
136 struct ublksrv_ctrl_dev_info dev_info;
138 struct blk_mq_tag_set tag_set;
141 struct device cdev_dev;
143 #define UB_STATE_OPEN 0
144 #define UB_STATE_USED 1
151 struct mm_struct *mm;
153 struct ublk_params params;
155 struct completion completion;
156 unsigned int nr_queues_ready;
157 atomic_t nr_aborted_queues;
160 * Our ubq->daemon may be killed without any notification, so
161 * monitor each queue's daemon periodically
163 struct delayed_work monitor_work;
164 struct work_struct stop_work;
167 /* header of ublk_params */
168 struct ublk_params_header {
173 static dev_t ublk_chr_devt;
174 static struct class *ublk_chr_class;
176 static DEFINE_IDR(ublk_index_idr);
177 static DEFINE_SPINLOCK(ublk_idr_lock);
178 static wait_queue_head_t ublk_idr_wq; /* wait until one idr is freed */
180 static DEFINE_MUTEX(ublk_ctl_mutex);
182 static struct miscdevice ublk_misc;
184 static void ublk_dev_param_basic_apply(struct ublk_device *ub)
186 struct request_queue *q = ub->ub_disk->queue;
187 const struct ublk_param_basic *p = &ub->params.basic;
189 blk_queue_logical_block_size(q, 1 << p->logical_bs_shift);
190 blk_queue_physical_block_size(q, 1 << p->physical_bs_shift);
191 blk_queue_io_min(q, 1 << p->io_min_shift);
192 blk_queue_io_opt(q, 1 << p->io_opt_shift);
194 blk_queue_write_cache(q, p->attrs & UBLK_ATTR_VOLATILE_CACHE,
195 p->attrs & UBLK_ATTR_FUA);
196 if (p->attrs & UBLK_ATTR_ROTATIONAL)
197 blk_queue_flag_clear(QUEUE_FLAG_NONROT, q);
199 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
201 blk_queue_max_hw_sectors(q, p->max_sectors);
202 blk_queue_chunk_sectors(q, p->chunk_sectors);
203 blk_queue_virt_boundary(q, p->virt_boundary_mask);
205 if (p->attrs & UBLK_ATTR_READ_ONLY)
206 set_disk_ro(ub->ub_disk, true);
208 set_capacity(ub->ub_disk, p->dev_sectors);
211 static void ublk_dev_param_discard_apply(struct ublk_device *ub)
213 struct request_queue *q = ub->ub_disk->queue;
214 const struct ublk_param_discard *p = &ub->params.discard;
216 q->limits.discard_alignment = p->discard_alignment;
217 q->limits.discard_granularity = p->discard_granularity;
218 blk_queue_max_discard_sectors(q, p->max_discard_sectors);
219 blk_queue_max_write_zeroes_sectors(q,
220 p->max_write_zeroes_sectors);
221 blk_queue_max_discard_segments(q, p->max_discard_segments);
224 static int ublk_validate_params(const struct ublk_device *ub)
226 /* basic param is the only one which must be set */
227 if (ub->params.types & UBLK_PARAM_TYPE_BASIC) {
228 const struct ublk_param_basic *p = &ub->params.basic;
230 if (p->logical_bs_shift > PAGE_SHIFT)
233 if (p->logical_bs_shift > p->physical_bs_shift)
236 if (p->max_sectors > (ub->dev_info.max_io_buf_bytes >> 9))
241 if (ub->params.types & UBLK_PARAM_TYPE_DISCARD) {
242 const struct ublk_param_discard *p = &ub->params.discard;
244 /* So far, only support single segment discard */
245 if (p->max_discard_sectors && p->max_discard_segments != 1)
248 if (!p->discard_granularity)
255 static int ublk_apply_params(struct ublk_device *ub)
257 if (!(ub->params.types & UBLK_PARAM_TYPE_BASIC))
260 ublk_dev_param_basic_apply(ub);
262 if (ub->params.types & UBLK_PARAM_TYPE_DISCARD)
263 ublk_dev_param_discard_apply(ub);
268 static inline bool ublk_can_use_task_work(const struct ublk_queue *ubq)
270 if (IS_BUILTIN(CONFIG_BLK_DEV_UBLK) &&
271 !(ubq->flags & UBLK_F_URING_CMD_COMP_IN_TASK))
276 static inline bool ublk_need_get_data(const struct ublk_queue *ubq)
278 if (ubq->flags & UBLK_F_NEED_GET_DATA)
283 static struct ublk_device *ublk_get_device(struct ublk_device *ub)
285 if (kobject_get_unless_zero(&ub->cdev_dev.kobj))
290 static void ublk_put_device(struct ublk_device *ub)
292 put_device(&ub->cdev_dev);
295 static inline struct ublk_queue *ublk_get_queue(struct ublk_device *dev,
298 return (struct ublk_queue *)&(dev->__queues[qid * dev->queue_size]);
301 static inline bool ublk_rq_has_data(const struct request *rq)
303 return rq->bio && bio_has_data(rq->bio);
306 static inline struct ublksrv_io_desc *ublk_get_iod(struct ublk_queue *ubq,
309 return (struct ublksrv_io_desc *)
310 &(ubq->io_cmd_buf[tag * sizeof(struct ublksrv_io_desc)]);
313 static inline char *ublk_queue_cmd_buf(struct ublk_device *ub, int q_id)
315 return ublk_get_queue(ub, q_id)->io_cmd_buf;
318 static inline int ublk_queue_cmd_buf_size(struct ublk_device *ub, int q_id)
320 struct ublk_queue *ubq = ublk_get_queue(ub, q_id);
322 return round_up(ubq->q_depth * sizeof(struct ublksrv_io_desc),
326 static void ublk_free_disk(struct gendisk *disk)
328 struct ublk_device *ub = disk->private_data;
330 clear_bit(UB_STATE_USED, &ub->state);
331 put_device(&ub->cdev_dev);
334 static const struct block_device_operations ub_fops = {
335 .owner = THIS_MODULE,
336 .free_disk = ublk_free_disk,
339 #define UBLK_MAX_PIN_PAGES 32
341 struct ublk_map_data {
342 const struct ublk_queue *ubq;
343 const struct request *rq;
344 const struct ublk_io *io;
348 struct ublk_io_iter {
349 struct page *pages[UBLK_MAX_PIN_PAGES];
350 unsigned pg_off; /* offset in the 1st page in pages */
351 int nr_pages; /* how many page pointers in pages */
353 struct bvec_iter iter;
356 static inline unsigned ublk_copy_io_pages(struct ublk_io_iter *data,
357 unsigned max_bytes, bool to_vm)
359 const unsigned total = min_t(unsigned, max_bytes,
360 PAGE_SIZE - data->pg_off +
361 ((data->nr_pages - 1) << PAGE_SHIFT));
365 while (done < total) {
366 struct bio_vec bv = bio_iter_iovec(data->bio, data->iter);
367 const unsigned int bytes = min3(bv.bv_len, total - done,
368 (unsigned)(PAGE_SIZE - data->pg_off));
369 void *bv_buf = bvec_kmap_local(&bv);
370 void *pg_buf = kmap_local_page(data->pages[pg_idx]);
373 memcpy(pg_buf + data->pg_off, bv_buf, bytes);
375 memcpy(bv_buf, pg_buf + data->pg_off, bytes);
377 kunmap_local(pg_buf);
378 kunmap_local(bv_buf);
380 /* advance page array */
381 data->pg_off += bytes;
382 if (data->pg_off == PAGE_SIZE) {
390 bio_advance_iter_single(data->bio, &data->iter, bytes);
391 if (!data->iter.bi_size) {
392 data->bio = data->bio->bi_next;
393 if (data->bio == NULL)
395 data->iter = data->bio->bi_iter;
402 static inline int ublk_copy_user_pages(struct ublk_map_data *data,
405 const unsigned int gup_flags = to_vm ? FOLL_WRITE : 0;
406 const unsigned long start_vm = data->io->addr;
407 unsigned int done = 0;
408 struct ublk_io_iter iter = {
409 .pg_off = start_vm & (PAGE_SIZE - 1),
410 .bio = data->rq->bio,
411 .iter = data->rq->bio->bi_iter,
413 const unsigned int nr_pages = round_up(data->max_bytes +
414 (start_vm & (PAGE_SIZE - 1)), PAGE_SIZE) >> PAGE_SHIFT;
416 while (done < nr_pages) {
417 const unsigned to_pin = min_t(unsigned, UBLK_MAX_PIN_PAGES,
421 iter.nr_pages = get_user_pages_fast(start_vm +
422 (done << PAGE_SHIFT), to_pin, gup_flags,
424 if (iter.nr_pages <= 0)
425 return done == 0 ? iter.nr_pages : done;
426 len = ublk_copy_io_pages(&iter, data->max_bytes, to_vm);
427 for (i = 0; i < iter.nr_pages; i++) {
429 set_page_dirty(iter.pages[i]);
430 put_page(iter.pages[i]);
432 data->max_bytes -= len;
433 done += iter.nr_pages;
439 static int ublk_map_io(const struct ublk_queue *ubq, const struct request *req,
442 const unsigned int rq_bytes = blk_rq_bytes(req);
444 * no zero copy, we delay copy WRITE request data into ublksrv
445 * context and the big benefit is that pinning pages in current
446 * context is pretty fast, see ublk_pin_user_pages
448 if (req_op(req) != REQ_OP_WRITE && req_op(req) != REQ_OP_FLUSH)
451 if (ublk_rq_has_data(req)) {
452 struct ublk_map_data data = {
456 .max_bytes = rq_bytes,
459 ublk_copy_user_pages(&data, true);
461 return rq_bytes - data.max_bytes;
466 static int ublk_unmap_io(const struct ublk_queue *ubq,
467 const struct request *req,
470 const unsigned int rq_bytes = blk_rq_bytes(req);
472 if (req_op(req) == REQ_OP_READ && ublk_rq_has_data(req)) {
473 struct ublk_map_data data = {
477 .max_bytes = io->res,
480 WARN_ON_ONCE(io->res > rq_bytes);
482 ublk_copy_user_pages(&data, false);
484 return io->res - data.max_bytes;
489 static inline unsigned int ublk_req_build_flags(struct request *req)
493 if (req->cmd_flags & REQ_FAILFAST_DEV)
494 flags |= UBLK_IO_F_FAILFAST_DEV;
496 if (req->cmd_flags & REQ_FAILFAST_TRANSPORT)
497 flags |= UBLK_IO_F_FAILFAST_TRANSPORT;
499 if (req->cmd_flags & REQ_FAILFAST_DRIVER)
500 flags |= UBLK_IO_F_FAILFAST_DRIVER;
502 if (req->cmd_flags & REQ_META)
503 flags |= UBLK_IO_F_META;
505 if (req->cmd_flags & REQ_FUA)
506 flags |= UBLK_IO_F_FUA;
508 if (req->cmd_flags & REQ_NOUNMAP)
509 flags |= UBLK_IO_F_NOUNMAP;
511 if (req->cmd_flags & REQ_SWAP)
512 flags |= UBLK_IO_F_SWAP;
517 static blk_status_t ublk_setup_iod(struct ublk_queue *ubq, struct request *req)
519 struct ublksrv_io_desc *iod = ublk_get_iod(ubq, req->tag);
520 struct ublk_io *io = &ubq->ios[req->tag];
523 switch (req_op(req)) {
525 ublk_op = UBLK_IO_OP_READ;
528 ublk_op = UBLK_IO_OP_WRITE;
531 ublk_op = UBLK_IO_OP_FLUSH;
534 ublk_op = UBLK_IO_OP_DISCARD;
536 case REQ_OP_WRITE_ZEROES:
537 ublk_op = UBLK_IO_OP_WRITE_ZEROES;
540 return BLK_STS_IOERR;
543 /* need to translate since kernel may change */
544 iod->op_flags = ublk_op | ublk_req_build_flags(req);
545 iod->nr_sectors = blk_rq_sectors(req);
546 iod->start_sector = blk_rq_pos(req);
547 iod->addr = io->addr;
552 static inline struct ublk_uring_cmd_pdu *ublk_get_uring_cmd_pdu(
553 struct io_uring_cmd *ioucmd)
555 return (struct ublk_uring_cmd_pdu *)&ioucmd->pdu;
558 static inline bool ubq_daemon_is_dying(struct ublk_queue *ubq)
560 return ubq->ubq_daemon->flags & PF_EXITING;
563 /* todo: handle partial completion */
564 static void ublk_complete_rq(struct request *req)
566 struct ublk_queue *ubq = req->mq_hctx->driver_data;
567 struct ublk_io *io = &ubq->ios[req->tag];
568 unsigned int unmapped_bytes;
570 /* failed read IO if nothing is read */
571 if (!io->res && req_op(req) == REQ_OP_READ)
575 blk_mq_end_request(req, errno_to_blk_status(io->res));
580 * FLUSH or DISCARD usually won't return bytes returned, so end them
583 * Both the two needn't unmap.
585 if (req_op(req) != REQ_OP_READ && req_op(req) != REQ_OP_WRITE) {
586 blk_mq_end_request(req, BLK_STS_OK);
590 /* for READ request, writing data in iod->addr to rq buffers */
591 unmapped_bytes = ublk_unmap_io(ubq, req, io);
594 * Extremely impossible since we got data filled in just before
596 * Re-read simply for this unlikely case.
598 if (unlikely(unmapped_bytes < io->res))
599 io->res = unmapped_bytes;
601 if (blk_update_request(req, BLK_STS_OK, io->res))
602 blk_mq_requeue_request(req, true);
604 __blk_mq_end_request(req, BLK_STS_OK);
608 * Since __ublk_rq_task_work always fails requests immediately during
609 * exiting, __ublk_fail_req() is only called from abort context during
610 * exiting. So lock is unnecessary.
612 * Also aborting may not be started yet, keep in mind that one failed
613 * request may be issued by block layer again.
615 static void __ublk_fail_req(struct ublk_io *io, struct request *req)
617 WARN_ON_ONCE(io->flags & UBLK_IO_FLAG_ACTIVE);
619 if (!(io->flags & UBLK_IO_FLAG_ABORTED)) {
620 io->flags |= UBLK_IO_FLAG_ABORTED;
621 blk_mq_end_request(req, BLK_STS_IOERR);
625 static void ubq_complete_io_cmd(struct ublk_io *io, int res)
627 /* mark this cmd owned by ublksrv */
628 io->flags |= UBLK_IO_FLAG_OWNED_BY_SRV;
631 * clear ACTIVE since we are done with this sqe/cmd slot
632 * We can only accept io cmd in case of being not active.
634 io->flags &= ~UBLK_IO_FLAG_ACTIVE;
636 /* tell ublksrv one io request is coming */
637 io_uring_cmd_done(io->cmd, res, 0);
640 #define UBLK_REQUEUE_DELAY_MS 3
642 static inline void __ublk_rq_task_work(struct request *req)
644 struct ublk_queue *ubq = req->mq_hctx->driver_data;
645 struct ublk_device *ub = ubq->dev;
647 struct ublk_io *io = &ubq->ios[tag];
648 bool task_exiting = current != ubq->ubq_daemon || ubq_daemon_is_dying(ubq);
649 unsigned int mapped_bytes;
651 pr_devel("%s: complete: op %d, qid %d tag %d io_flags %x addr %llx\n",
652 __func__, io->cmd->cmd_op, ubq->q_id, req->tag, io->flags,
653 ublk_get_iod(ubq, req->tag)->addr);
655 if (unlikely(task_exiting)) {
656 blk_mq_end_request(req, BLK_STS_IOERR);
657 mod_delayed_work(system_wq, &ub->monitor_work, 0);
661 if (ublk_need_get_data(ubq) &&
662 (req_op(req) == REQ_OP_WRITE ||
663 req_op(req) == REQ_OP_FLUSH)) {
665 * We have not handled UBLK_IO_NEED_GET_DATA command yet,
666 * so immepdately pass UBLK_IO_RES_NEED_GET_DATA to ublksrv
669 if (!(io->flags & UBLK_IO_FLAG_NEED_GET_DATA)) {
670 io->flags |= UBLK_IO_FLAG_NEED_GET_DATA;
671 pr_devel("%s: need get data. op %d, qid %d tag %d io_flags %x\n",
672 __func__, io->cmd->cmd_op, ubq->q_id,
673 req->tag, io->flags);
674 ubq_complete_io_cmd(io, UBLK_IO_RES_NEED_GET_DATA);
678 * We have handled UBLK_IO_NEED_GET_DATA command,
679 * so clear UBLK_IO_FLAG_NEED_GET_DATA now and just
682 io->flags &= ~UBLK_IO_FLAG_NEED_GET_DATA;
683 /* update iod->addr because ublksrv may have passed a new io buffer */
684 ublk_get_iod(ubq, req->tag)->addr = io->addr;
685 pr_devel("%s: update iod->addr: op %d, qid %d tag %d io_flags %x addr %llx\n",
686 __func__, io->cmd->cmd_op, ubq->q_id, req->tag, io->flags,
687 ublk_get_iod(ubq, req->tag)->addr);
690 mapped_bytes = ublk_map_io(ubq, req, io);
692 /* partially mapped, update io descriptor */
693 if (unlikely(mapped_bytes != blk_rq_bytes(req))) {
695 * Nothing mapped, retry until we succeed.
697 * We may never succeed in mapping any bytes here because
698 * of OOM. TODO: reserve one buffer with single page pinned
699 * for providing forward progress guarantee.
701 if (unlikely(!mapped_bytes)) {
702 blk_mq_requeue_request(req, false);
703 blk_mq_delay_kick_requeue_list(req->q,
704 UBLK_REQUEUE_DELAY_MS);
708 ublk_get_iod(ubq, req->tag)->nr_sectors =
712 ubq_complete_io_cmd(io, UBLK_IO_RES_OK);
715 static void ublk_rq_task_work_cb(struct io_uring_cmd *cmd)
717 struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
719 __ublk_rq_task_work(pdu->req);
722 static void ublk_rq_task_work_fn(struct callback_head *work)
724 struct ublk_rq_data *data = container_of(work,
725 struct ublk_rq_data, work);
726 struct request *req = blk_mq_rq_from_pdu(data);
728 __ublk_rq_task_work(req);
731 static blk_status_t ublk_queue_rq(struct blk_mq_hw_ctx *hctx,
732 const struct blk_mq_queue_data *bd)
734 struct ublk_queue *ubq = hctx->driver_data;
735 struct request *rq = bd->rq;
738 /* fill iod to slot in io cmd buffer */
739 res = ublk_setup_iod(ubq, rq);
740 if (unlikely(res != BLK_STS_OK))
741 return BLK_STS_IOERR;
743 blk_mq_start_request(bd->rq);
745 if (unlikely(ubq_daemon_is_dying(ubq))) {
747 mod_delayed_work(system_wq, &ubq->dev->monitor_work, 0);
748 return BLK_STS_IOERR;
751 if (ublk_can_use_task_work(ubq)) {
752 struct ublk_rq_data *data = blk_mq_rq_to_pdu(rq);
753 enum task_work_notify_mode notify_mode = bd->last ?
754 TWA_SIGNAL_NO_IPI : TWA_NONE;
756 if (task_work_add(ubq->ubq_daemon, &data->work, notify_mode))
759 struct ublk_io *io = &ubq->ios[rq->tag];
760 struct io_uring_cmd *cmd = io->cmd;
761 struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
764 * If the check pass, we know that this is a re-issued request aborted
765 * previously in monitor_work because the ubq_daemon(cmd's task) is
766 * PF_EXITING. We cannot call io_uring_cmd_complete_in_task() anymore
767 * because this ioucmd's io_uring context may be freed now if no inflight
768 * ioucmd exists. Otherwise we may cause null-deref in ctx->fallback_work.
770 * Note: monitor_work sets UBLK_IO_FLAG_ABORTED and ends this request(releasing
771 * the tag). Then the request is re-started(allocating the tag) and we are here.
772 * Since releasing/allocating a tag implies smp_mb(), finding UBLK_IO_FLAG_ABORTED
773 * guarantees that here is a re-issued request aborted previously.
775 if ((io->flags & UBLK_IO_FLAG_ABORTED))
779 io_uring_cmd_complete_in_task(cmd, ublk_rq_task_work_cb);
785 static void ublk_commit_rqs(struct blk_mq_hw_ctx *hctx)
787 struct ublk_queue *ubq = hctx->driver_data;
789 if (ublk_can_use_task_work(ubq))
790 __set_notify_signal(ubq->ubq_daemon);
793 static int ublk_init_hctx(struct blk_mq_hw_ctx *hctx, void *driver_data,
794 unsigned int hctx_idx)
796 struct ublk_device *ub = driver_data;
797 struct ublk_queue *ubq = ublk_get_queue(ub, hctx->queue_num);
799 hctx->driver_data = ubq;
803 static int ublk_init_rq(struct blk_mq_tag_set *set, struct request *req,
804 unsigned int hctx_idx, unsigned int numa_node)
806 struct ublk_rq_data *data = blk_mq_rq_to_pdu(req);
808 init_task_work(&data->work, ublk_rq_task_work_fn);
812 static const struct blk_mq_ops ublk_mq_ops = {
813 .queue_rq = ublk_queue_rq,
814 .commit_rqs = ublk_commit_rqs,
815 .init_hctx = ublk_init_hctx,
816 .init_request = ublk_init_rq,
819 static int ublk_ch_open(struct inode *inode, struct file *filp)
821 struct ublk_device *ub = container_of(inode->i_cdev,
822 struct ublk_device, cdev);
824 if (test_and_set_bit(UB_STATE_OPEN, &ub->state))
826 filp->private_data = ub;
830 static int ublk_ch_release(struct inode *inode, struct file *filp)
832 struct ublk_device *ub = filp->private_data;
834 clear_bit(UB_STATE_OPEN, &ub->state);
838 /* map pre-allocated per-queue cmd buffer to ublksrv daemon */
839 static int ublk_ch_mmap(struct file *filp, struct vm_area_struct *vma)
841 struct ublk_device *ub = filp->private_data;
842 size_t sz = vma->vm_end - vma->vm_start;
843 unsigned max_sz = UBLK_MAX_QUEUE_DEPTH * sizeof(struct ublksrv_io_desc);
844 unsigned long pfn, end, phys_off = vma->vm_pgoff << PAGE_SHIFT;
847 spin_lock(&ub->mm_lock);
849 ub->mm = current->mm;
850 if (current->mm != ub->mm)
852 spin_unlock(&ub->mm_lock);
857 if (vma->vm_flags & VM_WRITE)
860 end = UBLKSRV_CMD_BUF_OFFSET + ub->dev_info.nr_hw_queues * max_sz;
861 if (phys_off < UBLKSRV_CMD_BUF_OFFSET || phys_off >= end)
864 q_id = (phys_off - UBLKSRV_CMD_BUF_OFFSET) / max_sz;
865 pr_devel("%s: qid %d, pid %d, addr %lx pg_off %lx sz %lu\n",
866 __func__, q_id, current->pid, vma->vm_start,
867 phys_off, (unsigned long)sz);
869 if (sz != ublk_queue_cmd_buf_size(ub, q_id))
872 pfn = virt_to_phys(ublk_queue_cmd_buf(ub, q_id)) >> PAGE_SHIFT;
873 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
876 static void ublk_commit_completion(struct ublk_device *ub,
877 struct ublksrv_io_cmd *ub_cmd)
879 u32 qid = ub_cmd->q_id, tag = ub_cmd->tag;
880 struct ublk_queue *ubq = ublk_get_queue(ub, qid);
881 struct ublk_io *io = &ubq->ios[tag];
884 /* now this cmd slot is owned by nbd driver */
885 io->flags &= ~UBLK_IO_FLAG_OWNED_BY_SRV;
886 io->res = ub_cmd->result;
888 /* find the io request and complete */
889 req = blk_mq_tag_to_rq(ub->tag_set.tags[qid], tag);
891 if (req && likely(!blk_should_fake_timeout(req->q)))
892 ublk_complete_rq(req);
896 * When ->ubq_daemon is exiting, either new request is ended immediately,
897 * or any queued io command is drained, so it is safe to abort queue
900 static void ublk_abort_queue(struct ublk_device *ub, struct ublk_queue *ubq)
904 if (!ublk_get_device(ub))
907 for (i = 0; i < ubq->q_depth; i++) {
908 struct ublk_io *io = &ubq->ios[i];
910 if (!(io->flags & UBLK_IO_FLAG_ACTIVE)) {
914 * Either we fail the request or ublk_rq_task_work_fn
917 rq = blk_mq_tag_to_rq(ub->tag_set.tags[ubq->q_id], i);
919 __ublk_fail_req(io, rq);
925 static void ublk_daemon_monitor_work(struct work_struct *work)
927 struct ublk_device *ub =
928 container_of(work, struct ublk_device, monitor_work.work);
931 for (i = 0; i < ub->dev_info.nr_hw_queues; i++) {
932 struct ublk_queue *ubq = ublk_get_queue(ub, i);
934 if (ubq_daemon_is_dying(ubq)) {
935 schedule_work(&ub->stop_work);
937 /* abort queue is for making forward progress */
938 ublk_abort_queue(ub, ubq);
943 * We can't schedule monitor work after ublk_remove() is started.
945 * No need ub->mutex, monitor work are canceled after state is marked
946 * as DEAD, so DEAD state is observed reliably.
948 if (ub->dev_info.state != UBLK_S_DEV_DEAD)
949 schedule_delayed_work(&ub->monitor_work,
950 UBLK_DAEMON_MONITOR_PERIOD);
953 static inline bool ublk_queue_ready(struct ublk_queue *ubq)
955 return ubq->nr_io_ready == ubq->q_depth;
958 static void ublk_cancel_queue(struct ublk_queue *ubq)
962 if (!ublk_queue_ready(ubq))
965 for (i = 0; i < ubq->q_depth; i++) {
966 struct ublk_io *io = &ubq->ios[i];
968 if (io->flags & UBLK_IO_FLAG_ACTIVE)
969 io_uring_cmd_done(io->cmd, UBLK_IO_RES_ABORT, 0);
972 /* all io commands are canceled */
973 ubq->nr_io_ready = 0;
976 /* Cancel all pending commands, must be called after del_gendisk() returns */
977 static void ublk_cancel_dev(struct ublk_device *ub)
981 for (i = 0; i < ub->dev_info.nr_hw_queues; i++)
982 ublk_cancel_queue(ublk_get_queue(ub, i));
985 static void ublk_stop_dev(struct ublk_device *ub)
987 mutex_lock(&ub->mutex);
988 if (ub->dev_info.state != UBLK_S_DEV_LIVE)
991 del_gendisk(ub->ub_disk);
992 ub->dev_info.state = UBLK_S_DEV_DEAD;
993 ub->dev_info.ublksrv_pid = -1;
994 put_disk(ub->ub_disk);
998 mutex_unlock(&ub->mutex);
999 cancel_delayed_work_sync(&ub->monitor_work);
1002 /* device can only be started after all IOs are ready */
1003 static void ublk_mark_io_ready(struct ublk_device *ub, struct ublk_queue *ubq)
1005 mutex_lock(&ub->mutex);
1007 if (ublk_queue_ready(ubq)) {
1008 ubq->ubq_daemon = current;
1009 get_task_struct(ubq->ubq_daemon);
1010 ub->nr_queues_ready++;
1012 if (ub->nr_queues_ready == ub->dev_info.nr_hw_queues)
1013 complete_all(&ub->completion);
1014 mutex_unlock(&ub->mutex);
1017 static void ublk_handle_need_get_data(struct ublk_device *ub, int q_id,
1018 int tag, struct io_uring_cmd *cmd)
1020 struct ublk_queue *ubq = ublk_get_queue(ub, q_id);
1021 struct request *req = blk_mq_tag_to_rq(ub->tag_set.tags[q_id], tag);
1023 if (ublk_can_use_task_work(ubq)) {
1024 struct ublk_rq_data *data = blk_mq_rq_to_pdu(req);
1026 /* should not fail since we call it just in ubq->ubq_daemon */
1027 task_work_add(ubq->ubq_daemon, &data->work, TWA_SIGNAL_NO_IPI);
1029 struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
1032 io_uring_cmd_complete_in_task(cmd, ublk_rq_task_work_cb);
1036 static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
1038 struct ublksrv_io_cmd *ub_cmd = (struct ublksrv_io_cmd *)cmd->cmd;
1039 struct ublk_device *ub = cmd->file->private_data;
1040 struct ublk_queue *ubq;
1042 u32 cmd_op = cmd->cmd_op;
1043 unsigned tag = ub_cmd->tag;
1046 pr_devel("%s: received: cmd op %d queue %d tag %d result %d\n",
1047 __func__, cmd->cmd_op, ub_cmd->q_id, tag,
1050 if (!(issue_flags & IO_URING_F_SQE128))
1053 if (ub_cmd->q_id >= ub->dev_info.nr_hw_queues)
1056 ubq = ublk_get_queue(ub, ub_cmd->q_id);
1057 if (!ubq || ub_cmd->q_id != ubq->q_id)
1060 if (ubq->ubq_daemon && ubq->ubq_daemon != current)
1063 if (tag >= ubq->q_depth)
1066 io = &ubq->ios[tag];
1068 /* there is pending io cmd, something must be wrong */
1069 if (io->flags & UBLK_IO_FLAG_ACTIVE) {
1075 * ensure that the user issues UBLK_IO_NEED_GET_DATA
1076 * iff the driver have set the UBLK_IO_FLAG_NEED_GET_DATA.
1078 if ((!!(io->flags & UBLK_IO_FLAG_NEED_GET_DATA))
1079 ^ (cmd_op == UBLK_IO_NEED_GET_DATA))
1083 case UBLK_IO_FETCH_REQ:
1084 /* UBLK_IO_FETCH_REQ is only allowed before queue is setup */
1085 if (ublk_queue_ready(ubq)) {
1090 * The io is being handled by server, so COMMIT_RQ is expected
1091 * instead of FETCH_REQ
1093 if (io->flags & UBLK_IO_FLAG_OWNED_BY_SRV)
1095 /* FETCH_RQ has to provide IO buffer */
1099 io->flags |= UBLK_IO_FLAG_ACTIVE;
1100 io->addr = ub_cmd->addr;
1102 ublk_mark_io_ready(ub, ubq);
1104 case UBLK_IO_COMMIT_AND_FETCH_REQ:
1105 /* FETCH_RQ has to provide IO buffer */
1108 if (!(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV))
1110 io->addr = ub_cmd->addr;
1111 io->flags |= UBLK_IO_FLAG_ACTIVE;
1113 ublk_commit_completion(ub, ub_cmd);
1115 case UBLK_IO_NEED_GET_DATA:
1116 if (!(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV))
1118 io->addr = ub_cmd->addr;
1120 io->flags |= UBLK_IO_FLAG_ACTIVE;
1121 ublk_handle_need_get_data(ub, ub_cmd->q_id, ub_cmd->tag, cmd);
1126 return -EIOCBQUEUED;
1129 io_uring_cmd_done(cmd, ret, 0);
1130 pr_devel("%s: complete: cmd op %d, tag %d ret %x io_flags %x\n",
1131 __func__, cmd_op, tag, ret, io->flags);
1132 return -EIOCBQUEUED;
1135 static const struct file_operations ublk_ch_fops = {
1136 .owner = THIS_MODULE,
1137 .open = ublk_ch_open,
1138 .release = ublk_ch_release,
1139 .llseek = no_llseek,
1140 .uring_cmd = ublk_ch_uring_cmd,
1141 .mmap = ublk_ch_mmap,
1144 static void ublk_deinit_queue(struct ublk_device *ub, int q_id)
1146 int size = ublk_queue_cmd_buf_size(ub, q_id);
1147 struct ublk_queue *ubq = ublk_get_queue(ub, q_id);
1149 if (ubq->ubq_daemon)
1150 put_task_struct(ubq->ubq_daemon);
1151 if (ubq->io_cmd_buf)
1152 free_pages((unsigned long)ubq->io_cmd_buf, get_order(size));
1155 static int ublk_init_queue(struct ublk_device *ub, int q_id)
1157 struct ublk_queue *ubq = ublk_get_queue(ub, q_id);
1158 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO;
1162 ubq->flags = ub->dev_info.flags;
1164 ubq->q_depth = ub->dev_info.queue_depth;
1165 size = ublk_queue_cmd_buf_size(ub, q_id);
1167 ptr = (void *) __get_free_pages(gfp_flags, get_order(size));
1171 ubq->io_cmd_buf = ptr;
1176 static void ublk_deinit_queues(struct ublk_device *ub)
1178 int nr_queues = ub->dev_info.nr_hw_queues;
1184 for (i = 0; i < nr_queues; i++)
1185 ublk_deinit_queue(ub, i);
1186 kfree(ub->__queues);
1189 static int ublk_init_queues(struct ublk_device *ub)
1191 int nr_queues = ub->dev_info.nr_hw_queues;
1192 int depth = ub->dev_info.queue_depth;
1193 int ubq_size = sizeof(struct ublk_queue) + depth * sizeof(struct ublk_io);
1194 int i, ret = -ENOMEM;
1196 ub->queue_size = ubq_size;
1197 ub->__queues = kcalloc(nr_queues, ubq_size, GFP_KERNEL);
1201 for (i = 0; i < nr_queues; i++) {
1202 if (ublk_init_queue(ub, i))
1206 init_completion(&ub->completion);
1210 ublk_deinit_queues(ub);
1214 static int ublk_alloc_dev_number(struct ublk_device *ub, int idx)
1219 spin_lock(&ublk_idr_lock);
1220 /* allocate id, if @id >= 0, we're requesting that specific id */
1222 err = idr_alloc(&ublk_index_idr, ub, i, i + 1, GFP_NOWAIT);
1226 err = idr_alloc(&ublk_index_idr, ub, 0, 0, GFP_NOWAIT);
1228 spin_unlock(&ublk_idr_lock);
1231 ub->ub_number = err;
1236 static void ublk_free_dev_number(struct ublk_device *ub)
1238 spin_lock(&ublk_idr_lock);
1239 idr_remove(&ublk_index_idr, ub->ub_number);
1240 wake_up_all(&ublk_idr_wq);
1241 spin_unlock(&ublk_idr_lock);
1244 static void ublk_cdev_rel(struct device *dev)
1246 struct ublk_device *ub = container_of(dev, struct ublk_device, cdev_dev);
1248 blk_mq_free_tag_set(&ub->tag_set);
1249 ublk_deinit_queues(ub);
1250 ublk_free_dev_number(ub);
1251 mutex_destroy(&ub->mutex);
1255 static int ublk_add_chdev(struct ublk_device *ub)
1257 struct device *dev = &ub->cdev_dev;
1258 int minor = ub->ub_number;
1261 dev->parent = ublk_misc.this_device;
1262 dev->devt = MKDEV(MAJOR(ublk_chr_devt), minor);
1263 dev->class = ublk_chr_class;
1264 dev->release = ublk_cdev_rel;
1265 device_initialize(dev);
1267 ret = dev_set_name(dev, "ublkc%d", minor);
1271 cdev_init(&ub->cdev, &ublk_ch_fops);
1272 ret = cdev_device_add(&ub->cdev, dev);
1281 static void ublk_stop_work_fn(struct work_struct *work)
1283 struct ublk_device *ub =
1284 container_of(work, struct ublk_device, stop_work);
1289 /* align max io buffer size with PAGE_SIZE */
1290 static void ublk_align_max_io_size(struct ublk_device *ub)
1292 unsigned int max_io_bytes = ub->dev_info.max_io_buf_bytes;
1294 ub->dev_info.max_io_buf_bytes =
1295 round_down(max_io_bytes, PAGE_SIZE);
1298 static int ublk_add_tag_set(struct ublk_device *ub)
1300 ub->tag_set.ops = &ublk_mq_ops;
1301 ub->tag_set.nr_hw_queues = ub->dev_info.nr_hw_queues;
1302 ub->tag_set.queue_depth = ub->dev_info.queue_depth;
1303 ub->tag_set.numa_node = NUMA_NO_NODE;
1304 ub->tag_set.cmd_size = sizeof(struct ublk_rq_data);
1305 ub->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1306 ub->tag_set.driver_data = ub;
1307 return blk_mq_alloc_tag_set(&ub->tag_set);
1310 static void ublk_remove(struct ublk_device *ub)
1313 cancel_work_sync(&ub->stop_work);
1314 cdev_device_del(&ub->cdev, &ub->cdev_dev);
1315 put_device(&ub->cdev_dev);
1318 static struct ublk_device *ublk_get_device_from_id(int idx)
1320 struct ublk_device *ub = NULL;
1325 spin_lock(&ublk_idr_lock);
1326 ub = idr_find(&ublk_index_idr, idx);
1328 ub = ublk_get_device(ub);
1329 spin_unlock(&ublk_idr_lock);
1334 static int ublk_ctrl_start_dev(struct io_uring_cmd *cmd)
1336 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
1337 int ublksrv_pid = (int)header->data[0];
1338 struct ublk_device *ub;
1339 struct gendisk *disk;
1342 if (ublksrv_pid <= 0)
1345 ub = ublk_get_device_from_id(header->dev_id);
1349 wait_for_completion_interruptible(&ub->completion);
1351 schedule_delayed_work(&ub->monitor_work, UBLK_DAEMON_MONITOR_PERIOD);
1353 mutex_lock(&ub->mutex);
1354 if (ub->dev_info.state == UBLK_S_DEV_LIVE ||
1355 test_bit(UB_STATE_USED, &ub->state)) {
1360 disk = blk_mq_alloc_disk(&ub->tag_set, ub);
1362 ret = PTR_ERR(disk);
1365 sprintf(disk->disk_name, "ublkb%d", ub->ub_number);
1366 disk->fops = &ub_fops;
1367 disk->private_data = ub;
1369 ub->dev_info.ublksrv_pid = ublksrv_pid;
1372 ret = ublk_apply_params(ub);
1376 get_device(&ub->cdev_dev);
1377 ret = add_disk(disk);
1380 * Has to drop the reference since ->free_disk won't be
1381 * called in case of add_disk failure.
1383 ublk_put_device(ub);
1386 set_bit(UB_STATE_USED, &ub->state);
1387 ub->dev_info.state = UBLK_S_DEV_LIVE;
1392 mutex_unlock(&ub->mutex);
1393 ublk_put_device(ub);
1397 static int ublk_ctrl_get_queue_affinity(struct io_uring_cmd *cmd)
1399 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
1400 void __user *argp = (void __user *)(unsigned long)header->addr;
1401 struct ublk_device *ub;
1402 cpumask_var_t cpumask;
1403 unsigned long queue;
1404 unsigned int retlen;
1408 if (header->len * BITS_PER_BYTE < nr_cpu_ids)
1410 if (header->len & (sizeof(unsigned long)-1))
1415 ub = ublk_get_device_from_id(header->dev_id);
1419 queue = header->data[0];
1420 if (queue >= ub->dev_info.nr_hw_queues)
1421 goto out_put_device;
1424 if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL))
1425 goto out_put_device;
1427 for_each_possible_cpu(i) {
1428 if (ub->tag_set.map[HCTX_TYPE_DEFAULT].mq_map[i] == queue)
1429 cpumask_set_cpu(i, cpumask);
1433 retlen = min_t(unsigned short, header->len, cpumask_size());
1434 if (copy_to_user(argp, cpumask, retlen))
1435 goto out_free_cpumask;
1436 if (retlen != header->len &&
1437 clear_user(argp + retlen, header->len - retlen))
1438 goto out_free_cpumask;
1442 free_cpumask_var(cpumask);
1444 ublk_put_device(ub);
1448 static inline void ublk_dump_dev_info(struct ublksrv_ctrl_dev_info *info)
1450 pr_devel("%s: dev id %d flags %llx\n", __func__,
1451 info->dev_id, info->flags);
1452 pr_devel("\t nr_hw_queues %d queue_depth %d\n",
1453 info->nr_hw_queues, info->queue_depth);
1456 static int ublk_ctrl_add_dev(struct io_uring_cmd *cmd)
1458 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
1459 void __user *argp = (void __user *)(unsigned long)header->addr;
1460 struct ublksrv_ctrl_dev_info info;
1461 struct ublk_device *ub;
1464 if (header->len < sizeof(info) || !header->addr)
1466 if (header->queue_id != (u16)-1) {
1467 pr_warn("%s: queue_id is wrong %x\n",
1468 __func__, header->queue_id);
1471 if (copy_from_user(&info, argp, sizeof(info)))
1473 ublk_dump_dev_info(&info);
1474 if (header->dev_id != info.dev_id) {
1475 pr_warn("%s: dev id not match %u %u\n",
1476 __func__, header->dev_id, info.dev_id);
1480 ret = mutex_lock_killable(&ublk_ctl_mutex);
1485 ub = kzalloc(sizeof(*ub), GFP_KERNEL);
1488 mutex_init(&ub->mutex);
1489 spin_lock_init(&ub->mm_lock);
1490 INIT_WORK(&ub->stop_work, ublk_stop_work_fn);
1491 INIT_DELAYED_WORK(&ub->monitor_work, ublk_daemon_monitor_work);
1493 ret = ublk_alloc_dev_number(ub, header->dev_id);
1497 memcpy(&ub->dev_info, &info, sizeof(info));
1499 /* update device id */
1500 ub->dev_info.dev_id = ub->ub_number;
1503 * 64bit flags will be copied back to userspace as feature
1504 * negotiation result, so have to clear flags which driver
1505 * doesn't support yet, then userspace can get correct flags
1506 * (features) to handle.
1508 ub->dev_info.flags &= UBLK_F_ALL;
1510 /* We are not ready to support zero copy */
1511 ub->dev_info.flags &= ~UBLK_F_SUPPORT_ZERO_COPY;
1513 ub->dev_info.nr_hw_queues = min_t(unsigned int,
1514 ub->dev_info.nr_hw_queues, nr_cpu_ids);
1515 ublk_align_max_io_size(ub);
1517 ret = ublk_init_queues(ub);
1519 goto out_free_dev_number;
1521 ret = ublk_add_tag_set(ub);
1523 goto out_deinit_queues;
1526 if (copy_to_user(argp, &ub->dev_info, sizeof(info)))
1527 goto out_free_tag_set;
1530 * Add the char dev so that ublksrv daemon can be setup.
1531 * ublk_add_chdev() will cleanup everything if it fails.
1533 ret = ublk_add_chdev(ub);
1537 blk_mq_free_tag_set(&ub->tag_set);
1539 ublk_deinit_queues(ub);
1540 out_free_dev_number:
1541 ublk_free_dev_number(ub);
1543 mutex_destroy(&ub->mutex);
1546 mutex_unlock(&ublk_ctl_mutex);
1550 static inline bool ublk_idr_freed(int id)
1554 spin_lock(&ublk_idr_lock);
1555 ptr = idr_find(&ublk_index_idr, id);
1556 spin_unlock(&ublk_idr_lock);
1561 static int ublk_ctrl_del_dev(int idx)
1563 struct ublk_device *ub;
1566 ret = mutex_lock_killable(&ublk_ctl_mutex);
1570 ub = ublk_get_device_from_id(idx);
1573 ublk_put_device(ub);
1580 * Wait until the idr is removed, then it can be reused after
1581 * DEL_DEV command is returned.
1584 wait_event(ublk_idr_wq, ublk_idr_freed(idx));
1585 mutex_unlock(&ublk_ctl_mutex);
1590 static inline void ublk_ctrl_cmd_dump(struct io_uring_cmd *cmd)
1592 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
1594 pr_devel("%s: cmd_op %x, dev id %d qid %d data %llx buf %llx len %u\n",
1595 __func__, cmd->cmd_op, header->dev_id, header->queue_id,
1596 header->data[0], header->addr, header->len);
1599 static int ublk_ctrl_stop_dev(struct io_uring_cmd *cmd)
1601 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
1602 struct ublk_device *ub;
1604 ub = ublk_get_device_from_id(header->dev_id);
1609 cancel_work_sync(&ub->stop_work);
1611 ublk_put_device(ub);
1615 static int ublk_ctrl_get_dev_info(struct io_uring_cmd *cmd)
1617 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
1618 void __user *argp = (void __user *)(unsigned long)header->addr;
1619 struct ublk_device *ub;
1622 if (header->len < sizeof(struct ublksrv_ctrl_dev_info) || !header->addr)
1625 ub = ublk_get_device_from_id(header->dev_id);
1629 if (copy_to_user(argp, &ub->dev_info, sizeof(ub->dev_info)))
1631 ublk_put_device(ub);
1636 static int ublk_ctrl_get_params(struct io_uring_cmd *cmd)
1638 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
1639 void __user *argp = (void __user *)(unsigned long)header->addr;
1640 struct ublk_params_header ph;
1641 struct ublk_device *ub;
1644 if (header->len <= sizeof(ph) || !header->addr)
1647 if (copy_from_user(&ph, argp, sizeof(ph)))
1650 if (ph.len > header->len || !ph.len)
1653 if (ph.len > sizeof(struct ublk_params))
1654 ph.len = sizeof(struct ublk_params);
1656 ub = ublk_get_device_from_id(header->dev_id);
1660 mutex_lock(&ub->mutex);
1661 if (copy_to_user(argp, &ub->params, ph.len))
1665 mutex_unlock(&ub->mutex);
1667 ublk_put_device(ub);
1671 static int ublk_ctrl_set_params(struct io_uring_cmd *cmd)
1673 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
1674 void __user *argp = (void __user *)(unsigned long)header->addr;
1675 struct ublk_params_header ph;
1676 struct ublk_device *ub;
1679 if (header->len <= sizeof(ph) || !header->addr)
1682 if (copy_from_user(&ph, argp, sizeof(ph)))
1685 if (ph.len > header->len || !ph.len || !ph.types)
1688 if (ph.len > sizeof(struct ublk_params))
1689 ph.len = sizeof(struct ublk_params);
1691 ub = ublk_get_device_from_id(header->dev_id);
1695 /* parameters can only be changed when device isn't live */
1696 mutex_lock(&ub->mutex);
1697 if (ub->dev_info.state == UBLK_S_DEV_LIVE) {
1699 } else if (copy_from_user(&ub->params, argp, ph.len)) {
1702 /* clear all we don't support yet */
1703 ub->params.types &= UBLK_PARAM_TYPE_ALL;
1704 ret = ublk_validate_params(ub);
1706 mutex_unlock(&ub->mutex);
1707 ublk_put_device(ub);
1712 static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd,
1713 unsigned int issue_flags)
1715 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
1718 ublk_ctrl_cmd_dump(cmd);
1720 if (!(issue_flags & IO_URING_F_SQE128))
1724 if (!capable(CAP_SYS_ADMIN))
1728 switch (cmd->cmd_op) {
1729 case UBLK_CMD_START_DEV:
1730 ret = ublk_ctrl_start_dev(cmd);
1732 case UBLK_CMD_STOP_DEV:
1733 ret = ublk_ctrl_stop_dev(cmd);
1735 case UBLK_CMD_GET_DEV_INFO:
1736 ret = ublk_ctrl_get_dev_info(cmd);
1738 case UBLK_CMD_ADD_DEV:
1739 ret = ublk_ctrl_add_dev(cmd);
1741 case UBLK_CMD_DEL_DEV:
1742 ret = ublk_ctrl_del_dev(header->dev_id);
1744 case UBLK_CMD_GET_QUEUE_AFFINITY:
1745 ret = ublk_ctrl_get_queue_affinity(cmd);
1747 case UBLK_CMD_GET_PARAMS:
1748 ret = ublk_ctrl_get_params(cmd);
1750 case UBLK_CMD_SET_PARAMS:
1751 ret = ublk_ctrl_set_params(cmd);
1757 io_uring_cmd_done(cmd, ret, 0);
1758 pr_devel("%s: cmd done ret %d cmd_op %x, dev id %d qid %d\n",
1759 __func__, ret, cmd->cmd_op, header->dev_id, header->queue_id);
1760 return -EIOCBQUEUED;
1763 static const struct file_operations ublk_ctl_fops = {
1764 .open = nonseekable_open,
1765 .uring_cmd = ublk_ctrl_uring_cmd,
1766 .owner = THIS_MODULE,
1767 .llseek = noop_llseek,
1770 static struct miscdevice ublk_misc = {
1771 .minor = MISC_DYNAMIC_MINOR,
1772 .name = "ublk-control",
1773 .fops = &ublk_ctl_fops,
1776 static int __init ublk_init(void)
1780 init_waitqueue_head(&ublk_idr_wq);
1782 ret = misc_register(&ublk_misc);
1786 ret = alloc_chrdev_region(&ublk_chr_devt, 0, UBLK_MINORS, "ublk-char");
1788 goto unregister_mis;
1790 ublk_chr_class = class_create(THIS_MODULE, "ublk-char");
1791 if (IS_ERR(ublk_chr_class)) {
1792 ret = PTR_ERR(ublk_chr_class);
1793 goto free_chrdev_region;
1798 unregister_chrdev_region(ublk_chr_devt, UBLK_MINORS);
1800 misc_deregister(&ublk_misc);
1804 static void __exit ublk_exit(void)
1806 struct ublk_device *ub;
1809 class_destroy(ublk_chr_class);
1811 misc_deregister(&ublk_misc);
1813 idr_for_each_entry(&ublk_index_idr, ub, id)
1816 idr_destroy(&ublk_index_idr);
1817 unregister_chrdev_region(ublk_chr_devt, UBLK_MINORS);
1820 module_init(ublk_init);
1821 module_exit(ublk_exit);
1823 MODULE_AUTHOR("Ming Lei <ming.lei@redhat.com>");
1824 MODULE_LICENSE("GPL");