ublk: grab request reference when the request is handled by userspace
[linux-block.git] / drivers / block / ublk_drv.c
CommitLineData
71f28f31
ML
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Userspace block device - block device which IO is handled from userspace
4 *
5 * Take full use of io_uring passthrough command for communicating with
6 * ublk userspace daemon(ublksrvd) for handling basic IO request.
7 *
8 * Copyright 2022 Ming Lei <ming.lei@redhat.com>
9 *
10 * (part of code stolen from loop.c)
11 */
12#include <linux/module.h>
13#include <linux/moduleparam.h>
14#include <linux/sched.h>
15#include <linux/fs.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>
42#include <linux/mm.h>
43#include <asm/page.h>
0edb3696 44#include <linux/task_work.h>
4093cb5a 45#include <linux/namei.h>
82840669 46#include <linux/kref.h>
71f28f31
ML
47#include <uapi/linux/ublk_cmd.h>
48
49#define UBLK_MINORS (1U << MINORBITS)
50
6d8c5afc 51/* All UBLK_F_* have to be included into UBLK_F_ALL */
c86019ff
Z
52#define UBLK_F_ALL (UBLK_F_SUPPORT_ZERO_COPY \
53 | UBLK_F_URING_CMD_COMP_IN_TASK \
77a440e2 54 | UBLK_F_NEED_GET_DATA \
a0d41dc1 55 | UBLK_F_USER_RECOVERY \
4093cb5a 56 | UBLK_F_USER_RECOVERY_REISSUE \
2d786e66
ML
57 | UBLK_F_UNPRIVILEGED_DEV \
58 | UBLK_F_CMD_IOCTL_ENCODE)
6d8c5afc 59
0aa73170 60/* All UBLK_PARAM_TYPE_* should be included here */
abb864d3
ML
61#define UBLK_PARAM_TYPE_ALL (UBLK_PARAM_TYPE_BASIC | \
62 UBLK_PARAM_TYPE_DISCARD | UBLK_PARAM_TYPE_DEVT)
0aa73170 63
0edb3696 64struct ublk_rq_data {
7d4a9317 65 struct llist_node node;
82840669
ML
66
67 struct kref ref;
0edb3696
ML
68};
69
71f28f31 70struct ublk_uring_cmd_pdu {
3ab6e94c 71 struct ublk_queue *ubq;
71f28f31
ML
72};
73
74/*
75 * io command is active: sqe cmd is received, and its cqe isn't done
76 *
77 * If the flag is set, the io command is owned by ublk driver, and waited
78 * for incoming blk-mq request from the ublk block device.
79 *
80 * If the flag is cleared, the io command will be completed, and owned by
81 * ublk server.
82 */
83#define UBLK_IO_FLAG_ACTIVE 0x01
84
85/*
86 * IO command is completed via cqe, and it is being handled by ublksrv, and
87 * not committed yet
88 *
89 * Basically exclusively with UBLK_IO_FLAG_ACTIVE, so can be served for
90 * cross verification
91 */
92#define UBLK_IO_FLAG_OWNED_BY_SRV 0x02
93
94/*
95 * IO command is aborted, so this flag is set in case of
96 * !UBLK_IO_FLAG_ACTIVE.
97 *
98 * After this flag is observed, any pending or new incoming request
99 * associated with this io command will be failed immediately
100 */
101#define UBLK_IO_FLAG_ABORTED 0x04
102
c86019ff
Z
103/*
104 * UBLK_IO_FLAG_NEED_GET_DATA is set because IO command requires
105 * get data buffer address from ublksrv.
106 *
107 * Then, bio data could be copied into this data buffer for a WRITE request
108 * after the IO command is issued again and UBLK_IO_FLAG_NEED_GET_DATA is unset.
109 */
110#define UBLK_IO_FLAG_NEED_GET_DATA 0x08
111
71f28f31
ML
112struct ublk_io {
113 /* userspace buffer address from io cmd */
114 __u64 addr;
115 unsigned int flags;
116 int res;
117
118 struct io_uring_cmd *cmd;
119};
120
121struct ublk_queue {
122 int q_id;
123 int q_depth;
124
0edb3696 125 unsigned long flags;
71f28f31
ML
126 struct task_struct *ubq_daemon;
127 char *io_cmd_buf;
128
3ab6e94c
ML
129 struct llist_head io_cmds;
130
71f28f31
ML
131 unsigned long io_addr; /* mapped vm address */
132 unsigned int max_io_sz;
bbae8d1f 133 bool force_abort;
c0b79b0f 134 bool timeout;
71f28f31
ML
135 unsigned short nr_io_ready; /* how many ios setup */
136 struct ublk_device *dev;
72495b5a 137 struct ublk_io ios[];
71f28f31
ML
138};
139
140#define UBLK_DAEMON_MONITOR_PERIOD (5 * HZ)
141
142struct ublk_device {
143 struct gendisk *ub_disk;
71f28f31
ML
144
145 char *__queues;
146
29baef78 147 unsigned int queue_size;
71f28f31
ML
148 struct ublksrv_ctrl_dev_info dev_info;
149
150 struct blk_mq_tag_set tag_set;
151
152 struct cdev cdev;
153 struct device cdev_dev;
154
8d9fdb60
DC
155#define UB_STATE_OPEN 0
156#define UB_STATE_USED 1
0abe39de 157#define UB_STATE_DELETED 2
fa362045 158 unsigned long state;
71f28f31
ML
159 int ub_number;
160
161 struct mutex mutex;
162
e94eb459 163 spinlock_t mm_lock;
71f28f31
ML
164 struct mm_struct *mm;
165
0aa73170
ML
166 struct ublk_params params;
167
71f28f31
ML
168 struct completion completion;
169 unsigned int nr_queues_ready;
73a166d9 170 unsigned int nr_privileged_daemon;
71f28f31
ML
171
172 /*
173 * Our ubq->daemon may be killed without any notification, so
174 * monitor each queue's daemon periodically
175 */
176 struct delayed_work monitor_work;
bbae8d1f 177 struct work_struct quiesce_work;
71f28f31
ML
178 struct work_struct stop_work;
179};
180
0aa73170
ML
181/* header of ublk_params */
182struct ublk_params_header {
183 __u32 len;
184 __u32 types;
185};
186
82840669
ML
187static inline void __ublk_complete_rq(struct request *req);
188static void ublk_complete_rq(struct kref *ref);
189
71f28f31
ML
190static dev_t ublk_chr_devt;
191static struct class *ublk_chr_class;
192
193static DEFINE_IDR(ublk_index_idr);
194static DEFINE_SPINLOCK(ublk_idr_lock);
195static wait_queue_head_t ublk_idr_wq; /* wait until one idr is freed */
196
197static DEFINE_MUTEX(ublk_ctl_mutex);
198
403ebc87
ML
199/*
200 * Max ublk devices allowed to add
201 *
202 * It can be extended to one per-user limit in future or even controlled
203 * by cgroup.
204 */
205static unsigned int ublks_max = 64;
206static unsigned int ublks_added; /* protected by ublk_ctl_mutex */
207
71f28f31
ML
208static struct miscdevice ublk_misc;
209
0aa73170
ML
210static void ublk_dev_param_basic_apply(struct ublk_device *ub)
211{
212 struct request_queue *q = ub->ub_disk->queue;
213 const struct ublk_param_basic *p = &ub->params.basic;
214
215 blk_queue_logical_block_size(q, 1 << p->logical_bs_shift);
216 blk_queue_physical_block_size(q, 1 << p->physical_bs_shift);
217 blk_queue_io_min(q, 1 << p->io_min_shift);
218 blk_queue_io_opt(q, 1 << p->io_opt_shift);
219
220 blk_queue_write_cache(q, p->attrs & UBLK_ATTR_VOLATILE_CACHE,
221 p->attrs & UBLK_ATTR_FUA);
222 if (p->attrs & UBLK_ATTR_ROTATIONAL)
223 blk_queue_flag_clear(QUEUE_FLAG_NONROT, q);
224 else
225 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
226
227 blk_queue_max_hw_sectors(q, p->max_sectors);
228 blk_queue_chunk_sectors(q, p->chunk_sectors);
229 blk_queue_virt_boundary(q, p->virt_boundary_mask);
230
231 if (p->attrs & UBLK_ATTR_READ_ONLY)
232 set_disk_ro(ub->ub_disk, true);
233
234 set_capacity(ub->ub_disk, p->dev_sectors);
235}
236
237static void ublk_dev_param_discard_apply(struct ublk_device *ub)
238{
239 struct request_queue *q = ub->ub_disk->queue;
240 const struct ublk_param_discard *p = &ub->params.discard;
241
242 q->limits.discard_alignment = p->discard_alignment;
243 q->limits.discard_granularity = p->discard_granularity;
244 blk_queue_max_discard_sectors(q, p->max_discard_sectors);
245 blk_queue_max_write_zeroes_sectors(q,
246 p->max_write_zeroes_sectors);
247 blk_queue_max_discard_segments(q, p->max_discard_segments);
248}
249
250static int ublk_validate_params(const struct ublk_device *ub)
251{
252 /* basic param is the only one which must be set */
253 if (ub->params.types & UBLK_PARAM_TYPE_BASIC) {
254 const struct ublk_param_basic *p = &ub->params.basic;
255
1d166527 256 if (p->logical_bs_shift > PAGE_SHIFT || p->logical_bs_shift < 9)
0aa73170
ML
257 return -EINVAL;
258
259 if (p->logical_bs_shift > p->physical_bs_shift)
260 return -EINVAL;
261
4bf9cbf3 262 if (p->max_sectors > (ub->dev_info.max_io_buf_bytes >> 9))
0aa73170
ML
263 return -EINVAL;
264 } else
265 return -EINVAL;
266
267 if (ub->params.types & UBLK_PARAM_TYPE_DISCARD) {
268 const struct ublk_param_discard *p = &ub->params.discard;
269
270 /* So far, only support single segment discard */
271 if (p->max_discard_sectors && p->max_discard_segments != 1)
272 return -EINVAL;
273
274 if (!p->discard_granularity)
275 return -EINVAL;
276 }
277
abb864d3
ML
278 /* dev_t is read-only */
279 if (ub->params.types & UBLK_PARAM_TYPE_DEVT)
280 return -EINVAL;
281
0aa73170
ML
282 return 0;
283}
284
285static int ublk_apply_params(struct ublk_device *ub)
286{
287 if (!(ub->params.types & UBLK_PARAM_TYPE_BASIC))
288 return -EINVAL;
289
290 ublk_dev_param_basic_apply(ub);
291
292 if (ub->params.types & UBLK_PARAM_TYPE_DISCARD)
293 ublk_dev_param_discard_apply(ub);
294
295 return 0;
296}
297
82840669
ML
298static inline bool ublk_need_req_ref(const struct ublk_queue *ubq)
299{
300 return false;
301}
302
303static inline void ublk_init_req_ref(const struct ublk_queue *ubq,
304 struct request *req)
305{
306 if (ublk_need_req_ref(ubq)) {
307 struct ublk_rq_data *data = blk_mq_rq_to_pdu(req);
308
309 kref_init(&data->ref);
310 }
311}
312
313static inline bool ublk_get_req_ref(const struct ublk_queue *ubq,
314 struct request *req)
315{
316 if (ublk_need_req_ref(ubq)) {
317 struct ublk_rq_data *data = blk_mq_rq_to_pdu(req);
318
319 return kref_get_unless_zero(&data->ref);
320 }
321
322 return true;
323}
324
325static inline void ublk_put_req_ref(const struct ublk_queue *ubq,
326 struct request *req)
327{
328 if (ublk_need_req_ref(ubq)) {
329 struct ublk_rq_data *data = blk_mq_rq_to_pdu(req);
330
331 kref_put(&data->ref, ublk_complete_rq);
332 } else {
333 __ublk_complete_rq(req);
334 }
335}
336
c86019ff
Z
337static inline bool ublk_need_get_data(const struct ublk_queue *ubq)
338{
96cf2f54 339 return ubq->flags & UBLK_F_NEED_GET_DATA;
c86019ff
Z
340}
341
71f28f31
ML
342static struct ublk_device *ublk_get_device(struct ublk_device *ub)
343{
344 if (kobject_get_unless_zero(&ub->cdev_dev.kobj))
345 return ub;
346 return NULL;
347}
348
349static void ublk_put_device(struct ublk_device *ub)
350{
351 put_device(&ub->cdev_dev);
352}
353
354static inline struct ublk_queue *ublk_get_queue(struct ublk_device *dev,
355 int qid)
356{
357 return (struct ublk_queue *)&(dev->__queues[qid * dev->queue_size]);
358}
359
360static inline bool ublk_rq_has_data(const struct request *rq)
361{
731e208d 362 return bio_has_data(rq->bio);
71f28f31
ML
363}
364
365static inline struct ublksrv_io_desc *ublk_get_iod(struct ublk_queue *ubq,
366 int tag)
367{
368 return (struct ublksrv_io_desc *)
369 &(ubq->io_cmd_buf[tag * sizeof(struct ublksrv_io_desc)]);
370}
371
372static inline char *ublk_queue_cmd_buf(struct ublk_device *ub, int q_id)
373{
374 return ublk_get_queue(ub, q_id)->io_cmd_buf;
375}
376
377static inline int ublk_queue_cmd_buf_size(struct ublk_device *ub, int q_id)
378{
379 struct ublk_queue *ubq = ublk_get_queue(ub, q_id);
380
381 return round_up(ubq->q_depth * sizeof(struct ublksrv_io_desc),
382 PAGE_SIZE);
383}
384
a0d41dc1
Z
385static inline bool ublk_queue_can_use_recovery_reissue(
386 struct ublk_queue *ubq)
387{
96cf2f54
ML
388 return (ubq->flags & UBLK_F_USER_RECOVERY) &&
389 (ubq->flags & UBLK_F_USER_RECOVERY_REISSUE);
a0d41dc1
Z
390}
391
77a440e2
Z
392static inline bool ublk_queue_can_use_recovery(
393 struct ublk_queue *ubq)
394{
96cf2f54 395 return ubq->flags & UBLK_F_USER_RECOVERY;
77a440e2
Z
396}
397
398static inline bool ublk_can_use_recovery(struct ublk_device *ub)
399{
96cf2f54 400 return ub->dev_info.flags & UBLK_F_USER_RECOVERY;
77a440e2
Z
401}
402
6d9e6dfd
CH
403static void ublk_free_disk(struct gendisk *disk)
404{
405 struct ublk_device *ub = disk->private_data;
406
407 clear_bit(UB_STATE_USED, &ub->state);
408 put_device(&ub->cdev_dev);
409}
410
48a90519
ML
411static void ublk_store_owner_uid_gid(unsigned int *owner_uid,
412 unsigned int *owner_gid)
413{
414 kuid_t uid;
415 kgid_t gid;
416
417 current_uid_gid(&uid, &gid);
418
419 *owner_uid = from_kuid(&init_user_ns, uid);
420 *owner_gid = from_kgid(&init_user_ns, gid);
421}
422
423static int ublk_open(struct block_device *bdev, fmode_t mode)
424{
425 struct ublk_device *ub = bdev->bd_disk->private_data;
426
427 if (capable(CAP_SYS_ADMIN))
428 return 0;
429
430 /*
431 * If it is one unprivileged device, only owner can open
432 * the disk. Otherwise it could be one trap made by one
433 * evil user who grants this disk's privileges to other
434 * users deliberately.
435 *
436 * This way is reasonable too given anyone can create
437 * unprivileged device, and no need other's grant.
438 */
439 if (ub->dev_info.flags & UBLK_F_UNPRIVILEGED_DEV) {
440 unsigned int curr_uid, curr_gid;
441
442 ublk_store_owner_uid_gid(&curr_uid, &curr_gid);
443
444 if (curr_uid != ub->dev_info.owner_uid || curr_gid !=
445 ub->dev_info.owner_gid)
446 return -EPERM;
447 }
448
449 return 0;
450}
451
71f28f31
ML
452static const struct block_device_operations ub_fops = {
453 .owner = THIS_MODULE,
48a90519 454 .open = ublk_open,
6d9e6dfd 455 .free_disk = ublk_free_disk,
71f28f31
ML
456};
457
458#define UBLK_MAX_PIN_PAGES 32
459
71f28f31
ML
460struct ublk_io_iter {
461 struct page *pages[UBLK_MAX_PIN_PAGES];
71f28f31
ML
462 struct bio *bio;
463 struct bvec_iter iter;
464};
465
981f95a5
ML
466/* return how many pages are copied */
467static void ublk_copy_io_pages(struct ublk_io_iter *data,
468 size_t total, size_t pg_off, int dir)
71f28f31 469{
71f28f31
ML
470 unsigned done = 0;
471 unsigned pg_idx = 0;
472
473 while (done < total) {
474 struct bio_vec bv = bio_iter_iovec(data->bio, data->iter);
981f95a5
ML
475 unsigned int bytes = min3(bv.bv_len, (unsigned)total - done,
476 (unsigned)(PAGE_SIZE - pg_off));
71f28f31
ML
477 void *bv_buf = bvec_kmap_local(&bv);
478 void *pg_buf = kmap_local_page(data->pages[pg_idx]);
479
981f95a5
ML
480 if (dir == ITER_DEST)
481 memcpy(pg_buf + pg_off, bv_buf, bytes);
71f28f31 482 else
981f95a5 483 memcpy(bv_buf, pg_buf + pg_off, bytes);
71f28f31
ML
484
485 kunmap_local(pg_buf);
486 kunmap_local(bv_buf);
487
488 /* advance page array */
981f95a5
ML
489 pg_off += bytes;
490 if (pg_off == PAGE_SIZE) {
71f28f31 491 pg_idx += 1;
981f95a5 492 pg_off = 0;
71f28f31
ML
493 }
494
495 done += bytes;
496
497 /* advance bio */
498 bio_advance_iter_single(data->bio, &data->iter, bytes);
499 if (!data->iter.bi_size) {
500 data->bio = data->bio->bi_next;
501 if (data->bio == NULL)
502 break;
503 data->iter = data->bio->bi_iter;
504 }
505 }
71f28f31
ML
506}
507
981f95a5
ML
508/*
509 * Copy data between request pages and io_iter, and 'offset'
510 * is the start point of linear offset of request.
511 */
512static size_t ublk_copy_user_pages(const struct request *req,
513 struct iov_iter *uiter, int dir)
71f28f31 514{
71f28f31 515 struct ublk_io_iter iter = {
981f95a5
ML
516 .bio = req->bio,
517 .iter = req->bio->bi_iter,
71f28f31 518 };
981f95a5
ML
519 size_t done = 0;
520
521 while (iov_iter_count(uiter) && iter.bio) {
522 unsigned nr_pages;
523 size_t len, off;
524 int i;
525
526 len = iov_iter_get_pages2(uiter, iter.pages,
527 iov_iter_count(uiter),
528 UBLK_MAX_PIN_PAGES, &off);
529 if (len <= 0)
530 return done;
531
532 ublk_copy_io_pages(&iter, len, off, dir);
533 nr_pages = DIV_ROUND_UP(len + off, PAGE_SIZE);
534 for (i = 0; i < nr_pages; i++) {
535 if (dir == ITER_DEST)
71f28f31
ML
536 set_page_dirty(iter.pages[i]);
537 put_page(iter.pages[i]);
538 }
981f95a5 539 done += len;
71f28f31
ML
540 }
541
542 return done;
543}
544
2f3af723
ML
545static inline bool ublk_need_map_req(const struct request *req)
546{
547 return ublk_rq_has_data(req) && req_op(req) == REQ_OP_WRITE;
548}
549
550static inline bool ublk_need_unmap_req(const struct request *req)
551{
552 return ublk_rq_has_data(req) && req_op(req) == REQ_OP_READ;
553}
554
71f28f31
ML
555static int ublk_map_io(const struct ublk_queue *ubq, const struct request *req,
556 struct ublk_io *io)
557{
558 const unsigned int rq_bytes = blk_rq_bytes(req);
23ef8220 559
71f28f31
ML
560 /*
561 * no zero copy, we delay copy WRITE request data into ublksrv
562 * context and the big benefit is that pinning pages in current
563 * context is pretty fast, see ublk_pin_user_pages
564 */
2f3af723 565 if (ublk_need_map_req(req)) {
981f95a5
ML
566 struct iov_iter iter;
567 struct iovec iov;
568 const int dir = ITER_DEST;
71f28f31 569
981f95a5
ML
570 import_single_range(dir, u64_to_user_ptr(io->addr), rq_bytes,
571 &iov, &iter);
71f28f31 572
981f95a5 573 return ublk_copy_user_pages(req, &iter, dir);
71f28f31
ML
574 }
575 return rq_bytes;
576}
577
578static int ublk_unmap_io(const struct ublk_queue *ubq,
579 const struct request *req,
580 struct ublk_io *io)
581{
582 const unsigned int rq_bytes = blk_rq_bytes(req);
583
2f3af723 584 if (ublk_need_unmap_req(req)) {
981f95a5
ML
585 struct iov_iter iter;
586 struct iovec iov;
587 const int dir = ITER_SOURCE;
71f28f31
ML
588
589 WARN_ON_ONCE(io->res > rq_bytes);
590
981f95a5
ML
591 import_single_range(dir, u64_to_user_ptr(io->addr), io->res,
592 &iov, &iter);
593 return ublk_copy_user_pages(req, &iter, dir);
71f28f31
ML
594 }
595 return rq_bytes;
596}
597
598static inline unsigned int ublk_req_build_flags(struct request *req)
599{
600 unsigned flags = 0;
601
602 if (req->cmd_flags & REQ_FAILFAST_DEV)
603 flags |= UBLK_IO_F_FAILFAST_DEV;
604
605 if (req->cmd_flags & REQ_FAILFAST_TRANSPORT)
606 flags |= UBLK_IO_F_FAILFAST_TRANSPORT;
607
608 if (req->cmd_flags & REQ_FAILFAST_DRIVER)
609 flags |= UBLK_IO_F_FAILFAST_DRIVER;
610
611 if (req->cmd_flags & REQ_META)
612 flags |= UBLK_IO_F_META;
613
71f28f31
ML
614 if (req->cmd_flags & REQ_FUA)
615 flags |= UBLK_IO_F_FUA;
616
71f28f31
ML
617 if (req->cmd_flags & REQ_NOUNMAP)
618 flags |= UBLK_IO_F_NOUNMAP;
619
620 if (req->cmd_flags & REQ_SWAP)
621 flags |= UBLK_IO_F_SWAP;
622
623 return flags;
624}
625
f2450f8a 626static blk_status_t ublk_setup_iod(struct ublk_queue *ubq, struct request *req)
71f28f31
ML
627{
628 struct ublksrv_io_desc *iod = ublk_get_iod(ubq, req->tag);
629 struct ublk_io *io = &ubq->ios[req->tag];
630 u32 ublk_op;
631
632 switch (req_op(req)) {
633 case REQ_OP_READ:
634 ublk_op = UBLK_IO_OP_READ;
635 break;
636 case REQ_OP_WRITE:
637 ublk_op = UBLK_IO_OP_WRITE;
638 break;
639 case REQ_OP_FLUSH:
640 ublk_op = UBLK_IO_OP_FLUSH;
641 break;
642 case REQ_OP_DISCARD:
643 ublk_op = UBLK_IO_OP_DISCARD;
644 break;
645 case REQ_OP_WRITE_ZEROES:
646 ublk_op = UBLK_IO_OP_WRITE_ZEROES;
647 break;
648 default:
649 return BLK_STS_IOERR;
650 }
651
652 /* need to translate since kernel may change */
653 iod->op_flags = ublk_op | ublk_req_build_flags(req);
654 iod->nr_sectors = blk_rq_sectors(req);
655 iod->start_sector = blk_rq_pos(req);
656 iod->addr = io->addr;
657
658 return BLK_STS_OK;
659}
660
661static inline struct ublk_uring_cmd_pdu *ublk_get_uring_cmd_pdu(
662 struct io_uring_cmd *ioucmd)
663{
664 return (struct ublk_uring_cmd_pdu *)&ioucmd->pdu;
665}
666
966120b5 667static inline bool ubq_daemon_is_dying(struct ublk_queue *ubq)
71f28f31
ML
668{
669 return ubq->ubq_daemon->flags & PF_EXITING;
670}
671
672/* todo: handle partial completion */
82840669 673static inline void __ublk_complete_rq(struct request *req)
71f28f31
ML
674{
675 struct ublk_queue *ubq = req->mq_hctx->driver_data;
676 struct ublk_io *io = &ubq->ios[req->tag];
677 unsigned int unmapped_bytes;
903f8aee 678 blk_status_t res = BLK_STS_OK;
71f28f31 679
82840669
ML
680 /* called from ublk_abort_queue() code path */
681 if (io->flags & UBLK_IO_FLAG_ABORTED) {
682 res = BLK_STS_IOERR;
683 goto exit;
684 }
685
71f28f31
ML
686 /* failed read IO if nothing is read */
687 if (!io->res && req_op(req) == REQ_OP_READ)
688 io->res = -EIO;
689
690 if (io->res < 0) {
903f8aee
ML
691 res = errno_to_blk_status(io->res);
692 goto exit;
71f28f31
ML
693 }
694
695 /*
b352389e 696 * FLUSH, DISCARD or WRITE_ZEROES usually won't return bytes returned, so end them
71f28f31
ML
697 * directly.
698 *
699 * Both the two needn't unmap.
700 */
903f8aee
ML
701 if (req_op(req) != REQ_OP_READ && req_op(req) != REQ_OP_WRITE)
702 goto exit;
71f28f31
ML
703
704 /* for READ request, writing data in iod->addr to rq buffers */
705 unmapped_bytes = ublk_unmap_io(ubq, req, io);
706
707 /*
708 * Extremely impossible since we got data filled in just before
709 *
710 * Re-read simply for this unlikely case.
711 */
712 if (unlikely(unmapped_bytes < io->res))
713 io->res = unmapped_bytes;
714
715 if (blk_update_request(req, BLK_STS_OK, io->res))
716 blk_mq_requeue_request(req, true);
717 else
718 __blk_mq_end_request(req, BLK_STS_OK);
903f8aee
ML
719
720 return;
721exit:
722 blk_mq_end_request(req, res);
71f28f31
ML
723}
724
82840669
ML
725static void ublk_complete_rq(struct kref *ref)
726{
727 struct ublk_rq_data *data = container_of(ref, struct ublk_rq_data,
728 ref);
729 struct request *req = blk_mq_rq_from_pdu(data);
730
731 __ublk_complete_rq(req);
732}
733
71f28f31 734/*
bb241747
Z
735 * Since __ublk_rq_task_work always fails requests immediately during
736 * exiting, __ublk_fail_req() is only called from abort context during
737 * exiting. So lock is unnecessary.
71f28f31
ML
738 *
739 * Also aborting may not be started yet, keep in mind that one failed
740 * request may be issued by block layer again.
741 */
a0d41dc1
Z
742static void __ublk_fail_req(struct ublk_queue *ubq, struct ublk_io *io,
743 struct request *req)
71f28f31
ML
744{
745 WARN_ON_ONCE(io->flags & UBLK_IO_FLAG_ACTIVE);
746
747 if (!(io->flags & UBLK_IO_FLAG_ABORTED)) {
748 io->flags |= UBLK_IO_FLAG_ABORTED;
a0d41dc1
Z
749 if (ublk_queue_can_use_recovery_reissue(ubq))
750 blk_mq_requeue_request(req, false);
751 else
82840669 752 ublk_put_req_ref(ubq, req);
71f28f31
ML
753 }
754}
755
9d2789ac
JA
756static void ubq_complete_io_cmd(struct ublk_io *io, int res,
757 unsigned issue_flags)
c86019ff
Z
758{
759 /* mark this cmd owned by ublksrv */
760 io->flags |= UBLK_IO_FLAG_OWNED_BY_SRV;
761
762 /*
763 * clear ACTIVE since we are done with this sqe/cmd slot
764 * We can only accept io cmd in case of being not active.
765 */
766 io->flags &= ~UBLK_IO_FLAG_ACTIVE;
767
768 /* tell ublksrv one io request is coming */
9d2789ac 769 io_uring_cmd_done(io->cmd, res, 0, issue_flags);
c86019ff
Z
770}
771
71f28f31
ML
772#define UBLK_REQUEUE_DELAY_MS 3
773
42cf5fc5
Z
774static inline void __ublk_abort_rq(struct ublk_queue *ubq,
775 struct request *rq)
776{
777 /* We cannot process this rq so just requeue it. */
778 if (ublk_queue_can_use_recovery(ubq))
779 blk_mq_requeue_request(rq, false);
780 else
781 blk_mq_end_request(rq, BLK_STS_IOERR);
782
783 mod_delayed_work(system_wq, &ubq->dev->monitor_work, 0);
784}
785
9d2789ac
JA
786static inline void __ublk_rq_task_work(struct request *req,
787 unsigned issue_flags)
71f28f31 788{
71f28f31
ML
789 struct ublk_queue *ubq = req->mq_hctx->driver_data;
790 int tag = req->tag;
791 struct ublk_io *io = &ubq->ios[tag];
71f28f31
ML
792 unsigned int mapped_bytes;
793
794 pr_devel("%s: complete: op %d, qid %d tag %d io_flags %x addr %llx\n",
795 __func__, io->cmd->cmd_op, ubq->q_id, req->tag, io->flags,
796 ublk_get_iod(ubq, req->tag)->addr);
797
ae3f7193
Z
798 /*
799 * Task is exiting if either:
800 *
801 * (1) current != ubq_daemon.
802 * io_uring_cmd_complete_in_task() tries to run task_work
803 * in a workqueue if ubq_daemon(cmd's task) is PF_EXITING.
804 *
805 * (2) current->flags & PF_EXITING.
806 */
807 if (unlikely(current != ubq->ubq_daemon || current->flags & PF_EXITING)) {
42cf5fc5 808 __ublk_abort_rq(ubq, req);
71f28f31
ML
809 return;
810 }
811
2f3af723 812 if (ublk_need_get_data(ubq) && ublk_need_map_req(req)) {
c86019ff
Z
813 /*
814 * We have not handled UBLK_IO_NEED_GET_DATA command yet,
815 * so immepdately pass UBLK_IO_RES_NEED_GET_DATA to ublksrv
816 * and notify it.
817 */
818 if (!(io->flags & UBLK_IO_FLAG_NEED_GET_DATA)) {
819 io->flags |= UBLK_IO_FLAG_NEED_GET_DATA;
820 pr_devel("%s: need get data. op %d, qid %d tag %d io_flags %x\n",
821 __func__, io->cmd->cmd_op, ubq->q_id,
822 req->tag, io->flags);
9d2789ac 823 ubq_complete_io_cmd(io, UBLK_IO_RES_NEED_GET_DATA, issue_flags);
c86019ff
Z
824 return;
825 }
826 /*
827 * We have handled UBLK_IO_NEED_GET_DATA command,
828 * so clear UBLK_IO_FLAG_NEED_GET_DATA now and just
829 * do the copy work.
830 */
831 io->flags &= ~UBLK_IO_FLAG_NEED_GET_DATA;
92cb6e2e
Z
832 /* update iod->addr because ublksrv may have passed a new io buffer */
833 ublk_get_iod(ubq, req->tag)->addr = io->addr;
834 pr_devel("%s: update iod->addr: op %d, qid %d tag %d io_flags %x addr %llx\n",
835 __func__, io->cmd->cmd_op, ubq->q_id, req->tag, io->flags,
836 ublk_get_iod(ubq, req->tag)->addr);
c86019ff
Z
837 }
838
71f28f31
ML
839 mapped_bytes = ublk_map_io(ubq, req, io);
840
841 /* partially mapped, update io descriptor */
842 if (unlikely(mapped_bytes != blk_rq_bytes(req))) {
843 /*
844 * Nothing mapped, retry until we succeed.
845 *
846 * We may never succeed in mapping any bytes here because
847 * of OOM. TODO: reserve one buffer with single page pinned
848 * for providing forward progress guarantee.
849 */
850 if (unlikely(!mapped_bytes)) {
851 blk_mq_requeue_request(req, false);
852 blk_mq_delay_kick_requeue_list(req->q,
853 UBLK_REQUEUE_DELAY_MS);
854 return;
855 }
856
857 ublk_get_iod(ubq, req->tag)->nr_sectors =
858 mapped_bytes >> 9;
859 }
860
82840669 861 ublk_init_req_ref(ubq, req);
9d2789ac 862 ubq_complete_io_cmd(io, UBLK_IO_RES_OK, issue_flags);
71f28f31
ML
863}
864
9d2789ac
JA
865static inline void ublk_forward_io_cmds(struct ublk_queue *ubq,
866 unsigned issue_flags)
7d4a9317
ML
867{
868 struct llist_node *io_cmds = llist_del_all(&ubq->io_cmds);
869 struct ublk_rq_data *data, *tmp;
870
871 io_cmds = llist_reverse_order(io_cmds);
872 llist_for_each_entry_safe(data, tmp, io_cmds, node)
9d2789ac 873 __ublk_rq_task_work(blk_mq_rq_from_pdu(data), issue_flags);
7d4a9317
ML
874}
875
876static inline void ublk_abort_io_cmds(struct ublk_queue *ubq)
877{
878 struct llist_node *io_cmds = llist_del_all(&ubq->io_cmds);
879 struct ublk_rq_data *data, *tmp;
880
881 llist_for_each_entry_safe(data, tmp, io_cmds, node)
882 __ublk_abort_rq(ubq, blk_mq_rq_from_pdu(data));
883}
884
9d2789ac 885static void ublk_rq_task_work_cb(struct io_uring_cmd *cmd, unsigned issue_flags)
0edb3696
ML
886{
887 struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
3ab6e94c 888 struct ublk_queue *ubq = pdu->ubq;
0edb3696 889
9d2789ac 890 ublk_forward_io_cmds(ubq, issue_flags);
0edb3696
ML
891}
892
7d4a9317 893static void ublk_queue_cmd(struct ublk_queue *ubq, struct request *rq)
3ab6e94c 894{
7d4a9317
ML
895 struct ublk_rq_data *data = blk_mq_rq_to_pdu(rq);
896 struct ublk_io *io;
3ab6e94c 897
7d4a9317
ML
898 if (!llist_add(&data->node, &ubq->io_cmds))
899 return;
900
901 io = &ubq->ios[rq->tag];
3ab6e94c
ML
902 /*
903 * If the check pass, we know that this is a re-issued request aborted
904 * previously in monitor_work because the ubq_daemon(cmd's task) is
905 * PF_EXITING. We cannot call io_uring_cmd_complete_in_task() anymore
906 * because this ioucmd's io_uring context may be freed now if no inflight
907 * ioucmd exists. Otherwise we may cause null-deref in ctx->fallback_work.
908 *
909 * Note: monitor_work sets UBLK_IO_FLAG_ABORTED and ends this request(releasing
910 * the tag). Then the request is re-started(allocating the tag) and we are here.
911 * Since releasing/allocating a tag implies smp_mb(), finding UBLK_IO_FLAG_ABORTED
912 * guarantees that here is a re-issued request aborted previously.
913 */
914 if (unlikely(io->flags & UBLK_IO_FLAG_ABORTED)) {
7d4a9317 915 ublk_abort_io_cmds(ubq);
3ab6e94c
ML
916 } else {
917 struct io_uring_cmd *cmd = io->cmd;
918 struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
919
920 pdu->ubq = ubq;
921 io_uring_cmd_complete_in_task(cmd, ublk_rq_task_work_cb);
922 }
923}
924
c0b79b0f
ML
925static enum blk_eh_timer_return ublk_timeout(struct request *rq)
926{
927 struct ublk_queue *ubq = rq->mq_hctx->driver_data;
928
929 if (ubq->flags & UBLK_F_UNPRIVILEGED_DEV) {
930 if (!ubq->timeout) {
931 send_sig(SIGKILL, ubq->ubq_daemon, 0);
932 ubq->timeout = true;
933 }
934
935 return BLK_EH_DONE;
936 }
937
938 return BLK_EH_RESET_TIMER;
939}
940
71f28f31
ML
941static blk_status_t ublk_queue_rq(struct blk_mq_hw_ctx *hctx,
942 const struct blk_mq_queue_data *bd)
943{
944 struct ublk_queue *ubq = hctx->driver_data;
945 struct request *rq = bd->rq;
71f28f31
ML
946 blk_status_t res;
947
948 /* fill iod to slot in io cmd buffer */
949 res = ublk_setup_iod(ubq, rq);
950 if (unlikely(res != BLK_STS_OK))
951 return BLK_STS_IOERR;
3ab6e94c 952
bbae8d1f
Z
953 /* With recovery feature enabled, force_abort is set in
954 * ublk_stop_dev() before calling del_gendisk(). We have to
955 * abort all requeued and new rqs here to let del_gendisk()
956 * move on. Besides, we cannot not call io_uring_cmd_complete_in_task()
957 * to avoid UAF on io_uring ctx.
958 *
959 * Note: force_abort is guaranteed to be seen because it is set
960 * before request queue is unqiuesced.
961 */
962 if (ublk_queue_can_use_recovery(ubq) && unlikely(ubq->force_abort))
963 return BLK_STS_IOERR;
71f28f31
ML
964
965 blk_mq_start_request(bd->rq);
966
967 if (unlikely(ubq_daemon_is_dying(ubq))) {
42cf5fc5
Z
968 __ublk_abort_rq(ubq, rq);
969 return BLK_STS_OK;
71f28f31
ML
970 }
971
7d4a9317 972 ublk_queue_cmd(ubq, rq);
0edb3696 973
71f28f31
ML
974 return BLK_STS_OK;
975}
976
71f28f31
ML
977static int ublk_init_hctx(struct blk_mq_hw_ctx *hctx, void *driver_data,
978 unsigned int hctx_idx)
979{
cebbe577 980 struct ublk_device *ub = driver_data;
71f28f31
ML
981 struct ublk_queue *ubq = ublk_get_queue(ub, hctx->queue_num);
982
983 hctx->driver_data = ubq;
984 return 0;
985}
986
987static const struct blk_mq_ops ublk_mq_ops = {
988 .queue_rq = ublk_queue_rq,
989 .init_hctx = ublk_init_hctx,
c0b79b0f 990 .timeout = ublk_timeout,
71f28f31
ML
991};
992
993static int ublk_ch_open(struct inode *inode, struct file *filp)
994{
995 struct ublk_device *ub = container_of(inode->i_cdev,
996 struct ublk_device, cdev);
997
fa362045
CH
998 if (test_and_set_bit(UB_STATE_OPEN, &ub->state))
999 return -EBUSY;
1000 filp->private_data = ub;
1001 return 0;
71f28f31
ML
1002}
1003
1004static int ublk_ch_release(struct inode *inode, struct file *filp)
1005{
1006 struct ublk_device *ub = filp->private_data;
1007
fa362045 1008 clear_bit(UB_STATE_OPEN, &ub->state);
71f28f31
ML
1009 return 0;
1010}
1011
1012/* map pre-allocated per-queue cmd buffer to ublksrv daemon */
1013static int ublk_ch_mmap(struct file *filp, struct vm_area_struct *vma)
1014{
1015 struct ublk_device *ub = filp->private_data;
1016 size_t sz = vma->vm_end - vma->vm_start;
1017 unsigned max_sz = UBLK_MAX_QUEUE_DEPTH * sizeof(struct ublksrv_io_desc);
1018 unsigned long pfn, end, phys_off = vma->vm_pgoff << PAGE_SHIFT;
1019 int q_id, ret = 0;
1020
e94eb459 1021 spin_lock(&ub->mm_lock);
71f28f31
ML
1022 if (!ub->mm)
1023 ub->mm = current->mm;
1024 if (current->mm != ub->mm)
1025 ret = -EINVAL;
e94eb459 1026 spin_unlock(&ub->mm_lock);
71f28f31
ML
1027
1028 if (ret)
1029 return ret;
1030
1031 if (vma->vm_flags & VM_WRITE)
1032 return -EPERM;
1033
1034 end = UBLKSRV_CMD_BUF_OFFSET + ub->dev_info.nr_hw_queues * max_sz;
1035 if (phys_off < UBLKSRV_CMD_BUF_OFFSET || phys_off >= end)
1036 return -EINVAL;
1037
1038 q_id = (phys_off - UBLKSRV_CMD_BUF_OFFSET) / max_sz;
1039 pr_devel("%s: qid %d, pid %d, addr %lx pg_off %lx sz %lu\n",
1040 __func__, q_id, current->pid, vma->vm_start,
1041 phys_off, (unsigned long)sz);
1042
1043 if (sz != ublk_queue_cmd_buf_size(ub, q_id))
1044 return -EINVAL;
1045
1046 pfn = virt_to_phys(ublk_queue_cmd_buf(ub, q_id)) >> PAGE_SHIFT;
1047 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
1048}
1049
1050static void ublk_commit_completion(struct ublk_device *ub,
fd9b8547 1051 const struct ublksrv_io_cmd *ub_cmd)
71f28f31
ML
1052{
1053 u32 qid = ub_cmd->q_id, tag = ub_cmd->tag;
1054 struct ublk_queue *ubq = ublk_get_queue(ub, qid);
1055 struct ublk_io *io = &ubq->ios[tag];
1056 struct request *req;
1057
1058 /* now this cmd slot is owned by nbd driver */
1059 io->flags &= ~UBLK_IO_FLAG_OWNED_BY_SRV;
1060 io->res = ub_cmd->result;
1061
1062 /* find the io request and complete */
1063 req = blk_mq_tag_to_rq(ub->tag_set.tags[qid], tag);
1064
1065 if (req && likely(!blk_should_fake_timeout(req->q)))
82840669 1066 ublk_put_req_ref(ubq, req);
71f28f31
ML
1067}
1068
1069/*
1070 * When ->ubq_daemon is exiting, either new request is ended immediately,
1071 * or any queued io command is drained, so it is safe to abort queue
1072 * lockless
1073 */
1074static void ublk_abort_queue(struct ublk_device *ub, struct ublk_queue *ubq)
1075{
1076 int i;
1077
1078 if (!ublk_get_device(ub))
1079 return;
1080
1081 for (i = 0; i < ubq->q_depth; i++) {
1082 struct ublk_io *io = &ubq->ios[i];
1083
1084 if (!(io->flags & UBLK_IO_FLAG_ACTIVE)) {
1085 struct request *rq;
1086
1087 /*
1088 * Either we fail the request or ublk_rq_task_work_fn
1089 * will do it
1090 */
1091 rq = blk_mq_tag_to_rq(ub->tag_set.tags[ubq->q_id], i);
1092 if (rq)
a0d41dc1 1093 __ublk_fail_req(ubq, io, rq);
71f28f31
ML
1094 }
1095 }
1096 ublk_put_device(ub);
1097}
1098
1099static void ublk_daemon_monitor_work(struct work_struct *work)
1100{
1101 struct ublk_device *ub =
1102 container_of(work, struct ublk_device, monitor_work.work);
1103 int i;
1104
1105 for (i = 0; i < ub->dev_info.nr_hw_queues; i++) {
1106 struct ublk_queue *ubq = ublk_get_queue(ub, i);
1107
1108 if (ubq_daemon_is_dying(ubq)) {
bbae8d1f
Z
1109 if (ublk_queue_can_use_recovery(ubq))
1110 schedule_work(&ub->quiesce_work);
1111 else
1112 schedule_work(&ub->stop_work);
71f28f31
ML
1113
1114 /* abort queue is for making forward progress */
1115 ublk_abort_queue(ub, ubq);
1116 }
1117 }
1118
1119 /*
bbae8d1f
Z
1120 * We can't schedule monitor work after ub's state is not UBLK_S_DEV_LIVE.
1121 * after ublk_remove() or __ublk_quiesce_dev() is started.
71f28f31
ML
1122 *
1123 * No need ub->mutex, monitor work are canceled after state is marked
bbae8d1f 1124 * as not LIVE, so new state is observed reliably.
71f28f31 1125 */
bbae8d1f 1126 if (ub->dev_info.state == UBLK_S_DEV_LIVE)
71f28f31
ML
1127 schedule_delayed_work(&ub->monitor_work,
1128 UBLK_DAEMON_MONITOR_PERIOD);
1129}
1130
a8ce5f52
ML
1131static inline bool ublk_queue_ready(struct ublk_queue *ubq)
1132{
1133 return ubq->nr_io_ready == ubq->q_depth;
1134}
1135
71f28f31
ML
1136static void ublk_cancel_queue(struct ublk_queue *ubq)
1137{
1138 int i;
1139
a8ce5f52
ML
1140 if (!ublk_queue_ready(ubq))
1141 return;
1142
71f28f31
ML
1143 for (i = 0; i < ubq->q_depth; i++) {
1144 struct ublk_io *io = &ubq->ios[i];
1145
1146 if (io->flags & UBLK_IO_FLAG_ACTIVE)
9d2789ac
JA
1147 io_uring_cmd_done(io->cmd, UBLK_IO_RES_ABORT, 0,
1148 IO_URING_F_UNLOCKED);
71f28f31 1149 }
a8ce5f52
ML
1150
1151 /* all io commands are canceled */
1152 ubq->nr_io_ready = 0;
71f28f31
ML
1153}
1154
1155/* Cancel all pending commands, must be called after del_gendisk() returns */
1156static void ublk_cancel_dev(struct ublk_device *ub)
1157{
1158 int i;
1159
1160 for (i = 0; i < ub->dev_info.nr_hw_queues; i++)
1161 ublk_cancel_queue(ublk_get_queue(ub, i));
1162}
1163
bbae8d1f
Z
1164static bool ublk_check_inflight_rq(struct request *rq, void *data)
1165{
1166 bool *idle = data;
1167
1168 if (blk_mq_request_started(rq)) {
1169 *idle = false;
1170 return false;
1171 }
1172 return true;
1173}
1174
1175static void ublk_wait_tagset_rqs_idle(struct ublk_device *ub)
1176{
1177 bool idle;
1178
1179 WARN_ON_ONCE(!blk_queue_quiesced(ub->ub_disk->queue));
1180 while (true) {
1181 idle = true;
1182 blk_mq_tagset_busy_iter(&ub->tag_set,
1183 ublk_check_inflight_rq, &idle);
1184 if (idle)
1185 break;
1186 msleep(UBLK_REQUEUE_DELAY_MS);
1187 }
1188}
1189
1190static void __ublk_quiesce_dev(struct ublk_device *ub)
71f28f31 1191{
bbae8d1f
Z
1192 pr_devel("%s: quiesce ub: dev_id %d state %s\n",
1193 __func__, ub->dev_info.dev_id,
1194 ub->dev_info.state == UBLK_S_DEV_LIVE ?
1195 "LIVE" : "QUIESCED");
1196 blk_mq_quiesce_queue(ub->ub_disk->queue);
1197 ublk_wait_tagset_rqs_idle(ub);
1198 ub->dev_info.state = UBLK_S_DEV_QUIESCED;
1199 ublk_cancel_dev(ub);
1200 /* we are going to release task_struct of ubq_daemon and resets
1201 * ->ubq_daemon to NULL. So in monitor_work, check on ubq_daemon causes UAF.
1202 * Besides, monitor_work is not necessary in QUIESCED state since we have
1203 * already scheduled quiesce_work and quiesced all ubqs.
1204 *
1205 * Do not let monitor_work schedule itself if state it QUIESCED. And we cancel
1206 * it here and re-schedule it in END_USER_RECOVERY to avoid UAF.
1207 */
1208 cancel_delayed_work_sync(&ub->monitor_work);
1209}
1210
1211static void ublk_quiesce_work_fn(struct work_struct *work)
1212{
1213 struct ublk_device *ub =
1214 container_of(work, struct ublk_device, quiesce_work);
1215
71f28f31 1216 mutex_lock(&ub->mutex);
6d9e6dfd 1217 if (ub->dev_info.state != UBLK_S_DEV_LIVE)
71f28f31 1218 goto unlock;
bbae8d1f
Z
1219 __ublk_quiesce_dev(ub);
1220 unlock:
1221 mutex_unlock(&ub->mutex);
1222}
71f28f31 1223
bbae8d1f
Z
1224static void ublk_unquiesce_dev(struct ublk_device *ub)
1225{
1226 int i;
1227
1228 pr_devel("%s: unquiesce ub: dev_id %d state %s\n",
1229 __func__, ub->dev_info.dev_id,
1230 ub->dev_info.state == UBLK_S_DEV_LIVE ?
1231 "LIVE" : "QUIESCED");
1232 /* quiesce_work has run. We let requeued rqs be aborted
1233 * before running fallback_wq. "force_abort" must be seen
1234 * after request queue is unqiuesced. Then del_gendisk()
1235 * can move on.
1236 */
1237 for (i = 0; i < ub->dev_info.nr_hw_queues; i++)
1238 ublk_get_queue(ub, i)->force_abort = true;
1239
1240 blk_mq_unquiesce_queue(ub->ub_disk->queue);
1241 /* We may have requeued some rqs in ublk_quiesce_queue() */
1242 blk_mq_kick_requeue_list(ub->ub_disk->queue);
1243}
1244
1245static void ublk_stop_dev(struct ublk_device *ub)
1246{
1247 mutex_lock(&ub->mutex);
1248 if (ub->dev_info.state == UBLK_S_DEV_DEAD)
1249 goto unlock;
1250 if (ublk_can_use_recovery(ub)) {
1251 if (ub->dev_info.state == UBLK_S_DEV_LIVE)
1252 __ublk_quiesce_dev(ub);
1253 ublk_unquiesce_dev(ub);
1254 }
71f28f31
ML
1255 del_gendisk(ub->ub_disk);
1256 ub->dev_info.state = UBLK_S_DEV_DEAD;
1257 ub->dev_info.ublksrv_pid = -1;
6d9e6dfd
CH
1258 put_disk(ub->ub_disk);
1259 ub->ub_disk = NULL;
71f28f31 1260 unlock:
a8ce5f52 1261 ublk_cancel_dev(ub);
71f28f31
ML
1262 mutex_unlock(&ub->mutex);
1263 cancel_delayed_work_sync(&ub->monitor_work);
1264}
1265
71f28f31
ML
1266/* device can only be started after all IOs are ready */
1267static void ublk_mark_io_ready(struct ublk_device *ub, struct ublk_queue *ubq)
1268{
1269 mutex_lock(&ub->mutex);
1270 ubq->nr_io_ready++;
1271 if (ublk_queue_ready(ubq)) {
1272 ubq->ubq_daemon = current;
1273 get_task_struct(ubq->ubq_daemon);
1274 ub->nr_queues_ready++;
73a166d9
ML
1275
1276 if (capable(CAP_SYS_ADMIN))
1277 ub->nr_privileged_daemon++;
71f28f31
ML
1278 }
1279 if (ub->nr_queues_ready == ub->dev_info.nr_hw_queues)
1280 complete_all(&ub->completion);
1281 mutex_unlock(&ub->mutex);
1282}
1283
c86019ff 1284static void ublk_handle_need_get_data(struct ublk_device *ub, int q_id,
fee32f31 1285 int tag)
c86019ff
Z
1286{
1287 struct ublk_queue *ubq = ublk_get_queue(ub, q_id);
1288 struct request *req = blk_mq_tag_to_rq(ub->tag_set.tags[q_id], tag);
1289
7d4a9317 1290 ublk_queue_cmd(ubq, req);
c86019ff
Z
1291}
1292
2d786e66
ML
1293static inline int ublk_check_cmd_op(u32 cmd_op)
1294{
1295 u32 ioc_type = _IOC_TYPE(cmd_op);
1296
e485bd9e 1297 if (!IS_ENABLED(CONFIG_BLKDEV_UBLK_LEGACY_OPCODES) && ioc_type != 'u')
2d786e66
ML
1298 return -EOPNOTSUPP;
1299
1300 if (ioc_type != 'u' && ioc_type != 0)
1301 return -EOPNOTSUPP;
1302
1303 return 0;
1304}
1305
f236a214
ML
1306static inline void ublk_fill_io_cmd(struct ublk_io *io,
1307 struct io_uring_cmd *cmd, unsigned long buf_addr)
1308{
1309 io->cmd = cmd;
1310 io->flags |= UBLK_IO_FLAG_ACTIVE;
1311 io->addr = buf_addr;
1312}
1313
8c68ae3b
JA
1314static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd,
1315 unsigned int issue_flags,
03e5cb7b 1316 const struct ublksrv_io_cmd *ub_cmd)
71f28f31 1317{
71f28f31
ML
1318 struct ublk_device *ub = cmd->file->private_data;
1319 struct ublk_queue *ubq;
1320 struct ublk_io *io;
1321 u32 cmd_op = cmd->cmd_op;
1322 unsigned tag = ub_cmd->tag;
1323 int ret = -EINVAL;
2f1e07dd 1324 struct request *req;
71f28f31
ML
1325
1326 pr_devel("%s: received: cmd op %d queue %d tag %d result %d\n",
1327 __func__, cmd->cmd_op, ub_cmd->q_id, tag,
1328 ub_cmd->result);
1329
71f28f31
ML
1330 if (ub_cmd->q_id >= ub->dev_info.nr_hw_queues)
1331 goto out;
1332
1333 ubq = ublk_get_queue(ub, ub_cmd->q_id);
1334 if (!ubq || ub_cmd->q_id != ubq->q_id)
1335 goto out;
1336
1337 if (ubq->ubq_daemon && ubq->ubq_daemon != current)
1338 goto out;
1339
1340 if (tag >= ubq->q_depth)
1341 goto out;
1342
1343 io = &ubq->ios[tag];
1344
1345 /* there is pending io cmd, something must be wrong */
1346 if (io->flags & UBLK_IO_FLAG_ACTIVE) {
1347 ret = -EBUSY;
1348 goto out;
1349 }
1350
c86019ff
Z
1351 /*
1352 * ensure that the user issues UBLK_IO_NEED_GET_DATA
1353 * iff the driver have set the UBLK_IO_FLAG_NEED_GET_DATA.
1354 */
1355 if ((!!(io->flags & UBLK_IO_FLAG_NEED_GET_DATA))
2d786e66
ML
1356 ^ (_IOC_NR(cmd_op) == UBLK_IO_NEED_GET_DATA))
1357 goto out;
1358
1359 ret = ublk_check_cmd_op(cmd_op);
1360 if (ret)
c86019ff
Z
1361 goto out;
1362
7c75661c 1363 ret = -EINVAL;
2d786e66 1364 switch (_IOC_NR(cmd_op)) {
71f28f31
ML
1365 case UBLK_IO_FETCH_REQ:
1366 /* UBLK_IO_FETCH_REQ is only allowed before queue is setup */
1367 if (ublk_queue_ready(ubq)) {
1368 ret = -EBUSY;
1369 goto out;
1370 }
1371 /*
1372 * The io is being handled by server, so COMMIT_RQ is expected
1373 * instead of FETCH_REQ
1374 */
1375 if (io->flags & UBLK_IO_FLAG_OWNED_BY_SRV)
1376 goto out;
2f1e07dd
LX
1377 /* FETCH_RQ has to provide IO buffer if NEED GET DATA is not enabled */
1378 if (!ub_cmd->addr && !ublk_need_get_data(ubq))
71f28f31 1379 goto out;
71f28f31 1380
f236a214 1381 ublk_fill_io_cmd(io, cmd, ub_cmd->addr);
71f28f31
ML
1382 ublk_mark_io_ready(ub, ubq);
1383 break;
1384 case UBLK_IO_COMMIT_AND_FETCH_REQ:
2f1e07dd
LX
1385 req = blk_mq_tag_to_rq(ub->tag_set.tags[ub_cmd->q_id], tag);
1386 /*
1387 * COMMIT_AND_FETCH_REQ has to provide IO buffer if NEED GET DATA is
1388 * not enabled or it is Read IO.
1389 */
1390 if (!ub_cmd->addr && (!ublk_need_get_data(ubq) || req_op(req) == REQ_OP_READ))
71f28f31
ML
1391 goto out;
1392 if (!(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV))
1393 goto out;
f236a214 1394 ublk_fill_io_cmd(io, cmd, ub_cmd->addr);
71f28f31
ML
1395 ublk_commit_completion(ub, ub_cmd);
1396 break;
c86019ff
Z
1397 case UBLK_IO_NEED_GET_DATA:
1398 if (!(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV))
1399 goto out;
f236a214 1400 ublk_fill_io_cmd(io, cmd, ub_cmd->addr);
fee32f31 1401 ublk_handle_need_get_data(ub, ub_cmd->q_id, ub_cmd->tag);
c86019ff 1402 break;
71f28f31
ML
1403 default:
1404 goto out;
1405 }
1406 return -EIOCBQUEUED;
1407
1408 out:
9d2789ac 1409 io_uring_cmd_done(cmd, ret, 0, issue_flags);
71f28f31
ML
1410 pr_devel("%s: complete: cmd op %d, tag %d ret %x io_flags %x\n",
1411 __func__, cmd_op, tag, ret, io->flags);
1412 return -EIOCBQUEUED;
1413}
1414
8c68ae3b
JA
1415static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
1416{
8c68ae3b
JA
1417 /*
1418 * Not necessary for async retry, but let's keep it simple and always
1419 * copy the values to avoid any potential reuse.
1420 */
03e5cb7b
LT
1421 const struct ublksrv_io_cmd *ub_src = io_uring_sqe_cmd(cmd->sqe);
1422 const struct ublksrv_io_cmd ub_cmd = {
1423 .q_id = READ_ONCE(ub_src->q_id),
1424 .tag = READ_ONCE(ub_src->tag),
1425 .result = READ_ONCE(ub_src->result),
1426 .addr = READ_ONCE(ub_src->addr)
1427 };
8c68ae3b
JA
1428
1429 return __ublk_ch_uring_cmd(cmd, issue_flags, &ub_cmd);
1430}
1431
71f28f31
ML
1432static const struct file_operations ublk_ch_fops = {
1433 .owner = THIS_MODULE,
1434 .open = ublk_ch_open,
1435 .release = ublk_ch_release,
1436 .llseek = no_llseek,
1437 .uring_cmd = ublk_ch_uring_cmd,
1438 .mmap = ublk_ch_mmap,
1439};
1440
1441static void ublk_deinit_queue(struct ublk_device *ub, int q_id)
1442{
1443 int size = ublk_queue_cmd_buf_size(ub, q_id);
1444 struct ublk_queue *ubq = ublk_get_queue(ub, q_id);
1445
1446 if (ubq->ubq_daemon)
1447 put_task_struct(ubq->ubq_daemon);
1448 if (ubq->io_cmd_buf)
1449 free_pages((unsigned long)ubq->io_cmd_buf, get_order(size));
1450}
1451
1452static int ublk_init_queue(struct ublk_device *ub, int q_id)
1453{
1454 struct ublk_queue *ubq = ublk_get_queue(ub, q_id);
1455 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO;
1456 void *ptr;
1457 int size;
1458
6d8c5afc 1459 ubq->flags = ub->dev_info.flags;
71f28f31
ML
1460 ubq->q_id = q_id;
1461 ubq->q_depth = ub->dev_info.queue_depth;
1462 size = ublk_queue_cmd_buf_size(ub, q_id);
1463
1464 ptr = (void *) __get_free_pages(gfp_flags, get_order(size));
1465 if (!ptr)
1466 return -ENOMEM;
1467
1468 ubq->io_cmd_buf = ptr;
1469 ubq->dev = ub;
1470 return 0;
1471}
1472
1473static void ublk_deinit_queues(struct ublk_device *ub)
1474{
1475 int nr_queues = ub->dev_info.nr_hw_queues;
1476 int i;
1477
1478 if (!ub->__queues)
1479 return;
1480
1481 for (i = 0; i < nr_queues; i++)
1482 ublk_deinit_queue(ub, i);
1483 kfree(ub->__queues);
1484}
1485
1486static int ublk_init_queues(struct ublk_device *ub)
1487{
1488 int nr_queues = ub->dev_info.nr_hw_queues;
1489 int depth = ub->dev_info.queue_depth;
1490 int ubq_size = sizeof(struct ublk_queue) + depth * sizeof(struct ublk_io);
1491 int i, ret = -ENOMEM;
1492
1493 ub->queue_size = ubq_size;
1494 ub->__queues = kcalloc(nr_queues, ubq_size, GFP_KERNEL);
1495 if (!ub->__queues)
1496 return ret;
1497
1498 for (i = 0; i < nr_queues; i++) {
1499 if (ublk_init_queue(ub, i))
1500 goto fail;
1501 }
1502
1503 init_completion(&ub->completion);
1504 return 0;
1505
1506 fail:
1507 ublk_deinit_queues(ub);
1508 return ret;
1509}
1510
fa9482e0 1511static int ublk_alloc_dev_number(struct ublk_device *ub, int idx)
71f28f31
ML
1512{
1513 int i = idx;
1514 int err;
1515
1516 spin_lock(&ublk_idr_lock);
1517 /* allocate id, if @id >= 0, we're requesting that specific id */
1518 if (i >= 0) {
1519 err = idr_alloc(&ublk_index_idr, ub, i, i + 1, GFP_NOWAIT);
1520 if (err == -ENOSPC)
1521 err = -EEXIST;
1522 } else {
1523 err = idr_alloc(&ublk_index_idr, ub, 0, 0, GFP_NOWAIT);
1524 }
1525 spin_unlock(&ublk_idr_lock);
1526
1527 if (err >= 0)
1528 ub->ub_number = err;
1529
1530 return err;
1531}
1532
fa9482e0 1533static void ublk_free_dev_number(struct ublk_device *ub)
71f28f31
ML
1534{
1535 spin_lock(&ublk_idr_lock);
1536 idr_remove(&ublk_index_idr, ub->ub_number);
1537 wake_up_all(&ublk_idr_wq);
1538 spin_unlock(&ublk_idr_lock);
71f28f31
ML
1539}
1540
1541static void ublk_cdev_rel(struct device *dev)
1542{
1543 struct ublk_device *ub = container_of(dev, struct ublk_device, cdev_dev);
1544
71f28f31 1545 blk_mq_free_tag_set(&ub->tag_set);
71f28f31 1546 ublk_deinit_queues(ub);
fa9482e0
CH
1547 ublk_free_dev_number(ub);
1548 mutex_destroy(&ub->mutex);
1549 kfree(ub);
71f28f31
ML
1550}
1551
1552static int ublk_add_chdev(struct ublk_device *ub)
1553{
1554 struct device *dev = &ub->cdev_dev;
1555 int minor = ub->ub_number;
1556 int ret;
1557
1558 dev->parent = ublk_misc.this_device;
1559 dev->devt = MKDEV(MAJOR(ublk_chr_devt), minor);
1560 dev->class = ublk_chr_class;
1561 dev->release = ublk_cdev_rel;
1562 device_initialize(dev);
1563
1564 ret = dev_set_name(dev, "ublkc%d", minor);
1565 if (ret)
1566 goto fail;
1567
1568 cdev_init(&ub->cdev, &ublk_ch_fops);
1569 ret = cdev_device_add(&ub->cdev, dev);
1570 if (ret)
1571 goto fail;
403ebc87
ML
1572
1573 ublks_added++;
71f28f31
ML
1574 return 0;
1575 fail:
1576 put_device(dev);
1577 return ret;
1578}
1579
1580static void ublk_stop_work_fn(struct work_struct *work)
1581{
1582 struct ublk_device *ub =
1583 container_of(work, struct ublk_device, stop_work);
1584
1585 ublk_stop_dev(ub);
1586}
1587
4bf9cbf3 1588/* align max io buffer size with PAGE_SIZE */
6d9e6dfd 1589static void ublk_align_max_io_size(struct ublk_device *ub)
71f28f31 1590{
4bf9cbf3 1591 unsigned int max_io_bytes = ub->dev_info.max_io_buf_bytes;
71f28f31 1592
4bf9cbf3
ML
1593 ub->dev_info.max_io_buf_bytes =
1594 round_down(max_io_bytes, PAGE_SIZE);
71f28f31
ML
1595}
1596
fa9482e0 1597static int ublk_add_tag_set(struct ublk_device *ub)
71f28f31 1598{
71f28f31
ML
1599 ub->tag_set.ops = &ublk_mq_ops;
1600 ub->tag_set.nr_hw_queues = ub->dev_info.nr_hw_queues;
1601 ub->tag_set.queue_depth = ub->dev_info.queue_depth;
1602 ub->tag_set.numa_node = NUMA_NO_NODE;
0edb3696 1603 ub->tag_set.cmd_size = sizeof(struct ublk_rq_data);
71f28f31
ML
1604 ub->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1605 ub->tag_set.driver_data = ub;
fa9482e0 1606 return blk_mq_alloc_tag_set(&ub->tag_set);
71f28f31
ML
1607}
1608
1609static void ublk_remove(struct ublk_device *ub)
1610{
34d8f2be
CH
1611 ublk_stop_dev(ub);
1612 cancel_work_sync(&ub->stop_work);
bbae8d1f 1613 cancel_work_sync(&ub->quiesce_work);
71f28f31
ML
1614 cdev_device_del(&ub->cdev, &ub->cdev_dev);
1615 put_device(&ub->cdev_dev);
403ebc87 1616 ublks_added--;
71f28f31
ML
1617}
1618
1619static struct ublk_device *ublk_get_device_from_id(int idx)
1620{
1621 struct ublk_device *ub = NULL;
1622
1623 if (idx < 0)
1624 return NULL;
1625
1626 spin_lock(&ublk_idr_lock);
1627 ub = idr_find(&ublk_index_idr, idx);
1628 if (ub)
1629 ub = ublk_get_device(ub);
1630 spin_unlock(&ublk_idr_lock);
1631
1632 return ub;
1633}
1634
bfbcef03 1635static int ublk_ctrl_start_dev(struct ublk_device *ub, struct io_uring_cmd *cmd)
71f28f31 1636{
fd9b8547 1637 const struct ublksrv_ctrl_cmd *header = io_uring_sqe_cmd(cmd->sqe);
71f28f31 1638 int ublksrv_pid = (int)header->data[0];
6d9e6dfd 1639 struct gendisk *disk;
34d8f2be 1640 int ret = -EINVAL;
71f28f31
ML
1641
1642 if (ublksrv_pid <= 0)
34d8f2be
CH
1643 return -EINVAL;
1644
71f28f31
ML
1645 wait_for_completion_interruptible(&ub->completion);
1646
1647 schedule_delayed_work(&ub->monitor_work, UBLK_DAEMON_MONITOR_PERIOD);
1648
1649 mutex_lock(&ub->mutex);
6d9e6dfd
CH
1650 if (ub->dev_info.state == UBLK_S_DEV_LIVE ||
1651 test_bit(UB_STATE_USED, &ub->state)) {
71f28f31 1652 ret = -EEXIST;
34d8f2be 1653 goto out_unlock;
71f28f31 1654 }
71f28f31 1655
1972d038 1656 disk = blk_mq_alloc_disk(&ub->tag_set, NULL);
6d9e6dfd
CH
1657 if (IS_ERR(disk)) {
1658 ret = PTR_ERR(disk);
1659 goto out_unlock;
34d8f2be 1660 }
6d9e6dfd
CH
1661 sprintf(disk->disk_name, "ublkb%d", ub->ub_number);
1662 disk->fops = &ub_fops;
1663 disk->private_data = ub;
1664
34d8f2be 1665 ub->dev_info.ublksrv_pid = ublksrv_pid;
6d9e6dfd 1666 ub->ub_disk = disk;
0aa73170
ML
1667
1668 ret = ublk_apply_params(ub);
1669 if (ret)
1670 goto out_put_disk;
1671
73a166d9
ML
1672 /* don't probe partitions if any one ubq daemon is un-trusted */
1673 if (ub->nr_privileged_daemon != ub->nr_queues_ready)
1674 set_bit(GD_SUPPRESS_PART_SCAN, &disk->state);
1675
6d9e6dfd 1676 get_device(&ub->cdev_dev);
4985e7b2 1677 ub->dev_info.state = UBLK_S_DEV_LIVE;
6d9e6dfd
CH
1678 ret = add_disk(disk);
1679 if (ret) {
93d71ec8
ML
1680 /*
1681 * Has to drop the reference since ->free_disk won't be
1682 * called in case of add_disk failure.
1683 */
4985e7b2 1684 ub->dev_info.state = UBLK_S_DEV_DEAD;
93d71ec8 1685 ublk_put_device(ub);
0aa73170 1686 goto out_put_disk;
6d9e6dfd
CH
1687 }
1688 set_bit(UB_STATE_USED, &ub->state);
0aa73170
ML
1689out_put_disk:
1690 if (ret)
1691 put_disk(disk);
34d8f2be
CH
1692out_unlock:
1693 mutex_unlock(&ub->mutex);
71f28f31
ML
1694 return ret;
1695}
1696
bfbcef03
ML
1697static int ublk_ctrl_get_queue_affinity(struct ublk_device *ub,
1698 struct io_uring_cmd *cmd)
71f28f31 1699{
fd9b8547 1700 const struct ublksrv_ctrl_cmd *header = io_uring_sqe_cmd(cmd->sqe);
71f28f31 1701 void __user *argp = (void __user *)(unsigned long)header->addr;
c50061f0 1702 cpumask_var_t cpumask;
71f28f31
ML
1703 unsigned long queue;
1704 unsigned int retlen;
c50061f0 1705 unsigned int i;
bfbcef03
ML
1706 int ret;
1707
34d8f2be
CH
1708 if (header->len * BITS_PER_BYTE < nr_cpu_ids)
1709 return -EINVAL;
1710 if (header->len & (sizeof(unsigned long)-1))
1711 return -EINVAL;
1712 if (!header->addr)
1713 return -EINVAL;
71f28f31 1714
71f28f31
ML
1715 queue = header->data[0];
1716 if (queue >= ub->dev_info.nr_hw_queues)
bfbcef03 1717 return -EINVAL;
71f28f31 1718
c50061f0 1719 if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL))
bfbcef03 1720 return -ENOMEM;
c50061f0
CH
1721
1722 for_each_possible_cpu(i) {
1723 if (ub->tag_set.map[HCTX_TYPE_DEFAULT].mq_map[i] == queue)
1724 cpumask_set_cpu(i, cpumask);
71f28f31 1725 }
c50061f0
CH
1726
1727 ret = -EFAULT;
1728 retlen = min_t(unsigned short, header->len, cpumask_size());
1729 if (copy_to_user(argp, cpumask, retlen))
1730 goto out_free_cpumask;
1731 if (retlen != header->len &&
1732 clear_user(argp + retlen, header->len - retlen))
1733 goto out_free_cpumask;
1734
71f28f31 1735 ret = 0;
c50061f0
CH
1736out_free_cpumask:
1737 free_cpumask_var(cpumask);
71f28f31
ML
1738 return ret;
1739}
1740
34d8f2be 1741static inline void ublk_dump_dev_info(struct ublksrv_ctrl_dev_info *info)
71f28f31 1742{
34d8f2be 1743 pr_devel("%s: dev id %d flags %llx\n", __func__,
6d8c5afc 1744 info->dev_id, info->flags);
4bf9cbf3
ML
1745 pr_devel("\t nr_hw_queues %d queue_depth %d\n",
1746 info->nr_hw_queues, info->queue_depth);
34d8f2be
CH
1747}
1748
1749static int ublk_ctrl_add_dev(struct io_uring_cmd *cmd)
1750{
fd9b8547 1751 const struct ublksrv_ctrl_cmd *header = io_uring_sqe_cmd(cmd->sqe);
34d8f2be
CH
1752 void __user *argp = (void __user *)(unsigned long)header->addr;
1753 struct ublksrv_ctrl_dev_info info;
71f28f31 1754 struct ublk_device *ub;
34d8f2be
CH
1755 int ret = -EINVAL;
1756
1757 if (header->len < sizeof(info) || !header->addr)
1758 return -EINVAL;
1759 if (header->queue_id != (u16)-1) {
1760 pr_warn("%s: queue_id is wrong %x\n",
1761 __func__, header->queue_id);
1762 return -EINVAL;
1763 }
4093cb5a 1764
34d8f2be
CH
1765 if (copy_from_user(&info, argp, sizeof(info)))
1766 return -EFAULT;
4093cb5a
ML
1767
1768 if (capable(CAP_SYS_ADMIN))
1769 info.flags &= ~UBLK_F_UNPRIVILEGED_DEV;
1770 else if (!(info.flags & UBLK_F_UNPRIVILEGED_DEV))
1771 return -EPERM;
1772
c0b79b0f
ML
1773 /*
1774 * unprivileged device can't be trusted, but RECOVERY and
1775 * RECOVERY_REISSUE still may hang error handling, so can't
1776 * support recovery features for unprivileged ublk now
1777 *
1778 * TODO: provide forward progress for RECOVERY handler, so that
1779 * unprivileged device can benefit from it
1780 */
1781 if (info.flags & UBLK_F_UNPRIVILEGED_DEV)
1782 info.flags &= ~(UBLK_F_USER_RECOVERY_REISSUE |
1783 UBLK_F_USER_RECOVERY);
1784
4093cb5a 1785 /* the created device is always owned by current user */
48a90519 1786 ublk_store_owner_uid_gid(&info.owner_uid, &info.owner_gid);
4093cb5a 1787
34d8f2be
CH
1788 if (header->dev_id != info.dev_id) {
1789 pr_warn("%s: dev id not match %u %u\n",
1790 __func__, header->dev_id, info.dev_id);
1791 return -EINVAL;
1792 }
71f28f31 1793
4093cb5a
ML
1794 ublk_dump_dev_info(&info);
1795
71f28f31
ML
1796 ret = mutex_lock_killable(&ublk_ctl_mutex);
1797 if (ret)
1798 return ret;
1799
403ebc87
ML
1800 ret = -EACCES;
1801 if (ublks_added >= ublks_max)
1802 goto out_unlock;
1803
cfee7e4d
CH
1804 ret = -ENOMEM;
1805 ub = kzalloc(sizeof(*ub), GFP_KERNEL);
1806 if (!ub)
1807 goto out_unlock;
fa9482e0
CH
1808 mutex_init(&ub->mutex);
1809 spin_lock_init(&ub->mm_lock);
bbae8d1f 1810 INIT_WORK(&ub->quiesce_work, ublk_quiesce_work_fn);
fa9482e0
CH
1811 INIT_WORK(&ub->stop_work, ublk_stop_work_fn);
1812 INIT_DELAYED_WORK(&ub->monitor_work, ublk_daemon_monitor_work);
cfee7e4d 1813
fa9482e0
CH
1814 ret = ublk_alloc_dev_number(ub, header->dev_id);
1815 if (ret < 0)
1816 goto out_free_ub;
71f28f31 1817
34d8f2be 1818 memcpy(&ub->dev_info, &info, sizeof(info));
71f28f31 1819
34d8f2be
CH
1820 /* update device id */
1821 ub->dev_info.dev_id = ub->ub_number;
1822
6d8c5afc
ML
1823 /*
1824 * 64bit flags will be copied back to userspace as feature
1825 * negotiation result, so have to clear flags which driver
1826 * doesn't support yet, then userspace can get correct flags
1827 * (features) to handle.
1828 */
1829 ub->dev_info.flags &= UBLK_F_ALL;
1830
29dc5d06
ML
1831 ub->dev_info.flags |= UBLK_F_CMD_IOCTL_ENCODE |
1832 UBLK_F_URING_CMD_COMP_IN_TASK;
2d786e66 1833
fa9482e0 1834 /* We are not ready to support zero copy */
6d8c5afc 1835 ub->dev_info.flags &= ~UBLK_F_SUPPORT_ZERO_COPY;
fa9482e0 1836
fa9482e0
CH
1837 ub->dev_info.nr_hw_queues = min_t(unsigned int,
1838 ub->dev_info.nr_hw_queues, nr_cpu_ids);
1839 ublk_align_max_io_size(ub);
1840
1841 ret = ublk_init_queues(ub);
34d8f2be 1842 if (ret)
fa9482e0 1843 goto out_free_dev_number;
34d8f2be 1844
fa9482e0
CH
1845 ret = ublk_add_tag_set(ub);
1846 if (ret)
1847 goto out_deinit_queues;
1848
1849 ret = -EFAULT;
1850 if (copy_to_user(argp, &ub->dev_info, sizeof(info)))
1851 goto out_free_tag_set;
1852
1853 /*
1854 * Add the char dev so that ublksrv daemon can be setup.
1855 * ublk_add_chdev() will cleanup everything if it fails.
1856 */
1857 ret = ublk_add_chdev(ub);
1858 goto out_unlock;
1859
1860out_free_tag_set:
1861 blk_mq_free_tag_set(&ub->tag_set);
1862out_deinit_queues:
1863 ublk_deinit_queues(ub);
1864out_free_dev_number:
1865 ublk_free_dev_number(ub);
1866out_free_ub:
1867 mutex_destroy(&ub->mutex);
1868 kfree(ub);
34d8f2be 1869out_unlock:
71f28f31 1870 mutex_unlock(&ublk_ctl_mutex);
71f28f31
ML
1871 return ret;
1872}
1873
1874static inline bool ublk_idr_freed(int id)
1875{
1876 void *ptr;
1877
1878 spin_lock(&ublk_idr_lock);
1879 ptr = idr_find(&ublk_index_idr, id);
1880 spin_unlock(&ublk_idr_lock);
1881
1882 return ptr == NULL;
1883}
1884
bfbcef03 1885static int ublk_ctrl_del_dev(struct ublk_device **p_ub)
71f28f31 1886{
bfbcef03
ML
1887 struct ublk_device *ub = *p_ub;
1888 int idx = ub->ub_number;
71f28f31
ML
1889 int ret;
1890
1891 ret = mutex_lock_killable(&ublk_ctl_mutex);
1892 if (ret)
1893 return ret;
1894
0abe39de 1895 if (!test_bit(UB_STATE_DELETED, &ub->state)) {
71f28f31 1896 ublk_remove(ub);
0abe39de 1897 set_bit(UB_STATE_DELETED, &ub->state);
71f28f31
ML
1898 }
1899
bfbcef03
ML
1900 /* Mark the reference as consumed */
1901 *p_ub = NULL;
1902 ublk_put_device(ub);
0abe39de 1903 mutex_unlock(&ublk_ctl_mutex);
71f28f31
ML
1904
1905 /*
1906 * Wait until the idr is removed, then it can be reused after
1907 * DEL_DEV command is returned.
0abe39de
ML
1908 *
1909 * If we returns because of user interrupt, future delete command
1910 * may come:
1911 *
1912 * - the device number isn't freed, this device won't or needn't
1913 * be deleted again, since UB_STATE_DELETED is set, and device
1914 * will be released after the last reference is dropped
1915 *
1916 * - the device number is freed already, we will not find this
1917 * device via ublk_get_device_from_id()
71f28f31 1918 */
0abe39de 1919 wait_event_interruptible(ublk_idr_wq, ublk_idr_freed(idx));
71f28f31 1920
0abe39de 1921 return 0;
71f28f31
ML
1922}
1923
71f28f31
ML
1924static inline void ublk_ctrl_cmd_dump(struct io_uring_cmd *cmd)
1925{
fd9b8547 1926 const struct ublksrv_ctrl_cmd *header = io_uring_sqe_cmd(cmd->sqe);
71f28f31
ML
1927
1928 pr_devel("%s: cmd_op %x, dev id %d qid %d data %llx buf %llx len %u\n",
1929 __func__, cmd->cmd_op, header->dev_id, header->queue_id,
1930 header->data[0], header->addr, header->len);
1931}
1932
bfbcef03 1933static int ublk_ctrl_stop_dev(struct ublk_device *ub)
71f28f31 1934{
34d8f2be
CH
1935 ublk_stop_dev(ub);
1936 cancel_work_sync(&ub->stop_work);
bbae8d1f 1937 cancel_work_sync(&ub->quiesce_work);
71f28f31
ML
1938
1939 return 0;
1940}
1941
bfbcef03
ML
1942static int ublk_ctrl_get_dev_info(struct ublk_device *ub,
1943 struct io_uring_cmd *cmd)
71f28f31 1944{
fd9b8547 1945 const struct ublksrv_ctrl_cmd *header = io_uring_sqe_cmd(cmd->sqe);
71f28f31 1946 void __user *argp = (void __user *)(unsigned long)header->addr;
34d8f2be
CH
1947
1948 if (header->len < sizeof(struct ublksrv_ctrl_dev_info) || !header->addr)
1949 return -EINVAL;
1950
34d8f2be 1951 if (copy_to_user(argp, &ub->dev_info, sizeof(ub->dev_info)))
bfbcef03 1952 return -EFAULT;
34d8f2be 1953
bfbcef03 1954 return 0;
34d8f2be
CH
1955}
1956
abb864d3
ML
1957/* TYPE_DEVT is readonly, so fill it up before returning to userspace */
1958static void ublk_ctrl_fill_params_devt(struct ublk_device *ub)
1959{
1960 ub->params.devt.char_major = MAJOR(ub->cdev_dev.devt);
1961 ub->params.devt.char_minor = MINOR(ub->cdev_dev.devt);
1962
1963 if (ub->ub_disk) {
1964 ub->params.devt.disk_major = MAJOR(disk_devt(ub->ub_disk));
1965 ub->params.devt.disk_minor = MINOR(disk_devt(ub->ub_disk));
1966 } else {
1967 ub->params.devt.disk_major = 0;
1968 ub->params.devt.disk_minor = 0;
1969 }
1970 ub->params.types |= UBLK_PARAM_TYPE_DEVT;
34d8f2be
CH
1971}
1972
bfbcef03
ML
1973static int ublk_ctrl_get_params(struct ublk_device *ub,
1974 struct io_uring_cmd *cmd)
0aa73170 1975{
fd9b8547 1976 const struct ublksrv_ctrl_cmd *header = io_uring_sqe_cmd(cmd->sqe);
0aa73170
ML
1977 void __user *argp = (void __user *)(unsigned long)header->addr;
1978 struct ublk_params_header ph;
0aa73170
ML
1979 int ret;
1980
1981 if (header->len <= sizeof(ph) || !header->addr)
1982 return -EINVAL;
1983
1984 if (copy_from_user(&ph, argp, sizeof(ph)))
1985 return -EFAULT;
1986
1987 if (ph.len > header->len || !ph.len)
1988 return -EINVAL;
1989
1990 if (ph.len > sizeof(struct ublk_params))
1991 ph.len = sizeof(struct ublk_params);
1992
0aa73170 1993 mutex_lock(&ub->mutex);
abb864d3 1994 ublk_ctrl_fill_params_devt(ub);
0aa73170
ML
1995 if (copy_to_user(argp, &ub->params, ph.len))
1996 ret = -EFAULT;
1997 else
1998 ret = 0;
1999 mutex_unlock(&ub->mutex);
2000
0aa73170
ML
2001 return ret;
2002}
2003
bfbcef03
ML
2004static int ublk_ctrl_set_params(struct ublk_device *ub,
2005 struct io_uring_cmd *cmd)
0aa73170 2006{
fd9b8547 2007 const struct ublksrv_ctrl_cmd *header = io_uring_sqe_cmd(cmd->sqe);
0aa73170
ML
2008 void __user *argp = (void __user *)(unsigned long)header->addr;
2009 struct ublk_params_header ph;
0aa73170
ML
2010 int ret = -EFAULT;
2011
2012 if (header->len <= sizeof(ph) || !header->addr)
2013 return -EINVAL;
2014
2015 if (copy_from_user(&ph, argp, sizeof(ph)))
2016 return -EFAULT;
2017
2018 if (ph.len > header->len || !ph.len || !ph.types)
2019 return -EINVAL;
2020
2021 if (ph.len > sizeof(struct ublk_params))
2022 ph.len = sizeof(struct ublk_params);
2023
0aa73170
ML
2024 /* parameters can only be changed when device isn't live */
2025 mutex_lock(&ub->mutex);
2026 if (ub->dev_info.state == UBLK_S_DEV_LIVE) {
2027 ret = -EACCES;
2028 } else if (copy_from_user(&ub->params, argp, ph.len)) {
2029 ret = -EFAULT;
2030 } else {
2031 /* clear all we don't support yet */
2032 ub->params.types &= UBLK_PARAM_TYPE_ALL;
2033 ret = ublk_validate_params(ub);
1d166527
ML
2034 if (ret)
2035 ub->params.types = 0;
0aa73170
ML
2036 }
2037 mutex_unlock(&ub->mutex);
0aa73170
ML
2038
2039 return ret;
2040}
2041
c732a852
Z
2042static void ublk_queue_reinit(struct ublk_device *ub, struct ublk_queue *ubq)
2043{
2044 int i;
2045
2046 WARN_ON_ONCE(!(ubq->ubq_daemon && ubq_daemon_is_dying(ubq)));
2047 /* All old ioucmds have to be completed */
2048 WARN_ON_ONCE(ubq->nr_io_ready);
2049 /* old daemon is PF_EXITING, put it now */
2050 put_task_struct(ubq->ubq_daemon);
2051 /* We have to reset it to NULL, otherwise ub won't accept new FETCH_REQ */
2052 ubq->ubq_daemon = NULL;
c0b79b0f 2053 ubq->timeout = false;
c732a852
Z
2054
2055 for (i = 0; i < ubq->q_depth; i++) {
2056 struct ublk_io *io = &ubq->ios[i];
2057
2058 /* forget everything now and be ready for new FETCH_REQ */
2059 io->flags = 0;
2060 io->cmd = NULL;
2061 io->addr = 0;
2062 }
2063}
2064
bfbcef03
ML
2065static int ublk_ctrl_start_recovery(struct ublk_device *ub,
2066 struct io_uring_cmd *cmd)
c732a852 2067{
fd9b8547 2068 const struct ublksrv_ctrl_cmd *header = io_uring_sqe_cmd(cmd->sqe);
c732a852
Z
2069 int ret = -EINVAL;
2070 int i;
2071
c732a852
Z
2072 mutex_lock(&ub->mutex);
2073 if (!ublk_can_use_recovery(ub))
2074 goto out_unlock;
2075 /*
2076 * START_RECOVERY is only allowd after:
2077 *
2078 * (1) UB_STATE_OPEN is not set, which means the dying process is exited
2079 * and related io_uring ctx is freed so file struct of /dev/ublkcX is
2080 * released.
2081 *
2082 * (2) UBLK_S_DEV_QUIESCED is set, which means the quiesce_work:
2083 * (a)has quiesced request queue
2084 * (b)has requeued every inflight rqs whose io_flags is ACTIVE
2085 * (c)has requeued/aborted every inflight rqs whose io_flags is NOT ACTIVE
2086 * (d)has completed/camceled all ioucmds owned by ther dying process
2087 */
2088 if (test_bit(UB_STATE_OPEN, &ub->state) ||
2089 ub->dev_info.state != UBLK_S_DEV_QUIESCED) {
2090 ret = -EBUSY;
2091 goto out_unlock;
2092 }
2093 pr_devel("%s: start recovery for dev id %d.\n", __func__, header->dev_id);
2094 for (i = 0; i < ub->dev_info.nr_hw_queues; i++)
2095 ublk_queue_reinit(ub, ublk_get_queue(ub, i));
2096 /* set to NULL, otherwise new ubq_daemon cannot mmap the io_cmd_buf */
2097 ub->mm = NULL;
2098 ub->nr_queues_ready = 0;
73a166d9 2099 ub->nr_privileged_daemon = 0;
c732a852
Z
2100 init_completion(&ub->completion);
2101 ret = 0;
2102 out_unlock:
2103 mutex_unlock(&ub->mutex);
c732a852
Z
2104 return ret;
2105}
2106
bfbcef03
ML
2107static int ublk_ctrl_end_recovery(struct ublk_device *ub,
2108 struct io_uring_cmd *cmd)
c732a852 2109{
fd9b8547 2110 const struct ublksrv_ctrl_cmd *header = io_uring_sqe_cmd(cmd->sqe);
c732a852 2111 int ublksrv_pid = (int)header->data[0];
c732a852
Z
2112 int ret = -EINVAL;
2113
c732a852
Z
2114 pr_devel("%s: Waiting for new ubq_daemons(nr: %d) are ready, dev id %d...\n",
2115 __func__, ub->dev_info.nr_hw_queues, header->dev_id);
2116 /* wait until new ubq_daemon sending all FETCH_REQ */
2117 wait_for_completion_interruptible(&ub->completion);
2118 pr_devel("%s: All new ubq_daemons(nr: %d) are ready, dev id %d\n",
2119 __func__, ub->dev_info.nr_hw_queues, header->dev_id);
2120
2121 mutex_lock(&ub->mutex);
2122 if (!ublk_can_use_recovery(ub))
2123 goto out_unlock;
2124
2125 if (ub->dev_info.state != UBLK_S_DEV_QUIESCED) {
2126 ret = -EBUSY;
2127 goto out_unlock;
2128 }
2129 ub->dev_info.ublksrv_pid = ublksrv_pid;
2130 pr_devel("%s: new ublksrv_pid %d, dev id %d\n",
2131 __func__, ublksrv_pid, header->dev_id);
2132 blk_mq_unquiesce_queue(ub->ub_disk->queue);
2133 pr_devel("%s: queue unquiesced, dev id %d.\n",
2134 __func__, header->dev_id);
2135 blk_mq_kick_requeue_list(ub->ub_disk->queue);
2136 ub->dev_info.state = UBLK_S_DEV_LIVE;
2137 schedule_delayed_work(&ub->monitor_work, UBLK_DAEMON_MONITOR_PERIOD);
2138 ret = 0;
2139 out_unlock:
2140 mutex_unlock(&ub->mutex);
c732a852
Z
2141 return ret;
2142}
2143
4093cb5a
ML
2144/*
2145 * All control commands are sent via /dev/ublk-control, so we have to check
2146 * the destination device's permission
2147 */
2148static int ublk_char_dev_permission(struct ublk_device *ub,
2149 const char *dev_path, int mask)
2150{
2151 int err;
2152 struct path path;
2153 struct kstat stat;
2154
2155 err = kern_path(dev_path, LOOKUP_FOLLOW, &path);
2156 if (err)
2157 return err;
2158
2159 err = vfs_getattr(&path, &stat, STATX_TYPE, AT_STATX_SYNC_AS_STAT);
2160 if (err)
2161 goto exit;
2162
2163 err = -EPERM;
2164 if (stat.rdev != ub->cdev_dev.devt || !S_ISCHR(stat.mode))
2165 goto exit;
2166
5b0ed596 2167 err = inode_permission(&nop_mnt_idmap,
4093cb5a
ML
2168 d_backing_inode(path.dentry), mask);
2169exit:
2170 path_put(&path);
2171 return err;
2172}
2173
2174static int ublk_ctrl_uring_cmd_permission(struct ublk_device *ub,
2175 struct io_uring_cmd *cmd)
2176{
fd9b8547 2177 struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)io_uring_sqe_cmd(cmd->sqe);
4093cb5a
ML
2178 bool unprivileged = ub->dev_info.flags & UBLK_F_UNPRIVILEGED_DEV;
2179 void __user *argp = (void __user *)(unsigned long)header->addr;
2180 char *dev_path = NULL;
2181 int ret = 0;
2182 int mask;
2183
2184 if (!unprivileged) {
2185 if (!capable(CAP_SYS_ADMIN))
2186 return -EPERM;
2187 /*
2188 * The new added command of UBLK_CMD_GET_DEV_INFO2 includes
2189 * char_dev_path in payload too, since userspace may not
2190 * know if the specified device is created as unprivileged
2191 * mode.
2192 */
2d786e66 2193 if (_IOC_NR(cmd->cmd_op) != UBLK_CMD_GET_DEV_INFO2)
4093cb5a
ML
2194 return 0;
2195 }
2196
2197 /*
2198 * User has to provide the char device path for unprivileged ublk
2199 *
2200 * header->addr always points to the dev path buffer, and
2201 * header->dev_path_len records length of dev path buffer.
2202 */
2203 if (!header->dev_path_len || header->dev_path_len > PATH_MAX)
2204 return -EINVAL;
2205
2206 if (header->len < header->dev_path_len)
2207 return -EINVAL;
2208
2209 dev_path = kmalloc(header->dev_path_len + 1, GFP_KERNEL);
2210 if (!dev_path)
2211 return -ENOMEM;
2212
2213 ret = -EFAULT;
2214 if (copy_from_user(dev_path, argp, header->dev_path_len))
2215 goto exit;
2216 dev_path[header->dev_path_len] = 0;
2217
2218 ret = -EINVAL;
2d786e66 2219 switch (_IOC_NR(cmd->cmd_op)) {
4093cb5a
ML
2220 case UBLK_CMD_GET_DEV_INFO:
2221 case UBLK_CMD_GET_DEV_INFO2:
2222 case UBLK_CMD_GET_QUEUE_AFFINITY:
2223 case UBLK_CMD_GET_PARAMS:
2224 mask = MAY_READ;
2225 break;
2226 case UBLK_CMD_START_DEV:
2227 case UBLK_CMD_STOP_DEV:
2228 case UBLK_CMD_ADD_DEV:
2229 case UBLK_CMD_DEL_DEV:
2230 case UBLK_CMD_SET_PARAMS:
2231 case UBLK_CMD_START_USER_RECOVERY:
2232 case UBLK_CMD_END_USER_RECOVERY:
2233 mask = MAY_READ | MAY_WRITE;
2234 break;
2235 default:
2236 goto exit;
2237 }
2238
2239 ret = ublk_char_dev_permission(ub, dev_path, mask);
2240 if (!ret) {
2241 header->len -= header->dev_path_len;
2242 header->addr += header->dev_path_len;
2243 }
2244 pr_devel("%s: dev id %d cmd_op %x uid %d gid %d path %s ret %d\n",
2245 __func__, ub->ub_number, cmd->cmd_op,
2246 ub->dev_info.owner_uid, ub->dev_info.owner_gid,
2247 dev_path, ret);
2248exit:
2249 kfree(dev_path);
c732a852
Z
2250 return ret;
2251}
2252
34d8f2be
CH
2253static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd,
2254 unsigned int issue_flags)
2255{
fd9b8547 2256 const struct ublksrv_ctrl_cmd *header = io_uring_sqe_cmd(cmd->sqe);
bfbcef03 2257 struct ublk_device *ub = NULL;
2d786e66 2258 u32 cmd_op = cmd->cmd_op;
71f28f31
ML
2259 int ret = -EINVAL;
2260
fa8e442e
ML
2261 if (issue_flags & IO_URING_F_NONBLOCK)
2262 return -EAGAIN;
2263
71f28f31
ML
2264 ublk_ctrl_cmd_dump(cmd);
2265
2266 if (!(issue_flags & IO_URING_F_SQE128))
2267 goto out;
2268
2d786e66
ML
2269 ret = ublk_check_cmd_op(cmd_op);
2270 if (ret)
2271 goto out;
2272
2273 if (_IOC_NR(cmd_op) != UBLK_CMD_ADD_DEV) {
bfbcef03
ML
2274 ret = -ENODEV;
2275 ub = ublk_get_device_from_id(header->dev_id);
2276 if (!ub)
2277 goto out;
4093cb5a
ML
2278
2279 ret = ublk_ctrl_uring_cmd_permission(ub, cmd);
2d786e66
ML
2280 if (ret)
2281 goto put_dev;
bfbcef03
ML
2282 }
2283
2d786e66 2284 switch (_IOC_NR(cmd_op)) {
71f28f31 2285 case UBLK_CMD_START_DEV:
bfbcef03 2286 ret = ublk_ctrl_start_dev(ub, cmd);
71f28f31
ML
2287 break;
2288 case UBLK_CMD_STOP_DEV:
bfbcef03 2289 ret = ublk_ctrl_stop_dev(ub);
71f28f31
ML
2290 break;
2291 case UBLK_CMD_GET_DEV_INFO:
4093cb5a 2292 case UBLK_CMD_GET_DEV_INFO2:
bfbcef03 2293 ret = ublk_ctrl_get_dev_info(ub, cmd);
71f28f31
ML
2294 break;
2295 case UBLK_CMD_ADD_DEV:
34d8f2be 2296 ret = ublk_ctrl_add_dev(cmd);
71f28f31
ML
2297 break;
2298 case UBLK_CMD_DEL_DEV:
bfbcef03 2299 ret = ublk_ctrl_del_dev(&ub);
71f28f31
ML
2300 break;
2301 case UBLK_CMD_GET_QUEUE_AFFINITY:
bfbcef03 2302 ret = ublk_ctrl_get_queue_affinity(ub, cmd);
71f28f31 2303 break;
0aa73170 2304 case UBLK_CMD_GET_PARAMS:
bfbcef03 2305 ret = ublk_ctrl_get_params(ub, cmd);
0aa73170
ML
2306 break;
2307 case UBLK_CMD_SET_PARAMS:
bfbcef03 2308 ret = ublk_ctrl_set_params(ub, cmd);
0aa73170 2309 break;
c732a852 2310 case UBLK_CMD_START_USER_RECOVERY:
bfbcef03 2311 ret = ublk_ctrl_start_recovery(ub, cmd);
c732a852
Z
2312 break;
2313 case UBLK_CMD_END_USER_RECOVERY:
bfbcef03 2314 ret = ublk_ctrl_end_recovery(ub, cmd);
c732a852 2315 break;
71f28f31 2316 default:
bfbcef03 2317 ret = -ENOTSUPP;
71f28f31 2318 break;
6b1439d2 2319 }
4093cb5a
ML
2320
2321 put_dev:
bfbcef03
ML
2322 if (ub)
2323 ublk_put_device(ub);
71f28f31 2324 out:
9d2789ac 2325 io_uring_cmd_done(cmd, ret, 0, issue_flags);
71f28f31
ML
2326 pr_devel("%s: cmd done ret %d cmd_op %x, dev id %d qid %d\n",
2327 __func__, ret, cmd->cmd_op, header->dev_id, header->queue_id);
2328 return -EIOCBQUEUED;
2329}
2330
2331static const struct file_operations ublk_ctl_fops = {
2332 .open = nonseekable_open,
2333 .uring_cmd = ublk_ctrl_uring_cmd,
2334 .owner = THIS_MODULE,
2335 .llseek = noop_llseek,
2336};
2337
2338static struct miscdevice ublk_misc = {
2339 .minor = MISC_DYNAMIC_MINOR,
2340 .name = "ublk-control",
2341 .fops = &ublk_ctl_fops,
2342};
2343
2344static int __init ublk_init(void)
2345{
2346 int ret;
2347
2348 init_waitqueue_head(&ublk_idr_wq);
2349
2350 ret = misc_register(&ublk_misc);
2351 if (ret)
2352 return ret;
2353
2354 ret = alloc_chrdev_region(&ublk_chr_devt, 0, UBLK_MINORS, "ublk-char");
2355 if (ret)
2356 goto unregister_mis;
2357
1aaba11d 2358 ublk_chr_class = class_create("ublk-char");
71f28f31
ML
2359 if (IS_ERR(ublk_chr_class)) {
2360 ret = PTR_ERR(ublk_chr_class);
2361 goto free_chrdev_region;
2362 }
2363 return 0;
2364
2365free_chrdev_region:
2366 unregister_chrdev_region(ublk_chr_devt, UBLK_MINORS);
2367unregister_mis:
2368 misc_deregister(&ublk_misc);
2369 return ret;
2370}
2371
2372static void __exit ublk_exit(void)
2373{
2374 struct ublk_device *ub;
2375 int id;
2376
71f28f31
ML
2377 idr_for_each_entry(&ublk_index_idr, ub, id)
2378 ublk_remove(ub);
2379
8e4ff684
ML
2380 class_destroy(ublk_chr_class);
2381 misc_deregister(&ublk_misc);
2382
71f28f31
ML
2383 idr_destroy(&ublk_index_idr);
2384 unregister_chrdev_region(ublk_chr_devt, UBLK_MINORS);
2385}
2386
2387module_init(ublk_init);
2388module_exit(ublk_exit);
2389
403ebc87
ML
2390module_param(ublks_max, int, 0444);
2391MODULE_PARM_DESC(ublks_max, "max number of ublk devices allowed to add(default: 64)");
2392
71f28f31
ML
2393MODULE_AUTHOR("Ming Lei <ming.lei@redhat.com>");
2394MODULE_LICENSE("GPL");