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