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