xref: /freebsd/sys/contrib/openzfs/module/os/linux/zfs/zvol_os.c (revision e7be843b4a162e68651d3911f0357ed464915629)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2012, 2020 by Delphix. All rights reserved.
24  * Copyright (c) 2024, Rob Norris <robn@despairlabs.com>
25  * Copyright (c) 2024, Klara, Inc.
26  */
27 
28 #include <sys/dataset_kstats.h>
29 #include <sys/dbuf.h>
30 #include <sys/dmu_traverse.h>
31 #include <sys/dsl_dataset.h>
32 #include <sys/dsl_prop.h>
33 #include <sys/dsl_dir.h>
34 #include <sys/zap.h>
35 #include <sys/zfeature.h>
36 #include <sys/zil_impl.h>
37 #include <sys/dmu_tx.h>
38 #include <sys/zio.h>
39 #include <sys/zfs_rlock.h>
40 #include <sys/spa_impl.h>
41 #include <sys/zvol.h>
42 #include <sys/zvol_impl.h>
43 #include <cityhash.h>
44 
45 #include <linux/blkdev_compat.h>
46 #include <linux/task_io_accounting_ops.h>
47 #include <linux/workqueue.h>
48 #include <linux/blk-mq.h>
49 
50 static void zvol_request_impl(zvol_state_t *zv, struct bio *bio,
51     struct request *rq, boolean_t force_sync);
52 
53 static unsigned int zvol_major = ZVOL_MAJOR;
54 static unsigned long zvol_max_discard_blocks = 16384;
55 
56 #ifndef HAVE_BLKDEV_GET_ERESTARTSYS
57 static unsigned int zvol_open_timeout_ms = 1000;
58 #endif
59 
60 static unsigned int zvol_blk_mq_threads = 0;
61 static unsigned int zvol_blk_mq_actual_threads;
62 static boolean_t zvol_use_blk_mq = B_FALSE;
63 
64 /*
65  * The maximum number of volblocksize blocks to process per thread.  Typically,
66  * write heavy workloads preform better with higher values here, and read
67  * heavy workloads preform better with lower values, but that's not a hard
68  * and fast rule.  It's basically a knob to tune between "less overhead with
69  * less parallelism" and "more overhead, but more parallelism".
70  *
71  * '8' was chosen as a reasonable, balanced, default based off of sequential
72  * read and write tests to a zvol in an NVMe pool (with 16 CPUs).
73  */
74 static unsigned int zvol_blk_mq_blocks_per_thread = 8;
75 
76 #ifndef	BLKDEV_DEFAULT_RQ
77 /* BLKDEV_MAX_RQ was renamed to BLKDEV_DEFAULT_RQ in the 5.16 kernel */
78 #define	BLKDEV_DEFAULT_RQ BLKDEV_MAX_RQ
79 #endif
80 
81 /*
82  * Finalize our BIO or request.
83  */
84 static inline void
85 zvol_end_io(struct bio *bio, struct request *rq, int error)
86 {
87 	if (bio) {
88 		bio->bi_status = errno_to_bi_status(-error);
89 		bio_endio(bio);
90 	} else {
91 		blk_mq_end_request(rq, errno_to_bi_status(error));
92 	}
93 }
94 
95 static unsigned int zvol_blk_mq_queue_depth = BLKDEV_DEFAULT_RQ;
96 static unsigned int zvol_actual_blk_mq_queue_depth;
97 
98 struct zvol_state_os {
99 	struct gendisk		*zvo_disk;	/* generic disk */
100 	struct request_queue	*zvo_queue;	/* request queue */
101 	dev_t			zvo_dev;	/* device id */
102 
103 	struct blk_mq_tag_set tag_set;
104 
105 	/* Set from the global 'zvol_use_blk_mq' at zvol load */
106 	boolean_t use_blk_mq;
107 };
108 
109 static struct ida zvol_ida;
110 
111 /*
112  * This is called when a new block multiqueue request comes in.  A request
113  * contains one or more BIOs.
114  */
115 static blk_status_t zvol_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
116     const struct blk_mq_queue_data *bd)
117 {
118 	struct request *rq = bd->rq;
119 	zvol_state_t *zv = rq->q->queuedata;
120 
121 	/* Tell the kernel that we are starting to process this request */
122 	blk_mq_start_request(rq);
123 
124 	if (blk_rq_is_passthrough(rq)) {
125 		/* Skip non filesystem request */
126 		blk_mq_end_request(rq, BLK_STS_IOERR);
127 		return (BLK_STS_IOERR);
128 	}
129 
130 	zvol_request_impl(zv, NULL, rq, 0);
131 
132 	/* Acknowledge to the kernel that we got this request */
133 	return (BLK_STS_OK);
134 }
135 
136 static struct blk_mq_ops zvol_blk_mq_queue_ops = {
137 	.queue_rq = zvol_mq_queue_rq,
138 };
139 
140 /* Initialize our blk-mq struct */
141 static int zvol_blk_mq_alloc_tag_set(zvol_state_t *zv)
142 {
143 	struct zvol_state_os *zso = zv->zv_zso;
144 
145 	memset(&zso->tag_set, 0, sizeof (zso->tag_set));
146 
147 	/* Initialize tag set. */
148 	zso->tag_set.ops = &zvol_blk_mq_queue_ops;
149 	zso->tag_set.nr_hw_queues = zvol_blk_mq_actual_threads;
150 	zso->tag_set.queue_depth = zvol_actual_blk_mq_queue_depth;
151 	zso->tag_set.numa_node = NUMA_NO_NODE;
152 	zso->tag_set.cmd_size = 0;
153 
154 	/*
155 	 * We need BLK_MQ_F_BLOCKING here since we do blocking calls in
156 	 * zvol_request_impl()
157 	 */
158 	zso->tag_set.flags = BLK_MQ_F_BLOCKING;
159 
160 #ifdef BLK_MQ_F_SHOULD_MERGE
161 	/*
162 	 * Linux 6.14 removed BLK_MQ_F_SHOULD_MERGE and made it implicit.
163 	 * For older kernels, we set it.
164 	 */
165 	zso->tag_set.flags |= BLK_MQ_F_SHOULD_MERGE;
166 #endif
167 
168 	zso->tag_set.driver_data = zv;
169 
170 	return (blk_mq_alloc_tag_set(&zso->tag_set));
171 }
172 
173 /*
174  * Given a path, return TRUE if path is a ZVOL.
175  */
176 boolean_t
177 zvol_os_is_zvol(const char *path)
178 {
179 	dev_t dev = 0;
180 
181 	if (vdev_lookup_bdev(path, &dev) != 0)
182 		return (B_FALSE);
183 
184 	if (MAJOR(dev) == zvol_major)
185 		return (B_TRUE);
186 
187 	return (B_FALSE);
188 }
189 
190 static void
191 zvol_write(zv_request_t *zvr)
192 {
193 	struct bio *bio = zvr->bio;
194 	struct request *rq = zvr->rq;
195 	int error = 0;
196 	zfs_uio_t uio;
197 	zvol_state_t *zv = zvr->zv;
198 	struct request_queue *q;
199 	struct gendisk *disk;
200 	unsigned long start_time = 0;
201 	boolean_t acct = B_FALSE;
202 
203 	ASSERT3P(zv, !=, NULL);
204 	ASSERT3U(zv->zv_open_count, >, 0);
205 	ASSERT3P(zv->zv_zilog, !=, NULL);
206 
207 	q = zv->zv_zso->zvo_queue;
208 	disk = zv->zv_zso->zvo_disk;
209 
210 	/* bio marked as FLUSH need to flush before write */
211 	if (io_is_flush(bio, rq))
212 		zil_commit(zv->zv_zilog, ZVOL_OBJ);
213 
214 	/* Some requests are just for flush and nothing else. */
215 	if (io_size(bio, rq) == 0) {
216 		rw_exit(&zv->zv_suspend_lock);
217 		zvol_end_io(bio, rq, 0);
218 		return;
219 	}
220 
221 	zfs_uio_bvec_init(&uio, bio, rq);
222 
223 	ssize_t start_resid = uio.uio_resid;
224 
225 	/*
226 	 * With use_blk_mq, accounting is done by blk_mq_start_request()
227 	 * and blk_mq_end_request(), so we can skip it here.
228 	 */
229 	if (bio) {
230 		acct = blk_queue_io_stat(q);
231 		if (acct) {
232 			start_time = blk_generic_start_io_acct(q, disk, WRITE,
233 			    bio);
234 		}
235 	}
236 
237 	boolean_t sync =
238 	    io_is_fua(bio, rq) || zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS;
239 
240 	zfs_locked_range_t *lr = zfs_rangelock_enter(&zv->zv_rangelock,
241 	    uio.uio_loffset, uio.uio_resid, RL_WRITER);
242 
243 	uint64_t volsize = zv->zv_volsize;
244 	while (uio.uio_resid > 0 && uio.uio_loffset < volsize) {
245 		uint64_t bytes = MIN(uio.uio_resid, DMU_MAX_ACCESS >> 1);
246 		uint64_t off = uio.uio_loffset;
247 		dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
248 
249 		if (bytes > volsize - off)	/* don't write past the end */
250 			bytes = volsize - off;
251 
252 		dmu_tx_hold_write_by_dnode(tx, zv->zv_dn, off, bytes);
253 
254 		/* This will only fail for ENOSPC */
255 		error = dmu_tx_assign(tx, DMU_TX_WAIT);
256 		if (error) {
257 			dmu_tx_abort(tx);
258 			break;
259 		}
260 		error = dmu_write_uio_dnode(zv->zv_dn, &uio, bytes, tx,
261 		    DMU_READ_PREFETCH);
262 		if (error == 0) {
263 			zvol_log_write(zv, tx, off, bytes, sync);
264 		}
265 		dmu_tx_commit(tx);
266 
267 		if (error)
268 			break;
269 	}
270 	zfs_rangelock_exit(lr);
271 
272 	int64_t nwritten = start_resid - uio.uio_resid;
273 	dataset_kstats_update_write_kstats(&zv->zv_kstat, nwritten);
274 	task_io_account_write(nwritten);
275 
276 	if (sync)
277 		zil_commit(zv->zv_zilog, ZVOL_OBJ);
278 
279 	rw_exit(&zv->zv_suspend_lock);
280 
281 	if (bio && acct) {
282 		blk_generic_end_io_acct(q, disk, WRITE, bio, start_time);
283 	}
284 
285 	zvol_end_io(bio, rq, -error);
286 }
287 
288 static void
289 zvol_write_task(void *arg)
290 {
291 	zv_request_task_t *task = arg;
292 	zvol_write(&task->zvr);
293 	zv_request_task_free(task);
294 }
295 
296 static void
297 zvol_discard(zv_request_t *zvr)
298 {
299 	struct bio *bio = zvr->bio;
300 	struct request *rq = zvr->rq;
301 	zvol_state_t *zv = zvr->zv;
302 	uint64_t start = io_offset(bio, rq);
303 	uint64_t size = io_size(bio, rq);
304 	uint64_t end = start + size;
305 	boolean_t sync;
306 	int error = 0;
307 	dmu_tx_t *tx;
308 	struct request_queue *q = zv->zv_zso->zvo_queue;
309 	struct gendisk *disk = zv->zv_zso->zvo_disk;
310 	unsigned long start_time = 0;
311 	boolean_t acct = B_FALSE;
312 
313 	ASSERT3P(zv, !=, NULL);
314 	ASSERT3U(zv->zv_open_count, >, 0);
315 	ASSERT3P(zv->zv_zilog, !=, NULL);
316 
317 	if (bio) {
318 		acct = blk_queue_io_stat(q);
319 		if (acct) {
320 			start_time = blk_generic_start_io_acct(q, disk, WRITE,
321 			    bio);
322 		}
323 	}
324 
325 	sync = io_is_fua(bio, rq) || zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS;
326 
327 	if (end > zv->zv_volsize) {
328 		error = SET_ERROR(EIO);
329 		goto unlock;
330 	}
331 
332 	/*
333 	 * Align the request to volume block boundaries when a secure erase is
334 	 * not required.  This will prevent dnode_free_range() from zeroing out
335 	 * the unaligned parts which is slow (read-modify-write) and useless
336 	 * since we are not freeing any space by doing so.
337 	 */
338 	if (!io_is_secure_erase(bio, rq)) {
339 		start = P2ROUNDUP(start, zv->zv_volblocksize);
340 		end = P2ALIGN_TYPED(end, zv->zv_volblocksize, uint64_t);
341 		size = end - start;
342 	}
343 
344 	if (start >= end)
345 		goto unlock;
346 
347 	zfs_locked_range_t *lr = zfs_rangelock_enter(&zv->zv_rangelock,
348 	    start, size, RL_WRITER);
349 
350 	tx = dmu_tx_create(zv->zv_objset);
351 	dmu_tx_mark_netfree(tx);
352 	error = dmu_tx_assign(tx, DMU_TX_WAIT);
353 	if (error != 0) {
354 		dmu_tx_abort(tx);
355 	} else {
356 		zvol_log_truncate(zv, tx, start, size);
357 		dmu_tx_commit(tx);
358 		error = dmu_free_long_range(zv->zv_objset,
359 		    ZVOL_OBJ, start, size);
360 	}
361 	zfs_rangelock_exit(lr);
362 
363 	if (error == 0 && sync)
364 		zil_commit(zv->zv_zilog, ZVOL_OBJ);
365 
366 unlock:
367 	rw_exit(&zv->zv_suspend_lock);
368 
369 	if (bio && acct) {
370 		blk_generic_end_io_acct(q, disk, WRITE, bio,
371 		    start_time);
372 	}
373 
374 	zvol_end_io(bio, rq, -error);
375 }
376 
377 static void
378 zvol_discard_task(void *arg)
379 {
380 	zv_request_task_t *task = arg;
381 	zvol_discard(&task->zvr);
382 	zv_request_task_free(task);
383 }
384 
385 static void
386 zvol_read(zv_request_t *zvr)
387 {
388 	struct bio *bio = zvr->bio;
389 	struct request *rq = zvr->rq;
390 	int error = 0;
391 	zfs_uio_t uio;
392 	boolean_t acct = B_FALSE;
393 	zvol_state_t *zv = zvr->zv;
394 	struct request_queue *q;
395 	struct gendisk *disk;
396 	unsigned long start_time = 0;
397 
398 	ASSERT3P(zv, !=, NULL);
399 	ASSERT3U(zv->zv_open_count, >, 0);
400 
401 	zfs_uio_bvec_init(&uio, bio, rq);
402 
403 	q = zv->zv_zso->zvo_queue;
404 	disk = zv->zv_zso->zvo_disk;
405 
406 	ssize_t start_resid = uio.uio_resid;
407 
408 	/*
409 	 * When blk-mq is being used, accounting is done by
410 	 * blk_mq_start_request() and blk_mq_end_request().
411 	 */
412 	if (bio) {
413 		acct = blk_queue_io_stat(q);
414 		if (acct)
415 			start_time = blk_generic_start_io_acct(q, disk, READ,
416 			    bio);
417 	}
418 
419 	zfs_locked_range_t *lr = zfs_rangelock_enter(&zv->zv_rangelock,
420 	    uio.uio_loffset, uio.uio_resid, RL_READER);
421 
422 	uint64_t volsize = zv->zv_volsize;
423 
424 	while (uio.uio_resid > 0 && uio.uio_loffset < volsize) {
425 		uint64_t bytes = MIN(uio.uio_resid, DMU_MAX_ACCESS >> 1);
426 
427 		/* don't read past the end */
428 		if (bytes > volsize - uio.uio_loffset)
429 			bytes = volsize - uio.uio_loffset;
430 
431 		error = dmu_read_uio_dnode(zv->zv_dn, &uio, bytes,
432 		    DMU_READ_PREFETCH);
433 		if (error) {
434 			/* convert checksum errors into IO errors */
435 			if (error == ECKSUM)
436 				error = SET_ERROR(EIO);
437 			break;
438 		}
439 	}
440 	zfs_rangelock_exit(lr);
441 
442 	int64_t nread = start_resid - uio.uio_resid;
443 	dataset_kstats_update_read_kstats(&zv->zv_kstat, nread);
444 	task_io_account_read(nread);
445 
446 	rw_exit(&zv->zv_suspend_lock);
447 
448 	if (bio && acct) {
449 		blk_generic_end_io_acct(q, disk, READ, bio, start_time);
450 	}
451 
452 	zvol_end_io(bio, rq, -error);
453 }
454 
455 static void
456 zvol_read_task(void *arg)
457 {
458 	zv_request_task_t *task = arg;
459 	zvol_read(&task->zvr);
460 	zv_request_task_free(task);
461 }
462 
463 
464 /*
465  * Process a BIO or request
466  *
467  * Either 'bio' or 'rq' should be set depending on if we are processing a
468  * bio or a request (both should not be set).
469  *
470  * force_sync:	Set to 0 to defer processing to a background taskq
471  *			Set to 1 to process data synchronously
472  */
473 static void
474 zvol_request_impl(zvol_state_t *zv, struct bio *bio, struct request *rq,
475     boolean_t force_sync)
476 {
477 	fstrans_cookie_t cookie = spl_fstrans_mark();
478 	uint64_t offset = io_offset(bio, rq);
479 	uint64_t size = io_size(bio, rq);
480 	int rw = io_data_dir(bio, rq);
481 
482 	if (unlikely(zv->zv_flags & ZVOL_REMOVING)) {
483 		zvol_end_io(bio, rq, -SET_ERROR(ENXIO));
484 		goto out;
485 	}
486 
487 	if (zvol_request_sync || zv->zv_threading == B_FALSE)
488 		force_sync = 1;
489 
490 	zv_request_t zvr = {
491 		.zv = zv,
492 		.bio = bio,
493 		.rq = rq,
494 	};
495 
496 	if (io_has_data(bio, rq) && offset + size > zv->zv_volsize) {
497 		printk(KERN_INFO "%s: bad access: offset=%llu, size=%lu\n",
498 		    zv->zv_zso->zvo_disk->disk_name,
499 		    (long long unsigned)offset,
500 		    (long unsigned)size);
501 
502 		zvol_end_io(bio, rq, -SET_ERROR(EIO));
503 		goto out;
504 	}
505 
506 	zv_request_task_t *task;
507 	zv_taskq_t *ztqs = &zvol_taskqs;
508 	uint_t blk_mq_hw_queue = 0;
509 	uint_t tq_idx;
510 	uint_t taskq_hash;
511 	if (rq)
512 #ifdef HAVE_BLK_MQ_RQ_HCTX
513 		blk_mq_hw_queue = rq->mq_hctx->queue_num;
514 #else
515 		blk_mq_hw_queue =
516 		    rq->q->queue_hw_ctx[rq->q->mq_map[rq->cpu]]->queue_num;
517 #endif
518 	taskq_hash = cityhash3((uintptr_t)zv, offset >> ZVOL_TASKQ_OFFSET_SHIFT,
519 	    blk_mq_hw_queue);
520 	tq_idx = taskq_hash % ztqs->tqs_cnt;
521 
522 	if (rw == WRITE) {
523 		if (unlikely(zv->zv_flags & ZVOL_RDONLY)) {
524 			zvol_end_io(bio, rq, -SET_ERROR(EROFS));
525 			goto out;
526 		}
527 
528 		/*
529 		 * Prevents the zvol from being suspended, or the ZIL being
530 		 * concurrently opened.  Will be released after the i/o
531 		 * completes.
532 		 */
533 		rw_enter(&zv->zv_suspend_lock, RW_READER);
534 
535 		/*
536 		 * Open a ZIL if this is the first time we have written to this
537 		 * zvol. We protect zv->zv_zilog with zv_suspend_lock rather
538 		 * than zv_state_lock so that we don't need to acquire an
539 		 * additional lock in this path.
540 		 */
541 		if (zv->zv_zilog == NULL) {
542 			rw_exit(&zv->zv_suspend_lock);
543 			rw_enter(&zv->zv_suspend_lock, RW_WRITER);
544 			if (zv->zv_zilog == NULL) {
545 				zv->zv_zilog = zil_open(zv->zv_objset,
546 				    zvol_get_data, &zv->zv_kstat.dk_zil_sums);
547 				zv->zv_flags |= ZVOL_WRITTEN_TO;
548 				/* replay / destroy done in zvol_create_minor */
549 				VERIFY0((zv->zv_zilog->zl_header->zh_flags &
550 				    ZIL_REPLAY_NEEDED));
551 			}
552 			rw_downgrade(&zv->zv_suspend_lock);
553 		}
554 
555 		/*
556 		 * We don't want this thread to be blocked waiting for i/o to
557 		 * complete, so we instead wait from a taskq callback. The
558 		 * i/o may be a ZIL write (via zil_commit()), or a read of an
559 		 * indirect block, or a read of a data block (if this is a
560 		 * partial-block write).  We will indicate that the i/o is
561 		 * complete by calling END_IO() from the taskq callback.
562 		 *
563 		 * This design allows the calling thread to continue and
564 		 * initiate more concurrent operations by calling
565 		 * zvol_request() again. There are typically only a small
566 		 * number of threads available to call zvol_request() (e.g.
567 		 * one per iSCSI target), so keeping the latency of
568 		 * zvol_request() low is important for performance.
569 		 *
570 		 * The zvol_request_sync module parameter allows this
571 		 * behavior to be altered, for performance evaluation
572 		 * purposes.  If the callback blocks, setting
573 		 * zvol_request_sync=1 will result in much worse performance.
574 		 *
575 		 * We can have up to zvol_threads concurrent i/o's being
576 		 * processed for all zvols on the system.  This is typically
577 		 * a vast improvement over the zvol_request_sync=1 behavior
578 		 * of one i/o at a time per zvol.  However, an even better
579 		 * design would be for zvol_request() to initiate the zio
580 		 * directly, and then be notified by the zio_done callback,
581 		 * which would call END_IO().  Unfortunately, the DMU/ZIL
582 		 * interfaces lack this functionality (they block waiting for
583 		 * the i/o to complete).
584 		 */
585 		if (io_is_discard(bio, rq) || io_is_secure_erase(bio, rq)) {
586 			if (force_sync) {
587 				zvol_discard(&zvr);
588 			} else {
589 				task = zv_request_task_create(zvr);
590 				taskq_dispatch_ent(ztqs->tqs_taskq[tq_idx],
591 				    zvol_discard_task, task, 0, &task->ent);
592 			}
593 		} else {
594 			if (force_sync) {
595 				zvol_write(&zvr);
596 			} else {
597 				task = zv_request_task_create(zvr);
598 				taskq_dispatch_ent(ztqs->tqs_taskq[tq_idx],
599 				    zvol_write_task, task, 0, &task->ent);
600 			}
601 		}
602 	} else {
603 		/*
604 		 * The SCST driver, and possibly others, may issue READ I/Os
605 		 * with a length of zero bytes.  These empty I/Os contain no
606 		 * data and require no additional handling.
607 		 */
608 		if (size == 0) {
609 			zvol_end_io(bio, rq, 0);
610 			goto out;
611 		}
612 
613 		rw_enter(&zv->zv_suspend_lock, RW_READER);
614 
615 		/* See comment in WRITE case above. */
616 		if (force_sync) {
617 			zvol_read(&zvr);
618 		} else {
619 			task = zv_request_task_create(zvr);
620 			taskq_dispatch_ent(ztqs->tqs_taskq[tq_idx],
621 			    zvol_read_task, task, 0, &task->ent);
622 		}
623 	}
624 
625 out:
626 	spl_fstrans_unmark(cookie);
627 }
628 
629 #ifdef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
630 #ifdef HAVE_BDEV_SUBMIT_BIO_RETURNS_VOID
631 static void
632 zvol_submit_bio(struct bio *bio)
633 #else
634 static blk_qc_t
635 zvol_submit_bio(struct bio *bio)
636 #endif
637 #else
638 static MAKE_REQUEST_FN_RET
639 zvol_request(struct request_queue *q, struct bio *bio)
640 #endif
641 {
642 #ifdef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
643 #if defined(HAVE_BIO_BDEV_DISK)
644 	struct request_queue *q = bio->bi_bdev->bd_disk->queue;
645 #else
646 	struct request_queue *q = bio->bi_disk->queue;
647 #endif
648 #endif
649 	zvol_state_t *zv = q->queuedata;
650 
651 	zvol_request_impl(zv, bio, NULL, 0);
652 #if defined(HAVE_MAKE_REQUEST_FN_RET_QC) || \
653 	defined(HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS) && \
654 	!defined(HAVE_BDEV_SUBMIT_BIO_RETURNS_VOID)
655 	return (BLK_QC_T_NONE);
656 #endif
657 }
658 
659 static int
660 #ifdef HAVE_BLK_MODE_T
661 zvol_open(struct gendisk *disk, blk_mode_t flag)
662 #else
663 zvol_open(struct block_device *bdev, fmode_t flag)
664 #endif
665 {
666 	zvol_state_t *zv;
667 	int error = 0;
668 	boolean_t drop_suspend = B_FALSE;
669 #ifndef HAVE_BLKDEV_GET_ERESTARTSYS
670 	hrtime_t timeout = MSEC2NSEC(zvol_open_timeout_ms);
671 	hrtime_t start = gethrtime();
672 
673 retry:
674 #endif
675 	rw_enter(&zvol_state_lock, RW_READER);
676 	/*
677 	 * Obtain a copy of private_data under the zvol_state_lock to make
678 	 * sure that either the result of zvol free code path setting
679 	 * disk->private_data to NULL is observed, or zvol_os_free()
680 	 * is not called on this zv because of the positive zv_open_count.
681 	 */
682 #ifdef HAVE_BLK_MODE_T
683 	zv = disk->private_data;
684 #else
685 	zv = bdev->bd_disk->private_data;
686 #endif
687 	if (zv == NULL) {
688 		rw_exit(&zvol_state_lock);
689 		return (-SET_ERROR(ENXIO));
690 	}
691 
692 	mutex_enter(&zv->zv_state_lock);
693 
694 	if (unlikely(zv->zv_flags & ZVOL_REMOVING)) {
695 		mutex_exit(&zv->zv_state_lock);
696 		rw_exit(&zvol_state_lock);
697 		return (-SET_ERROR(ENXIO));
698 	}
699 
700 	/*
701 	 * Make sure zvol is not suspended during first open
702 	 * (hold zv_suspend_lock) and respect proper lock acquisition
703 	 * ordering - zv_suspend_lock before zv_state_lock
704 	 */
705 	if (zv->zv_open_count == 0) {
706 		if (!rw_tryenter(&zv->zv_suspend_lock, RW_READER)) {
707 			mutex_exit(&zv->zv_state_lock);
708 			rw_enter(&zv->zv_suspend_lock, RW_READER);
709 			mutex_enter(&zv->zv_state_lock);
710 			/* check to see if zv_suspend_lock is needed */
711 			if (zv->zv_open_count != 0) {
712 				rw_exit(&zv->zv_suspend_lock);
713 			} else {
714 				drop_suspend = B_TRUE;
715 			}
716 		} else {
717 			drop_suspend = B_TRUE;
718 		}
719 	}
720 	rw_exit(&zvol_state_lock);
721 
722 	ASSERT(MUTEX_HELD(&zv->zv_state_lock));
723 
724 	if (zv->zv_open_count == 0) {
725 		boolean_t drop_namespace = B_FALSE;
726 
727 		ASSERT(RW_READ_HELD(&zv->zv_suspend_lock));
728 
729 		/*
730 		 * In all other call paths the spa_namespace_lock is taken
731 		 * before the bdev->bd_mutex lock.  However, on open(2)
732 		 * the __blkdev_get() function calls fops->open() with the
733 		 * bdev->bd_mutex lock held.  This can result in a deadlock
734 		 * when zvols from one pool are used as vdevs in another.
735 		 *
736 		 * To prevent a lock inversion deadlock we preemptively
737 		 * take the spa_namespace_lock.  Normally the lock will not
738 		 * be contended and this is safe because spa_open_common()
739 		 * handles the case where the caller already holds the
740 		 * spa_namespace_lock.
741 		 *
742 		 * When the lock cannot be aquired after multiple retries
743 		 * this must be the vdev on zvol deadlock case and we have
744 		 * no choice but to return an error.  For 5.12 and older
745 		 * kernels returning -ERESTARTSYS will result in the
746 		 * bdev->bd_mutex being dropped, then reacquired, and
747 		 * fops->open() being called again.  This process can be
748 		 * repeated safely until both locks are acquired.  For 5.13
749 		 * and newer the -ERESTARTSYS retry logic was removed from
750 		 * the kernel so the only option is to return the error for
751 		 * the caller to handle it.
752 		 */
753 		if (!mutex_owned(&spa_namespace_lock)) {
754 			if (!mutex_tryenter(&spa_namespace_lock)) {
755 				mutex_exit(&zv->zv_state_lock);
756 				rw_exit(&zv->zv_suspend_lock);
757 				drop_suspend = B_FALSE;
758 
759 #ifdef HAVE_BLKDEV_GET_ERESTARTSYS
760 				schedule();
761 				return (-SET_ERROR(ERESTARTSYS));
762 #else
763 				if ((gethrtime() - start) > timeout)
764 					return (-SET_ERROR(ERESTARTSYS));
765 
766 				schedule_timeout_interruptible(
767 					MSEC_TO_TICK(10));
768 				goto retry;
769 #endif
770 			} else {
771 				drop_namespace = B_TRUE;
772 			}
773 		}
774 
775 		error = -zvol_first_open(zv, !(blk_mode_is_open_write(flag)));
776 
777 		if (drop_namespace)
778 			mutex_exit(&spa_namespace_lock);
779 	}
780 
781 	if (error == 0) {
782 		if ((blk_mode_is_open_write(flag)) &&
783 		    (zv->zv_flags & ZVOL_RDONLY)) {
784 			if (zv->zv_open_count == 0)
785 				zvol_last_close(zv);
786 
787 			error = -SET_ERROR(EROFS);
788 		} else {
789 			zv->zv_open_count++;
790 		}
791 	}
792 
793 	mutex_exit(&zv->zv_state_lock);
794 	if (drop_suspend)
795 		rw_exit(&zv->zv_suspend_lock);
796 
797 	if (error == 0)
798 #ifdef HAVE_BLK_MODE_T
799 		disk_check_media_change(disk);
800 #else
801 		zfs_check_media_change(bdev);
802 #endif
803 
804 	return (error);
805 }
806 
807 static void
808 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_1ARG
809 zvol_release(struct gendisk *disk)
810 #else
811 zvol_release(struct gendisk *disk, fmode_t unused)
812 #endif
813 {
814 #if !defined(HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_1ARG)
815 	(void) unused;
816 #endif
817 	zvol_state_t *zv;
818 	boolean_t drop_suspend = B_TRUE;
819 
820 	rw_enter(&zvol_state_lock, RW_READER);
821 	zv = disk->private_data;
822 
823 	mutex_enter(&zv->zv_state_lock);
824 	ASSERT3U(zv->zv_open_count, >, 0);
825 	/*
826 	 * make sure zvol is not suspended during last close
827 	 * (hold zv_suspend_lock) and respect proper lock acquisition
828 	 * ordering - zv_suspend_lock before zv_state_lock
829 	 */
830 	if (zv->zv_open_count == 1) {
831 		if (!rw_tryenter(&zv->zv_suspend_lock, RW_READER)) {
832 			mutex_exit(&zv->zv_state_lock);
833 			rw_enter(&zv->zv_suspend_lock, RW_READER);
834 			mutex_enter(&zv->zv_state_lock);
835 			/* check to see if zv_suspend_lock is needed */
836 			if (zv->zv_open_count != 1) {
837 				rw_exit(&zv->zv_suspend_lock);
838 				drop_suspend = B_FALSE;
839 			}
840 		}
841 	} else {
842 		drop_suspend = B_FALSE;
843 	}
844 	rw_exit(&zvol_state_lock);
845 
846 	ASSERT(MUTEX_HELD(&zv->zv_state_lock));
847 
848 	zv->zv_open_count--;
849 	if (zv->zv_open_count == 0) {
850 		ASSERT(RW_READ_HELD(&zv->zv_suspend_lock));
851 		zvol_last_close(zv);
852 	}
853 
854 	mutex_exit(&zv->zv_state_lock);
855 
856 	if (drop_suspend)
857 		rw_exit(&zv->zv_suspend_lock);
858 }
859 
860 static int
861 zvol_ioctl(struct block_device *bdev, fmode_t mode,
862     unsigned int cmd, unsigned long arg)
863 {
864 	zvol_state_t *zv = bdev->bd_disk->private_data;
865 	int error = 0;
866 
867 	ASSERT3U(zv->zv_open_count, >, 0);
868 
869 	switch (cmd) {
870 	case BLKFLSBUF:
871 #ifdef HAVE_FSYNC_BDEV
872 		fsync_bdev(bdev);
873 #elif defined(HAVE_SYNC_BLOCKDEV)
874 		sync_blockdev(bdev);
875 #else
876 #error "Neither fsync_bdev() nor sync_blockdev() found"
877 #endif
878 		invalidate_bdev(bdev);
879 		rw_enter(&zv->zv_suspend_lock, RW_READER);
880 
881 		if (!(zv->zv_flags & ZVOL_RDONLY))
882 			txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
883 
884 		rw_exit(&zv->zv_suspend_lock);
885 		break;
886 
887 	case BLKZNAME:
888 		mutex_enter(&zv->zv_state_lock);
889 		error = copy_to_user((void *)arg, zv->zv_name, MAXNAMELEN);
890 		mutex_exit(&zv->zv_state_lock);
891 		break;
892 
893 	default:
894 		error = -ENOTTY;
895 		break;
896 	}
897 
898 	return (SET_ERROR(error));
899 }
900 
901 #ifdef CONFIG_COMPAT
902 static int
903 zvol_compat_ioctl(struct block_device *bdev, fmode_t mode,
904     unsigned cmd, unsigned long arg)
905 {
906 	return (zvol_ioctl(bdev, mode, cmd, arg));
907 }
908 #else
909 #define	zvol_compat_ioctl	NULL
910 #endif
911 
912 static unsigned int
913 zvol_check_events(struct gendisk *disk, unsigned int clearing)
914 {
915 	unsigned int mask = 0;
916 
917 	rw_enter(&zvol_state_lock, RW_READER);
918 
919 	zvol_state_t *zv = disk->private_data;
920 	if (zv != NULL) {
921 		mutex_enter(&zv->zv_state_lock);
922 		mask = zv->zv_changed ? DISK_EVENT_MEDIA_CHANGE : 0;
923 		zv->zv_changed = 0;
924 		mutex_exit(&zv->zv_state_lock);
925 	}
926 
927 	rw_exit(&zvol_state_lock);
928 
929 	return (mask);
930 }
931 
932 static int
933 zvol_revalidate_disk(struct gendisk *disk)
934 {
935 	rw_enter(&zvol_state_lock, RW_READER);
936 
937 	zvol_state_t *zv = disk->private_data;
938 	if (zv != NULL) {
939 		mutex_enter(&zv->zv_state_lock);
940 		set_capacity(zv->zv_zso->zvo_disk,
941 		    zv->zv_volsize >> SECTOR_BITS);
942 		mutex_exit(&zv->zv_state_lock);
943 	}
944 
945 	rw_exit(&zvol_state_lock);
946 
947 	return (0);
948 }
949 
950 int
951 zvol_os_update_volsize(zvol_state_t *zv, uint64_t volsize)
952 {
953 	struct gendisk *disk = zv->zv_zso->zvo_disk;
954 
955 #if defined(HAVE_REVALIDATE_DISK_SIZE)
956 	revalidate_disk_size(disk, zvol_revalidate_disk(disk) == 0);
957 #elif defined(HAVE_REVALIDATE_DISK)
958 	revalidate_disk(disk);
959 #else
960 	zvol_revalidate_disk(disk);
961 #endif
962 	return (0);
963 }
964 
965 void
966 zvol_os_clear_private(zvol_state_t *zv)
967 {
968 	/*
969 	 * Cleared while holding zvol_state_lock as a writer
970 	 * which will prevent zvol_open() from opening it.
971 	 */
972 	zv->zv_zso->zvo_disk->private_data = NULL;
973 }
974 
975 /*
976  * Provide a simple virtual geometry for legacy compatibility.  For devices
977  * smaller than 1 MiB a small head and sector count is used to allow very
978  * tiny devices.  For devices over 1 Mib a standard head and sector count
979  * is used to keep the cylinders count reasonable.
980  */
981 static int
982 zvol_getgeo(struct block_device *bdev, struct hd_geometry *geo)
983 {
984 	zvol_state_t *zv = bdev->bd_disk->private_data;
985 	sector_t sectors;
986 
987 	ASSERT3U(zv->zv_open_count, >, 0);
988 
989 	sectors = get_capacity(zv->zv_zso->zvo_disk);
990 
991 	if (sectors > 2048) {
992 		geo->heads = 16;
993 		geo->sectors = 63;
994 	} else {
995 		geo->heads = 2;
996 		geo->sectors = 4;
997 	}
998 
999 	geo->start = 0;
1000 	geo->cylinders = sectors / (geo->heads * geo->sectors);
1001 
1002 	return (0);
1003 }
1004 
1005 /*
1006  * Why have two separate block_device_operations structs?
1007  *
1008  * Normally we'd just have one, and assign 'submit_bio' as needed.  However,
1009  * it's possible the user's kernel is built with CONSTIFY_PLUGIN, meaning we
1010  * can't just change submit_bio dynamically at runtime.  So just create two
1011  * separate structs to get around this.
1012  */
1013 static const struct block_device_operations zvol_ops_blk_mq = {
1014 	.open			= zvol_open,
1015 	.release		= zvol_release,
1016 	.ioctl			= zvol_ioctl,
1017 	.compat_ioctl		= zvol_compat_ioctl,
1018 	.check_events		= zvol_check_events,
1019 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_REVALIDATE_DISK
1020 	.revalidate_disk	= zvol_revalidate_disk,
1021 #endif
1022 	.getgeo			= zvol_getgeo,
1023 	.owner			= THIS_MODULE,
1024 };
1025 
1026 static const struct block_device_operations zvol_ops = {
1027 	.open			= zvol_open,
1028 	.release		= zvol_release,
1029 	.ioctl			= zvol_ioctl,
1030 	.compat_ioctl		= zvol_compat_ioctl,
1031 	.check_events		= zvol_check_events,
1032 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_REVALIDATE_DISK
1033 	.revalidate_disk	= zvol_revalidate_disk,
1034 #endif
1035 	.getgeo			= zvol_getgeo,
1036 	.owner			= THIS_MODULE,
1037 #ifdef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
1038 	.submit_bio		= zvol_submit_bio,
1039 #endif
1040 };
1041 
1042 /*
1043  * Since 6.9, Linux has been removing queue limit setters in favour of an
1044  * initial queue_limits struct applied when the device is open. Since 6.11,
1045  * queue_limits is being extended to allow more things to be applied when the
1046  * device is open. Setters are also being removed for this.
1047  *
1048  * For OpenZFS, this means that depending on kernel version, some options may
1049  * be set up before the device is open, and some applied to an open device
1050  * (queue) after the fact.
1051  *
1052  * We manage this complexity by having our own limits struct,
1053  * zvol_queue_limits_t, in which we carry any queue config that we're
1054  * interested in setting. This structure is the same on all kernels.
1055  *
1056  * These limits are then applied to the queue at device open time by the most
1057  * appropriate method for the kernel.
1058  *
1059  * zvol_queue_limits_convert() is used on 6.9+ (where the two-arg form of
1060  * blk_alloc_disk() exists). This converts our limits struct to a proper Linux
1061  * struct queue_limits, and passes it in. Any fields added in later kernels are
1062  * (obviously) not set up here.
1063  *
1064  * zvol_queue_limits_apply() is called on all kernel versions after the queue
1065  * is created, and applies any remaining config. Before 6.9 that will be
1066  * everything, via setter methods. After 6.9 that will be whatever couldn't be
1067  * put into struct queue_limits. (This implies that zvol_queue_limits_apply()
1068  * will always be a no-op on the latest kernel we support).
1069  */
1070 typedef struct zvol_queue_limits {
1071 	unsigned int	zql_max_hw_sectors;
1072 	unsigned short	zql_max_segments;
1073 	unsigned int	zql_max_segment_size;
1074 	unsigned int	zql_io_opt;
1075 	unsigned int	zql_physical_block_size;
1076 	unsigned int	zql_max_discard_sectors;
1077 	unsigned int	zql_discard_granularity;
1078 } zvol_queue_limits_t;
1079 
1080 static void
1081 zvol_queue_limits_init(zvol_queue_limits_t *limits, zvol_state_t *zv,
1082     boolean_t use_blk_mq)
1083 {
1084 	limits->zql_max_hw_sectors = (DMU_MAX_ACCESS / 4) >> 9;
1085 
1086 	if (use_blk_mq) {
1087 		/*
1088 		 * IO requests can be really big (1MB).  When an IO request
1089 		 * comes in, it is passed off to zvol_read() or zvol_write()
1090 		 * in a new thread, where it is chunked up into 'volblocksize'
1091 		 * sized pieces and processed.  So for example, if the request
1092 		 * is a 1MB write and your volblocksize is 128k, one zvol_write
1093 		 * thread will take that request and sequentially do ten 128k
1094 		 * IOs.  This is due to the fact that the thread needs to lock
1095 		 * each volblocksize sized block.  So you might be wondering:
1096 		 * "instead of passing the whole 1MB request to one thread,
1097 		 * why not pass ten individual 128k chunks to ten threads and
1098 		 * process the whole write in parallel?"  The short answer is
1099 		 * that there's a sweet spot number of chunks that balances
1100 		 * the greater parallelism with the added overhead of more
1101 		 * threads. The sweet spot can be different depending on if you
1102 		 * have a read or write  heavy workload.  Writes typically want
1103 		 * high chunk counts while reads typically want lower ones.  On
1104 		 * a test pool with 6 NVMe drives in a 3x 2-disk mirror
1105 		 * configuration, with volblocksize=8k, the sweet spot for good
1106 		 * sequential reads and writes was at 8 chunks.
1107 		 */
1108 
1109 		/*
1110 		 * Below we tell the kernel how big we want our requests
1111 		 * to be.  You would think that blk_queue_io_opt() would be
1112 		 * used to do this since it is used to "set optimal request
1113 		 * size for the queue", but that doesn't seem to do
1114 		 * anything - the kernel still gives you huge requests
1115 		 * with tons of little PAGE_SIZE segments contained within it.
1116 		 *
1117 		 * Knowing that the kernel will just give you PAGE_SIZE segments
1118 		 * no matter what, you can say "ok, I want PAGE_SIZE byte
1119 		 * segments, and I want 'N' of them per request", where N is
1120 		 * the correct number of segments for the volblocksize and
1121 		 * number of chunks you want.
1122 		 */
1123 		if (zvol_blk_mq_blocks_per_thread != 0) {
1124 			unsigned int chunks;
1125 			chunks = MIN(zvol_blk_mq_blocks_per_thread, UINT16_MAX);
1126 
1127 			limits->zql_max_segment_size = PAGE_SIZE;
1128 			limits->zql_max_segments =
1129 			    (zv->zv_volblocksize * chunks) / PAGE_SIZE;
1130 		} else {
1131 			/*
1132 			 * Special case: zvol_blk_mq_blocks_per_thread = 0
1133 			 * Max everything out.
1134 			 */
1135 			limits->zql_max_segments = UINT16_MAX;
1136 			limits->zql_max_segment_size = UINT_MAX;
1137 		}
1138 	} else {
1139 		limits->zql_max_segments = UINT16_MAX;
1140 		limits->zql_max_segment_size = UINT_MAX;
1141 	}
1142 
1143 	limits->zql_io_opt = DMU_MAX_ACCESS / 2;
1144 
1145 	limits->zql_physical_block_size = zv->zv_volblocksize;
1146 	limits->zql_max_discard_sectors =
1147 	    (zvol_max_discard_blocks * zv->zv_volblocksize) >> 9;
1148 	limits->zql_discard_granularity = zv->zv_volblocksize;
1149 }
1150 
1151 #ifdef HAVE_BLK_ALLOC_DISK_2ARG
1152 static void
1153 zvol_queue_limits_convert(zvol_queue_limits_t *limits,
1154     struct queue_limits *qlimits)
1155 {
1156 	memset(qlimits, 0, sizeof (struct queue_limits));
1157 	qlimits->max_hw_sectors = limits->zql_max_hw_sectors;
1158 	qlimits->max_segments = limits->zql_max_segments;
1159 	qlimits->max_segment_size = limits->zql_max_segment_size;
1160 	qlimits->io_opt = limits->zql_io_opt;
1161 	qlimits->physical_block_size = limits->zql_physical_block_size;
1162 	qlimits->max_discard_sectors = limits->zql_max_discard_sectors;
1163 	qlimits->max_hw_discard_sectors = limits->zql_max_discard_sectors;
1164 	qlimits->discard_granularity = limits->zql_discard_granularity;
1165 #ifdef HAVE_BLKDEV_QUEUE_LIMITS_FEATURES
1166 	qlimits->features =
1167 	    BLK_FEAT_WRITE_CACHE | BLK_FEAT_FUA | BLK_FEAT_IO_STAT;
1168 #endif
1169 }
1170 #endif
1171 
1172 static void
1173 zvol_queue_limits_apply(zvol_queue_limits_t *limits,
1174     struct request_queue *queue)
1175 {
1176 #ifndef HAVE_BLK_ALLOC_DISK_2ARG
1177 	blk_queue_max_hw_sectors(queue, limits->zql_max_hw_sectors);
1178 	blk_queue_max_segments(queue, limits->zql_max_segments);
1179 	blk_queue_max_segment_size(queue, limits->zql_max_segment_size);
1180 	blk_queue_io_opt(queue, limits->zql_io_opt);
1181 	blk_queue_physical_block_size(queue, limits->zql_physical_block_size);
1182 	blk_queue_max_discard_sectors(queue, limits->zql_max_discard_sectors);
1183 	blk_queue_discard_granularity(queue, limits->zql_discard_granularity);
1184 #endif
1185 #ifndef HAVE_BLKDEV_QUEUE_LIMITS_FEATURES
1186 	blk_queue_set_write_cache(queue, B_TRUE);
1187 	blk_queue_flag_set(QUEUE_FLAG_IO_STAT, queue);
1188 #endif
1189 }
1190 
1191 static int
1192 zvol_alloc_non_blk_mq(struct zvol_state_os *zso, zvol_queue_limits_t *limits)
1193 {
1194 #if defined(HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS)
1195 #if defined(HAVE_BLK_ALLOC_DISK)
1196 	zso->zvo_disk = blk_alloc_disk(NUMA_NO_NODE);
1197 	if (zso->zvo_disk == NULL)
1198 		return (1);
1199 
1200 	zso->zvo_disk->minors = ZVOL_MINORS;
1201 	zso->zvo_queue = zso->zvo_disk->queue;
1202 #elif defined(HAVE_BLK_ALLOC_DISK_2ARG)
1203 	struct queue_limits qlimits;
1204 	zvol_queue_limits_convert(limits, &qlimits);
1205 	struct gendisk *disk = blk_alloc_disk(&qlimits, NUMA_NO_NODE);
1206 	if (IS_ERR(disk)) {
1207 		zso->zvo_disk = NULL;
1208 		return (1);
1209 	}
1210 
1211 	zso->zvo_disk = disk;
1212 	zso->zvo_disk->minors = ZVOL_MINORS;
1213 	zso->zvo_queue = zso->zvo_disk->queue;
1214 
1215 #else
1216 	zso->zvo_queue = blk_alloc_queue(NUMA_NO_NODE);
1217 	if (zso->zvo_queue == NULL)
1218 		return (1);
1219 
1220 	zso->zvo_disk = alloc_disk(ZVOL_MINORS);
1221 	if (zso->zvo_disk == NULL) {
1222 		blk_cleanup_queue(zso->zvo_queue);
1223 		return (1);
1224 	}
1225 
1226 	zso->zvo_disk->queue = zso->zvo_queue;
1227 #endif /* HAVE_BLK_ALLOC_DISK */
1228 #else
1229 	zso->zvo_queue = blk_generic_alloc_queue(zvol_request, NUMA_NO_NODE);
1230 	if (zso->zvo_queue == NULL)
1231 		return (1);
1232 
1233 	zso->zvo_disk = alloc_disk(ZVOL_MINORS);
1234 	if (zso->zvo_disk == NULL) {
1235 		blk_cleanup_queue(zso->zvo_queue);
1236 		return (1);
1237 	}
1238 
1239 	zso->zvo_disk->queue = zso->zvo_queue;
1240 #endif /* HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS */
1241 
1242 	zvol_queue_limits_apply(limits, zso->zvo_queue);
1243 
1244 	return (0);
1245 
1246 }
1247 
1248 static int
1249 zvol_alloc_blk_mq(zvol_state_t *zv, zvol_queue_limits_t *limits)
1250 {
1251 	struct zvol_state_os *zso = zv->zv_zso;
1252 
1253 	/* Allocate our blk-mq tag_set */
1254 	if (zvol_blk_mq_alloc_tag_set(zv) != 0)
1255 		return (1);
1256 
1257 #if defined(HAVE_BLK_ALLOC_DISK)
1258 	zso->zvo_disk = blk_mq_alloc_disk(&zso->tag_set, zv);
1259 	if (zso->zvo_disk == NULL) {
1260 		blk_mq_free_tag_set(&zso->tag_set);
1261 		return (1);
1262 	}
1263 	zso->zvo_queue = zso->zvo_disk->queue;
1264 	zso->zvo_disk->minors = ZVOL_MINORS;
1265 #elif defined(HAVE_BLK_ALLOC_DISK_2ARG)
1266 	struct queue_limits qlimits;
1267 	zvol_queue_limits_convert(limits, &qlimits);
1268 	struct gendisk *disk = blk_mq_alloc_disk(&zso->tag_set, &qlimits, zv);
1269 	if (IS_ERR(disk)) {
1270 		zso->zvo_disk = NULL;
1271 		blk_mq_free_tag_set(&zso->tag_set);
1272 		return (1);
1273 	}
1274 
1275 	zso->zvo_disk = disk;
1276 	zso->zvo_queue = zso->zvo_disk->queue;
1277 	zso->zvo_disk->minors = ZVOL_MINORS;
1278 #else
1279 	zso->zvo_disk = alloc_disk(ZVOL_MINORS);
1280 	if (zso->zvo_disk == NULL) {
1281 		blk_cleanup_queue(zso->zvo_queue);
1282 		blk_mq_free_tag_set(&zso->tag_set);
1283 		return (1);
1284 	}
1285 	/* Allocate queue */
1286 	zso->zvo_queue = blk_mq_init_queue(&zso->tag_set);
1287 	if (IS_ERR(zso->zvo_queue)) {
1288 		blk_mq_free_tag_set(&zso->tag_set);
1289 		return (1);
1290 	}
1291 
1292 	/* Our queue is now created, assign it to our disk */
1293 	zso->zvo_disk->queue = zso->zvo_queue;
1294 #endif
1295 
1296 	zvol_queue_limits_apply(limits, zso->zvo_queue);
1297 
1298 	return (0);
1299 }
1300 
1301 /*
1302  * Allocate memory for a new zvol_state_t and setup the required
1303  * request queue and generic disk structures for the block device.
1304  */
1305 static int
1306 zvol_alloc(dev_t dev, const char *name, uint64_t volsize, uint64_t volblocksize,
1307     zvol_state_t **zvp)
1308 {
1309 	zvol_state_t *zv;
1310 	struct zvol_state_os *zso;
1311 	uint64_t volmode;
1312 	int ret;
1313 
1314 	ret = dsl_prop_get_integer(name, "volmode", &volmode, NULL);
1315 	if (ret)
1316 		return (ret);
1317 
1318 	if (volmode == ZFS_VOLMODE_DEFAULT)
1319 		volmode = zvol_volmode;
1320 
1321 	if (volmode == ZFS_VOLMODE_NONE)
1322 		return (0);
1323 
1324 	zv = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
1325 	zso = kmem_zalloc(sizeof (struct zvol_state_os), KM_SLEEP);
1326 	zv->zv_zso = zso;
1327 	zv->zv_volmode = volmode;
1328 	zv->zv_volsize = volsize;
1329 	zv->zv_volblocksize = volblocksize;
1330 
1331 	list_link_init(&zv->zv_next);
1332 	mutex_init(&zv->zv_state_lock, NULL, MUTEX_DEFAULT, NULL);
1333 	cv_init(&zv->zv_removing_cv, NULL, CV_DEFAULT, NULL);
1334 
1335 	zv->zv_zso->use_blk_mq = zvol_use_blk_mq;
1336 
1337 	zvol_queue_limits_t limits;
1338 	zvol_queue_limits_init(&limits, zv, zv->zv_zso->use_blk_mq);
1339 
1340 	/*
1341 	 * The block layer has 3 interfaces for getting BIOs:
1342 	 *
1343 	 * 1. blk-mq request queues (new)
1344 	 * 2. submit_bio() (oldest)
1345 	 * 3. regular request queues (old).
1346 	 *
1347 	 * Each of those interfaces has two permutations:
1348 	 *
1349 	 * a) We have blk_alloc_disk()/blk_mq_alloc_disk(), which allocates
1350 	 *    both the disk and its queue (5.14 kernel or newer)
1351 	 *
1352 	 * b) We don't have blk_*alloc_disk(), and have to allocate the
1353 	 *    disk and the queue separately. (5.13 kernel or older)
1354 	 */
1355 	if (zv->zv_zso->use_blk_mq) {
1356 		ret = zvol_alloc_blk_mq(zv, &limits);
1357 		if (ret != 0)
1358 			goto out_kmem;
1359 		zso->zvo_disk->fops = &zvol_ops_blk_mq;
1360 	} else {
1361 		ret = zvol_alloc_non_blk_mq(zso, &limits);
1362 		if (ret != 0)
1363 			goto out_kmem;
1364 		zso->zvo_disk->fops = &zvol_ops;
1365 	}
1366 
1367 	/* Limit read-ahead to a single page to prevent over-prefetching. */
1368 	blk_queue_set_read_ahead(zso->zvo_queue, 1);
1369 
1370 	if (!zv->zv_zso->use_blk_mq) {
1371 		/* Disable write merging in favor of the ZIO pipeline. */
1372 		blk_queue_flag_set(QUEUE_FLAG_NOMERGES, zso->zvo_queue);
1373 	}
1374 
1375 	zso->zvo_queue->queuedata = zv;
1376 	zso->zvo_dev = dev;
1377 	zv->zv_open_count = 0;
1378 	strlcpy(zv->zv_name, name, sizeof (zv->zv_name));
1379 
1380 	zfs_rangelock_init(&zv->zv_rangelock, NULL, NULL);
1381 	rw_init(&zv->zv_suspend_lock, NULL, RW_DEFAULT, NULL);
1382 
1383 	zso->zvo_disk->major = zvol_major;
1384 	zso->zvo_disk->events = DISK_EVENT_MEDIA_CHANGE;
1385 
1386 	/*
1387 	 * Setting ZFS_VOLMODE_DEV disables partitioning on ZVOL devices.
1388 	 * This is accomplished by limiting the number of minors for the
1389 	 * device to one and explicitly disabling partition scanning.
1390 	 */
1391 	if (volmode == ZFS_VOLMODE_DEV) {
1392 		zso->zvo_disk->minors = 1;
1393 		zso->zvo_disk->flags &= ~GENHD_FL_EXT_DEVT;
1394 		zso->zvo_disk->flags |= GENHD_FL_NO_PART;
1395 	}
1396 
1397 	zso->zvo_disk->first_minor = (dev & MINORMASK);
1398 	zso->zvo_disk->private_data = zv;
1399 	snprintf(zso->zvo_disk->disk_name, DISK_NAME_LEN, "%s%d",
1400 	    ZVOL_DEV_NAME, (dev & MINORMASK));
1401 
1402 	*zvp = zv;
1403 	return (ret);
1404 
1405 out_kmem:
1406 	kmem_free(zso, sizeof (struct zvol_state_os));
1407 	kmem_free(zv, sizeof (zvol_state_t));
1408 	return (ret);
1409 }
1410 
1411 /*
1412  * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1413  * At this time, the structure is not opened by anyone, is taken off
1414  * the zvol_state_list, and has its private data set to NULL.
1415  * The zvol_state_lock is dropped.
1416  *
1417  * This function may take many milliseconds to complete (e.g. we've seen
1418  * it take over 256ms), due to the calls to "blk_cleanup_queue" and
1419  * "del_gendisk". Thus, consumers need to be careful to account for this
1420  * latency when calling this function.
1421  */
1422 void
1423 zvol_os_free(zvol_state_t *zv)
1424 {
1425 
1426 	ASSERT(!RW_LOCK_HELD(&zv->zv_suspend_lock));
1427 	ASSERT(!MUTEX_HELD(&zv->zv_state_lock));
1428 	ASSERT0(zv->zv_open_count);
1429 	ASSERT3P(zv->zv_zso->zvo_disk->private_data, ==, NULL);
1430 
1431 	rw_destroy(&zv->zv_suspend_lock);
1432 	zfs_rangelock_fini(&zv->zv_rangelock);
1433 
1434 	del_gendisk(zv->zv_zso->zvo_disk);
1435 #if defined(HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS) && \
1436 	(defined(HAVE_BLK_ALLOC_DISK) || defined(HAVE_BLK_ALLOC_DISK_2ARG))
1437 #if defined(HAVE_BLK_CLEANUP_DISK)
1438 	blk_cleanup_disk(zv->zv_zso->zvo_disk);
1439 #else
1440 	put_disk(zv->zv_zso->zvo_disk);
1441 #endif
1442 #else
1443 	blk_cleanup_queue(zv->zv_zso->zvo_queue);
1444 	put_disk(zv->zv_zso->zvo_disk);
1445 #endif
1446 
1447 	if (zv->zv_zso->use_blk_mq)
1448 		blk_mq_free_tag_set(&zv->zv_zso->tag_set);
1449 
1450 	ida_simple_remove(&zvol_ida,
1451 	    MINOR(zv->zv_zso->zvo_dev) >> ZVOL_MINOR_BITS);
1452 
1453 	cv_destroy(&zv->zv_removing_cv);
1454 	mutex_destroy(&zv->zv_state_lock);
1455 	dataset_kstats_destroy(&zv->zv_kstat);
1456 
1457 	kmem_free(zv->zv_zso, sizeof (struct zvol_state_os));
1458 	kmem_free(zv, sizeof (zvol_state_t));
1459 }
1460 
1461 void
1462 zvol_wait_close(zvol_state_t *zv)
1463 {
1464 }
1465 
1466 struct add_disk_work {
1467 	struct delayed_work work;
1468 	struct gendisk *disk;
1469 	int error;
1470 };
1471 
1472 static int
1473 __zvol_os_add_disk(struct gendisk *disk)
1474 {
1475 	int error = 0;
1476 #ifdef HAVE_ADD_DISK_RET
1477 	error = add_disk(disk);
1478 #else
1479 	add_disk(disk);
1480 #endif
1481 	return (error);
1482 }
1483 
1484 #if defined(HAVE_BDEV_FILE_OPEN_BY_PATH)
1485 static void
1486 zvol_os_add_disk_work(struct work_struct *work)
1487 {
1488 	struct add_disk_work *add_disk_work;
1489 	add_disk_work = container_of(work, struct add_disk_work, work.work);
1490 	add_disk_work->error = __zvol_os_add_disk(add_disk_work->disk);
1491 }
1492 #endif
1493 
1494 /*
1495  * SPECIAL CASE:
1496  *
1497  * This function basically calls add_disk() from a workqueue.   You may be
1498  * thinking: why not just call add_disk() directly?
1499  *
1500  * When you call add_disk(), the zvol appears to the world.  When this happens,
1501  * the kernel calls disk_scan_partitions() on the zvol, which behaves
1502  * differently on the 6.9+ kernels:
1503  *
1504  * - 6.8 and older kernels -
1505  * disk_scan_partitions()
1506  *	handle = bdev_open_by_dev(
1507  *		zvol_open()
1508  *	bdev_release(handle);
1509  *		zvol_release()
1510  *
1511  *
1512  * - 6.9+ kernels -
1513  * disk_scan_partitions()
1514  * 	file = bdev_file_open_by_dev()
1515  *		zvol_open()
1516  *	fput(file)
1517  *	< wait for return to userspace >
1518  *		zvol_release()
1519  *
1520  * The difference is that the bdev_release() from the 6.8 kernel is synchronous
1521  * while the fput() from the 6.9 kernel is async.  Or more specifically it's
1522  * async that has to wait until we return to userspace (since it adds the fput
1523  * into the caller's work queue with the TWA_RESUME flag set).  This is not the
1524  * behavior we want, since we want do things like create+destroy a zvol within
1525  * a single ZFS_IOC_CREATE ioctl, and the "create" part needs to release the
1526  * reference to the zvol while we're in the IOCTL, which can't wait until we
1527  * return to userspace.
1528  *
1529  * We can get around this since fput() has a special codepath for when it's
1530  * running in a kernel thread or interrupt.  In those cases, it just puts the
1531  * fput into the system workqueue, which we can force to run with
1532  * __flush_workqueue().  That is why we call add_disk() from a workqueue - so it
1533  * run from a kernel thread and "tricks" the fput() codepaths.
1534  *
1535  * Note that __flush_workqueue() is slowly getting deprecated.  This may be ok
1536  * though, since our IOCTL will spin on EBUSY waiting for the zvol release (via
1537  * fput) to happen, which it eventually, naturally, will from the system_wq
1538  * without us explicitly calling __flush_workqueue().
1539  */
1540 static int
1541 zvol_os_add_disk(struct gendisk *disk)
1542 {
1543 #if defined(HAVE_BDEV_FILE_OPEN_BY_PATH)	/* 6.9+ kernel */
1544 	struct add_disk_work add_disk_work;
1545 
1546 	INIT_DELAYED_WORK(&add_disk_work.work, zvol_os_add_disk_work);
1547 	add_disk_work.disk = disk;
1548 	add_disk_work.error = 0;
1549 
1550 	/* Use *_delayed_work functions since they're not GPL'd */
1551 	schedule_delayed_work(&add_disk_work.work, 0);
1552 	flush_delayed_work(&add_disk_work.work);
1553 
1554 	__flush_workqueue(system_wq);
1555 	return (add_disk_work.error);
1556 #else	/* <= 6.8 kernel */
1557 	return (__zvol_os_add_disk(disk));
1558 #endif
1559 }
1560 
1561 /*
1562  * Create a block device minor node and setup the linkage between it
1563  * and the specified volume.  Once this function returns the block
1564  * device is live and ready for use.
1565  */
1566 int
1567 zvol_os_create_minor(const char *name)
1568 {
1569 	zvol_state_t *zv = NULL;
1570 	objset_t *os;
1571 	dmu_object_info_t *doi;
1572 	uint64_t volsize;
1573 	uint64_t len;
1574 	unsigned minor = 0;
1575 	int error = 0;
1576 	int idx;
1577 	uint64_t hash = zvol_name_hash(name);
1578 	uint64_t volthreading;
1579 	bool replayed_zil = B_FALSE;
1580 
1581 	if (zvol_inhibit_dev)
1582 		return (0);
1583 
1584 	idx = ida_simple_get(&zvol_ida, 0, 0, kmem_flags_convert(KM_SLEEP));
1585 	if (idx < 0)
1586 		return (SET_ERROR(-idx));
1587 	minor = idx << ZVOL_MINOR_BITS;
1588 	if (MINOR(minor) != minor) {
1589 		/* too many partitions can cause an overflow */
1590 		zfs_dbgmsg("zvol: create minor overflow: %s, minor %u/%u",
1591 		    name, minor, MINOR(minor));
1592 		ida_simple_remove(&zvol_ida, idx);
1593 		return (SET_ERROR(EINVAL));
1594 	}
1595 
1596 	zv = zvol_find_by_name_hash(name, hash, RW_NONE);
1597 	if (zv) {
1598 		ASSERT(MUTEX_HELD(&zv->zv_state_lock));
1599 		mutex_exit(&zv->zv_state_lock);
1600 		ida_simple_remove(&zvol_ida, idx);
1601 		return (SET_ERROR(EEXIST));
1602 	}
1603 
1604 	doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
1605 
1606 	error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, B_TRUE, FTAG, &os);
1607 	if (error)
1608 		goto out_doi;
1609 
1610 	error = dmu_object_info(os, ZVOL_OBJ, doi);
1611 	if (error)
1612 		goto out_dmu_objset_disown;
1613 
1614 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
1615 	if (error)
1616 		goto out_dmu_objset_disown;
1617 
1618 	error = zvol_alloc(MKDEV(zvol_major, minor), name,
1619 	    volsize, doi->doi_data_block_size, &zv);
1620 	if (error || zv == NULL)
1621 		goto out_dmu_objset_disown;
1622 
1623 	zv->zv_hash = hash;
1624 
1625 	if (dmu_objset_is_snapshot(os))
1626 		zv->zv_flags |= ZVOL_RDONLY;
1627 
1628 	zv->zv_objset = os;
1629 
1630 	/* Default */
1631 	zv->zv_threading = B_TRUE;
1632 	if (dsl_prop_get_integer(name, "volthreading", &volthreading, NULL)
1633 	    == 0)
1634 		zv->zv_threading = volthreading;
1635 
1636 	set_capacity(zv->zv_zso->zvo_disk, zv->zv_volsize >> 9);
1637 
1638 #ifdef QUEUE_FLAG_DISCARD
1639 	blk_queue_flag_set(QUEUE_FLAG_DISCARD, zv->zv_zso->zvo_queue);
1640 #endif
1641 #ifdef QUEUE_FLAG_NONROT
1642 	blk_queue_flag_set(QUEUE_FLAG_NONROT, zv->zv_zso->zvo_queue);
1643 #endif
1644 #ifdef QUEUE_FLAG_ADD_RANDOM
1645 	blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, zv->zv_zso->zvo_queue);
1646 #endif
1647 	/* This flag was introduced in kernel version 4.12. */
1648 #ifdef QUEUE_FLAG_SCSI_PASSTHROUGH
1649 	blk_queue_flag_set(QUEUE_FLAG_SCSI_PASSTHROUGH, zv->zv_zso->zvo_queue);
1650 #endif
1651 
1652 	ASSERT3P(zv->zv_kstat.dk_kstats, ==, NULL);
1653 	error = dataset_kstats_create(&zv->zv_kstat, zv->zv_objset);
1654 	if (error)
1655 		goto out_dmu_objset_disown;
1656 	ASSERT3P(zv->zv_zilog, ==, NULL);
1657 	zv->zv_zilog = zil_open(os, zvol_get_data, &zv->zv_kstat.dk_zil_sums);
1658 	if (spa_writeable(dmu_objset_spa(os))) {
1659 		if (zil_replay_disable)
1660 			replayed_zil = zil_destroy(zv->zv_zilog, B_FALSE);
1661 		else
1662 			replayed_zil = zil_replay(os, zv, zvol_replay_vector);
1663 	}
1664 	if (replayed_zil)
1665 		zil_close(zv->zv_zilog);
1666 	zv->zv_zilog = NULL;
1667 
1668 	/*
1669 	 * When udev detects the addition of the device it will immediately
1670 	 * invoke blkid(8) to determine the type of content on the device.
1671 	 * Prefetching the blocks commonly scanned by blkid(8) will speed
1672 	 * up this process.
1673 	 */
1674 	len = MIN(zvol_prefetch_bytes, SPA_MAXBLOCKSIZE);
1675 	if (len > 0) {
1676 		dmu_prefetch(os, ZVOL_OBJ, 0, 0, len, ZIO_PRIORITY_SYNC_READ);
1677 		dmu_prefetch(os, ZVOL_OBJ, 0, volsize - len, len,
1678 		    ZIO_PRIORITY_SYNC_READ);
1679 	}
1680 
1681 	zv->zv_objset = NULL;
1682 out_dmu_objset_disown:
1683 	dmu_objset_disown(os, B_TRUE, FTAG);
1684 out_doi:
1685 	kmem_free(doi, sizeof (dmu_object_info_t));
1686 
1687 	/*
1688 	 * Keep in mind that once add_disk() is called, the zvol is
1689 	 * announced to the world, and zvol_open()/zvol_release() can
1690 	 * be called at any time. Incidentally, add_disk() itself calls
1691 	 * zvol_open()->zvol_first_open() and zvol_release()->zvol_last_close()
1692 	 * directly as well.
1693 	 */
1694 	if (error == 0 && zv) {
1695 		rw_enter(&zvol_state_lock, RW_WRITER);
1696 		zvol_insert(zv);
1697 		rw_exit(&zvol_state_lock);
1698 		error = zvol_os_add_disk(zv->zv_zso->zvo_disk);
1699 	} else {
1700 		ida_simple_remove(&zvol_ida, idx);
1701 	}
1702 
1703 	return (error);
1704 }
1705 
1706 int
1707 zvol_os_rename_minor(zvol_state_t *zv, const char *newname)
1708 {
1709 	int readonly = get_disk_ro(zv->zv_zso->zvo_disk);
1710 
1711 	ASSERT(RW_LOCK_HELD(&zvol_state_lock));
1712 	ASSERT(MUTEX_HELD(&zv->zv_state_lock));
1713 
1714 	strlcpy(zv->zv_name, newname, sizeof (zv->zv_name));
1715 
1716 	/* move to new hashtable entry  */
1717 	zv->zv_hash = zvol_name_hash(newname);
1718 	hlist_del(&zv->zv_hlink);
1719 	hlist_add_head(&zv->zv_hlink, ZVOL_HT_HEAD(zv->zv_hash));
1720 
1721 	/*
1722 	 * The block device's read-only state is briefly changed causing
1723 	 * a KOBJ_CHANGE uevent to be issued.  This ensures udev detects
1724 	 * the name change and fixes the symlinks.  This does not change
1725 	 * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
1726 	 * changes.  This would normally be done using kobject_uevent() but
1727 	 * that is a GPL-only symbol which is why we need this workaround.
1728 	 */
1729 	set_disk_ro(zv->zv_zso->zvo_disk, !readonly);
1730 	set_disk_ro(zv->zv_zso->zvo_disk, readonly);
1731 
1732 	dataset_kstats_rename(&zv->zv_kstat, newname);
1733 
1734 	return (0);
1735 }
1736 
1737 void
1738 zvol_os_set_disk_ro(zvol_state_t *zv, int flags)
1739 {
1740 
1741 	set_disk_ro(zv->zv_zso->zvo_disk, flags);
1742 }
1743 
1744 void
1745 zvol_os_set_capacity(zvol_state_t *zv, uint64_t capacity)
1746 {
1747 
1748 	set_capacity(zv->zv_zso->zvo_disk, capacity);
1749 }
1750 
1751 int
1752 zvol_init(void)
1753 {
1754 	int error;
1755 
1756 	error = zvol_init_impl();
1757 	if (error) {
1758 		printk(KERN_INFO "ZFS: zvol_init_impl() failed %d\n", error);
1759 		return (error);
1760 	}
1761 
1762 	error = register_blkdev(zvol_major, ZVOL_DRIVER);
1763 	if (error) {
1764 		printk(KERN_INFO "ZFS: register_blkdev() failed %d\n", error);
1765 		return (error);
1766 	}
1767 
1768 	if (zvol_blk_mq_queue_depth == 0) {
1769 		zvol_actual_blk_mq_queue_depth = BLKDEV_DEFAULT_RQ;
1770 	} else {
1771 		zvol_actual_blk_mq_queue_depth =
1772 		    MAX(zvol_blk_mq_queue_depth, BLKDEV_MIN_RQ);
1773 	}
1774 
1775 	if (zvol_blk_mq_threads == 0) {
1776 		zvol_blk_mq_actual_threads = num_online_cpus();
1777 	} else {
1778 		zvol_blk_mq_actual_threads = MIN(MAX(zvol_blk_mq_threads, 1),
1779 		    1024);
1780 	}
1781 
1782 	ida_init(&zvol_ida);
1783 	return (0);
1784 }
1785 
1786 void
1787 zvol_fini(void)
1788 {
1789 	unregister_blkdev(zvol_major, ZVOL_DRIVER);
1790 
1791 	zvol_fini_impl();
1792 
1793 	ida_destroy(&zvol_ida);
1794 }
1795 
1796 module_param(zvol_major, uint, 0444);
1797 MODULE_PARM_DESC(zvol_major, "Major number for zvol device");
1798 
1799 module_param(zvol_max_discard_blocks, ulong, 0444);
1800 MODULE_PARM_DESC(zvol_max_discard_blocks, "Max number of blocks to discard");
1801 
1802 module_param(zvol_blk_mq_queue_depth, uint, 0644);
1803 MODULE_PARM_DESC(zvol_blk_mq_queue_depth, "Default blk-mq queue depth");
1804 
1805 module_param(zvol_use_blk_mq, uint, 0644);
1806 MODULE_PARM_DESC(zvol_use_blk_mq, "Use the blk-mq API for zvols");
1807 
1808 module_param(zvol_blk_mq_blocks_per_thread, uint, 0644);
1809 MODULE_PARM_DESC(zvol_blk_mq_blocks_per_thread,
1810 	"Process volblocksize blocks per thread");
1811 
1812 #ifndef HAVE_BLKDEV_GET_ERESTARTSYS
1813 module_param(zvol_open_timeout_ms, uint, 0644);
1814 MODULE_PARM_DESC(zvol_open_timeout_ms, "Timeout for ZVOL open retries");
1815 #endif
1816