]> git.proxmox.com Git - mirror_zfs-debian.git/blame - module/zfs/zvol.c
Imported Upstream version 0.6.5.9
[mirror_zfs-debian.git] / module / zfs / zvol.c
CommitLineData
60101509
BB
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) 2008-2010 Lawrence Livermore National Security, LLC.
23 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
24 * Rewritten for Linux by Brian Behlendorf <behlendorf1@llnl.gov>.
25 * LLNL-CODE-403049.
26 *
27 * ZFS volume emulation driver.
28 *
29 * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
30 * Volumes are accessed through the symbolic links named:
31 *
32 * /dev/<pool_name>/<dataset_name>
33 *
34 * Volumes are persistent through reboot and module load. No user command
35 * needs to be run before opening and using a device.
4e820b5a
AX
36 *
37 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
60101509
BB
38 */
39
a08ee875 40#include <sys/dbuf.h>
60101509
BB
41#include <sys/dmu_traverse.h>
42#include <sys/dsl_dataset.h>
43#include <sys/dsl_prop.h>
4e820b5a 44#include <sys/dsl_dir.h>
60101509 45#include <sys/zap.h>
e10b0808 46#include <sys/zfeature.h>
60101509 47#include <sys/zil_impl.h>
4e820b5a 48#include <sys/dmu_tx.h>
60101509
BB
49#include <sys/zio.h>
50#include <sys/zfs_rlock.h>
51#include <sys/zfs_znode.h>
4e820b5a 52#include <sys/spa_impl.h>
60101509 53#include <sys/zvol.h>
61e90960 54#include <linux/blkdev_compat.h>
60101509 55
74497b7a 56unsigned int zvol_inhibit_dev = 0;
60101509 57unsigned int zvol_major = ZVOL_MAJOR;
e10b0808 58unsigned int zvol_prefetch_bytes = (128 * 1024);
7c0e5708 59unsigned long zvol_max_discard_blocks = 16384;
60101509 60
60101509
BB
61static kmutex_t zvol_state_lock;
62static list_t zvol_state_list;
63static char *zvol_tag = "zvol_tag";
64
65/*
66 * The in-core state of each volume.
67 */
68typedef struct zvol_state {
4c0d8e50 69 char zv_name[MAXNAMELEN]; /* name */
a08ee875
LG
70 uint64_t zv_volsize; /* advertised space */
71 uint64_t zv_volblocksize; /* volume block size */
60101509
BB
72 objset_t *zv_objset; /* objset handle */
73 uint32_t zv_flags; /* ZVOL_* flags */
74 uint32_t zv_open_count; /* open counts */
75 uint32_t zv_changed; /* disk changed */
76 zilog_t *zv_zilog; /* ZIL handle */
87dac73d 77 zfs_rlock_t zv_range_lock; /* range lock */
60101509
BB
78 dmu_buf_t *zv_dbuf; /* bonus handle */
79 dev_t zv_dev; /* device id */
80 struct gendisk *zv_disk; /* generic disk */
81 struct request_queue *zv_queue; /* request queue */
82 spinlock_t zv_lock; /* request queue lock */
83 list_node_t zv_next; /* next zvol_state_t linkage */
84} zvol_state_t;
85
4e820b5a
AX
86typedef enum {
87 ZVOL_ASYNC_CREATE_MINORS,
88 ZVOL_ASYNC_REMOVE_MINORS,
89 ZVOL_ASYNC_RENAME_MINORS,
90 ZVOL_ASYNC_SET_SNAPDEV,
91 ZVOL_ASYNC_MAX
92} zvol_async_op_t;
93
94typedef struct {
95 zvol_async_op_t op;
96 char pool[MAXNAMELEN];
97 char name1[MAXNAMELEN];
98 char name2[MAXNAMELEN];
99 zprop_source_t source;
100 uint64_t snapdev;
101} zvol_task_t;
102
60101509
BB
103#define ZVOL_RDONLY 0x1
104
105/*
106 * Find the next available range of ZVOL_MINORS minor numbers. The
107 * zvol_state_list is kept in ascending minor order so we simply need
108 * to scan the list for the first gap in the sequence. This allows us
109 * to recycle minor number as devices are created and removed.
110 */
111static int
112zvol_find_minor(unsigned *minor)
113{
114 zvol_state_t *zv;
115
116 *minor = 0;
117 ASSERT(MUTEX_HELD(&zvol_state_lock));
118 for (zv = list_head(&zvol_state_list); zv != NULL;
a08ee875 119 zv = list_next(&zvol_state_list, zv), *minor += ZVOL_MINORS) {
60101509
BB
120 if (MINOR(zv->zv_dev) != MINOR(*minor))
121 break;
122 }
123
124 /* All minors are in use */
125 if (*minor >= (1 << MINORBITS))
a08ee875 126 return (SET_ERROR(ENXIO));
60101509 127
a08ee875 128 return (0);
60101509
BB
129}
130
131/*
132 * Find a zvol_state_t given the full major+minor dev_t.
133 */
134static zvol_state_t *
135zvol_find_by_dev(dev_t dev)
136{
137 zvol_state_t *zv;
138
139 ASSERT(MUTEX_HELD(&zvol_state_lock));
140 for (zv = list_head(&zvol_state_list); zv != NULL;
a08ee875 141 zv = list_next(&zvol_state_list, zv)) {
60101509 142 if (zv->zv_dev == dev)
a08ee875 143 return (zv);
60101509
BB
144 }
145
a08ee875 146 return (NULL);
60101509
BB
147}
148
149/*
150 * Find a zvol_state_t given the name provided at zvol_alloc() time.
151 */
152static zvol_state_t *
153zvol_find_by_name(const char *name)
154{
155 zvol_state_t *zv;
156
157 ASSERT(MUTEX_HELD(&zvol_state_lock));
158 for (zv = list_head(&zvol_state_list); zv != NULL;
a08ee875
LG
159 zv = list_next(&zvol_state_list, zv)) {
160 if (strncmp(zv->zv_name, name, MAXNAMELEN) == 0)
161 return (zv);
60101509
BB
162 }
163
a08ee875 164 return (NULL);
60101509
BB
165}
166
6c285672
JL
167
168/*
169 * Given a path, return TRUE if path is a ZVOL.
170 */
171boolean_t
172zvol_is_zvol(const char *device)
173{
174 struct block_device *bdev;
175 unsigned int major;
176
68d83c55 177 bdev = vdev_lookup_bdev(device);
6c285672
JL
178 if (IS_ERR(bdev))
179 return (B_FALSE);
180
181 major = MAJOR(bdev->bd_dev);
182 bdput(bdev);
183
184 if (major == zvol_major)
a08ee875 185 return (B_TRUE);
6c285672
JL
186
187 return (B_FALSE);
188}
189
60101509
BB
190/*
191 * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation.
192 */
193void
194zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
195{
196 zfs_creat_t *zct = arg;
197 nvlist_t *nvprops = zct->zct_props;
198 int error;
199 uint64_t volblocksize, volsize;
200
201 VERIFY(nvlist_lookup_uint64(nvprops,
202 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
203 if (nvlist_lookup_uint64(nvprops,
204 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
205 volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
206
207 /*
208 * These properties must be removed from the list so the generic
209 * property setting step won't apply to them.
210 */
211 VERIFY(nvlist_remove_all(nvprops,
212 zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
213 (void) nvlist_remove_all(nvprops,
214 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
215
216 error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
217 DMU_OT_NONE, 0, tx);
218 ASSERT(error == 0);
219
220 error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
221 DMU_OT_NONE, 0, tx);
222 ASSERT(error == 0);
223
224 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
225 ASSERT(error == 0);
226}
227
228/*
229 * ZFS_IOC_OBJSET_STATS entry point.
230 */
231int
232zvol_get_stats(objset_t *os, nvlist_t *nv)
233{
234 int error;
235 dmu_object_info_t *doi;
236 uint64_t val;
237
238 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
239 if (error)
a08ee875 240 return (SET_ERROR(error));
60101509
BB
241
242 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
a08ee875 243 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
60101509
BB
244 error = dmu_object_info(os, ZVOL_OBJ, doi);
245
246 if (error == 0) {
247 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
248 doi->doi_data_block_size);
249 }
250
a08ee875 251 kmem_free(doi, sizeof (dmu_object_info_t));
60101509 252
a08ee875
LG
253 return (SET_ERROR(error));
254}
255
256static void
257zvol_size_changed(zvol_state_t *zv, uint64_t volsize)
258{
259 struct block_device *bdev;
260
261 bdev = bdget_disk(zv->zv_disk, 0);
262 if (bdev == NULL)
263 return;
264/*
265 * 2.6.28 API change
266 * Added check_disk_size_change() helper function.
267 */
268#ifdef HAVE_CHECK_DISK_SIZE_CHANGE
269 set_capacity(zv->zv_disk, volsize >> 9);
270 zv->zv_volsize = volsize;
271 check_disk_size_change(zv->zv_disk, bdev);
272#else
273 zv->zv_volsize = volsize;
274 zv->zv_changed = 1;
275 (void) check_disk_change(bdev);
276#endif /* HAVE_CHECK_DISK_SIZE_CHANGE */
277
278 bdput(bdev);
60101509
BB
279}
280
281/*
282 * Sanity check volume size.
283 */
284int
285zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
286{
287 if (volsize == 0)
a08ee875 288 return (SET_ERROR(EINVAL));
60101509
BB
289
290 if (volsize % blocksize != 0)
a08ee875 291 return (SET_ERROR(EINVAL));
60101509
BB
292
293#ifdef _ILP32
294 if (volsize - 1 > MAXOFFSET_T)
a08ee875 295 return (SET_ERROR(EOVERFLOW));
60101509
BB
296#endif
297 return (0);
298}
299
300/*
301 * Ensure the zap is flushed then inform the VFS of the capacity change.
302 */
303static int
a08ee875 304zvol_update_volsize(uint64_t volsize, objset_t *os)
60101509 305{
60101509
BB
306 dmu_tx_t *tx;
307 int error;
308
309 ASSERT(MUTEX_HELD(&zvol_state_lock));
310
df554c14 311 tx = dmu_tx_create(os);
60101509
BB
312 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
313 error = dmu_tx_assign(tx, TXG_WAIT);
314 if (error) {
315 dmu_tx_abort(tx);
a08ee875 316 return (SET_ERROR(error));
60101509
BB
317 }
318
df554c14 319 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
60101509
BB
320 &volsize, tx);
321 dmu_tx_commit(tx);
322
a08ee875
LG
323 if (error == 0)
324 error = dmu_free_long_range(os,
325 ZVOL_OBJ, volsize, DMU_OBJECT_END);
60101509 326
a08ee875
LG
327 return (error);
328}
60101509 329
a08ee875
LG
330static int
331zvol_update_live_volsize(zvol_state_t *zv, uint64_t volsize)
332{
333 zvol_size_changed(zv, volsize);
60101509 334
a08ee875
LG
335 /*
336 * We should post a event here describing the expansion. However,
337 * the zfs_ereport_post() interface doesn't nicely support posting
338 * events for zvols, it assumes events relate to vdevs or zios.
339 */
60101509
BB
340
341 return (0);
342}
343
344/*
345 * Set ZFS_PROP_VOLSIZE set entry point.
346 */
347int
348zvol_set_volsize(const char *name, uint64_t volsize)
349{
a08ee875 350 zvol_state_t *zv = NULL;
60101509 351 objset_t *os = NULL;
60101509 352 int error;
a08ee875
LG
353 dmu_object_info_t *doi;
354 uint64_t readonly;
355 boolean_t owned = B_FALSE;
60101509 356
a08ee875
LG
357 error = dsl_prop_get_integer(name,
358 zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL);
359 if (error != 0)
360 return (SET_ERROR(error));
361 if (readonly)
362 return (SET_ERROR(EROFS));
60101509 363
a08ee875 364 mutex_enter(&zvol_state_lock);
60101509 365 zv = zvol_find_by_name(name);
60101509 366
a08ee875
LG
367 if (zv == NULL || zv->zv_objset == NULL) {
368 if ((error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE,
369 FTAG, &os)) != 0) {
370 mutex_exit(&zvol_state_lock);
371 return (SET_ERROR(error));
372 }
373 owned = B_TRUE;
374 if (zv != NULL)
375 zv->zv_objset = os;
376 } else {
377 os = zv->zv_objset;
378 }
60101509 379
a08ee875 380 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
60101509 381
a08ee875
LG
382 if ((error = dmu_object_info(os, ZVOL_OBJ, doi)) ||
383 (error = zvol_check_volsize(volsize, doi->doi_data_block_size)))
384 goto out;
60101509 385
a08ee875
LG
386 error = zvol_update_volsize(volsize, os);
387 kmem_free(doi, sizeof (dmu_object_info_t));
60101509 388
a08ee875
LG
389 if (error == 0 && zv != NULL)
390 error = zvol_update_live_volsize(zv, volsize);
60101509 391out:
a08ee875
LG
392 if (owned) {
393 dmu_objset_disown(os, FTAG);
394 if (zv != NULL)
395 zv->zv_objset = NULL;
396 }
60101509 397 mutex_exit(&zvol_state_lock);
60101509
BB
398 return (error);
399}
400
401/*
402 * Sanity check volume block size.
403 */
404int
e10b0808 405zvol_check_volblocksize(const char *name, uint64_t volblocksize)
60101509 406{
e10b0808
AX
407 /* Record sizes above 128k need the feature to be enabled */
408 if (volblocksize > SPA_OLD_MAXBLOCKSIZE) {
409 spa_t *spa;
410 int error;
411
412 if ((error = spa_open(name, &spa, FTAG)) != 0)
413 return (error);
414
415 if (!spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
416 spa_close(spa, FTAG);
417 return (SET_ERROR(ENOTSUP));
418 }
419
420 /*
421 * We don't allow setting the property above 1MB,
422 * unless the tunable has been changed.
423 */
424 if (volblocksize > zfs_max_recordsize)
425 return (SET_ERROR(EDOM));
426
427 spa_close(spa, FTAG);
428 }
429
60101509
BB
430 if (volblocksize < SPA_MINBLOCKSIZE ||
431 volblocksize > SPA_MAXBLOCKSIZE ||
432 !ISP2(volblocksize))
a08ee875 433 return (SET_ERROR(EDOM));
60101509
BB
434
435 return (0);
436}
437
438/*
439 * Set ZFS_PROP_VOLBLOCKSIZE set entry point.
440 */
441int
442zvol_set_volblocksize(const char *name, uint64_t volblocksize)
443{
444 zvol_state_t *zv;
445 dmu_tx_t *tx;
446 int error;
447
448 mutex_enter(&zvol_state_lock);
449
450 zv = zvol_find_by_name(name);
451 if (zv == NULL) {
a08ee875 452 error = SET_ERROR(ENXIO);
60101509
BB
453 goto out;
454 }
455
a08ee875
LG
456 if (zv->zv_flags & ZVOL_RDONLY) {
457 error = SET_ERROR(EROFS);
60101509
BB
458 goto out;
459 }
460
461 tx = dmu_tx_create(zv->zv_objset);
462 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
463 error = dmu_tx_assign(tx, TXG_WAIT);
464 if (error) {
465 dmu_tx_abort(tx);
466 } else {
467 error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
468 volblocksize, 0, tx);
469 if (error == ENOTSUP)
a08ee875 470 error = SET_ERROR(EBUSY);
60101509
BB
471 dmu_tx_commit(tx);
472 if (error == 0)
473 zv->zv_volblocksize = volblocksize;
474 }
475out:
476 mutex_exit(&zvol_state_lock);
477
a08ee875 478 return (SET_ERROR(error));
60101509
BB
479}
480
481/*
482 * Replay a TX_WRITE ZIL transaction that didn't get committed
483 * after a system failure
484 */
485static int
486zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
487{
488 objset_t *os = zv->zv_objset;
489 char *data = (char *)(lr + 1); /* data follows lr_write_t */
490 uint64_t off = lr->lr_offset;
491 uint64_t len = lr->lr_length;
492 dmu_tx_t *tx;
493 int error;
494
495 if (byteswap)
496 byteswap_uint64_array(lr, sizeof (*lr));
497
498 tx = dmu_tx_create(os);
499 dmu_tx_hold_write(tx, ZVOL_OBJ, off, len);
500 error = dmu_tx_assign(tx, TXG_WAIT);
501 if (error) {
502 dmu_tx_abort(tx);
503 } else {
504 dmu_write(os, ZVOL_OBJ, off, len, data, tx);
505 dmu_tx_commit(tx);
506 }
507
a08ee875 508 return (SET_ERROR(error));
60101509
BB
509}
510
511static int
512zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
513{
a08ee875 514 return (SET_ERROR(ENOTSUP));
60101509
BB
515}
516
517/*
518 * Callback vectors for replaying records.
519 * Only TX_WRITE is needed for zvol.
520 */
b01615d5
RY
521zil_replay_func_t zvol_replay_vector[TX_MAX_TYPE] = {
522 (zil_replay_func_t)zvol_replay_err, /* no such transaction type */
523 (zil_replay_func_t)zvol_replay_err, /* TX_CREATE */
524 (zil_replay_func_t)zvol_replay_err, /* TX_MKDIR */
525 (zil_replay_func_t)zvol_replay_err, /* TX_MKXATTR */
526 (zil_replay_func_t)zvol_replay_err, /* TX_SYMLINK */
527 (zil_replay_func_t)zvol_replay_err, /* TX_REMOVE */
528 (zil_replay_func_t)zvol_replay_err, /* TX_RMDIR */
529 (zil_replay_func_t)zvol_replay_err, /* TX_LINK */
530 (zil_replay_func_t)zvol_replay_err, /* TX_RENAME */
531 (zil_replay_func_t)zvol_replay_write, /* TX_WRITE */
532 (zil_replay_func_t)zvol_replay_err, /* TX_TRUNCATE */
533 (zil_replay_func_t)zvol_replay_err, /* TX_SETATTR */
534 (zil_replay_func_t)zvol_replay_err, /* TX_ACL */
60101509
BB
535};
536
537/*
538 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
539 *
540 * We store data in the log buffers if it's small enough.
541 * Otherwise we will later flush the data out via dmu_sync().
542 */
543ssize_t zvol_immediate_write_sz = 32768;
544
545static void
a08ee875
LG
546zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, uint64_t offset,
547 uint64_t size, int sync)
60101509
BB
548{
549 uint32_t blocksize = zv->zv_volblocksize;
550 zilog_t *zilog = zv->zv_zilog;
551 boolean_t slogging;
ab85f845 552 ssize_t immediate_write_sz;
60101509
BB
553
554 if (zil_replaying(zilog, tx))
555 return;
556
ab85f845
ED
557 immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
558 ? 0 : zvol_immediate_write_sz;
559 slogging = spa_has_slogs(zilog->zl_spa) &&
560 (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
60101509
BB
561
562 while (size) {
563 itx_t *itx;
564 lr_write_t *lr;
565 ssize_t len;
566 itx_wr_state_t write_state;
567
568 /*
569 * Unlike zfs_log_write() we can be called with
570 * up to DMU_MAX_ACCESS/2 (5MB) writes.
571 */
ab85f845 572 if (blocksize > immediate_write_sz && !slogging &&
60101509
BB
573 size >= blocksize && offset % blocksize == 0) {
574 write_state = WR_INDIRECT; /* uses dmu_sync */
575 len = blocksize;
576 } else if (sync) {
577 write_state = WR_COPIED;
578 len = MIN(ZIL_MAX_LOG_DATA, size);
579 } else {
580 write_state = WR_NEED_COPY;
581 len = MIN(ZIL_MAX_LOG_DATA, size);
582 }
583
584 itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
585 (write_state == WR_COPIED ? len : 0));
586 lr = (lr_write_t *)&itx->itx_lr;
587 if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
588 ZVOL_OBJ, offset, len, lr+1, DMU_READ_NO_PREFETCH) != 0) {
589 zil_itx_destroy(itx);
590 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
591 lr = (lr_write_t *)&itx->itx_lr;
592 write_state = WR_NEED_COPY;
593 }
594
595 itx->itx_wr_state = write_state;
596 if (write_state == WR_NEED_COPY)
597 itx->itx_sod += len;
598 lr->lr_foid = ZVOL_OBJ;
599 lr->lr_offset = offset;
600 lr->lr_length = len;
601 lr->lr_blkoff = 0;
602 BP_ZERO(&lr->lr_blkptr);
603
604 itx->itx_private = zv;
605 itx->itx_sync = sync;
606
607 (void) zil_itx_assign(zilog, itx, tx);
608
609 offset += len;
610 size -= len;
611 }
612}
613
e10b0808
AX
614static int
615zvol_write(struct bio *bio)
60101509 616{
e10b0808
AX
617 zvol_state_t *zv = bio->bi_bdev->bd_disk->private_data;
618 uint64_t offset = BIO_BI_SECTOR(bio) << 9;
619 uint64_t size = BIO_BI_SIZE(bio);
60101509
BB
620 int error = 0;
621 dmu_tx_t *tx;
622 rl_t *rl;
623
4e820b5a
AX
624 ASSERT(zv && zv->zv_open_count > 0);
625
87dac73d 626 if (bio_is_flush(bio))
b18019d2
ED
627 zil_commit(zv->zv_zilog, ZVOL_OBJ);
628
629 /*
630 * Some requests are just for flush and nothing else.
631 */
e10b0808 632 if (size == 0)
8630650a 633 goto out;
b18019d2 634
87dac73d 635 rl = zfs_range_lock(&zv->zv_range_lock, offset, size, RL_WRITER);
60101509
BB
636
637 tx = dmu_tx_create(zv->zv_objset);
638 dmu_tx_hold_write(tx, ZVOL_OBJ, offset, size);
639
640 /* This will only fail for ENOSPC */
641 error = dmu_tx_assign(tx, TXG_WAIT);
642 if (error) {
643 dmu_tx_abort(tx);
644 zfs_range_unlock(rl);
8630650a 645 goto out;
60101509
BB
646 }
647
e10b0808 648 error = dmu_write_bio(zv->zv_objset, ZVOL_OBJ, bio, tx);
60101509 649 if (error == 0)
b18019d2 650 zvol_log_write(zv, tx, offset, size,
87dac73d 651 !!(bio_is_fua(bio)));
60101509
BB
652
653 dmu_tx_commit(tx);
654 zfs_range_unlock(rl);
655
87dac73d 656 if ((bio_is_fua(bio)) ||
b18019d2 657 zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)
60101509
BB
658 zil_commit(zv->zv_zilog, ZVOL_OBJ);
659
8630650a 660out:
e10b0808 661 return (error);
60101509
BB
662}
663
e10b0808
AX
664static int
665zvol_discard(struct bio *bio)
30930fba 666{
e10b0808
AX
667 zvol_state_t *zv = bio->bi_bdev->bd_disk->private_data;
668 uint64_t start = BIO_BI_SECTOR(bio) << 9;
669 uint64_t size = BIO_BI_SIZE(bio);
670 uint64_t end = start + size;
30930fba
ED
671 int error;
672 rl_t *rl;
673
4e820b5a
AX
674 ASSERT(zv && zv->zv_open_count > 0);
675
e10b0808
AX
676 if (end > zv->zv_volsize)
677 return (SET_ERROR(EIO));
30930fba 678
089fa91b 679 /*
87dac73d
AX
680 * Align the request to volume block boundaries when a secure erase is
681 * not required. This will prevent dnode_free_range() from zeroing out
682 * the unaligned parts which is slow (read-modify-write) and useless
683 * since we are not freeing any space by doing so.
089fa91b 684 */
87dac73d 685 if (!bio_is_secure_erase(bio)) {
e10b0808
AX
686 start = P2ROUNDUP(start, zv->zv_volblocksize);
687 end = P2ALIGN(end, zv->zv_volblocksize);
688 size = end - start;
30930fba
ED
689 }
690
e10b0808
AX
691 if (start >= end)
692 return (0);
693
87dac73d 694 rl = zfs_range_lock(&zv->zv_range_lock, start, size, RL_WRITER);
30930fba 695
e10b0808 696 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, start, size);
30930fba
ED
697
698 /*
699 * TODO: maybe we should add the operation to the log.
700 */
30930fba 701 zfs_range_unlock(rl);
e10b0808
AX
702
703 return (error);
30930fba 704}
30930fba 705
e10b0808
AX
706static int
707zvol_read(struct bio *bio)
60101509 708{
e10b0808
AX
709 zvol_state_t *zv = bio->bi_bdev->bd_disk->private_data;
710 uint64_t offset = BIO_BI_SECTOR(bio) << 9;
711 uint64_t len = BIO_BI_SIZE(bio);
60101509
BB
712 int error;
713 rl_t *rl;
714
4e820b5a
AX
715 ASSERT(zv && zv->zv_open_count > 0);
716
e10b0808
AX
717 if (len == 0)
718 return (0);
b18019d2 719
87dac73d 720 rl = zfs_range_lock(&zv->zv_range_lock, offset, len, RL_READER);
e10b0808
AX
721
722 error = dmu_read_bio(zv->zv_objset, ZVOL_OBJ, bio);
60101509
BB
723
724 zfs_range_unlock(rl);
725
726 /* convert checksum errors into IO errors */
727 if (error == ECKSUM)
a08ee875 728 error = SET_ERROR(EIO);
60101509 729
e10b0808 730 return (error);
60101509
BB
731}
732
e10b0808
AX
733static MAKE_REQUEST_FN_RET
734zvol_request(struct request_queue *q, struct bio *bio)
60101509
BB
735{
736 zvol_state_t *zv = q->queuedata;
e10b0808
AX
737 fstrans_cookie_t cookie = spl_fstrans_mark();
738 uint64_t offset = BIO_BI_SECTOR(bio);
739 unsigned int sectors = bio_sectors(bio);
740 int rw = bio_data_dir(bio);
741#ifdef HAVE_GENERIC_IO_ACCT
742 unsigned long start = jiffies;
743#endif
744 int error = 0;
60101509 745
e10b0808
AX
746 if (bio_has_data(bio) && offset + sectors >
747 get_capacity(zv->zv_disk)) {
748 printk(KERN_INFO
749 "%s: bad access: block=%llu, count=%lu\n",
750 zv->zv_disk->disk_name,
751 (long long unsigned)offset,
752 (long unsigned)sectors);
753 error = SET_ERROR(EIO);
754 goto out1;
755 }
60101509 756
e10b0808 757 generic_start_io_acct(rw, sectors, &zv->zv_disk->part0);
60101509 758
e10b0808
AX
759 if (rw == WRITE) {
760 if (unlikely(zv->zv_flags & ZVOL_RDONLY)) {
761 error = SET_ERROR(EROFS);
762 goto out2;
763 }
30930fba 764
87dac73d 765 if (bio_is_discard(bio) || bio_is_secure_erase(bio)) {
e10b0808
AX
766 error = zvol_discard(bio);
767 goto out2;
60101509 768 }
e10b0808
AX
769
770 error = zvol_write(bio);
771 } else
772 error = zvol_read(bio);
773
774out2:
775 generic_end_io_acct(rw, &zv->zv_disk->part0, start);
776out1:
777 BIO_END_IO(bio, -error);
778 spl_fstrans_unmark(cookie);
779#ifdef HAVE_MAKE_REQUEST_FN_RET_INT
780 return (0);
94a40997
AX
781#elif defined(HAVE_MAKE_REQUEST_FN_RET_QC)
782 return (BLK_QC_T_NONE);
e10b0808 783#endif
60101509
BB
784}
785
786static void
787zvol_get_done(zgd_t *zgd, int error)
788{
789 if (zgd->zgd_db)
790 dmu_buf_rele(zgd->zgd_db, zgd);
791
792 zfs_range_unlock(zgd->zgd_rl);
793
794 if (error == 0 && zgd->zgd_bp)
795 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
796
797 kmem_free(zgd, sizeof (zgd_t));
798}
799
800/*
801 * Get data to generate a TX_WRITE intent log record.
802 */
803static int
804zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
805{
806 zvol_state_t *zv = arg;
807 objset_t *os = zv->zv_objset;
a08ee875 808 uint64_t object = ZVOL_OBJ;
60101509
BB
809 uint64_t offset = lr->lr_offset;
810 uint64_t size = lr->lr_length;
a08ee875 811 blkptr_t *bp = &lr->lr_blkptr;
60101509
BB
812 dmu_buf_t *db;
813 zgd_t *zgd;
814 int error;
815
816 ASSERT(zio != NULL);
817 ASSERT(size != 0);
818
ea04106b 819 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
60101509 820 zgd->zgd_zilog = zv->zv_zilog;
87dac73d
AX
821 zgd->zgd_rl = zfs_range_lock(&zv->zv_range_lock, offset, size,
822 RL_READER);
60101509
BB
823
824 /*
825 * Write records come in two flavors: immediate and indirect.
826 * For small writes it's cheaper to store the data with the
827 * log record (immediate); for large writes it's cheaper to
828 * sync the data and get a pointer to it (indirect) so that
829 * we don't have to write the data twice.
830 */
831 if (buf != NULL) { /* immediate write */
a08ee875 832 error = dmu_read(os, object, offset, size, buf,
60101509
BB
833 DMU_READ_NO_PREFETCH);
834 } else {
835 size = zv->zv_volblocksize;
836 offset = P2ALIGN_TYPED(offset, size, uint64_t);
a08ee875 837 error = dmu_buf_hold(os, object, offset, zgd, &db,
60101509
BB
838 DMU_READ_NO_PREFETCH);
839 if (error == 0) {
a08ee875
LG
840 blkptr_t *obp = dmu_buf_get_blkptr(db);
841 if (obp) {
842 ASSERT(BP_IS_HOLE(bp));
843 *bp = *obp;
844 }
845
60101509
BB
846 zgd->zgd_db = db;
847 zgd->zgd_bp = &lr->lr_blkptr;
848
849 ASSERT(db != NULL);
850 ASSERT(db->db_offset == offset);
851 ASSERT(db->db_size == size);
852
853 error = dmu_sync(zio, lr->lr_common.lrc_txg,
854 zvol_get_done, zgd);
855
856 if (error == 0)
857 return (0);
858 }
859 }
860
861 zvol_get_done(zgd, error);
862
a08ee875 863 return (SET_ERROR(error));
60101509
BB
864}
865
866/*
867 * The zvol_state_t's are inserted in increasing MINOR(dev_t) order.
868 */
869static void
870zvol_insert(zvol_state_t *zv_insert)
871{
872 zvol_state_t *zv = NULL;
873
874 ASSERT(MUTEX_HELD(&zvol_state_lock));
875 ASSERT3U(MINOR(zv_insert->zv_dev) & ZVOL_MINOR_MASK, ==, 0);
876 for (zv = list_head(&zvol_state_list); zv != NULL;
a08ee875 877 zv = list_next(&zvol_state_list, zv)) {
60101509
BB
878 if (MINOR(zv->zv_dev) > MINOR(zv_insert->zv_dev))
879 break;
880 }
881
882 list_insert_before(&zvol_state_list, zv, zv_insert);
883}
884
885/*
886 * Simply remove the zvol from to list of zvols.
887 */
888static void
889zvol_remove(zvol_state_t *zv_remove)
890{
891 ASSERT(MUTEX_HELD(&zvol_state_lock));
892 list_remove(&zvol_state_list, zv_remove);
893}
894
895static int
896zvol_first_open(zvol_state_t *zv)
897{
898 objset_t *os;
899 uint64_t volsize;
900 int error;
901 uint64_t ro;
902
903 /* lie and say we're read-only */
904 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, 1, zvol_tag, &os);
905 if (error)
4e820b5a
AX
906 return (SET_ERROR(-error));
907
908 zv->zv_objset = os;
909
910 error = dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL);
911 if (error)
912 goto out_owned;
60101509
BB
913
914 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
4e820b5a
AX
915 if (error)
916 goto out_owned;
60101509 917
60101509 918 error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
4e820b5a
AX
919 if (error)
920 goto out_owned;
60101509
BB
921
922 set_capacity(zv->zv_disk, volsize >> 9);
923 zv->zv_volsize = volsize;
924 zv->zv_zilog = zil_open(os, zvol_get_data);
925
a4430fce
GW
926 if (ro || dmu_objset_is_snapshot(os) ||
927 !spa_writeable(dmu_objset_spa(os))) {
babf3f9b
MM
928 set_disk_ro(zv->zv_disk, 1);
929 zv->zv_flags |= ZVOL_RDONLY;
60101509 930 } else {
babf3f9b
MM
931 set_disk_ro(zv->zv_disk, 0);
932 zv->zv_flags &= ~ZVOL_RDONLY;
60101509
BB
933 }
934
4e820b5a
AX
935out_owned:
936 if (error) {
937 dmu_objset_disown(os, zvol_tag);
938 zv->zv_objset = NULL;
939 }
babf3f9b 940
a08ee875 941 return (SET_ERROR(-error));
60101509
BB
942}
943
944static void
945zvol_last_close(zvol_state_t *zv)
946{
947 zil_close(zv->zv_zilog);
948 zv->zv_zilog = NULL;
04434775 949
60101509
BB
950 dmu_buf_rele(zv->zv_dbuf, zvol_tag);
951 zv->zv_dbuf = NULL;
04434775
MA
952
953 /*
954 * Evict cached data
955 */
956 if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) &&
957 !(zv->zv_flags & ZVOL_RDONLY))
958 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
959 (void) dmu_objset_evict_dbufs(zv->zv_objset);
960
60101509
BB
961 dmu_objset_disown(zv->zv_objset, zvol_tag);
962 zv->zv_objset = NULL;
963}
964
965static int
966zvol_open(struct block_device *bdev, fmode_t flag)
967{
4e820b5a 968 zvol_state_t *zv;
60101509
BB
969 int error = 0, drop_mutex = 0;
970
971 /*
972 * If the caller is already holding the mutex do not take it
4e820b5a 973 * again, this will happen as part of zvol_create_minor_impl().
60101509
BB
974 * Once add_disk() is called the device is live and the kernel
975 * will attempt to open it to read the partition information.
976 */
977 if (!mutex_owned(&zvol_state_lock)) {
978 mutex_enter(&zvol_state_lock);
979 drop_mutex = 1;
980 }
981
4e820b5a
AX
982 /*
983 * Obtain a copy of private_data under the lock to make sure
984 * that either the result of zvol_freeg() setting
985 * bdev->bd_disk->private_data to NULL is observed, or zvol_free()
986 * is not called on this zv because of the positive zv_open_count.
987 */
988 zv = bdev->bd_disk->private_data;
989 if (zv == NULL) {
990 error = -ENXIO;
991 goto out_mutex;
992 }
60101509
BB
993
994 if (zv->zv_open_count == 0) {
995 error = zvol_first_open(zv);
996 if (error)
997 goto out_mutex;
998 }
999
a08ee875 1000 if ((flag & FMODE_WRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
60101509
BB
1001 error = -EROFS;
1002 goto out_open_count;
1003 }
1004
1005 zv->zv_open_count++;
1006
4e820b5a
AX
1007 check_disk_change(bdev);
1008
60101509
BB
1009out_open_count:
1010 if (zv->zv_open_count == 0)
1011 zvol_last_close(zv);
1012
1013out_mutex:
1014 if (drop_mutex)
1015 mutex_exit(&zvol_state_lock);
1016
a08ee875 1017 return (SET_ERROR(error));
60101509
BB
1018}
1019
c06d4368
AX
1020#ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1021static void
1022#else
60101509 1023static int
c06d4368 1024#endif
60101509
BB
1025zvol_release(struct gendisk *disk, fmode_t mode)
1026{
1027 zvol_state_t *zv = disk->private_data;
1028 int drop_mutex = 0;
1029
4e820b5a
AX
1030 ASSERT(zv && zv->zv_open_count > 0);
1031
60101509
BB
1032 if (!mutex_owned(&zvol_state_lock)) {
1033 mutex_enter(&zvol_state_lock);
1034 drop_mutex = 1;
1035 }
1036
4e820b5a
AX
1037 zv->zv_open_count--;
1038 if (zv->zv_open_count == 0)
1039 zvol_last_close(zv);
60101509
BB
1040
1041 if (drop_mutex)
1042 mutex_exit(&zvol_state_lock);
1043
c06d4368 1044#ifndef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
60101509 1045 return (0);
c06d4368 1046#endif
60101509
BB
1047}
1048
1049static int
1050zvol_ioctl(struct block_device *bdev, fmode_t mode,
a08ee875 1051 unsigned int cmd, unsigned long arg)
60101509
BB
1052{
1053 zvol_state_t *zv = bdev->bd_disk->private_data;
1054 int error = 0;
1055
4e820b5a 1056 ASSERT(zv && zv->zv_open_count > 0);
60101509
BB
1057
1058 switch (cmd) {
1059 case BLKFLSBUF:
1060 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1061 break;
4c0d8e50
FN
1062 case BLKZNAME:
1063 error = copy_to_user((void *)arg, zv->zv_name, MAXNAMELEN);
1064 break;
60101509
BB
1065
1066 default:
1067 error = -ENOTTY;
1068 break;
1069
1070 }
1071
a08ee875 1072 return (SET_ERROR(error));
60101509
BB
1073}
1074
1075#ifdef CONFIG_COMPAT
1076static int
1077zvol_compat_ioctl(struct block_device *bdev, fmode_t mode,
a08ee875 1078 unsigned cmd, unsigned long arg)
60101509 1079{
a08ee875 1080 return (zvol_ioctl(bdev, mode, cmd, arg));
60101509
BB
1081}
1082#else
a08ee875 1083#define zvol_compat_ioctl NULL
60101509
BB
1084#endif
1085
1086static int zvol_media_changed(struct gendisk *disk)
1087{
1088 zvol_state_t *zv = disk->private_data;
1089
4e820b5a
AX
1090 ASSERT(zv && zv->zv_open_count > 0);
1091
a08ee875 1092 return (zv->zv_changed);
60101509
BB
1093}
1094
1095static int zvol_revalidate_disk(struct gendisk *disk)
1096{
1097 zvol_state_t *zv = disk->private_data;
1098
4e820b5a
AX
1099 ASSERT(zv && zv->zv_open_count > 0);
1100
60101509
BB
1101 zv->zv_changed = 0;
1102 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1103
a08ee875 1104 return (0);
60101509
BB
1105}
1106
1107/*
1108 * Provide a simple virtual geometry for legacy compatibility. For devices
1109 * smaller than 1 MiB a small head and sector count is used to allow very
1110 * tiny devices. For devices over 1 Mib a standard head and sector count
1111 * is used to keep the cylinders count reasonable.
1112 */
1113static int
1114zvol_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1115{
1116 zvol_state_t *zv = bdev->bd_disk->private_data;
4e820b5a
AX
1117 sector_t sectors;
1118
1119 ASSERT(zv && zv->zv_open_count > 0);
1120
1121 sectors = get_capacity(zv->zv_disk);
60101509
BB
1122
1123 if (sectors > 2048) {
1124 geo->heads = 16;
1125 geo->sectors = 63;
1126 } else {
1127 geo->heads = 2;
1128 geo->sectors = 4;
1129 }
1130
1131 geo->start = 0;
1132 geo->cylinders = sectors / (geo->heads * geo->sectors);
1133
a08ee875 1134 return (0);
60101509
BB
1135}
1136
1137static struct kobject *
1138zvol_probe(dev_t dev, int *part, void *arg)
1139{
1140 zvol_state_t *zv;
1141 struct kobject *kobj;
1142
1143 mutex_enter(&zvol_state_lock);
1144 zv = zvol_find_by_dev(dev);
23a61ccc 1145 kobj = zv ? get_disk(zv->zv_disk) : NULL;
60101509
BB
1146 mutex_exit(&zvol_state_lock);
1147
a08ee875 1148 return (kobj);
60101509
BB
1149}
1150
1151#ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS
1152static struct block_device_operations zvol_ops = {
a08ee875
LG
1153 .open = zvol_open,
1154 .release = zvol_release,
1155 .ioctl = zvol_ioctl,
1156 .compat_ioctl = zvol_compat_ioctl,
1157 .media_changed = zvol_media_changed,
1158 .revalidate_disk = zvol_revalidate_disk,
1159 .getgeo = zvol_getgeo,
1160 .owner = THIS_MODULE,
60101509
BB
1161};
1162
1163#else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1164
1165static int
1166zvol_open_by_inode(struct inode *inode, struct file *file)
1167{
a08ee875 1168 return (zvol_open(inode->i_bdev, file->f_mode));
60101509
BB
1169}
1170
1171static int
1172zvol_release_by_inode(struct inode *inode, struct file *file)
1173{
a08ee875 1174 return (zvol_release(inode->i_bdev->bd_disk, file->f_mode));
60101509
BB
1175}
1176
1177static int
1178zvol_ioctl_by_inode(struct inode *inode, struct file *file,
a08ee875 1179 unsigned int cmd, unsigned long arg)
60101509 1180{
b1c58213 1181 if (file == NULL || inode == NULL)
a08ee875
LG
1182 return (SET_ERROR(-EINVAL));
1183
1184 return (zvol_ioctl(inode->i_bdev, file->f_mode, cmd, arg));
60101509
BB
1185}
1186
a08ee875 1187#ifdef CONFIG_COMPAT
60101509
BB
1188static long
1189zvol_compat_ioctl_by_inode(struct file *file,
a08ee875 1190 unsigned int cmd, unsigned long arg)
60101509 1191{
b1c58213 1192 if (file == NULL)
a08ee875
LG
1193 return (SET_ERROR(-EINVAL));
1194
1195 return (zvol_compat_ioctl(file->f_dentry->d_inode->i_bdev,
1196 file->f_mode, cmd, arg));
60101509 1197}
a08ee875
LG
1198#else
1199#define zvol_compat_ioctl_by_inode NULL
1200#endif
60101509
BB
1201
1202static struct block_device_operations zvol_ops = {
a08ee875
LG
1203 .open = zvol_open_by_inode,
1204 .release = zvol_release_by_inode,
1205 .ioctl = zvol_ioctl_by_inode,
1206 .compat_ioctl = zvol_compat_ioctl_by_inode,
1207 .media_changed = zvol_media_changed,
1208 .revalidate_disk = zvol_revalidate_disk,
1209 .getgeo = zvol_getgeo,
1210 .owner = THIS_MODULE,
60101509
BB
1211};
1212#endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1213
1214/*
1215 * Allocate memory for a new zvol_state_t and setup the required
1216 * request queue and generic disk structures for the block device.
1217 */
1218static zvol_state_t *
1219zvol_alloc(dev_t dev, const char *name)
1220{
1221 zvol_state_t *zv;
1222
ea04106b 1223 zv = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
c06d4368
AX
1224
1225 spin_lock_init(&zv->zv_lock);
1226 list_link_init(&zv->zv_next);
60101509 1227
e10b0808 1228 zv->zv_queue = blk_alloc_queue(GFP_ATOMIC);
60101509
BB
1229 if (zv->zv_queue == NULL)
1230 goto out_kmem;
1231
e10b0808 1232 blk_queue_make_request(zv->zv_queue, zvol_request);
87dac73d 1233 blk_queue_set_write_cache(zv->zv_queue, B_TRUE, B_TRUE);
b18019d2 1234
60101509
BB
1235 zv->zv_disk = alloc_disk(ZVOL_MINORS);
1236 if (zv->zv_disk == NULL)
1237 goto out_queue;
1238
1239 zv->zv_queue->queuedata = zv;
1240 zv->zv_dev = dev;
1241 zv->zv_open_count = 0;
4c0d8e50 1242 strlcpy(zv->zv_name, name, MAXNAMELEN);
60101509 1243
87dac73d 1244 zfs_rlock_init(&zv->zv_range_lock);
3c4988c8 1245
60101509
BB
1246 zv->zv_disk->major = zvol_major;
1247 zv->zv_disk->first_minor = (dev & MINORMASK);
1248 zv->zv_disk->fops = &zvol_ops;
1249 zv->zv_disk->private_data = zv;
1250 zv->zv_disk->queue = zv->zv_queue;
4c0d8e50
FN
1251 snprintf(zv->zv_disk->disk_name, DISK_NAME_LEN, "%s%d",
1252 ZVOL_DEV_NAME, (dev & MINORMASK));
60101509 1253
a08ee875 1254 return (zv);
60101509
BB
1255
1256out_queue:
1257 blk_cleanup_queue(zv->zv_queue);
1258out_kmem:
1259 kmem_free(zv, sizeof (zvol_state_t));
c06d4368 1260
a08ee875 1261 return (NULL);
60101509
BB
1262}
1263
1264/*
1265 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1266 */
1267static void
1268zvol_free(zvol_state_t *zv)
1269{
4e820b5a
AX
1270 ASSERT(MUTEX_HELD(&zvol_state_lock));
1271 ASSERT(zv->zv_open_count == 0);
1272
87dac73d 1273 zfs_rlock_destroy(&zv->zv_range_lock);
60101509 1274
4e820b5a
AX
1275 zv->zv_disk->private_data = NULL;
1276
60101509
BB
1277 del_gendisk(zv->zv_disk);
1278 blk_cleanup_queue(zv->zv_queue);
1279 put_disk(zv->zv_disk);
1280
1281 kmem_free(zv, sizeof (zvol_state_t));
1282}
1283
4e820b5a
AX
1284/*
1285 * Create a block device minor node and setup the linkage between it
1286 * and the specified volume. Once this function returns the block
1287 * device is live and ready for use.
1288 */
60101509 1289static int
4e820b5a 1290zvol_create_minor_impl(const char *name)
60101509
BB
1291{
1292 zvol_state_t *zv;
1293 objset_t *os;
1294 dmu_object_info_t *doi;
1295 uint64_t volsize;
e10b0808 1296 uint64_t len;
60101509
BB
1297 unsigned minor = 0;
1298 int error = 0;
1299
4e820b5a 1300 mutex_enter(&zvol_state_lock);
60101509
BB
1301
1302 zv = zvol_find_by_name(name);
1303 if (zv) {
a08ee875 1304 error = SET_ERROR(EEXIST);
60101509
BB
1305 goto out;
1306 }
1307
ea04106b 1308 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
60101509
BB
1309
1310 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
1311 if (error)
1312 goto out_doi;
1313
1314 error = dmu_object_info(os, ZVOL_OBJ, doi);
1315 if (error)
1316 goto out_dmu_objset_disown;
1317
1318 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
1319 if (error)
1320 goto out_dmu_objset_disown;
1321
1322 error = zvol_find_minor(&minor);
1323 if (error)
1324 goto out_dmu_objset_disown;
1325
1326 zv = zvol_alloc(MKDEV(zvol_major, minor), name);
1327 if (zv == NULL) {
a08ee875 1328 error = SET_ERROR(EAGAIN);
60101509
BB
1329 goto out_dmu_objset_disown;
1330 }
1331
1332 if (dmu_objset_is_snapshot(os))
1333 zv->zv_flags |= ZVOL_RDONLY;
1334
1335 zv->zv_volblocksize = doi->doi_data_block_size;
1336 zv->zv_volsize = volsize;
1337 zv->zv_objset = os;
1338
1339 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1340
e10b0808 1341 blk_queue_max_hw_sectors(zv->zv_queue, (DMU_MAX_ACCESS / 4) >> 9);
34037afe
ED
1342 blk_queue_max_segments(zv->zv_queue, UINT16_MAX);
1343 blk_queue_max_segment_size(zv->zv_queue, UINT_MAX);
1344 blk_queue_physical_block_size(zv->zv_queue, zv->zv_volblocksize);
1345 blk_queue_io_opt(zv->zv_queue, zv->zv_volblocksize);
7c0e5708
ED
1346 blk_queue_max_discard_sectors(zv->zv_queue,
1347 (zvol_max_discard_blocks * zv->zv_volblocksize) >> 9);
ee5fd0bb 1348 blk_queue_discard_granularity(zv->zv_queue, zv->zv_volblocksize);
30930fba 1349 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zv->zv_queue);
e10b0808 1350#ifdef QUEUE_FLAG_NONROT
34037afe
ED
1351 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zv->zv_queue);
1352#endif
e10b0808
AX
1353#ifdef QUEUE_FLAG_ADD_RANDOM
1354 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, zv->zv_queue);
1355#endif
34037afe 1356
a4430fce
GW
1357 if (spa_writeable(dmu_objset_spa(os))) {
1358 if (zil_replay_disable)
1359 zil_destroy(dmu_objset_zil(os), B_FALSE);
1360 else
1361 zil_replay(os, zv, zvol_replay_vector);
1362 }
60101509 1363
e10b0808
AX
1364 /*
1365 * When udev detects the addition of the device it will immediately
1366 * invoke blkid(8) to determine the type of content on the device.
1367 * Prefetching the blocks commonly scanned by blkid(8) will speed
1368 * up this process.
1369 */
1370 len = MIN(MAX(zvol_prefetch_bytes, 0), SPA_MAXBLOCKSIZE);
1371 if (len > 0) {
1372 dmu_prefetch(os, ZVOL_OBJ, 0, len);
1373 dmu_prefetch(os, ZVOL_OBJ, volsize - len, len);
1374 }
1375
f74a147c 1376 zv->zv_objset = NULL;
60101509
BB
1377out_dmu_objset_disown:
1378 dmu_objset_disown(os, zvol_tag);
60101509 1379out_doi:
a08ee875 1380 kmem_free(doi, sizeof (dmu_object_info_t));
60101509
BB
1381out:
1382
1383 if (error == 0) {
1384 zvol_insert(zv);
4e820b5a
AX
1385 /*
1386 * Drop the lock to prevent deadlock with sys_open() ->
1387 * zvol_open(), which first takes bd_disk->bd_mutex and then
1388 * takes zvol_state_lock, whereas this code path first takes
1389 * zvol_state_lock, and then takes bd_disk->bd_mutex.
1390 */
1391 mutex_exit(&zvol_state_lock);
60101509 1392 add_disk(zv->zv_disk);
4e820b5a
AX
1393 } else {
1394 mutex_exit(&zvol_state_lock);
60101509
BB
1395 }
1396
a08ee875 1397 return (SET_ERROR(error));
60101509
BB
1398}
1399
a08ee875
LG
1400/*
1401 * Rename a block device minor mode for the specified volume.
1402 */
1403static void
4e820b5a 1404zvol_rename_minor(zvol_state_t *zv, const char *newname)
a08ee875
LG
1405{
1406 int readonly = get_disk_ro(zv->zv_disk);
1407
1408 ASSERT(MUTEX_HELD(&zvol_state_lock));
1409
1410 strlcpy(zv->zv_name, newname, sizeof (zv->zv_name));
1411
1412 /*
1413 * The block device's read-only state is briefly changed causing
1414 * a KOBJ_CHANGE uevent to be issued. This ensures udev detects
1415 * the name change and fixes the symlinks. This does not change
1416 * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
1417 * changes. This would normally be done using kobject_uevent() but
1418 * that is a GPL-only symbol which is why we need this workaround.
1419 */
1420 set_disk_ro(zv->zv_disk, !readonly);
1421 set_disk_ro(zv->zv_disk, readonly);
60101509
BB
1422}
1423
4e820b5a
AX
1424
1425/*
1426 * Mask errors to continue dmu_objset_find() traversal
1427 */
1428static int
1429zvol_create_snap_minor_cb(const char *dsname, void *arg)
1430{
1431 const char *name = (const char *)arg;
1432
1433 ASSERT0(MUTEX_HELD(&spa_namespace_lock));
1434
1435 /* skip the designated dataset */
1436 if (name && strcmp(dsname, name) == 0)
1437 return (0);
1438
1439 /* at this point, the dsname should name a snapshot */
1440 if (strchr(dsname, '@') == 0) {
1441 dprintf("zvol_create_snap_minor_cb(): "
1442 "%s is not a shapshot name\n", dsname);
1443 } else {
1444 (void) zvol_create_minor_impl(dsname);
1445 }
1446
1447 return (0);
1448}
1449
1450/*
1451 * Mask errors to continue dmu_objset_find() traversal
1452 */
60101509 1453static int
a08ee875 1454zvol_create_minors_cb(const char *dsname, void *arg)
60101509 1455{
4e820b5a
AX
1456 uint64_t snapdev;
1457 int error;
1458
1459 ASSERT0(MUTEX_HELD(&spa_namespace_lock));
1460
1461 error = dsl_prop_get_integer(dsname, "snapdev", &snapdev, NULL);
1462 if (error)
1463 return (0);
1464
1465 /*
1466 * Given the name and the 'snapdev' property, create device minor nodes
1467 * with the linkages to zvols/snapshots as needed.
1468 * If the name represents a zvol, create a minor node for the zvol, then
1469 * check if its snapshots are 'visible', and if so, iterate over the
1470 * snapshots and create device minor nodes for those.
1471 */
1472 if (strchr(dsname, '@') == 0) {
1473 /* create minor for the 'dsname' explicitly */
1474 error = zvol_create_minor_impl(dsname);
1475 if ((error == 0 || error == EEXIST) &&
1476 (snapdev == ZFS_SNAPDEV_VISIBLE)) {
1477 fstrans_cookie_t cookie = spl_fstrans_mark();
1478 /*
1479 * traverse snapshots only, do not traverse children,
1480 * and skip the 'dsname'
1481 */
1482 error = dmu_objset_find((char *)dsname,
1483 zvol_create_snap_minor_cb, (void *)dsname,
1484 DS_FIND_SNAPSHOTS);
1485 spl_fstrans_unmark(cookie);
1486 }
1487 } else {
1488 dprintf("zvol_create_minors_cb(): %s is not a zvol name\n",
1489 dsname);
1490 }
60101509 1491
d5674448 1492 return (0);
60101509
BB
1493}
1494
1495/*
4e820b5a
AX
1496 * Create minors for the specified dataset, including children and snapshots.
1497 * Pay attention to the 'snapdev' property and iterate over the snapshots
1498 * only if they are 'visible'. This approach allows one to assure that the
1499 * snapshot metadata is read from disk only if it is needed.
1500 *
1501 * The name can represent a dataset to be recursively scanned for zvols and
1502 * their snapshots, or a single zvol snapshot. If the name represents a
1503 * dataset, the scan is performed in two nested stages:
1504 * - scan the dataset for zvols, and
1505 * - for each zvol, create a minor node, then check if the zvol's snapshots
1506 * are 'visible', and only then iterate over the snapshots if needed
1507 *
1508 * If the name represents a snapshot, a check is perfromed if the snapshot is
1509 * 'visible' (which also verifies that the parent is a zvol), and if so,
1510 * a minor node for that snapshot is created.
60101509 1511 */
4e820b5a
AX
1512static int
1513zvol_create_minors_impl(const char *name)
60101509 1514{
60101509 1515 int error = 0;
4e820b5a
AX
1516 fstrans_cookie_t cookie;
1517 char *atp, *parent;
1518
1519 if (zvol_inhibit_dev)
1520 return (0);
1521
1522 parent = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1523 (void) strlcpy(parent, name, MAXPATHLEN);
1524
1525 if ((atp = strrchr(parent, '@')) != NULL) {
1526 uint64_t snapdev;
1527
1528 *atp = '\0';
1529 error = dsl_prop_get_integer(parent, "snapdev",
1530 &snapdev, NULL);
1531
1532 if (error == 0 && snapdev == ZFS_SNAPDEV_VISIBLE)
1533 error = zvol_create_minor_impl(name);
1534 } else {
1535 cookie = spl_fstrans_mark();
1536 error = dmu_objset_find(parent, zvol_create_minors_cb,
1537 NULL, DS_FIND_CHILDREN);
1538 spl_fstrans_unmark(cookie);
1539 }
60101509 1540
4e820b5a 1541 kmem_free(parent, MAXPATHLEN);
a08ee875
LG
1542
1543 return (SET_ERROR(error));
1544}
1545
1546/*
1547 * Remove minors for specified dataset including children and snapshots.
1548 */
4e820b5a
AX
1549static void
1550zvol_remove_minors_impl(const char *name)
a08ee875
LG
1551{
1552 zvol_state_t *zv, *zv_next;
1553 int namelen = ((name) ? strlen(name) : 0);
1554
74497b7a 1555 if (zvol_inhibit_dev)
a08ee875 1556 return;
74497b7a 1557
60101509 1558 mutex_enter(&zvol_state_lock);
a08ee875
LG
1559
1560 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1561 zv_next = list_next(&zvol_state_list, zv);
1562
1563 if (name == NULL || strcmp(zv->zv_name, name) == 0 ||
1564 (strncmp(zv->zv_name, name, namelen) == 0 &&
4e820b5a
AX
1565 (zv->zv_name[namelen] == '/' ||
1566 zv->zv_name[namelen] == '@'))) {
1567
1568 /* If in use, leave alone */
1569 if (zv->zv_open_count > 0)
1570 continue;
1571
1572 zvol_remove(zv);
1573 zvol_free(zv);
1574 }
1575 }
1576
1577 mutex_exit(&zvol_state_lock);
1578}
1579
1580/* Remove minor for this specific snapshot only */
1581static void
1582zvol_remove_minor_impl(const char *name)
1583{
1584 zvol_state_t *zv, *zv_next;
1585
1586 if (zvol_inhibit_dev)
1587 return;
1588
1589 if (strchr(name, '@') == NULL)
1590 return;
1591
1592 mutex_enter(&zvol_state_lock);
1593
1594 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1595 zv_next = list_next(&zvol_state_list, zv);
1596
1597 if (strcmp(zv->zv_name, name) == 0) {
1598 /* If in use, leave alone */
1599 if (zv->zv_open_count > 0)
1600 continue;
a08ee875
LG
1601 zvol_remove(zv);
1602 zvol_free(zv);
4e820b5a 1603 break;
60101509 1604 }
60101509 1605 }
60101509 1606
a08ee875 1607 mutex_exit(&zvol_state_lock);
60101509
BB
1608}
1609
1610/*
a08ee875 1611 * Rename minors for specified dataset including children and snapshots.
60101509 1612 */
4e820b5a
AX
1613static void
1614zvol_rename_minors_impl(const char *oldname, const char *newname)
60101509
BB
1615{
1616 zvol_state_t *zv, *zv_next;
a08ee875
LG
1617 int oldnamelen, newnamelen;
1618 char *name;
60101509 1619
74497b7a
DH
1620 if (zvol_inhibit_dev)
1621 return;
1622
a08ee875
LG
1623 oldnamelen = strlen(oldname);
1624 newnamelen = strlen(newname);
ea04106b 1625 name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
60101509
BB
1626
1627 mutex_enter(&zvol_state_lock);
a08ee875 1628
60101509
BB
1629 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1630 zv_next = list_next(&zvol_state_list, zv);
1631
4e820b5a
AX
1632 /* If in use, leave alone */
1633 if (zv->zv_open_count > 0)
1634 continue;
1635
a08ee875 1636 if (strcmp(zv->zv_name, oldname) == 0) {
4e820b5a 1637 zvol_rename_minor(zv, newname);
a08ee875
LG
1638 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 &&
1639 (zv->zv_name[oldnamelen] == '/' ||
1640 zv->zv_name[oldnamelen] == '@')) {
1641 snprintf(name, MAXNAMELEN, "%s%c%s", newname,
1642 zv->zv_name[oldnamelen],
1643 zv->zv_name + oldnamelen + 1);
4e820b5a 1644 zvol_rename_minor(zv, name);
60101509
BB
1645 }
1646 }
a08ee875 1647
60101509 1648 mutex_exit(&zvol_state_lock);
a08ee875
LG
1649
1650 kmem_free(name, MAXNAMELEN);
60101509
BB
1651}
1652
4e820b5a
AX
1653typedef struct zvol_snapdev_cb_arg {
1654 uint64_t snapdev;
1655} zvol_snapdev_cb_arg_t;
1656
0b4d1b58 1657static int
4e820b5a
AX
1658zvol_set_snapdev_cb(const char *dsname, void *param) {
1659 zvol_snapdev_cb_arg_t *arg = param;
0b4d1b58
ED
1660
1661 if (strchr(dsname, '@') == NULL)
a08ee875 1662 return (0);
0b4d1b58 1663
4e820b5a 1664 switch (arg->snapdev) {
0b4d1b58 1665 case ZFS_SNAPDEV_VISIBLE:
4e820b5a 1666 (void) zvol_create_minor_impl(dsname);
0b4d1b58
ED
1667 break;
1668 case ZFS_SNAPDEV_HIDDEN:
4e820b5a 1669 (void) zvol_remove_minor_impl(dsname);
0b4d1b58
ED
1670 break;
1671 }
a08ee875
LG
1672
1673 return (0);
0b4d1b58
ED
1674}
1675
4e820b5a
AX
1676static void
1677zvol_set_snapdev_impl(char *name, uint64_t snapdev)
1678{
1679 zvol_snapdev_cb_arg_t arg = {snapdev};
1680 fstrans_cookie_t cookie = spl_fstrans_mark();
1681 /*
1682 * The zvol_set_snapdev_sync() sets snapdev appropriately
1683 * in the dataset hierarchy. Here, we only scan snapshots.
1684 */
1685 dmu_objset_find(name, zvol_set_snapdev_cb, &arg, DS_FIND_SNAPSHOTS);
1686 spl_fstrans_unmark(cookie);
1687}
1688
1689static zvol_task_t *
1690zvol_task_alloc(zvol_async_op_t op, const char *name1, const char *name2,
1691 uint64_t snapdev)
1692{
1693 zvol_task_t *task;
1694 char *delim;
1695
1696 /* Never allow tasks on hidden names. */
1697 if (name1[0] == '$')
1698 return (NULL);
1699
1700 task = kmem_zalloc(sizeof (zvol_task_t), KM_SLEEP);
1701 task->op = op;
1702 task->snapdev = snapdev;
1703 delim = strchr(name1, '/');
1704 strlcpy(task->pool, name1, delim ? (delim - name1 + 1) : MAXNAMELEN);
1705
1706 strlcpy(task->name1, name1, MAXNAMELEN);
1707 if (name2 != NULL)
1708 strlcpy(task->name2, name2, MAXNAMELEN);
1709
1710 return (task);
1711}
1712
1713static void
1714zvol_task_free(zvol_task_t *task)
1715{
1716 kmem_free(task, sizeof (zvol_task_t));
1717}
1718
1719/*
1720 * The worker thread function performed asynchronously.
1721 */
1722static void
1723zvol_task_cb(void *param)
1724{
1725 zvol_task_t *task = (zvol_task_t *)param;
1726
1727 switch (task->op) {
1728 case ZVOL_ASYNC_CREATE_MINORS:
1729 (void) zvol_create_minors_impl(task->name1);
1730 break;
1731 case ZVOL_ASYNC_REMOVE_MINORS:
1732 zvol_remove_minors_impl(task->name1);
1733 break;
1734 case ZVOL_ASYNC_RENAME_MINORS:
1735 zvol_rename_minors_impl(task->name1, task->name2);
1736 break;
1737 case ZVOL_ASYNC_SET_SNAPDEV:
1738 zvol_set_snapdev_impl(task->name1, task->snapdev);
1739 break;
1740 default:
1741 VERIFY(0);
1742 break;
1743 }
1744
1745 zvol_task_free(task);
1746}
1747
1748typedef struct zvol_set_snapdev_arg {
1749 const char *zsda_name;
1750 uint64_t zsda_value;
1751 zprop_source_t zsda_source;
1752 dmu_tx_t *zsda_tx;
1753} zvol_set_snapdev_arg_t;
1754
1755/*
1756 * Sanity check the dataset for safe use by the sync task. No additional
1757 * conditions are imposed.
1758 */
1759static int
1760zvol_set_snapdev_check(void *arg, dmu_tx_t *tx)
1761{
1762 zvol_set_snapdev_arg_t *zsda = arg;
1763 dsl_pool_t *dp = dmu_tx_pool(tx);
1764 dsl_dir_t *dd;
1765 int error;
1766
1767 error = dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL);
1768 if (error != 0)
1769 return (error);
1770
1771 dsl_dir_rele(dd, FTAG);
1772
1773 return (error);
1774}
1775
1776static int
1777zvol_set_snapdev_sync_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1778{
1779 zvol_set_snapdev_arg_t *zsda = arg;
1780 char dsname[MAXNAMELEN];
1781 zvol_task_t *task;
1782
1783 dsl_dataset_name(ds, dsname);
1784 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_SNAPDEV),
1785 zsda->zsda_source, sizeof (zsda->zsda_value), 1,
1786 &zsda->zsda_value, zsda->zsda_tx);
1787
1788 task = zvol_task_alloc(ZVOL_ASYNC_SET_SNAPDEV, dsname,
1789 NULL, zsda->zsda_value);
1790 if (task == NULL)
1791 return (0);
1792
1793 (void) taskq_dispatch(dp->dp_spa->spa_zvol_taskq, zvol_task_cb,
1794 task, TQ_SLEEP);
1795 return (0);
1796}
1797
1798/*
1799 * Traverse all child snapshot datasets and apply snapdev appropriately.
1800 */
1801static void
1802zvol_set_snapdev_sync(void *arg, dmu_tx_t *tx)
1803{
1804 zvol_set_snapdev_arg_t *zsda = arg;
1805 dsl_pool_t *dp = dmu_tx_pool(tx);
1806 dsl_dir_t *dd;
1807
1808 VERIFY0(dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL));
1809 zsda->zsda_tx = tx;
1810
1811 dmu_objset_find_dp(dp, dd->dd_object, zvol_set_snapdev_sync_cb,
1812 zsda, DS_FIND_CHILDREN);
1813
1814 dsl_dir_rele(dd, FTAG);
1815}
1816
0b4d1b58 1817int
4e820b5a
AX
1818zvol_set_snapdev(const char *ddname, zprop_source_t source, uint64_t snapdev)
1819{
1820 zvol_set_snapdev_arg_t zsda;
1821
1822 zsda.zsda_name = ddname;
1823 zsda.zsda_source = source;
1824 zsda.zsda_value = snapdev;
1825
1826 return (dsl_sync_task(ddname, zvol_set_snapdev_check,
1827 zvol_set_snapdev_sync, &zsda, 0, ZFS_SPACE_CHECK_NONE));
1828}
1829
1830void
1831zvol_create_minors(spa_t *spa, const char *name, boolean_t async)
1832{
1833 zvol_task_t *task;
1834 taskqid_t id;
1835
1836 task = zvol_task_alloc(ZVOL_ASYNC_CREATE_MINORS, name, NULL, ~0ULL);
1837 if (task == NULL)
1838 return;
1839
1840 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
1841 if ((async == B_FALSE) && (id != 0))
1842 taskq_wait_id(spa->spa_zvol_taskq, id);
1843}
1844
1845void
1846zvol_remove_minors(spa_t *spa, const char *name, boolean_t async)
1847{
1848 zvol_task_t *task;
1849 taskqid_t id;
1850
1851 task = zvol_task_alloc(ZVOL_ASYNC_REMOVE_MINORS, name, NULL, ~0ULL);
1852 if (task == NULL)
1853 return;
1854
1855 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
1856 if ((async == B_FALSE) && (id != 0))
1857 taskq_wait_id(spa->spa_zvol_taskq, id);
1858}
1859
1860void
1861zvol_rename_minors(spa_t *spa, const char *name1, const char *name2,
1862 boolean_t async)
1863{
1864 zvol_task_t *task;
1865 taskqid_t id;
1866
1867 task = zvol_task_alloc(ZVOL_ASYNC_RENAME_MINORS, name1, name2, ~0ULL);
1868 if (task == NULL)
1869 return;
1870
1871 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
1872 if ((async == B_FALSE) && (id != 0))
1873 taskq_wait_id(spa->spa_zvol_taskq, id);
0b4d1b58
ED
1874}
1875
60101509
BB
1876int
1877zvol_init(void)
1878{
1879 int error;
1880
c06d4368 1881 list_create(&zvol_state_list, sizeof (zvol_state_t),
a08ee875 1882 offsetof(zvol_state_t, zv_next));
c06d4368
AX
1883 mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1884
60101509
BB
1885 error = register_blkdev(zvol_major, ZVOL_DRIVER);
1886 if (error) {
1887 printk(KERN_INFO "ZFS: register_blkdev() failed %d\n", error);
e10b0808 1888 goto out;
60101509
BB
1889 }
1890
1891 blk_register_region(MKDEV(zvol_major, 0), 1UL << MINORBITS,
a08ee875 1892 THIS_MODULE, zvol_probe, NULL, NULL);
60101509 1893
c06d4368 1894 return (0);
60101509 1895
e10b0808 1896out:
c06d4368
AX
1897 mutex_destroy(&zvol_state_lock);
1898 list_destroy(&zvol_state_list);
60101509 1899
a08ee875 1900 return (SET_ERROR(error));
60101509
BB
1901}
1902
1903void
1904zvol_fini(void)
1905{
4e820b5a
AX
1906 zvol_remove_minors_impl(NULL);
1907
60101509
BB
1908 blk_unregister_region(MKDEV(zvol_major, 0), 1UL << MINORBITS);
1909 unregister_blkdev(zvol_major, ZVOL_DRIVER);
4e820b5a 1910
60101509 1911 list_destroy(&zvol_state_list);
4e820b5a 1912 mutex_destroy(&zvol_state_lock);
60101509
BB
1913}
1914
74497b7a
DH
1915module_param(zvol_inhibit_dev, uint, 0644);
1916MODULE_PARM_DESC(zvol_inhibit_dev, "Do not create zvol device nodes");
1917
30a9524e 1918module_param(zvol_major, uint, 0444);
60101509
BB
1919MODULE_PARM_DESC(zvol_major, "Major number for zvol device");
1920
7c0e5708 1921module_param(zvol_max_discard_blocks, ulong, 0444);
a08ee875 1922MODULE_PARM_DESC(zvol_max_discard_blocks, "Max number of blocks to discard");
e10b0808
AX
1923
1924module_param(zvol_prefetch_bytes, uint, 0644);
1925MODULE_PARM_DESC(zvol_prefetch_bytes, "Prefetch N bytes at zvol start+end");