]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zvol.c
Fix manual description of zfs_arc_dnode_limit
[mirror_zfs.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.
460a0213
DM
36 *
37 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
5428dc51 38 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
60101509
BB
39 */
40
5559ba09
BP
41/*
42 * Note on locking of zvol state structures.
43 *
44 * These structures are used to maintain internal state used to emulate block
45 * devices on top of zvols. In particular, management of device minor number
46 * operations - create, remove, rename, and set_snapdev - involves access to
47 * these structures. The zvol_state_lock is primarily used to protect the
48 * zvol_state_list. The zv->zv_state_lock is used to protect the contents
49 * of the zvol_state_t structures, as well as to make sure that when the
50 * time comes to remove the structure from the list, it is not in use, and
51 * therefore, it can be taken off zvol_state_list and freed.
52 *
53 * The minor operations are issued to the spa->spa_zvol_taskq quues, that are
54 * single-threaded (to preserve order of minor operations), and are executed
55 * through the zvol_task_cb that dispatches the specific operations. Therefore,
56 * these operations are serialized per pool. Consequently, we can be certain
57 * that for a given zvol, there is only one operation at a time in progress.
58 * That is why one can be sure that first, zvol_state_t for a given zvol is
59 * allocated and placed on zvol_state_list, and then other minor operations
60 * for this zvol are going to proceed in the order of issue.
61 *
62 * It is also worth keeping in mind that once add_disk() is called, the zvol is
63 * announced to the world, and zvol_open()/zvol_release() can be called at any
64 * time. Incidentally, add_disk() itself calls zvol_open()->zvol_first_open()
65 * and zvol_release()->zvol_last_close() directly as well.
66 */
67
03c6040b 68#include <sys/dbuf.h>
60101509
BB
69#include <sys/dmu_traverse.h>
70#include <sys/dsl_dataset.h>
71#include <sys/dsl_prop.h>
a0bd735a 72#include <sys/dsl_dir.h>
60101509 73#include <sys/zap.h>
4cb7b9c5 74#include <sys/zfeature.h>
60101509 75#include <sys/zil_impl.h>
460a0213 76#include <sys/dmu_tx.h>
60101509
BB
77#include <sys/zio.h>
78#include <sys/zfs_rlock.h>
79#include <sys/zfs_znode.h>
a0bd735a 80#include <sys/spa_impl.h>
60101509 81#include <sys/zvol.h>
61e90960 82#include <linux/blkdev_compat.h>
60101509 83
74497b7a 84unsigned int zvol_inhibit_dev = 0;
60101509 85unsigned int zvol_major = ZVOL_MAJOR;
692e55b8 86unsigned int zvol_threads = 32;
8fa5250f 87unsigned int zvol_request_sync = 0;
9965059a 88unsigned int zvol_prefetch_bytes = (128 * 1024);
7c0e5708 89unsigned long zvol_max_discard_blocks = 16384;
60101509 90
692e55b8 91static taskq_t *zvol_taskq;
60101509
BB
92static kmutex_t zvol_state_lock;
93static list_t zvol_state_list;
60101509 94
d45e010d
CC
95#define ZVOL_HT_SIZE 1024
96static struct hlist_head *zvol_htable;
97#define ZVOL_HT_HEAD(hash) (&zvol_htable[(hash) & (ZVOL_HT_SIZE-1)])
4a5d7f82
MA
98
99static struct ida zvol_ida;
d45e010d 100
60101509
BB
101/*
102 * The in-core state of each volume.
103 */
040dab99 104struct zvol_state {
4c0d8e50 105 char zv_name[MAXNAMELEN]; /* name */
ce37ebd2
BB
106 uint64_t zv_volsize; /* advertised space */
107 uint64_t zv_volblocksize; /* volume block size */
60101509
BB
108 objset_t *zv_objset; /* objset handle */
109 uint32_t zv_flags; /* ZVOL_* flags */
110 uint32_t zv_open_count; /* open counts */
111 uint32_t zv_changed; /* disk changed */
112 zilog_t *zv_zilog; /* ZIL handle */
d88895a0 113 zfs_rlock_t zv_range_lock; /* range lock */
5228cf01 114 dnode_t *zv_dn; /* dnode hold */
60101509
BB
115 dev_t zv_dev; /* device id */
116 struct gendisk *zv_disk; /* generic disk */
117 struct request_queue *zv_queue; /* request queue */
60101509 118 list_node_t zv_next; /* next zvol_state_t linkage */
d45e010d
CC
119 uint64_t zv_hash; /* name hash */
120 struct hlist_node zv_hlink; /* hash link */
5559ba09 121 kmutex_t zv_state_lock; /* protects zvol_state_t */
040dab99
CC
122 atomic_t zv_suspend_ref; /* refcount for suspend */
123 krwlock_t zv_suspend_lock; /* suspend lock */
124};
60101509 125
a0bd735a
BP
126typedef enum {
127 ZVOL_ASYNC_CREATE_MINORS,
128 ZVOL_ASYNC_REMOVE_MINORS,
129 ZVOL_ASYNC_RENAME_MINORS,
130 ZVOL_ASYNC_SET_SNAPDEV,
131 ZVOL_ASYNC_MAX
132} zvol_async_op_t;
133
134typedef struct {
135 zvol_async_op_t op;
136 char pool[MAXNAMELEN];
137 char name1[MAXNAMELEN];
138 char name2[MAXNAMELEN];
139 zprop_source_t source;
140 uint64_t snapdev;
141} zvol_task_t;
142
60101509
BB
143#define ZVOL_RDONLY 0x1
144
d45e010d
CC
145static uint64_t
146zvol_name_hash(const char *name)
60101509 147{
d45e010d
CC
148 int i;
149 uint64_t crc = -1ULL;
150 uint8_t *p = (uint8_t *)name;
151 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
152 for (i = 0; i < MAXNAMELEN - 1 && *p; i++, p++) {
153 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (*p)) & 0xFF];
60101509 154 }
d45e010d 155 return (crc);
60101509
BB
156}
157
158/*
159 * Find a zvol_state_t given the full major+minor dev_t.
160 */
161static zvol_state_t *
162zvol_find_by_dev(dev_t dev)
163{
164 zvol_state_t *zv;
165
166 ASSERT(MUTEX_HELD(&zvol_state_lock));
167 for (zv = list_head(&zvol_state_list); zv != NULL;
ce37ebd2 168 zv = list_next(&zvol_state_list, zv)) {
60101509 169 if (zv->zv_dev == dev)
ce37ebd2 170 return (zv);
60101509
BB
171 }
172
ce37ebd2 173 return (NULL);
60101509
BB
174}
175
176/*
d45e010d 177 * Find a zvol_state_t given the name and hash generated by zvol_name_hash.
60101509
BB
178 */
179static zvol_state_t *
d45e010d 180zvol_find_by_name_hash(const char *name, uint64_t hash)
60101509
BB
181{
182 zvol_state_t *zv;
d45e010d 183 struct hlist_node *p;
60101509
BB
184
185 ASSERT(MUTEX_HELD(&zvol_state_lock));
d45e010d
CC
186 hlist_for_each(p, ZVOL_HT_HEAD(hash)) {
187 zv = hlist_entry(p, zvol_state_t, zv_hlink);
188 if (zv->zv_hash == hash &&
189 strncmp(zv->zv_name, name, MAXNAMELEN) == 0)
ce37ebd2 190 return (zv);
60101509 191 }
ce37ebd2 192 return (NULL);
60101509
BB
193}
194
d45e010d
CC
195/*
196 * Find a zvol_state_t given the name provided at zvol_alloc() time.
197 */
198static zvol_state_t *
199zvol_find_by_name(const char *name)
200{
201 return (zvol_find_by_name_hash(name, zvol_name_hash(name)));
202}
203
6c285672
JL
204
205/*
206 * Given a path, return TRUE if path is a ZVOL.
207 */
208boolean_t
209zvol_is_zvol(const char *device)
210{
211 struct block_device *bdev;
212 unsigned int major;
213
e02aaf17 214 bdev = vdev_lookup_bdev(device);
6c285672
JL
215 if (IS_ERR(bdev))
216 return (B_FALSE);
217
218 major = MAJOR(bdev->bd_dev);
219 bdput(bdev);
220
221 if (major == zvol_major)
ce37ebd2 222 return (B_TRUE);
6c285672
JL
223
224 return (B_FALSE);
225}
226
60101509
BB
227/*
228 * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation.
229 */
230void
231zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
232{
233 zfs_creat_t *zct = arg;
234 nvlist_t *nvprops = zct->zct_props;
235 int error;
236 uint64_t volblocksize, volsize;
237
238 VERIFY(nvlist_lookup_uint64(nvprops,
239 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
240 if (nvlist_lookup_uint64(nvprops,
241 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
242 volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
243
244 /*
245 * These properties must be removed from the list so the generic
246 * property setting step won't apply to them.
247 */
248 VERIFY(nvlist_remove_all(nvprops,
249 zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
250 (void) nvlist_remove_all(nvprops,
251 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
252
253 error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
254 DMU_OT_NONE, 0, tx);
255 ASSERT(error == 0);
256
257 error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
258 DMU_OT_NONE, 0, tx);
259 ASSERT(error == 0);
260
261 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
262 ASSERT(error == 0);
263}
264
265/*
266 * ZFS_IOC_OBJSET_STATS entry point.
267 */
268int
269zvol_get_stats(objset_t *os, nvlist_t *nv)
270{
271 int error;
272 dmu_object_info_t *doi;
273 uint64_t val;
274
275 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
276 if (error)
ce37ebd2 277 return (SET_ERROR(error));
60101509
BB
278
279 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
ce37ebd2 280 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
60101509
BB
281 error = dmu_object_info(os, ZVOL_OBJ, doi);
282
283 if (error == 0) {
284 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
285 doi->doi_data_block_size);
286 }
287
ce37ebd2 288 kmem_free(doi, sizeof (dmu_object_info_t));
60101509 289
ce37ebd2 290 return (SET_ERROR(error));
60101509
BB
291}
292
35d3e322
BB
293static void
294zvol_size_changed(zvol_state_t *zv, uint64_t volsize)
295{
296 struct block_device *bdev;
297
298 bdev = bdget_disk(zv->zv_disk, 0);
299 if (bdev == NULL)
300 return;
35d3e322
BB
301 set_capacity(zv->zv_disk, volsize >> 9);
302 zv->zv_volsize = volsize;
303 check_disk_size_change(zv->zv_disk, bdev);
35d3e322
BB
304
305 bdput(bdev);
306}
307
60101509
BB
308/*
309 * Sanity check volume size.
310 */
311int
312zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
313{
314 if (volsize == 0)
2e528b49 315 return (SET_ERROR(EINVAL));
60101509
BB
316
317 if (volsize % blocksize != 0)
2e528b49 318 return (SET_ERROR(EINVAL));
60101509
BB
319
320#ifdef _ILP32
82ec9d41 321 if (volsize - 1 > SPEC_MAXOFFSET_T)
2e528b49 322 return (SET_ERROR(EOVERFLOW));
60101509
BB
323#endif
324 return (0);
325}
326
327/*
328 * Ensure the zap is flushed then inform the VFS of the capacity change.
329 */
330static int
35d3e322 331zvol_update_volsize(uint64_t volsize, objset_t *os)
60101509 332{
60101509
BB
333 dmu_tx_t *tx;
334 int error;
513168ab 335 uint64_t txg;
60101509 336
df554c14 337 tx = dmu_tx_create(os);
60101509 338 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
19d55079 339 dmu_tx_mark_netfree(tx);
60101509
BB
340 error = dmu_tx_assign(tx, TXG_WAIT);
341 if (error) {
342 dmu_tx_abort(tx);
ce37ebd2 343 return (SET_ERROR(error));
60101509 344 }
513168ab 345 txg = dmu_tx_get_txg(tx);
60101509 346
df554c14 347 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
60101509
BB
348 &volsize, tx);
349 dmu_tx_commit(tx);
350
513168ab 351 txg_wait_synced(dmu_objset_pool(os), txg);
352
35d3e322
BB
353 if (error == 0)
354 error = dmu_free_long_range(os,
355 ZVOL_OBJ, volsize, DMU_OBJECT_END);
60101509 356
35d3e322
BB
357 return (error);
358}
60101509 359
35d3e322
BB
360static int
361zvol_update_live_volsize(zvol_state_t *zv, uint64_t volsize)
362{
363 zvol_size_changed(zv, volsize);
60101509 364
35d3e322
BB
365 /*
366 * We should post a event here describing the expansion. However,
367 * the zfs_ereport_post() interface doesn't nicely support posting
368 * events for zvols, it assumes events relate to vdevs or zios.
369 */
60101509
BB
370
371 return (0);
372}
373
374/*
375 * Set ZFS_PROP_VOLSIZE set entry point.
376 */
377int
378zvol_set_volsize(const char *name, uint64_t volsize)
379{
35d3e322 380 zvol_state_t *zv = NULL;
60101509 381 objset_t *os = NULL;
60101509 382 int error;
35d3e322
BB
383 dmu_object_info_t *doi;
384 uint64_t readonly;
385 boolean_t owned = B_FALSE;
60101509 386
13fe0198
MA
387 error = dsl_prop_get_integer(name,
388 zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL);
389 if (error != 0)
ce37ebd2 390 return (SET_ERROR(error));
13fe0198 391 if (readonly)
2e528b49 392 return (SET_ERROR(EROFS));
13fe0198 393
60101509 394 mutex_enter(&zvol_state_lock);
60101509 395 zv = zvol_find_by_name(name);
5559ba09
BP
396 if (zv != NULL)
397 mutex_enter(&zv->zv_state_lock);
398 mutex_exit(&zvol_state_lock);
35d3e322
BB
399
400 if (zv == NULL || zv->zv_objset == NULL) {
401 if ((error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE,
402 FTAG, &os)) != 0) {
5559ba09
BP
403 if (zv != NULL)
404 mutex_exit(&zv->zv_state_lock);
35d3e322
BB
405 return (SET_ERROR(error));
406 }
407 owned = B_TRUE;
408 if (zv != NULL)
409 zv->zv_objset = os;
410 } else {
040dab99 411 rw_enter(&zv->zv_suspend_lock, RW_READER);
35d3e322 412 os = zv->zv_objset;
60101509
BB
413 }
414
ce37ebd2 415 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
60101509 416
ce37ebd2
BB
417 if ((error = dmu_object_info(os, ZVOL_OBJ, doi)) ||
418 (error = zvol_check_volsize(volsize, doi->doi_data_block_size)))
35d3e322 419 goto out;
60101509 420
35d3e322 421 error = zvol_update_volsize(volsize, os);
60101509 422
35d3e322
BB
423 if (error == 0 && zv != NULL)
424 error = zvol_update_live_volsize(zv, volsize);
425out:
3f7d0418 426 kmem_free(doi, sizeof (dmu_object_info_t));
427
35d3e322
BB
428 if (owned) {
429 dmu_objset_disown(os, FTAG);
430 if (zv != NULL)
431 zv->zv_objset = NULL;
040dab99
CC
432 } else {
433 rw_exit(&zv->zv_suspend_lock);
35d3e322 434 }
5559ba09
BP
435
436 if (zv != NULL)
437 mutex_exit(&zv->zv_state_lock);
438 return (SET_ERROR(error));
60101509
BB
439}
440
441/*
442 * Sanity check volume block size.
443 */
444int
4cb7b9c5 445zvol_check_volblocksize(const char *name, uint64_t volblocksize)
60101509 446{
4cb7b9c5
BB
447 /* Record sizes above 128k need the feature to be enabled */
448 if (volblocksize > SPA_OLD_MAXBLOCKSIZE) {
449 spa_t *spa;
450 int error;
451
452 if ((error = spa_open(name, &spa, FTAG)) != 0)
453 return (error);
454
455 if (!spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
456 spa_close(spa, FTAG);
457 return (SET_ERROR(ENOTSUP));
458 }
459
460 /*
461 * We don't allow setting the property above 1MB,
462 * unless the tunable has been changed.
463 */
464 if (volblocksize > zfs_max_recordsize)
465 return (SET_ERROR(EDOM));
466
467 spa_close(spa, FTAG);
468 }
469
60101509
BB
470 if (volblocksize < SPA_MINBLOCKSIZE ||
471 volblocksize > SPA_MAXBLOCKSIZE ||
472 !ISP2(volblocksize))
2e528b49 473 return (SET_ERROR(EDOM));
60101509
BB
474
475 return (0);
476}
477
478/*
479 * Set ZFS_PROP_VOLBLOCKSIZE set entry point.
480 */
481int
482zvol_set_volblocksize(const char *name, uint64_t volblocksize)
483{
484 zvol_state_t *zv;
485 dmu_tx_t *tx;
486 int error;
487
488 mutex_enter(&zvol_state_lock);
489
490 zv = zvol_find_by_name(name);
491 if (zv == NULL) {
5559ba09
BP
492 mutex_exit(&zvol_state_lock);
493 return (SET_ERROR(ENXIO));
60101509 494 }
5559ba09
BP
495 mutex_enter(&zv->zv_state_lock);
496 mutex_exit(&zvol_state_lock);
60101509 497
ba6a2402 498 if (zv->zv_flags & ZVOL_RDONLY) {
5559ba09
BP
499 mutex_exit(&zv->zv_state_lock);
500 return (SET_ERROR(EROFS));
60101509
BB
501 }
502
040dab99
CC
503 rw_enter(&zv->zv_suspend_lock, RW_READER);
504
60101509
BB
505 tx = dmu_tx_create(zv->zv_objset);
506 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
507 error = dmu_tx_assign(tx, TXG_WAIT);
508 if (error) {
509 dmu_tx_abort(tx);
510 } else {
511 error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
512 volblocksize, 0, tx);
513 if (error == ENOTSUP)
2e528b49 514 error = SET_ERROR(EBUSY);
60101509
BB
515 dmu_tx_commit(tx);
516 if (error == 0)
517 zv->zv_volblocksize = volblocksize;
518 }
040dab99 519 rw_exit(&zv->zv_suspend_lock);
5559ba09
BP
520
521 mutex_exit(&zv->zv_state_lock);
60101509 522
ce37ebd2 523 return (SET_ERROR(error));
60101509
BB
524}
525
460a0213
DM
526/*
527 * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we
528 * implement DKIOCFREE/free-long-range.
529 */
530static int
531zvol_replay_truncate(zvol_state_t *zv, lr_truncate_t *lr, boolean_t byteswap)
532{
533 uint64_t offset, length;
534
535 if (byteswap)
536 byteswap_uint64_array(lr, sizeof (*lr));
537
538 offset = lr->lr_offset;
539 length = lr->lr_length;
540
541 return (dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, length));
542}
543
60101509
BB
544/*
545 * Replay a TX_WRITE ZIL transaction that didn't get committed
546 * after a system failure
547 */
548static int
549zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
550{
551 objset_t *os = zv->zv_objset;
552 char *data = (char *)(lr + 1); /* data follows lr_write_t */
553 uint64_t off = lr->lr_offset;
554 uint64_t len = lr->lr_length;
555 dmu_tx_t *tx;
556 int error;
557
558 if (byteswap)
559 byteswap_uint64_array(lr, sizeof (*lr));
560
561 tx = dmu_tx_create(os);
562 dmu_tx_hold_write(tx, ZVOL_OBJ, off, len);
563 error = dmu_tx_assign(tx, TXG_WAIT);
564 if (error) {
565 dmu_tx_abort(tx);
566 } else {
567 dmu_write(os, ZVOL_OBJ, off, len, data, tx);
568 dmu_tx_commit(tx);
569 }
570
ce37ebd2 571 return (SET_ERROR(error));
60101509
BB
572}
573
574static int
575zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
576{
2e528b49 577 return (SET_ERROR(ENOTSUP));
60101509
BB
578}
579
580/*
581 * Callback vectors for replaying records.
460a0213 582 * Only TX_WRITE and TX_TRUNCATE are needed for zvol.
60101509 583 */
b01615d5
RY
584zil_replay_func_t zvol_replay_vector[TX_MAX_TYPE] = {
585 (zil_replay_func_t)zvol_replay_err, /* no such transaction type */
586 (zil_replay_func_t)zvol_replay_err, /* TX_CREATE */
587 (zil_replay_func_t)zvol_replay_err, /* TX_MKDIR */
588 (zil_replay_func_t)zvol_replay_err, /* TX_MKXATTR */
589 (zil_replay_func_t)zvol_replay_err, /* TX_SYMLINK */
590 (zil_replay_func_t)zvol_replay_err, /* TX_REMOVE */
591 (zil_replay_func_t)zvol_replay_err, /* TX_RMDIR */
592 (zil_replay_func_t)zvol_replay_err, /* TX_LINK */
593 (zil_replay_func_t)zvol_replay_err, /* TX_RENAME */
594 (zil_replay_func_t)zvol_replay_write, /* TX_WRITE */
460a0213 595 (zil_replay_func_t)zvol_replay_truncate, /* TX_TRUNCATE */
b01615d5
RY
596 (zil_replay_func_t)zvol_replay_err, /* TX_SETATTR */
597 (zil_replay_func_t)zvol_replay_err, /* TX_ACL */
60101509
BB
598};
599
600/*
601 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
602 *
603 * We store data in the log buffers if it's small enough.
604 * Otherwise we will later flush the data out via dmu_sync().
605 */
606ssize_t zvol_immediate_write_sz = 32768;
607
608static void
ce37ebd2
BB
609zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, uint64_t offset,
610 uint64_t size, int sync)
60101509
BB
611{
612 uint32_t blocksize = zv->zv_volblocksize;
613 zilog_t *zilog = zv->zv_zilog;
1b7c1e5c 614 itx_wr_state_t write_state;
60101509
BB
615
616 if (zil_replaying(zilog, tx))
617 return;
618
1b7c1e5c
GDN
619 if (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
620 write_state = WR_INDIRECT;
621 else if (!spa_has_slogs(zilog->zl_spa) &&
622 size >= blocksize && blocksize > zvol_immediate_write_sz)
623 write_state = WR_INDIRECT;
624 else if (sync)
625 write_state = WR_COPIED;
626 else
627 write_state = WR_NEED_COPY;
60101509
BB
628
629 while (size) {
630 itx_t *itx;
631 lr_write_t *lr;
1b7c1e5c
GDN
632 itx_wr_state_t wr_state = write_state;
633 ssize_t len = size;
60101509 634
1b7c1e5c
GDN
635 if (wr_state == WR_COPIED && size > ZIL_MAX_COPIED_DATA)
636 wr_state = WR_NEED_COPY;
637 else if (wr_state == WR_INDIRECT)
638 len = MIN(blocksize - P2PHASE(offset, blocksize), size);
60101509
BB
639
640 itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
1b7c1e5c 641 (wr_state == WR_COPIED ? len : 0));
60101509 642 lr = (lr_write_t *)&itx->itx_lr;
5228cf01
RY
643 if (wr_state == WR_COPIED && dmu_read_by_dnode(zv->zv_dn,
644 offset, len, lr+1, DMU_READ_NO_PREFETCH) != 0) {
60101509
BB
645 zil_itx_destroy(itx);
646 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
647 lr = (lr_write_t *)&itx->itx_lr;
1b7c1e5c 648 wr_state = WR_NEED_COPY;
60101509
BB
649 }
650
1b7c1e5c 651 itx->itx_wr_state = wr_state;
60101509
BB
652 lr->lr_foid = ZVOL_OBJ;
653 lr->lr_offset = offset;
654 lr->lr_length = len;
655 lr->lr_blkoff = 0;
656 BP_ZERO(&lr->lr_blkptr);
657
658 itx->itx_private = zv;
659 itx->itx_sync = sync;
660
661 (void) zil_itx_assign(zilog, itx, tx);
662
663 offset += len;
664 size -= len;
665 }
666}
667
692e55b8
CC
668typedef struct zv_request {
669 zvol_state_t *zv;
670 struct bio *bio;
671 rl_t *rl;
672} zv_request_t;
673
674static void
675uio_from_bio(uio_t *uio, struct bio *bio)
60101509 676{
692e55b8
CC
677 uio->uio_bvec = &bio->bi_io_vec[BIO_BI_IDX(bio)];
678 uio->uio_skip = BIO_BI_SKIP(bio);
679 uio->uio_resid = BIO_BI_SIZE(bio);
680 uio->uio_iovcnt = bio->bi_vcnt - BIO_BI_IDX(bio);
681 uio->uio_loffset = BIO_BI_SECTOR(bio) << 9;
682 uio->uio_limit = MAXOFFSET_T;
683 uio->uio_segflg = UIO_BVEC;
684}
685
686static void
687zvol_write(void *arg)
688{
689 zv_request_t *zvr = arg;
690 struct bio *bio = zvr->bio;
691 uio_t uio;
692 zvol_state_t *zv = zvr->zv;
a765a34a 693 uint64_t volsize = zv->zv_volsize;
692e55b8 694 boolean_t sync;
a765a34a 695 int error = 0;
692e55b8
CC
696 unsigned long start_jif;
697
698 uio_from_bio(&uio, bio);
60101509 699
5428dc51
BP
700 ASSERT(zv && zv->zv_open_count > 0);
701
692e55b8
CC
702 start_jif = jiffies;
703 generic_start_io_acct(WRITE, bio_sectors(bio), &zv->zv_disk->part0);
b18019d2 704
692e55b8
CC
705 sync = bio_is_fua(bio) || zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS;
706
707 while (uio.uio_resid > 0 && uio.uio_loffset < volsize) {
708 uint64_t bytes = MIN(uio.uio_resid, DMU_MAX_ACCESS >> 1);
709 uint64_t off = uio.uio_loffset;
a765a34a 710 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
b18019d2 711
a765a34a
RY
712 if (bytes > volsize - off) /* don't write past the end */
713 bytes = volsize - off;
2727b9d3 714
a765a34a 715 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
60101509 716
a765a34a
RY
717 /* This will only fail for ENOSPC */
718 error = dmu_tx_assign(tx, TXG_WAIT);
719 if (error) {
720 dmu_tx_abort(tx);
721 break;
722 }
5228cf01 723 error = dmu_write_uio_dnode(zv->zv_dn, &uio, bytes, tx);
a765a34a
RY
724 if (error == 0)
725 zvol_log_write(zv, tx, off, bytes, sync);
726 dmu_tx_commit(tx);
60101509 727
a765a34a
RY
728 if (error)
729 break;
60101509 730 }
692e55b8 731 zfs_range_unlock(zvr->rl);
a765a34a 732 if (sync)
60101509 733 zil_commit(zv->zv_zilog, ZVOL_OBJ);
692e55b8
CC
734
735 rw_exit(&zv->zv_suspend_lock);
736 generic_end_io_acct(WRITE, &zv->zv_disk->part0, start_jif);
737 BIO_END_IO(bio, -error);
738 kmem_free(zvr, sizeof (zv_request_t));
60101509
BB
739}
740
460a0213
DM
741/*
742 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE.
743 */
744static void
745zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len,
746 boolean_t sync)
747{
748 itx_t *itx;
749 lr_truncate_t *lr;
750 zilog_t *zilog = zv->zv_zilog;
751
752 if (zil_replaying(zilog, tx))
753 return;
754
755 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
756 lr = (lr_truncate_t *)&itx->itx_lr;
757 lr->lr_foid = ZVOL_OBJ;
758 lr->lr_offset = off;
759 lr->lr_length = len;
760
761 itx->itx_sync = sync;
762 zil_itx_assign(zilog, itx, tx);
763}
764
692e55b8
CC
765static void
766zvol_discard(void *arg)
30930fba 767{
692e55b8
CC
768 zv_request_t *zvr = arg;
769 struct bio *bio = zvr->bio;
770 zvol_state_t *zv = zvr->zv;
37f9dac5
RY
771 uint64_t start = BIO_BI_SECTOR(bio) << 9;
772 uint64_t size = BIO_BI_SIZE(bio);
773 uint64_t end = start + size;
692e55b8 774 int error = 0;
460a0213 775 dmu_tx_t *tx;
692e55b8 776 unsigned long start_jif;
30930fba 777
5428dc51
BP
778 ASSERT(zv && zv->zv_open_count > 0);
779
692e55b8
CC
780 start_jif = jiffies;
781 generic_start_io_acct(WRITE, bio_sectors(bio), &zv->zv_disk->part0);
782
783 if (end > zv->zv_volsize) {
784 error = SET_ERROR(EIO);
785 goto out;
786 }
30930fba 787
089fa91b 788 /*
cf41432c
BB
789 * Align the request to volume block boundaries when a secure erase is
790 * not required. This will prevent dnode_free_range() from zeroing out
791 * the unaligned parts which is slow (read-modify-write) and useless
792 * since we are not freeing any space by doing so.
089fa91b 793 */
cf41432c 794 if (!bio_is_secure_erase(bio)) {
fa565676
RY
795 start = P2ROUNDUP(start, zv->zv_volblocksize);
796 end = P2ALIGN(end, zv->zv_volblocksize);
f52ebcb3 797 size = end - start;
fa565676 798 }
089fa91b 799
37f9dac5 800 if (start >= end)
692e55b8 801 goto out;
30930fba 802
460a0213
DM
803 tx = dmu_tx_create(zv->zv_objset);
804 dmu_tx_mark_netfree(tx);
805 error = dmu_tx_assign(tx, TXG_WAIT);
806 if (error != 0) {
807 dmu_tx_abort(tx);
808 } else {
809 zvol_log_truncate(zv, tx, start, size, B_TRUE);
810 dmu_tx_commit(tx);
811 error = dmu_free_long_range(zv->zv_objset,
812 ZVOL_OBJ, start, size);
813 }
30930fba 814
692e55b8
CC
815out:
816 zfs_range_unlock(zvr->rl);
817 rw_exit(&zv->zv_suspend_lock);
818 generic_end_io_acct(WRITE, &zv->zv_disk->part0, start_jif);
819 BIO_END_IO(bio, -error);
820 kmem_free(zvr, sizeof (zv_request_t));
30930fba 821}
30930fba 822
692e55b8
CC
823static void
824zvol_read(void *arg)
60101509 825{
692e55b8
CC
826 zv_request_t *zvr = arg;
827 struct bio *bio = zvr->bio;
828 uio_t uio;
829 zvol_state_t *zv = zvr->zv;
a765a34a 830 uint64_t volsize = zv->zv_volsize;
a765a34a 831 int error = 0;
692e55b8
CC
832 unsigned long start_jif;
833
834 uio_from_bio(&uio, bio);
b18019d2 835
5428dc51
BP
836 ASSERT(zv && zv->zv_open_count > 0);
837
692e55b8
CC
838 start_jif = jiffies;
839 generic_start_io_acct(READ, bio_sectors(bio), &zv->zv_disk->part0);
840
841 while (uio.uio_resid > 0 && uio.uio_loffset < volsize) {
842 uint64_t bytes = MIN(uio.uio_resid, DMU_MAX_ACCESS >> 1);
60101509 843
a765a34a 844 /* don't read past the end */
692e55b8
CC
845 if (bytes > volsize - uio.uio_loffset)
846 bytes = volsize - uio.uio_loffset;
60101509 847
5228cf01 848 error = dmu_read_uio_dnode(zv->zv_dn, &uio, bytes);
a765a34a
RY
849 if (error) {
850 /* convert checksum errors into IO errors */
851 if (error == ECKSUM)
852 error = SET_ERROR(EIO);
853 break;
854 }
855 }
692e55b8
CC
856 zfs_range_unlock(zvr->rl);
857
858 rw_exit(&zv->zv_suspend_lock);
859 generic_end_io_acct(READ, &zv->zv_disk->part0, start_jif);
860 BIO_END_IO(bio, -error);
861 kmem_free(zvr, sizeof (zv_request_t));
60101509
BB
862}
863
37f9dac5
RY
864static MAKE_REQUEST_FN_RET
865zvol_request(struct request_queue *q, struct bio *bio)
60101509
BB
866{
867 zvol_state_t *zv = q->queuedata;
37f9dac5 868 fstrans_cookie_t cookie = spl_fstrans_mark();
692e55b8
CC
869 uint64_t offset = BIO_BI_SECTOR(bio) << 9;
870 uint64_t size = BIO_BI_SIZE(bio);
8198d18c 871 int rw = bio_data_dir(bio);
692e55b8 872 zv_request_t *zvr;
a765a34a 873
692e55b8 874 if (bio_has_data(bio) && offset + size > zv->zv_volsize) {
37f9dac5 875 printk(KERN_INFO
a765a34a 876 "%s: bad access: offset=%llu, size=%lu\n",
37f9dac5 877 zv->zv_disk->disk_name,
692e55b8
CC
878 (long long unsigned)offset,
879 (long unsigned)size);
37f9dac5 880
692e55b8
CC
881 BIO_END_IO(bio, -SET_ERROR(EIO));
882 goto out;
883 }
8198d18c
RY
884
885 if (rw == WRITE) {
37f9dac5 886 if (unlikely(zv->zv_flags & ZVOL_RDONLY)) {
692e55b8
CC
887 BIO_END_IO(bio, -SET_ERROR(EROFS));
888 goto out;
60101509
BB
889 }
890
692e55b8
CC
891 /*
892 * To be released in the I/O function. See the comment on
893 * zfs_range_lock below.
894 */
895 rw_enter(&zv->zv_suspend_lock, RW_READER);
896
897 /* bio marked as FLUSH need to flush before write */
898 if (bio_is_flush(bio))
899 zil_commit(zv->zv_zilog, ZVOL_OBJ);
900
901 /* Some requests are just for flush and nothing else. */
902 if (size == 0) {
903 rw_exit(&zv->zv_suspend_lock);
904 BIO_END_IO(bio, 0);
905 goto out;
37f9dac5 906 }
60101509 907
692e55b8
CC
908 zvr = kmem_alloc(sizeof (zv_request_t), KM_SLEEP);
909 zvr->zv = zv;
910 zvr->bio = bio;
911
a765a34a 912 /*
692e55b8
CC
913 * To be released in the I/O function. Since the I/O functions
914 * are asynchronous, we take it here synchronously to make
915 * sure overlapped I/Os are properly ordered.
a765a34a 916 */
692e55b8
CC
917 zvr->rl = zfs_range_lock(&zv->zv_range_lock, offset, size,
918 RL_WRITER);
919 if (bio_is_discard(bio) || bio_is_secure_erase(bio)) {
920 if (zvol_request_sync || taskq_dispatch(zvol_taskq,
921 zvol_discard, zvr, TQ_SLEEP) == TASKQID_INVALID)
922 zvol_discard(zvr);
923 } else {
924 if (zvol_request_sync || taskq_dispatch(zvol_taskq,
925 zvol_write, zvr, TQ_SLEEP) == TASKQID_INVALID)
926 zvol_write(zvr);
a765a34a 927 }
692e55b8
CC
928 } else {
929 zvr = kmem_alloc(sizeof (zv_request_t), KM_SLEEP);
930 zvr->zv = zv;
931 zvr->bio = bio;
a765a34a 932
692e55b8 933 rw_enter(&zv->zv_suspend_lock, RW_READER);
30930fba 934
692e55b8
CC
935 zvr->rl = zfs_range_lock(&zv->zv_range_lock, offset, size,
936 RL_READER);
937 if (zvol_request_sync || taskq_dispatch(zvol_taskq,
938 zvol_read, zvr, TQ_SLEEP) == TASKQID_INVALID)
939 zvol_read(zvr);
940 }
941
942out:
37f9dac5
RY
943 spl_fstrans_unmark(cookie);
944#ifdef HAVE_MAKE_REQUEST_FN_RET_INT
945 return (0);
1a093716
CC
946#elif defined(HAVE_MAKE_REQUEST_FN_RET_QC)
947 return (BLK_QC_T_NONE);
37f9dac5 948#endif
60101509
BB
949}
950
951static void
952zvol_get_done(zgd_t *zgd, int error)
953{
954 if (zgd->zgd_db)
955 dmu_buf_rele(zgd->zgd_db, zgd);
956
957 zfs_range_unlock(zgd->zgd_rl);
958
959 if (error == 0 && zgd->zgd_bp)
960 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
961
962 kmem_free(zgd, sizeof (zgd_t));
963}
964
965/*
966 * Get data to generate a TX_WRITE intent log record.
967 */
968static int
969zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
970{
971 zvol_state_t *zv = arg;
60101509
BB
972 uint64_t offset = lr->lr_offset;
973 uint64_t size = lr->lr_length;
03c6040b 974 blkptr_t *bp = &lr->lr_blkptr;
60101509
BB
975 dmu_buf_t *db;
976 zgd_t *zgd;
977 int error;
978
979 ASSERT(zio != NULL);
980 ASSERT(size != 0);
981
79c76d5b 982 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
60101509 983 zgd->zgd_zilog = zv->zv_zilog;
d88895a0
CC
984 zgd->zgd_rl = zfs_range_lock(&zv->zv_range_lock, offset, size,
985 RL_READER);
60101509
BB
986
987 /*
988 * Write records come in two flavors: immediate and indirect.
989 * For small writes it's cheaper to store the data with the
990 * log record (immediate); for large writes it's cheaper to
991 * sync the data and get a pointer to it (indirect) so that
992 * we don't have to write the data twice.
993 */
994 if (buf != NULL) { /* immediate write */
5228cf01 995 error = dmu_read_by_dnode(zv->zv_dn, offset, size, buf,
60101509
BB
996 DMU_READ_NO_PREFETCH);
997 } else {
998 size = zv->zv_volblocksize;
999 offset = P2ALIGN_TYPED(offset, size, uint64_t);
5228cf01 1000 error = dmu_buf_hold_by_dnode(zv->zv_dn, offset, zgd, &db,
60101509
BB
1001 DMU_READ_NO_PREFETCH);
1002 if (error == 0) {
03c6040b
GW
1003 blkptr_t *obp = dmu_buf_get_blkptr(db);
1004 if (obp) {
1005 ASSERT(BP_IS_HOLE(bp));
1006 *bp = *obp;
1007 }
1008
60101509
BB
1009 zgd->zgd_db = db;
1010 zgd->zgd_bp = &lr->lr_blkptr;
1011
1012 ASSERT(db != NULL);
1013 ASSERT(db->db_offset == offset);
1014 ASSERT(db->db_size == size);
1015
1016 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1017 zvol_get_done, zgd);
1018
1019 if (error == 0)
1020 return (0);
1021 }
1022 }
1023
1024 zvol_get_done(zgd, error);
1025
ce37ebd2 1026 return (SET_ERROR(error));
60101509
BB
1027}
1028
1029/*
d45e010d 1030 * The zvol_state_t's are inserted into zvol_state_list and zvol_htable.
60101509
BB
1031 */
1032static void
d45e010d 1033zvol_insert(zvol_state_t *zv)
60101509 1034{
60101509 1035 ASSERT(MUTEX_HELD(&zvol_state_lock));
d45e010d
CC
1036 ASSERT3U(MINOR(zv->zv_dev) & ZVOL_MINOR_MASK, ==, 0);
1037 list_insert_head(&zvol_state_list, zv);
1038 hlist_add_head(&zv->zv_hlink, ZVOL_HT_HEAD(zv->zv_hash));
60101509
BB
1039}
1040
1041/*
1042 * Simply remove the zvol from to list of zvols.
1043 */
1044static void
d45e010d 1045zvol_remove(zvol_state_t *zv)
60101509
BB
1046{
1047 ASSERT(MUTEX_HELD(&zvol_state_lock));
d45e010d
CC
1048 list_remove(&zvol_state_list, zv);
1049 hlist_del(&zv->zv_hlink);
60101509
BB
1050}
1051
040dab99
CC
1052/*
1053 * Setup zv after we just own the zv->objset
1054 */
60101509 1055static int
040dab99 1056zvol_setup_zv(zvol_state_t *zv)
60101509 1057{
60101509
BB
1058 uint64_t volsize;
1059 int error;
1060 uint64_t ro;
040dab99 1061 objset_t *os = zv->zv_objset;
1ee159f4
BP
1062
1063 error = dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL);
1064 if (error)
040dab99 1065 return (SET_ERROR(error));
60101509
BB
1066
1067 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
1ee159f4 1068 if (error)
040dab99 1069 return (SET_ERROR(error));
60101509 1070
5228cf01 1071 error = dnode_hold(os, ZVOL_OBJ, FTAG, &zv->zv_dn);
1ee159f4 1072 if (error)
040dab99 1073 return (SET_ERROR(error));
60101509
BB
1074
1075 set_capacity(zv->zv_disk, volsize >> 9);
1076 zv->zv_volsize = volsize;
1077 zv->zv_zilog = zil_open(os, zvol_get_data);
1078
a4430fce
GW
1079 if (ro || dmu_objset_is_snapshot(os) ||
1080 !spa_writeable(dmu_objset_spa(os))) {
babf3f9b
MM
1081 set_disk_ro(zv->zv_disk, 1);
1082 zv->zv_flags |= ZVOL_RDONLY;
60101509 1083 } else {
babf3f9b
MM
1084 set_disk_ro(zv->zv_disk, 0);
1085 zv->zv_flags &= ~ZVOL_RDONLY;
60101509 1086 }
040dab99 1087 return (0);
60101509
BB
1088}
1089
040dab99
CC
1090/*
1091 * Shutdown every zv_objset related stuff except zv_objset itself.
1092 * The is the reverse of zvol_setup_zv.
1093 */
60101509 1094static void
040dab99 1095zvol_shutdown_zv(zvol_state_t *zv)
60101509
BB
1096{
1097 zil_close(zv->zv_zilog);
1098 zv->zv_zilog = NULL;
04434775 1099
5228cf01
RY
1100 dnode_rele(zv->zv_dn, FTAG);
1101 zv->zv_dn = NULL;
04434775
MA
1102
1103 /*
1104 * Evict cached data
1105 */
1106 if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) &&
1107 !(zv->zv_flags & ZVOL_RDONLY))
1108 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1109 (void) dmu_objset_evict_dbufs(zv->zv_objset);
040dab99
CC
1110}
1111
1112/*
1113 * return the proper tag for rollback and recv
1114 */
1115void *
1116zvol_tag(zvol_state_t *zv)
1117{
1118 ASSERT(RW_WRITE_HELD(&zv->zv_suspend_lock));
1119 return (zv->zv_open_count > 0 ? zv : NULL);
1120}
1121
1122/*
1123 * Suspend the zvol for recv and rollback.
1124 */
1125zvol_state_t *
1126zvol_suspend(const char *name)
1127{
1128 zvol_state_t *zv;
1129
1130 mutex_enter(&zvol_state_lock);
1131 zv = zvol_find_by_name(name);
5559ba09
BP
1132 if (zv == NULL) {
1133 mutex_exit(&zvol_state_lock);
1134 return (NULL);
1135 }
1136 mutex_enter(&zv->zv_state_lock);
1137 mutex_exit(&zvol_state_lock);
04434775 1138
040dab99
CC
1139 /* block all I/O, release in zvol_resume. */
1140 rw_enter(&zv->zv_suspend_lock, RW_WRITER);
1141
1142 atomic_inc(&zv->zv_suspend_ref);
1143
1144 if (zv->zv_open_count > 0)
1145 zvol_shutdown_zv(zv);
5559ba09
BP
1146
1147 mutex_exit(&zv->zv_state_lock);
040dab99
CC
1148 return (zv);
1149}
1150
1151int
1152zvol_resume(zvol_state_t *zv)
1153{
1154 int error = 0;
1155
1156 ASSERT(RW_WRITE_HELD(&zv->zv_suspend_lock));
2d82116e
BP
1157
1158 /*
1159 * Cannot take zv_state_lock here with zv_suspend_lock
1160 * held; however, the latter is held in exclusive mode,
1161 * so it is not necessary to do so
1162 */
040dab99
CC
1163 if (zv->zv_open_count > 0) {
1164 VERIFY0(dmu_objset_hold(zv->zv_name, zv, &zv->zv_objset));
1165 VERIFY3P(zv->zv_objset->os_dsl_dataset->ds_owner, ==, zv);
1166 VERIFY(dsl_dataset_long_held(zv->zv_objset->os_dsl_dataset));
1167 dmu_objset_rele(zv->zv_objset, zv);
1168
1169 error = zvol_setup_zv(zv);
1170 }
1171 rw_exit(&zv->zv_suspend_lock);
1172 /*
1173 * We need this because we don't hold zvol_state_lock while releasing
1174 * zv_suspend_lock. zvol_remove_minors_impl thus cannot check
1175 * zv_suspend_lock to determine it is safe to free because rwlock is
1176 * not inherent atomic.
1177 */
1178 atomic_dec(&zv->zv_suspend_ref);
1179
1180 return (SET_ERROR(error));
1181}
1182
1183static int
1184zvol_first_open(zvol_state_t *zv)
1185{
1186 objset_t *os;
07783588
BP
1187 int error, locked = 0;
1188
1189 /*
1190 * In all other cases the spa_namespace_lock is taken before the
1191 * bdev->bd_mutex lock. But in this case the Linux __blkdev_get()
1192 * function calls fops->open() with the bdev->bd_mutex lock held.
1193 * This deadlock can be easily observed with zvols used as vdevs.
1194 *
1195 * To avoid a potential lock inversion deadlock we preemptively
1196 * try to take the spa_namespace_lock(). Normally it will not
1197 * be contended and this is safe because spa_open_common() handles
1198 * the case where the caller already holds the spa_namespace_lock.
1199 *
1200 * When it is contended we risk a lock inversion if we were to
1201 * block waiting for the lock. Luckily, the __blkdev_get()
1202 * function allows us to return -ERESTARTSYS which will result in
1203 * bdev->bd_mutex being dropped, reacquired, and fops->open() being
1204 * called again. This process can be repeated safely until both
1205 * locks are acquired.
1206 */
1207 if (!mutex_owned(&spa_namespace_lock)) {
1208 locked = mutex_tryenter(&spa_namespace_lock);
1209 if (!locked)
1210 return (-SET_ERROR(ERESTARTSYS));
1211 }
040dab99
CC
1212
1213 /* lie and say we're read-only */
1214 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, 1, zv, &os);
1215 if (error)
07783588 1216 goto out_mutex;
040dab99
CC
1217
1218 zv->zv_objset = os;
1219
1220 error = zvol_setup_zv(zv);
1221
1222 if (error) {
1223 dmu_objset_disown(os, zv);
1224 zv->zv_objset = NULL;
1225 }
1226
07783588
BP
1227out_mutex:
1228 if (locked)
1229 mutex_exit(&spa_namespace_lock);
040dab99
CC
1230 return (SET_ERROR(-error));
1231}
1232
1233static void
1234zvol_last_close(zvol_state_t *zv)
1235{
1236 zvol_shutdown_zv(zv);
1237
1238 dmu_objset_disown(zv->zv_objset, zv);
60101509
BB
1239 zv->zv_objset = NULL;
1240}
1241
1242static int
1243zvol_open(struct block_device *bdev, fmode_t flag)
1244{
5428dc51 1245 zvol_state_t *zv;
5559ba09 1246 int error = 0, drop_suspend = 0;
60101509 1247
5559ba09 1248 ASSERT(!mutex_owned(&zvol_state_lock));
60101509 1249
5559ba09 1250 mutex_enter(&zvol_state_lock);
5428dc51
BP
1251 /*
1252 * Obtain a copy of private_data under the lock to make sure
5559ba09 1253 * that either the result of zvol free code path setting
5428dc51
BP
1254 * bdev->bd_disk->private_data to NULL is observed, or zvol_free()
1255 * is not called on this zv because of the positive zv_open_count.
1256 */
1257 zv = bdev->bd_disk->private_data;
1258 if (zv == NULL) {
5559ba09
BP
1259 mutex_exit(&zvol_state_lock);
1260 return (SET_ERROR(-ENXIO));
5428dc51 1261 }
5559ba09
BP
1262 mutex_enter(&zv->zv_state_lock);
1263 mutex_exit(&zvol_state_lock);
60101509
BB
1264
1265 if (zv->zv_open_count == 0) {
040dab99
CC
1266 /* make sure zvol is not suspended when first open */
1267 rw_enter(&zv->zv_suspend_lock, RW_READER);
1268 drop_suspend = 1;
1269
60101509
BB
1270 error = zvol_first_open(zv);
1271 if (error)
1272 goto out_mutex;
1273 }
1274
ba6a2402 1275 if ((flag & FMODE_WRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
60101509
BB
1276 error = -EROFS;
1277 goto out_open_count;
1278 }
1279
1280 zv->zv_open_count++;
1281
5428dc51
BP
1282 check_disk_change(bdev);
1283
60101509
BB
1284out_open_count:
1285 if (zv->zv_open_count == 0)
1286 zvol_last_close(zv);
60101509 1287out_mutex:
040dab99
CC
1288 if (drop_suspend)
1289 rw_exit(&zv->zv_suspend_lock);
5559ba09 1290 mutex_exit(&zv->zv_state_lock);
60101509 1291
ce37ebd2 1292 return (SET_ERROR(error));
60101509
BB
1293}
1294
a1d9543a
CD
1295#ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1296static void
1297#else
60101509 1298static int
a1d9543a 1299#endif
60101509
BB
1300zvol_release(struct gendisk *disk, fmode_t mode)
1301{
5559ba09 1302 zvol_state_t *zv;
60101509 1303
5559ba09 1304 ASSERT(!mutex_owned(&zvol_state_lock));
5428dc51 1305
5559ba09
BP
1306 mutex_enter(&zvol_state_lock);
1307 zv = disk->private_data;
1308 ASSERT(zv && zv->zv_open_count > 0);
1309 mutex_enter(&zv->zv_state_lock);
1310 mutex_exit(&zvol_state_lock);
60101509 1311
040dab99
CC
1312 /* make sure zvol is not suspended when last close */
1313 if (zv->zv_open_count == 1)
1314 rw_enter(&zv->zv_suspend_lock, RW_READER);
1315
5428dc51 1316 zv->zv_open_count--;
040dab99 1317 if (zv->zv_open_count == 0) {
5428dc51 1318 zvol_last_close(zv);
040dab99
CC
1319 rw_exit(&zv->zv_suspend_lock);
1320 }
60101509 1321
5559ba09 1322 mutex_exit(&zv->zv_state_lock);
60101509 1323
a1d9543a 1324#ifndef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
60101509 1325 return (0);
a1d9543a 1326#endif
60101509
BB
1327}
1328
1329static int
1330zvol_ioctl(struct block_device *bdev, fmode_t mode,
ce37ebd2 1331 unsigned int cmd, unsigned long arg)
60101509
BB
1332{
1333 zvol_state_t *zv = bdev->bd_disk->private_data;
1334 int error = 0;
1335
5428dc51 1336 ASSERT(zv && zv->zv_open_count > 0);
60101509
BB
1337
1338 switch (cmd) {
1339 case BLKFLSBUF:
ef1bdf36
BB
1340 fsync_bdev(bdev);
1341 invalidate_bdev(bdev);
1342 rw_enter(&zv->zv_suspend_lock, RW_READER);
1343
1344 if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) &&
1345 !(zv->zv_flags & ZVOL_RDONLY))
1346 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1347
1348 rw_exit(&zv->zv_suspend_lock);
60101509 1349 break;
ef1bdf36 1350
4c0d8e50 1351 case BLKZNAME:
5559ba09 1352 mutex_enter(&zv->zv_state_lock);
4c0d8e50 1353 error = copy_to_user((void *)arg, zv->zv_name, MAXNAMELEN);
5559ba09 1354 mutex_exit(&zv->zv_state_lock);
4c0d8e50 1355 break;
60101509
BB
1356
1357 default:
1358 error = -ENOTTY;
1359 break;
60101509
BB
1360 }
1361
ce37ebd2 1362 return (SET_ERROR(error));
60101509
BB
1363}
1364
1365#ifdef CONFIG_COMPAT
1366static int
1367zvol_compat_ioctl(struct block_device *bdev, fmode_t mode,
ce37ebd2 1368 unsigned cmd, unsigned long arg)
60101509 1369{
ce37ebd2 1370 return (zvol_ioctl(bdev, mode, cmd, arg));
60101509
BB
1371}
1372#else
ce37ebd2 1373#define zvol_compat_ioctl NULL
60101509
BB
1374#endif
1375
1376static int zvol_media_changed(struct gendisk *disk)
1377{
1378 zvol_state_t *zv = disk->private_data;
1379
5428dc51
BP
1380 ASSERT(zv && zv->zv_open_count > 0);
1381
ce37ebd2 1382 return (zv->zv_changed);
60101509
BB
1383}
1384
1385static int zvol_revalidate_disk(struct gendisk *disk)
1386{
1387 zvol_state_t *zv = disk->private_data;
1388
5428dc51
BP
1389 ASSERT(zv && zv->zv_open_count > 0);
1390
60101509
BB
1391 zv->zv_changed = 0;
1392 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1393
ce37ebd2 1394 return (0);
60101509
BB
1395}
1396
1397/*
1398 * Provide a simple virtual geometry for legacy compatibility. For devices
1399 * smaller than 1 MiB a small head and sector count is used to allow very
1400 * tiny devices. For devices over 1 Mib a standard head and sector count
1401 * is used to keep the cylinders count reasonable.
1402 */
1403static int
1404zvol_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1405{
1406 zvol_state_t *zv = bdev->bd_disk->private_data;
5428dc51
BP
1407 sector_t sectors;
1408
1409 ASSERT(zv && zv->zv_open_count > 0);
1410
1411 sectors = get_capacity(zv->zv_disk);
60101509
BB
1412
1413 if (sectors > 2048) {
1414 geo->heads = 16;
1415 geo->sectors = 63;
1416 } else {
1417 geo->heads = 2;
1418 geo->sectors = 4;
1419 }
1420
1421 geo->start = 0;
1422 geo->cylinders = sectors / (geo->heads * geo->sectors);
1423
ce37ebd2 1424 return (0);
60101509
BB
1425}
1426
1427static struct kobject *
1428zvol_probe(dev_t dev, int *part, void *arg)
1429{
1430 zvol_state_t *zv;
1431 struct kobject *kobj;
1432
1433 mutex_enter(&zvol_state_lock);
1434 zv = zvol_find_by_dev(dev);
23a61ccc 1435 kobj = zv ? get_disk(zv->zv_disk) : NULL;
60101509
BB
1436 mutex_exit(&zvol_state_lock);
1437
ce37ebd2 1438 return (kobj);
60101509
BB
1439}
1440
1441#ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS
1442static struct block_device_operations zvol_ops = {
ce37ebd2
BB
1443 .open = zvol_open,
1444 .release = zvol_release,
1445 .ioctl = zvol_ioctl,
1446 .compat_ioctl = zvol_compat_ioctl,
1447 .media_changed = zvol_media_changed,
1448 .revalidate_disk = zvol_revalidate_disk,
1449 .getgeo = zvol_getgeo,
1450 .owner = THIS_MODULE,
60101509
BB
1451};
1452
1453#else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1454
1455static int
1456zvol_open_by_inode(struct inode *inode, struct file *file)
1457{
ce37ebd2 1458 return (zvol_open(inode->i_bdev, file->f_mode));
60101509
BB
1459}
1460
1461static int
1462zvol_release_by_inode(struct inode *inode, struct file *file)
1463{
ce37ebd2 1464 return (zvol_release(inode->i_bdev->bd_disk, file->f_mode));
60101509
BB
1465}
1466
1467static int
1468zvol_ioctl_by_inode(struct inode *inode, struct file *file,
ce37ebd2 1469 unsigned int cmd, unsigned long arg)
60101509 1470{
b1c58213 1471 if (file == NULL || inode == NULL)
ce37ebd2
BB
1472 return (SET_ERROR(-EINVAL));
1473
1474 return (zvol_ioctl(inode->i_bdev, file->f_mode, cmd, arg));
60101509
BB
1475}
1476
ce37ebd2 1477#ifdef CONFIG_COMPAT
60101509
BB
1478static long
1479zvol_compat_ioctl_by_inode(struct file *file,
ce37ebd2 1480 unsigned int cmd, unsigned long arg)
60101509 1481{
b1c58213 1482 if (file == NULL)
ce37ebd2
BB
1483 return (SET_ERROR(-EINVAL));
1484
1485 return (zvol_compat_ioctl(file->f_dentry->d_inode->i_bdev,
1486 file->f_mode, cmd, arg));
60101509 1487}
ce37ebd2
BB
1488#else
1489#define zvol_compat_ioctl_by_inode NULL
1490#endif
60101509
BB
1491
1492static struct block_device_operations zvol_ops = {
ce37ebd2
BB
1493 .open = zvol_open_by_inode,
1494 .release = zvol_release_by_inode,
1495 .ioctl = zvol_ioctl_by_inode,
1496 .compat_ioctl = zvol_compat_ioctl_by_inode,
1497 .media_changed = zvol_media_changed,
1498 .revalidate_disk = zvol_revalidate_disk,
1499 .getgeo = zvol_getgeo,
1500 .owner = THIS_MODULE,
60101509
BB
1501};
1502#endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1503
1504/*
1505 * Allocate memory for a new zvol_state_t and setup the required
1506 * request queue and generic disk structures for the block device.
1507 */
1508static zvol_state_t *
1509zvol_alloc(dev_t dev, const char *name)
1510{
1511 zvol_state_t *zv;
1512
79c76d5b 1513 zv = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
60101509 1514
2a3871d4
RY
1515 list_link_init(&zv->zv_next);
1516
5559ba09
BP
1517 mutex_init(&zv->zv_state_lock, NULL, MUTEX_DEFAULT, NULL);
1518
37f9dac5 1519 zv->zv_queue = blk_alloc_queue(GFP_ATOMIC);
60101509
BB
1520 if (zv->zv_queue == NULL)
1521 goto out_kmem;
1522
37f9dac5 1523 blk_queue_make_request(zv->zv_queue, zvol_request);
cf41432c 1524 blk_queue_set_write_cache(zv->zv_queue, B_TRUE, B_TRUE);
b18019d2 1525
bc17f104
RY
1526 /* Limit read-ahead to a single page to prevent over-prefetching. */
1527 blk_queue_set_read_ahead(zv->zv_queue, 1);
1528
5731140e
R
1529 /* Disable write merging in favor of the ZIO pipeline. */
1530 queue_flag_set(QUEUE_FLAG_NOMERGES, zv->zv_queue);
1531
60101509
BB
1532 zv->zv_disk = alloc_disk(ZVOL_MINORS);
1533 if (zv->zv_disk == NULL)
1534 goto out_queue;
1535
1536 zv->zv_queue->queuedata = zv;
1537 zv->zv_dev = dev;
1538 zv->zv_open_count = 0;
4c0d8e50 1539 strlcpy(zv->zv_name, name, MAXNAMELEN);
60101509 1540
d88895a0 1541 zfs_rlock_init(&zv->zv_range_lock);
040dab99 1542 rw_init(&zv->zv_suspend_lock, NULL, RW_DEFAULT, NULL);
3c4988c8 1543
60101509
BB
1544 zv->zv_disk->major = zvol_major;
1545 zv->zv_disk->first_minor = (dev & MINORMASK);
1546 zv->zv_disk->fops = &zvol_ops;
1547 zv->zv_disk->private_data = zv;
1548 zv->zv_disk->queue = zv->zv_queue;
4c0d8e50
FN
1549 snprintf(zv->zv_disk->disk_name, DISK_NAME_LEN, "%s%d",
1550 ZVOL_DEV_NAME, (dev & MINORMASK));
60101509 1551
ce37ebd2 1552 return (zv);
60101509
BB
1553
1554out_queue:
1555 blk_cleanup_queue(zv->zv_queue);
1556out_kmem:
1557 kmem_free(zv, sizeof (zvol_state_t));
0a6bef26 1558
ce37ebd2 1559 return (NULL);
60101509
BB
1560}
1561
1562/*
5559ba09
BP
1563 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1564 * At this time, the structure is not opened by anyone, is taken off
1565 * the zvol_state_list, and has its private data set to NULL.
1566 * The zvol_state_lock is dropped.
60101509
BB
1567 */
1568static void
5559ba09 1569zvol_free(void *arg)
60101509 1570{
899662e3 1571 zvol_state_t *zv = arg;
5559ba09
BP
1572
1573 ASSERT(!MUTEX_HELD(&zvol_state_lock));
5428dc51 1574 ASSERT(zv->zv_open_count == 0);
5559ba09 1575 ASSERT(zv->zv_disk->private_data == NULL);
5428dc51 1576
040dab99 1577 rw_destroy(&zv->zv_suspend_lock);
d88895a0 1578 zfs_rlock_destroy(&zv->zv_range_lock);
60101509
BB
1579
1580 del_gendisk(zv->zv_disk);
1581 blk_cleanup_queue(zv->zv_queue);
1582 put_disk(zv->zv_disk);
1583
d45e010d 1584 ida_simple_remove(&zvol_ida, MINOR(zv->zv_dev) >> ZVOL_MINOR_BITS);
60101509 1585
5559ba09
BP
1586 mutex_destroy(&zv->zv_state_lock);
1587
1588 kmem_free(zv, sizeof (zvol_state_t));
899662e3
CC
1589}
1590
a0bd735a
BP
1591/*
1592 * Create a block device minor node and setup the linkage between it
1593 * and the specified volume. Once this function returns the block
1594 * device is live and ready for use.
1595 */
60101509 1596static int
a0bd735a 1597zvol_create_minor_impl(const char *name)
60101509
BB
1598{
1599 zvol_state_t *zv;
1600 objset_t *os;
1601 dmu_object_info_t *doi;
1602 uint64_t volsize;
9965059a 1603 uint64_t len;
60101509
BB
1604 unsigned minor = 0;
1605 int error = 0;
d45e010d
CC
1606 int idx;
1607 uint64_t hash = zvol_name_hash(name);
1608
1609 idx = ida_simple_get(&zvol_ida, 0, 0, kmem_flags_convert(KM_SLEEP));
1610 if (idx < 0)
1611 return (SET_ERROR(-idx));
1612 minor = idx << ZVOL_MINOR_BITS;
60101509 1613
a0bd735a 1614 mutex_enter(&zvol_state_lock);
60101509 1615
d45e010d 1616 zv = zvol_find_by_name_hash(name, hash);
60101509 1617 if (zv) {
5559ba09 1618 mutex_exit(&zvol_state_lock);
2d82116e 1619 ida_simple_remove(&zvol_ida, idx);
5559ba09 1620 return (SET_ERROR(EEXIST));
60101509
BB
1621 }
1622
5559ba09
BP
1623 mutex_exit(&zvol_state_lock);
1624
79c76d5b 1625 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
60101509 1626
040dab99 1627 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, FTAG, &os);
60101509
BB
1628 if (error)
1629 goto out_doi;
1630
1631 error = dmu_object_info(os, ZVOL_OBJ, doi);
1632 if (error)
1633 goto out_dmu_objset_disown;
1634
1635 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
1636 if (error)
1637 goto out_dmu_objset_disown;
1638
60101509
BB
1639 zv = zvol_alloc(MKDEV(zvol_major, minor), name);
1640 if (zv == NULL) {
2e528b49 1641 error = SET_ERROR(EAGAIN);
60101509
BB
1642 goto out_dmu_objset_disown;
1643 }
d45e010d 1644 zv->zv_hash = hash;
60101509
BB
1645
1646 if (dmu_objset_is_snapshot(os))
1647 zv->zv_flags |= ZVOL_RDONLY;
1648
1649 zv->zv_volblocksize = doi->doi_data_block_size;
1650 zv->zv_volsize = volsize;
1651 zv->zv_objset = os;
1652
1653 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1654
c495fe2c 1655 blk_queue_max_hw_sectors(zv->zv_queue, (DMU_MAX_ACCESS / 4) >> 9);
34037afe
ED
1656 blk_queue_max_segments(zv->zv_queue, UINT16_MAX);
1657 blk_queue_max_segment_size(zv->zv_queue, UINT_MAX);
1658 blk_queue_physical_block_size(zv->zv_queue, zv->zv_volblocksize);
1659 blk_queue_io_opt(zv->zv_queue, zv->zv_volblocksize);
7c0e5708
ED
1660 blk_queue_max_discard_sectors(zv->zv_queue,
1661 (zvol_max_discard_blocks * zv->zv_volblocksize) >> 9);
ee5fd0bb 1662 blk_queue_discard_granularity(zv->zv_queue, zv->zv_volblocksize);
30930fba 1663 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zv->zv_queue);
37f9dac5 1664#ifdef QUEUE_FLAG_NONROT
34037afe
ED
1665 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zv->zv_queue);
1666#endif
c6a3a222
RY
1667#ifdef QUEUE_FLAG_ADD_RANDOM
1668 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, zv->zv_queue);
1669#endif
34037afe 1670
a4430fce
GW
1671 if (spa_writeable(dmu_objset_spa(os))) {
1672 if (zil_replay_disable)
1673 zil_destroy(dmu_objset_zil(os), B_FALSE);
1674 else
1675 zil_replay(os, zv, zvol_replay_vector);
1676 }
60101509 1677
9965059a
BB
1678 /*
1679 * When udev detects the addition of the device it will immediately
1680 * invoke blkid(8) to determine the type of content on the device.
1681 * Prefetching the blocks commonly scanned by blkid(8) will speed
1682 * up this process.
1683 */
1684 len = MIN(MAX(zvol_prefetch_bytes, 0), SPA_MAXBLOCKSIZE);
1685 if (len > 0) {
fcff0f35
PD
1686 dmu_prefetch(os, ZVOL_OBJ, 0, 0, len, ZIO_PRIORITY_SYNC_READ);
1687 dmu_prefetch(os, ZVOL_OBJ, 0, volsize - len, len,
02730c33 1688 ZIO_PRIORITY_SYNC_READ);
9965059a
BB
1689 }
1690
f74a147c 1691 zv->zv_objset = NULL;
60101509 1692out_dmu_objset_disown:
040dab99 1693 dmu_objset_disown(os, FTAG);
60101509 1694out_doi:
ce37ebd2 1695 kmem_free(doi, sizeof (dmu_object_info_t));
60101509
BB
1696
1697 if (error == 0) {
5559ba09 1698 mutex_enter(&zvol_state_lock);
60101509 1699 zvol_insert(zv);
5428dc51 1700 mutex_exit(&zvol_state_lock);
60101509 1701 add_disk(zv->zv_disk);
a0bd735a 1702 } else {
d45e010d 1703 ida_simple_remove(&zvol_ida, idx);
60101509
BB
1704 }
1705
ce37ebd2 1706 return (SET_ERROR(error));
60101509
BB
1707}
1708
ba6a2402
BB
1709/*
1710 * Rename a block device minor mode for the specified volume.
1711 */
1712static void
a0bd735a 1713zvol_rename_minor(zvol_state_t *zv, const char *newname)
ba6a2402
BB
1714{
1715 int readonly = get_disk_ro(zv->zv_disk);
1716
1717 ASSERT(MUTEX_HELD(&zvol_state_lock));
1718
040dab99 1719 rw_enter(&zv->zv_suspend_lock, RW_READER);
ba6a2402 1720 strlcpy(zv->zv_name, newname, sizeof (zv->zv_name));
040dab99
CC
1721 rw_exit(&zv->zv_suspend_lock);
1722
1723 /* move to new hashtable entry */
1724 zv->zv_hash = zvol_name_hash(zv->zv_name);
1725 hlist_del(&zv->zv_hlink);
1726 hlist_add_head(&zv->zv_hlink, ZVOL_HT_HEAD(zv->zv_hash));
ba6a2402
BB
1727
1728 /*
1729 * The block device's read-only state is briefly changed causing
1730 * a KOBJ_CHANGE uevent to be issued. This ensures udev detects
1731 * the name change and fixes the symlinks. This does not change
1732 * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
1733 * changes. This would normally be done using kobject_uevent() but
1734 * that is a GPL-only symbol which is why we need this workaround.
1735 */
1736 set_disk_ro(zv->zv_disk, !readonly);
1737 set_disk_ro(zv->zv_disk, readonly);
1738}
1739
7ac557ce
CC
1740typedef struct minors_job {
1741 list_t *list;
1742 list_node_t link;
1743 /* input */
1744 char *name;
1745 /* output */
1746 int error;
1747} minors_job_t;
1748
1749/*
1750 * Prefetch zvol dnodes for the minors_job
1751 */
1752static void
1753zvol_prefetch_minors_impl(void *arg)
1754{
1755 minors_job_t *job = arg;
1756 char *dsname = job->name;
1757 objset_t *os = NULL;
1758
040dab99 1759 job->error = dmu_objset_own(dsname, DMU_OST_ZVOL, B_TRUE, FTAG,
7ac557ce
CC
1760 &os);
1761 if (job->error == 0) {
1762 dmu_prefetch(os, ZVOL_OBJ, 0, 0, 0, ZIO_PRIORITY_SYNC_READ);
040dab99 1763 dmu_objset_disown(os, FTAG);
7ac557ce
CC
1764 }
1765}
a0bd735a
BP
1766
1767/*
1768 * Mask errors to continue dmu_objset_find() traversal
1769 */
1770static int
1771zvol_create_snap_minor_cb(const char *dsname, void *arg)
1772{
7ac557ce
CC
1773 minors_job_t *j = arg;
1774 list_t *minors_list = j->list;
1775 const char *name = j->name;
a0bd735a 1776
1ee159f4
BP
1777 ASSERT0(MUTEX_HELD(&spa_namespace_lock));
1778
a0bd735a
BP
1779 /* skip the designated dataset */
1780 if (name && strcmp(dsname, name) == 0)
1781 return (0);
1782
1783 /* at this point, the dsname should name a snapshot */
1784 if (strchr(dsname, '@') == 0) {
1785 dprintf("zvol_create_snap_minor_cb(): "
02730c33 1786 "%s is not a shapshot name\n", dsname);
a0bd735a 1787 } else {
7ac557ce
CC
1788 minors_job_t *job;
1789 char *n = strdup(dsname);
1790 if (n == NULL)
1791 return (0);
1792
1793 job = kmem_alloc(sizeof (minors_job_t), KM_SLEEP);
1794 job->name = n;
1795 job->list = minors_list;
1796 job->error = 0;
1797 list_insert_tail(minors_list, job);
1798 /* don't care if dispatch fails, because job->error is 0 */
1799 taskq_dispatch(system_taskq, zvol_prefetch_minors_impl, job,
1800 TQ_SLEEP);
a0bd735a
BP
1801 }
1802
1803 return (0);
1804}
1805
1806/*
1807 * Mask errors to continue dmu_objset_find() traversal
1808 */
60101509 1809static int
13fe0198 1810zvol_create_minors_cb(const char *dsname, void *arg)
60101509 1811{
a0bd735a
BP
1812 uint64_t snapdev;
1813 int error;
7ac557ce 1814 list_t *minors_list = arg;
a0bd735a 1815
1ee159f4
BP
1816 ASSERT0(MUTEX_HELD(&spa_namespace_lock));
1817
a0bd735a
BP
1818 error = dsl_prop_get_integer(dsname, "snapdev", &snapdev, NULL);
1819 if (error)
1820 return (0);
1821
1822 /*
1823 * Given the name and the 'snapdev' property, create device minor nodes
1824 * with the linkages to zvols/snapshots as needed.
1825 * If the name represents a zvol, create a minor node for the zvol, then
1826 * check if its snapshots are 'visible', and if so, iterate over the
1827 * snapshots and create device minor nodes for those.
1828 */
1829 if (strchr(dsname, '@') == 0) {
7ac557ce
CC
1830 minors_job_t *job;
1831 char *n = strdup(dsname);
1832 if (n == NULL)
1833 return (0);
1834
1835 job = kmem_alloc(sizeof (minors_job_t), KM_SLEEP);
1836 job->name = n;
1837 job->list = minors_list;
1838 job->error = 0;
1839 list_insert_tail(minors_list, job);
1840 /* don't care if dispatch fails, because job->error is 0 */
1841 taskq_dispatch(system_taskq, zvol_prefetch_minors_impl, job,
1842 TQ_SLEEP);
1843
1844 if (snapdev == ZFS_SNAPDEV_VISIBLE) {
a0bd735a
BP
1845 /*
1846 * traverse snapshots only, do not traverse children,
1847 * and skip the 'dsname'
1848 */
1849 error = dmu_objset_find((char *)dsname,
7ac557ce 1850 zvol_create_snap_minor_cb, (void *)job,
a0bd735a 1851 DS_FIND_SNAPSHOTS);
a0bd735a
BP
1852 }
1853 } else {
1854 dprintf("zvol_create_minors_cb(): %s is not a zvol name\n",
02730c33 1855 dsname);
a0bd735a 1856 }
60101509 1857
d5674448 1858 return (0);
60101509
BB
1859}
1860
1861/*
a0bd735a
BP
1862 * Create minors for the specified dataset, including children and snapshots.
1863 * Pay attention to the 'snapdev' property and iterate over the snapshots
1864 * only if they are 'visible'. This approach allows one to assure that the
1865 * snapshot metadata is read from disk only if it is needed.
1866 *
1867 * The name can represent a dataset to be recursively scanned for zvols and
1868 * their snapshots, or a single zvol snapshot. If the name represents a
1869 * dataset, the scan is performed in two nested stages:
1870 * - scan the dataset for zvols, and
1871 * - for each zvol, create a minor node, then check if the zvol's snapshots
1872 * are 'visible', and only then iterate over the snapshots if needed
1873 *
4e33ba4c 1874 * If the name represents a snapshot, a check is performed if the snapshot is
a0bd735a
BP
1875 * 'visible' (which also verifies that the parent is a zvol), and if so,
1876 * a minor node for that snapshot is created.
60101509 1877 */
a0bd735a
BP
1878static int
1879zvol_create_minors_impl(const char *name)
60101509 1880{
60101509 1881 int error = 0;
5428dc51 1882 fstrans_cookie_t cookie;
a0bd735a 1883 char *atp, *parent;
7ac557ce
CC
1884 list_t minors_list;
1885 minors_job_t *job;
60101509 1886
5428dc51
BP
1887 if (zvol_inhibit_dev)
1888 return (0);
1889
7ac557ce
CC
1890 /*
1891 * This is the list for prefetch jobs. Whenever we found a match
1892 * during dmu_objset_find, we insert a minors_job to the list and do
1893 * taskq_dispatch to parallel prefetch zvol dnodes. Note we don't need
1894 * any lock because all list operation is done on the current thread.
1895 *
1896 * We will use this list to do zvol_create_minor_impl after prefetch
1897 * so we don't have to traverse using dmu_objset_find again.
1898 */
1899 list_create(&minors_list, sizeof (minors_job_t),
1900 offsetof(minors_job_t, link));
1901
a0bd735a
BP
1902 parent = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1903 (void) strlcpy(parent, name, MAXPATHLEN);
1904
1905 if ((atp = strrchr(parent, '@')) != NULL) {
1906 uint64_t snapdev;
1907
1908 *atp = '\0';
1909 error = dsl_prop_get_integer(parent, "snapdev",
1910 &snapdev, NULL);
1911
1912 if (error == 0 && snapdev == ZFS_SNAPDEV_VISIBLE)
1913 error = zvol_create_minor_impl(name);
1914 } else {
1915 cookie = spl_fstrans_mark();
1916 error = dmu_objset_find(parent, zvol_create_minors_cb,
7ac557ce 1917 &minors_list, DS_FIND_CHILDREN);
a0bd735a
BP
1918 spl_fstrans_unmark(cookie);
1919 }
1920
1921 kmem_free(parent, MAXPATHLEN);
7ac557ce
CC
1922 taskq_wait_outstanding(system_taskq, 0);
1923
1924 /*
1925 * Prefetch is completed, we can do zvol_create_minor_impl
1926 * sequentially.
1927 */
1928 while ((job = list_head(&minors_list)) != NULL) {
1929 list_remove(&minors_list, job);
1930 if (!job->error)
1931 zvol_create_minor_impl(job->name);
1932 strfree(job->name);
1933 kmem_free(job, sizeof (minors_job_t));
1934 }
1935
1936 list_destroy(&minors_list);
ba6a2402
BB
1937
1938 return (SET_ERROR(error));
1939}
1940
1941/*
1942 * Remove minors for specified dataset including children and snapshots.
1943 */
a0bd735a
BP
1944static void
1945zvol_remove_minors_impl(const char *name)
ba6a2402
BB
1946{
1947 zvol_state_t *zv, *zv_next;
1948 int namelen = ((name) ? strlen(name) : 0);
899662e3 1949 taskqid_t t, tid = TASKQID_INVALID;
5559ba09 1950 list_t free_list;
ba6a2402 1951
74497b7a 1952 if (zvol_inhibit_dev)
ba6a2402 1953 return;
74497b7a 1954
5559ba09
BP
1955 list_create(&free_list, sizeof (zvol_state_t),
1956 offsetof(zvol_state_t, zv_next));
1957
60101509 1958 mutex_enter(&zvol_state_lock);
ba6a2402
BB
1959
1960 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1961 zv_next = list_next(&zvol_state_list, zv);
1962
1963 if (name == NULL || strcmp(zv->zv_name, name) == 0 ||
1964 (strncmp(zv->zv_name, name, namelen) == 0 &&
5428dc51
BP
1965 (zv->zv_name[namelen] == '/' ||
1966 zv->zv_name[namelen] == '@'))) {
1967
1968 /* If in use, leave alone */
040dab99
CC
1969 if (zv->zv_open_count > 0 ||
1970 atomic_read(&zv->zv_suspend_ref))
5428dc51 1971 continue;
5559ba09
BP
1972 /*
1973 * By taking zv_state_lock here, we guarantee that no
1974 * one is currently using this zv
1975 */
1976 mutex_enter(&zv->zv_state_lock);
ba6a2402 1977 zvol_remove(zv);
5559ba09 1978 mutex_exit(&zv->zv_state_lock);
899662e3
CC
1979
1980 /* clear this so zvol_open won't open it */
1981 zv->zv_disk->private_data = NULL;
1982
1983 /* try parallel zv_free, if failed do it in place */
5559ba09 1984 t = taskq_dispatch(system_taskq, zvol_free, zv,
899662e3
CC
1985 TQ_SLEEP);
1986 if (t == TASKQID_INVALID)
5559ba09 1987 list_insert_head(&free_list, zv);
899662e3
CC
1988 else
1989 tid = t;
60101509 1990 }
60101509 1991 }
ba6a2402 1992 mutex_exit(&zvol_state_lock);
5559ba09
BP
1993
1994 /*
1995 * Drop zvol_state_lock before calling zvol_free()
1996 */
1997 while ((zv = list_head(&free_list)) != NULL) {
1998 list_remove(&free_list, zv);
1999 zvol_free(zv);
2000 }
2001
899662e3
CC
2002 if (tid != TASKQID_INVALID)
2003 taskq_wait_outstanding(system_taskq, tid);
60101509
BB
2004}
2005
a0bd735a
BP
2006/* Remove minor for this specific snapshot only */
2007static void
2008zvol_remove_minor_impl(const char *name)
2009{
92aceb2a 2010 zvol_state_t *zv = NULL, *zv_next;
a0bd735a
BP
2011
2012 if (zvol_inhibit_dev)
2013 return;
2014
2015 if (strchr(name, '@') == NULL)
2016 return;
2017
2018 mutex_enter(&zvol_state_lock);
2019
2020 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
2021 zv_next = list_next(&zvol_state_list, zv);
2022
2023 if (strcmp(zv->zv_name, name) == 0) {
2024 /* If in use, leave alone */
040dab99
CC
2025 if (zv->zv_open_count > 0 ||
2026 atomic_read(&zv->zv_suspend_ref))
a0bd735a 2027 continue;
5559ba09
BP
2028 /*
2029 * By taking zv_state_lock here, we guarantee that no
2030 * one is currently using this zv
2031 */
2032 mutex_enter(&zv->zv_state_lock);
a0bd735a 2033 zvol_remove(zv);
5559ba09
BP
2034 mutex_exit(&zv->zv_state_lock);
2035 /* clear this so zvol_open won't open it */
2036 zv->zv_disk->private_data = NULL;
a0bd735a
BP
2037 break;
2038 }
2039 }
2040
92aceb2a 2041 /* Drop zvol_state_lock before calling zvol_free() */
a0bd735a 2042 mutex_exit(&zvol_state_lock);
5559ba09 2043
92aceb2a 2044 if (zv != NULL)
2045 zvol_free(zv);
a0bd735a
BP
2046}
2047
60101509 2048/*
ba6a2402 2049 * Rename minors for specified dataset including children and snapshots.
60101509 2050 */
a0bd735a
BP
2051static void
2052zvol_rename_minors_impl(const char *oldname, const char *newname)
60101509
BB
2053{
2054 zvol_state_t *zv, *zv_next;
ba6a2402
BB
2055 int oldnamelen, newnamelen;
2056 char *name;
60101509 2057
74497b7a
DH
2058 if (zvol_inhibit_dev)
2059 return;
2060
ba6a2402
BB
2061 oldnamelen = strlen(oldname);
2062 newnamelen = strlen(newname);
79c76d5b 2063 name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
60101509
BB
2064
2065 mutex_enter(&zvol_state_lock);
ba6a2402 2066
60101509
BB
2067 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
2068 zv_next = list_next(&zvol_state_list, zv);
2069
5428dc51
BP
2070 /* If in use, leave alone */
2071 if (zv->zv_open_count > 0)
2072 continue;
2073
ba6a2402 2074 if (strcmp(zv->zv_name, oldname) == 0) {
a0bd735a 2075 zvol_rename_minor(zv, newname);
ba6a2402
BB
2076 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 &&
2077 (zv->zv_name[oldnamelen] == '/' ||
2078 zv->zv_name[oldnamelen] == '@')) {
2079 snprintf(name, MAXNAMELEN, "%s%c%s", newname,
2080 zv->zv_name[oldnamelen],
2081 zv->zv_name + oldnamelen + 1);
a0bd735a 2082 zvol_rename_minor(zv, name);
60101509
BB
2083 }
2084 }
ba6a2402 2085
60101509 2086 mutex_exit(&zvol_state_lock);
ba6a2402
BB
2087
2088 kmem_free(name, MAXNAMELEN);
60101509
BB
2089}
2090
a0bd735a
BP
2091typedef struct zvol_snapdev_cb_arg {
2092 uint64_t snapdev;
2093} zvol_snapdev_cb_arg_t;
2094
0b4d1b58 2095static int
4ea3f864
GM
2096zvol_set_snapdev_cb(const char *dsname, void *param)
2097{
a0bd735a 2098 zvol_snapdev_cb_arg_t *arg = param;
0b4d1b58
ED
2099
2100 if (strchr(dsname, '@') == NULL)
ba6a2402 2101 return (0);
0b4d1b58 2102
a0bd735a 2103 switch (arg->snapdev) {
0b4d1b58 2104 case ZFS_SNAPDEV_VISIBLE:
a0bd735a 2105 (void) zvol_create_minor_impl(dsname);
0b4d1b58
ED
2106 break;
2107 case ZFS_SNAPDEV_HIDDEN:
a0bd735a 2108 (void) zvol_remove_minor_impl(dsname);
0b4d1b58
ED
2109 break;
2110 }
ba6a2402
BB
2111
2112 return (0);
0b4d1b58
ED
2113}
2114
a0bd735a
BP
2115static void
2116zvol_set_snapdev_impl(char *name, uint64_t snapdev)
2117{
2118 zvol_snapdev_cb_arg_t arg = {snapdev};
2119 fstrans_cookie_t cookie = spl_fstrans_mark();
2120 /*
2121 * The zvol_set_snapdev_sync() sets snapdev appropriately
2122 * in the dataset hierarchy. Here, we only scan snapshots.
2123 */
2124 dmu_objset_find(name, zvol_set_snapdev_cb, &arg, DS_FIND_SNAPSHOTS);
2125 spl_fstrans_unmark(cookie);
2126}
2127
2128static zvol_task_t *
2129zvol_task_alloc(zvol_async_op_t op, const char *name1, const char *name2,
2130 uint64_t snapdev)
2131{
2132 zvol_task_t *task;
2133 char *delim;
2134
2135 /* Never allow tasks on hidden names. */
2136 if (name1[0] == '$')
2137 return (NULL);
2138
2139 task = kmem_zalloc(sizeof (zvol_task_t), KM_SLEEP);
2140 task->op = op;
2141 task->snapdev = snapdev;
2142 delim = strchr(name1, '/');
2143 strlcpy(task->pool, name1, delim ? (delim - name1 + 1) : MAXNAMELEN);
2144
2145 strlcpy(task->name1, name1, MAXNAMELEN);
2146 if (name2 != NULL)
2147 strlcpy(task->name2, name2, MAXNAMELEN);
2148
2149 return (task);
2150}
2151
2152static void
2153zvol_task_free(zvol_task_t *task)
2154{
2155 kmem_free(task, sizeof (zvol_task_t));
2156}
2157
2158/*
2159 * The worker thread function performed asynchronously.
2160 */
2161static void
2162zvol_task_cb(void *param)
2163{
2164 zvol_task_t *task = (zvol_task_t *)param;
2165
2166 switch (task->op) {
2167 case ZVOL_ASYNC_CREATE_MINORS:
2168 (void) zvol_create_minors_impl(task->name1);
2169 break;
2170 case ZVOL_ASYNC_REMOVE_MINORS:
2171 zvol_remove_minors_impl(task->name1);
2172 break;
2173 case ZVOL_ASYNC_RENAME_MINORS:
2174 zvol_rename_minors_impl(task->name1, task->name2);
2175 break;
2176 case ZVOL_ASYNC_SET_SNAPDEV:
2177 zvol_set_snapdev_impl(task->name1, task->snapdev);
2178 break;
2179 default:
2180 VERIFY(0);
2181 break;
2182 }
2183
2184 zvol_task_free(task);
2185}
2186
2187typedef struct zvol_set_snapdev_arg {
2188 const char *zsda_name;
2189 uint64_t zsda_value;
2190 zprop_source_t zsda_source;
2191 dmu_tx_t *zsda_tx;
2192} zvol_set_snapdev_arg_t;
2193
2194/*
2195 * Sanity check the dataset for safe use by the sync task. No additional
2196 * conditions are imposed.
2197 */
2198static int
2199zvol_set_snapdev_check(void *arg, dmu_tx_t *tx)
2200{
2201 zvol_set_snapdev_arg_t *zsda = arg;
2202 dsl_pool_t *dp = dmu_tx_pool(tx);
2203 dsl_dir_t *dd;
2204 int error;
2205
2206 error = dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL);
2207 if (error != 0)
2208 return (error);
2209
2210 dsl_dir_rele(dd, FTAG);
2211
2212 return (error);
2213}
2214
92aceb2a 2215/* ARGSUSED */
a0bd735a
BP
2216static int
2217zvol_set_snapdev_sync_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
2218{
a0bd735a
BP
2219 char dsname[MAXNAMELEN];
2220 zvol_task_t *task;
92aceb2a 2221 uint64_t snapdev;
a0bd735a
BP
2222
2223 dsl_dataset_name(ds, dsname);
92aceb2a 2224 if (dsl_prop_get_int_ds(ds, "snapdev", &snapdev) != 0)
2225 return (0);
2226 task = zvol_task_alloc(ZVOL_ASYNC_SET_SNAPDEV, dsname, NULL, snapdev);
a0bd735a
BP
2227 if (task == NULL)
2228 return (0);
2229
2230 (void) taskq_dispatch(dp->dp_spa->spa_zvol_taskq, zvol_task_cb,
02730c33 2231 task, TQ_SLEEP);
a0bd735a
BP
2232 return (0);
2233}
2234
2235/*
92aceb2a 2236 * Traverse all child datasets and apply snapdev appropriately.
2237 * We call dsl_prop_set_sync_impl() here to set the value only on the toplevel
2238 * dataset and read the effective "snapdev" on every child in the callback
2239 * function: this is because the value is not guaranteed to be the same in the
2240 * whole dataset hierarchy.
a0bd735a
BP
2241 */
2242static void
2243zvol_set_snapdev_sync(void *arg, dmu_tx_t *tx)
2244{
2245 zvol_set_snapdev_arg_t *zsda = arg;
2246 dsl_pool_t *dp = dmu_tx_pool(tx);
2247 dsl_dir_t *dd;
92aceb2a 2248 dsl_dataset_t *ds;
2249 int error;
a0bd735a
BP
2250
2251 VERIFY0(dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL));
2252 zsda->zsda_tx = tx;
2253
92aceb2a 2254 error = dsl_dataset_hold(dp, zsda->zsda_name, FTAG, &ds);
2255 if (error == 0) {
2256 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_SNAPDEV),
2257 zsda->zsda_source, sizeof (zsda->zsda_value), 1,
2258 &zsda->zsda_value, zsda->zsda_tx);
2259 dsl_dataset_rele(ds, FTAG);
2260 }
a0bd735a
BP
2261 dmu_objset_find_dp(dp, dd->dd_object, zvol_set_snapdev_sync_cb,
2262 zsda, DS_FIND_CHILDREN);
2263
2264 dsl_dir_rele(dd, FTAG);
2265}
2266
0b4d1b58 2267int
a0bd735a
BP
2268zvol_set_snapdev(const char *ddname, zprop_source_t source, uint64_t snapdev)
2269{
2270 zvol_set_snapdev_arg_t zsda;
5428dc51 2271
a0bd735a
BP
2272 zsda.zsda_name = ddname;
2273 zsda.zsda_source = source;
2274 zsda.zsda_value = snapdev;
5428dc51 2275
a0bd735a
BP
2276 return (dsl_sync_task(ddname, zvol_set_snapdev_check,
2277 zvol_set_snapdev_sync, &zsda, 0, ZFS_SPACE_CHECK_NONE));
2278}
2279
2280void
2281zvol_create_minors(spa_t *spa, const char *name, boolean_t async)
2282{
2283 zvol_task_t *task;
2284 taskqid_t id;
2285
2286 task = zvol_task_alloc(ZVOL_ASYNC_CREATE_MINORS, name, NULL, ~0ULL);
2287 if (task == NULL)
2288 return;
2289
2290 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
48d3eb40 2291 if ((async == B_FALSE) && (id != TASKQID_INVALID))
a0bd735a
BP
2292 taskq_wait_id(spa->spa_zvol_taskq, id);
2293}
2294
2295void
2296zvol_remove_minors(spa_t *spa, const char *name, boolean_t async)
2297{
2298 zvol_task_t *task;
2299 taskqid_t id;
2300
2301 task = zvol_task_alloc(ZVOL_ASYNC_REMOVE_MINORS, name, NULL, ~0ULL);
2302 if (task == NULL)
2303 return;
5428dc51 2304
a0bd735a 2305 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
48d3eb40 2306 if ((async == B_FALSE) && (id != TASKQID_INVALID))
a0bd735a
BP
2307 taskq_wait_id(spa->spa_zvol_taskq, id);
2308}
2309
2310void
2311zvol_rename_minors(spa_t *spa, const char *name1, const char *name2,
2312 boolean_t async)
2313{
2314 zvol_task_t *task;
2315 taskqid_t id;
2316
2317 task = zvol_task_alloc(ZVOL_ASYNC_RENAME_MINORS, name1, name2, ~0ULL);
2318 if (task == NULL)
2319 return;
2320
2321 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
48d3eb40 2322 if ((async == B_FALSE) && (id != TASKQID_INVALID))
a0bd735a 2323 taskq_wait_id(spa->spa_zvol_taskq, id);
0b4d1b58
ED
2324}
2325
60101509
BB
2326int
2327zvol_init(void)
2328{
692e55b8 2329 int threads = MIN(MAX(zvol_threads, 1), 1024);
d45e010d 2330 int i, error;
60101509 2331
2a3871d4 2332 list_create(&zvol_state_list, sizeof (zvol_state_t),
ce37ebd2 2333 offsetof(zvol_state_t, zv_next));
2a3871d4 2334 mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
4a5d7f82 2335 ida_init(&zvol_ida);
2a3871d4 2336
692e55b8
CC
2337 zvol_taskq = taskq_create(ZVOL_DRIVER, threads, maxclsyspri,
2338 threads * 2, INT_MAX, TASKQ_PREPOPULATE | TASKQ_DYNAMIC);
2339 if (zvol_taskq == NULL) {
2340 printk(KERN_INFO "ZFS: taskq_create() failed\n");
2341 error = -ENOMEM;
2342 goto out;
2343 }
2344
d45e010d
CC
2345 zvol_htable = kmem_alloc(ZVOL_HT_SIZE * sizeof (struct hlist_head),
2346 KM_SLEEP);
2347 if (!zvol_htable) {
692e55b8
CC
2348 error = -ENOMEM;
2349 goto out_taskq;
d45e010d
CC
2350 }
2351 for (i = 0; i < ZVOL_HT_SIZE; i++)
2352 INIT_HLIST_HEAD(&zvol_htable[i]);
2353
60101509
BB
2354 error = register_blkdev(zvol_major, ZVOL_DRIVER);
2355 if (error) {
2356 printk(KERN_INFO "ZFS: register_blkdev() failed %d\n", error);
d45e010d 2357 goto out_free;
60101509
BB
2358 }
2359
2360 blk_register_region(MKDEV(zvol_major, 0), 1UL << MINORBITS,
ce37ebd2 2361 THIS_MODULE, zvol_probe, NULL, NULL);
60101509 2362
60101509 2363 return (0);
2a3871d4 2364
d45e010d
CC
2365out_free:
2366 kmem_free(zvol_htable, ZVOL_HT_SIZE * sizeof (struct hlist_head));
692e55b8
CC
2367out_taskq:
2368 taskq_destroy(zvol_taskq);
37f9dac5 2369out:
8f7933fe 2370 ida_destroy(&zvol_ida);
2a3871d4
RY
2371 mutex_destroy(&zvol_state_lock);
2372 list_destroy(&zvol_state_list);
2373
ce37ebd2 2374 return (SET_ERROR(error));
60101509
BB
2375}
2376
2377void
2378zvol_fini(void)
2379{
a0bd735a
BP
2380 zvol_remove_minors_impl(NULL);
2381
60101509
BB
2382 blk_unregister_region(MKDEV(zvol_major, 0), 1UL << MINORBITS);
2383 unregister_blkdev(zvol_major, ZVOL_DRIVER);
d45e010d 2384 kmem_free(zvol_htable, ZVOL_HT_SIZE * sizeof (struct hlist_head));
a0bd735a 2385
692e55b8 2386 taskq_destroy(zvol_taskq);
60101509 2387 list_destroy(&zvol_state_list);
a0bd735a 2388 mutex_destroy(&zvol_state_lock);
f2d8bdc6
CC
2389
2390 ida_destroy(&zvol_ida);
60101509
BB
2391}
2392
02730c33 2393/* BEGIN CSTYLED */
74497b7a
DH
2394module_param(zvol_inhibit_dev, uint, 0644);
2395MODULE_PARM_DESC(zvol_inhibit_dev, "Do not create zvol device nodes");
2396
30a9524e 2397module_param(zvol_major, uint, 0444);
60101509
BB
2398MODULE_PARM_DESC(zvol_major, "Major number for zvol device");
2399
692e55b8
CC
2400module_param(zvol_threads, uint, 0444);
2401MODULE_PARM_DESC(zvol_threads, "Max number of threads to handle I/O requests");
2402
2403module_param(zvol_request_sync, uint, 0644);
2404MODULE_PARM_DESC(zvol_request_sync, "Synchronously handle bio requests");
2405
7c0e5708 2406module_param(zvol_max_discard_blocks, ulong, 0444);
ce37ebd2 2407MODULE_PARM_DESC(zvol_max_discard_blocks, "Max number of blocks to discard");
9965059a
BB
2408
2409module_param(zvol_prefetch_bytes, uint, 0644);
2410MODULE_PARM_DESC(zvol_prefetch_bytes, "Prefetch N bytes at zvol start+end");
02730c33 2411/* END CSTYLED */