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/cmd.h>
40 #include <linux/blk-mq.h>
41 #include <linux/delay.h>
44 #include <linux/task_work.h>
45 #include <linux/namei.h>
46 #include <linux/kref.h>
47 #include <uapi/linux/ublk_cmd.h>
49 #define UBLK_MINORS (1U << MINORBITS)
51 /* private ioctl command mirror */
52 #define UBLK_CMD_DEL_DEV_ASYNC _IOC_NR(UBLK_U_CMD_DEL_DEV_ASYNC)
54 /* All UBLK_F_* have to be included into UBLK_F_ALL */
55 #define UBLK_F_ALL (UBLK_F_SUPPORT_ZERO_COPY \
56 | UBLK_F_URING_CMD_COMP_IN_TASK \
57 | UBLK_F_NEED_GET_DATA \
58 | UBLK_F_USER_RECOVERY \
59 | UBLK_F_USER_RECOVERY_REISSUE \
60 | UBLK_F_UNPRIVILEGED_DEV \
61 | UBLK_F_CMD_IOCTL_ENCODE \
65 /* All UBLK_PARAM_TYPE_* should be included here */
66 #define UBLK_PARAM_TYPE_ALL \
67 (UBLK_PARAM_TYPE_BASIC | UBLK_PARAM_TYPE_DISCARD | \
68 UBLK_PARAM_TYPE_DEVT | UBLK_PARAM_TYPE_ZONED)
71 struct llist_node node
;
76 struct ublk_uring_cmd_pdu
{
77 struct ublk_queue
*ubq
;
82 * io command is active: sqe cmd is received, and its cqe isn't done
84 * If the flag is set, the io command is owned by ublk driver, and waited
85 * for incoming blk-mq request from the ublk block device.
87 * If the flag is cleared, the io command will be completed, and owned by
90 #define UBLK_IO_FLAG_ACTIVE 0x01
93 * IO command is completed via cqe, and it is being handled by ublksrv, and
96 * Basically exclusively with UBLK_IO_FLAG_ACTIVE, so can be served for
99 #define UBLK_IO_FLAG_OWNED_BY_SRV 0x02
102 * IO command is aborted, so this flag is set in case of
103 * !UBLK_IO_FLAG_ACTIVE.
105 * After this flag is observed, any pending or new incoming request
106 * associated with this io command will be failed immediately
108 #define UBLK_IO_FLAG_ABORTED 0x04
111 * UBLK_IO_FLAG_NEED_GET_DATA is set because IO command requires
112 * get data buffer address from ublksrv.
114 * Then, bio data could be copied into this data buffer for a WRITE request
115 * after the IO command is issued again and UBLK_IO_FLAG_NEED_GET_DATA is unset.
117 #define UBLK_IO_FLAG_NEED_GET_DATA 0x08
119 /* atomic RW with ubq->cancel_lock */
120 #define UBLK_IO_FLAG_CANCELED 0x80000000
123 /* userspace buffer address from io cmd */
128 struct io_uring_cmd
*cmd
;
136 struct task_struct
*ubq_daemon
;
139 struct llist_head io_cmds
;
141 unsigned long io_addr
; /* mapped vm address */
142 unsigned int max_io_sz
;
146 unsigned short nr_io_ready
; /* how many ios setup */
147 spinlock_t cancel_lock
;
148 struct ublk_device
*dev
;
149 struct ublk_io ios
[];
153 struct gendisk
*ub_disk
;
157 unsigned int queue_size
;
158 struct ublksrv_ctrl_dev_info dev_info
;
160 struct blk_mq_tag_set tag_set
;
163 struct device cdev_dev
;
165 #define UB_STATE_OPEN 0
166 #define UB_STATE_USED 1
167 #define UB_STATE_DELETED 2
174 struct mm_struct
*mm
;
176 struct ublk_params params
;
178 struct completion completion
;
179 unsigned int nr_queues_ready
;
180 unsigned int nr_privileged_daemon
;
182 struct work_struct quiesce_work
;
183 struct work_struct stop_work
;
186 /* header of ublk_params */
187 struct ublk_params_header
{
192 static bool ublk_abort_requests(struct ublk_device
*ub
, struct ublk_queue
*ubq
);
194 static inline unsigned int ublk_req_build_flags(struct request
*req
);
195 static inline struct ublksrv_io_desc
*ublk_get_iod(struct ublk_queue
*ubq
,
197 static inline bool ublk_dev_is_user_copy(const struct ublk_device
*ub
)
199 return ub
->dev_info
.flags
& UBLK_F_USER_COPY
;
202 static inline bool ublk_dev_is_zoned(const struct ublk_device
*ub
)
204 return ub
->dev_info
.flags
& UBLK_F_ZONED
;
207 static inline bool ublk_queue_is_zoned(struct ublk_queue
*ubq
)
209 return ubq
->flags
& UBLK_F_ZONED
;
212 #ifdef CONFIG_BLK_DEV_ZONED
214 struct ublk_zoned_report_desc
{
220 static DEFINE_XARRAY(ublk_zoned_report_descs
);
222 static int ublk_zoned_insert_report_desc(const struct request
*req
,
223 struct ublk_zoned_report_desc
*desc
)
225 return xa_insert(&ublk_zoned_report_descs
, (unsigned long)req
,
229 static struct ublk_zoned_report_desc
*ublk_zoned_erase_report_desc(
230 const struct request
*req
)
232 return xa_erase(&ublk_zoned_report_descs
, (unsigned long)req
);
235 static struct ublk_zoned_report_desc
*ublk_zoned_get_report_desc(
236 const struct request
*req
)
238 return xa_load(&ublk_zoned_report_descs
, (unsigned long)req
);
241 static int ublk_get_nr_zones(const struct ublk_device
*ub
)
243 const struct ublk_param_basic
*p
= &ub
->params
.basic
;
245 /* Zone size is a power of 2 */
246 return p
->dev_sectors
>> ilog2(p
->chunk_sectors
);
249 static int ublk_revalidate_disk_zones(struct ublk_device
*ub
)
251 return blk_revalidate_disk_zones(ub
->ub_disk
);
254 static int ublk_dev_param_zoned_validate(const struct ublk_device
*ub
)
256 const struct ublk_param_zoned
*p
= &ub
->params
.zoned
;
259 if (!ublk_dev_is_zoned(ub
))
262 if (!p
->max_zone_append_sectors
)
265 nr_zones
= ublk_get_nr_zones(ub
);
267 if (p
->max_active_zones
> nr_zones
)
270 if (p
->max_open_zones
> nr_zones
)
276 static void ublk_dev_param_zoned_apply(struct ublk_device
*ub
)
278 ub
->ub_disk
->nr_zones
= ublk_get_nr_zones(ub
);
281 /* Based on virtblk_alloc_report_buffer */
282 static void *ublk_alloc_report_buffer(struct ublk_device
*ublk
,
283 unsigned int nr_zones
, size_t *buflen
)
285 struct request_queue
*q
= ublk
->ub_disk
->queue
;
289 nr_zones
= min_t(unsigned int, nr_zones
,
290 ublk
->ub_disk
->nr_zones
);
292 bufsize
= nr_zones
* sizeof(struct blk_zone
);
294 min_t(size_t, bufsize
, queue_max_hw_sectors(q
) << SECTOR_SHIFT
);
296 while (bufsize
>= sizeof(struct blk_zone
)) {
297 buf
= kvmalloc(bufsize
, GFP_KERNEL
| __GFP_NORETRY
);
309 static int ublk_report_zones(struct gendisk
*disk
, sector_t sector
,
310 unsigned int nr_zones
, report_zones_cb cb
, void *data
)
312 struct ublk_device
*ub
= disk
->private_data
;
313 unsigned int zone_size_sectors
= disk
->queue
->limits
.chunk_sectors
;
314 unsigned int first_zone
= sector
>> ilog2(zone_size_sectors
);
315 unsigned int done_zones
= 0;
316 unsigned int max_zones_per_request
;
318 struct blk_zone
*buffer
;
319 size_t buffer_length
;
321 nr_zones
= min_t(unsigned int, ub
->ub_disk
->nr_zones
- first_zone
,
324 buffer
= ublk_alloc_report_buffer(ub
, nr_zones
, &buffer_length
);
328 max_zones_per_request
= buffer_length
/ sizeof(struct blk_zone
);
330 while (done_zones
< nr_zones
) {
331 unsigned int remaining_zones
= nr_zones
- done_zones
;
332 unsigned int zones_in_request
=
333 min_t(unsigned int, remaining_zones
, max_zones_per_request
);
335 struct ublk_zoned_report_desc desc
;
338 memset(buffer
, 0, buffer_length
);
340 req
= blk_mq_alloc_request(disk
->queue
, REQ_OP_DRV_IN
, 0);
346 desc
.operation
= UBLK_IO_OP_REPORT_ZONES
;
347 desc
.sector
= sector
;
348 desc
.nr_zones
= zones_in_request
;
349 ret
= ublk_zoned_insert_report_desc(req
, &desc
);
353 ret
= blk_rq_map_kern(disk
->queue
, req
, buffer
, buffer_length
,
358 status
= blk_execute_rq(req
, 0);
359 ret
= blk_status_to_errno(status
);
361 ublk_zoned_erase_report_desc(req
);
363 blk_mq_free_request(req
);
367 for (unsigned int i
= 0; i
< zones_in_request
; i
++) {
368 struct blk_zone
*zone
= buffer
+ i
;
370 /* A zero length zone means no more zones in this response */
374 ret
= cb(zone
, i
, data
);
379 sector
+= zone_size_sectors
;
391 static blk_status_t
ublk_setup_iod_zoned(struct ublk_queue
*ubq
,
394 struct ublksrv_io_desc
*iod
= ublk_get_iod(ubq
, req
->tag
);
395 struct ublk_io
*io
= &ubq
->ios
[req
->tag
];
396 struct ublk_zoned_report_desc
*desc
;
399 switch (req_op(req
)) {
400 case REQ_OP_ZONE_OPEN
:
401 ublk_op
= UBLK_IO_OP_ZONE_OPEN
;
403 case REQ_OP_ZONE_CLOSE
:
404 ublk_op
= UBLK_IO_OP_ZONE_CLOSE
;
406 case REQ_OP_ZONE_FINISH
:
407 ublk_op
= UBLK_IO_OP_ZONE_FINISH
;
409 case REQ_OP_ZONE_RESET
:
410 ublk_op
= UBLK_IO_OP_ZONE_RESET
;
412 case REQ_OP_ZONE_APPEND
:
413 ublk_op
= UBLK_IO_OP_ZONE_APPEND
;
415 case REQ_OP_ZONE_RESET_ALL
:
416 ublk_op
= UBLK_IO_OP_ZONE_RESET_ALL
;
419 desc
= ublk_zoned_get_report_desc(req
);
421 return BLK_STS_IOERR
;
422 ublk_op
= desc
->operation
;
424 case UBLK_IO_OP_REPORT_ZONES
:
425 iod
->op_flags
= ublk_op
| ublk_req_build_flags(req
);
426 iod
->nr_zones
= desc
->nr_zones
;
427 iod
->start_sector
= desc
->sector
;
430 return BLK_STS_IOERR
;
433 /* We do not support drv_out */
434 return BLK_STS_NOTSUPP
;
436 return BLK_STS_IOERR
;
439 iod
->op_flags
= ublk_op
| ublk_req_build_flags(req
);
440 iod
->nr_sectors
= blk_rq_sectors(req
);
441 iod
->start_sector
= blk_rq_pos(req
);
442 iod
->addr
= io
->addr
;
449 #define ublk_report_zones (NULL)
451 static int ublk_dev_param_zoned_validate(const struct ublk_device
*ub
)
456 static void ublk_dev_param_zoned_apply(struct ublk_device
*ub
)
460 static int ublk_revalidate_disk_zones(struct ublk_device
*ub
)
465 static blk_status_t
ublk_setup_iod_zoned(struct ublk_queue
*ubq
,
468 return BLK_STS_NOTSUPP
;
473 static inline void __ublk_complete_rq(struct request
*req
);
474 static void ublk_complete_rq(struct kref
*ref
);
476 static dev_t ublk_chr_devt
;
477 static const struct class ublk_chr_class
= {
481 static DEFINE_IDR(ublk_index_idr
);
482 static DEFINE_SPINLOCK(ublk_idr_lock
);
483 static wait_queue_head_t ublk_idr_wq
; /* wait until one idr is freed */
485 static DEFINE_MUTEX(ublk_ctl_mutex
);
488 * Max ublk devices allowed to add
490 * It can be extended to one per-user limit in future or even controlled
493 #define UBLK_MAX_UBLKS UBLK_MINORS
494 static unsigned int ublks_max
= 64;
495 static unsigned int ublks_added
; /* protected by ublk_ctl_mutex */
497 static struct miscdevice ublk_misc
;
499 static inline unsigned ublk_pos_to_hwq(loff_t pos
)
501 return ((pos
- UBLKSRV_IO_BUF_OFFSET
) >> UBLK_QID_OFF
) &
505 static inline unsigned ublk_pos_to_buf_off(loff_t pos
)
507 return (pos
- UBLKSRV_IO_BUF_OFFSET
) & UBLK_IO_BUF_BITS_MASK
;
510 static inline unsigned ublk_pos_to_tag(loff_t pos
)
512 return ((pos
- UBLKSRV_IO_BUF_OFFSET
) >> UBLK_TAG_OFF
) &
516 static void ublk_dev_param_basic_apply(struct ublk_device
*ub
)
518 const struct ublk_param_basic
*p
= &ub
->params
.basic
;
520 if (p
->attrs
& UBLK_ATTR_READ_ONLY
)
521 set_disk_ro(ub
->ub_disk
, true);
523 set_capacity(ub
->ub_disk
, p
->dev_sectors
);
526 static int ublk_validate_params(const struct ublk_device
*ub
)
528 /* basic param is the only one which must be set */
529 if (ub
->params
.types
& UBLK_PARAM_TYPE_BASIC
) {
530 const struct ublk_param_basic
*p
= &ub
->params
.basic
;
532 if (p
->logical_bs_shift
> PAGE_SHIFT
|| p
->logical_bs_shift
< 9)
535 if (p
->logical_bs_shift
> p
->physical_bs_shift
)
538 if (p
->max_sectors
> (ub
->dev_info
.max_io_buf_bytes
>> 9))
541 if (ublk_dev_is_zoned(ub
) && !p
->chunk_sectors
)
546 if (ub
->params
.types
& UBLK_PARAM_TYPE_DISCARD
) {
547 const struct ublk_param_discard
*p
= &ub
->params
.discard
;
549 /* So far, only support single segment discard */
550 if (p
->max_discard_sectors
&& p
->max_discard_segments
!= 1)
553 if (!p
->discard_granularity
)
557 /* dev_t is read-only */
558 if (ub
->params
.types
& UBLK_PARAM_TYPE_DEVT
)
561 if (ub
->params
.types
& UBLK_PARAM_TYPE_ZONED
)
562 return ublk_dev_param_zoned_validate(ub
);
563 else if (ublk_dev_is_zoned(ub
))
569 static void ublk_apply_params(struct ublk_device
*ub
)
571 ublk_dev_param_basic_apply(ub
);
573 if (ub
->params
.types
& UBLK_PARAM_TYPE_ZONED
)
574 ublk_dev_param_zoned_apply(ub
);
577 static inline bool ublk_support_user_copy(const struct ublk_queue
*ubq
)
579 return ubq
->flags
& UBLK_F_USER_COPY
;
582 static inline bool ublk_need_req_ref(const struct ublk_queue
*ubq
)
585 * read()/write() is involved in user copy, so request reference
588 return ublk_support_user_copy(ubq
);
591 static inline void ublk_init_req_ref(const struct ublk_queue
*ubq
,
594 if (ublk_need_req_ref(ubq
)) {
595 struct ublk_rq_data
*data
= blk_mq_rq_to_pdu(req
);
597 kref_init(&data
->ref
);
601 static inline bool ublk_get_req_ref(const struct ublk_queue
*ubq
,
604 if (ublk_need_req_ref(ubq
)) {
605 struct ublk_rq_data
*data
= blk_mq_rq_to_pdu(req
);
607 return kref_get_unless_zero(&data
->ref
);
613 static inline void ublk_put_req_ref(const struct ublk_queue
*ubq
,
616 if (ublk_need_req_ref(ubq
)) {
617 struct ublk_rq_data
*data
= blk_mq_rq_to_pdu(req
);
619 kref_put(&data
->ref
, ublk_complete_rq
);
621 __ublk_complete_rq(req
);
625 static inline bool ublk_need_get_data(const struct ublk_queue
*ubq
)
627 return ubq
->flags
& UBLK_F_NEED_GET_DATA
;
630 /* Called in slow path only, keep it noinline for trace purpose */
631 static noinline
struct ublk_device
*ublk_get_device(struct ublk_device
*ub
)
633 if (kobject_get_unless_zero(&ub
->cdev_dev
.kobj
))
638 /* Called in slow path only, keep it noinline for trace purpose */
639 static noinline
void ublk_put_device(struct ublk_device
*ub
)
641 put_device(&ub
->cdev_dev
);
644 static inline struct ublk_queue
*ublk_get_queue(struct ublk_device
*dev
,
647 return (struct ublk_queue
*)&(dev
->__queues
[qid
* dev
->queue_size
]);
650 static inline bool ublk_rq_has_data(const struct request
*rq
)
652 return bio_has_data(rq
->bio
);
655 static inline struct ublksrv_io_desc
*ublk_get_iod(struct ublk_queue
*ubq
,
658 return (struct ublksrv_io_desc
*)
659 &(ubq
->io_cmd_buf
[tag
* sizeof(struct ublksrv_io_desc
)]);
662 static inline char *ublk_queue_cmd_buf(struct ublk_device
*ub
, int q_id
)
664 return ublk_get_queue(ub
, q_id
)->io_cmd_buf
;
667 static inline int ublk_queue_cmd_buf_size(struct ublk_device
*ub
, int q_id
)
669 struct ublk_queue
*ubq
= ublk_get_queue(ub
, q_id
);
671 return round_up(ubq
->q_depth
* sizeof(struct ublksrv_io_desc
),
675 static inline bool ublk_queue_can_use_recovery_reissue(
676 struct ublk_queue
*ubq
)
678 return (ubq
->flags
& UBLK_F_USER_RECOVERY
) &&
679 (ubq
->flags
& UBLK_F_USER_RECOVERY_REISSUE
);
682 static inline bool ublk_queue_can_use_recovery(
683 struct ublk_queue
*ubq
)
685 return ubq
->flags
& UBLK_F_USER_RECOVERY
;
688 static inline bool ublk_can_use_recovery(struct ublk_device
*ub
)
690 return ub
->dev_info
.flags
& UBLK_F_USER_RECOVERY
;
693 static void ublk_free_disk(struct gendisk
*disk
)
695 struct ublk_device
*ub
= disk
->private_data
;
697 clear_bit(UB_STATE_USED
, &ub
->state
);
701 static void ublk_store_owner_uid_gid(unsigned int *owner_uid
,
702 unsigned int *owner_gid
)
707 current_uid_gid(&uid
, &gid
);
709 *owner_uid
= from_kuid(&init_user_ns
, uid
);
710 *owner_gid
= from_kgid(&init_user_ns
, gid
);
713 static int ublk_open(struct gendisk
*disk
, blk_mode_t mode
)
715 struct ublk_device
*ub
= disk
->private_data
;
717 if (capable(CAP_SYS_ADMIN
))
721 * If it is one unprivileged device, only owner can open
722 * the disk. Otherwise it could be one trap made by one
723 * evil user who grants this disk's privileges to other
724 * users deliberately.
726 * This way is reasonable too given anyone can create
727 * unprivileged device, and no need other's grant.
729 if (ub
->dev_info
.flags
& UBLK_F_UNPRIVILEGED_DEV
) {
730 unsigned int curr_uid
, curr_gid
;
732 ublk_store_owner_uid_gid(&curr_uid
, &curr_gid
);
734 if (curr_uid
!= ub
->dev_info
.owner_uid
|| curr_gid
!=
735 ub
->dev_info
.owner_gid
)
742 static const struct block_device_operations ub_fops
= {
743 .owner
= THIS_MODULE
,
745 .free_disk
= ublk_free_disk
,
746 .report_zones
= ublk_report_zones
,
749 #define UBLK_MAX_PIN_PAGES 32
751 struct ublk_io_iter
{
752 struct page
*pages
[UBLK_MAX_PIN_PAGES
];
754 struct bvec_iter iter
;
757 /* return how many pages are copied */
758 static void ublk_copy_io_pages(struct ublk_io_iter
*data
,
759 size_t total
, size_t pg_off
, int dir
)
764 while (done
< total
) {
765 struct bio_vec bv
= bio_iter_iovec(data
->bio
, data
->iter
);
766 unsigned int bytes
= min3(bv
.bv_len
, (unsigned)total
- done
,
767 (unsigned)(PAGE_SIZE
- pg_off
));
768 void *bv_buf
= bvec_kmap_local(&bv
);
769 void *pg_buf
= kmap_local_page(data
->pages
[pg_idx
]);
771 if (dir
== ITER_DEST
)
772 memcpy(pg_buf
+ pg_off
, bv_buf
, bytes
);
774 memcpy(bv_buf
, pg_buf
+ pg_off
, bytes
);
776 kunmap_local(pg_buf
);
777 kunmap_local(bv_buf
);
779 /* advance page array */
781 if (pg_off
== PAGE_SIZE
) {
789 bio_advance_iter_single(data
->bio
, &data
->iter
, bytes
);
790 if (!data
->iter
.bi_size
) {
791 data
->bio
= data
->bio
->bi_next
;
792 if (data
->bio
== NULL
)
794 data
->iter
= data
->bio
->bi_iter
;
799 static bool ublk_advance_io_iter(const struct request
*req
,
800 struct ublk_io_iter
*iter
, unsigned int offset
)
802 struct bio
*bio
= req
->bio
;
805 if (bio
->bi_iter
.bi_size
> offset
) {
807 iter
->iter
= bio
->bi_iter
;
808 bio_advance_iter(iter
->bio
, &iter
->iter
, offset
);
811 offset
-= bio
->bi_iter
.bi_size
;
817 * Copy data between request pages and io_iter, and 'offset'
818 * is the start point of linear offset of request.
820 static size_t ublk_copy_user_pages(const struct request
*req
,
821 unsigned offset
, struct iov_iter
*uiter
, int dir
)
823 struct ublk_io_iter iter
;
826 if (!ublk_advance_io_iter(req
, &iter
, offset
))
829 while (iov_iter_count(uiter
) && iter
.bio
) {
835 len
= iov_iter_get_pages2(uiter
, iter
.pages
,
836 iov_iter_count(uiter
),
837 UBLK_MAX_PIN_PAGES
, &off
);
841 ublk_copy_io_pages(&iter
, len
, off
, dir
);
842 nr_pages
= DIV_ROUND_UP(len
+ off
, PAGE_SIZE
);
843 for (i
= 0; i
< nr_pages
; i
++) {
844 if (dir
== ITER_DEST
)
845 set_page_dirty(iter
.pages
[i
]);
846 put_page(iter
.pages
[i
]);
854 static inline bool ublk_need_map_req(const struct request
*req
)
856 return ublk_rq_has_data(req
) && req_op(req
) == REQ_OP_WRITE
;
859 static inline bool ublk_need_unmap_req(const struct request
*req
)
861 return ublk_rq_has_data(req
) &&
862 (req_op(req
) == REQ_OP_READ
|| req_op(req
) == REQ_OP_DRV_IN
);
865 static int ublk_map_io(const struct ublk_queue
*ubq
, const struct request
*req
,
868 const unsigned int rq_bytes
= blk_rq_bytes(req
);
870 if (ublk_support_user_copy(ubq
))
874 * no zero copy, we delay copy WRITE request data into ublksrv
875 * context and the big benefit is that pinning pages in current
876 * context is pretty fast, see ublk_pin_user_pages
878 if (ublk_need_map_req(req
)) {
879 struct iov_iter iter
;
880 const int dir
= ITER_DEST
;
882 import_ubuf(dir
, u64_to_user_ptr(io
->addr
), rq_bytes
, &iter
);
883 return ublk_copy_user_pages(req
, 0, &iter
, dir
);
888 static int ublk_unmap_io(const struct ublk_queue
*ubq
,
889 const struct request
*req
,
892 const unsigned int rq_bytes
= blk_rq_bytes(req
);
894 if (ublk_support_user_copy(ubq
))
897 if (ublk_need_unmap_req(req
)) {
898 struct iov_iter iter
;
899 const int dir
= ITER_SOURCE
;
901 WARN_ON_ONCE(io
->res
> rq_bytes
);
903 import_ubuf(dir
, u64_to_user_ptr(io
->addr
), io
->res
, &iter
);
904 return ublk_copy_user_pages(req
, 0, &iter
, dir
);
909 static inline unsigned int ublk_req_build_flags(struct request
*req
)
913 if (req
->cmd_flags
& REQ_FAILFAST_DEV
)
914 flags
|= UBLK_IO_F_FAILFAST_DEV
;
916 if (req
->cmd_flags
& REQ_FAILFAST_TRANSPORT
)
917 flags
|= UBLK_IO_F_FAILFAST_TRANSPORT
;
919 if (req
->cmd_flags
& REQ_FAILFAST_DRIVER
)
920 flags
|= UBLK_IO_F_FAILFAST_DRIVER
;
922 if (req
->cmd_flags
& REQ_META
)
923 flags
|= UBLK_IO_F_META
;
925 if (req
->cmd_flags
& REQ_FUA
)
926 flags
|= UBLK_IO_F_FUA
;
928 if (req
->cmd_flags
& REQ_NOUNMAP
)
929 flags
|= UBLK_IO_F_NOUNMAP
;
931 if (req
->cmd_flags
& REQ_SWAP
)
932 flags
|= UBLK_IO_F_SWAP
;
937 static blk_status_t
ublk_setup_iod(struct ublk_queue
*ubq
, struct request
*req
)
939 struct ublksrv_io_desc
*iod
= ublk_get_iod(ubq
, req
->tag
);
940 struct ublk_io
*io
= &ubq
->ios
[req
->tag
];
941 enum req_op op
= req_op(req
);
944 if (!ublk_queue_is_zoned(ubq
) &&
945 (op_is_zone_mgmt(op
) || op
== REQ_OP_ZONE_APPEND
))
946 return BLK_STS_IOERR
;
948 switch (req_op(req
)) {
950 ublk_op
= UBLK_IO_OP_READ
;
953 ublk_op
= UBLK_IO_OP_WRITE
;
956 ublk_op
= UBLK_IO_OP_FLUSH
;
959 ublk_op
= UBLK_IO_OP_DISCARD
;
961 case REQ_OP_WRITE_ZEROES
:
962 ublk_op
= UBLK_IO_OP_WRITE_ZEROES
;
965 if (ublk_queue_is_zoned(ubq
))
966 return ublk_setup_iod_zoned(ubq
, req
);
967 return BLK_STS_IOERR
;
970 /* need to translate since kernel may change */
971 iod
->op_flags
= ublk_op
| ublk_req_build_flags(req
);
972 iod
->nr_sectors
= blk_rq_sectors(req
);
973 iod
->start_sector
= blk_rq_pos(req
);
974 iod
->addr
= io
->addr
;
979 static inline struct ublk_uring_cmd_pdu
*ublk_get_uring_cmd_pdu(
980 struct io_uring_cmd
*ioucmd
)
982 return (struct ublk_uring_cmd_pdu
*)&ioucmd
->pdu
;
985 static inline bool ubq_daemon_is_dying(struct ublk_queue
*ubq
)
987 return ubq
->ubq_daemon
->flags
& PF_EXITING
;
990 /* todo: handle partial completion */
991 static inline void __ublk_complete_rq(struct request
*req
)
993 struct ublk_queue
*ubq
= req
->mq_hctx
->driver_data
;
994 struct ublk_io
*io
= &ubq
->ios
[req
->tag
];
995 unsigned int unmapped_bytes
;
996 blk_status_t res
= BLK_STS_OK
;
998 /* called from ublk_abort_queue() code path */
999 if (io
->flags
& UBLK_IO_FLAG_ABORTED
) {
1000 res
= BLK_STS_IOERR
;
1004 /* failed read IO if nothing is read */
1005 if (!io
->res
&& req_op(req
) == REQ_OP_READ
)
1009 res
= errno_to_blk_status(io
->res
);
1014 * FLUSH, DISCARD or WRITE_ZEROES usually won't return bytes returned, so end them
1017 * Both the two needn't unmap.
1019 if (req_op(req
) != REQ_OP_READ
&& req_op(req
) != REQ_OP_WRITE
&&
1020 req_op(req
) != REQ_OP_DRV_IN
)
1023 /* for READ request, writing data in iod->addr to rq buffers */
1024 unmapped_bytes
= ublk_unmap_io(ubq
, req
, io
);
1027 * Extremely impossible since we got data filled in just before
1029 * Re-read simply for this unlikely case.
1031 if (unlikely(unmapped_bytes
< io
->res
))
1032 io
->res
= unmapped_bytes
;
1034 if (blk_update_request(req
, BLK_STS_OK
, io
->res
))
1035 blk_mq_requeue_request(req
, true);
1037 __blk_mq_end_request(req
, BLK_STS_OK
);
1041 blk_mq_end_request(req
, res
);
1044 static void ublk_complete_rq(struct kref
*ref
)
1046 struct ublk_rq_data
*data
= container_of(ref
, struct ublk_rq_data
,
1048 struct request
*req
= blk_mq_rq_from_pdu(data
);
1050 __ublk_complete_rq(req
);
1054 * Since __ublk_rq_task_work always fails requests immediately during
1055 * exiting, __ublk_fail_req() is only called from abort context during
1056 * exiting. So lock is unnecessary.
1058 * Also aborting may not be started yet, keep in mind that one failed
1059 * request may be issued by block layer again.
1061 static void __ublk_fail_req(struct ublk_queue
*ubq
, struct ublk_io
*io
,
1062 struct request
*req
)
1064 WARN_ON_ONCE(io
->flags
& UBLK_IO_FLAG_ACTIVE
);
1066 if (ublk_queue_can_use_recovery_reissue(ubq
))
1067 blk_mq_requeue_request(req
, false);
1069 ublk_put_req_ref(ubq
, req
);
1072 static void ubq_complete_io_cmd(struct ublk_io
*io
, int res
,
1073 unsigned issue_flags
)
1075 /* mark this cmd owned by ublksrv */
1076 io
->flags
|= UBLK_IO_FLAG_OWNED_BY_SRV
;
1079 * clear ACTIVE since we are done with this sqe/cmd slot
1080 * We can only accept io cmd in case of being not active.
1082 io
->flags
&= ~UBLK_IO_FLAG_ACTIVE
;
1084 /* tell ublksrv one io request is coming */
1085 io_uring_cmd_done(io
->cmd
, res
, 0, issue_flags
);
1088 #define UBLK_REQUEUE_DELAY_MS 3
1090 static inline void __ublk_abort_rq(struct ublk_queue
*ubq
,
1093 /* We cannot process this rq so just requeue it. */
1094 if (ublk_queue_can_use_recovery(ubq
))
1095 blk_mq_requeue_request(rq
, false);
1097 blk_mq_end_request(rq
, BLK_STS_IOERR
);
1100 static inline void __ublk_rq_task_work(struct request
*req
,
1101 unsigned issue_flags
)
1103 struct ublk_queue
*ubq
= req
->mq_hctx
->driver_data
;
1105 struct ublk_io
*io
= &ubq
->ios
[tag
];
1106 unsigned int mapped_bytes
;
1108 pr_devel("%s: complete: op %d, qid %d tag %d io_flags %x addr %llx\n",
1109 __func__
, io
->cmd
->cmd_op
, ubq
->q_id
, req
->tag
, io
->flags
,
1110 ublk_get_iod(ubq
, req
->tag
)->addr
);
1113 * Task is exiting if either:
1115 * (1) current != ubq_daemon.
1116 * io_uring_cmd_complete_in_task() tries to run task_work
1117 * in a workqueue if ubq_daemon(cmd's task) is PF_EXITING.
1119 * (2) current->flags & PF_EXITING.
1121 if (unlikely(current
!= ubq
->ubq_daemon
|| current
->flags
& PF_EXITING
)) {
1122 __ublk_abort_rq(ubq
, req
);
1126 if (ublk_need_get_data(ubq
) && ublk_need_map_req(req
)) {
1128 * We have not handled UBLK_IO_NEED_GET_DATA command yet,
1129 * so immepdately pass UBLK_IO_RES_NEED_GET_DATA to ublksrv
1132 if (!(io
->flags
& UBLK_IO_FLAG_NEED_GET_DATA
)) {
1133 io
->flags
|= UBLK_IO_FLAG_NEED_GET_DATA
;
1134 pr_devel("%s: need get data. op %d, qid %d tag %d io_flags %x\n",
1135 __func__
, io
->cmd
->cmd_op
, ubq
->q_id
,
1136 req
->tag
, io
->flags
);
1137 ubq_complete_io_cmd(io
, UBLK_IO_RES_NEED_GET_DATA
, issue_flags
);
1141 * We have handled UBLK_IO_NEED_GET_DATA command,
1142 * so clear UBLK_IO_FLAG_NEED_GET_DATA now and just
1145 io
->flags
&= ~UBLK_IO_FLAG_NEED_GET_DATA
;
1146 /* update iod->addr because ublksrv may have passed a new io buffer */
1147 ublk_get_iod(ubq
, req
->tag
)->addr
= io
->addr
;
1148 pr_devel("%s: update iod->addr: op %d, qid %d tag %d io_flags %x addr %llx\n",
1149 __func__
, io
->cmd
->cmd_op
, ubq
->q_id
, req
->tag
, io
->flags
,
1150 ublk_get_iod(ubq
, req
->tag
)->addr
);
1153 mapped_bytes
= ublk_map_io(ubq
, req
, io
);
1155 /* partially mapped, update io descriptor */
1156 if (unlikely(mapped_bytes
!= blk_rq_bytes(req
))) {
1158 * Nothing mapped, retry until we succeed.
1160 * We may never succeed in mapping any bytes here because
1161 * of OOM. TODO: reserve one buffer with single page pinned
1162 * for providing forward progress guarantee.
1164 if (unlikely(!mapped_bytes
)) {
1165 blk_mq_requeue_request(req
, false);
1166 blk_mq_delay_kick_requeue_list(req
->q
,
1167 UBLK_REQUEUE_DELAY_MS
);
1171 ublk_get_iod(ubq
, req
->tag
)->nr_sectors
=
1175 ublk_init_req_ref(ubq
, req
);
1176 ubq_complete_io_cmd(io
, UBLK_IO_RES_OK
, issue_flags
);
1179 static inline void ublk_forward_io_cmds(struct ublk_queue
*ubq
,
1180 unsigned issue_flags
)
1182 struct llist_node
*io_cmds
= llist_del_all(&ubq
->io_cmds
);
1183 struct ublk_rq_data
*data
, *tmp
;
1185 io_cmds
= llist_reverse_order(io_cmds
);
1186 llist_for_each_entry_safe(data
, tmp
, io_cmds
, node
)
1187 __ublk_rq_task_work(blk_mq_rq_from_pdu(data
), issue_flags
);
1190 static void ublk_rq_task_work_cb(struct io_uring_cmd
*cmd
, unsigned issue_flags
)
1192 struct ublk_uring_cmd_pdu
*pdu
= ublk_get_uring_cmd_pdu(cmd
);
1193 struct ublk_queue
*ubq
= pdu
->ubq
;
1195 ublk_forward_io_cmds(ubq
, issue_flags
);
1198 static void ublk_queue_cmd(struct ublk_queue
*ubq
, struct request
*rq
)
1200 struct ublk_rq_data
*data
= blk_mq_rq_to_pdu(rq
);
1202 if (llist_add(&data
->node
, &ubq
->io_cmds
)) {
1203 struct ublk_io
*io
= &ubq
->ios
[rq
->tag
];
1205 io_uring_cmd_complete_in_task(io
->cmd
, ublk_rq_task_work_cb
);
1209 static enum blk_eh_timer_return
ublk_timeout(struct request
*rq
)
1211 struct ublk_queue
*ubq
= rq
->mq_hctx
->driver_data
;
1212 unsigned int nr_inflight
= 0;
1215 if (ubq
->flags
& UBLK_F_UNPRIVILEGED_DEV
) {
1216 if (!ubq
->timeout
) {
1217 send_sig(SIGKILL
, ubq
->ubq_daemon
, 0);
1218 ubq
->timeout
= true;
1224 if (!ubq_daemon_is_dying(ubq
))
1225 return BLK_EH_RESET_TIMER
;
1227 for (i
= 0; i
< ubq
->q_depth
; i
++) {
1228 struct ublk_io
*io
= &ubq
->ios
[i
];
1230 if (!(io
->flags
& UBLK_IO_FLAG_ACTIVE
))
1234 /* cancelable uring_cmd can't help us if all commands are in-flight */
1235 if (nr_inflight
== ubq
->q_depth
) {
1236 struct ublk_device
*ub
= ubq
->dev
;
1238 if (ublk_abort_requests(ub
, ubq
)) {
1239 if (ublk_can_use_recovery(ub
))
1240 schedule_work(&ub
->quiesce_work
);
1242 schedule_work(&ub
->stop_work
);
1247 return BLK_EH_RESET_TIMER
;
1250 static blk_status_t
ublk_queue_rq(struct blk_mq_hw_ctx
*hctx
,
1251 const struct blk_mq_queue_data
*bd
)
1253 struct ublk_queue
*ubq
= hctx
->driver_data
;
1254 struct request
*rq
= bd
->rq
;
1257 /* fill iod to slot in io cmd buffer */
1258 res
= ublk_setup_iod(ubq
, rq
);
1259 if (unlikely(res
!= BLK_STS_OK
))
1260 return BLK_STS_IOERR
;
1262 /* With recovery feature enabled, force_abort is set in
1263 * ublk_stop_dev() before calling del_gendisk(). We have to
1264 * abort all requeued and new rqs here to let del_gendisk()
1265 * move on. Besides, we cannot not call io_uring_cmd_complete_in_task()
1266 * to avoid UAF on io_uring ctx.
1268 * Note: force_abort is guaranteed to be seen because it is set
1269 * before request queue is unqiuesced.
1271 if (ublk_queue_can_use_recovery(ubq
) && unlikely(ubq
->force_abort
))
1272 return BLK_STS_IOERR
;
1274 if (unlikely(ubq
->canceling
)) {
1275 __ublk_abort_rq(ubq
, rq
);
1279 blk_mq_start_request(bd
->rq
);
1280 ublk_queue_cmd(ubq
, rq
);
1285 static int ublk_init_hctx(struct blk_mq_hw_ctx
*hctx
, void *driver_data
,
1286 unsigned int hctx_idx
)
1288 struct ublk_device
*ub
= driver_data
;
1289 struct ublk_queue
*ubq
= ublk_get_queue(ub
, hctx
->queue_num
);
1291 hctx
->driver_data
= ubq
;
1295 static const struct blk_mq_ops ublk_mq_ops
= {
1296 .queue_rq
= ublk_queue_rq
,
1297 .init_hctx
= ublk_init_hctx
,
1298 .timeout
= ublk_timeout
,
1301 static int ublk_ch_open(struct inode
*inode
, struct file
*filp
)
1303 struct ublk_device
*ub
= container_of(inode
->i_cdev
,
1304 struct ublk_device
, cdev
);
1306 if (test_and_set_bit(UB_STATE_OPEN
, &ub
->state
))
1308 filp
->private_data
= ub
;
1312 static int ublk_ch_release(struct inode
*inode
, struct file
*filp
)
1314 struct ublk_device
*ub
= filp
->private_data
;
1316 clear_bit(UB_STATE_OPEN
, &ub
->state
);
1320 /* map pre-allocated per-queue cmd buffer to ublksrv daemon */
1321 static int ublk_ch_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1323 struct ublk_device
*ub
= filp
->private_data
;
1324 size_t sz
= vma
->vm_end
- vma
->vm_start
;
1325 unsigned max_sz
= UBLK_MAX_QUEUE_DEPTH
* sizeof(struct ublksrv_io_desc
);
1326 unsigned long pfn
, end
, phys_off
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1329 spin_lock(&ub
->lock
);
1331 ub
->mm
= current
->mm
;
1332 if (current
->mm
!= ub
->mm
)
1334 spin_unlock(&ub
->lock
);
1339 if (vma
->vm_flags
& VM_WRITE
)
1342 end
= UBLKSRV_CMD_BUF_OFFSET
+ ub
->dev_info
.nr_hw_queues
* max_sz
;
1343 if (phys_off
< UBLKSRV_CMD_BUF_OFFSET
|| phys_off
>= end
)
1346 q_id
= (phys_off
- UBLKSRV_CMD_BUF_OFFSET
) / max_sz
;
1347 pr_devel("%s: qid %d, pid %d, addr %lx pg_off %lx sz %lu\n",
1348 __func__
, q_id
, current
->pid
, vma
->vm_start
,
1349 phys_off
, (unsigned long)sz
);
1351 if (sz
!= ublk_queue_cmd_buf_size(ub
, q_id
))
1354 pfn
= virt_to_phys(ublk_queue_cmd_buf(ub
, q_id
)) >> PAGE_SHIFT
;
1355 return remap_pfn_range(vma
, vma
->vm_start
, pfn
, sz
, vma
->vm_page_prot
);
1358 static void ublk_commit_completion(struct ublk_device
*ub
,
1359 const struct ublksrv_io_cmd
*ub_cmd
)
1361 u32 qid
= ub_cmd
->q_id
, tag
= ub_cmd
->tag
;
1362 struct ublk_queue
*ubq
= ublk_get_queue(ub
, qid
);
1363 struct ublk_io
*io
= &ubq
->ios
[tag
];
1364 struct request
*req
;
1366 /* now this cmd slot is owned by nbd driver */
1367 io
->flags
&= ~UBLK_IO_FLAG_OWNED_BY_SRV
;
1368 io
->res
= ub_cmd
->result
;
1370 /* find the io request and complete */
1371 req
= blk_mq_tag_to_rq(ub
->tag_set
.tags
[qid
], tag
);
1372 if (WARN_ON_ONCE(unlikely(!req
)))
1375 if (req_op(req
) == REQ_OP_ZONE_APPEND
)
1376 req
->__sector
= ub_cmd
->zone_append_lba
;
1378 if (likely(!blk_should_fake_timeout(req
->q
)))
1379 ublk_put_req_ref(ubq
, req
);
1383 * Called from ubq_daemon context via cancel fn, meantime quiesce ublk
1384 * blk-mq queue, so we are called exclusively with blk-mq and ubq_daemon
1385 * context, so everything is serialized.
1387 static void ublk_abort_queue(struct ublk_device
*ub
, struct ublk_queue
*ubq
)
1391 for (i
= 0; i
< ubq
->q_depth
; i
++) {
1392 struct ublk_io
*io
= &ubq
->ios
[i
];
1394 if (!(io
->flags
& UBLK_IO_FLAG_ACTIVE
)) {
1398 * Either we fail the request or ublk_rq_task_work_fn
1401 rq
= blk_mq_tag_to_rq(ub
->tag_set
.tags
[ubq
->q_id
], i
);
1402 if (rq
&& blk_mq_request_started(rq
)) {
1403 io
->flags
|= UBLK_IO_FLAG_ABORTED
;
1404 __ublk_fail_req(ubq
, io
, rq
);
1410 static bool ublk_abort_requests(struct ublk_device
*ub
, struct ublk_queue
*ubq
)
1412 struct gendisk
*disk
;
1414 spin_lock(&ubq
->cancel_lock
);
1415 if (ubq
->canceling
) {
1416 spin_unlock(&ubq
->cancel_lock
);
1419 ubq
->canceling
= true;
1420 spin_unlock(&ubq
->cancel_lock
);
1422 spin_lock(&ub
->lock
);
1425 get_device(disk_to_dev(disk
));
1426 spin_unlock(&ub
->lock
);
1428 /* Our disk has been dead */
1432 /* Now we are serialized with ublk_queue_rq() */
1433 blk_mq_quiesce_queue(disk
->queue
);
1434 /* abort queue is for making forward progress */
1435 ublk_abort_queue(ub
, ubq
);
1436 blk_mq_unquiesce_queue(disk
->queue
);
1437 put_device(disk_to_dev(disk
));
1442 static void ublk_cancel_cmd(struct ublk_queue
*ubq
, struct ublk_io
*io
,
1443 unsigned int issue_flags
)
1447 if (!(io
->flags
& UBLK_IO_FLAG_ACTIVE
))
1450 spin_lock(&ubq
->cancel_lock
);
1451 done
= !!(io
->flags
& UBLK_IO_FLAG_CANCELED
);
1453 io
->flags
|= UBLK_IO_FLAG_CANCELED
;
1454 spin_unlock(&ubq
->cancel_lock
);
1457 io_uring_cmd_done(io
->cmd
, UBLK_IO_RES_ABORT
, 0, issue_flags
);
1461 * The ublk char device won't be closed when calling cancel fn, so both
1462 * ublk device and queue are guaranteed to be live
1464 static void ublk_uring_cmd_cancel_fn(struct io_uring_cmd
*cmd
,
1465 unsigned int issue_flags
)
1467 struct ublk_uring_cmd_pdu
*pdu
= ublk_get_uring_cmd_pdu(cmd
);
1468 struct ublk_queue
*ubq
= pdu
->ubq
;
1469 struct task_struct
*task
;
1470 struct ublk_device
*ub
;
1474 if (WARN_ON_ONCE(!ubq
))
1477 if (WARN_ON_ONCE(pdu
->tag
>= ubq
->q_depth
))
1480 task
= io_uring_cmd_get_task(cmd
);
1481 if (WARN_ON_ONCE(task
&& task
!= ubq
->ubq_daemon
))
1485 need_schedule
= ublk_abort_requests(ub
, ubq
);
1487 io
= &ubq
->ios
[pdu
->tag
];
1488 WARN_ON_ONCE(io
->cmd
!= cmd
);
1489 ublk_cancel_cmd(ubq
, io
, issue_flags
);
1491 if (need_schedule
) {
1492 if (ublk_can_use_recovery(ub
))
1493 schedule_work(&ub
->quiesce_work
);
1495 schedule_work(&ub
->stop_work
);
1499 static inline bool ublk_queue_ready(struct ublk_queue
*ubq
)
1501 return ubq
->nr_io_ready
== ubq
->q_depth
;
1504 static void ublk_cancel_queue(struct ublk_queue
*ubq
)
1508 for (i
= 0; i
< ubq
->q_depth
; i
++)
1509 ublk_cancel_cmd(ubq
, &ubq
->ios
[i
], IO_URING_F_UNLOCKED
);
1512 /* Cancel all pending commands, must be called after del_gendisk() returns */
1513 static void ublk_cancel_dev(struct ublk_device
*ub
)
1517 for (i
= 0; i
< ub
->dev_info
.nr_hw_queues
; i
++)
1518 ublk_cancel_queue(ublk_get_queue(ub
, i
));
1521 static bool ublk_check_inflight_rq(struct request
*rq
, void *data
)
1525 if (blk_mq_request_started(rq
)) {
1532 static void ublk_wait_tagset_rqs_idle(struct ublk_device
*ub
)
1536 WARN_ON_ONCE(!blk_queue_quiesced(ub
->ub_disk
->queue
));
1539 blk_mq_tagset_busy_iter(&ub
->tag_set
,
1540 ublk_check_inflight_rq
, &idle
);
1543 msleep(UBLK_REQUEUE_DELAY_MS
);
1547 static void __ublk_quiesce_dev(struct ublk_device
*ub
)
1549 pr_devel("%s: quiesce ub: dev_id %d state %s\n",
1550 __func__
, ub
->dev_info
.dev_id
,
1551 ub
->dev_info
.state
== UBLK_S_DEV_LIVE
?
1552 "LIVE" : "QUIESCED");
1553 blk_mq_quiesce_queue(ub
->ub_disk
->queue
);
1554 ublk_wait_tagset_rqs_idle(ub
);
1555 ub
->dev_info
.state
= UBLK_S_DEV_QUIESCED
;
1558 static void ublk_quiesce_work_fn(struct work_struct
*work
)
1560 struct ublk_device
*ub
=
1561 container_of(work
, struct ublk_device
, quiesce_work
);
1563 mutex_lock(&ub
->mutex
);
1564 if (ub
->dev_info
.state
!= UBLK_S_DEV_LIVE
)
1566 __ublk_quiesce_dev(ub
);
1568 mutex_unlock(&ub
->mutex
);
1569 ublk_cancel_dev(ub
);
1572 static void ublk_unquiesce_dev(struct ublk_device
*ub
)
1576 pr_devel("%s: unquiesce ub: dev_id %d state %s\n",
1577 __func__
, ub
->dev_info
.dev_id
,
1578 ub
->dev_info
.state
== UBLK_S_DEV_LIVE
?
1579 "LIVE" : "QUIESCED");
1580 /* quiesce_work has run. We let requeued rqs be aborted
1581 * before running fallback_wq. "force_abort" must be seen
1582 * after request queue is unqiuesced. Then del_gendisk()
1585 for (i
= 0; i
< ub
->dev_info
.nr_hw_queues
; i
++)
1586 ublk_get_queue(ub
, i
)->force_abort
= true;
1588 blk_mq_unquiesce_queue(ub
->ub_disk
->queue
);
1589 /* We may have requeued some rqs in ublk_quiesce_queue() */
1590 blk_mq_kick_requeue_list(ub
->ub_disk
->queue
);
1593 static void ublk_stop_dev(struct ublk_device
*ub
)
1595 struct gendisk
*disk
;
1597 mutex_lock(&ub
->mutex
);
1598 if (ub
->dev_info
.state
== UBLK_S_DEV_DEAD
)
1600 if (ublk_can_use_recovery(ub
)) {
1601 if (ub
->dev_info
.state
== UBLK_S_DEV_LIVE
)
1602 __ublk_quiesce_dev(ub
);
1603 ublk_unquiesce_dev(ub
);
1605 del_gendisk(ub
->ub_disk
);
1607 /* Sync with ublk_abort_queue() by holding the lock */
1608 spin_lock(&ub
->lock
);
1610 ub
->dev_info
.state
= UBLK_S_DEV_DEAD
;
1611 ub
->dev_info
.ublksrv_pid
= -1;
1613 spin_unlock(&ub
->lock
);
1616 mutex_unlock(&ub
->mutex
);
1617 ublk_cancel_dev(ub
);
1620 /* device can only be started after all IOs are ready */
1621 static void ublk_mark_io_ready(struct ublk_device
*ub
, struct ublk_queue
*ubq
)
1623 mutex_lock(&ub
->mutex
);
1625 if (ublk_queue_ready(ubq
)) {
1626 ubq
->ubq_daemon
= current
;
1627 get_task_struct(ubq
->ubq_daemon
);
1628 ub
->nr_queues_ready
++;
1630 if (capable(CAP_SYS_ADMIN
))
1631 ub
->nr_privileged_daemon
++;
1633 if (ub
->nr_queues_ready
== ub
->dev_info
.nr_hw_queues
)
1634 complete_all(&ub
->completion
);
1635 mutex_unlock(&ub
->mutex
);
1638 static void ublk_handle_need_get_data(struct ublk_device
*ub
, int q_id
,
1641 struct ublk_queue
*ubq
= ublk_get_queue(ub
, q_id
);
1642 struct request
*req
= blk_mq_tag_to_rq(ub
->tag_set
.tags
[q_id
], tag
);
1644 ublk_queue_cmd(ubq
, req
);
1647 static inline int ublk_check_cmd_op(u32 cmd_op
)
1649 u32 ioc_type
= _IOC_TYPE(cmd_op
);
1651 if (!IS_ENABLED(CONFIG_BLKDEV_UBLK_LEGACY_OPCODES
) && ioc_type
!= 'u')
1654 if (ioc_type
!= 'u' && ioc_type
!= 0)
1660 static inline void ublk_fill_io_cmd(struct ublk_io
*io
,
1661 struct io_uring_cmd
*cmd
, unsigned long buf_addr
)
1664 io
->flags
|= UBLK_IO_FLAG_ACTIVE
;
1665 io
->addr
= buf_addr
;
1668 static inline void ublk_prep_cancel(struct io_uring_cmd
*cmd
,
1669 unsigned int issue_flags
,
1670 struct ublk_queue
*ubq
, unsigned int tag
)
1672 struct ublk_uring_cmd_pdu
*pdu
= ublk_get_uring_cmd_pdu(cmd
);
1675 * Safe to refer to @ubq since ublk_queue won't be died until its
1676 * commands are completed
1680 io_uring_cmd_mark_cancelable(cmd
, issue_flags
);
1683 static int __ublk_ch_uring_cmd(struct io_uring_cmd
*cmd
,
1684 unsigned int issue_flags
,
1685 const struct ublksrv_io_cmd
*ub_cmd
)
1687 struct ublk_device
*ub
= cmd
->file
->private_data
;
1688 struct ublk_queue
*ubq
;
1690 u32 cmd_op
= cmd
->cmd_op
;
1691 unsigned tag
= ub_cmd
->tag
;
1693 struct request
*req
;
1695 pr_devel("%s: received: cmd op %d queue %d tag %d result %d\n",
1696 __func__
, cmd
->cmd_op
, ub_cmd
->q_id
, tag
,
1699 if (ub_cmd
->q_id
>= ub
->dev_info
.nr_hw_queues
)
1702 ubq
= ublk_get_queue(ub
, ub_cmd
->q_id
);
1703 if (!ubq
|| ub_cmd
->q_id
!= ubq
->q_id
)
1706 if (ubq
->ubq_daemon
&& ubq
->ubq_daemon
!= current
)
1709 if (tag
>= ubq
->q_depth
)
1712 io
= &ubq
->ios
[tag
];
1714 /* there is pending io cmd, something must be wrong */
1715 if (io
->flags
& UBLK_IO_FLAG_ACTIVE
) {
1721 * ensure that the user issues UBLK_IO_NEED_GET_DATA
1722 * iff the driver have set the UBLK_IO_FLAG_NEED_GET_DATA.
1724 if ((!!(io
->flags
& UBLK_IO_FLAG_NEED_GET_DATA
))
1725 ^ (_IOC_NR(cmd_op
) == UBLK_IO_NEED_GET_DATA
))
1728 ret
= ublk_check_cmd_op(cmd_op
);
1733 switch (_IOC_NR(cmd_op
)) {
1734 case UBLK_IO_FETCH_REQ
:
1735 /* UBLK_IO_FETCH_REQ is only allowed before queue is setup */
1736 if (ublk_queue_ready(ubq
)) {
1741 * The io is being handled by server, so COMMIT_RQ is expected
1742 * instead of FETCH_REQ
1744 if (io
->flags
& UBLK_IO_FLAG_OWNED_BY_SRV
)
1747 if (!ublk_support_user_copy(ubq
)) {
1749 * FETCH_RQ has to provide IO buffer if NEED GET
1750 * DATA is not enabled
1752 if (!ub_cmd
->addr
&& !ublk_need_get_data(ubq
))
1754 } else if (ub_cmd
->addr
) {
1755 /* User copy requires addr to be unset */
1760 ublk_fill_io_cmd(io
, cmd
, ub_cmd
->addr
);
1761 ublk_mark_io_ready(ub
, ubq
);
1763 case UBLK_IO_COMMIT_AND_FETCH_REQ
:
1764 req
= blk_mq_tag_to_rq(ub
->tag_set
.tags
[ub_cmd
->q_id
], tag
);
1766 if (!(io
->flags
& UBLK_IO_FLAG_OWNED_BY_SRV
))
1769 if (!ublk_support_user_copy(ubq
)) {
1771 * COMMIT_AND_FETCH_REQ has to provide IO buffer if
1772 * NEED GET DATA is not enabled or it is Read IO.
1774 if (!ub_cmd
->addr
&& (!ublk_need_get_data(ubq
) ||
1775 req_op(req
) == REQ_OP_READ
))
1777 } else if (req_op(req
) != REQ_OP_ZONE_APPEND
&& ub_cmd
->addr
) {
1779 * User copy requires addr to be unset when command is
1786 ublk_fill_io_cmd(io
, cmd
, ub_cmd
->addr
);
1787 ublk_commit_completion(ub
, ub_cmd
);
1789 case UBLK_IO_NEED_GET_DATA
:
1790 if (!(io
->flags
& UBLK_IO_FLAG_OWNED_BY_SRV
))
1792 ublk_fill_io_cmd(io
, cmd
, ub_cmd
->addr
);
1793 ublk_handle_need_get_data(ub
, ub_cmd
->q_id
, ub_cmd
->tag
);
1798 ublk_prep_cancel(cmd
, issue_flags
, ubq
, tag
);
1799 return -EIOCBQUEUED
;
1802 io_uring_cmd_done(cmd
, ret
, 0, issue_flags
);
1803 pr_devel("%s: complete: cmd op %d, tag %d ret %x io_flags %x\n",
1804 __func__
, cmd_op
, tag
, ret
, io
->flags
);
1805 return -EIOCBQUEUED
;
1808 static inline struct request
*__ublk_check_and_get_req(struct ublk_device
*ub
,
1809 struct ublk_queue
*ubq
, int tag
, size_t offset
)
1811 struct request
*req
;
1813 if (!ublk_need_req_ref(ubq
))
1816 req
= blk_mq_tag_to_rq(ub
->tag_set
.tags
[ubq
->q_id
], tag
);
1820 if (!ublk_get_req_ref(ubq
, req
))
1823 if (unlikely(!blk_mq_request_started(req
) || req
->tag
!= tag
))
1826 if (!ublk_rq_has_data(req
))
1829 if (offset
> blk_rq_bytes(req
))
1834 ublk_put_req_ref(ubq
, req
);
1838 static inline int ublk_ch_uring_cmd_local(struct io_uring_cmd
*cmd
,
1839 unsigned int issue_flags
)
1842 * Not necessary for async retry, but let's keep it simple and always
1843 * copy the values to avoid any potential reuse.
1845 const struct ublksrv_io_cmd
*ub_src
= io_uring_sqe_cmd(cmd
->sqe
);
1846 const struct ublksrv_io_cmd ub_cmd
= {
1847 .q_id
= READ_ONCE(ub_src
->q_id
),
1848 .tag
= READ_ONCE(ub_src
->tag
),
1849 .result
= READ_ONCE(ub_src
->result
),
1850 .addr
= READ_ONCE(ub_src
->addr
)
1853 WARN_ON_ONCE(issue_flags
& IO_URING_F_UNLOCKED
);
1855 return __ublk_ch_uring_cmd(cmd
, issue_flags
, &ub_cmd
);
1858 static void ublk_ch_uring_cmd_cb(struct io_uring_cmd
*cmd
,
1859 unsigned int issue_flags
)
1861 ublk_ch_uring_cmd_local(cmd
, issue_flags
);
1864 static int ublk_ch_uring_cmd(struct io_uring_cmd
*cmd
, unsigned int issue_flags
)
1866 if (unlikely(issue_flags
& IO_URING_F_CANCEL
)) {
1867 ublk_uring_cmd_cancel_fn(cmd
, issue_flags
);
1871 /* well-implemented server won't run into unlocked */
1872 if (unlikely(issue_flags
& IO_URING_F_UNLOCKED
)) {
1873 io_uring_cmd_complete_in_task(cmd
, ublk_ch_uring_cmd_cb
);
1874 return -EIOCBQUEUED
;
1877 return ublk_ch_uring_cmd_local(cmd
, issue_flags
);
1880 static inline bool ublk_check_ubuf_dir(const struct request
*req
,
1883 /* copy ubuf to request pages */
1884 if ((req_op(req
) == REQ_OP_READ
|| req_op(req
) == REQ_OP_DRV_IN
) &&
1885 ubuf_dir
== ITER_SOURCE
)
1888 /* copy request pages to ubuf */
1889 if ((req_op(req
) == REQ_OP_WRITE
||
1890 req_op(req
) == REQ_OP_ZONE_APPEND
) &&
1891 ubuf_dir
== ITER_DEST
)
1897 static struct request
*ublk_check_and_get_req(struct kiocb
*iocb
,
1898 struct iov_iter
*iter
, size_t *off
, int dir
)
1900 struct ublk_device
*ub
= iocb
->ki_filp
->private_data
;
1901 struct ublk_queue
*ubq
;
1902 struct request
*req
;
1907 return ERR_PTR(-EACCES
);
1909 if (!user_backed_iter(iter
))
1910 return ERR_PTR(-EACCES
);
1912 if (ub
->dev_info
.state
== UBLK_S_DEV_DEAD
)
1913 return ERR_PTR(-EACCES
);
1915 tag
= ublk_pos_to_tag(iocb
->ki_pos
);
1916 q_id
= ublk_pos_to_hwq(iocb
->ki_pos
);
1917 buf_off
= ublk_pos_to_buf_off(iocb
->ki_pos
);
1919 if (q_id
>= ub
->dev_info
.nr_hw_queues
)
1920 return ERR_PTR(-EINVAL
);
1922 ubq
= ublk_get_queue(ub
, q_id
);
1924 return ERR_PTR(-EINVAL
);
1926 if (tag
>= ubq
->q_depth
)
1927 return ERR_PTR(-EINVAL
);
1929 req
= __ublk_check_and_get_req(ub
, ubq
, tag
, buf_off
);
1931 return ERR_PTR(-EINVAL
);
1933 if (!req
->mq_hctx
|| !req
->mq_hctx
->driver_data
)
1936 if (!ublk_check_ubuf_dir(req
, dir
))
1942 ublk_put_req_ref(ubq
, req
);
1943 return ERR_PTR(-EACCES
);
1946 static ssize_t
ublk_ch_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
1948 struct ublk_queue
*ubq
;
1949 struct request
*req
;
1953 req
= ublk_check_and_get_req(iocb
, to
, &buf_off
, ITER_DEST
);
1955 return PTR_ERR(req
);
1957 ret
= ublk_copy_user_pages(req
, buf_off
, to
, ITER_DEST
);
1958 ubq
= req
->mq_hctx
->driver_data
;
1959 ublk_put_req_ref(ubq
, req
);
1964 static ssize_t
ublk_ch_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1966 struct ublk_queue
*ubq
;
1967 struct request
*req
;
1971 req
= ublk_check_and_get_req(iocb
, from
, &buf_off
, ITER_SOURCE
);
1973 return PTR_ERR(req
);
1975 ret
= ublk_copy_user_pages(req
, buf_off
, from
, ITER_SOURCE
);
1976 ubq
= req
->mq_hctx
->driver_data
;
1977 ublk_put_req_ref(ubq
, req
);
1982 static const struct file_operations ublk_ch_fops
= {
1983 .owner
= THIS_MODULE
,
1984 .open
= ublk_ch_open
,
1985 .release
= ublk_ch_release
,
1986 .read_iter
= ublk_ch_read_iter
,
1987 .write_iter
= ublk_ch_write_iter
,
1988 .uring_cmd
= ublk_ch_uring_cmd
,
1989 .mmap
= ublk_ch_mmap
,
1992 static void ublk_deinit_queue(struct ublk_device
*ub
, int q_id
)
1994 int size
= ublk_queue_cmd_buf_size(ub
, q_id
);
1995 struct ublk_queue
*ubq
= ublk_get_queue(ub
, q_id
);
1997 if (ubq
->ubq_daemon
)
1998 put_task_struct(ubq
->ubq_daemon
);
1999 if (ubq
->io_cmd_buf
)
2000 free_pages((unsigned long)ubq
->io_cmd_buf
, get_order(size
));
2003 static int ublk_init_queue(struct ublk_device
*ub
, int q_id
)
2005 struct ublk_queue
*ubq
= ublk_get_queue(ub
, q_id
);
2006 gfp_t gfp_flags
= GFP_KERNEL
| __GFP_ZERO
;
2010 spin_lock_init(&ubq
->cancel_lock
);
2011 ubq
->flags
= ub
->dev_info
.flags
;
2013 ubq
->q_depth
= ub
->dev_info
.queue_depth
;
2014 size
= ublk_queue_cmd_buf_size(ub
, q_id
);
2016 ptr
= (void *) __get_free_pages(gfp_flags
, get_order(size
));
2020 ubq
->io_cmd_buf
= ptr
;
2025 static void ublk_deinit_queues(struct ublk_device
*ub
)
2027 int nr_queues
= ub
->dev_info
.nr_hw_queues
;
2033 for (i
= 0; i
< nr_queues
; i
++)
2034 ublk_deinit_queue(ub
, i
);
2035 kfree(ub
->__queues
);
2038 static int ublk_init_queues(struct ublk_device
*ub
)
2040 int nr_queues
= ub
->dev_info
.nr_hw_queues
;
2041 int depth
= ub
->dev_info
.queue_depth
;
2042 int ubq_size
= sizeof(struct ublk_queue
) + depth
* sizeof(struct ublk_io
);
2043 int i
, ret
= -ENOMEM
;
2045 ub
->queue_size
= ubq_size
;
2046 ub
->__queues
= kcalloc(nr_queues
, ubq_size
, GFP_KERNEL
);
2050 for (i
= 0; i
< nr_queues
; i
++) {
2051 if (ublk_init_queue(ub
, i
))
2055 init_completion(&ub
->completion
);
2059 ublk_deinit_queues(ub
);
2063 static int ublk_alloc_dev_number(struct ublk_device
*ub
, int idx
)
2068 spin_lock(&ublk_idr_lock
);
2069 /* allocate id, if @id >= 0, we're requesting that specific id */
2071 err
= idr_alloc(&ublk_index_idr
, ub
, i
, i
+ 1, GFP_NOWAIT
);
2075 err
= idr_alloc(&ublk_index_idr
, ub
, 0, UBLK_MAX_UBLKS
,
2078 spin_unlock(&ublk_idr_lock
);
2081 ub
->ub_number
= err
;
2086 static void ublk_free_dev_number(struct ublk_device
*ub
)
2088 spin_lock(&ublk_idr_lock
);
2089 idr_remove(&ublk_index_idr
, ub
->ub_number
);
2090 wake_up_all(&ublk_idr_wq
);
2091 spin_unlock(&ublk_idr_lock
);
2094 static void ublk_cdev_rel(struct device
*dev
)
2096 struct ublk_device
*ub
= container_of(dev
, struct ublk_device
, cdev_dev
);
2098 blk_mq_free_tag_set(&ub
->tag_set
);
2099 ublk_deinit_queues(ub
);
2100 ublk_free_dev_number(ub
);
2101 mutex_destroy(&ub
->mutex
);
2105 static int ublk_add_chdev(struct ublk_device
*ub
)
2107 struct device
*dev
= &ub
->cdev_dev
;
2108 int minor
= ub
->ub_number
;
2111 dev
->parent
= ublk_misc
.this_device
;
2112 dev
->devt
= MKDEV(MAJOR(ublk_chr_devt
), minor
);
2113 dev
->class = &ublk_chr_class
;
2114 dev
->release
= ublk_cdev_rel
;
2115 device_initialize(dev
);
2117 ret
= dev_set_name(dev
, "ublkc%d", minor
);
2121 cdev_init(&ub
->cdev
, &ublk_ch_fops
);
2122 ret
= cdev_device_add(&ub
->cdev
, dev
);
2133 static void ublk_stop_work_fn(struct work_struct
*work
)
2135 struct ublk_device
*ub
=
2136 container_of(work
, struct ublk_device
, stop_work
);
2141 /* align max io buffer size with PAGE_SIZE */
2142 static void ublk_align_max_io_size(struct ublk_device
*ub
)
2144 unsigned int max_io_bytes
= ub
->dev_info
.max_io_buf_bytes
;
2146 ub
->dev_info
.max_io_buf_bytes
=
2147 round_down(max_io_bytes
, PAGE_SIZE
);
2150 static int ublk_add_tag_set(struct ublk_device
*ub
)
2152 ub
->tag_set
.ops
= &ublk_mq_ops
;
2153 ub
->tag_set
.nr_hw_queues
= ub
->dev_info
.nr_hw_queues
;
2154 ub
->tag_set
.queue_depth
= ub
->dev_info
.queue_depth
;
2155 ub
->tag_set
.numa_node
= NUMA_NO_NODE
;
2156 ub
->tag_set
.cmd_size
= sizeof(struct ublk_rq_data
);
2157 ub
->tag_set
.flags
= BLK_MQ_F_SHOULD_MERGE
;
2158 ub
->tag_set
.driver_data
= ub
;
2159 return blk_mq_alloc_tag_set(&ub
->tag_set
);
2162 static void ublk_remove(struct ublk_device
*ub
)
2165 cancel_work_sync(&ub
->stop_work
);
2166 cancel_work_sync(&ub
->quiesce_work
);
2167 cdev_device_del(&ub
->cdev
, &ub
->cdev_dev
);
2168 ublk_put_device(ub
);
2172 static struct ublk_device
*ublk_get_device_from_id(int idx
)
2174 struct ublk_device
*ub
= NULL
;
2179 spin_lock(&ublk_idr_lock
);
2180 ub
= idr_find(&ublk_index_idr
, idx
);
2182 ub
= ublk_get_device(ub
);
2183 spin_unlock(&ublk_idr_lock
);
2188 static int ublk_ctrl_start_dev(struct ublk_device
*ub
, struct io_uring_cmd
*cmd
)
2190 const struct ublksrv_ctrl_cmd
*header
= io_uring_sqe_cmd(cmd
->sqe
);
2191 const struct ublk_param_basic
*p
= &ub
->params
.basic
;
2192 int ublksrv_pid
= (int)header
->data
[0];
2193 struct queue_limits lim
= {
2194 .logical_block_size
= 1 << p
->logical_bs_shift
,
2195 .physical_block_size
= 1 << p
->physical_bs_shift
,
2196 .io_min
= 1 << p
->io_min_shift
,
2197 .io_opt
= 1 << p
->io_opt_shift
,
2198 .max_hw_sectors
= p
->max_sectors
,
2199 .chunk_sectors
= p
->chunk_sectors
,
2200 .virt_boundary_mask
= p
->virt_boundary_mask
,
2201 .max_segments
= USHRT_MAX
,
2202 .max_segment_size
= UINT_MAX
,
2205 struct gendisk
*disk
;
2208 if (ublksrv_pid
<= 0)
2210 if (!(ub
->params
.types
& UBLK_PARAM_TYPE_BASIC
))
2213 if (ub
->params
.types
& UBLK_PARAM_TYPE_DISCARD
) {
2214 const struct ublk_param_discard
*pd
= &ub
->params
.discard
;
2216 lim
.discard_alignment
= pd
->discard_alignment
;
2217 lim
.discard_granularity
= pd
->discard_granularity
;
2218 lim
.max_hw_discard_sectors
= pd
->max_discard_sectors
;
2219 lim
.max_write_zeroes_sectors
= pd
->max_write_zeroes_sectors
;
2220 lim
.max_discard_segments
= pd
->max_discard_segments
;
2223 if (ub
->params
.types
& UBLK_PARAM_TYPE_ZONED
) {
2224 const struct ublk_param_zoned
*p
= &ub
->params
.zoned
;
2226 if (!IS_ENABLED(CONFIG_BLK_DEV_ZONED
))
2229 lim
.features
|= BLK_FEAT_ZONED
;
2230 lim
.max_active_zones
= p
->max_active_zones
;
2231 lim
.max_open_zones
= p
->max_open_zones
;
2232 lim
.max_zone_append_sectors
= p
->max_zone_append_sectors
;
2235 if (ub
->params
.basic
.attrs
& UBLK_ATTR_VOLATILE_CACHE
) {
2236 lim
.features
|= BLK_FEAT_WRITE_CACHE
;
2237 if (ub
->params
.basic
.attrs
& UBLK_ATTR_FUA
)
2238 lim
.features
|= BLK_FEAT_FUA
;
2241 if (ub
->params
.basic
.attrs
& UBLK_ATTR_ROTATIONAL
)
2242 lim
.features
|= BLK_FEAT_ROTATIONAL
;
2244 if (wait_for_completion_interruptible(&ub
->completion
) != 0)
2247 mutex_lock(&ub
->mutex
);
2248 if (ub
->dev_info
.state
== UBLK_S_DEV_LIVE
||
2249 test_bit(UB_STATE_USED
, &ub
->state
)) {
2254 disk
= blk_mq_alloc_disk(&ub
->tag_set
, &lim
, NULL
);
2256 ret
= PTR_ERR(disk
);
2259 sprintf(disk
->disk_name
, "ublkb%d", ub
->ub_number
);
2260 disk
->fops
= &ub_fops
;
2261 disk
->private_data
= ub
;
2263 ub
->dev_info
.ublksrv_pid
= ublksrv_pid
;
2266 ublk_apply_params(ub
);
2268 /* don't probe partitions if any one ubq daemon is un-trusted */
2269 if (ub
->nr_privileged_daemon
!= ub
->nr_queues_ready
)
2270 set_bit(GD_SUPPRESS_PART_SCAN
, &disk
->state
);
2272 ublk_get_device(ub
);
2273 ub
->dev_info
.state
= UBLK_S_DEV_LIVE
;
2275 if (ublk_dev_is_zoned(ub
)) {
2276 ret
= ublk_revalidate_disk_zones(ub
);
2281 ret
= add_disk(disk
);
2285 set_bit(UB_STATE_USED
, &ub
->state
);
2289 ub
->dev_info
.state
= UBLK_S_DEV_DEAD
;
2290 ublk_put_device(ub
);
2295 mutex_unlock(&ub
->mutex
);
2299 static int ublk_ctrl_get_queue_affinity(struct ublk_device
*ub
,
2300 struct io_uring_cmd
*cmd
)
2302 const struct ublksrv_ctrl_cmd
*header
= io_uring_sqe_cmd(cmd
->sqe
);
2303 void __user
*argp
= (void __user
*)(unsigned long)header
->addr
;
2304 cpumask_var_t cpumask
;
2305 unsigned long queue
;
2306 unsigned int retlen
;
2310 if (header
->len
* BITS_PER_BYTE
< nr_cpu_ids
)
2312 if (header
->len
& (sizeof(unsigned long)-1))
2317 queue
= header
->data
[0];
2318 if (queue
>= ub
->dev_info
.nr_hw_queues
)
2321 if (!zalloc_cpumask_var(&cpumask
, GFP_KERNEL
))
2324 for_each_possible_cpu(i
) {
2325 if (ub
->tag_set
.map
[HCTX_TYPE_DEFAULT
].mq_map
[i
] == queue
)
2326 cpumask_set_cpu(i
, cpumask
);
2330 retlen
= min_t(unsigned short, header
->len
, cpumask_size());
2331 if (copy_to_user(argp
, cpumask
, retlen
))
2332 goto out_free_cpumask
;
2333 if (retlen
!= header
->len
&&
2334 clear_user(argp
+ retlen
, header
->len
- retlen
))
2335 goto out_free_cpumask
;
2339 free_cpumask_var(cpumask
);
2343 static inline void ublk_dump_dev_info(struct ublksrv_ctrl_dev_info
*info
)
2345 pr_devel("%s: dev id %d flags %llx\n", __func__
,
2346 info
->dev_id
, info
->flags
);
2347 pr_devel("\t nr_hw_queues %d queue_depth %d\n",
2348 info
->nr_hw_queues
, info
->queue_depth
);
2351 static int ublk_ctrl_add_dev(struct io_uring_cmd
*cmd
)
2353 const struct ublksrv_ctrl_cmd
*header
= io_uring_sqe_cmd(cmd
->sqe
);
2354 void __user
*argp
= (void __user
*)(unsigned long)header
->addr
;
2355 struct ublksrv_ctrl_dev_info info
;
2356 struct ublk_device
*ub
;
2359 if (header
->len
< sizeof(info
) || !header
->addr
)
2361 if (header
->queue_id
!= (u16
)-1) {
2362 pr_warn("%s: queue_id is wrong %x\n",
2363 __func__
, header
->queue_id
);
2367 if (copy_from_user(&info
, argp
, sizeof(info
)))
2370 if (capable(CAP_SYS_ADMIN
))
2371 info
.flags
&= ~UBLK_F_UNPRIVILEGED_DEV
;
2372 else if (!(info
.flags
& UBLK_F_UNPRIVILEGED_DEV
))
2376 * unprivileged device can't be trusted, but RECOVERY and
2377 * RECOVERY_REISSUE still may hang error handling, so can't
2378 * support recovery features for unprivileged ublk now
2380 * TODO: provide forward progress for RECOVERY handler, so that
2381 * unprivileged device can benefit from it
2383 if (info
.flags
& UBLK_F_UNPRIVILEGED_DEV
) {
2384 info
.flags
&= ~(UBLK_F_USER_RECOVERY_REISSUE
|
2385 UBLK_F_USER_RECOVERY
);
2388 * For USER_COPY, we depends on userspace to fill request
2389 * buffer by pwrite() to ublk char device, which can't be
2390 * used for unprivileged device
2392 if (info
.flags
& UBLK_F_USER_COPY
)
2396 /* the created device is always owned by current user */
2397 ublk_store_owner_uid_gid(&info
.owner_uid
, &info
.owner_gid
);
2399 if (header
->dev_id
!= info
.dev_id
) {
2400 pr_warn("%s: dev id not match %u %u\n",
2401 __func__
, header
->dev_id
, info
.dev_id
);
2405 if (header
->dev_id
!= U32_MAX
&& header
->dev_id
>= UBLK_MAX_UBLKS
) {
2406 pr_warn("%s: dev id is too large. Max supported is %d\n",
2407 __func__
, UBLK_MAX_UBLKS
- 1);
2411 ublk_dump_dev_info(&info
);
2413 ret
= mutex_lock_killable(&ublk_ctl_mutex
);
2418 if (ublks_added
>= ublks_max
)
2422 ub
= kzalloc(sizeof(*ub
), GFP_KERNEL
);
2425 mutex_init(&ub
->mutex
);
2426 spin_lock_init(&ub
->lock
);
2427 INIT_WORK(&ub
->quiesce_work
, ublk_quiesce_work_fn
);
2428 INIT_WORK(&ub
->stop_work
, ublk_stop_work_fn
);
2430 ret
= ublk_alloc_dev_number(ub
, header
->dev_id
);
2434 memcpy(&ub
->dev_info
, &info
, sizeof(info
));
2436 /* update device id */
2437 ub
->dev_info
.dev_id
= ub
->ub_number
;
2440 * 64bit flags will be copied back to userspace as feature
2441 * negotiation result, so have to clear flags which driver
2442 * doesn't support yet, then userspace can get correct flags
2443 * (features) to handle.
2445 ub
->dev_info
.flags
&= UBLK_F_ALL
;
2447 ub
->dev_info
.flags
|= UBLK_F_CMD_IOCTL_ENCODE
|
2448 UBLK_F_URING_CMD_COMP_IN_TASK
;
2450 /* GET_DATA isn't needed any more with USER_COPY */
2451 if (ublk_dev_is_user_copy(ub
))
2452 ub
->dev_info
.flags
&= ~UBLK_F_NEED_GET_DATA
;
2454 /* Zoned storage support requires user copy feature */
2455 if (ublk_dev_is_zoned(ub
) &&
2456 (!IS_ENABLED(CONFIG_BLK_DEV_ZONED
) || !ublk_dev_is_user_copy(ub
))) {
2458 goto out_free_dev_number
;
2461 /* We are not ready to support zero copy */
2462 ub
->dev_info
.flags
&= ~UBLK_F_SUPPORT_ZERO_COPY
;
2464 ub
->dev_info
.nr_hw_queues
= min_t(unsigned int,
2465 ub
->dev_info
.nr_hw_queues
, nr_cpu_ids
);
2466 ublk_align_max_io_size(ub
);
2468 ret
= ublk_init_queues(ub
);
2470 goto out_free_dev_number
;
2472 ret
= ublk_add_tag_set(ub
);
2474 goto out_deinit_queues
;
2477 if (copy_to_user(argp
, &ub
->dev_info
, sizeof(info
)))
2478 goto out_free_tag_set
;
2481 * Add the char dev so that ublksrv daemon can be setup.
2482 * ublk_add_chdev() will cleanup everything if it fails.
2484 ret
= ublk_add_chdev(ub
);
2488 blk_mq_free_tag_set(&ub
->tag_set
);
2490 ublk_deinit_queues(ub
);
2491 out_free_dev_number
:
2492 ublk_free_dev_number(ub
);
2494 mutex_destroy(&ub
->mutex
);
2497 mutex_unlock(&ublk_ctl_mutex
);
2501 static inline bool ublk_idr_freed(int id
)
2505 spin_lock(&ublk_idr_lock
);
2506 ptr
= idr_find(&ublk_index_idr
, id
);
2507 spin_unlock(&ublk_idr_lock
);
2512 static int ublk_ctrl_del_dev(struct ublk_device
**p_ub
, bool wait
)
2514 struct ublk_device
*ub
= *p_ub
;
2515 int idx
= ub
->ub_number
;
2518 ret
= mutex_lock_killable(&ublk_ctl_mutex
);
2522 if (!test_bit(UB_STATE_DELETED
, &ub
->state
)) {
2524 set_bit(UB_STATE_DELETED
, &ub
->state
);
2527 /* Mark the reference as consumed */
2529 ublk_put_device(ub
);
2530 mutex_unlock(&ublk_ctl_mutex
);
2533 * Wait until the idr is removed, then it can be reused after
2534 * DEL_DEV command is returned.
2536 * If we returns because of user interrupt, future delete command
2539 * - the device number isn't freed, this device won't or needn't
2540 * be deleted again, since UB_STATE_DELETED is set, and device
2541 * will be released after the last reference is dropped
2543 * - the device number is freed already, we will not find this
2544 * device via ublk_get_device_from_id()
2546 if (wait
&& wait_event_interruptible(ublk_idr_wq
, ublk_idr_freed(idx
)))
2551 static inline void ublk_ctrl_cmd_dump(struct io_uring_cmd
*cmd
)
2553 const struct ublksrv_ctrl_cmd
*header
= io_uring_sqe_cmd(cmd
->sqe
);
2555 pr_devel("%s: cmd_op %x, dev id %d qid %d data %llx buf %llx len %u\n",
2556 __func__
, cmd
->cmd_op
, header
->dev_id
, header
->queue_id
,
2557 header
->data
[0], header
->addr
, header
->len
);
2560 static int ublk_ctrl_stop_dev(struct ublk_device
*ub
)
2563 cancel_work_sync(&ub
->stop_work
);
2564 cancel_work_sync(&ub
->quiesce_work
);
2569 static int ublk_ctrl_get_dev_info(struct ublk_device
*ub
,
2570 struct io_uring_cmd
*cmd
)
2572 const struct ublksrv_ctrl_cmd
*header
= io_uring_sqe_cmd(cmd
->sqe
);
2573 void __user
*argp
= (void __user
*)(unsigned long)header
->addr
;
2575 if (header
->len
< sizeof(struct ublksrv_ctrl_dev_info
) || !header
->addr
)
2578 if (copy_to_user(argp
, &ub
->dev_info
, sizeof(ub
->dev_info
)))
2584 /* TYPE_DEVT is readonly, so fill it up before returning to userspace */
2585 static void ublk_ctrl_fill_params_devt(struct ublk_device
*ub
)
2587 ub
->params
.devt
.char_major
= MAJOR(ub
->cdev_dev
.devt
);
2588 ub
->params
.devt
.char_minor
= MINOR(ub
->cdev_dev
.devt
);
2591 ub
->params
.devt
.disk_major
= MAJOR(disk_devt(ub
->ub_disk
));
2592 ub
->params
.devt
.disk_minor
= MINOR(disk_devt(ub
->ub_disk
));
2594 ub
->params
.devt
.disk_major
= 0;
2595 ub
->params
.devt
.disk_minor
= 0;
2597 ub
->params
.types
|= UBLK_PARAM_TYPE_DEVT
;
2600 static int ublk_ctrl_get_params(struct ublk_device
*ub
,
2601 struct io_uring_cmd
*cmd
)
2603 const struct ublksrv_ctrl_cmd
*header
= io_uring_sqe_cmd(cmd
->sqe
);
2604 void __user
*argp
= (void __user
*)(unsigned long)header
->addr
;
2605 struct ublk_params_header ph
;
2608 if (header
->len
<= sizeof(ph
) || !header
->addr
)
2611 if (copy_from_user(&ph
, argp
, sizeof(ph
)))
2614 if (ph
.len
> header
->len
|| !ph
.len
)
2617 if (ph
.len
> sizeof(struct ublk_params
))
2618 ph
.len
= sizeof(struct ublk_params
);
2620 mutex_lock(&ub
->mutex
);
2621 ublk_ctrl_fill_params_devt(ub
);
2622 if (copy_to_user(argp
, &ub
->params
, ph
.len
))
2626 mutex_unlock(&ub
->mutex
);
2631 static int ublk_ctrl_set_params(struct ublk_device
*ub
,
2632 struct io_uring_cmd
*cmd
)
2634 const struct ublksrv_ctrl_cmd
*header
= io_uring_sqe_cmd(cmd
->sqe
);
2635 void __user
*argp
= (void __user
*)(unsigned long)header
->addr
;
2636 struct ublk_params_header ph
;
2639 if (header
->len
<= sizeof(ph
) || !header
->addr
)
2642 if (copy_from_user(&ph
, argp
, sizeof(ph
)))
2645 if (ph
.len
> header
->len
|| !ph
.len
|| !ph
.types
)
2648 if (ph
.len
> sizeof(struct ublk_params
))
2649 ph
.len
= sizeof(struct ublk_params
);
2651 /* parameters can only be changed when device isn't live */
2652 mutex_lock(&ub
->mutex
);
2653 if (ub
->dev_info
.state
== UBLK_S_DEV_LIVE
) {
2655 } else if (copy_from_user(&ub
->params
, argp
, ph
.len
)) {
2658 /* clear all we don't support yet */
2659 ub
->params
.types
&= UBLK_PARAM_TYPE_ALL
;
2660 ret
= ublk_validate_params(ub
);
2662 ub
->params
.types
= 0;
2664 mutex_unlock(&ub
->mutex
);
2669 static void ublk_queue_reinit(struct ublk_device
*ub
, struct ublk_queue
*ubq
)
2673 WARN_ON_ONCE(!(ubq
->ubq_daemon
&& ubq_daemon_is_dying(ubq
)));
2675 /* All old ioucmds have to be completed */
2676 ubq
->nr_io_ready
= 0;
2677 /* old daemon is PF_EXITING, put it now */
2678 put_task_struct(ubq
->ubq_daemon
);
2679 /* We have to reset it to NULL, otherwise ub won't accept new FETCH_REQ */
2680 ubq
->ubq_daemon
= NULL
;
2681 ubq
->timeout
= false;
2682 ubq
->canceling
= false;
2684 for (i
= 0; i
< ubq
->q_depth
; i
++) {
2685 struct ublk_io
*io
= &ubq
->ios
[i
];
2687 /* forget everything now and be ready for new FETCH_REQ */
2694 static int ublk_ctrl_start_recovery(struct ublk_device
*ub
,
2695 struct io_uring_cmd
*cmd
)
2697 const struct ublksrv_ctrl_cmd
*header
= io_uring_sqe_cmd(cmd
->sqe
);
2701 mutex_lock(&ub
->mutex
);
2702 if (!ublk_can_use_recovery(ub
))
2704 if (!ub
->nr_queues_ready
)
2707 * START_RECOVERY is only allowd after:
2709 * (1) UB_STATE_OPEN is not set, which means the dying process is exited
2710 * and related io_uring ctx is freed so file struct of /dev/ublkcX is
2713 * (2) UBLK_S_DEV_QUIESCED is set, which means the quiesce_work:
2714 * (a)has quiesced request queue
2715 * (b)has requeued every inflight rqs whose io_flags is ACTIVE
2716 * (c)has requeued/aborted every inflight rqs whose io_flags is NOT ACTIVE
2717 * (d)has completed/camceled all ioucmds owned by ther dying process
2719 if (test_bit(UB_STATE_OPEN
, &ub
->state
) ||
2720 ub
->dev_info
.state
!= UBLK_S_DEV_QUIESCED
) {
2724 pr_devel("%s: start recovery for dev id %d.\n", __func__
, header
->dev_id
);
2725 for (i
= 0; i
< ub
->dev_info
.nr_hw_queues
; i
++)
2726 ublk_queue_reinit(ub
, ublk_get_queue(ub
, i
));
2727 /* set to NULL, otherwise new ubq_daemon cannot mmap the io_cmd_buf */
2729 ub
->nr_queues_ready
= 0;
2730 ub
->nr_privileged_daemon
= 0;
2731 init_completion(&ub
->completion
);
2734 mutex_unlock(&ub
->mutex
);
2738 static int ublk_ctrl_end_recovery(struct ublk_device
*ub
,
2739 struct io_uring_cmd
*cmd
)
2741 const struct ublksrv_ctrl_cmd
*header
= io_uring_sqe_cmd(cmd
->sqe
);
2742 int ublksrv_pid
= (int)header
->data
[0];
2745 pr_devel("%s: Waiting for new ubq_daemons(nr: %d) are ready, dev id %d...\n",
2746 __func__
, ub
->dev_info
.nr_hw_queues
, header
->dev_id
);
2747 /* wait until new ubq_daemon sending all FETCH_REQ */
2748 if (wait_for_completion_interruptible(&ub
->completion
))
2751 pr_devel("%s: All new ubq_daemons(nr: %d) are ready, dev id %d\n",
2752 __func__
, ub
->dev_info
.nr_hw_queues
, header
->dev_id
);
2754 mutex_lock(&ub
->mutex
);
2755 if (!ublk_can_use_recovery(ub
))
2758 if (ub
->dev_info
.state
!= UBLK_S_DEV_QUIESCED
) {
2762 ub
->dev_info
.ublksrv_pid
= ublksrv_pid
;
2763 pr_devel("%s: new ublksrv_pid %d, dev id %d\n",
2764 __func__
, ublksrv_pid
, header
->dev_id
);
2765 blk_mq_unquiesce_queue(ub
->ub_disk
->queue
);
2766 pr_devel("%s: queue unquiesced, dev id %d.\n",
2767 __func__
, header
->dev_id
);
2768 blk_mq_kick_requeue_list(ub
->ub_disk
->queue
);
2769 ub
->dev_info
.state
= UBLK_S_DEV_LIVE
;
2772 mutex_unlock(&ub
->mutex
);
2776 static int ublk_ctrl_get_features(struct io_uring_cmd
*cmd
)
2778 const struct ublksrv_ctrl_cmd
*header
= io_uring_sqe_cmd(cmd
->sqe
);
2779 void __user
*argp
= (void __user
*)(unsigned long)header
->addr
;
2780 u64 features
= UBLK_F_ALL
& ~UBLK_F_SUPPORT_ZERO_COPY
;
2782 if (header
->len
!= UBLK_FEATURES_LEN
|| !header
->addr
)
2785 if (copy_to_user(argp
, &features
, UBLK_FEATURES_LEN
))
2792 * All control commands are sent via /dev/ublk-control, so we have to check
2793 * the destination device's permission
2795 static int ublk_char_dev_permission(struct ublk_device
*ub
,
2796 const char *dev_path
, int mask
)
2802 err
= kern_path(dev_path
, LOOKUP_FOLLOW
, &path
);
2806 err
= vfs_getattr(&path
, &stat
, STATX_TYPE
, AT_STATX_SYNC_AS_STAT
);
2811 if (stat
.rdev
!= ub
->cdev_dev
.devt
|| !S_ISCHR(stat
.mode
))
2814 err
= inode_permission(&nop_mnt_idmap
,
2815 d_backing_inode(path
.dentry
), mask
);
2821 static int ublk_ctrl_uring_cmd_permission(struct ublk_device
*ub
,
2822 struct io_uring_cmd
*cmd
)
2824 struct ublksrv_ctrl_cmd
*header
= (struct ublksrv_ctrl_cmd
*)io_uring_sqe_cmd(cmd
->sqe
);
2825 bool unprivileged
= ub
->dev_info
.flags
& UBLK_F_UNPRIVILEGED_DEV
;
2826 void __user
*argp
= (void __user
*)(unsigned long)header
->addr
;
2827 char *dev_path
= NULL
;
2831 if (!unprivileged
) {
2832 if (!capable(CAP_SYS_ADMIN
))
2835 * The new added command of UBLK_CMD_GET_DEV_INFO2 includes
2836 * char_dev_path in payload too, since userspace may not
2837 * know if the specified device is created as unprivileged
2840 if (_IOC_NR(cmd
->cmd_op
) != UBLK_CMD_GET_DEV_INFO2
)
2845 * User has to provide the char device path for unprivileged ublk
2847 * header->addr always points to the dev path buffer, and
2848 * header->dev_path_len records length of dev path buffer.
2850 if (!header
->dev_path_len
|| header
->dev_path_len
> PATH_MAX
)
2853 if (header
->len
< header
->dev_path_len
)
2856 dev_path
= memdup_user_nul(argp
, header
->dev_path_len
);
2857 if (IS_ERR(dev_path
))
2858 return PTR_ERR(dev_path
);
2861 switch (_IOC_NR(cmd
->cmd_op
)) {
2862 case UBLK_CMD_GET_DEV_INFO
:
2863 case UBLK_CMD_GET_DEV_INFO2
:
2864 case UBLK_CMD_GET_QUEUE_AFFINITY
:
2865 case UBLK_CMD_GET_PARAMS
:
2866 case (_IOC_NR(UBLK_U_CMD_GET_FEATURES
)):
2869 case UBLK_CMD_START_DEV
:
2870 case UBLK_CMD_STOP_DEV
:
2871 case UBLK_CMD_ADD_DEV
:
2872 case UBLK_CMD_DEL_DEV
:
2873 case UBLK_CMD_SET_PARAMS
:
2874 case UBLK_CMD_START_USER_RECOVERY
:
2875 case UBLK_CMD_END_USER_RECOVERY
:
2876 mask
= MAY_READ
| MAY_WRITE
;
2882 ret
= ublk_char_dev_permission(ub
, dev_path
, mask
);
2884 header
->len
-= header
->dev_path_len
;
2885 header
->addr
+= header
->dev_path_len
;
2887 pr_devel("%s: dev id %d cmd_op %x uid %d gid %d path %s ret %d\n",
2888 __func__
, ub
->ub_number
, cmd
->cmd_op
,
2889 ub
->dev_info
.owner_uid
, ub
->dev_info
.owner_gid
,
2896 static int ublk_ctrl_uring_cmd(struct io_uring_cmd
*cmd
,
2897 unsigned int issue_flags
)
2899 const struct ublksrv_ctrl_cmd
*header
= io_uring_sqe_cmd(cmd
->sqe
);
2900 struct ublk_device
*ub
= NULL
;
2901 u32 cmd_op
= cmd
->cmd_op
;
2904 if (issue_flags
& IO_URING_F_NONBLOCK
)
2907 ublk_ctrl_cmd_dump(cmd
);
2909 if (!(issue_flags
& IO_URING_F_SQE128
))
2912 ret
= ublk_check_cmd_op(cmd_op
);
2916 if (cmd_op
== UBLK_U_CMD_GET_FEATURES
) {
2917 ret
= ublk_ctrl_get_features(cmd
);
2921 if (_IOC_NR(cmd_op
) != UBLK_CMD_ADD_DEV
) {
2923 ub
= ublk_get_device_from_id(header
->dev_id
);
2927 ret
= ublk_ctrl_uring_cmd_permission(ub
, cmd
);
2932 switch (_IOC_NR(cmd_op
)) {
2933 case UBLK_CMD_START_DEV
:
2934 ret
= ublk_ctrl_start_dev(ub
, cmd
);
2936 case UBLK_CMD_STOP_DEV
:
2937 ret
= ublk_ctrl_stop_dev(ub
);
2939 case UBLK_CMD_GET_DEV_INFO
:
2940 case UBLK_CMD_GET_DEV_INFO2
:
2941 ret
= ublk_ctrl_get_dev_info(ub
, cmd
);
2943 case UBLK_CMD_ADD_DEV
:
2944 ret
= ublk_ctrl_add_dev(cmd
);
2946 case UBLK_CMD_DEL_DEV
:
2947 ret
= ublk_ctrl_del_dev(&ub
, true);
2949 case UBLK_CMD_DEL_DEV_ASYNC
:
2950 ret
= ublk_ctrl_del_dev(&ub
, false);
2952 case UBLK_CMD_GET_QUEUE_AFFINITY
:
2953 ret
= ublk_ctrl_get_queue_affinity(ub
, cmd
);
2955 case UBLK_CMD_GET_PARAMS
:
2956 ret
= ublk_ctrl_get_params(ub
, cmd
);
2958 case UBLK_CMD_SET_PARAMS
:
2959 ret
= ublk_ctrl_set_params(ub
, cmd
);
2961 case UBLK_CMD_START_USER_RECOVERY
:
2962 ret
= ublk_ctrl_start_recovery(ub
, cmd
);
2964 case UBLK_CMD_END_USER_RECOVERY
:
2965 ret
= ublk_ctrl_end_recovery(ub
, cmd
);
2974 ublk_put_device(ub
);
2976 io_uring_cmd_done(cmd
, ret
, 0, issue_flags
);
2977 pr_devel("%s: cmd done ret %d cmd_op %x, dev id %d qid %d\n",
2978 __func__
, ret
, cmd
->cmd_op
, header
->dev_id
, header
->queue_id
);
2979 return -EIOCBQUEUED
;
2982 static const struct file_operations ublk_ctl_fops
= {
2983 .open
= nonseekable_open
,
2984 .uring_cmd
= ublk_ctrl_uring_cmd
,
2985 .owner
= THIS_MODULE
,
2986 .llseek
= noop_llseek
,
2989 static struct miscdevice ublk_misc
= {
2990 .minor
= MISC_DYNAMIC_MINOR
,
2991 .name
= "ublk-control",
2992 .fops
= &ublk_ctl_fops
,
2995 static int __init
ublk_init(void)
2999 BUILD_BUG_ON((u64
)UBLKSRV_IO_BUF_OFFSET
+
3000 UBLKSRV_IO_BUF_TOTAL_SIZE
< UBLKSRV_IO_BUF_OFFSET
);
3002 init_waitqueue_head(&ublk_idr_wq
);
3004 ret
= misc_register(&ublk_misc
);
3008 ret
= alloc_chrdev_region(&ublk_chr_devt
, 0, UBLK_MINORS
, "ublk-char");
3010 goto unregister_mis
;
3012 ret
= class_register(&ublk_chr_class
);
3014 goto free_chrdev_region
;
3019 unregister_chrdev_region(ublk_chr_devt
, UBLK_MINORS
);
3021 misc_deregister(&ublk_misc
);
3025 static void __exit
ublk_exit(void)
3027 struct ublk_device
*ub
;
3030 idr_for_each_entry(&ublk_index_idr
, ub
, id
)
3033 class_unregister(&ublk_chr_class
);
3034 misc_deregister(&ublk_misc
);
3036 idr_destroy(&ublk_index_idr
);
3037 unregister_chrdev_region(ublk_chr_devt
, UBLK_MINORS
);
3040 module_init(ublk_init
);
3041 module_exit(ublk_exit
);
3043 static int ublk_set_max_ublks(const char *buf
, const struct kernel_param
*kp
)
3045 return param_set_uint_minmax(buf
, kp
, 0, UBLK_MAX_UBLKS
);
3048 static int ublk_get_max_ublks(char *buf
, const struct kernel_param
*kp
)
3050 return sysfs_emit(buf
, "%u\n", ublks_max
);
3053 static const struct kernel_param_ops ublk_max_ublks_ops
= {
3054 .set
= ublk_set_max_ublks
,
3055 .get
= ublk_get_max_ublks
,
3058 module_param_cb(ublks_max
, &ublk_max_ublks_ops
, &ublks_max
, 0644);
3059 MODULE_PARM_DESC(ublks_max
, "max number of ublk devices allowed to add(default: 64)");
3061 MODULE_AUTHOR("Ming Lei <ming.lei@redhat.com>");
3062 MODULE_DESCRIPTION("Userspace block device");
3063 MODULE_LICENSE("GPL");