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