]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zvol.c
Introduce ARC Buffer Data (ABD)
[mirror_zfs.git] / module / zfs / zvol.c
CommitLineData
60101509
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright (C) 2008-2010 Lawrence Livermore National Security, LLC.
23 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
24 * Rewritten for Linux by Brian Behlendorf <behlendorf1@llnl.gov>.
25 * LLNL-CODE-403049.
26 *
27 * ZFS volume emulation driver.
28 *
29 * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
30 * Volumes are accessed through the symbolic links named:
31 *
32 * /dev/<pool_name>/<dataset_name>
33 *
34 * Volumes are persistent through reboot and module load. No user command
35 * needs to be run before opening and using a device.
460a0213
DM
36 *
37 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
5428dc51 38 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
60101509
BB
39 */
40
03c6040b 41#include <sys/dbuf.h>
60101509
BB
42#include <sys/dmu_traverse.h>
43#include <sys/dsl_dataset.h>
44#include <sys/dsl_prop.h>
a0bd735a 45#include <sys/dsl_dir.h>
60101509 46#include <sys/zap.h>
4cb7b9c5 47#include <sys/zfeature.h>
60101509 48#include <sys/zil_impl.h>
460a0213 49#include <sys/dmu_tx.h>
60101509
BB
50#include <sys/zio.h>
51#include <sys/zfs_rlock.h>
52#include <sys/zfs_znode.h>
a0bd735a 53#include <sys/spa_impl.h>
60101509 54#include <sys/zvol.h>
61e90960 55#include <linux/blkdev_compat.h>
60101509 56
74497b7a 57unsigned int zvol_inhibit_dev = 0;
60101509 58unsigned int zvol_major = ZVOL_MAJOR;
9965059a 59unsigned int zvol_prefetch_bytes = (128 * 1024);
7c0e5708 60unsigned long zvol_max_discard_blocks = 16384;
60101509 61
60101509
BB
62static kmutex_t zvol_state_lock;
63static list_t zvol_state_list;
47dfff3b 64void *zvol_tag = "zvol_tag";
60101509
BB
65
66/*
67 * The in-core state of each volume.
68 */
69typedef struct zvol_state {
4c0d8e50 70 char zv_name[MAXNAMELEN]; /* name */
ce37ebd2
BB
71 uint64_t zv_volsize; /* advertised space */
72 uint64_t zv_volblocksize; /* volume block size */
60101509
BB
73 objset_t *zv_objset; /* objset handle */
74 uint32_t zv_flags; /* ZVOL_* flags */
75 uint32_t zv_open_count; /* open counts */
76 uint32_t zv_changed; /* disk changed */
77 zilog_t *zv_zilog; /* ZIL handle */
d88895a0 78 zfs_rlock_t zv_range_lock; /* range lock */
60101509
BB
79 dmu_buf_t *zv_dbuf; /* bonus handle */
80 dev_t zv_dev; /* device id */
81 struct gendisk *zv_disk; /* generic disk */
82 struct request_queue *zv_queue; /* request queue */
60101509
BB
83 list_node_t zv_next; /* next zvol_state_t linkage */
84} zvol_state_t;
85
a0bd735a
BP
86typedef enum {
87 ZVOL_ASYNC_CREATE_MINORS,
88 ZVOL_ASYNC_REMOVE_MINORS,
89 ZVOL_ASYNC_RENAME_MINORS,
90 ZVOL_ASYNC_SET_SNAPDEV,
91 ZVOL_ASYNC_MAX
92} zvol_async_op_t;
93
94typedef struct {
95 zvol_async_op_t op;
96 char pool[MAXNAMELEN];
97 char name1[MAXNAMELEN];
98 char name2[MAXNAMELEN];
99 zprop_source_t source;
100 uint64_t snapdev;
101} zvol_task_t;
102
60101509
BB
103#define ZVOL_RDONLY 0x1
104
105/*
106 * Find the next available range of ZVOL_MINORS minor numbers. The
107 * zvol_state_list is kept in ascending minor order so we simply need
108 * to scan the list for the first gap in the sequence. This allows us
109 * to recycle minor number as devices are created and removed.
110 */
111static int
112zvol_find_minor(unsigned *minor)
113{
114 zvol_state_t *zv;
115
116 *minor = 0;
117 ASSERT(MUTEX_HELD(&zvol_state_lock));
118 for (zv = list_head(&zvol_state_list); zv != NULL;
ce37ebd2 119 zv = list_next(&zvol_state_list, zv), *minor += ZVOL_MINORS) {
60101509
BB
120 if (MINOR(zv->zv_dev) != MINOR(*minor))
121 break;
122 }
123
124 /* All minors are in use */
125 if (*minor >= (1 << MINORBITS))
ce37ebd2 126 return (SET_ERROR(ENXIO));
60101509 127
ce37ebd2 128 return (0);
60101509
BB
129}
130
131/*
132 * Find a zvol_state_t given the full major+minor dev_t.
133 */
134static zvol_state_t *
135zvol_find_by_dev(dev_t dev)
136{
137 zvol_state_t *zv;
138
139 ASSERT(MUTEX_HELD(&zvol_state_lock));
140 for (zv = list_head(&zvol_state_list); zv != NULL;
ce37ebd2 141 zv = list_next(&zvol_state_list, zv)) {
60101509 142 if (zv->zv_dev == dev)
ce37ebd2 143 return (zv);
60101509
BB
144 }
145
ce37ebd2 146 return (NULL);
60101509
BB
147}
148
149/*
150 * Find a zvol_state_t given the name provided at zvol_alloc() time.
151 */
152static zvol_state_t *
153zvol_find_by_name(const char *name)
154{
155 zvol_state_t *zv;
156
157 ASSERT(MUTEX_HELD(&zvol_state_lock));
158 for (zv = list_head(&zvol_state_list); zv != NULL;
ce37ebd2
BB
159 zv = list_next(&zvol_state_list, zv)) {
160 if (strncmp(zv->zv_name, name, MAXNAMELEN) == 0)
161 return (zv);
60101509
BB
162 }
163
ce37ebd2 164 return (NULL);
60101509
BB
165}
166
6c285672
JL
167
168/*
169 * Given a path, return TRUE if path is a ZVOL.
170 */
171boolean_t
172zvol_is_zvol(const char *device)
173{
174 struct block_device *bdev;
175 unsigned int major;
176
e02aaf17 177 bdev = vdev_lookup_bdev(device);
6c285672
JL
178 if (IS_ERR(bdev))
179 return (B_FALSE);
180
181 major = MAJOR(bdev->bd_dev);
182 bdput(bdev);
183
184 if (major == zvol_major)
ce37ebd2 185 return (B_TRUE);
6c285672
JL
186
187 return (B_FALSE);
188}
189
60101509
BB
190/*
191 * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation.
192 */
193void
194zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
195{
196 zfs_creat_t *zct = arg;
197 nvlist_t *nvprops = zct->zct_props;
198 int error;
199 uint64_t volblocksize, volsize;
200
201 VERIFY(nvlist_lookup_uint64(nvprops,
202 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
203 if (nvlist_lookup_uint64(nvprops,
204 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
205 volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
206
207 /*
208 * These properties must be removed from the list so the generic
209 * property setting step won't apply to them.
210 */
211 VERIFY(nvlist_remove_all(nvprops,
212 zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
213 (void) nvlist_remove_all(nvprops,
214 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
215
216 error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
217 DMU_OT_NONE, 0, tx);
218 ASSERT(error == 0);
219
220 error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
221 DMU_OT_NONE, 0, tx);
222 ASSERT(error == 0);
223
224 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
225 ASSERT(error == 0);
226}
227
228/*
229 * ZFS_IOC_OBJSET_STATS entry point.
230 */
231int
232zvol_get_stats(objset_t *os, nvlist_t *nv)
233{
234 int error;
235 dmu_object_info_t *doi;
236 uint64_t val;
237
238 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
239 if (error)
ce37ebd2 240 return (SET_ERROR(error));
60101509
BB
241
242 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
ce37ebd2 243 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
60101509
BB
244 error = dmu_object_info(os, ZVOL_OBJ, doi);
245
246 if (error == 0) {
247 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
248 doi->doi_data_block_size);
249 }
250
ce37ebd2 251 kmem_free(doi, sizeof (dmu_object_info_t));
60101509 252
ce37ebd2 253 return (SET_ERROR(error));
60101509
BB
254}
255
35d3e322
BB
256static void
257zvol_size_changed(zvol_state_t *zv, uint64_t volsize)
258{
259 struct block_device *bdev;
260
261 bdev = bdget_disk(zv->zv_disk, 0);
262 if (bdev == NULL)
263 return;
35d3e322
BB
264 set_capacity(zv->zv_disk, volsize >> 9);
265 zv->zv_volsize = volsize;
266 check_disk_size_change(zv->zv_disk, bdev);
35d3e322
BB
267
268 bdput(bdev);
269}
270
60101509
BB
271/*
272 * Sanity check volume size.
273 */
274int
275zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
276{
277 if (volsize == 0)
2e528b49 278 return (SET_ERROR(EINVAL));
60101509
BB
279
280 if (volsize % blocksize != 0)
2e528b49 281 return (SET_ERROR(EINVAL));
60101509
BB
282
283#ifdef _ILP32
82ec9d41 284 if (volsize - 1 > SPEC_MAXOFFSET_T)
2e528b49 285 return (SET_ERROR(EOVERFLOW));
60101509
BB
286#endif
287 return (0);
288}
289
290/*
291 * Ensure the zap is flushed then inform the VFS of the capacity change.
292 */
293static int
35d3e322 294zvol_update_volsize(uint64_t volsize, objset_t *os)
60101509 295{
60101509
BB
296 dmu_tx_t *tx;
297 int error;
513168ab 298 uint64_t txg;
60101509
BB
299
300 ASSERT(MUTEX_HELD(&zvol_state_lock));
301
df554c14 302 tx = dmu_tx_create(os);
60101509 303 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
19d55079 304 dmu_tx_mark_netfree(tx);
60101509
BB
305 error = dmu_tx_assign(tx, TXG_WAIT);
306 if (error) {
307 dmu_tx_abort(tx);
ce37ebd2 308 return (SET_ERROR(error));
60101509 309 }
513168ab 310 txg = dmu_tx_get_txg(tx);
60101509 311
df554c14 312 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
60101509
BB
313 &volsize, tx);
314 dmu_tx_commit(tx);
315
513168ab 316 txg_wait_synced(dmu_objset_pool(os), txg);
317
35d3e322
BB
318 if (error == 0)
319 error = dmu_free_long_range(os,
320 ZVOL_OBJ, volsize, DMU_OBJECT_END);
60101509 321
35d3e322
BB
322 return (error);
323}
60101509 324
35d3e322
BB
325static int
326zvol_update_live_volsize(zvol_state_t *zv, uint64_t volsize)
327{
328 zvol_size_changed(zv, volsize);
60101509 329
35d3e322
BB
330 /*
331 * We should post a event here describing the expansion. However,
332 * the zfs_ereport_post() interface doesn't nicely support posting
333 * events for zvols, it assumes events relate to vdevs or zios.
334 */
60101509
BB
335
336 return (0);
337}
338
339/*
340 * Set ZFS_PROP_VOLSIZE set entry point.
341 */
342int
343zvol_set_volsize(const char *name, uint64_t volsize)
344{
35d3e322 345 zvol_state_t *zv = NULL;
60101509 346 objset_t *os = NULL;
60101509 347 int error;
35d3e322
BB
348 dmu_object_info_t *doi;
349 uint64_t readonly;
350 boolean_t owned = B_FALSE;
60101509 351
13fe0198
MA
352 error = dsl_prop_get_integer(name,
353 zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL);
354 if (error != 0)
ce37ebd2 355 return (SET_ERROR(error));
13fe0198 356 if (readonly)
2e528b49 357 return (SET_ERROR(EROFS));
13fe0198 358
60101509 359 mutex_enter(&zvol_state_lock);
60101509 360 zv = zvol_find_by_name(name);
35d3e322
BB
361
362 if (zv == NULL || zv->zv_objset == NULL) {
363 if ((error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE,
364 FTAG, &os)) != 0) {
365 mutex_exit(&zvol_state_lock);
366 return (SET_ERROR(error));
367 }
368 owned = B_TRUE;
369 if (zv != NULL)
370 zv->zv_objset = os;
371 } else {
372 os = zv->zv_objset;
60101509
BB
373 }
374
ce37ebd2 375 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
60101509 376
ce37ebd2
BB
377 if ((error = dmu_object_info(os, ZVOL_OBJ, doi)) ||
378 (error = zvol_check_volsize(volsize, doi->doi_data_block_size)))
35d3e322 379 goto out;
60101509 380
35d3e322 381 error = zvol_update_volsize(volsize, os);
ce37ebd2 382 kmem_free(doi, sizeof (dmu_object_info_t));
60101509 383
35d3e322
BB
384 if (error == 0 && zv != NULL)
385 error = zvol_update_live_volsize(zv, volsize);
386out:
387 if (owned) {
388 dmu_objset_disown(os, FTAG);
389 if (zv != NULL)
390 zv->zv_objset = NULL;
391 }
60101509 392 mutex_exit(&zvol_state_lock);
35d3e322 393 return (error);
60101509
BB
394}
395
396/*
397 * Sanity check volume block size.
398 */
399int
4cb7b9c5 400zvol_check_volblocksize(const char *name, uint64_t volblocksize)
60101509 401{
4cb7b9c5
BB
402 /* Record sizes above 128k need the feature to be enabled */
403 if (volblocksize > SPA_OLD_MAXBLOCKSIZE) {
404 spa_t *spa;
405 int error;
406
407 if ((error = spa_open(name, &spa, FTAG)) != 0)
408 return (error);
409
410 if (!spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
411 spa_close(spa, FTAG);
412 return (SET_ERROR(ENOTSUP));
413 }
414
415 /*
416 * We don't allow setting the property above 1MB,
417 * unless the tunable has been changed.
418 */
419 if (volblocksize > zfs_max_recordsize)
420 return (SET_ERROR(EDOM));
421
422 spa_close(spa, FTAG);
423 }
424
60101509
BB
425 if (volblocksize < SPA_MINBLOCKSIZE ||
426 volblocksize > SPA_MAXBLOCKSIZE ||
427 !ISP2(volblocksize))
2e528b49 428 return (SET_ERROR(EDOM));
60101509
BB
429
430 return (0);
431}
432
433/*
434 * Set ZFS_PROP_VOLBLOCKSIZE set entry point.
435 */
436int
437zvol_set_volblocksize(const char *name, uint64_t volblocksize)
438{
439 zvol_state_t *zv;
440 dmu_tx_t *tx;
441 int error;
442
443 mutex_enter(&zvol_state_lock);
444
445 zv = zvol_find_by_name(name);
446 if (zv == NULL) {
2e528b49 447 error = SET_ERROR(ENXIO);
60101509
BB
448 goto out;
449 }
450
ba6a2402 451 if (zv->zv_flags & ZVOL_RDONLY) {
2e528b49 452 error = SET_ERROR(EROFS);
60101509
BB
453 goto out;
454 }
455
456 tx = dmu_tx_create(zv->zv_objset);
457 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
458 error = dmu_tx_assign(tx, TXG_WAIT);
459 if (error) {
460 dmu_tx_abort(tx);
461 } else {
462 error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
463 volblocksize, 0, tx);
464 if (error == ENOTSUP)
2e528b49 465 error = SET_ERROR(EBUSY);
60101509
BB
466 dmu_tx_commit(tx);
467 if (error == 0)
468 zv->zv_volblocksize = volblocksize;
469 }
470out:
471 mutex_exit(&zvol_state_lock);
472
ce37ebd2 473 return (SET_ERROR(error));
60101509
BB
474}
475
460a0213
DM
476/*
477 * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we
478 * implement DKIOCFREE/free-long-range.
479 */
480static int
481zvol_replay_truncate(zvol_state_t *zv, lr_truncate_t *lr, boolean_t byteswap)
482{
483 uint64_t offset, length;
484
485 if (byteswap)
486 byteswap_uint64_array(lr, sizeof (*lr));
487
488 offset = lr->lr_offset;
489 length = lr->lr_length;
490
491 return (dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, length));
492}
493
60101509
BB
494/*
495 * Replay a TX_WRITE ZIL transaction that didn't get committed
496 * after a system failure
497 */
498static int
499zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
500{
501 objset_t *os = zv->zv_objset;
502 char *data = (char *)(lr + 1); /* data follows lr_write_t */
503 uint64_t off = lr->lr_offset;
504 uint64_t len = lr->lr_length;
505 dmu_tx_t *tx;
506 int error;
507
508 if (byteswap)
509 byteswap_uint64_array(lr, sizeof (*lr));
510
511 tx = dmu_tx_create(os);
512 dmu_tx_hold_write(tx, ZVOL_OBJ, off, len);
513 error = dmu_tx_assign(tx, TXG_WAIT);
514 if (error) {
515 dmu_tx_abort(tx);
516 } else {
517 dmu_write(os, ZVOL_OBJ, off, len, data, tx);
518 dmu_tx_commit(tx);
519 }
520
ce37ebd2 521 return (SET_ERROR(error));
60101509
BB
522}
523
524static int
525zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
526{
2e528b49 527 return (SET_ERROR(ENOTSUP));
60101509
BB
528}
529
530/*
531 * Callback vectors for replaying records.
460a0213 532 * Only TX_WRITE and TX_TRUNCATE are needed for zvol.
60101509 533 */
b01615d5
RY
534zil_replay_func_t zvol_replay_vector[TX_MAX_TYPE] = {
535 (zil_replay_func_t)zvol_replay_err, /* no such transaction type */
536 (zil_replay_func_t)zvol_replay_err, /* TX_CREATE */
537 (zil_replay_func_t)zvol_replay_err, /* TX_MKDIR */
538 (zil_replay_func_t)zvol_replay_err, /* TX_MKXATTR */
539 (zil_replay_func_t)zvol_replay_err, /* TX_SYMLINK */
540 (zil_replay_func_t)zvol_replay_err, /* TX_REMOVE */
541 (zil_replay_func_t)zvol_replay_err, /* TX_RMDIR */
542 (zil_replay_func_t)zvol_replay_err, /* TX_LINK */
543 (zil_replay_func_t)zvol_replay_err, /* TX_RENAME */
544 (zil_replay_func_t)zvol_replay_write, /* TX_WRITE */
460a0213 545 (zil_replay_func_t)zvol_replay_truncate, /* TX_TRUNCATE */
b01615d5
RY
546 (zil_replay_func_t)zvol_replay_err, /* TX_SETATTR */
547 (zil_replay_func_t)zvol_replay_err, /* TX_ACL */
60101509
BB
548};
549
550/*
551 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
552 *
553 * We store data in the log buffers if it's small enough.
554 * Otherwise we will later flush the data out via dmu_sync().
555 */
556ssize_t zvol_immediate_write_sz = 32768;
557
558static void
ce37ebd2
BB
559zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, uint64_t offset,
560 uint64_t size, int sync)
60101509
BB
561{
562 uint32_t blocksize = zv->zv_volblocksize;
563 zilog_t *zilog = zv->zv_zilog;
564 boolean_t slogging;
ab85f845 565 ssize_t immediate_write_sz;
60101509
BB
566
567 if (zil_replaying(zilog, tx))
568 return;
569
ab85f845
ED
570 immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
571 ? 0 : zvol_immediate_write_sz;
572 slogging = spa_has_slogs(zilog->zl_spa) &&
573 (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
60101509
BB
574
575 while (size) {
576 itx_t *itx;
577 lr_write_t *lr;
578 ssize_t len;
579 itx_wr_state_t write_state;
580
581 /*
582 * Unlike zfs_log_write() we can be called with
583 * up to DMU_MAX_ACCESS/2 (5MB) writes.
584 */
ab85f845 585 if (blocksize > immediate_write_sz && !slogging &&
60101509
BB
586 size >= blocksize && offset % blocksize == 0) {
587 write_state = WR_INDIRECT; /* uses dmu_sync */
588 len = blocksize;
589 } else if (sync) {
590 write_state = WR_COPIED;
591 len = MIN(ZIL_MAX_LOG_DATA, size);
592 } else {
593 write_state = WR_NEED_COPY;
594 len = MIN(ZIL_MAX_LOG_DATA, size);
595 }
596
597 itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
598 (write_state == WR_COPIED ? len : 0));
599 lr = (lr_write_t *)&itx->itx_lr;
600 if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
601 ZVOL_OBJ, offset, len, lr+1, DMU_READ_NO_PREFETCH) != 0) {
602 zil_itx_destroy(itx);
603 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
604 lr = (lr_write_t *)&itx->itx_lr;
605 write_state = WR_NEED_COPY;
606 }
607
608 itx->itx_wr_state = write_state;
609 if (write_state == WR_NEED_COPY)
610 itx->itx_sod += len;
611 lr->lr_foid = ZVOL_OBJ;
612 lr->lr_offset = offset;
613 lr->lr_length = len;
614 lr->lr_blkoff = 0;
615 BP_ZERO(&lr->lr_blkptr);
616
617 itx->itx_private = zv;
618 itx->itx_sync = sync;
619
620 (void) zil_itx_assign(zilog, itx, tx);
621
622 offset += len;
623 size -= len;
624 }
625}
626
37f9dac5 627static int
a765a34a 628zvol_write(zvol_state_t *zv, uio_t *uio, boolean_t sync)
60101509 629{
a765a34a 630 uint64_t volsize = zv->zv_volsize;
60101509 631 rl_t *rl;
a765a34a 632 int error = 0;
60101509 633
5428dc51
BP
634 ASSERT(zv && zv->zv_open_count > 0);
635
d88895a0
CC
636 rl = zfs_range_lock(&zv->zv_range_lock, uio->uio_loffset,
637 uio->uio_resid, RL_WRITER);
b18019d2 638
a765a34a
RY
639 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
640 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
641 uint64_t off = uio->uio_loffset;
642 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
b18019d2 643
a765a34a
RY
644 if (bytes > volsize - off) /* don't write past the end */
645 bytes = volsize - off;
2727b9d3 646
a765a34a 647 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
60101509 648
a765a34a
RY
649 /* This will only fail for ENOSPC */
650 error = dmu_tx_assign(tx, TXG_WAIT);
651 if (error) {
652 dmu_tx_abort(tx);
653 break;
654 }
655 error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx);
656 if (error == 0)
657 zvol_log_write(zv, tx, off, bytes, sync);
658 dmu_tx_commit(tx);
60101509 659
a765a34a
RY
660 if (error)
661 break;
60101509 662 }
60101509 663 zfs_range_unlock(rl);
a765a34a 664 if (sync)
60101509 665 zil_commit(zv->zv_zilog, ZVOL_OBJ);
37f9dac5 666 return (error);
60101509
BB
667}
668
460a0213
DM
669/*
670 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE.
671 */
672static void
673zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len,
674 boolean_t sync)
675{
676 itx_t *itx;
677 lr_truncate_t *lr;
678 zilog_t *zilog = zv->zv_zilog;
679
680 if (zil_replaying(zilog, tx))
681 return;
682
683 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
684 lr = (lr_truncate_t *)&itx->itx_lr;
685 lr->lr_foid = ZVOL_OBJ;
686 lr->lr_offset = off;
687 lr->lr_length = len;
688
689 itx->itx_sync = sync;
690 zil_itx_assign(zilog, itx, tx);
691}
692
37f9dac5
RY
693static int
694zvol_discard(struct bio *bio)
30930fba 695{
37f9dac5
RY
696 zvol_state_t *zv = bio->bi_bdev->bd_disk->private_data;
697 uint64_t start = BIO_BI_SECTOR(bio) << 9;
698 uint64_t size = BIO_BI_SIZE(bio);
699 uint64_t end = start + size;
30930fba
ED
700 int error;
701 rl_t *rl;
460a0213 702 dmu_tx_t *tx;
30930fba 703
5428dc51
BP
704 ASSERT(zv && zv->zv_open_count > 0);
705
37f9dac5
RY
706 if (end > zv->zv_volsize)
707 return (SET_ERROR(EIO));
30930fba 708
089fa91b 709 /*
cf41432c
BB
710 * Align the request to volume block boundaries when a secure erase is
711 * not required. This will prevent dnode_free_range() from zeroing out
712 * the unaligned parts which is slow (read-modify-write) and useless
713 * since we are not freeing any space by doing so.
089fa91b 714 */
cf41432c 715 if (!bio_is_secure_erase(bio)) {
fa565676
RY
716 start = P2ROUNDUP(start, zv->zv_volblocksize);
717 end = P2ALIGN(end, zv->zv_volblocksize);
f52ebcb3 718 size = end - start;
fa565676 719 }
089fa91b 720
37f9dac5
RY
721 if (start >= end)
722 return (0);
30930fba 723
d88895a0 724 rl = zfs_range_lock(&zv->zv_range_lock, start, size, RL_WRITER);
460a0213
DM
725 tx = dmu_tx_create(zv->zv_objset);
726 dmu_tx_mark_netfree(tx);
727 error = dmu_tx_assign(tx, TXG_WAIT);
728 if (error != 0) {
729 dmu_tx_abort(tx);
730 } else {
731 zvol_log_truncate(zv, tx, start, size, B_TRUE);
732 dmu_tx_commit(tx);
733 error = dmu_free_long_range(zv->zv_objset,
734 ZVOL_OBJ, start, size);
735 }
30930fba
ED
736
737 zfs_range_unlock(rl);
37f9dac5
RY
738
739 return (error);
30930fba 740}
30930fba 741
37f9dac5 742static int
a765a34a 743zvol_read(zvol_state_t *zv, uio_t *uio)
60101509 744{
a765a34a 745 uint64_t volsize = zv->zv_volsize;
60101509 746 rl_t *rl;
a765a34a 747 int error = 0;
b18019d2 748
5428dc51
BP
749 ASSERT(zv && zv->zv_open_count > 0);
750
d88895a0
CC
751 rl = zfs_range_lock(&zv->zv_range_lock, uio->uio_loffset,
752 uio->uio_resid, RL_READER);
a765a34a
RY
753 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
754 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
60101509 755
a765a34a
RY
756 /* don't read past the end */
757 if (bytes > volsize - uio->uio_loffset)
758 bytes = volsize - uio->uio_loffset;
60101509 759
19a47cb1 760 error = dmu_read_uio_dbuf(zv->zv_dbuf, uio, bytes);
a765a34a
RY
761 if (error) {
762 /* convert checksum errors into IO errors */
763 if (error == ECKSUM)
764 error = SET_ERROR(EIO);
765 break;
766 }
767 }
60101509 768 zfs_range_unlock(rl);
37f9dac5 769 return (error);
60101509
BB
770}
771
37f9dac5
RY
772static MAKE_REQUEST_FN_RET
773zvol_request(struct request_queue *q, struct bio *bio)
60101509 774{
a765a34a 775 uio_t uio;
60101509 776 zvol_state_t *zv = q->queuedata;
37f9dac5 777 fstrans_cookie_t cookie = spl_fstrans_mark();
8198d18c
RY
778 int rw = bio_data_dir(bio);
779#ifdef HAVE_GENERIC_IO_ACCT
780 unsigned long start = jiffies;
781#endif
37f9dac5 782 int error = 0;
60101509 783
a765a34a
RY
784 uio.uio_bvec = &bio->bi_io_vec[BIO_BI_IDX(bio)];
785 uio.uio_skip = BIO_BI_SKIP(bio);
786 uio.uio_resid = BIO_BI_SIZE(bio);
787 uio.uio_iovcnt = bio->bi_vcnt - BIO_BI_IDX(bio);
788 uio.uio_loffset = BIO_BI_SECTOR(bio) << 9;
789 uio.uio_limit = MAXOFFSET_T;
790 uio.uio_segflg = UIO_BVEC;
791
792 if (bio_has_data(bio) && uio.uio_loffset + uio.uio_resid >
793 zv->zv_volsize) {
37f9dac5 794 printk(KERN_INFO
a765a34a 795 "%s: bad access: offset=%llu, size=%lu\n",
37f9dac5 796 zv->zv_disk->disk_name,
a765a34a
RY
797 (long long unsigned)uio.uio_loffset,
798 (long unsigned)uio.uio_resid);
37f9dac5 799 error = SET_ERROR(EIO);
8198d18c 800 goto out1;
37f9dac5
RY
801 }
802
a765a34a 803 generic_start_io_acct(rw, bio_sectors(bio), &zv->zv_disk->part0);
8198d18c
RY
804
805 if (rw == WRITE) {
37f9dac5
RY
806 if (unlikely(zv->zv_flags & ZVOL_RDONLY)) {
807 error = SET_ERROR(EROFS);
8198d18c 808 goto out2;
60101509
BB
809 }
810
cf41432c 811 if (bio_is_discard(bio) || bio_is_secure_erase(bio)) {
37f9dac5 812 error = zvol_discard(bio);
8198d18c 813 goto out2;
37f9dac5 814 }
60101509 815
a765a34a
RY
816 /*
817 * Some requests are just for flush and nothing else.
818 */
819 if (uio.uio_resid == 0) {
cf41432c 820 if (bio_is_flush(bio))
a765a34a
RY
821 zil_commit(zv->zv_zilog, ZVOL_OBJ);
822 goto out2;
823 }
824
825 error = zvol_write(zv, &uio,
cf41432c
BB
826 bio_is_flush(bio) || bio_is_fua(bio) ||
827 zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS);
37f9dac5 828 } else
a765a34a 829 error = zvol_read(zv, &uio);
30930fba 830
8198d18c
RY
831out2:
832 generic_end_io_acct(rw, &zv->zv_disk->part0, start);
833out1:
784a7fe5 834 BIO_END_IO(bio, -error);
37f9dac5
RY
835 spl_fstrans_unmark(cookie);
836#ifdef HAVE_MAKE_REQUEST_FN_RET_INT
837 return (0);
1a093716
CC
838#elif defined(HAVE_MAKE_REQUEST_FN_RET_QC)
839 return (BLK_QC_T_NONE);
37f9dac5 840#endif
60101509
BB
841}
842
843static void
844zvol_get_done(zgd_t *zgd, int error)
845{
846 if (zgd->zgd_db)
847 dmu_buf_rele(zgd->zgd_db, zgd);
848
849 zfs_range_unlock(zgd->zgd_rl);
850
851 if (error == 0 && zgd->zgd_bp)
852 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
853
854 kmem_free(zgd, sizeof (zgd_t));
855}
856
857/*
858 * Get data to generate a TX_WRITE intent log record.
859 */
860static int
861zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
862{
863 zvol_state_t *zv = arg;
864 objset_t *os = zv->zv_objset;
03c6040b 865 uint64_t object = ZVOL_OBJ;
60101509
BB
866 uint64_t offset = lr->lr_offset;
867 uint64_t size = lr->lr_length;
03c6040b 868 blkptr_t *bp = &lr->lr_blkptr;
60101509
BB
869 dmu_buf_t *db;
870 zgd_t *zgd;
871 int error;
872
873 ASSERT(zio != NULL);
874 ASSERT(size != 0);
875
79c76d5b 876 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
60101509 877 zgd->zgd_zilog = zv->zv_zilog;
d88895a0
CC
878 zgd->zgd_rl = zfs_range_lock(&zv->zv_range_lock, offset, size,
879 RL_READER);
60101509
BB
880
881 /*
882 * Write records come in two flavors: immediate and indirect.
883 * For small writes it's cheaper to store the data with the
884 * log record (immediate); for large writes it's cheaper to
885 * sync the data and get a pointer to it (indirect) so that
886 * we don't have to write the data twice.
887 */
888 if (buf != NULL) { /* immediate write */
03c6040b 889 error = dmu_read(os, object, offset, size, buf,
60101509
BB
890 DMU_READ_NO_PREFETCH);
891 } else {
892 size = zv->zv_volblocksize;
893 offset = P2ALIGN_TYPED(offset, size, uint64_t);
03c6040b 894 error = dmu_buf_hold(os, object, offset, zgd, &db,
60101509
BB
895 DMU_READ_NO_PREFETCH);
896 if (error == 0) {
03c6040b
GW
897 blkptr_t *obp = dmu_buf_get_blkptr(db);
898 if (obp) {
899 ASSERT(BP_IS_HOLE(bp));
900 *bp = *obp;
901 }
902
60101509
BB
903 zgd->zgd_db = db;
904 zgd->zgd_bp = &lr->lr_blkptr;
905
906 ASSERT(db != NULL);
907 ASSERT(db->db_offset == offset);
908 ASSERT(db->db_size == size);
909
910 error = dmu_sync(zio, lr->lr_common.lrc_txg,
911 zvol_get_done, zgd);
912
913 if (error == 0)
914 return (0);
915 }
916 }
917
918 zvol_get_done(zgd, error);
919
ce37ebd2 920 return (SET_ERROR(error));
60101509
BB
921}
922
923/*
924 * The zvol_state_t's are inserted in increasing MINOR(dev_t) order.
925 */
926static void
927zvol_insert(zvol_state_t *zv_insert)
928{
929 zvol_state_t *zv = NULL;
930
931 ASSERT(MUTEX_HELD(&zvol_state_lock));
932 ASSERT3U(MINOR(zv_insert->zv_dev) & ZVOL_MINOR_MASK, ==, 0);
933 for (zv = list_head(&zvol_state_list); zv != NULL;
ce37ebd2 934 zv = list_next(&zvol_state_list, zv)) {
60101509
BB
935 if (MINOR(zv->zv_dev) > MINOR(zv_insert->zv_dev))
936 break;
937 }
938
939 list_insert_before(&zvol_state_list, zv, zv_insert);
940}
941
942/*
943 * Simply remove the zvol from to list of zvols.
944 */
945static void
946zvol_remove(zvol_state_t *zv_remove)
947{
948 ASSERT(MUTEX_HELD(&zvol_state_lock));
949 list_remove(&zvol_state_list, zv_remove);
950}
951
952static int
953zvol_first_open(zvol_state_t *zv)
954{
955 objset_t *os;
956 uint64_t volsize;
957 int error;
958 uint64_t ro;
959
960 /* lie and say we're read-only */
961 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, 1, zvol_tag, &os);
962 if (error)
1ee159f4
BP
963 return (SET_ERROR(-error));
964
965 zv->zv_objset = os;
966
967 error = dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL);
968 if (error)
969 goto out_owned;
60101509
BB
970
971 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
1ee159f4
BP
972 if (error)
973 goto out_owned;
60101509 974
60101509 975 error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
1ee159f4
BP
976 if (error)
977 goto out_owned;
60101509
BB
978
979 set_capacity(zv->zv_disk, volsize >> 9);
980 zv->zv_volsize = volsize;
981 zv->zv_zilog = zil_open(os, zvol_get_data);
982
a4430fce
GW
983 if (ro || dmu_objset_is_snapshot(os) ||
984 !spa_writeable(dmu_objset_spa(os))) {
babf3f9b
MM
985 set_disk_ro(zv->zv_disk, 1);
986 zv->zv_flags |= ZVOL_RDONLY;
60101509 987 } else {
babf3f9b
MM
988 set_disk_ro(zv->zv_disk, 0);
989 zv->zv_flags &= ~ZVOL_RDONLY;
60101509
BB
990 }
991
1ee159f4
BP
992out_owned:
993 if (error) {
994 dmu_objset_disown(os, zvol_tag);
995 zv->zv_objset = NULL;
996 }
babf3f9b 997
ce37ebd2 998 return (SET_ERROR(-error));
60101509
BB
999}
1000
1001static void
1002zvol_last_close(zvol_state_t *zv)
1003{
1004 zil_close(zv->zv_zilog);
1005 zv->zv_zilog = NULL;
04434775 1006
60101509
BB
1007 dmu_buf_rele(zv->zv_dbuf, zvol_tag);
1008 zv->zv_dbuf = NULL;
04434775
MA
1009
1010 /*
1011 * Evict cached data
1012 */
1013 if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) &&
1014 !(zv->zv_flags & ZVOL_RDONLY))
1015 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1016 (void) dmu_objset_evict_dbufs(zv->zv_objset);
1017
60101509
BB
1018 dmu_objset_disown(zv->zv_objset, zvol_tag);
1019 zv->zv_objset = NULL;
1020}
1021
1022static int
1023zvol_open(struct block_device *bdev, fmode_t flag)
1024{
5428dc51 1025 zvol_state_t *zv;
60101509
BB
1026 int error = 0, drop_mutex = 0;
1027
1028 /*
1029 * If the caller is already holding the mutex do not take it
a0bd735a 1030 * again, this will happen as part of zvol_create_minor_impl().
60101509
BB
1031 * Once add_disk() is called the device is live and the kernel
1032 * will attempt to open it to read the partition information.
1033 */
1034 if (!mutex_owned(&zvol_state_lock)) {
1035 mutex_enter(&zvol_state_lock);
1036 drop_mutex = 1;
1037 }
1038
5428dc51
BP
1039 /*
1040 * Obtain a copy of private_data under the lock to make sure
1041 * that either the result of zvol_freeg() setting
1042 * bdev->bd_disk->private_data to NULL is observed, or zvol_free()
1043 * is not called on this zv because of the positive zv_open_count.
1044 */
1045 zv = bdev->bd_disk->private_data;
1046 if (zv == NULL) {
1047 error = -ENXIO;
1048 goto out_mutex;
1049 }
60101509
BB
1050
1051 if (zv->zv_open_count == 0) {
1052 error = zvol_first_open(zv);
1053 if (error)
1054 goto out_mutex;
1055 }
1056
ba6a2402 1057 if ((flag & FMODE_WRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
60101509
BB
1058 error = -EROFS;
1059 goto out_open_count;
1060 }
1061
1062 zv->zv_open_count++;
1063
5428dc51
BP
1064 check_disk_change(bdev);
1065
60101509
BB
1066out_open_count:
1067 if (zv->zv_open_count == 0)
1068 zvol_last_close(zv);
1069
1070out_mutex:
1071 if (drop_mutex)
1072 mutex_exit(&zvol_state_lock);
1073
ce37ebd2 1074 return (SET_ERROR(error));
60101509
BB
1075}
1076
a1d9543a
CD
1077#ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1078static void
1079#else
60101509 1080static int
a1d9543a 1081#endif
60101509
BB
1082zvol_release(struct gendisk *disk, fmode_t mode)
1083{
1084 zvol_state_t *zv = disk->private_data;
1085 int drop_mutex = 0;
1086
5428dc51
BP
1087 ASSERT(zv && zv->zv_open_count > 0);
1088
60101509
BB
1089 if (!mutex_owned(&zvol_state_lock)) {
1090 mutex_enter(&zvol_state_lock);
1091 drop_mutex = 1;
1092 }
1093
5428dc51
BP
1094 zv->zv_open_count--;
1095 if (zv->zv_open_count == 0)
1096 zvol_last_close(zv);
60101509
BB
1097
1098 if (drop_mutex)
1099 mutex_exit(&zvol_state_lock);
1100
a1d9543a 1101#ifndef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
60101509 1102 return (0);
a1d9543a 1103#endif
60101509
BB
1104}
1105
1106static int
1107zvol_ioctl(struct block_device *bdev, fmode_t mode,
ce37ebd2 1108 unsigned int cmd, unsigned long arg)
60101509
BB
1109{
1110 zvol_state_t *zv = bdev->bd_disk->private_data;
1111 int error = 0;
1112
5428dc51 1113 ASSERT(zv && zv->zv_open_count > 0);
60101509
BB
1114
1115 switch (cmd) {
1116 case BLKFLSBUF:
1117 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1118 break;
4c0d8e50
FN
1119 case BLKZNAME:
1120 error = copy_to_user((void *)arg, zv->zv_name, MAXNAMELEN);
1121 break;
60101509
BB
1122
1123 default:
1124 error = -ENOTTY;
1125 break;
1126
1127 }
1128
ce37ebd2 1129 return (SET_ERROR(error));
60101509
BB
1130}
1131
1132#ifdef CONFIG_COMPAT
1133static int
1134zvol_compat_ioctl(struct block_device *bdev, fmode_t mode,
ce37ebd2 1135 unsigned cmd, unsigned long arg)
60101509 1136{
ce37ebd2 1137 return (zvol_ioctl(bdev, mode, cmd, arg));
60101509
BB
1138}
1139#else
ce37ebd2 1140#define zvol_compat_ioctl NULL
60101509
BB
1141#endif
1142
1143static int zvol_media_changed(struct gendisk *disk)
1144{
1145 zvol_state_t *zv = disk->private_data;
1146
5428dc51
BP
1147 ASSERT(zv && zv->zv_open_count > 0);
1148
ce37ebd2 1149 return (zv->zv_changed);
60101509
BB
1150}
1151
1152static int zvol_revalidate_disk(struct gendisk *disk)
1153{
1154 zvol_state_t *zv = disk->private_data;
1155
5428dc51
BP
1156 ASSERT(zv && zv->zv_open_count > 0);
1157
60101509
BB
1158 zv->zv_changed = 0;
1159 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1160
ce37ebd2 1161 return (0);
60101509
BB
1162}
1163
1164/*
1165 * Provide a simple virtual geometry for legacy compatibility. For devices
1166 * smaller than 1 MiB a small head and sector count is used to allow very
1167 * tiny devices. For devices over 1 Mib a standard head and sector count
1168 * is used to keep the cylinders count reasonable.
1169 */
1170static int
1171zvol_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1172{
1173 zvol_state_t *zv = bdev->bd_disk->private_data;
5428dc51
BP
1174 sector_t sectors;
1175
1176 ASSERT(zv && zv->zv_open_count > 0);
1177
1178 sectors = get_capacity(zv->zv_disk);
60101509
BB
1179
1180 if (sectors > 2048) {
1181 geo->heads = 16;
1182 geo->sectors = 63;
1183 } else {
1184 geo->heads = 2;
1185 geo->sectors = 4;
1186 }
1187
1188 geo->start = 0;
1189 geo->cylinders = sectors / (geo->heads * geo->sectors);
1190
ce37ebd2 1191 return (0);
60101509
BB
1192}
1193
1194static struct kobject *
1195zvol_probe(dev_t dev, int *part, void *arg)
1196{
1197 zvol_state_t *zv;
1198 struct kobject *kobj;
1199
1200 mutex_enter(&zvol_state_lock);
1201 zv = zvol_find_by_dev(dev);
23a61ccc 1202 kobj = zv ? get_disk(zv->zv_disk) : NULL;
60101509
BB
1203 mutex_exit(&zvol_state_lock);
1204
ce37ebd2 1205 return (kobj);
60101509
BB
1206}
1207
1208#ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS
1209static struct block_device_operations zvol_ops = {
ce37ebd2
BB
1210 .open = zvol_open,
1211 .release = zvol_release,
1212 .ioctl = zvol_ioctl,
1213 .compat_ioctl = zvol_compat_ioctl,
1214 .media_changed = zvol_media_changed,
1215 .revalidate_disk = zvol_revalidate_disk,
1216 .getgeo = zvol_getgeo,
1217 .owner = THIS_MODULE,
60101509
BB
1218};
1219
1220#else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1221
1222static int
1223zvol_open_by_inode(struct inode *inode, struct file *file)
1224{
ce37ebd2 1225 return (zvol_open(inode->i_bdev, file->f_mode));
60101509
BB
1226}
1227
1228static int
1229zvol_release_by_inode(struct inode *inode, struct file *file)
1230{
ce37ebd2 1231 return (zvol_release(inode->i_bdev->bd_disk, file->f_mode));
60101509
BB
1232}
1233
1234static int
1235zvol_ioctl_by_inode(struct inode *inode, struct file *file,
ce37ebd2 1236 unsigned int cmd, unsigned long arg)
60101509 1237{
b1c58213 1238 if (file == NULL || inode == NULL)
ce37ebd2
BB
1239 return (SET_ERROR(-EINVAL));
1240
1241 return (zvol_ioctl(inode->i_bdev, file->f_mode, cmd, arg));
60101509
BB
1242}
1243
ce37ebd2 1244#ifdef CONFIG_COMPAT
60101509
BB
1245static long
1246zvol_compat_ioctl_by_inode(struct file *file,
ce37ebd2 1247 unsigned int cmd, unsigned long arg)
60101509 1248{
b1c58213 1249 if (file == NULL)
ce37ebd2
BB
1250 return (SET_ERROR(-EINVAL));
1251
1252 return (zvol_compat_ioctl(file->f_dentry->d_inode->i_bdev,
1253 file->f_mode, cmd, arg));
60101509 1254}
ce37ebd2
BB
1255#else
1256#define zvol_compat_ioctl_by_inode NULL
1257#endif
60101509
BB
1258
1259static struct block_device_operations zvol_ops = {
ce37ebd2
BB
1260 .open = zvol_open_by_inode,
1261 .release = zvol_release_by_inode,
1262 .ioctl = zvol_ioctl_by_inode,
1263 .compat_ioctl = zvol_compat_ioctl_by_inode,
1264 .media_changed = zvol_media_changed,
1265 .revalidate_disk = zvol_revalidate_disk,
1266 .getgeo = zvol_getgeo,
1267 .owner = THIS_MODULE,
60101509
BB
1268};
1269#endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1270
1271/*
1272 * Allocate memory for a new zvol_state_t and setup the required
1273 * request queue and generic disk structures for the block device.
1274 */
1275static zvol_state_t *
1276zvol_alloc(dev_t dev, const char *name)
1277{
1278 zvol_state_t *zv;
1279
79c76d5b 1280 zv = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
60101509 1281
2a3871d4
RY
1282 list_link_init(&zv->zv_next);
1283
37f9dac5 1284 zv->zv_queue = blk_alloc_queue(GFP_ATOMIC);
60101509
BB
1285 if (zv->zv_queue == NULL)
1286 goto out_kmem;
1287
37f9dac5 1288 blk_queue_make_request(zv->zv_queue, zvol_request);
cf41432c 1289 blk_queue_set_write_cache(zv->zv_queue, B_TRUE, B_TRUE);
b18019d2 1290
60101509
BB
1291 zv->zv_disk = alloc_disk(ZVOL_MINORS);
1292 if (zv->zv_disk == NULL)
1293 goto out_queue;
1294
1295 zv->zv_queue->queuedata = zv;
1296 zv->zv_dev = dev;
1297 zv->zv_open_count = 0;
4c0d8e50 1298 strlcpy(zv->zv_name, name, MAXNAMELEN);
60101509 1299
d88895a0 1300 zfs_rlock_init(&zv->zv_range_lock);
3c4988c8 1301
60101509
BB
1302 zv->zv_disk->major = zvol_major;
1303 zv->zv_disk->first_minor = (dev & MINORMASK);
1304 zv->zv_disk->fops = &zvol_ops;
1305 zv->zv_disk->private_data = zv;
1306 zv->zv_disk->queue = zv->zv_queue;
4c0d8e50
FN
1307 snprintf(zv->zv_disk->disk_name, DISK_NAME_LEN, "%s%d",
1308 ZVOL_DEV_NAME, (dev & MINORMASK));
60101509 1309
ce37ebd2 1310 return (zv);
60101509
BB
1311
1312out_queue:
1313 blk_cleanup_queue(zv->zv_queue);
1314out_kmem:
1315 kmem_free(zv, sizeof (zvol_state_t));
0a6bef26 1316
ce37ebd2 1317 return (NULL);
60101509
BB
1318}
1319
1320/*
1321 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1322 */
1323static void
1324zvol_free(zvol_state_t *zv)
1325{
5428dc51
BP
1326 ASSERT(MUTEX_HELD(&zvol_state_lock));
1327 ASSERT(zv->zv_open_count == 0);
1328
d88895a0 1329 zfs_rlock_destroy(&zv->zv_range_lock);
60101509 1330
5428dc51
BP
1331 zv->zv_disk->private_data = NULL;
1332
60101509
BB
1333 del_gendisk(zv->zv_disk);
1334 blk_cleanup_queue(zv->zv_queue);
1335 put_disk(zv->zv_disk);
1336
1337 kmem_free(zv, sizeof (zvol_state_t));
1338}
1339
a0bd735a
BP
1340/*
1341 * Create a block device minor node and setup the linkage between it
1342 * and the specified volume. Once this function returns the block
1343 * device is live and ready for use.
1344 */
60101509 1345static int
a0bd735a 1346zvol_create_minor_impl(const char *name)
60101509
BB
1347{
1348 zvol_state_t *zv;
1349 objset_t *os;
1350 dmu_object_info_t *doi;
1351 uint64_t volsize;
9965059a 1352 uint64_t len;
60101509
BB
1353 unsigned minor = 0;
1354 int error = 0;
1355
a0bd735a 1356 mutex_enter(&zvol_state_lock);
60101509
BB
1357
1358 zv = zvol_find_by_name(name);
1359 if (zv) {
2e528b49 1360 error = SET_ERROR(EEXIST);
60101509
BB
1361 goto out;
1362 }
1363
79c76d5b 1364 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
60101509
BB
1365
1366 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
1367 if (error)
1368 goto out_doi;
1369
1370 error = dmu_object_info(os, ZVOL_OBJ, doi);
1371 if (error)
1372 goto out_dmu_objset_disown;
1373
1374 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
1375 if (error)
1376 goto out_dmu_objset_disown;
1377
1378 error = zvol_find_minor(&minor);
1379 if (error)
1380 goto out_dmu_objset_disown;
1381
1382 zv = zvol_alloc(MKDEV(zvol_major, minor), name);
1383 if (zv == NULL) {
2e528b49 1384 error = SET_ERROR(EAGAIN);
60101509
BB
1385 goto out_dmu_objset_disown;
1386 }
1387
1388 if (dmu_objset_is_snapshot(os))
1389 zv->zv_flags |= ZVOL_RDONLY;
1390
1391 zv->zv_volblocksize = doi->doi_data_block_size;
1392 zv->zv_volsize = volsize;
1393 zv->zv_objset = os;
1394
1395 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1396
c495fe2c 1397 blk_queue_max_hw_sectors(zv->zv_queue, (DMU_MAX_ACCESS / 4) >> 9);
34037afe
ED
1398 blk_queue_max_segments(zv->zv_queue, UINT16_MAX);
1399 blk_queue_max_segment_size(zv->zv_queue, UINT_MAX);
1400 blk_queue_physical_block_size(zv->zv_queue, zv->zv_volblocksize);
1401 blk_queue_io_opt(zv->zv_queue, zv->zv_volblocksize);
7c0e5708
ED
1402 blk_queue_max_discard_sectors(zv->zv_queue,
1403 (zvol_max_discard_blocks * zv->zv_volblocksize) >> 9);
ee5fd0bb 1404 blk_queue_discard_granularity(zv->zv_queue, zv->zv_volblocksize);
30930fba 1405 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zv->zv_queue);
37f9dac5 1406#ifdef QUEUE_FLAG_NONROT
34037afe
ED
1407 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zv->zv_queue);
1408#endif
c6a3a222
RY
1409#ifdef QUEUE_FLAG_ADD_RANDOM
1410 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, zv->zv_queue);
1411#endif
34037afe 1412
a4430fce
GW
1413 if (spa_writeable(dmu_objset_spa(os))) {
1414 if (zil_replay_disable)
1415 zil_destroy(dmu_objset_zil(os), B_FALSE);
1416 else
1417 zil_replay(os, zv, zvol_replay_vector);
1418 }
60101509 1419
9965059a
BB
1420 /*
1421 * When udev detects the addition of the device it will immediately
1422 * invoke blkid(8) to determine the type of content on the device.
1423 * Prefetching the blocks commonly scanned by blkid(8) will speed
1424 * up this process.
1425 */
1426 len = MIN(MAX(zvol_prefetch_bytes, 0), SPA_MAXBLOCKSIZE);
1427 if (len > 0) {
fcff0f35
PD
1428 dmu_prefetch(os, ZVOL_OBJ, 0, 0, len, ZIO_PRIORITY_SYNC_READ);
1429 dmu_prefetch(os, ZVOL_OBJ, 0, volsize - len, len,
1430 ZIO_PRIORITY_SYNC_READ);
9965059a
BB
1431 }
1432
f74a147c 1433 zv->zv_objset = NULL;
60101509
BB
1434out_dmu_objset_disown:
1435 dmu_objset_disown(os, zvol_tag);
60101509 1436out_doi:
ce37ebd2 1437 kmem_free(doi, sizeof (dmu_object_info_t));
60101509
BB
1438out:
1439
1440 if (error == 0) {
1441 zvol_insert(zv);
5428dc51
BP
1442 /*
1443 * Drop the lock to prevent deadlock with sys_open() ->
1444 * zvol_open(), which first takes bd_disk->bd_mutex and then
1445 * takes zvol_state_lock, whereas this code path first takes
1446 * zvol_state_lock, and then takes bd_disk->bd_mutex.
1447 */
1448 mutex_exit(&zvol_state_lock);
60101509 1449 add_disk(zv->zv_disk);
a0bd735a
BP
1450 } else {
1451 mutex_exit(&zvol_state_lock);
60101509
BB
1452 }
1453
ce37ebd2 1454 return (SET_ERROR(error));
60101509
BB
1455}
1456
ba6a2402
BB
1457/*
1458 * Rename a block device minor mode for the specified volume.
1459 */
1460static void
a0bd735a 1461zvol_rename_minor(zvol_state_t *zv, const char *newname)
ba6a2402
BB
1462{
1463 int readonly = get_disk_ro(zv->zv_disk);
1464
1465 ASSERT(MUTEX_HELD(&zvol_state_lock));
1466
1467 strlcpy(zv->zv_name, newname, sizeof (zv->zv_name));
1468
1469 /*
1470 * The block device's read-only state is briefly changed causing
1471 * a KOBJ_CHANGE uevent to be issued. This ensures udev detects
1472 * the name change and fixes the symlinks. This does not change
1473 * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
1474 * changes. This would normally be done using kobject_uevent() but
1475 * that is a GPL-only symbol which is why we need this workaround.
1476 */
1477 set_disk_ro(zv->zv_disk, !readonly);
1478 set_disk_ro(zv->zv_disk, readonly);
1479}
1480
a0bd735a
BP
1481
1482/*
1483 * Mask errors to continue dmu_objset_find() traversal
1484 */
1485static int
1486zvol_create_snap_minor_cb(const char *dsname, void *arg)
1487{
1488 const char *name = (const char *)arg;
1489
1ee159f4
BP
1490 ASSERT0(MUTEX_HELD(&spa_namespace_lock));
1491
a0bd735a
BP
1492 /* skip the designated dataset */
1493 if (name && strcmp(dsname, name) == 0)
1494 return (0);
1495
1496 /* at this point, the dsname should name a snapshot */
1497 if (strchr(dsname, '@') == 0) {
1498 dprintf("zvol_create_snap_minor_cb(): "
1499 "%s is not a shapshot name\n", dsname);
1500 } else {
1501 (void) zvol_create_minor_impl(dsname);
1502 }
1503
1504 return (0);
1505}
1506
1507/*
1508 * Mask errors to continue dmu_objset_find() traversal
1509 */
60101509 1510static int
13fe0198 1511zvol_create_minors_cb(const char *dsname, void *arg)
60101509 1512{
a0bd735a
BP
1513 uint64_t snapdev;
1514 int error;
1515
1ee159f4
BP
1516 ASSERT0(MUTEX_HELD(&spa_namespace_lock));
1517
a0bd735a
BP
1518 error = dsl_prop_get_integer(dsname, "snapdev", &snapdev, NULL);
1519 if (error)
1520 return (0);
1521
1522 /*
1523 * Given the name and the 'snapdev' property, create device minor nodes
1524 * with the linkages to zvols/snapshots as needed.
1525 * If the name represents a zvol, create a minor node for the zvol, then
1526 * check if its snapshots are 'visible', and if so, iterate over the
1527 * snapshots and create device minor nodes for those.
1528 */
1529 if (strchr(dsname, '@') == 0) {
1530 /* create minor for the 'dsname' explicitly */
1531 error = zvol_create_minor_impl(dsname);
1532 if ((error == 0 || error == EEXIST) &&
1533 (snapdev == ZFS_SNAPDEV_VISIBLE)) {
1534 fstrans_cookie_t cookie = spl_fstrans_mark();
1535 /*
1536 * traverse snapshots only, do not traverse children,
1537 * and skip the 'dsname'
1538 */
1539 error = dmu_objset_find((char *)dsname,
1540 zvol_create_snap_minor_cb, (void *)dsname,
1541 DS_FIND_SNAPSHOTS);
1542 spl_fstrans_unmark(cookie);
1543 }
1544 } else {
1545 dprintf("zvol_create_minors_cb(): %s is not a zvol name\n",
1546 dsname);
1547 }
60101509 1548
d5674448 1549 return (0);
60101509
BB
1550}
1551
1552/*
a0bd735a
BP
1553 * Create minors for the specified dataset, including children and snapshots.
1554 * Pay attention to the 'snapdev' property and iterate over the snapshots
1555 * only if they are 'visible'. This approach allows one to assure that the
1556 * snapshot metadata is read from disk only if it is needed.
1557 *
1558 * The name can represent a dataset to be recursively scanned for zvols and
1559 * their snapshots, or a single zvol snapshot. If the name represents a
1560 * dataset, the scan is performed in two nested stages:
1561 * - scan the dataset for zvols, and
1562 * - for each zvol, create a minor node, then check if the zvol's snapshots
1563 * are 'visible', and only then iterate over the snapshots if needed
1564 *
1565 * If the name represents a snapshot, a check is perfromed if the snapshot is
1566 * 'visible' (which also verifies that the parent is a zvol), and if so,
1567 * a minor node for that snapshot is created.
60101509 1568 */
a0bd735a
BP
1569static int
1570zvol_create_minors_impl(const char *name)
60101509 1571{
60101509 1572 int error = 0;
5428dc51 1573 fstrans_cookie_t cookie;
a0bd735a 1574 char *atp, *parent;
60101509 1575
5428dc51
BP
1576 if (zvol_inhibit_dev)
1577 return (0);
1578
a0bd735a
BP
1579 parent = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1580 (void) strlcpy(parent, name, MAXPATHLEN);
1581
1582 if ((atp = strrchr(parent, '@')) != NULL) {
1583 uint64_t snapdev;
1584
1585 *atp = '\0';
1586 error = dsl_prop_get_integer(parent, "snapdev",
1587 &snapdev, NULL);
1588
1589 if (error == 0 && snapdev == ZFS_SNAPDEV_VISIBLE)
1590 error = zvol_create_minor_impl(name);
1591 } else {
1592 cookie = spl_fstrans_mark();
1593 error = dmu_objset_find(parent, zvol_create_minors_cb,
1594 NULL, DS_FIND_CHILDREN);
1595 spl_fstrans_unmark(cookie);
1596 }
1597
1598 kmem_free(parent, MAXPATHLEN);
ba6a2402
BB
1599
1600 return (SET_ERROR(error));
1601}
1602
1603/*
1604 * Remove minors for specified dataset including children and snapshots.
1605 */
a0bd735a
BP
1606static void
1607zvol_remove_minors_impl(const char *name)
ba6a2402
BB
1608{
1609 zvol_state_t *zv, *zv_next;
1610 int namelen = ((name) ? strlen(name) : 0);
1611
74497b7a 1612 if (zvol_inhibit_dev)
ba6a2402 1613 return;
74497b7a 1614
60101509 1615 mutex_enter(&zvol_state_lock);
ba6a2402
BB
1616
1617 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1618 zv_next = list_next(&zvol_state_list, zv);
1619
1620 if (name == NULL || strcmp(zv->zv_name, name) == 0 ||
1621 (strncmp(zv->zv_name, name, namelen) == 0 &&
5428dc51
BP
1622 (zv->zv_name[namelen] == '/' ||
1623 zv->zv_name[namelen] == '@'))) {
1624
1625 /* If in use, leave alone */
1626 if (zv->zv_open_count > 0)
1627 continue;
1628
ba6a2402
BB
1629 zvol_remove(zv);
1630 zvol_free(zv);
60101509 1631 }
60101509 1632 }
60101509 1633
ba6a2402 1634 mutex_exit(&zvol_state_lock);
60101509
BB
1635}
1636
a0bd735a
BP
1637/* Remove minor for this specific snapshot only */
1638static void
1639zvol_remove_minor_impl(const char *name)
1640{
1641 zvol_state_t *zv, *zv_next;
1642
1643 if (zvol_inhibit_dev)
1644 return;
1645
1646 if (strchr(name, '@') == NULL)
1647 return;
1648
1649 mutex_enter(&zvol_state_lock);
1650
1651 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1652 zv_next = list_next(&zvol_state_list, zv);
1653
1654 if (strcmp(zv->zv_name, name) == 0) {
1655 /* If in use, leave alone */
1656 if (zv->zv_open_count > 0)
1657 continue;
1658 zvol_remove(zv);
1659 zvol_free(zv);
1660 break;
1661 }
1662 }
1663
1664 mutex_exit(&zvol_state_lock);
1665}
1666
60101509 1667/*
ba6a2402 1668 * Rename minors for specified dataset including children and snapshots.
60101509 1669 */
a0bd735a
BP
1670static void
1671zvol_rename_minors_impl(const char *oldname, const char *newname)
60101509
BB
1672{
1673 zvol_state_t *zv, *zv_next;
ba6a2402
BB
1674 int oldnamelen, newnamelen;
1675 char *name;
60101509 1676
74497b7a
DH
1677 if (zvol_inhibit_dev)
1678 return;
1679
ba6a2402
BB
1680 oldnamelen = strlen(oldname);
1681 newnamelen = strlen(newname);
79c76d5b 1682 name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
60101509
BB
1683
1684 mutex_enter(&zvol_state_lock);
ba6a2402 1685
60101509
BB
1686 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1687 zv_next = list_next(&zvol_state_list, zv);
1688
5428dc51
BP
1689 /* If in use, leave alone */
1690 if (zv->zv_open_count > 0)
1691 continue;
1692
ba6a2402 1693 if (strcmp(zv->zv_name, oldname) == 0) {
a0bd735a 1694 zvol_rename_minor(zv, newname);
ba6a2402
BB
1695 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 &&
1696 (zv->zv_name[oldnamelen] == '/' ||
1697 zv->zv_name[oldnamelen] == '@')) {
1698 snprintf(name, MAXNAMELEN, "%s%c%s", newname,
1699 zv->zv_name[oldnamelen],
1700 zv->zv_name + oldnamelen + 1);
a0bd735a 1701 zvol_rename_minor(zv, name);
60101509
BB
1702 }
1703 }
ba6a2402 1704
60101509 1705 mutex_exit(&zvol_state_lock);
ba6a2402
BB
1706
1707 kmem_free(name, MAXNAMELEN);
60101509
BB
1708}
1709
a0bd735a
BP
1710typedef struct zvol_snapdev_cb_arg {
1711 uint64_t snapdev;
1712} zvol_snapdev_cb_arg_t;
1713
0b4d1b58 1714static int
a0bd735a
BP
1715zvol_set_snapdev_cb(const char *dsname, void *param) {
1716 zvol_snapdev_cb_arg_t *arg = param;
0b4d1b58
ED
1717
1718 if (strchr(dsname, '@') == NULL)
ba6a2402 1719 return (0);
0b4d1b58 1720
a0bd735a 1721 switch (arg->snapdev) {
0b4d1b58 1722 case ZFS_SNAPDEV_VISIBLE:
a0bd735a 1723 (void) zvol_create_minor_impl(dsname);
0b4d1b58
ED
1724 break;
1725 case ZFS_SNAPDEV_HIDDEN:
a0bd735a 1726 (void) zvol_remove_minor_impl(dsname);
0b4d1b58
ED
1727 break;
1728 }
ba6a2402
BB
1729
1730 return (0);
0b4d1b58
ED
1731}
1732
a0bd735a
BP
1733static void
1734zvol_set_snapdev_impl(char *name, uint64_t snapdev)
1735{
1736 zvol_snapdev_cb_arg_t arg = {snapdev};
1737 fstrans_cookie_t cookie = spl_fstrans_mark();
1738 /*
1739 * The zvol_set_snapdev_sync() sets snapdev appropriately
1740 * in the dataset hierarchy. Here, we only scan snapshots.
1741 */
1742 dmu_objset_find(name, zvol_set_snapdev_cb, &arg, DS_FIND_SNAPSHOTS);
1743 spl_fstrans_unmark(cookie);
1744}
1745
1746static zvol_task_t *
1747zvol_task_alloc(zvol_async_op_t op, const char *name1, const char *name2,
1748 uint64_t snapdev)
1749{
1750 zvol_task_t *task;
1751 char *delim;
1752
1753 /* Never allow tasks on hidden names. */
1754 if (name1[0] == '$')
1755 return (NULL);
1756
1757 task = kmem_zalloc(sizeof (zvol_task_t), KM_SLEEP);
1758 task->op = op;
1759 task->snapdev = snapdev;
1760 delim = strchr(name1, '/');
1761 strlcpy(task->pool, name1, delim ? (delim - name1 + 1) : MAXNAMELEN);
1762
1763 strlcpy(task->name1, name1, MAXNAMELEN);
1764 if (name2 != NULL)
1765 strlcpy(task->name2, name2, MAXNAMELEN);
1766
1767 return (task);
1768}
1769
1770static void
1771zvol_task_free(zvol_task_t *task)
1772{
1773 kmem_free(task, sizeof (zvol_task_t));
1774}
1775
1776/*
1777 * The worker thread function performed asynchronously.
1778 */
1779static void
1780zvol_task_cb(void *param)
1781{
1782 zvol_task_t *task = (zvol_task_t *)param;
1783
1784 switch (task->op) {
1785 case ZVOL_ASYNC_CREATE_MINORS:
1786 (void) zvol_create_minors_impl(task->name1);
1787 break;
1788 case ZVOL_ASYNC_REMOVE_MINORS:
1789 zvol_remove_minors_impl(task->name1);
1790 break;
1791 case ZVOL_ASYNC_RENAME_MINORS:
1792 zvol_rename_minors_impl(task->name1, task->name2);
1793 break;
1794 case ZVOL_ASYNC_SET_SNAPDEV:
1795 zvol_set_snapdev_impl(task->name1, task->snapdev);
1796 break;
1797 default:
1798 VERIFY(0);
1799 break;
1800 }
1801
1802 zvol_task_free(task);
1803}
1804
1805typedef struct zvol_set_snapdev_arg {
1806 const char *zsda_name;
1807 uint64_t zsda_value;
1808 zprop_source_t zsda_source;
1809 dmu_tx_t *zsda_tx;
1810} zvol_set_snapdev_arg_t;
1811
1812/*
1813 * Sanity check the dataset for safe use by the sync task. No additional
1814 * conditions are imposed.
1815 */
1816static int
1817zvol_set_snapdev_check(void *arg, dmu_tx_t *tx)
1818{
1819 zvol_set_snapdev_arg_t *zsda = arg;
1820 dsl_pool_t *dp = dmu_tx_pool(tx);
1821 dsl_dir_t *dd;
1822 int error;
1823
1824 error = dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL);
1825 if (error != 0)
1826 return (error);
1827
1828 dsl_dir_rele(dd, FTAG);
1829
1830 return (error);
1831}
1832
1833static int
1834zvol_set_snapdev_sync_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1835{
1836 zvol_set_snapdev_arg_t *zsda = arg;
1837 char dsname[MAXNAMELEN];
1838 zvol_task_t *task;
1839
1840 dsl_dataset_name(ds, dsname);
1841 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_SNAPDEV),
1842 zsda->zsda_source, sizeof (zsda->zsda_value), 1,
1843 &zsda->zsda_value, zsda->zsda_tx);
1844
1845 task = zvol_task_alloc(ZVOL_ASYNC_SET_SNAPDEV, dsname,
1846 NULL, zsda->zsda_value);
1847 if (task == NULL)
1848 return (0);
1849
1850 (void) taskq_dispatch(dp->dp_spa->spa_zvol_taskq, zvol_task_cb,
1851 task, TQ_SLEEP);
1852 return (0);
1853}
1854
1855/*
1856 * Traverse all child snapshot datasets and apply snapdev appropriately.
1857 */
1858static void
1859zvol_set_snapdev_sync(void *arg, dmu_tx_t *tx)
1860{
1861 zvol_set_snapdev_arg_t *zsda = arg;
1862 dsl_pool_t *dp = dmu_tx_pool(tx);
1863 dsl_dir_t *dd;
1864
1865 VERIFY0(dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL));
1866 zsda->zsda_tx = tx;
1867
1868 dmu_objset_find_dp(dp, dd->dd_object, zvol_set_snapdev_sync_cb,
1869 zsda, DS_FIND_CHILDREN);
1870
1871 dsl_dir_rele(dd, FTAG);
1872}
1873
0b4d1b58 1874int
a0bd735a
BP
1875zvol_set_snapdev(const char *ddname, zprop_source_t source, uint64_t snapdev)
1876{
1877 zvol_set_snapdev_arg_t zsda;
5428dc51 1878
a0bd735a
BP
1879 zsda.zsda_name = ddname;
1880 zsda.zsda_source = source;
1881 zsda.zsda_value = snapdev;
5428dc51 1882
a0bd735a
BP
1883 return (dsl_sync_task(ddname, zvol_set_snapdev_check,
1884 zvol_set_snapdev_sync, &zsda, 0, ZFS_SPACE_CHECK_NONE));
1885}
1886
1887void
1888zvol_create_minors(spa_t *spa, const char *name, boolean_t async)
1889{
1890 zvol_task_t *task;
1891 taskqid_t id;
1892
1893 task = zvol_task_alloc(ZVOL_ASYNC_CREATE_MINORS, name, NULL, ~0ULL);
1894 if (task == NULL)
1895 return;
1896
1897 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
48d3eb40 1898 if ((async == B_FALSE) && (id != TASKQID_INVALID))
a0bd735a
BP
1899 taskq_wait_id(spa->spa_zvol_taskq, id);
1900}
1901
1902void
1903zvol_remove_minors(spa_t *spa, const char *name, boolean_t async)
1904{
1905 zvol_task_t *task;
1906 taskqid_t id;
1907
1908 task = zvol_task_alloc(ZVOL_ASYNC_REMOVE_MINORS, name, NULL, ~0ULL);
1909 if (task == NULL)
1910 return;
5428dc51 1911
a0bd735a 1912 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
48d3eb40 1913 if ((async == B_FALSE) && (id != TASKQID_INVALID))
a0bd735a
BP
1914 taskq_wait_id(spa->spa_zvol_taskq, id);
1915}
1916
1917void
1918zvol_rename_minors(spa_t *spa, const char *name1, const char *name2,
1919 boolean_t async)
1920{
1921 zvol_task_t *task;
1922 taskqid_t id;
1923
1924 task = zvol_task_alloc(ZVOL_ASYNC_RENAME_MINORS, name1, name2, ~0ULL);
1925 if (task == NULL)
1926 return;
1927
1928 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
48d3eb40 1929 if ((async == B_FALSE) && (id != TASKQID_INVALID))
a0bd735a 1930 taskq_wait_id(spa->spa_zvol_taskq, id);
0b4d1b58
ED
1931}
1932
60101509
BB
1933int
1934zvol_init(void)
1935{
1936 int error;
1937
2a3871d4 1938 list_create(&zvol_state_list, sizeof (zvol_state_t),
ce37ebd2 1939 offsetof(zvol_state_t, zv_next));
2a3871d4
RY
1940 mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1941
60101509
BB
1942 error = register_blkdev(zvol_major, ZVOL_DRIVER);
1943 if (error) {
1944 printk(KERN_INFO "ZFS: register_blkdev() failed %d\n", error);
37f9dac5 1945 goto out;
60101509
BB
1946 }
1947
1948 blk_register_region(MKDEV(zvol_major, 0), 1UL << MINORBITS,
ce37ebd2 1949 THIS_MODULE, zvol_probe, NULL, NULL);
60101509 1950
60101509 1951 return (0);
2a3871d4 1952
37f9dac5 1953out:
2a3871d4
RY
1954 mutex_destroy(&zvol_state_lock);
1955 list_destroy(&zvol_state_list);
1956
ce37ebd2 1957 return (SET_ERROR(error));
60101509
BB
1958}
1959
1960void
1961zvol_fini(void)
1962{
a0bd735a
BP
1963 zvol_remove_minors_impl(NULL);
1964
60101509
BB
1965 blk_unregister_region(MKDEV(zvol_major, 0), 1UL << MINORBITS);
1966 unregister_blkdev(zvol_major, ZVOL_DRIVER);
a0bd735a 1967
60101509 1968 list_destroy(&zvol_state_list);
a0bd735a 1969 mutex_destroy(&zvol_state_lock);
60101509
BB
1970}
1971
74497b7a
DH
1972module_param(zvol_inhibit_dev, uint, 0644);
1973MODULE_PARM_DESC(zvol_inhibit_dev, "Do not create zvol device nodes");
1974
30a9524e 1975module_param(zvol_major, uint, 0444);
60101509
BB
1976MODULE_PARM_DESC(zvol_major, "Major number for zvol device");
1977
7c0e5708 1978module_param(zvol_max_discard_blocks, ulong, 0444);
ce37ebd2 1979MODULE_PARM_DESC(zvol_max_discard_blocks, "Max number of blocks to discard");
9965059a
BB
1980
1981module_param(zvol_prefetch_bytes, uint, 0644);
1982MODULE_PARM_DESC(zvol_prefetch_bytes, "Prefetch N bytes at zvol start+end");