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