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