]> git.proxmox.com Git - mirror_zfs.git/blame - module/os/linux/zfs/zvol_os.c
init.d/zfs-mount: Don't fsck or mount/umount fstab entries
[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
9 * or http://www.opensolaris.org/os/licensing.
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
18168da7
AZ
44static unsigned int zvol_major = ZVOL_MAJOR;
45static unsigned int zvol_request_sync = 0;
46static unsigned int zvol_prefetch_bytes = (128 * 1024);
47static unsigned long zvol_max_discard_blocks = 16384;
48static unsigned int zvol_threads = 32;
abdcef47
PH
49
50#ifndef HAVE_BLKDEV_GET_ERESTARTSYS
18168da7 51static const unsigned int zvol_open_timeout_ms = 1000;
abdcef47 52#endif
5df7e9d8
MM
53
54struct zvol_state_os {
55 struct gendisk *zvo_disk; /* generic disk */
56 struct request_queue *zvo_queue; /* request queue */
5df7e9d8
MM
57 dev_t zvo_dev; /* device id */
58};
59
60taskq_t *zvol_taskq;
61static struct ida zvol_ida;
62
e439ee83 63typedef struct zv_request_stack {
5df7e9d8
MM
64 zvol_state_t *zv;
65 struct bio *bio;
5df7e9d8
MM
66} zv_request_t;
67
e439ee83
CS
68typedef struct zv_request_task {
69 zv_request_t zvr;
70 taskq_ent_t ent;
71} zv_request_task_t;
72
73static zv_request_task_t *
74zv_request_task_create(zv_request_t zvr)
75{
76 zv_request_task_t *task;
77 task = kmem_alloc(sizeof (zv_request_task_t), KM_SLEEP);
78 taskq_init_ent(&task->ent);
79 task->zvr = zvr;
80 return (task);
81}
82
83static void
84zv_request_task_free(zv_request_task_t *task)
85{
86 kmem_free(task, sizeof (*task));
87}
88
5df7e9d8
MM
89/*
90 * Given a path, return TRUE if path is a ZVOL.
91 */
1dccfd7a
CS
92boolean_t
93zvol_os_is_zvol(const char *path)
5df7e9d8 94{
b7281c88 95 dev_t dev = 0;
5df7e9d8 96
b7281c88 97 if (vdev_lookup_bdev(path, &dev) != 0)
5df7e9d8
MM
98 return (B_FALSE);
99
b7281c88 100 if (MAJOR(dev) == zvol_major)
5df7e9d8
MM
101 return (B_TRUE);
102
103 return (B_FALSE);
104}
105
5df7e9d8 106static void
e439ee83 107zvol_write(zv_request_t *zvr)
5df7e9d8 108{
5df7e9d8 109 struct bio *bio = zvr->bio;
1c2358c1 110 int error = 0;
d0cd9a5c 111 zfs_uio_t uio;
1c2358c1 112
d0cd9a5c 113 zfs_uio_bvec_init(&uio, bio);
5df7e9d8
MM
114
115 zvol_state_t *zv = zvr->zv;
0b32d817
RM
116 ASSERT3P(zv, !=, NULL);
117 ASSERT3U(zv->zv_open_count, >, 0);
118 ASSERT3P(zv->zv_zilog, !=, NULL);
5df7e9d8 119
0929c4de
MA
120 /* bio marked as FLUSH need to flush before write */
121 if (bio_is_flush(bio))
122 zil_commit(zv->zv_zilog, ZVOL_OBJ);
123
124 /* Some requests are just for flush and nothing else. */
125 if (uio.uio_resid == 0) {
126 rw_exit(&zv->zv_suspend_lock);
127 BIO_END_IO(bio, 0);
0929c4de
MA
128 return;
129 }
130
a970f059
BB
131 struct request_queue *q = zv->zv_zso->zvo_queue;
132 struct gendisk *disk = zv->zv_zso->zvo_disk;
5df7e9d8 133 ssize_t start_resid = uio.uio_resid;
a970f059
BB
134 unsigned long start_time;
135
136 boolean_t acct = blk_queue_io_stat(q);
137 if (acct)
138 start_time = blk_generic_start_io_acct(q, disk, WRITE, bio);
5df7e9d8
MM
139
140 boolean_t sync =
141 bio_is_fua(bio) || zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS;
142
0929c4de
MA
143 zfs_locked_range_t *lr = zfs_rangelock_enter(&zv->zv_rangelock,
144 uio.uio_loffset, uio.uio_resid, RL_WRITER);
145
5df7e9d8
MM
146 uint64_t volsize = zv->zv_volsize;
147 while (uio.uio_resid > 0 && uio.uio_loffset < volsize) {
148 uint64_t bytes = MIN(uio.uio_resid, DMU_MAX_ACCESS >> 1);
149 uint64_t off = uio.uio_loffset;
150 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
151
152 if (bytes > volsize - off) /* don't write past the end */
153 bytes = volsize - off;
154
20f28785 155 dmu_tx_hold_write_by_dnode(tx, zv->zv_dn, off, bytes);
5df7e9d8
MM
156
157 /* This will only fail for ENOSPC */
158 error = dmu_tx_assign(tx, TXG_WAIT);
159 if (error) {
160 dmu_tx_abort(tx);
161 break;
162 }
163 error = dmu_write_uio_dnode(zv->zv_dn, &uio, bytes, tx);
164 if (error == 0) {
165 zvol_log_write(zv, tx, off, bytes, sync);
166 }
167 dmu_tx_commit(tx);
168
169 if (error)
170 break;
171 }
0929c4de 172 zfs_rangelock_exit(lr);
5df7e9d8
MM
173
174 int64_t nwritten = start_resid - uio.uio_resid;
4547fc4e 175 dataset_kstats_update_write_kstats(&zv->zv_kstat, nwritten);
5df7e9d8
MM
176 task_io_account_write(nwritten);
177
178 if (sync)
179 zil_commit(zv->zv_zilog, ZVOL_OBJ);
180
181 rw_exit(&zv->zv_suspend_lock);
a970f059
BB
182
183 if (acct)
184 blk_generic_end_io_acct(q, disk, WRITE, bio, start_time);
185
5df7e9d8 186 BIO_END_IO(bio, -error);
5df7e9d8
MM
187}
188
189static void
e439ee83
CS
190zvol_write_task(void *arg)
191{
192 zv_request_task_t *task = arg;
193 zvol_write(&task->zvr);
194 zv_request_task_free(task);
195}
196
197static void
198zvol_discard(zv_request_t *zvr)
5df7e9d8 199{
5df7e9d8
MM
200 struct bio *bio = zvr->bio;
201 zvol_state_t *zv = zvr->zv;
202 uint64_t start = BIO_BI_SECTOR(bio) << 9;
203 uint64_t size = BIO_BI_SIZE(bio);
204 uint64_t end = start + size;
205 boolean_t sync;
206 int error = 0;
207 dmu_tx_t *tx;
5df7e9d8 208
0b32d817
RM
209 ASSERT3P(zv, !=, NULL);
210 ASSERT3U(zv->zv_open_count, >, 0);
211 ASSERT3P(zv->zv_zilog, !=, NULL);
5df7e9d8 212
a970f059
BB
213 struct request_queue *q = zv->zv_zso->zvo_queue;
214 struct gendisk *disk = zv->zv_zso->zvo_disk;
215 unsigned long start_time;
216
217 boolean_t acct = blk_queue_io_stat(q);
218 if (acct)
219 start_time = blk_generic_start_io_acct(q, disk, WRITE, bio);
5df7e9d8
MM
220
221 sync = bio_is_fua(bio) || zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS;
222
223 if (end > zv->zv_volsize) {
224 error = SET_ERROR(EIO);
225 goto unlock;
226 }
227
228 /*
229 * Align the request to volume block boundaries when a secure erase is
230 * not required. This will prevent dnode_free_range() from zeroing out
231 * the unaligned parts which is slow (read-modify-write) and useless
232 * since we are not freeing any space by doing so.
233 */
234 if (!bio_is_secure_erase(bio)) {
235 start = P2ROUNDUP(start, zv->zv_volblocksize);
236 end = P2ALIGN(end, zv->zv_volblocksize);
237 size = end - start;
238 }
239
240 if (start >= end)
241 goto unlock;
242
0929c4de
MA
243 zfs_locked_range_t *lr = zfs_rangelock_enter(&zv->zv_rangelock,
244 start, size, RL_WRITER);
245
5df7e9d8
MM
246 tx = dmu_tx_create(zv->zv_objset);
247 dmu_tx_mark_netfree(tx);
248 error = dmu_tx_assign(tx, TXG_WAIT);
249 if (error != 0) {
250 dmu_tx_abort(tx);
251 } else {
252 zvol_log_truncate(zv, tx, start, size, B_TRUE);
253 dmu_tx_commit(tx);
254 error = dmu_free_long_range(zv->zv_objset,
255 ZVOL_OBJ, start, size);
256 }
0929c4de 257 zfs_rangelock_exit(lr);
5df7e9d8
MM
258
259 if (error == 0 && sync)
260 zil_commit(zv->zv_zilog, ZVOL_OBJ);
261
0929c4de 262unlock:
5df7e9d8 263 rw_exit(&zv->zv_suspend_lock);
a970f059
BB
264
265 if (acct)
266 blk_generic_end_io_acct(q, disk, WRITE, bio, start_time);
267
5df7e9d8 268 BIO_END_IO(bio, -error);
5df7e9d8
MM
269}
270
271static void
e439ee83
CS
272zvol_discard_task(void *arg)
273{
274 zv_request_task_t *task = arg;
275 zvol_discard(&task->zvr);
276 zv_request_task_free(task);
277}
278
279static void
280zvol_read(zv_request_t *zvr)
5df7e9d8 281{
5df7e9d8 282 struct bio *bio = zvr->bio;
1c2358c1 283 int error = 0;
d0cd9a5c 284 zfs_uio_t uio;
1c2358c1 285
d0cd9a5c 286 zfs_uio_bvec_init(&uio, bio);
5df7e9d8
MM
287
288 zvol_state_t *zv = zvr->zv;
0b32d817
RM
289 ASSERT3P(zv, !=, NULL);
290 ASSERT3U(zv->zv_open_count, >, 0);
5df7e9d8 291
a970f059
BB
292 struct request_queue *q = zv->zv_zso->zvo_queue;
293 struct gendisk *disk = zv->zv_zso->zvo_disk;
5df7e9d8 294 ssize_t start_resid = uio.uio_resid;
a970f059
BB
295 unsigned long start_time;
296
297 boolean_t acct = blk_queue_io_stat(q);
298 if (acct)
299 start_time = blk_generic_start_io_acct(q, disk, READ, bio);
5df7e9d8 300
0929c4de
MA
301 zfs_locked_range_t *lr = zfs_rangelock_enter(&zv->zv_rangelock,
302 uio.uio_loffset, uio.uio_resid, RL_READER);
303
5df7e9d8
MM
304 uint64_t volsize = zv->zv_volsize;
305 while (uio.uio_resid > 0 && uio.uio_loffset < volsize) {
306 uint64_t bytes = MIN(uio.uio_resid, DMU_MAX_ACCESS >> 1);
307
308 /* don't read past the end */
309 if (bytes > volsize - uio.uio_loffset)
310 bytes = volsize - uio.uio_loffset;
311
312 error = dmu_read_uio_dnode(zv->zv_dn, &uio, bytes);
313 if (error) {
314 /* convert checksum errors into IO errors */
315 if (error == ECKSUM)
316 error = SET_ERROR(EIO);
317 break;
318 }
319 }
0929c4de 320 zfs_rangelock_exit(lr);
5df7e9d8
MM
321
322 int64_t nread = start_resid - uio.uio_resid;
4547fc4e 323 dataset_kstats_update_read_kstats(&zv->zv_kstat, nread);
5df7e9d8
MM
324 task_io_account_read(nread);
325
326 rw_exit(&zv->zv_suspend_lock);
a970f059
BB
327
328 if (acct)
329 blk_generic_end_io_acct(q, disk, READ, bio, start_time);
330
5df7e9d8 331 BIO_END_IO(bio, -error);
e439ee83
CS
332}
333
334static void
335zvol_read_task(void *arg)
336{
337 zv_request_task_t *task = arg;
338 zvol_read(&task->zvr);
339 zv_request_task_free(task);
5df7e9d8
MM
340}
341
d817c171 342#ifdef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
435a451e
CK
343#ifdef HAVE_BDEV_SUBMIT_BIO_RETURNS_VOID
344static void
345zvol_submit_bio(struct bio *bio)
346#else
d817c171
CK
347static blk_qc_t
348zvol_submit_bio(struct bio *bio)
435a451e 349#endif
d817c171 350#else
5df7e9d8
MM
351static MAKE_REQUEST_FN_RET
352zvol_request(struct request_queue *q, struct bio *bio)
d817c171 353#endif
5df7e9d8 354{
d817c171 355#ifdef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
d939930f
CK
356#if defined(HAVE_BIO_BDEV_DISK)
357 struct request_queue *q = bio->bi_bdev->bd_disk->queue;
358#else
d817c171 359 struct request_queue *q = bio->bi_disk->queue;
d939930f 360#endif
d817c171 361#endif
5df7e9d8
MM
362 zvol_state_t *zv = q->queuedata;
363 fstrans_cookie_t cookie = spl_fstrans_mark();
364 uint64_t offset = BIO_BI_SECTOR(bio) << 9;
365 uint64_t size = BIO_BI_SIZE(bio);
366 int rw = bio_data_dir(bio);
5df7e9d8
MM
367
368 if (bio_has_data(bio) && offset + size > zv->zv_volsize) {
369 printk(KERN_INFO
370 "%s: bad access: offset=%llu, size=%lu\n",
371 zv->zv_zso->zvo_disk->disk_name,
372 (long long unsigned)offset,
373 (long unsigned)size);
374
375 BIO_END_IO(bio, -SET_ERROR(EIO));
376 goto out;
377 }
378
e439ee83
CS
379 zv_request_t zvr = {
380 .zv = zv,
381 .bio = bio,
382 };
383 zv_request_task_t *task;
384
5df7e9d8 385 if (rw == WRITE) {
5df7e9d8
MM
386 if (unlikely(zv->zv_flags & ZVOL_RDONLY)) {
387 BIO_END_IO(bio, -SET_ERROR(EROFS));
388 goto out;
389 }
390
391 /*
0929c4de
MA
392 * Prevents the zvol from being suspended, or the ZIL being
393 * concurrently opened. Will be released after the i/o
394 * completes.
5df7e9d8
MM
395 */
396 rw_enter(&zv->zv_suspend_lock, RW_READER);
397
398 /*
399 * Open a ZIL if this is the first time we have written to this
400 * zvol. We protect zv->zv_zilog with zv_suspend_lock rather
401 * than zv_state_lock so that we don't need to acquire an
402 * additional lock in this path.
403 */
404 if (zv->zv_zilog == NULL) {
405 rw_exit(&zv->zv_suspend_lock);
406 rw_enter(&zv->zv_suspend_lock, RW_WRITER);
407 if (zv->zv_zilog == NULL) {
408 zv->zv_zilog = zil_open(zv->zv_objset,
409 zvol_get_data);
410 zv->zv_flags |= ZVOL_WRITTEN_TO;
93e36580
CS
411 /* replay / destroy done in zvol_create_minor */
412 VERIFY0((zv->zv_zilog->zl_header->zh_flags &
413 ZIL_REPLAY_NEEDED));
5df7e9d8
MM
414 }
415 rw_downgrade(&zv->zv_suspend_lock);
416 }
417
5df7e9d8 418 /*
0929c4de
MA
419 * We don't want this thread to be blocked waiting for i/o to
420 * complete, so we instead wait from a taskq callback. The
421 * i/o may be a ZIL write (via zil_commit()), or a read of an
422 * indirect block, or a read of a data block (if this is a
423 * partial-block write). We will indicate that the i/o is
424 * complete by calling BIO_END_IO() from the taskq callback.
425 *
426 * This design allows the calling thread to continue and
427 * initiate more concurrent operations by calling
428 * zvol_request() again. There are typically only a small
429 * number of threads available to call zvol_request() (e.g.
430 * one per iSCSI target), so keeping the latency of
431 * zvol_request() low is important for performance.
432 *
433 * The zvol_request_sync module parameter allows this
434 * behavior to be altered, for performance evaluation
435 * purposes. If the callback blocks, setting
436 * zvol_request_sync=1 will result in much worse performance.
437 *
438 * We can have up to zvol_threads concurrent i/o's being
439 * processed for all zvols on the system. This is typically
440 * a vast improvement over the zvol_request_sync=1 behavior
441 * of one i/o at a time per zvol. However, an even better
442 * design would be for zvol_request() to initiate the zio
443 * directly, and then be notified by the zio_done callback,
444 * which would call BIO_END_IO(). Unfortunately, the DMU/ZIL
445 * interfaces lack this functionality (they block waiting for
446 * the i/o to complete).
5df7e9d8 447 */
5df7e9d8 448 if (bio_is_discard(bio) || bio_is_secure_erase(bio)) {
0929c4de 449 if (zvol_request_sync) {
e439ee83 450 zvol_discard(&zvr);
0929c4de 451 } else {
e439ee83 452 task = zv_request_task_create(zvr);
0929c4de 453 taskq_dispatch_ent(zvol_taskq,
e439ee83 454 zvol_discard_task, task, 0, &task->ent);
0929c4de 455 }
5df7e9d8 456 } else {
0929c4de 457 if (zvol_request_sync) {
e439ee83 458 zvol_write(&zvr);
0929c4de 459 } else {
e439ee83 460 task = zv_request_task_create(zvr);
0929c4de 461 taskq_dispatch_ent(zvol_taskq,
e439ee83 462 zvol_write_task, task, 0, &task->ent);
0929c4de 463 }
5df7e9d8
MM
464 }
465 } else {
466 /*
467 * The SCST driver, and possibly others, may issue READ I/Os
468 * with a length of zero bytes. These empty I/Os contain no
469 * data and require no additional handling.
470 */
471 if (size == 0) {
472 BIO_END_IO(bio, 0);
473 goto out;
474 }
475
5df7e9d8
MM
476 rw_enter(&zv->zv_suspend_lock, RW_READER);
477
0929c4de
MA
478 /* See comment in WRITE case above. */
479 if (zvol_request_sync) {
e439ee83 480 zvol_read(&zvr);
0929c4de 481 } else {
e439ee83 482 task = zv_request_task_create(zvr);
0929c4de 483 taskq_dispatch_ent(zvol_taskq,
e439ee83 484 zvol_read_task, task, 0, &task->ent);
0929c4de 485 }
5df7e9d8
MM
486 }
487
488out:
489 spl_fstrans_unmark(cookie);
435a451e
CK
490#if (defined(HAVE_MAKE_REQUEST_FN_RET_QC) || \
491 defined(HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS)) && \
492 !defined(HAVE_BDEV_SUBMIT_BIO_RETURNS_VOID)
5df7e9d8
MM
493 return (BLK_QC_T_NONE);
494#endif
495}
496
497static int
498zvol_open(struct block_device *bdev, fmode_t flag)
499{
500 zvol_state_t *zv;
501 int error = 0;
8a02d01e 502 boolean_t drop_suspend = B_FALSE;
77e2756d
BB
503#ifndef HAVE_BLKDEV_GET_ERESTARTSYS
504 hrtime_t timeout = MSEC2NSEC(zvol_open_timeout_ms);
505 hrtime_t start = gethrtime();
5df7e9d8 506
77e2756d
BB
507retry:
508#endif
5df7e9d8
MM
509 rw_enter(&zvol_state_lock, RW_READER);
510 /*
511 * Obtain a copy of private_data under the zvol_state_lock to make
512 * sure that either the result of zvol free code path setting
1dccfd7a 513 * bdev->bd_disk->private_data to NULL is observed, or zvol_os_free()
5df7e9d8
MM
514 * is not called on this zv because of the positive zv_open_count.
515 */
516 zv = bdev->bd_disk->private_data;
517 if (zv == NULL) {
518 rw_exit(&zvol_state_lock);
519 return (SET_ERROR(-ENXIO));
520 }
521
8a02d01e
BB
522 mutex_enter(&zv->zv_state_lock);
523 /*
524 * Make sure zvol is not suspended during first open
525 * (hold zv_suspend_lock) and respect proper lock acquisition
526 * ordering - zv_suspend_lock before zv_state_lock
527 */
528 if (zv->zv_open_count == 0) {
529 if (!rw_tryenter(&zv->zv_suspend_lock, RW_READER)) {
530 mutex_exit(&zv->zv_state_lock);
531 rw_enter(&zv->zv_suspend_lock, RW_READER);
532 mutex_enter(&zv->zv_state_lock);
533 /* check to see if zv_suspend_lock is needed */
534 if (zv->zv_open_count != 0) {
535 rw_exit(&zv->zv_suspend_lock);
536 } else {
537 drop_suspend = B_TRUE;
538 }
539 } else {
540 drop_suspend = B_TRUE;
541 }
542 }
543 rw_exit(&zvol_state_lock);
544
545 ASSERT(MUTEX_HELD(&zv->zv_state_lock));
546
547 if (zv->zv_open_count == 0) {
548 boolean_t drop_namespace = B_FALSE;
549
550 ASSERT(RW_READ_HELD(&zv->zv_suspend_lock));
551
77e2756d
BB
552 /*
553 * In all other call paths the spa_namespace_lock is taken
554 * before the bdev->bd_mutex lock. However, on open(2)
555 * the __blkdev_get() function calls fops->open() with the
556 * bdev->bd_mutex lock held. This can result in a deadlock
557 * when zvols from one pool are used as vdevs in another.
558 *
559 * To prevent a lock inversion deadlock we preemptively
560 * take the spa_namespace_lock. Normally the lock will not
561 * be contended and this is safe because spa_open_common()
562 * handles the case where the caller already holds the
563 * spa_namespace_lock.
564 *
565 * When the lock cannot be aquired after multiple retries
566 * this must be the vdev on zvol deadlock case and we have
567 * no choice but to return an error. For 5.12 and older
568 * kernels returning -ERESTARTSYS will result in the
569 * bdev->bd_mutex being dropped, then reacquired, and
570 * fops->open() being called again. This process can be
571 * repeated safely until both locks are acquired. For 5.13
572 * and newer the -ERESTARTSYS retry logic was removed from
573 * the kernel so the only option is to return the error for
574 * the caller to handle it.
575 */
8a02d01e
BB
576 if (!mutex_owned(&spa_namespace_lock)) {
577 if (!mutex_tryenter(&spa_namespace_lock)) {
578 mutex_exit(&zv->zv_state_lock);
579 rw_exit(&zv->zv_suspend_lock);
77e2756d
BB
580
581#ifdef HAVE_BLKDEV_GET_ERESTARTSYS
8a02d01e 582 schedule();
77e2756d 583 return (SET_ERROR(-ERESTARTSYS));
8a02d01e
BB
584#else
585 if ((gethrtime() - start) > timeout)
586 return (SET_ERROR(-ERESTARTSYS));
77e2756d 587
8a02d01e
BB
588 schedule_timeout(MSEC_TO_TICK(10));
589 goto retry;
77e2756d 590#endif
8a02d01e
BB
591 } else {
592 drop_namespace = B_TRUE;
5df7e9d8
MM
593 }
594 }
5df7e9d8 595
5df7e9d8 596 error = -zvol_first_open(zv, !(flag & FMODE_WRITE));
5df7e9d8 597
8a02d01e
BB
598 if (drop_namespace)
599 mutex_exit(&spa_namespace_lock);
5df7e9d8
MM
600 }
601
8a02d01e
BB
602 if (error == 0) {
603 if ((flag & FMODE_WRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
604 if (zv->zv_open_count == 0)
605 zvol_last_close(zv);
5df7e9d8 606
8a02d01e
BB
607 error = SET_ERROR(-EROFS);
608 } else {
609 zv->zv_open_count++;
610 }
611 }
5df7e9d8 612
5df7e9d8
MM
613 mutex_exit(&zv->zv_state_lock);
614 if (drop_suspend)
615 rw_exit(&zv->zv_suspend_lock);
77e2756d 616
8a02d01e
BB
617 if (error == 0)
618 zfs_check_media_change(bdev);
619
620 return (error);
5df7e9d8
MM
621}
622
5df7e9d8 623static void
5df7e9d8
MM
624zvol_release(struct gendisk *disk, fmode_t mode)
625{
626 zvol_state_t *zv;
627 boolean_t drop_suspend = B_TRUE;
628
629 rw_enter(&zvol_state_lock, RW_READER);
630 zv = disk->private_data;
631
632 mutex_enter(&zv->zv_state_lock);
0b32d817 633 ASSERT3U(zv->zv_open_count, >, 0);
5df7e9d8
MM
634 /*
635 * make sure zvol is not suspended during last close
636 * (hold zv_suspend_lock) and respect proper lock acquisition
637 * ordering - zv_suspend_lock before zv_state_lock
638 */
639 if (zv->zv_open_count == 1) {
640 if (!rw_tryenter(&zv->zv_suspend_lock, RW_READER)) {
641 mutex_exit(&zv->zv_state_lock);
642 rw_enter(&zv->zv_suspend_lock, RW_READER);
643 mutex_enter(&zv->zv_state_lock);
644 /* check to see if zv_suspend_lock is needed */
645 if (zv->zv_open_count != 1) {
646 rw_exit(&zv->zv_suspend_lock);
647 drop_suspend = B_FALSE;
648 }
649 }
650 } else {
651 drop_suspend = B_FALSE;
652 }
653 rw_exit(&zvol_state_lock);
654
655 ASSERT(MUTEX_HELD(&zv->zv_state_lock));
5df7e9d8
MM
656
657 zv->zv_open_count--;
0b32d817
RM
658 if (zv->zv_open_count == 0) {
659 ASSERT(RW_READ_HELD(&zv->zv_suspend_lock));
5df7e9d8 660 zvol_last_close(zv);
0b32d817 661 }
5df7e9d8
MM
662
663 mutex_exit(&zv->zv_state_lock);
664
665 if (drop_suspend)
666 rw_exit(&zv->zv_suspend_lock);
5df7e9d8
MM
667}
668
669static int
670zvol_ioctl(struct block_device *bdev, fmode_t mode,
671 unsigned int cmd, unsigned long arg)
672{
673 zvol_state_t *zv = bdev->bd_disk->private_data;
674 int error = 0;
675
676 ASSERT3U(zv->zv_open_count, >, 0);
677
678 switch (cmd) {
679 case BLKFLSBUF:
680 fsync_bdev(bdev);
681 invalidate_bdev(bdev);
682 rw_enter(&zv->zv_suspend_lock, RW_READER);
683
684 if (!(zv->zv_flags & ZVOL_RDONLY))
685 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
686
687 rw_exit(&zv->zv_suspend_lock);
688 break;
689
690 case BLKZNAME:
691 mutex_enter(&zv->zv_state_lock);
692 error = copy_to_user((void *)arg, zv->zv_name, MAXNAMELEN);
693 mutex_exit(&zv->zv_state_lock);
694 break;
695
696 default:
697 error = -ENOTTY;
698 break;
699 }
700
701 return (SET_ERROR(error));
702}
703
704#ifdef CONFIG_COMPAT
705static int
706zvol_compat_ioctl(struct block_device *bdev, fmode_t mode,
707 unsigned cmd, unsigned long arg)
708{
709 return (zvol_ioctl(bdev, mode, cmd, arg));
710}
711#else
712#define zvol_compat_ioctl NULL
713#endif
714
5df7e9d8
MM
715static unsigned int
716zvol_check_events(struct gendisk *disk, unsigned int clearing)
717{
718 unsigned int mask = 0;
719
720 rw_enter(&zvol_state_lock, RW_READER);
721
722 zvol_state_t *zv = disk->private_data;
723 if (zv != NULL) {
724 mutex_enter(&zv->zv_state_lock);
725 mask = zv->zv_changed ? DISK_EVENT_MEDIA_CHANGE : 0;
726 zv->zv_changed = 0;
727 mutex_exit(&zv->zv_state_lock);
728 }
729
730 rw_exit(&zvol_state_lock);
731
732 return (mask);
733}
5df7e9d8
MM
734
735static int
736zvol_revalidate_disk(struct gendisk *disk)
737{
738 rw_enter(&zvol_state_lock, RW_READER);
739
740 zvol_state_t *zv = disk->private_data;
741 if (zv != NULL) {
742 mutex_enter(&zv->zv_state_lock);
743 set_capacity(zv->zv_zso->zvo_disk,
744 zv->zv_volsize >> SECTOR_BITS);
745 mutex_exit(&zv->zv_state_lock);
746 }
747
748 rw_exit(&zvol_state_lock);
749
750 return (0);
751}
752
1dccfd7a
CS
753int
754zvol_os_update_volsize(zvol_state_t *zv, uint64_t volsize)
5df7e9d8 755{
1c0bbd52 756 struct gendisk *disk = zv->zv_zso->zvo_disk;
5df7e9d8 757
19697e45 758#if defined(HAVE_REVALIDATE_DISK_SIZE)
1c0bbd52 759 revalidate_disk_size(disk, zvol_revalidate_disk(disk) == 0);
19697e45 760#elif defined(HAVE_REVALIDATE_DISK)
1c0bbd52 761 revalidate_disk(disk);
19697e45
BB
762#else
763 zvol_revalidate_disk(disk);
59b68723 764#endif
5df7e9d8
MM
765 return (0);
766}
767
1dccfd7a
CS
768void
769zvol_os_clear_private(zvol_state_t *zv)
5df7e9d8
MM
770{
771 /*
772 * Cleared while holding zvol_state_lock as a writer
773 * which will prevent zvol_open() from opening it.
774 */
775 zv->zv_zso->zvo_disk->private_data = NULL;
776}
777
778/*
779 * Provide a simple virtual geometry for legacy compatibility. For devices
780 * smaller than 1 MiB a small head and sector count is used to allow very
781 * tiny devices. For devices over 1 Mib a standard head and sector count
782 * is used to keep the cylinders count reasonable.
783 */
784static int
785zvol_getgeo(struct block_device *bdev, struct hd_geometry *geo)
786{
787 zvol_state_t *zv = bdev->bd_disk->private_data;
788 sector_t sectors;
789
790 ASSERT3U(zv->zv_open_count, >, 0);
791
792 sectors = get_capacity(zv->zv_zso->zvo_disk);
793
794 if (sectors > 2048) {
795 geo->heads = 16;
796 geo->sectors = 63;
797 } else {
798 geo->heads = 2;
799 geo->sectors = 4;
800 }
801
802 geo->start = 0;
803 geo->cylinders = sectors / (geo->heads * geo->sectors);
804
805 return (0);
806}
807
18168da7 808static const struct block_device_operations zvol_ops = {
5df7e9d8
MM
809 .open = zvol_open,
810 .release = zvol_release,
811 .ioctl = zvol_ioctl,
812 .compat_ioctl = zvol_compat_ioctl,
5df7e9d8 813 .check_events = zvol_check_events,
48c7b0e4 814#ifdef HAVE_BLOCK_DEVICE_OPERATIONS_REVALIDATE_DISK
5df7e9d8 815 .revalidate_disk = zvol_revalidate_disk,
48c7b0e4 816#endif
5df7e9d8
MM
817 .getgeo = zvol_getgeo,
818 .owner = THIS_MODULE,
d817c171 819#ifdef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
1b06b03a 820 .submit_bio = zvol_submit_bio,
d817c171 821#endif
5df7e9d8
MM
822};
823
824/*
825 * Allocate memory for a new zvol_state_t and setup the required
826 * request queue and generic disk structures for the block device.
827 */
828static zvol_state_t *
829zvol_alloc(dev_t dev, const char *name)
830{
831 zvol_state_t *zv;
68dde63d 832 struct zvol_state_os *zso;
5df7e9d8
MM
833 uint64_t volmode;
834
835 if (dsl_prop_get_integer(name, "volmode", &volmode, NULL) != 0)
836 return (NULL);
837
838 if (volmode == ZFS_VOLMODE_DEFAULT)
839 volmode = zvol_volmode;
840
841 if (volmode == ZFS_VOLMODE_NONE)
842 return (NULL);
843
844 zv = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
68dde63d
BB
845 zso = kmem_zalloc(sizeof (struct zvol_state_os), KM_SLEEP);
846 zv->zv_zso = zso;
0ca45cb3 847 zv->zv_volmode = volmode;
5df7e9d8
MM
848
849 list_link_init(&zv->zv_next);
5df7e9d8
MM
850 mutex_init(&zv->zv_state_lock, NULL, MUTEX_DEFAULT, NULL);
851
d817c171 852#ifdef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
1b06b03a
BB
853#ifdef HAVE_BLK_ALLOC_DISK
854 zso->zvo_disk = blk_alloc_disk(NUMA_NO_NODE);
855 if (zso->zvo_disk == NULL)
856 goto out_kmem;
857
858 zso->zvo_disk->minors = ZVOL_MINORS;
859 zso->zvo_queue = zso->zvo_disk->queue;
860#else
d817c171 861 zso->zvo_queue = blk_alloc_queue(NUMA_NO_NODE);
1b06b03a
BB
862 if (zso->zvo_queue == NULL)
863 goto out_kmem;
864
865 zso->zvo_disk = alloc_disk(ZVOL_MINORS);
866 if (zso->zvo_disk == NULL) {
867 blk_cleanup_queue(zso->zvo_queue);
868 goto out_kmem;
869 }
870
871 zso->zvo_disk->queue = zso->zvo_queue;
872#endif /* HAVE_BLK_ALLOC_DISK */
d817c171 873#else
68dde63d
BB
874 zso->zvo_queue = blk_generic_alloc_queue(zvol_request, NUMA_NO_NODE);
875 if (zso->zvo_queue == NULL)
5df7e9d8
MM
876 goto out_kmem;
877
1b06b03a
BB
878 zso->zvo_disk = alloc_disk(ZVOL_MINORS);
879 if (zso->zvo_disk == NULL) {
880 blk_cleanup_queue(zso->zvo_queue);
881 goto out_kmem;
882 }
883
884 zso->zvo_disk->queue = zso->zvo_queue;
885#endif /* HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS */
886
68dde63d 887 blk_queue_set_write_cache(zso->zvo_queue, B_TRUE, B_TRUE);
5df7e9d8
MM
888
889 /* Limit read-ahead to a single page to prevent over-prefetching. */
68dde63d 890 blk_queue_set_read_ahead(zso->zvo_queue, 1);
5df7e9d8
MM
891
892 /* Disable write merging in favor of the ZIO pipeline. */
68dde63d 893 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, zso->zvo_queue);
5df7e9d8 894
ae1e40b3
BB
895 /* Enable /proc/diskstats */
896 blk_queue_flag_set(QUEUE_FLAG_IO_STAT, zso->zvo_queue);
897
68dde63d
BB
898 zso->zvo_queue->queuedata = zv;
899 zso->zvo_dev = dev;
5df7e9d8
MM
900 zv->zv_open_count = 0;
901 strlcpy(zv->zv_name, name, MAXNAMELEN);
902
2cc479d0 903 zfs_rangelock_init(&zv->zv_rangelock, NULL, NULL);
5df7e9d8
MM
904 rw_init(&zv->zv_suspend_lock, NULL, RW_DEFAULT, NULL);
905
68dde63d
BB
906 zso->zvo_disk->major = zvol_major;
907 zso->zvo_disk->events = DISK_EVENT_MEDIA_CHANGE;
5df7e9d8
MM
908
909 if (volmode == ZFS_VOLMODE_DEV) {
910 /*
911 * ZFS_VOLMODE_DEV disable partitioning on ZVOL devices: set
86690775 912 * gendisk->minors = 1 as noted in include/linux/blkdev.h.
5df7e9d8
MM
913 * Also disable extended partition numbers (GENHD_FL_EXT_DEVT)
914 * and suppresses partition scanning (GENHD_FL_NO_PART_SCAN)
915 * setting gendisk->flags accordingly.
916 */
68dde63d 917 zso->zvo_disk->minors = 1;
5df7e9d8 918#if defined(GENHD_FL_EXT_DEVT)
68dde63d 919 zso->zvo_disk->flags &= ~GENHD_FL_EXT_DEVT;
5df7e9d8
MM
920#endif
921#if defined(GENHD_FL_NO_PART_SCAN)
68dde63d 922 zso->zvo_disk->flags |= GENHD_FL_NO_PART_SCAN;
5df7e9d8
MM
923#endif
924 }
68dde63d
BB
925 zso->zvo_disk->first_minor = (dev & MINORMASK);
926 zso->zvo_disk->fops = &zvol_ops;
927 zso->zvo_disk->private_data = zv;
68dde63d 928 snprintf(zso->zvo_disk->disk_name, DISK_NAME_LEN, "%s%d",
5df7e9d8
MM
929 ZVOL_DEV_NAME, (dev & MINORMASK));
930
931 return (zv);
932
5df7e9d8 933out_kmem:
68dde63d 934 kmem_free(zso, sizeof (struct zvol_state_os));
5df7e9d8
MM
935 kmem_free(zv, sizeof (zvol_state_t));
936 return (NULL);
937}
938
939/*
940 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
941 * At this time, the structure is not opened by anyone, is taken off
942 * the zvol_state_list, and has its private data set to NULL.
943 * The zvol_state_lock is dropped.
99573cc0
PS
944 *
945 * This function may take many milliseconds to complete (e.g. we've seen
946 * it take over 256ms), due to the calls to "blk_cleanup_queue" and
947 * "del_gendisk". Thus, consumers need to be careful to account for this
948 * latency when calling this function.
5df7e9d8 949 */
1dccfd7a
CS
950void
951zvol_os_free(zvol_state_t *zv)
5df7e9d8
MM
952{
953
954 ASSERT(!RW_LOCK_HELD(&zv->zv_suspend_lock));
955 ASSERT(!MUTEX_HELD(&zv->zv_state_lock));
0b32d817
RM
956 ASSERT0(zv->zv_open_count);
957 ASSERT3P(zv->zv_zso->zvo_disk->private_data, ==, NULL);
5df7e9d8
MM
958
959 rw_destroy(&zv->zv_suspend_lock);
2cc479d0 960 zfs_rangelock_fini(&zv->zv_rangelock);
5df7e9d8
MM
961
962 del_gendisk(zv->zv_zso->zvo_disk);
1b06b03a
BB
963#if defined(HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS) && \
964 defined(HAVE_BLK_ALLOC_DISK)
965 blk_cleanup_disk(zv->zv_zso->zvo_disk);
966#else
5df7e9d8
MM
967 blk_cleanup_queue(zv->zv_zso->zvo_queue);
968 put_disk(zv->zv_zso->zvo_disk);
1b06b03a 969#endif
5df7e9d8
MM
970
971 ida_simple_remove(&zvol_ida,
972 MINOR(zv->zv_zso->zvo_dev) >> ZVOL_MINOR_BITS);
973
974 mutex_destroy(&zv->zv_state_lock);
4547fc4e 975 dataset_kstats_destroy(&zv->zv_kstat);
5df7e9d8
MM
976
977 kmem_free(zv->zv_zso, sizeof (struct zvol_state_os));
978 kmem_free(zv, sizeof (zvol_state_t));
979}
980
0ca45cb3
MM
981void
982zvol_wait_close(zvol_state_t *zv)
983{
984}
985
5df7e9d8
MM
986/*
987 * Create a block device minor node and setup the linkage between it
988 * and the specified volume. Once this function returns the block
989 * device is live and ready for use.
990 */
1dccfd7a 991int
ec213971 992zvol_os_create_minor(const char *name)
5df7e9d8
MM
993{
994 zvol_state_t *zv;
995 objset_t *os;
996 dmu_object_info_t *doi;
997 uint64_t volsize;
998 uint64_t len;
999 unsigned minor = 0;
1000 int error = 0;
1001 int idx;
1002 uint64_t hash = zvol_name_hash(name);
1003
1004 if (zvol_inhibit_dev)
1005 return (0);
1006
1007 idx = ida_simple_get(&zvol_ida, 0, 0, kmem_flags_convert(KM_SLEEP));
1008 if (idx < 0)
1009 return (SET_ERROR(-idx));
1010 minor = idx << ZVOL_MINOR_BITS;
1011
1012 zv = zvol_find_by_name_hash(name, hash, RW_NONE);
1013 if (zv) {
1014 ASSERT(MUTEX_HELD(&zv->zv_state_lock));
1015 mutex_exit(&zv->zv_state_lock);
1016 ida_simple_remove(&zvol_ida, idx);
1017 return (SET_ERROR(EEXIST));
1018 }
1019
1020 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
1021
1022 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, B_TRUE, FTAG, &os);
1023 if (error)
1024 goto out_doi;
1025
1026 error = dmu_object_info(os, ZVOL_OBJ, doi);
1027 if (error)
1028 goto out_dmu_objset_disown;
1029
1030 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
1031 if (error)
1032 goto out_dmu_objset_disown;
1033
1034 zv = zvol_alloc(MKDEV(zvol_major, minor), name);
1035 if (zv == NULL) {
1036 error = SET_ERROR(EAGAIN);
1037 goto out_dmu_objset_disown;
1038 }
1039 zv->zv_hash = hash;
1040
1041 if (dmu_objset_is_snapshot(os))
1042 zv->zv_flags |= ZVOL_RDONLY;
1043
1044 zv->zv_volblocksize = doi->doi_data_block_size;
1045 zv->zv_volsize = volsize;
1046 zv->zv_objset = os;
1047
1048 set_capacity(zv->zv_zso->zvo_disk, zv->zv_volsize >> 9);
1049
1050 blk_queue_max_hw_sectors(zv->zv_zso->zvo_queue,
1051 (DMU_MAX_ACCESS / 4) >> 9);
1052 blk_queue_max_segments(zv->zv_zso->zvo_queue, UINT16_MAX);
1053 blk_queue_max_segment_size(zv->zv_zso->zvo_queue, UINT_MAX);
1054 blk_queue_physical_block_size(zv->zv_zso->zvo_queue,
1055 zv->zv_volblocksize);
1056 blk_queue_io_opt(zv->zv_zso->zvo_queue, zv->zv_volblocksize);
1057 blk_queue_max_discard_sectors(zv->zv_zso->zvo_queue,
1058 (zvol_max_discard_blocks * zv->zv_volblocksize) >> 9);
1059 blk_queue_discard_granularity(zv->zv_zso->zvo_queue,
1060 zv->zv_volblocksize);
1061 blk_queue_flag_set(QUEUE_FLAG_DISCARD, zv->zv_zso->zvo_queue);
1062#ifdef QUEUE_FLAG_NONROT
1063 blk_queue_flag_set(QUEUE_FLAG_NONROT, zv->zv_zso->zvo_queue);
1064#endif
1065#ifdef QUEUE_FLAG_ADD_RANDOM
1066 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, zv->zv_zso->zvo_queue);
1067#endif
1068 /* This flag was introduced in kernel version 4.12. */
1069#ifdef QUEUE_FLAG_SCSI_PASSTHROUGH
1070 blk_queue_flag_set(QUEUE_FLAG_SCSI_PASSTHROUGH, zv->zv_zso->zvo_queue);
1071#endif
1072
93e36580
CS
1073 ASSERT3P(zv->zv_zilog, ==, NULL);
1074 zv->zv_zilog = zil_open(os, zvol_get_data);
5df7e9d8
MM
1075 if (spa_writeable(dmu_objset_spa(os))) {
1076 if (zil_replay_disable)
93e36580 1077 zil_destroy(zv->zv_zilog, B_FALSE);
5df7e9d8
MM
1078 else
1079 zil_replay(os, zv, zvol_replay_vector);
1080 }
93e36580
CS
1081 zil_close(zv->zv_zilog);
1082 zv->zv_zilog = NULL;
4547fc4e
AJ
1083 ASSERT3P(zv->zv_kstat.dk_kstats, ==, NULL);
1084 dataset_kstats_create(&zv->zv_kstat, zv->zv_objset);
5df7e9d8
MM
1085
1086 /*
1087 * When udev detects the addition of the device it will immediately
1088 * invoke blkid(8) to determine the type of content on the device.
1089 * Prefetching the blocks commonly scanned by blkid(8) will speed
1090 * up this process.
1091 */
1092 len = MIN(MAX(zvol_prefetch_bytes, 0), SPA_MAXBLOCKSIZE);
1093 if (len > 0) {
1094 dmu_prefetch(os, ZVOL_OBJ, 0, 0, len, ZIO_PRIORITY_SYNC_READ);
1095 dmu_prefetch(os, ZVOL_OBJ, 0, volsize - len, len,
1096 ZIO_PRIORITY_SYNC_READ);
1097 }
1098
1099 zv->zv_objset = NULL;
1100out_dmu_objset_disown:
1101 dmu_objset_disown(os, B_TRUE, FTAG);
1102out_doi:
1103 kmem_free(doi, sizeof (dmu_object_info_t));
1104
1105 /*
1106 * Keep in mind that once add_disk() is called, the zvol is
1107 * announced to the world, and zvol_open()/zvol_release() can
1108 * be called at any time. Incidentally, add_disk() itself calls
1109 * zvol_open()->zvol_first_open() and zvol_release()->zvol_last_close()
1110 * directly as well.
1111 */
1112 if (error == 0) {
1113 rw_enter(&zvol_state_lock, RW_WRITER);
1114 zvol_insert(zv);
1115 rw_exit(&zvol_state_lock);
12fa250d
RE
1116#ifdef HAVE_ADD_DISK_RET
1117 error = add_disk(zv->zv_zso->zvo_disk);
1118#else
5df7e9d8 1119 add_disk(zv->zv_zso->zvo_disk);
12fa250d 1120#endif
5df7e9d8
MM
1121 } else {
1122 ida_simple_remove(&zvol_ida, idx);
1123 }
1124
ec213971 1125 return (error);
5df7e9d8
MM
1126}
1127
1dccfd7a
CS
1128void
1129zvol_os_rename_minor(zvol_state_t *zv, const char *newname)
5df7e9d8
MM
1130{
1131 int readonly = get_disk_ro(zv->zv_zso->zvo_disk);
1132
1133 ASSERT(RW_LOCK_HELD(&zvol_state_lock));
1134 ASSERT(MUTEX_HELD(&zv->zv_state_lock));
1135
1136 strlcpy(zv->zv_name, newname, sizeof (zv->zv_name));
1137
1138 /* move to new hashtable entry */
1139 zv->zv_hash = zvol_name_hash(zv->zv_name);
1140 hlist_del(&zv->zv_hlink);
1141 hlist_add_head(&zv->zv_hlink, ZVOL_HT_HEAD(zv->zv_hash));
1142
1143 /*
1144 * The block device's read-only state is briefly changed causing
1145 * a KOBJ_CHANGE uevent to be issued. This ensures udev detects
1146 * the name change and fixes the symlinks. This does not change
1147 * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
1148 * changes. This would normally be done using kobject_uevent() but
1149 * that is a GPL-only symbol which is why we need this workaround.
1150 */
1151 set_disk_ro(zv->zv_zso->zvo_disk, !readonly);
1152 set_disk_ro(zv->zv_zso->zvo_disk, readonly);
1153}
1154
1dccfd7a
CS
1155void
1156zvol_os_set_disk_ro(zvol_state_t *zv, int flags)
5df7e9d8
MM
1157{
1158
1159 set_disk_ro(zv->zv_zso->zvo_disk, flags);
1160}
1161
1dccfd7a
CS
1162void
1163zvol_os_set_capacity(zvol_state_t *zv, uint64_t capacity)
5df7e9d8
MM
1164{
1165
1166 set_capacity(zv->zv_zso->zvo_disk, capacity);
1167}
1168
5df7e9d8
MM
1169int
1170zvol_init(void)
1171{
1172 int error;
1173 int threads = MIN(MAX(zvol_threads, 1), 1024);
1174
1175 error = register_blkdev(zvol_major, ZVOL_DRIVER);
1176 if (error) {
1177 printk(KERN_INFO "ZFS: register_blkdev() failed %d\n", error);
1178 return (error);
1179 }
1180 zvol_taskq = taskq_create(ZVOL_DRIVER, threads, maxclsyspri,
1181 threads * 2, INT_MAX, TASKQ_PREPOPULATE | TASKQ_DYNAMIC);
1182 if (zvol_taskq == NULL) {
1183 unregister_blkdev(zvol_major, ZVOL_DRIVER);
1184 return (-ENOMEM);
1185 }
1186 zvol_init_impl();
5df7e9d8 1187 ida_init(&zvol_ida);
5df7e9d8
MM
1188 return (0);
1189}
1190
1191void
1192zvol_fini(void)
1193{
5df7e9d8 1194 zvol_fini_impl();
5df7e9d8
MM
1195 unregister_blkdev(zvol_major, ZVOL_DRIVER);
1196 taskq_destroy(zvol_taskq);
1197 ida_destroy(&zvol_ida);
1198}
1199
1200/* BEGIN CSTYLED */
1201module_param(zvol_inhibit_dev, uint, 0644);
1202MODULE_PARM_DESC(zvol_inhibit_dev, "Do not create zvol device nodes");
1203
1204module_param(zvol_major, uint, 0444);
1205MODULE_PARM_DESC(zvol_major, "Major number for zvol device");
1206
1207module_param(zvol_threads, uint, 0444);
1208MODULE_PARM_DESC(zvol_threads, "Max number of threads to handle I/O requests");
1209
1210module_param(zvol_request_sync, uint, 0644);
1211MODULE_PARM_DESC(zvol_request_sync, "Synchronously handle bio requests");
1212
1213module_param(zvol_max_discard_blocks, ulong, 0444);
1214MODULE_PARM_DESC(zvol_max_discard_blocks, "Max number of blocks to discard");
1215
1216module_param(zvol_prefetch_bytes, uint, 0644);
1217MODULE_PARM_DESC(zvol_prefetch_bytes, "Prefetch N bytes at zvol start+end");
1218
1219module_param(zvol_volmode, uint, 0644);
1220MODULE_PARM_DESC(zvol_volmode, "Default volmode property value");
1221/* END CSTYLED */