xref: /linux/drivers/block/ublk_drv.c (revision 5a4332062e9e71de8e78dc1b389d21e0dd44848b)
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/cmd.h>
40 #include <linux/blk-mq.h>
41 #include <linux/delay.h>
42 #include <linux/mm.h>
43 #include <asm/page.h>
44 #include <linux/task_work.h>
45 #include <linux/namei.h>
46 #include <linux/kref.h>
47 #include <uapi/linux/ublk_cmd.h>
48 
49 #define UBLK_MINORS		(1U << MINORBITS)
50 
51 /* private ioctl command mirror */
52 #define UBLK_CMD_DEL_DEV_ASYNC	_IOC_NR(UBLK_U_CMD_DEL_DEV_ASYNC)
53 
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 \
62 		| UBLK_F_USER_COPY \
63 		| UBLK_F_ZONED)
64 
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)
69 
70 struct ublk_rq_data {
71 	struct llist_node node;
72 
73 	struct kref ref;
74 };
75 
76 struct ublk_uring_cmd_pdu {
77 	struct ublk_queue *ubq;
78 	u16 tag;
79 };
80 
81 /*
82  * io command is active: sqe cmd is received, and its cqe isn't done
83  *
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.
86  *
87  * If the flag is cleared, the io command will be completed, and owned by
88  * ublk server.
89  */
90 #define UBLK_IO_FLAG_ACTIVE	0x01
91 
92 /*
93  * IO command is completed via cqe, and it is being handled by ublksrv, and
94  * not committed yet
95  *
96  * Basically exclusively with UBLK_IO_FLAG_ACTIVE, so can be served for
97  * cross verification
98  */
99 #define UBLK_IO_FLAG_OWNED_BY_SRV 0x02
100 
101 /*
102  * IO command is aborted, so this flag is set in case of
103  * !UBLK_IO_FLAG_ACTIVE.
104  *
105  * After this flag is observed, any pending or new incoming request
106  * associated with this io command will be failed immediately
107  */
108 #define UBLK_IO_FLAG_ABORTED 0x04
109 
110 /*
111  * UBLK_IO_FLAG_NEED_GET_DATA is set because IO command requires
112  * get data buffer address from ublksrv.
113  *
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.
116  */
117 #define UBLK_IO_FLAG_NEED_GET_DATA 0x08
118 
119 /* atomic RW with ubq->cancel_lock */
120 #define UBLK_IO_FLAG_CANCELED	0x80000000
121 
122 struct ublk_io {
123 	/* userspace buffer address from io cmd */
124 	__u64	addr;
125 	unsigned int flags;
126 	int res;
127 
128 	struct io_uring_cmd *cmd;
129 };
130 
131 struct ublk_queue {
132 	int q_id;
133 	int q_depth;
134 
135 	unsigned long flags;
136 	struct task_struct	*ubq_daemon;
137 	char *io_cmd_buf;
138 
139 	struct llist_head	io_cmds;
140 
141 	unsigned long io_addr;	/* mapped vm address */
142 	unsigned int max_io_sz;
143 	bool force_abort;
144 	bool timeout;
145 	bool canceling;
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[];
150 };
151 
152 struct ublk_device {
153 	struct gendisk		*ub_disk;
154 
155 	char	*__queues;
156 
157 	unsigned int	queue_size;
158 	struct ublksrv_ctrl_dev_info	dev_info;
159 
160 	struct blk_mq_tag_set	tag_set;
161 
162 	struct cdev		cdev;
163 	struct device		cdev_dev;
164 
165 #define UB_STATE_OPEN		0
166 #define UB_STATE_USED		1
167 #define UB_STATE_DELETED	2
168 	unsigned long		state;
169 	int			ub_number;
170 
171 	struct mutex		mutex;
172 
173 	spinlock_t		lock;
174 	struct mm_struct	*mm;
175 
176 	struct ublk_params	params;
177 
178 	struct completion	completion;
179 	unsigned int		nr_queues_ready;
180 	unsigned int		nr_privileged_daemon;
181 
182 	struct work_struct	quiesce_work;
183 	struct work_struct	stop_work;
184 };
185 
186 /* header of ublk_params */
187 struct ublk_params_header {
188 	__u32	len;
189 	__u32	types;
190 };
191 
192 static bool ublk_abort_requests(struct ublk_device *ub, struct ublk_queue *ubq);
193 
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,
196 						   int tag);
197 static inline bool ublk_dev_is_user_copy(const struct ublk_device *ub)
198 {
199 	return ub->dev_info.flags & UBLK_F_USER_COPY;
200 }
201 
202 static inline bool ublk_dev_is_zoned(const struct ublk_device *ub)
203 {
204 	return ub->dev_info.flags & UBLK_F_ZONED;
205 }
206 
207 static inline bool ublk_queue_is_zoned(struct ublk_queue *ubq)
208 {
209 	return ubq->flags & UBLK_F_ZONED;
210 }
211 
212 #ifdef CONFIG_BLK_DEV_ZONED
213 
214 struct ublk_zoned_report_desc {
215 	__u64 sector;
216 	__u32 operation;
217 	__u32 nr_zones;
218 };
219 
220 static DEFINE_XARRAY(ublk_zoned_report_descs);
221 
222 static int ublk_zoned_insert_report_desc(const struct request *req,
223 		struct ublk_zoned_report_desc *desc)
224 {
225 	return xa_insert(&ublk_zoned_report_descs, (unsigned long)req,
226 			    desc, GFP_KERNEL);
227 }
228 
229 static struct ublk_zoned_report_desc *ublk_zoned_erase_report_desc(
230 		const struct request *req)
231 {
232 	return xa_erase(&ublk_zoned_report_descs, (unsigned long)req);
233 }
234 
235 static struct ublk_zoned_report_desc *ublk_zoned_get_report_desc(
236 		const struct request *req)
237 {
238 	return xa_load(&ublk_zoned_report_descs, (unsigned long)req);
239 }
240 
241 static int ublk_get_nr_zones(const struct ublk_device *ub)
242 {
243 	const struct ublk_param_basic *p = &ub->params.basic;
244 
245 	/* Zone size is a power of 2 */
246 	return p->dev_sectors >> ilog2(p->chunk_sectors);
247 }
248 
249 static int ublk_revalidate_disk_zones(struct ublk_device *ub)
250 {
251 	return blk_revalidate_disk_zones(ub->ub_disk);
252 }
253 
254 static int ublk_dev_param_zoned_validate(const struct ublk_device *ub)
255 {
256 	const struct ublk_param_zoned *p = &ub->params.zoned;
257 	int nr_zones;
258 
259 	if (!ublk_dev_is_zoned(ub))
260 		return -EINVAL;
261 
262 	if (!p->max_zone_append_sectors)
263 		return -EINVAL;
264 
265 	nr_zones = ublk_get_nr_zones(ub);
266 
267 	if (p->max_active_zones > nr_zones)
268 		return -EINVAL;
269 
270 	if (p->max_open_zones > nr_zones)
271 		return -EINVAL;
272 
273 	return 0;
274 }
275 
276 static void ublk_dev_param_zoned_apply(struct ublk_device *ub)
277 {
278 	ub->ub_disk->nr_zones = ublk_get_nr_zones(ub);
279 }
280 
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)
284 {
285 	struct request_queue *q = ublk->ub_disk->queue;
286 	size_t bufsize;
287 	void *buf;
288 
289 	nr_zones = min_t(unsigned int, nr_zones,
290 			 ublk->ub_disk->nr_zones);
291 
292 	bufsize = nr_zones * sizeof(struct blk_zone);
293 	bufsize =
294 		min_t(size_t, bufsize, queue_max_hw_sectors(q) << SECTOR_SHIFT);
295 
296 	while (bufsize >= sizeof(struct blk_zone)) {
297 		buf = kvmalloc(bufsize, GFP_KERNEL | __GFP_NORETRY);
298 		if (buf) {
299 			*buflen = bufsize;
300 			return buf;
301 		}
302 		bufsize >>= 1;
303 	}
304 
305 	*buflen = 0;
306 	return NULL;
307 }
308 
309 static int ublk_report_zones(struct gendisk *disk, sector_t sector,
310 		      unsigned int nr_zones, report_zones_cb cb, void *data)
311 {
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;
317 	int ret;
318 	struct blk_zone *buffer;
319 	size_t buffer_length;
320 
321 	nr_zones = min_t(unsigned int, ub->ub_disk->nr_zones - first_zone,
322 			 nr_zones);
323 
324 	buffer = ublk_alloc_report_buffer(ub, nr_zones, &buffer_length);
325 	if (!buffer)
326 		return -ENOMEM;
327 
328 	max_zones_per_request = buffer_length / sizeof(struct blk_zone);
329 
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);
334 		struct request *req;
335 		struct ublk_zoned_report_desc desc;
336 		blk_status_t status;
337 
338 		memset(buffer, 0, buffer_length);
339 
340 		req = blk_mq_alloc_request(disk->queue, REQ_OP_DRV_IN, 0);
341 		if (IS_ERR(req)) {
342 			ret = PTR_ERR(req);
343 			goto out;
344 		}
345 
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);
350 		if (ret)
351 			goto free_req;
352 
353 		ret = blk_rq_map_kern(disk->queue, req, buffer, buffer_length,
354 					GFP_KERNEL);
355 		if (ret)
356 			goto erase_desc;
357 
358 		status = blk_execute_rq(req, 0);
359 		ret = blk_status_to_errno(status);
360 erase_desc:
361 		ublk_zoned_erase_report_desc(req);
362 free_req:
363 		blk_mq_free_request(req);
364 		if (ret)
365 			goto out;
366 
367 		for (unsigned int i = 0; i < zones_in_request; i++) {
368 			struct blk_zone *zone = buffer + i;
369 
370 			/* A zero length zone means no more zones in this response */
371 			if (!zone->len)
372 				break;
373 
374 			ret = cb(zone, i, data);
375 			if (ret)
376 				goto out;
377 
378 			done_zones++;
379 			sector += zone_size_sectors;
380 
381 		}
382 	}
383 
384 	ret = done_zones;
385 
386 out:
387 	kvfree(buffer);
388 	return ret;
389 }
390 
391 static blk_status_t ublk_setup_iod_zoned(struct ublk_queue *ubq,
392 					 struct request *req)
393 {
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;
397 	u32 ublk_op;
398 
399 	switch (req_op(req)) {
400 	case REQ_OP_ZONE_OPEN:
401 		ublk_op = UBLK_IO_OP_ZONE_OPEN;
402 		break;
403 	case REQ_OP_ZONE_CLOSE:
404 		ublk_op = UBLK_IO_OP_ZONE_CLOSE;
405 		break;
406 	case REQ_OP_ZONE_FINISH:
407 		ublk_op = UBLK_IO_OP_ZONE_FINISH;
408 		break;
409 	case REQ_OP_ZONE_RESET:
410 		ublk_op = UBLK_IO_OP_ZONE_RESET;
411 		break;
412 	case REQ_OP_ZONE_APPEND:
413 		ublk_op = UBLK_IO_OP_ZONE_APPEND;
414 		break;
415 	case REQ_OP_ZONE_RESET_ALL:
416 		ublk_op = UBLK_IO_OP_ZONE_RESET_ALL;
417 		break;
418 	case REQ_OP_DRV_IN:
419 		desc = ublk_zoned_get_report_desc(req);
420 		if (!desc)
421 			return BLK_STS_IOERR;
422 		ublk_op = desc->operation;
423 		switch (ublk_op) {
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;
428 			return BLK_STS_OK;
429 		default:
430 			return BLK_STS_IOERR;
431 		}
432 	case REQ_OP_DRV_OUT:
433 		/* We do not support drv_out */
434 		return BLK_STS_NOTSUPP;
435 	default:
436 		return BLK_STS_IOERR;
437 	}
438 
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;
443 
444 	return BLK_STS_OK;
445 }
446 
447 #else
448 
449 #define ublk_report_zones (NULL)
450 
451 static int ublk_dev_param_zoned_validate(const struct ublk_device *ub)
452 {
453 	return -EOPNOTSUPP;
454 }
455 
456 static void ublk_dev_param_zoned_apply(struct ublk_device *ub)
457 {
458 }
459 
460 static int ublk_revalidate_disk_zones(struct ublk_device *ub)
461 {
462 	return 0;
463 }
464 
465 static blk_status_t ublk_setup_iod_zoned(struct ublk_queue *ubq,
466 					 struct request *req)
467 {
468 	return BLK_STS_NOTSUPP;
469 }
470 
471 #endif
472 
473 static inline void __ublk_complete_rq(struct request *req);
474 static void ublk_complete_rq(struct kref *ref);
475 
476 static dev_t ublk_chr_devt;
477 static const struct class ublk_chr_class = {
478 	.name = "ublk-char",
479 };
480 
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 */
484 
485 static DEFINE_MUTEX(ublk_ctl_mutex);
486 
487 /*
488  * Max ublk devices allowed to add
489  *
490  * It can be extended to one per-user limit in future or even controlled
491  * by cgroup.
492  */
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 */
496 
497 static struct miscdevice ublk_misc;
498 
499 static inline unsigned ublk_pos_to_hwq(loff_t pos)
500 {
501 	return ((pos - UBLKSRV_IO_BUF_OFFSET) >> UBLK_QID_OFF) &
502 		UBLK_QID_BITS_MASK;
503 }
504 
505 static inline unsigned ublk_pos_to_buf_off(loff_t pos)
506 {
507 	return (pos - UBLKSRV_IO_BUF_OFFSET) & UBLK_IO_BUF_BITS_MASK;
508 }
509 
510 static inline unsigned ublk_pos_to_tag(loff_t pos)
511 {
512 	return ((pos - UBLKSRV_IO_BUF_OFFSET) >> UBLK_TAG_OFF) &
513 		UBLK_TAG_BITS_MASK;
514 }
515 
516 static void ublk_dev_param_basic_apply(struct ublk_device *ub)
517 {
518 	const struct ublk_param_basic *p = &ub->params.basic;
519 
520 	if (p->attrs & UBLK_ATTR_READ_ONLY)
521 		set_disk_ro(ub->ub_disk, true);
522 
523 	set_capacity(ub->ub_disk, p->dev_sectors);
524 }
525 
526 static int ublk_validate_params(const struct ublk_device *ub)
527 {
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;
531 
532 		if (p->logical_bs_shift > PAGE_SHIFT || p->logical_bs_shift < 9)
533 			return -EINVAL;
534 
535 		if (p->logical_bs_shift > p->physical_bs_shift)
536 			return -EINVAL;
537 
538 		if (p->max_sectors > (ub->dev_info.max_io_buf_bytes >> 9))
539 			return -EINVAL;
540 
541 		if (ublk_dev_is_zoned(ub) && !p->chunk_sectors)
542 			return -EINVAL;
543 	} else
544 		return -EINVAL;
545 
546 	if (ub->params.types & UBLK_PARAM_TYPE_DISCARD) {
547 		const struct ublk_param_discard *p = &ub->params.discard;
548 
549 		/* So far, only support single segment discard */
550 		if (p->max_discard_sectors && p->max_discard_segments != 1)
551 			return -EINVAL;
552 
553 		if (!p->discard_granularity)
554 			return -EINVAL;
555 	}
556 
557 	/* dev_t is read-only */
558 	if (ub->params.types & UBLK_PARAM_TYPE_DEVT)
559 		return -EINVAL;
560 
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))
564 		return -EINVAL;
565 
566 	return 0;
567 }
568 
569 static void ublk_apply_params(struct ublk_device *ub)
570 {
571 	ublk_dev_param_basic_apply(ub);
572 
573 	if (ub->params.types & UBLK_PARAM_TYPE_ZONED)
574 		ublk_dev_param_zoned_apply(ub);
575 }
576 
577 static inline bool ublk_support_user_copy(const struct ublk_queue *ubq)
578 {
579 	return ubq->flags & UBLK_F_USER_COPY;
580 }
581 
582 static inline bool ublk_need_req_ref(const struct ublk_queue *ubq)
583 {
584 	/*
585 	 * read()/write() is involved in user copy, so request reference
586 	 * has to be grabbed
587 	 */
588 	return ublk_support_user_copy(ubq);
589 }
590 
591 static inline void ublk_init_req_ref(const struct ublk_queue *ubq,
592 		struct request *req)
593 {
594 	if (ublk_need_req_ref(ubq)) {
595 		struct ublk_rq_data *data = blk_mq_rq_to_pdu(req);
596 
597 		kref_init(&data->ref);
598 	}
599 }
600 
601 static inline bool ublk_get_req_ref(const struct ublk_queue *ubq,
602 		struct request *req)
603 {
604 	if (ublk_need_req_ref(ubq)) {
605 		struct ublk_rq_data *data = blk_mq_rq_to_pdu(req);
606 
607 		return kref_get_unless_zero(&data->ref);
608 	}
609 
610 	return true;
611 }
612 
613 static inline void ublk_put_req_ref(const struct ublk_queue *ubq,
614 		struct request *req)
615 {
616 	if (ublk_need_req_ref(ubq)) {
617 		struct ublk_rq_data *data = blk_mq_rq_to_pdu(req);
618 
619 		kref_put(&data->ref, ublk_complete_rq);
620 	} else {
621 		__ublk_complete_rq(req);
622 	}
623 }
624 
625 static inline bool ublk_need_get_data(const struct ublk_queue *ubq)
626 {
627 	return ubq->flags & UBLK_F_NEED_GET_DATA;
628 }
629 
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)
632 {
633 	if (kobject_get_unless_zero(&ub->cdev_dev.kobj))
634 		return ub;
635 	return NULL;
636 }
637 
638 /* Called in slow path only, keep it noinline for trace purpose */
639 static noinline void ublk_put_device(struct ublk_device *ub)
640 {
641 	put_device(&ub->cdev_dev);
642 }
643 
644 static inline struct ublk_queue *ublk_get_queue(struct ublk_device *dev,
645 		int qid)
646 {
647        return (struct ublk_queue *)&(dev->__queues[qid * dev->queue_size]);
648 }
649 
650 static inline bool ublk_rq_has_data(const struct request *rq)
651 {
652 	return bio_has_data(rq->bio);
653 }
654 
655 static inline struct ublksrv_io_desc *ublk_get_iod(struct ublk_queue *ubq,
656 		int tag)
657 {
658 	return (struct ublksrv_io_desc *)
659 		&(ubq->io_cmd_buf[tag * sizeof(struct ublksrv_io_desc)]);
660 }
661 
662 static inline char *ublk_queue_cmd_buf(struct ublk_device *ub, int q_id)
663 {
664 	return ublk_get_queue(ub, q_id)->io_cmd_buf;
665 }
666 
667 static inline int ublk_queue_cmd_buf_size(struct ublk_device *ub, int q_id)
668 {
669 	struct ublk_queue *ubq = ublk_get_queue(ub, q_id);
670 
671 	return round_up(ubq->q_depth * sizeof(struct ublksrv_io_desc),
672 			PAGE_SIZE);
673 }
674 
675 static inline bool ublk_queue_can_use_recovery_reissue(
676 		struct ublk_queue *ubq)
677 {
678 	return (ubq->flags & UBLK_F_USER_RECOVERY) &&
679 			(ubq->flags & UBLK_F_USER_RECOVERY_REISSUE);
680 }
681 
682 static inline bool ublk_queue_can_use_recovery(
683 		struct ublk_queue *ubq)
684 {
685 	return ubq->flags & UBLK_F_USER_RECOVERY;
686 }
687 
688 static inline bool ublk_can_use_recovery(struct ublk_device *ub)
689 {
690 	return ub->dev_info.flags & UBLK_F_USER_RECOVERY;
691 }
692 
693 static void ublk_free_disk(struct gendisk *disk)
694 {
695 	struct ublk_device *ub = disk->private_data;
696 
697 	clear_bit(UB_STATE_USED, &ub->state);
698 	ublk_put_device(ub);
699 }
700 
701 static void ublk_store_owner_uid_gid(unsigned int *owner_uid,
702 		unsigned int *owner_gid)
703 {
704 	kuid_t uid;
705 	kgid_t gid;
706 
707 	current_uid_gid(&uid, &gid);
708 
709 	*owner_uid = from_kuid(&init_user_ns, uid);
710 	*owner_gid = from_kgid(&init_user_ns, gid);
711 }
712 
713 static int ublk_open(struct gendisk *disk, blk_mode_t mode)
714 {
715 	struct ublk_device *ub = disk->private_data;
716 
717 	if (capable(CAP_SYS_ADMIN))
718 		return 0;
719 
720 	/*
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.
725 	 *
726 	 * This way is reasonable too given anyone can create
727 	 * unprivileged device, and no need other's grant.
728 	 */
729 	if (ub->dev_info.flags & UBLK_F_UNPRIVILEGED_DEV) {
730 		unsigned int curr_uid, curr_gid;
731 
732 		ublk_store_owner_uid_gid(&curr_uid, &curr_gid);
733 
734 		if (curr_uid != ub->dev_info.owner_uid || curr_gid !=
735 				ub->dev_info.owner_gid)
736 			return -EPERM;
737 	}
738 
739 	return 0;
740 }
741 
742 static const struct block_device_operations ub_fops = {
743 	.owner =	THIS_MODULE,
744 	.open =		ublk_open,
745 	.free_disk =	ublk_free_disk,
746 	.report_zones =	ublk_report_zones,
747 };
748 
749 #define UBLK_MAX_PIN_PAGES	32
750 
751 struct ublk_io_iter {
752 	struct page *pages[UBLK_MAX_PIN_PAGES];
753 	struct bio *bio;
754 	struct bvec_iter iter;
755 };
756 
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)
760 {
761 	unsigned done = 0;
762 	unsigned pg_idx = 0;
763 
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]);
770 
771 		if (dir == ITER_DEST)
772 			memcpy(pg_buf + pg_off, bv_buf, bytes);
773 		else
774 			memcpy(bv_buf, pg_buf + pg_off, bytes);
775 
776 		kunmap_local(pg_buf);
777 		kunmap_local(bv_buf);
778 
779 		/* advance page array */
780 		pg_off += bytes;
781 		if (pg_off == PAGE_SIZE) {
782 			pg_idx += 1;
783 			pg_off = 0;
784 		}
785 
786 		done += bytes;
787 
788 		/* advance bio */
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)
793 				break;
794 			data->iter = data->bio->bi_iter;
795 		}
796 	}
797 }
798 
799 static bool ublk_advance_io_iter(const struct request *req,
800 		struct ublk_io_iter *iter, unsigned int offset)
801 {
802 	struct bio *bio = req->bio;
803 
804 	for_each_bio(bio) {
805 		if (bio->bi_iter.bi_size > offset) {
806 			iter->bio = bio;
807 			iter->iter = bio->bi_iter;
808 			bio_advance_iter(iter->bio, &iter->iter, offset);
809 			return true;
810 		}
811 		offset -= bio->bi_iter.bi_size;
812 	}
813 	return false;
814 }
815 
816 /*
817  * Copy data between request pages and io_iter, and 'offset'
818  * is the start point of linear offset of request.
819  */
820 static size_t ublk_copy_user_pages(const struct request *req,
821 		unsigned offset, struct iov_iter *uiter, int dir)
822 {
823 	struct ublk_io_iter iter;
824 	size_t done = 0;
825 
826 	if (!ublk_advance_io_iter(req, &iter, offset))
827 		return 0;
828 
829 	while (iov_iter_count(uiter) && iter.bio) {
830 		unsigned nr_pages;
831 		ssize_t len;
832 		size_t off;
833 		int i;
834 
835 		len = iov_iter_get_pages2(uiter, iter.pages,
836 				iov_iter_count(uiter),
837 				UBLK_MAX_PIN_PAGES, &off);
838 		if (len <= 0)
839 			return done;
840 
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]);
847 		}
848 		done += len;
849 	}
850 
851 	return done;
852 }
853 
854 static inline bool ublk_need_map_req(const struct request *req)
855 {
856 	return ublk_rq_has_data(req) && req_op(req) == REQ_OP_WRITE;
857 }
858 
859 static inline bool ublk_need_unmap_req(const struct request *req)
860 {
861 	return ublk_rq_has_data(req) &&
862 	       (req_op(req) == REQ_OP_READ || req_op(req) == REQ_OP_DRV_IN);
863 }
864 
865 static int ublk_map_io(const struct ublk_queue *ubq, const struct request *req,
866 		struct ublk_io *io)
867 {
868 	const unsigned int rq_bytes = blk_rq_bytes(req);
869 
870 	if (ublk_support_user_copy(ubq))
871 		return rq_bytes;
872 
873 	/*
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
877 	 */
878 	if (ublk_need_map_req(req)) {
879 		struct iov_iter iter;
880 		const int dir = ITER_DEST;
881 
882 		import_ubuf(dir, u64_to_user_ptr(io->addr), rq_bytes, &iter);
883 		return ublk_copy_user_pages(req, 0, &iter, dir);
884 	}
885 	return rq_bytes;
886 }
887 
888 static int ublk_unmap_io(const struct ublk_queue *ubq,
889 		const struct request *req,
890 		struct ublk_io *io)
891 {
892 	const unsigned int rq_bytes = blk_rq_bytes(req);
893 
894 	if (ublk_support_user_copy(ubq))
895 		return rq_bytes;
896 
897 	if (ublk_need_unmap_req(req)) {
898 		struct iov_iter iter;
899 		const int dir = ITER_SOURCE;
900 
901 		WARN_ON_ONCE(io->res > rq_bytes);
902 
903 		import_ubuf(dir, u64_to_user_ptr(io->addr), io->res, &iter);
904 		return ublk_copy_user_pages(req, 0, &iter, dir);
905 	}
906 	return rq_bytes;
907 }
908 
909 static inline unsigned int ublk_req_build_flags(struct request *req)
910 {
911 	unsigned flags = 0;
912 
913 	if (req->cmd_flags & REQ_FAILFAST_DEV)
914 		flags |= UBLK_IO_F_FAILFAST_DEV;
915 
916 	if (req->cmd_flags & REQ_FAILFAST_TRANSPORT)
917 		flags |= UBLK_IO_F_FAILFAST_TRANSPORT;
918 
919 	if (req->cmd_flags & REQ_FAILFAST_DRIVER)
920 		flags |= UBLK_IO_F_FAILFAST_DRIVER;
921 
922 	if (req->cmd_flags & REQ_META)
923 		flags |= UBLK_IO_F_META;
924 
925 	if (req->cmd_flags & REQ_FUA)
926 		flags |= UBLK_IO_F_FUA;
927 
928 	if (req->cmd_flags & REQ_NOUNMAP)
929 		flags |= UBLK_IO_F_NOUNMAP;
930 
931 	if (req->cmd_flags & REQ_SWAP)
932 		flags |= UBLK_IO_F_SWAP;
933 
934 	return flags;
935 }
936 
937 static blk_status_t ublk_setup_iod(struct ublk_queue *ubq, struct request *req)
938 {
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);
942 	u32 ublk_op;
943 
944 	if (!ublk_queue_is_zoned(ubq) &&
945 	    (op_is_zone_mgmt(op) || op == REQ_OP_ZONE_APPEND))
946 		return BLK_STS_IOERR;
947 
948 	switch (req_op(req)) {
949 	case REQ_OP_READ:
950 		ublk_op = UBLK_IO_OP_READ;
951 		break;
952 	case REQ_OP_WRITE:
953 		ublk_op = UBLK_IO_OP_WRITE;
954 		break;
955 	case REQ_OP_FLUSH:
956 		ublk_op = UBLK_IO_OP_FLUSH;
957 		break;
958 	case REQ_OP_DISCARD:
959 		ublk_op = UBLK_IO_OP_DISCARD;
960 		break;
961 	case REQ_OP_WRITE_ZEROES:
962 		ublk_op = UBLK_IO_OP_WRITE_ZEROES;
963 		break;
964 	default:
965 		if (ublk_queue_is_zoned(ubq))
966 			return ublk_setup_iod_zoned(ubq, req);
967 		return BLK_STS_IOERR;
968 	}
969 
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;
975 
976 	return BLK_STS_OK;
977 }
978 
979 static inline struct ublk_uring_cmd_pdu *ublk_get_uring_cmd_pdu(
980 		struct io_uring_cmd *ioucmd)
981 {
982 	return (struct ublk_uring_cmd_pdu *)&ioucmd->pdu;
983 }
984 
985 static inline bool ubq_daemon_is_dying(struct ublk_queue *ubq)
986 {
987 	return ubq->ubq_daemon->flags & PF_EXITING;
988 }
989 
990 /* todo: handle partial completion */
991 static inline void __ublk_complete_rq(struct request *req)
992 {
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;
997 
998 	/* called from ublk_abort_queue() code path */
999 	if (io->flags & UBLK_IO_FLAG_ABORTED) {
1000 		res = BLK_STS_IOERR;
1001 		goto exit;
1002 	}
1003 
1004 	/* failed read IO if nothing is read */
1005 	if (!io->res && req_op(req) == REQ_OP_READ)
1006 		io->res = -EIO;
1007 
1008 	if (io->res < 0) {
1009 		res = errno_to_blk_status(io->res);
1010 		goto exit;
1011 	}
1012 
1013 	/*
1014 	 * FLUSH, DISCARD or WRITE_ZEROES usually won't return bytes returned, so end them
1015 	 * directly.
1016 	 *
1017 	 * Both the two needn't unmap.
1018 	 */
1019 	if (req_op(req) != REQ_OP_READ && req_op(req) != REQ_OP_WRITE &&
1020 	    req_op(req) != REQ_OP_DRV_IN)
1021 		goto exit;
1022 
1023 	/* for READ request, writing data in iod->addr to rq buffers */
1024 	unmapped_bytes = ublk_unmap_io(ubq, req, io);
1025 
1026 	/*
1027 	 * Extremely impossible since we got data filled in just before
1028 	 *
1029 	 * Re-read simply for this unlikely case.
1030 	 */
1031 	if (unlikely(unmapped_bytes < io->res))
1032 		io->res = unmapped_bytes;
1033 
1034 	if (blk_update_request(req, BLK_STS_OK, io->res))
1035 		blk_mq_requeue_request(req, true);
1036 	else
1037 		__blk_mq_end_request(req, BLK_STS_OK);
1038 
1039 	return;
1040 exit:
1041 	blk_mq_end_request(req, res);
1042 }
1043 
1044 static void ublk_complete_rq(struct kref *ref)
1045 {
1046 	struct ublk_rq_data *data = container_of(ref, struct ublk_rq_data,
1047 			ref);
1048 	struct request *req = blk_mq_rq_from_pdu(data);
1049 
1050 	__ublk_complete_rq(req);
1051 }
1052 
1053 /*
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.
1057  *
1058  * Also aborting may not be started yet, keep in mind that one failed
1059  * request may be issued by block layer again.
1060  */
1061 static void __ublk_fail_req(struct ublk_queue *ubq, struct ublk_io *io,
1062 		struct request *req)
1063 {
1064 	WARN_ON_ONCE(io->flags & UBLK_IO_FLAG_ACTIVE);
1065 
1066 	if (ublk_queue_can_use_recovery_reissue(ubq))
1067 		blk_mq_requeue_request(req, false);
1068 	else
1069 		ublk_put_req_ref(ubq, req);
1070 }
1071 
1072 static void ubq_complete_io_cmd(struct ublk_io *io, int res,
1073 				unsigned issue_flags)
1074 {
1075 	/* mark this cmd owned by ublksrv */
1076 	io->flags |= UBLK_IO_FLAG_OWNED_BY_SRV;
1077 
1078 	/*
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.
1081 	 */
1082 	io->flags &= ~UBLK_IO_FLAG_ACTIVE;
1083 
1084 	/* tell ublksrv one io request is coming */
1085 	io_uring_cmd_done(io->cmd, res, 0, issue_flags);
1086 }
1087 
1088 #define UBLK_REQUEUE_DELAY_MS	3
1089 
1090 static inline void __ublk_abort_rq(struct ublk_queue *ubq,
1091 		struct request *rq)
1092 {
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);
1096 	else
1097 		blk_mq_end_request(rq, BLK_STS_IOERR);
1098 }
1099 
1100 static inline void __ublk_rq_task_work(struct request *req,
1101 				       unsigned issue_flags)
1102 {
1103 	struct ublk_queue *ubq = req->mq_hctx->driver_data;
1104 	int tag = req->tag;
1105 	struct ublk_io *io = &ubq->ios[tag];
1106 	unsigned int mapped_bytes;
1107 
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);
1111 
1112 	/*
1113 	 * Task is exiting if either:
1114 	 *
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.
1118 	 *
1119 	 * (2) current->flags & PF_EXITING.
1120 	 */
1121 	if (unlikely(current != ubq->ubq_daemon || current->flags & PF_EXITING)) {
1122 		__ublk_abort_rq(ubq, req);
1123 		return;
1124 	}
1125 
1126 	if (ublk_need_get_data(ubq) && ublk_need_map_req(req)) {
1127 		/*
1128 		 * We have not handled UBLK_IO_NEED_GET_DATA command yet,
1129 		 * so immepdately pass UBLK_IO_RES_NEED_GET_DATA to ublksrv
1130 		 * and notify it.
1131 		 */
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);
1138 			return;
1139 		}
1140 		/*
1141 		 * We have handled UBLK_IO_NEED_GET_DATA command,
1142 		 * so clear UBLK_IO_FLAG_NEED_GET_DATA now and just
1143 		 * do the copy work.
1144 		 */
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);
1151 	}
1152 
1153 	mapped_bytes = ublk_map_io(ubq, req, io);
1154 
1155 	/* partially mapped, update io descriptor */
1156 	if (unlikely(mapped_bytes != blk_rq_bytes(req))) {
1157 		/*
1158 		 * Nothing mapped, retry until we succeed.
1159 		 *
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.
1163 		 */
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);
1168 			return;
1169 		}
1170 
1171 		ublk_get_iod(ubq, req->tag)->nr_sectors =
1172 			mapped_bytes >> 9;
1173 	}
1174 
1175 	ublk_init_req_ref(ubq, req);
1176 	ubq_complete_io_cmd(io, UBLK_IO_RES_OK, issue_flags);
1177 }
1178 
1179 static inline void ublk_forward_io_cmds(struct ublk_queue *ubq,
1180 					unsigned issue_flags)
1181 {
1182 	struct llist_node *io_cmds = llist_del_all(&ubq->io_cmds);
1183 	struct ublk_rq_data *data, *tmp;
1184 
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);
1188 }
1189 
1190 static void ublk_rq_task_work_cb(struct io_uring_cmd *cmd, unsigned issue_flags)
1191 {
1192 	struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
1193 	struct ublk_queue *ubq = pdu->ubq;
1194 
1195 	ublk_forward_io_cmds(ubq, issue_flags);
1196 }
1197 
1198 static void ublk_queue_cmd(struct ublk_queue *ubq, struct request *rq)
1199 {
1200 	struct ublk_rq_data *data = blk_mq_rq_to_pdu(rq);
1201 
1202 	if (llist_add(&data->node, &ubq->io_cmds)) {
1203 		struct ublk_io *io = &ubq->ios[rq->tag];
1204 
1205 		io_uring_cmd_complete_in_task(io->cmd, ublk_rq_task_work_cb);
1206 	}
1207 }
1208 
1209 static enum blk_eh_timer_return ublk_timeout(struct request *rq)
1210 {
1211 	struct ublk_queue *ubq = rq->mq_hctx->driver_data;
1212 	unsigned int nr_inflight = 0;
1213 	int i;
1214 
1215 	if (ubq->flags & UBLK_F_UNPRIVILEGED_DEV) {
1216 		if (!ubq->timeout) {
1217 			send_sig(SIGKILL, ubq->ubq_daemon, 0);
1218 			ubq->timeout = true;
1219 		}
1220 
1221 		return BLK_EH_DONE;
1222 	}
1223 
1224 	if (!ubq_daemon_is_dying(ubq))
1225 		return BLK_EH_RESET_TIMER;
1226 
1227 	for (i = 0; i < ubq->q_depth; i++) {
1228 		struct ublk_io *io = &ubq->ios[i];
1229 
1230 		if (!(io->flags & UBLK_IO_FLAG_ACTIVE))
1231 			nr_inflight++;
1232 	}
1233 
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;
1237 
1238 		if (ublk_abort_requests(ub, ubq)) {
1239 			if (ublk_can_use_recovery(ub))
1240 				schedule_work(&ub->quiesce_work);
1241 			else
1242 				schedule_work(&ub->stop_work);
1243 		}
1244 		return BLK_EH_DONE;
1245 	}
1246 
1247 	return BLK_EH_RESET_TIMER;
1248 }
1249 
1250 static blk_status_t ublk_queue_rq(struct blk_mq_hw_ctx *hctx,
1251 		const struct blk_mq_queue_data *bd)
1252 {
1253 	struct ublk_queue *ubq = hctx->driver_data;
1254 	struct request *rq = bd->rq;
1255 	blk_status_t res;
1256 
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;
1261 
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.
1267 	 *
1268 	 * Note: force_abort is guaranteed to be seen because it is set
1269 	 * before request queue is unqiuesced.
1270 	 */
1271 	if (ublk_queue_can_use_recovery(ubq) && unlikely(ubq->force_abort))
1272 		return BLK_STS_IOERR;
1273 
1274 	if (unlikely(ubq->canceling)) {
1275 		__ublk_abort_rq(ubq, rq);
1276 		return BLK_STS_OK;
1277 	}
1278 
1279 	blk_mq_start_request(bd->rq);
1280 	ublk_queue_cmd(ubq, rq);
1281 
1282 	return BLK_STS_OK;
1283 }
1284 
1285 static int ublk_init_hctx(struct blk_mq_hw_ctx *hctx, void *driver_data,
1286 		unsigned int hctx_idx)
1287 {
1288 	struct ublk_device *ub = driver_data;
1289 	struct ublk_queue *ubq = ublk_get_queue(ub, hctx->queue_num);
1290 
1291 	hctx->driver_data = ubq;
1292 	return 0;
1293 }
1294 
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,
1299 };
1300 
1301 static int ublk_ch_open(struct inode *inode, struct file *filp)
1302 {
1303 	struct ublk_device *ub = container_of(inode->i_cdev,
1304 			struct ublk_device, cdev);
1305 
1306 	if (test_and_set_bit(UB_STATE_OPEN, &ub->state))
1307 		return -EBUSY;
1308 	filp->private_data = ub;
1309 	return 0;
1310 }
1311 
1312 static int ublk_ch_release(struct inode *inode, struct file *filp)
1313 {
1314 	struct ublk_device *ub = filp->private_data;
1315 
1316 	clear_bit(UB_STATE_OPEN, &ub->state);
1317 	return 0;
1318 }
1319 
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)
1322 {
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;
1327 	int q_id, ret = 0;
1328 
1329 	spin_lock(&ub->lock);
1330 	if (!ub->mm)
1331 		ub->mm = current->mm;
1332 	if (current->mm != ub->mm)
1333 		ret = -EINVAL;
1334 	spin_unlock(&ub->lock);
1335 
1336 	if (ret)
1337 		return ret;
1338 
1339 	if (vma->vm_flags & VM_WRITE)
1340 		return -EPERM;
1341 
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)
1344 		return -EINVAL;
1345 
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);
1350 
1351 	if (sz != ublk_queue_cmd_buf_size(ub, q_id))
1352 		return -EINVAL;
1353 
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);
1356 }
1357 
1358 static void ublk_commit_completion(struct ublk_device *ub,
1359 		const struct ublksrv_io_cmd *ub_cmd)
1360 {
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;
1365 
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;
1369 
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)))
1373 		return;
1374 
1375 	if (req_op(req) == REQ_OP_ZONE_APPEND)
1376 		req->__sector = ub_cmd->zone_append_lba;
1377 
1378 	if (likely(!blk_should_fake_timeout(req->q)))
1379 		ublk_put_req_ref(ubq, req);
1380 }
1381 
1382 /*
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.
1386  */
1387 static void ublk_abort_queue(struct ublk_device *ub, struct ublk_queue *ubq)
1388 {
1389 	int i;
1390 
1391 	for (i = 0; i < ubq->q_depth; i++) {
1392 		struct ublk_io *io = &ubq->ios[i];
1393 
1394 		if (!(io->flags & UBLK_IO_FLAG_ACTIVE)) {
1395 			struct request *rq;
1396 
1397 			/*
1398 			 * Either we fail the request or ublk_rq_task_work_fn
1399 			 * will do it
1400 			 */
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);
1405 			}
1406 		}
1407 	}
1408 }
1409 
1410 static bool ublk_abort_requests(struct ublk_device *ub, struct ublk_queue *ubq)
1411 {
1412 	struct gendisk *disk;
1413 
1414 	spin_lock(&ubq->cancel_lock);
1415 	if (ubq->canceling) {
1416 		spin_unlock(&ubq->cancel_lock);
1417 		return false;
1418 	}
1419 	ubq->canceling = true;
1420 	spin_unlock(&ubq->cancel_lock);
1421 
1422 	spin_lock(&ub->lock);
1423 	disk = ub->ub_disk;
1424 	if (disk)
1425 		get_device(disk_to_dev(disk));
1426 	spin_unlock(&ub->lock);
1427 
1428 	/* Our disk has been dead */
1429 	if (!disk)
1430 		return false;
1431 
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));
1438 
1439 	return true;
1440 }
1441 
1442 static void ublk_cancel_cmd(struct ublk_queue *ubq, struct ublk_io *io,
1443 		unsigned int issue_flags)
1444 {
1445 	bool done;
1446 
1447 	if (!(io->flags & UBLK_IO_FLAG_ACTIVE))
1448 		return;
1449 
1450 	spin_lock(&ubq->cancel_lock);
1451 	done = !!(io->flags & UBLK_IO_FLAG_CANCELED);
1452 	if (!done)
1453 		io->flags |= UBLK_IO_FLAG_CANCELED;
1454 	spin_unlock(&ubq->cancel_lock);
1455 
1456 	if (!done)
1457 		io_uring_cmd_done(io->cmd, UBLK_IO_RES_ABORT, 0, issue_flags);
1458 }
1459 
1460 /*
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
1463  */
1464 static void ublk_uring_cmd_cancel_fn(struct io_uring_cmd *cmd,
1465 		unsigned int issue_flags)
1466 {
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;
1471 	bool need_schedule;
1472 	struct ublk_io *io;
1473 
1474 	if (WARN_ON_ONCE(!ubq))
1475 		return;
1476 
1477 	if (WARN_ON_ONCE(pdu->tag >= ubq->q_depth))
1478 		return;
1479 
1480 	task = io_uring_cmd_get_task(cmd);
1481 	if (WARN_ON_ONCE(task && task != ubq->ubq_daemon))
1482 		return;
1483 
1484 	ub = ubq->dev;
1485 	need_schedule = ublk_abort_requests(ub, ubq);
1486 
1487 	io = &ubq->ios[pdu->tag];
1488 	WARN_ON_ONCE(io->cmd != cmd);
1489 	ublk_cancel_cmd(ubq, io, issue_flags);
1490 
1491 	if (need_schedule) {
1492 		if (ublk_can_use_recovery(ub))
1493 			schedule_work(&ub->quiesce_work);
1494 		else
1495 			schedule_work(&ub->stop_work);
1496 	}
1497 }
1498 
1499 static inline bool ublk_queue_ready(struct ublk_queue *ubq)
1500 {
1501 	return ubq->nr_io_ready == ubq->q_depth;
1502 }
1503 
1504 static void ublk_cancel_queue(struct ublk_queue *ubq)
1505 {
1506 	int i;
1507 
1508 	for (i = 0; i < ubq->q_depth; i++)
1509 		ublk_cancel_cmd(ubq, &ubq->ios[i], IO_URING_F_UNLOCKED);
1510 }
1511 
1512 /* Cancel all pending commands, must be called after del_gendisk() returns */
1513 static void ublk_cancel_dev(struct ublk_device *ub)
1514 {
1515 	int i;
1516 
1517 	for (i = 0; i < ub->dev_info.nr_hw_queues; i++)
1518 		ublk_cancel_queue(ublk_get_queue(ub, i));
1519 }
1520 
1521 static bool ublk_check_inflight_rq(struct request *rq, void *data)
1522 {
1523 	bool *idle = data;
1524 
1525 	if (blk_mq_request_started(rq)) {
1526 		*idle = false;
1527 		return false;
1528 	}
1529 	return true;
1530 }
1531 
1532 static void ublk_wait_tagset_rqs_idle(struct ublk_device *ub)
1533 {
1534 	bool idle;
1535 
1536 	WARN_ON_ONCE(!blk_queue_quiesced(ub->ub_disk->queue));
1537 	while (true) {
1538 		idle = true;
1539 		blk_mq_tagset_busy_iter(&ub->tag_set,
1540 				ublk_check_inflight_rq, &idle);
1541 		if (idle)
1542 			break;
1543 		msleep(UBLK_REQUEUE_DELAY_MS);
1544 	}
1545 }
1546 
1547 static void __ublk_quiesce_dev(struct ublk_device *ub)
1548 {
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;
1556 }
1557 
1558 static void ublk_quiesce_work_fn(struct work_struct *work)
1559 {
1560 	struct ublk_device *ub =
1561 		container_of(work, struct ublk_device, quiesce_work);
1562 
1563 	mutex_lock(&ub->mutex);
1564 	if (ub->dev_info.state != UBLK_S_DEV_LIVE)
1565 		goto unlock;
1566 	__ublk_quiesce_dev(ub);
1567  unlock:
1568 	mutex_unlock(&ub->mutex);
1569 	ublk_cancel_dev(ub);
1570 }
1571 
1572 static void ublk_unquiesce_dev(struct ublk_device *ub)
1573 {
1574 	int i;
1575 
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()
1583 	 * can move on.
1584 	 */
1585 	for (i = 0; i < ub->dev_info.nr_hw_queues; i++)
1586 		ublk_get_queue(ub, i)->force_abort = true;
1587 
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);
1591 }
1592 
1593 static void ublk_stop_dev(struct ublk_device *ub)
1594 {
1595 	struct gendisk *disk;
1596 
1597 	mutex_lock(&ub->mutex);
1598 	if (ub->dev_info.state == UBLK_S_DEV_DEAD)
1599 		goto unlock;
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);
1604 	}
1605 	del_gendisk(ub->ub_disk);
1606 
1607 	/* Sync with ublk_abort_queue() by holding the lock */
1608 	spin_lock(&ub->lock);
1609 	disk = ub->ub_disk;
1610 	ub->dev_info.state = UBLK_S_DEV_DEAD;
1611 	ub->dev_info.ublksrv_pid = -1;
1612 	ub->ub_disk = NULL;
1613 	spin_unlock(&ub->lock);
1614 	put_disk(disk);
1615  unlock:
1616 	mutex_unlock(&ub->mutex);
1617 	ublk_cancel_dev(ub);
1618 }
1619 
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)
1622 {
1623 	mutex_lock(&ub->mutex);
1624 	ubq->nr_io_ready++;
1625 	if (ublk_queue_ready(ubq)) {
1626 		ubq->ubq_daemon = current;
1627 		get_task_struct(ubq->ubq_daemon);
1628 		ub->nr_queues_ready++;
1629 
1630 		if (capable(CAP_SYS_ADMIN))
1631 			ub->nr_privileged_daemon++;
1632 	}
1633 	if (ub->nr_queues_ready == ub->dev_info.nr_hw_queues)
1634 		complete_all(&ub->completion);
1635 	mutex_unlock(&ub->mutex);
1636 }
1637 
1638 static void ublk_handle_need_get_data(struct ublk_device *ub, int q_id,
1639 		int tag)
1640 {
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);
1643 
1644 	ublk_queue_cmd(ubq, req);
1645 }
1646 
1647 static inline int ublk_check_cmd_op(u32 cmd_op)
1648 {
1649 	u32 ioc_type = _IOC_TYPE(cmd_op);
1650 
1651 	if (!IS_ENABLED(CONFIG_BLKDEV_UBLK_LEGACY_OPCODES) && ioc_type != 'u')
1652 		return -EOPNOTSUPP;
1653 
1654 	if (ioc_type != 'u' && ioc_type != 0)
1655 		return -EOPNOTSUPP;
1656 
1657 	return 0;
1658 }
1659 
1660 static inline void ublk_fill_io_cmd(struct ublk_io *io,
1661 		struct io_uring_cmd *cmd, unsigned long buf_addr)
1662 {
1663 	io->cmd = cmd;
1664 	io->flags |= UBLK_IO_FLAG_ACTIVE;
1665 	io->addr = buf_addr;
1666 }
1667 
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)
1671 {
1672 	struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
1673 
1674 	/*
1675 	 * Safe to refer to @ubq since ublk_queue won't be died until its
1676 	 * commands are completed
1677 	 */
1678 	pdu->ubq = ubq;
1679 	pdu->tag = tag;
1680 	io_uring_cmd_mark_cancelable(cmd, issue_flags);
1681 }
1682 
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)
1686 {
1687 	struct ublk_device *ub = cmd->file->private_data;
1688 	struct ublk_queue *ubq;
1689 	struct ublk_io *io;
1690 	u32 cmd_op = cmd->cmd_op;
1691 	unsigned tag = ub_cmd->tag;
1692 	int ret = -EINVAL;
1693 	struct request *req;
1694 
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,
1697 			ub_cmd->result);
1698 
1699 	if (ub_cmd->q_id >= ub->dev_info.nr_hw_queues)
1700 		goto out;
1701 
1702 	ubq = ublk_get_queue(ub, ub_cmd->q_id);
1703 	if (!ubq || ub_cmd->q_id != ubq->q_id)
1704 		goto out;
1705 
1706 	if (ubq->ubq_daemon && ubq->ubq_daemon != current)
1707 		goto out;
1708 
1709 	if (tag >= ubq->q_depth)
1710 		goto out;
1711 
1712 	io = &ubq->ios[tag];
1713 
1714 	/* there is pending io cmd, something must be wrong */
1715 	if (io->flags & UBLK_IO_FLAG_ACTIVE) {
1716 		ret = -EBUSY;
1717 		goto out;
1718 	}
1719 
1720 	/*
1721 	 * ensure that the user issues UBLK_IO_NEED_GET_DATA
1722 	 * iff the driver have set the UBLK_IO_FLAG_NEED_GET_DATA.
1723 	 */
1724 	if ((!!(io->flags & UBLK_IO_FLAG_NEED_GET_DATA))
1725 			^ (_IOC_NR(cmd_op) == UBLK_IO_NEED_GET_DATA))
1726 		goto out;
1727 
1728 	ret = ublk_check_cmd_op(cmd_op);
1729 	if (ret)
1730 		goto out;
1731 
1732 	ret = -EINVAL;
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)) {
1737 			ret = -EBUSY;
1738 			goto out;
1739 		}
1740 		/*
1741 		 * The io is being handled by server, so COMMIT_RQ is expected
1742 		 * instead of FETCH_REQ
1743 		 */
1744 		if (io->flags & UBLK_IO_FLAG_OWNED_BY_SRV)
1745 			goto out;
1746 
1747 		if (!ublk_support_user_copy(ubq)) {
1748 			/*
1749 			 * FETCH_RQ has to provide IO buffer if NEED GET
1750 			 * DATA is not enabled
1751 			 */
1752 			if (!ub_cmd->addr && !ublk_need_get_data(ubq))
1753 				goto out;
1754 		} else if (ub_cmd->addr) {
1755 			/* User copy requires addr to be unset */
1756 			ret = -EINVAL;
1757 			goto out;
1758 		}
1759 
1760 		ublk_fill_io_cmd(io, cmd, ub_cmd->addr);
1761 		ublk_mark_io_ready(ub, ubq);
1762 		break;
1763 	case UBLK_IO_COMMIT_AND_FETCH_REQ:
1764 		req = blk_mq_tag_to_rq(ub->tag_set.tags[ub_cmd->q_id], tag);
1765 
1766 		if (!(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV))
1767 			goto out;
1768 
1769 		if (!ublk_support_user_copy(ubq)) {
1770 			/*
1771 			 * COMMIT_AND_FETCH_REQ has to provide IO buffer if
1772 			 * NEED GET DATA is not enabled or it is Read IO.
1773 			 */
1774 			if (!ub_cmd->addr && (!ublk_need_get_data(ubq) ||
1775 						req_op(req) == REQ_OP_READ))
1776 				goto out;
1777 		} else if (req_op(req) != REQ_OP_ZONE_APPEND && ub_cmd->addr) {
1778 			/*
1779 			 * User copy requires addr to be unset when command is
1780 			 * not zone append
1781 			 */
1782 			ret = -EINVAL;
1783 			goto out;
1784 		}
1785 
1786 		ublk_fill_io_cmd(io, cmd, ub_cmd->addr);
1787 		ublk_commit_completion(ub, ub_cmd);
1788 		break;
1789 	case UBLK_IO_NEED_GET_DATA:
1790 		if (!(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV))
1791 			goto out;
1792 		ublk_fill_io_cmd(io, cmd, ub_cmd->addr);
1793 		ublk_handle_need_get_data(ub, ub_cmd->q_id, ub_cmd->tag);
1794 		break;
1795 	default:
1796 		goto out;
1797 	}
1798 	ublk_prep_cancel(cmd, issue_flags, ubq, tag);
1799 	return -EIOCBQUEUED;
1800 
1801  out:
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;
1806 }
1807 
1808 static inline struct request *__ublk_check_and_get_req(struct ublk_device *ub,
1809 		struct ublk_queue *ubq, int tag, size_t offset)
1810 {
1811 	struct request *req;
1812 
1813 	if (!ublk_need_req_ref(ubq))
1814 		return NULL;
1815 
1816 	req = blk_mq_tag_to_rq(ub->tag_set.tags[ubq->q_id], tag);
1817 	if (!req)
1818 		return NULL;
1819 
1820 	if (!ublk_get_req_ref(ubq, req))
1821 		return NULL;
1822 
1823 	if (unlikely(!blk_mq_request_started(req) || req->tag != tag))
1824 		goto fail_put;
1825 
1826 	if (!ublk_rq_has_data(req))
1827 		goto fail_put;
1828 
1829 	if (offset > blk_rq_bytes(req))
1830 		goto fail_put;
1831 
1832 	return req;
1833 fail_put:
1834 	ublk_put_req_ref(ubq, req);
1835 	return NULL;
1836 }
1837 
1838 static inline int ublk_ch_uring_cmd_local(struct io_uring_cmd *cmd,
1839 		unsigned int issue_flags)
1840 {
1841 	/*
1842 	 * Not necessary for async retry, but let's keep it simple and always
1843 	 * copy the values to avoid any potential reuse.
1844 	 */
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)
1851 	};
1852 
1853 	WARN_ON_ONCE(issue_flags & IO_URING_F_UNLOCKED);
1854 
1855 	return __ublk_ch_uring_cmd(cmd, issue_flags, &ub_cmd);
1856 }
1857 
1858 static void ublk_ch_uring_cmd_cb(struct io_uring_cmd *cmd,
1859 		unsigned int issue_flags)
1860 {
1861 	ublk_ch_uring_cmd_local(cmd, issue_flags);
1862 }
1863 
1864 static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
1865 {
1866 	if (unlikely(issue_flags & IO_URING_F_CANCEL)) {
1867 		ublk_uring_cmd_cancel_fn(cmd, issue_flags);
1868 		return 0;
1869 	}
1870 
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;
1875 	}
1876 
1877 	return ublk_ch_uring_cmd_local(cmd, issue_flags);
1878 }
1879 
1880 static inline bool ublk_check_ubuf_dir(const struct request *req,
1881 		int ubuf_dir)
1882 {
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)
1886 		return true;
1887 
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)
1892 		return true;
1893 
1894 	return false;
1895 }
1896 
1897 static struct request *ublk_check_and_get_req(struct kiocb *iocb,
1898 		struct iov_iter *iter, size_t *off, int dir)
1899 {
1900 	struct ublk_device *ub = iocb->ki_filp->private_data;
1901 	struct ublk_queue *ubq;
1902 	struct request *req;
1903 	size_t buf_off;
1904 	u16 tag, q_id;
1905 
1906 	if (!ub)
1907 		return ERR_PTR(-EACCES);
1908 
1909 	if (!user_backed_iter(iter))
1910 		return ERR_PTR(-EACCES);
1911 
1912 	if (ub->dev_info.state == UBLK_S_DEV_DEAD)
1913 		return ERR_PTR(-EACCES);
1914 
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);
1918 
1919 	if (q_id >= ub->dev_info.nr_hw_queues)
1920 		return ERR_PTR(-EINVAL);
1921 
1922 	ubq = ublk_get_queue(ub, q_id);
1923 	if (!ubq)
1924 		return ERR_PTR(-EINVAL);
1925 
1926 	if (tag >= ubq->q_depth)
1927 		return ERR_PTR(-EINVAL);
1928 
1929 	req = __ublk_check_and_get_req(ub, ubq, tag, buf_off);
1930 	if (!req)
1931 		return ERR_PTR(-EINVAL);
1932 
1933 	if (!req->mq_hctx || !req->mq_hctx->driver_data)
1934 		goto fail;
1935 
1936 	if (!ublk_check_ubuf_dir(req, dir))
1937 		goto fail;
1938 
1939 	*off = buf_off;
1940 	return req;
1941 fail:
1942 	ublk_put_req_ref(ubq, req);
1943 	return ERR_PTR(-EACCES);
1944 }
1945 
1946 static ssize_t ublk_ch_read_iter(struct kiocb *iocb, struct iov_iter *to)
1947 {
1948 	struct ublk_queue *ubq;
1949 	struct request *req;
1950 	size_t buf_off;
1951 	size_t ret;
1952 
1953 	req = ublk_check_and_get_req(iocb, to, &buf_off, ITER_DEST);
1954 	if (IS_ERR(req))
1955 		return PTR_ERR(req);
1956 
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);
1960 
1961 	return ret;
1962 }
1963 
1964 static ssize_t ublk_ch_write_iter(struct kiocb *iocb, struct iov_iter *from)
1965 {
1966 	struct ublk_queue *ubq;
1967 	struct request *req;
1968 	size_t buf_off;
1969 	size_t ret;
1970 
1971 	req = ublk_check_and_get_req(iocb, from, &buf_off, ITER_SOURCE);
1972 	if (IS_ERR(req))
1973 		return PTR_ERR(req);
1974 
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);
1978 
1979 	return ret;
1980 }
1981 
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,
1990 };
1991 
1992 static void ublk_deinit_queue(struct ublk_device *ub, int q_id)
1993 {
1994 	int size = ublk_queue_cmd_buf_size(ub, q_id);
1995 	struct ublk_queue *ubq = ublk_get_queue(ub, q_id);
1996 
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));
2001 }
2002 
2003 static int ublk_init_queue(struct ublk_device *ub, int q_id)
2004 {
2005 	struct ublk_queue *ubq = ublk_get_queue(ub, q_id);
2006 	gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO;
2007 	void *ptr;
2008 	int size;
2009 
2010 	spin_lock_init(&ubq->cancel_lock);
2011 	ubq->flags = ub->dev_info.flags;
2012 	ubq->q_id = q_id;
2013 	ubq->q_depth = ub->dev_info.queue_depth;
2014 	size = ublk_queue_cmd_buf_size(ub, q_id);
2015 
2016 	ptr = (void *) __get_free_pages(gfp_flags, get_order(size));
2017 	if (!ptr)
2018 		return -ENOMEM;
2019 
2020 	ubq->io_cmd_buf = ptr;
2021 	ubq->dev = ub;
2022 	return 0;
2023 }
2024 
2025 static void ublk_deinit_queues(struct ublk_device *ub)
2026 {
2027 	int nr_queues = ub->dev_info.nr_hw_queues;
2028 	int i;
2029 
2030 	if (!ub->__queues)
2031 		return;
2032 
2033 	for (i = 0; i < nr_queues; i++)
2034 		ublk_deinit_queue(ub, i);
2035 	kfree(ub->__queues);
2036 }
2037 
2038 static int ublk_init_queues(struct ublk_device *ub)
2039 {
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;
2044 
2045 	ub->queue_size = ubq_size;
2046 	ub->__queues = kcalloc(nr_queues, ubq_size, GFP_KERNEL);
2047 	if (!ub->__queues)
2048 		return ret;
2049 
2050 	for (i = 0; i < nr_queues; i++) {
2051 		if (ublk_init_queue(ub, i))
2052 			goto fail;
2053 	}
2054 
2055 	init_completion(&ub->completion);
2056 	return 0;
2057 
2058  fail:
2059 	ublk_deinit_queues(ub);
2060 	return ret;
2061 }
2062 
2063 static int ublk_alloc_dev_number(struct ublk_device *ub, int idx)
2064 {
2065 	int i = idx;
2066 	int err;
2067 
2068 	spin_lock(&ublk_idr_lock);
2069 	/* allocate id, if @id >= 0, we're requesting that specific id */
2070 	if (i >= 0) {
2071 		err = idr_alloc(&ublk_index_idr, ub, i, i + 1, GFP_NOWAIT);
2072 		if (err == -ENOSPC)
2073 			err = -EEXIST;
2074 	} else {
2075 		err = idr_alloc(&ublk_index_idr, ub, 0, UBLK_MAX_UBLKS,
2076 				GFP_NOWAIT);
2077 	}
2078 	spin_unlock(&ublk_idr_lock);
2079 
2080 	if (err >= 0)
2081 		ub->ub_number = err;
2082 
2083 	return err;
2084 }
2085 
2086 static void ublk_free_dev_number(struct ublk_device *ub)
2087 {
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);
2092 }
2093 
2094 static void ublk_cdev_rel(struct device *dev)
2095 {
2096 	struct ublk_device *ub = container_of(dev, struct ublk_device, cdev_dev);
2097 
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);
2102 	kfree(ub);
2103 }
2104 
2105 static int ublk_add_chdev(struct ublk_device *ub)
2106 {
2107 	struct device *dev = &ub->cdev_dev;
2108 	int minor = ub->ub_number;
2109 	int ret;
2110 
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);
2116 
2117 	ret = dev_set_name(dev, "ublkc%d", minor);
2118 	if (ret)
2119 		goto fail;
2120 
2121 	cdev_init(&ub->cdev, &ublk_ch_fops);
2122 	ret = cdev_device_add(&ub->cdev, dev);
2123 	if (ret)
2124 		goto fail;
2125 
2126 	ublks_added++;
2127 	return 0;
2128  fail:
2129 	put_device(dev);
2130 	return ret;
2131 }
2132 
2133 static void ublk_stop_work_fn(struct work_struct *work)
2134 {
2135 	struct ublk_device *ub =
2136 		container_of(work, struct ublk_device, stop_work);
2137 
2138 	ublk_stop_dev(ub);
2139 }
2140 
2141 /* align max io buffer size with PAGE_SIZE */
2142 static void ublk_align_max_io_size(struct ublk_device *ub)
2143 {
2144 	unsigned int max_io_bytes = ub->dev_info.max_io_buf_bytes;
2145 
2146 	ub->dev_info.max_io_buf_bytes =
2147 		round_down(max_io_bytes, PAGE_SIZE);
2148 }
2149 
2150 static int ublk_add_tag_set(struct ublk_device *ub)
2151 {
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);
2160 }
2161 
2162 static void ublk_remove(struct ublk_device *ub)
2163 {
2164 	ublk_stop_dev(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);
2169 	ublks_added--;
2170 }
2171 
2172 static struct ublk_device *ublk_get_device_from_id(int idx)
2173 {
2174 	struct ublk_device *ub = NULL;
2175 
2176 	if (idx < 0)
2177 		return NULL;
2178 
2179 	spin_lock(&ublk_idr_lock);
2180 	ub = idr_find(&ublk_index_idr, idx);
2181 	if (ub)
2182 		ub = ublk_get_device(ub);
2183 	spin_unlock(&ublk_idr_lock);
2184 
2185 	return ub;
2186 }
2187 
2188 static int ublk_ctrl_start_dev(struct ublk_device *ub, struct io_uring_cmd *cmd)
2189 {
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,
2203 		.dma_alignment		= 3,
2204 	};
2205 	struct gendisk *disk;
2206 	int ret = -EINVAL;
2207 
2208 	if (ublksrv_pid <= 0)
2209 		return -EINVAL;
2210 	if (!(ub->params.types & UBLK_PARAM_TYPE_BASIC))
2211 		return -EINVAL;
2212 
2213 	if (ub->params.types & UBLK_PARAM_TYPE_DISCARD) {
2214 		const struct ublk_param_discard *pd = &ub->params.discard;
2215 
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;
2221 	}
2222 
2223 	if (ub->params.types & UBLK_PARAM_TYPE_ZONED) {
2224 		const struct ublk_param_zoned *p = &ub->params.zoned;
2225 
2226 		if (!IS_ENABLED(CONFIG_BLK_DEV_ZONED))
2227 			return -EOPNOTSUPP;
2228 
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;
2233 	}
2234 
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;
2239 	}
2240 
2241 	if (ub->params.basic.attrs & UBLK_ATTR_ROTATIONAL)
2242 		lim.features |= BLK_FEAT_ROTATIONAL;
2243 
2244 	if (wait_for_completion_interruptible(&ub->completion) != 0)
2245 		return -EINTR;
2246 
2247 	mutex_lock(&ub->mutex);
2248 	if (ub->dev_info.state == UBLK_S_DEV_LIVE ||
2249 	    test_bit(UB_STATE_USED, &ub->state)) {
2250 		ret = -EEXIST;
2251 		goto out_unlock;
2252 	}
2253 
2254 	disk = blk_mq_alloc_disk(&ub->tag_set, &lim, NULL);
2255 	if (IS_ERR(disk)) {
2256 		ret = PTR_ERR(disk);
2257 		goto out_unlock;
2258 	}
2259 	sprintf(disk->disk_name, "ublkb%d", ub->ub_number);
2260 	disk->fops = &ub_fops;
2261 	disk->private_data = ub;
2262 
2263 	ub->dev_info.ublksrv_pid = ublksrv_pid;
2264 	ub->ub_disk = disk;
2265 
2266 	ublk_apply_params(ub);
2267 
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);
2271 
2272 	ublk_get_device(ub);
2273 	ub->dev_info.state = UBLK_S_DEV_LIVE;
2274 
2275 	if (ublk_dev_is_zoned(ub)) {
2276 		ret = ublk_revalidate_disk_zones(ub);
2277 		if (ret)
2278 			goto out_put_cdev;
2279 	}
2280 
2281 	ret = add_disk(disk);
2282 	if (ret)
2283 		goto out_put_cdev;
2284 
2285 	set_bit(UB_STATE_USED, &ub->state);
2286 
2287 out_put_cdev:
2288 	if (ret) {
2289 		ub->dev_info.state = UBLK_S_DEV_DEAD;
2290 		ublk_put_device(ub);
2291 	}
2292 	if (ret)
2293 		put_disk(disk);
2294 out_unlock:
2295 	mutex_unlock(&ub->mutex);
2296 	return ret;
2297 }
2298 
2299 static int ublk_ctrl_get_queue_affinity(struct ublk_device *ub,
2300 		struct io_uring_cmd *cmd)
2301 {
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;
2307 	unsigned int i;
2308 	int ret;
2309 
2310 	if (header->len * BITS_PER_BYTE < nr_cpu_ids)
2311 		return -EINVAL;
2312 	if (header->len & (sizeof(unsigned long)-1))
2313 		return -EINVAL;
2314 	if (!header->addr)
2315 		return -EINVAL;
2316 
2317 	queue = header->data[0];
2318 	if (queue >= ub->dev_info.nr_hw_queues)
2319 		return -EINVAL;
2320 
2321 	if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL))
2322 		return -ENOMEM;
2323 
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);
2327 	}
2328 
2329 	ret = -EFAULT;
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;
2336 
2337 	ret = 0;
2338 out_free_cpumask:
2339 	free_cpumask_var(cpumask);
2340 	return ret;
2341 }
2342 
2343 static inline void ublk_dump_dev_info(struct ublksrv_ctrl_dev_info *info)
2344 {
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);
2349 }
2350 
2351 static int ublk_ctrl_add_dev(struct io_uring_cmd *cmd)
2352 {
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;
2357 	int ret = -EINVAL;
2358 
2359 	if (header->len < sizeof(info) || !header->addr)
2360 		return -EINVAL;
2361 	if (header->queue_id != (u16)-1) {
2362 		pr_warn("%s: queue_id is wrong %x\n",
2363 			__func__, header->queue_id);
2364 		return -EINVAL;
2365 	}
2366 
2367 	if (copy_from_user(&info, argp, sizeof(info)))
2368 		return -EFAULT;
2369 
2370 	if (capable(CAP_SYS_ADMIN))
2371 		info.flags &= ~UBLK_F_UNPRIVILEGED_DEV;
2372 	else if (!(info.flags & UBLK_F_UNPRIVILEGED_DEV))
2373 		return -EPERM;
2374 
2375 	/*
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
2379 	 *
2380 	 * TODO: provide forward progress for RECOVERY handler, so that
2381 	 * unprivileged device can benefit from it
2382 	 */
2383 	if (info.flags & UBLK_F_UNPRIVILEGED_DEV)
2384 		info.flags &= ~(UBLK_F_USER_RECOVERY_REISSUE |
2385 				UBLK_F_USER_RECOVERY);
2386 
2387 	/* the created device is always owned by current user */
2388 	ublk_store_owner_uid_gid(&info.owner_uid, &info.owner_gid);
2389 
2390 	if (header->dev_id != info.dev_id) {
2391 		pr_warn("%s: dev id not match %u %u\n",
2392 			__func__, header->dev_id, info.dev_id);
2393 		return -EINVAL;
2394 	}
2395 
2396 	if (header->dev_id != U32_MAX && header->dev_id >= UBLK_MAX_UBLKS) {
2397 		pr_warn("%s: dev id is too large. Max supported is %d\n",
2398 			__func__, UBLK_MAX_UBLKS - 1);
2399 		return -EINVAL;
2400 	}
2401 
2402 	ublk_dump_dev_info(&info);
2403 
2404 	ret = mutex_lock_killable(&ublk_ctl_mutex);
2405 	if (ret)
2406 		return ret;
2407 
2408 	ret = -EACCES;
2409 	if (ublks_added >= ublks_max)
2410 		goto out_unlock;
2411 
2412 	ret = -ENOMEM;
2413 	ub = kzalloc(sizeof(*ub), GFP_KERNEL);
2414 	if (!ub)
2415 		goto out_unlock;
2416 	mutex_init(&ub->mutex);
2417 	spin_lock_init(&ub->lock);
2418 	INIT_WORK(&ub->quiesce_work, ublk_quiesce_work_fn);
2419 	INIT_WORK(&ub->stop_work, ublk_stop_work_fn);
2420 
2421 	ret = ublk_alloc_dev_number(ub, header->dev_id);
2422 	if (ret < 0)
2423 		goto out_free_ub;
2424 
2425 	memcpy(&ub->dev_info, &info, sizeof(info));
2426 
2427 	/* update device id */
2428 	ub->dev_info.dev_id = ub->ub_number;
2429 
2430 	/*
2431 	 * 64bit flags will be copied back to userspace as feature
2432 	 * negotiation result, so have to clear flags which driver
2433 	 * doesn't support yet, then userspace can get correct flags
2434 	 * (features) to handle.
2435 	 */
2436 	ub->dev_info.flags &= UBLK_F_ALL;
2437 
2438 	ub->dev_info.flags |= UBLK_F_CMD_IOCTL_ENCODE |
2439 		UBLK_F_URING_CMD_COMP_IN_TASK;
2440 
2441 	/* GET_DATA isn't needed any more with USER_COPY */
2442 	if (ublk_dev_is_user_copy(ub))
2443 		ub->dev_info.flags &= ~UBLK_F_NEED_GET_DATA;
2444 
2445 	/* Zoned storage support requires user copy feature */
2446 	if (ublk_dev_is_zoned(ub) &&
2447 	    (!IS_ENABLED(CONFIG_BLK_DEV_ZONED) || !ublk_dev_is_user_copy(ub))) {
2448 		ret = -EINVAL;
2449 		goto out_free_dev_number;
2450 	}
2451 
2452 	/* We are not ready to support zero copy */
2453 	ub->dev_info.flags &= ~UBLK_F_SUPPORT_ZERO_COPY;
2454 
2455 	ub->dev_info.nr_hw_queues = min_t(unsigned int,
2456 			ub->dev_info.nr_hw_queues, nr_cpu_ids);
2457 	ublk_align_max_io_size(ub);
2458 
2459 	ret = ublk_init_queues(ub);
2460 	if (ret)
2461 		goto out_free_dev_number;
2462 
2463 	ret = ublk_add_tag_set(ub);
2464 	if (ret)
2465 		goto out_deinit_queues;
2466 
2467 	ret = -EFAULT;
2468 	if (copy_to_user(argp, &ub->dev_info, sizeof(info)))
2469 		goto out_free_tag_set;
2470 
2471 	/*
2472 	 * Add the char dev so that ublksrv daemon can be setup.
2473 	 * ublk_add_chdev() will cleanup everything if it fails.
2474 	 */
2475 	ret = ublk_add_chdev(ub);
2476 	goto out_unlock;
2477 
2478 out_free_tag_set:
2479 	blk_mq_free_tag_set(&ub->tag_set);
2480 out_deinit_queues:
2481 	ublk_deinit_queues(ub);
2482 out_free_dev_number:
2483 	ublk_free_dev_number(ub);
2484 out_free_ub:
2485 	mutex_destroy(&ub->mutex);
2486 	kfree(ub);
2487 out_unlock:
2488 	mutex_unlock(&ublk_ctl_mutex);
2489 	return ret;
2490 }
2491 
2492 static inline bool ublk_idr_freed(int id)
2493 {
2494 	void *ptr;
2495 
2496 	spin_lock(&ublk_idr_lock);
2497 	ptr = idr_find(&ublk_index_idr, id);
2498 	spin_unlock(&ublk_idr_lock);
2499 
2500 	return ptr == NULL;
2501 }
2502 
2503 static int ublk_ctrl_del_dev(struct ublk_device **p_ub, bool wait)
2504 {
2505 	struct ublk_device *ub = *p_ub;
2506 	int idx = ub->ub_number;
2507 	int ret;
2508 
2509 	ret = mutex_lock_killable(&ublk_ctl_mutex);
2510 	if (ret)
2511 		return ret;
2512 
2513 	if (!test_bit(UB_STATE_DELETED, &ub->state)) {
2514 		ublk_remove(ub);
2515 		set_bit(UB_STATE_DELETED, &ub->state);
2516 	}
2517 
2518 	/* Mark the reference as consumed */
2519 	*p_ub = NULL;
2520 	ublk_put_device(ub);
2521 	mutex_unlock(&ublk_ctl_mutex);
2522 
2523 	/*
2524 	 * Wait until the idr is removed, then it can be reused after
2525 	 * DEL_DEV command is returned.
2526 	 *
2527 	 * If we returns because of user interrupt, future delete command
2528 	 * may come:
2529 	 *
2530 	 * - the device number isn't freed, this device won't or needn't
2531 	 *   be deleted again, since UB_STATE_DELETED is set, and device
2532 	 *   will be released after the last reference is dropped
2533 	 *
2534 	 * - the device number is freed already, we will not find this
2535 	 *   device via ublk_get_device_from_id()
2536 	 */
2537 	if (wait && wait_event_interruptible(ublk_idr_wq, ublk_idr_freed(idx)))
2538 		return -EINTR;
2539 	return 0;
2540 }
2541 
2542 static inline void ublk_ctrl_cmd_dump(struct io_uring_cmd *cmd)
2543 {
2544 	const struct ublksrv_ctrl_cmd *header = io_uring_sqe_cmd(cmd->sqe);
2545 
2546 	pr_devel("%s: cmd_op %x, dev id %d qid %d data %llx buf %llx len %u\n",
2547 			__func__, cmd->cmd_op, header->dev_id, header->queue_id,
2548 			header->data[0], header->addr, header->len);
2549 }
2550 
2551 static int ublk_ctrl_stop_dev(struct ublk_device *ub)
2552 {
2553 	ublk_stop_dev(ub);
2554 	cancel_work_sync(&ub->stop_work);
2555 	cancel_work_sync(&ub->quiesce_work);
2556 
2557 	return 0;
2558 }
2559 
2560 static int ublk_ctrl_get_dev_info(struct ublk_device *ub,
2561 		struct io_uring_cmd *cmd)
2562 {
2563 	const struct ublksrv_ctrl_cmd *header = io_uring_sqe_cmd(cmd->sqe);
2564 	void __user *argp = (void __user *)(unsigned long)header->addr;
2565 
2566 	if (header->len < sizeof(struct ublksrv_ctrl_dev_info) || !header->addr)
2567 		return -EINVAL;
2568 
2569 	if (copy_to_user(argp, &ub->dev_info, sizeof(ub->dev_info)))
2570 		return -EFAULT;
2571 
2572 	return 0;
2573 }
2574 
2575 /* TYPE_DEVT is readonly, so fill it up before returning to userspace */
2576 static void ublk_ctrl_fill_params_devt(struct ublk_device *ub)
2577 {
2578 	ub->params.devt.char_major = MAJOR(ub->cdev_dev.devt);
2579 	ub->params.devt.char_minor = MINOR(ub->cdev_dev.devt);
2580 
2581 	if (ub->ub_disk) {
2582 		ub->params.devt.disk_major = MAJOR(disk_devt(ub->ub_disk));
2583 		ub->params.devt.disk_minor = MINOR(disk_devt(ub->ub_disk));
2584 	} else {
2585 		ub->params.devt.disk_major = 0;
2586 		ub->params.devt.disk_minor = 0;
2587 	}
2588 	ub->params.types |= UBLK_PARAM_TYPE_DEVT;
2589 }
2590 
2591 static int ublk_ctrl_get_params(struct ublk_device *ub,
2592 		struct io_uring_cmd *cmd)
2593 {
2594 	const struct ublksrv_ctrl_cmd *header = io_uring_sqe_cmd(cmd->sqe);
2595 	void __user *argp = (void __user *)(unsigned long)header->addr;
2596 	struct ublk_params_header ph;
2597 	int ret;
2598 
2599 	if (header->len <= sizeof(ph) || !header->addr)
2600 		return -EINVAL;
2601 
2602 	if (copy_from_user(&ph, argp, sizeof(ph)))
2603 		return -EFAULT;
2604 
2605 	if (ph.len > header->len || !ph.len)
2606 		return -EINVAL;
2607 
2608 	if (ph.len > sizeof(struct ublk_params))
2609 		ph.len = sizeof(struct ublk_params);
2610 
2611 	mutex_lock(&ub->mutex);
2612 	ublk_ctrl_fill_params_devt(ub);
2613 	if (copy_to_user(argp, &ub->params, ph.len))
2614 		ret = -EFAULT;
2615 	else
2616 		ret = 0;
2617 	mutex_unlock(&ub->mutex);
2618 
2619 	return ret;
2620 }
2621 
2622 static int ublk_ctrl_set_params(struct ublk_device *ub,
2623 		struct io_uring_cmd *cmd)
2624 {
2625 	const struct ublksrv_ctrl_cmd *header = io_uring_sqe_cmd(cmd->sqe);
2626 	void __user *argp = (void __user *)(unsigned long)header->addr;
2627 	struct ublk_params_header ph;
2628 	int ret = -EFAULT;
2629 
2630 	if (header->len <= sizeof(ph) || !header->addr)
2631 		return -EINVAL;
2632 
2633 	if (copy_from_user(&ph, argp, sizeof(ph)))
2634 		return -EFAULT;
2635 
2636 	if (ph.len > header->len || !ph.len || !ph.types)
2637 		return -EINVAL;
2638 
2639 	if (ph.len > sizeof(struct ublk_params))
2640 		ph.len = sizeof(struct ublk_params);
2641 
2642 	/* parameters can only be changed when device isn't live */
2643 	mutex_lock(&ub->mutex);
2644 	if (ub->dev_info.state == UBLK_S_DEV_LIVE) {
2645 		ret = -EACCES;
2646 	} else if (copy_from_user(&ub->params, argp, ph.len)) {
2647 		ret = -EFAULT;
2648 	} else {
2649 		/* clear all we don't support yet */
2650 		ub->params.types &= UBLK_PARAM_TYPE_ALL;
2651 		ret = ublk_validate_params(ub);
2652 		if (ret)
2653 			ub->params.types = 0;
2654 	}
2655 	mutex_unlock(&ub->mutex);
2656 
2657 	return ret;
2658 }
2659 
2660 static void ublk_queue_reinit(struct ublk_device *ub, struct ublk_queue *ubq)
2661 {
2662 	int i;
2663 
2664 	WARN_ON_ONCE(!(ubq->ubq_daemon && ubq_daemon_is_dying(ubq)));
2665 
2666 	/* All old ioucmds have to be completed */
2667 	ubq->nr_io_ready = 0;
2668 	/* old daemon is PF_EXITING, put it now */
2669 	put_task_struct(ubq->ubq_daemon);
2670 	/* We have to reset it to NULL, otherwise ub won't accept new FETCH_REQ */
2671 	ubq->ubq_daemon = NULL;
2672 	ubq->timeout = false;
2673 	ubq->canceling = false;
2674 
2675 	for (i = 0; i < ubq->q_depth; i++) {
2676 		struct ublk_io *io = &ubq->ios[i];
2677 
2678 		/* forget everything now and be ready for new FETCH_REQ */
2679 		io->flags = 0;
2680 		io->cmd = NULL;
2681 		io->addr = 0;
2682 	}
2683 }
2684 
2685 static int ublk_ctrl_start_recovery(struct ublk_device *ub,
2686 		struct io_uring_cmd *cmd)
2687 {
2688 	const struct ublksrv_ctrl_cmd *header = io_uring_sqe_cmd(cmd->sqe);
2689 	int ret = -EINVAL;
2690 	int i;
2691 
2692 	mutex_lock(&ub->mutex);
2693 	if (!ublk_can_use_recovery(ub))
2694 		goto out_unlock;
2695 	if (!ub->nr_queues_ready)
2696 		goto out_unlock;
2697 	/*
2698 	 * START_RECOVERY is only allowd after:
2699 	 *
2700 	 * (1) UB_STATE_OPEN is not set, which means the dying process is exited
2701 	 *     and related io_uring ctx is freed so file struct of /dev/ublkcX is
2702 	 *     released.
2703 	 *
2704 	 * (2) UBLK_S_DEV_QUIESCED is set, which means the quiesce_work:
2705 	 *     (a)has quiesced request queue
2706 	 *     (b)has requeued every inflight rqs whose io_flags is ACTIVE
2707 	 *     (c)has requeued/aborted every inflight rqs whose io_flags is NOT ACTIVE
2708 	 *     (d)has completed/camceled all ioucmds owned by ther dying process
2709 	 */
2710 	if (test_bit(UB_STATE_OPEN, &ub->state) ||
2711 			ub->dev_info.state != UBLK_S_DEV_QUIESCED) {
2712 		ret = -EBUSY;
2713 		goto out_unlock;
2714 	}
2715 	pr_devel("%s: start recovery for dev id %d.\n", __func__, header->dev_id);
2716 	for (i = 0; i < ub->dev_info.nr_hw_queues; i++)
2717 		ublk_queue_reinit(ub, ublk_get_queue(ub, i));
2718 	/* set to NULL, otherwise new ubq_daemon cannot mmap the io_cmd_buf */
2719 	ub->mm = NULL;
2720 	ub->nr_queues_ready = 0;
2721 	ub->nr_privileged_daemon = 0;
2722 	init_completion(&ub->completion);
2723 	ret = 0;
2724  out_unlock:
2725 	mutex_unlock(&ub->mutex);
2726 	return ret;
2727 }
2728 
2729 static int ublk_ctrl_end_recovery(struct ublk_device *ub,
2730 		struct io_uring_cmd *cmd)
2731 {
2732 	const struct ublksrv_ctrl_cmd *header = io_uring_sqe_cmd(cmd->sqe);
2733 	int ublksrv_pid = (int)header->data[0];
2734 	int ret = -EINVAL;
2735 
2736 	pr_devel("%s: Waiting for new ubq_daemons(nr: %d) are ready, dev id %d...\n",
2737 			__func__, ub->dev_info.nr_hw_queues, header->dev_id);
2738 	/* wait until new ubq_daemon sending all FETCH_REQ */
2739 	if (wait_for_completion_interruptible(&ub->completion))
2740 		return -EINTR;
2741 
2742 	pr_devel("%s: All new ubq_daemons(nr: %d) are ready, dev id %d\n",
2743 			__func__, ub->dev_info.nr_hw_queues, header->dev_id);
2744 
2745 	mutex_lock(&ub->mutex);
2746 	if (!ublk_can_use_recovery(ub))
2747 		goto out_unlock;
2748 
2749 	if (ub->dev_info.state != UBLK_S_DEV_QUIESCED) {
2750 		ret = -EBUSY;
2751 		goto out_unlock;
2752 	}
2753 	ub->dev_info.ublksrv_pid = ublksrv_pid;
2754 	pr_devel("%s: new ublksrv_pid %d, dev id %d\n",
2755 			__func__, ublksrv_pid, header->dev_id);
2756 	blk_mq_unquiesce_queue(ub->ub_disk->queue);
2757 	pr_devel("%s: queue unquiesced, dev id %d.\n",
2758 			__func__, header->dev_id);
2759 	blk_mq_kick_requeue_list(ub->ub_disk->queue);
2760 	ub->dev_info.state = UBLK_S_DEV_LIVE;
2761 	ret = 0;
2762  out_unlock:
2763 	mutex_unlock(&ub->mutex);
2764 	return ret;
2765 }
2766 
2767 static int ublk_ctrl_get_features(struct io_uring_cmd *cmd)
2768 {
2769 	const struct ublksrv_ctrl_cmd *header = io_uring_sqe_cmd(cmd->sqe);
2770 	void __user *argp = (void __user *)(unsigned long)header->addr;
2771 	u64 features = UBLK_F_ALL & ~UBLK_F_SUPPORT_ZERO_COPY;
2772 
2773 	if (header->len != UBLK_FEATURES_LEN || !header->addr)
2774 		return -EINVAL;
2775 
2776 	if (copy_to_user(argp, &features, UBLK_FEATURES_LEN))
2777 		return -EFAULT;
2778 
2779 	return 0;
2780 }
2781 
2782 /*
2783  * All control commands are sent via /dev/ublk-control, so we have to check
2784  * the destination device's permission
2785  */
2786 static int ublk_char_dev_permission(struct ublk_device *ub,
2787 		const char *dev_path, int mask)
2788 {
2789 	int err;
2790 	struct path path;
2791 	struct kstat stat;
2792 
2793 	err = kern_path(dev_path, LOOKUP_FOLLOW, &path);
2794 	if (err)
2795 		return err;
2796 
2797 	err = vfs_getattr(&path, &stat, STATX_TYPE, AT_STATX_SYNC_AS_STAT);
2798 	if (err)
2799 		goto exit;
2800 
2801 	err = -EPERM;
2802 	if (stat.rdev != ub->cdev_dev.devt || !S_ISCHR(stat.mode))
2803 		goto exit;
2804 
2805 	err = inode_permission(&nop_mnt_idmap,
2806 			d_backing_inode(path.dentry), mask);
2807 exit:
2808 	path_put(&path);
2809 	return err;
2810 }
2811 
2812 static int ublk_ctrl_uring_cmd_permission(struct ublk_device *ub,
2813 		struct io_uring_cmd *cmd)
2814 {
2815 	struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)io_uring_sqe_cmd(cmd->sqe);
2816 	bool unprivileged = ub->dev_info.flags & UBLK_F_UNPRIVILEGED_DEV;
2817 	void __user *argp = (void __user *)(unsigned long)header->addr;
2818 	char *dev_path = NULL;
2819 	int ret = 0;
2820 	int mask;
2821 
2822 	if (!unprivileged) {
2823 		if (!capable(CAP_SYS_ADMIN))
2824 			return -EPERM;
2825 		/*
2826 		 * The new added command of UBLK_CMD_GET_DEV_INFO2 includes
2827 		 * char_dev_path in payload too, since userspace may not
2828 		 * know if the specified device is created as unprivileged
2829 		 * mode.
2830 		 */
2831 		if (_IOC_NR(cmd->cmd_op) != UBLK_CMD_GET_DEV_INFO2)
2832 			return 0;
2833 	}
2834 
2835 	/*
2836 	 * User has to provide the char device path for unprivileged ublk
2837 	 *
2838 	 * header->addr always points to the dev path buffer, and
2839 	 * header->dev_path_len records length of dev path buffer.
2840 	 */
2841 	if (!header->dev_path_len || header->dev_path_len > PATH_MAX)
2842 		return -EINVAL;
2843 
2844 	if (header->len < header->dev_path_len)
2845 		return -EINVAL;
2846 
2847 	dev_path = memdup_user_nul(argp, header->dev_path_len);
2848 	if (IS_ERR(dev_path))
2849 		return PTR_ERR(dev_path);
2850 
2851 	ret = -EINVAL;
2852 	switch (_IOC_NR(cmd->cmd_op)) {
2853 	case UBLK_CMD_GET_DEV_INFO:
2854 	case UBLK_CMD_GET_DEV_INFO2:
2855 	case UBLK_CMD_GET_QUEUE_AFFINITY:
2856 	case UBLK_CMD_GET_PARAMS:
2857 	case (_IOC_NR(UBLK_U_CMD_GET_FEATURES)):
2858 		mask = MAY_READ;
2859 		break;
2860 	case UBLK_CMD_START_DEV:
2861 	case UBLK_CMD_STOP_DEV:
2862 	case UBLK_CMD_ADD_DEV:
2863 	case UBLK_CMD_DEL_DEV:
2864 	case UBLK_CMD_SET_PARAMS:
2865 	case UBLK_CMD_START_USER_RECOVERY:
2866 	case UBLK_CMD_END_USER_RECOVERY:
2867 		mask = MAY_READ | MAY_WRITE;
2868 		break;
2869 	default:
2870 		goto exit;
2871 	}
2872 
2873 	ret = ublk_char_dev_permission(ub, dev_path, mask);
2874 	if (!ret) {
2875 		header->len -= header->dev_path_len;
2876 		header->addr += header->dev_path_len;
2877 	}
2878 	pr_devel("%s: dev id %d cmd_op %x uid %d gid %d path %s ret %d\n",
2879 			__func__, ub->ub_number, cmd->cmd_op,
2880 			ub->dev_info.owner_uid, ub->dev_info.owner_gid,
2881 			dev_path, ret);
2882 exit:
2883 	kfree(dev_path);
2884 	return ret;
2885 }
2886 
2887 static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd,
2888 		unsigned int issue_flags)
2889 {
2890 	const struct ublksrv_ctrl_cmd *header = io_uring_sqe_cmd(cmd->sqe);
2891 	struct ublk_device *ub = NULL;
2892 	u32 cmd_op = cmd->cmd_op;
2893 	int ret = -EINVAL;
2894 
2895 	if (issue_flags & IO_URING_F_NONBLOCK)
2896 		return -EAGAIN;
2897 
2898 	ublk_ctrl_cmd_dump(cmd);
2899 
2900 	if (!(issue_flags & IO_URING_F_SQE128))
2901 		goto out;
2902 
2903 	ret = ublk_check_cmd_op(cmd_op);
2904 	if (ret)
2905 		goto out;
2906 
2907 	if (cmd_op == UBLK_U_CMD_GET_FEATURES) {
2908 		ret = ublk_ctrl_get_features(cmd);
2909 		goto out;
2910 	}
2911 
2912 	if (_IOC_NR(cmd_op) != UBLK_CMD_ADD_DEV) {
2913 		ret = -ENODEV;
2914 		ub = ublk_get_device_from_id(header->dev_id);
2915 		if (!ub)
2916 			goto out;
2917 
2918 		ret = ublk_ctrl_uring_cmd_permission(ub, cmd);
2919 		if (ret)
2920 			goto put_dev;
2921 	}
2922 
2923 	switch (_IOC_NR(cmd_op)) {
2924 	case UBLK_CMD_START_DEV:
2925 		ret = ublk_ctrl_start_dev(ub, cmd);
2926 		break;
2927 	case UBLK_CMD_STOP_DEV:
2928 		ret = ublk_ctrl_stop_dev(ub);
2929 		break;
2930 	case UBLK_CMD_GET_DEV_INFO:
2931 	case UBLK_CMD_GET_DEV_INFO2:
2932 		ret = ublk_ctrl_get_dev_info(ub, cmd);
2933 		break;
2934 	case UBLK_CMD_ADD_DEV:
2935 		ret = ublk_ctrl_add_dev(cmd);
2936 		break;
2937 	case UBLK_CMD_DEL_DEV:
2938 		ret = ublk_ctrl_del_dev(&ub, true);
2939 		break;
2940 	case UBLK_CMD_DEL_DEV_ASYNC:
2941 		ret = ublk_ctrl_del_dev(&ub, false);
2942 		break;
2943 	case UBLK_CMD_GET_QUEUE_AFFINITY:
2944 		ret = ublk_ctrl_get_queue_affinity(ub, cmd);
2945 		break;
2946 	case UBLK_CMD_GET_PARAMS:
2947 		ret = ublk_ctrl_get_params(ub, cmd);
2948 		break;
2949 	case UBLK_CMD_SET_PARAMS:
2950 		ret = ublk_ctrl_set_params(ub, cmd);
2951 		break;
2952 	case UBLK_CMD_START_USER_RECOVERY:
2953 		ret = ublk_ctrl_start_recovery(ub, cmd);
2954 		break;
2955 	case UBLK_CMD_END_USER_RECOVERY:
2956 		ret = ublk_ctrl_end_recovery(ub, cmd);
2957 		break;
2958 	default:
2959 		ret = -ENOTSUPP;
2960 		break;
2961 	}
2962 
2963  put_dev:
2964 	if (ub)
2965 		ublk_put_device(ub);
2966  out:
2967 	io_uring_cmd_done(cmd, ret, 0, issue_flags);
2968 	pr_devel("%s: cmd done ret %d cmd_op %x, dev id %d qid %d\n",
2969 			__func__, ret, cmd->cmd_op, header->dev_id, header->queue_id);
2970 	return -EIOCBQUEUED;
2971 }
2972 
2973 static const struct file_operations ublk_ctl_fops = {
2974 	.open		= nonseekable_open,
2975 	.uring_cmd      = ublk_ctrl_uring_cmd,
2976 	.owner		= THIS_MODULE,
2977 	.llseek		= noop_llseek,
2978 };
2979 
2980 static struct miscdevice ublk_misc = {
2981 	.minor		= MISC_DYNAMIC_MINOR,
2982 	.name		= "ublk-control",
2983 	.fops		= &ublk_ctl_fops,
2984 };
2985 
2986 static int __init ublk_init(void)
2987 {
2988 	int ret;
2989 
2990 	BUILD_BUG_ON((u64)UBLKSRV_IO_BUF_OFFSET +
2991 			UBLKSRV_IO_BUF_TOTAL_SIZE < UBLKSRV_IO_BUF_OFFSET);
2992 
2993 	init_waitqueue_head(&ublk_idr_wq);
2994 
2995 	ret = misc_register(&ublk_misc);
2996 	if (ret)
2997 		return ret;
2998 
2999 	ret = alloc_chrdev_region(&ublk_chr_devt, 0, UBLK_MINORS, "ublk-char");
3000 	if (ret)
3001 		goto unregister_mis;
3002 
3003 	ret = class_register(&ublk_chr_class);
3004 	if (ret)
3005 		goto free_chrdev_region;
3006 
3007 	return 0;
3008 
3009 free_chrdev_region:
3010 	unregister_chrdev_region(ublk_chr_devt, UBLK_MINORS);
3011 unregister_mis:
3012 	misc_deregister(&ublk_misc);
3013 	return ret;
3014 }
3015 
3016 static void __exit ublk_exit(void)
3017 {
3018 	struct ublk_device *ub;
3019 	int id;
3020 
3021 	idr_for_each_entry(&ublk_index_idr, ub, id)
3022 		ublk_remove(ub);
3023 
3024 	class_unregister(&ublk_chr_class);
3025 	misc_deregister(&ublk_misc);
3026 
3027 	idr_destroy(&ublk_index_idr);
3028 	unregister_chrdev_region(ublk_chr_devt, UBLK_MINORS);
3029 }
3030 
3031 module_init(ublk_init);
3032 module_exit(ublk_exit);
3033 
3034 static int ublk_set_max_ublks(const char *buf, const struct kernel_param *kp)
3035 {
3036 	return param_set_uint_minmax(buf, kp, 0, UBLK_MAX_UBLKS);
3037 }
3038 
3039 static int ublk_get_max_ublks(char *buf, const struct kernel_param *kp)
3040 {
3041 	return sysfs_emit(buf, "%u\n", ublks_max);
3042 }
3043 
3044 static const struct kernel_param_ops ublk_max_ublks_ops = {
3045 	.set = ublk_set_max_ublks,
3046 	.get = ublk_get_max_ublks,
3047 };
3048 
3049 module_param_cb(ublks_max, &ublk_max_ublks_ops, &ublks_max, 0644);
3050 MODULE_PARM_DESC(ublks_max, "max number of ublk devices allowed to add(default: 64)");
3051 
3052 MODULE_AUTHOR("Ming Lei <ming.lei@redhat.com>");
3053 MODULE_DESCRIPTION("Userspace block device");
3054 MODULE_LICENSE("GPL");
3055