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