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