]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zvol.c
OpenZFS 2605, 6980, 6902
[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
177 bdev = lookup_bdev(device);
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
284 if (volsize - 1 > 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 /*
fa565676
RY
710 * Align the request to volume block boundaries when REQ_SECURE is
711 * available, but not requested. If we don't, then this will force
712 * dnode_free_range() to zero out the unaligned parts, which is slow
713 * (read-modify-write) and useless since we are not freeing any space
714 * by doing so. Kernels that do not support REQ_SECURE (2.6.32 through
715 * 2.6.35) will not receive this optimization.
089fa91b 716 */
fa565676
RY
717#ifdef REQ_SECURE
718 if (!(bio->bi_rw & REQ_SECURE)) {
719 start = P2ROUNDUP(start, zv->zv_volblocksize);
720 end = P2ALIGN(end, zv->zv_volblocksize);
f52ebcb3 721 size = end - start;
fa565676
RY
722 }
723#endif
089fa91b 724
37f9dac5
RY
725 if (start >= end)
726 return (0);
30930fba 727
d88895a0 728 rl = zfs_range_lock(&zv->zv_range_lock, start, size, RL_WRITER);
460a0213
DM
729 tx = dmu_tx_create(zv->zv_objset);
730 dmu_tx_mark_netfree(tx);
731 error = dmu_tx_assign(tx, TXG_WAIT);
732 if (error != 0) {
733 dmu_tx_abort(tx);
734 } else {
735 zvol_log_truncate(zv, tx, start, size, B_TRUE);
736 dmu_tx_commit(tx);
737 error = dmu_free_long_range(zv->zv_objset,
738 ZVOL_OBJ, start, size);
739 }
30930fba
ED
740
741 zfs_range_unlock(rl);
37f9dac5
RY
742
743 return (error);
30930fba 744}
30930fba 745
37f9dac5 746static int
a765a34a 747zvol_read(zvol_state_t *zv, uio_t *uio)
60101509 748{
a765a34a 749 uint64_t volsize = zv->zv_volsize;
60101509 750 rl_t *rl;
a765a34a 751 int error = 0;
b18019d2 752
5428dc51
BP
753 ASSERT(zv && zv->zv_open_count > 0);
754
d88895a0
CC
755 rl = zfs_range_lock(&zv->zv_range_lock, uio->uio_loffset,
756 uio->uio_resid, RL_READER);
a765a34a
RY
757 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
758 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
60101509 759
a765a34a
RY
760 /* don't read past the end */
761 if (bytes > volsize - uio->uio_loffset)
762 bytes = volsize - uio->uio_loffset;
60101509 763
19a47cb1 764 error = dmu_read_uio_dbuf(zv->zv_dbuf, uio, bytes);
a765a34a
RY
765 if (error) {
766 /* convert checksum errors into IO errors */
767 if (error == ECKSUM)
768 error = SET_ERROR(EIO);
769 break;
770 }
771 }
60101509 772 zfs_range_unlock(rl);
37f9dac5 773 return (error);
60101509
BB
774}
775
37f9dac5
RY
776static MAKE_REQUEST_FN_RET
777zvol_request(struct request_queue *q, struct bio *bio)
60101509 778{
a765a34a 779 uio_t uio;
60101509 780 zvol_state_t *zv = q->queuedata;
37f9dac5 781 fstrans_cookie_t cookie = spl_fstrans_mark();
8198d18c
RY
782 int rw = bio_data_dir(bio);
783#ifdef HAVE_GENERIC_IO_ACCT
784 unsigned long start = jiffies;
785#endif
37f9dac5 786 int error = 0;
60101509 787
a765a34a
RY
788 uio.uio_bvec = &bio->bi_io_vec[BIO_BI_IDX(bio)];
789 uio.uio_skip = BIO_BI_SKIP(bio);
790 uio.uio_resid = BIO_BI_SIZE(bio);
791 uio.uio_iovcnt = bio->bi_vcnt - BIO_BI_IDX(bio);
792 uio.uio_loffset = BIO_BI_SECTOR(bio) << 9;
793 uio.uio_limit = MAXOFFSET_T;
794 uio.uio_segflg = UIO_BVEC;
795
796 if (bio_has_data(bio) && uio.uio_loffset + uio.uio_resid >
797 zv->zv_volsize) {
37f9dac5 798 printk(KERN_INFO
a765a34a 799 "%s: bad access: offset=%llu, size=%lu\n",
37f9dac5 800 zv->zv_disk->disk_name,
a765a34a
RY
801 (long long unsigned)uio.uio_loffset,
802 (long unsigned)uio.uio_resid);
37f9dac5 803 error = SET_ERROR(EIO);
8198d18c 804 goto out1;
37f9dac5
RY
805 }
806
a765a34a 807 generic_start_io_acct(rw, bio_sectors(bio), &zv->zv_disk->part0);
8198d18c
RY
808
809 if (rw == WRITE) {
37f9dac5
RY
810 if (unlikely(zv->zv_flags & ZVOL_RDONLY)) {
811 error = SET_ERROR(EROFS);
8198d18c 812 goto out2;
60101509
BB
813 }
814
37f9dac5
RY
815 if (bio->bi_rw & VDEV_REQ_DISCARD) {
816 error = zvol_discard(bio);
8198d18c 817 goto out2;
37f9dac5 818 }
60101509 819
a765a34a
RY
820 /*
821 * Some requests are just for flush and nothing else.
822 */
823 if (uio.uio_resid == 0) {
824 if (bio->bi_rw & VDEV_REQ_FLUSH)
825 zil_commit(zv->zv_zilog, ZVOL_OBJ);
826 goto out2;
827 }
828
829 error = zvol_write(zv, &uio,
830 ((bio->bi_rw & (VDEV_REQ_FUA|VDEV_REQ_FLUSH)) ||
831 zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS));
37f9dac5 832 } else
a765a34a 833 error = zvol_read(zv, &uio);
30930fba 834
8198d18c
RY
835out2:
836 generic_end_io_acct(rw, &zv->zv_disk->part0, start);
837out1:
784a7fe5 838 BIO_END_IO(bio, -error);
37f9dac5
RY
839 spl_fstrans_unmark(cookie);
840#ifdef HAVE_MAKE_REQUEST_FN_RET_INT
841 return (0);
1a093716
CC
842#elif defined(HAVE_MAKE_REQUEST_FN_RET_QC)
843 return (BLK_QC_T_NONE);
37f9dac5 844#endif
60101509
BB
845}
846
847static void
848zvol_get_done(zgd_t *zgd, int error)
849{
850 if (zgd->zgd_db)
851 dmu_buf_rele(zgd->zgd_db, zgd);
852
853 zfs_range_unlock(zgd->zgd_rl);
854
855 if (error == 0 && zgd->zgd_bp)
856 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
857
858 kmem_free(zgd, sizeof (zgd_t));
859}
860
861/*
862 * Get data to generate a TX_WRITE intent log record.
863 */
864static int
865zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
866{
867 zvol_state_t *zv = arg;
868 objset_t *os = zv->zv_objset;
03c6040b 869 uint64_t object = ZVOL_OBJ;
60101509
BB
870 uint64_t offset = lr->lr_offset;
871 uint64_t size = lr->lr_length;
03c6040b 872 blkptr_t *bp = &lr->lr_blkptr;
60101509
BB
873 dmu_buf_t *db;
874 zgd_t *zgd;
875 int error;
876
877 ASSERT(zio != NULL);
878 ASSERT(size != 0);
879
79c76d5b 880 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
60101509 881 zgd->zgd_zilog = zv->zv_zilog;
d88895a0
CC
882 zgd->zgd_rl = zfs_range_lock(&zv->zv_range_lock, offset, size,
883 RL_READER);
60101509
BB
884
885 /*
886 * Write records come in two flavors: immediate and indirect.
887 * For small writes it's cheaper to store the data with the
888 * log record (immediate); for large writes it's cheaper to
889 * sync the data and get a pointer to it (indirect) so that
890 * we don't have to write the data twice.
891 */
892 if (buf != NULL) { /* immediate write */
03c6040b 893 error = dmu_read(os, object, offset, size, buf,
60101509
BB
894 DMU_READ_NO_PREFETCH);
895 } else {
896 size = zv->zv_volblocksize;
897 offset = P2ALIGN_TYPED(offset, size, uint64_t);
03c6040b 898 error = dmu_buf_hold(os, object, offset, zgd, &db,
60101509
BB
899 DMU_READ_NO_PREFETCH);
900 if (error == 0) {
03c6040b
GW
901 blkptr_t *obp = dmu_buf_get_blkptr(db);
902 if (obp) {
903 ASSERT(BP_IS_HOLE(bp));
904 *bp = *obp;
905 }
906
60101509
BB
907 zgd->zgd_db = db;
908 zgd->zgd_bp = &lr->lr_blkptr;
909
910 ASSERT(db != NULL);
911 ASSERT(db->db_offset == offset);
912 ASSERT(db->db_size == size);
913
914 error = dmu_sync(zio, lr->lr_common.lrc_txg,
915 zvol_get_done, zgd);
916
917 if (error == 0)
918 return (0);
919 }
920 }
921
922 zvol_get_done(zgd, error);
923
ce37ebd2 924 return (SET_ERROR(error));
60101509
BB
925}
926
927/*
928 * The zvol_state_t's are inserted in increasing MINOR(dev_t) order.
929 */
930static void
931zvol_insert(zvol_state_t *zv_insert)
932{
933 zvol_state_t *zv = NULL;
934
935 ASSERT(MUTEX_HELD(&zvol_state_lock));
936 ASSERT3U(MINOR(zv_insert->zv_dev) & ZVOL_MINOR_MASK, ==, 0);
937 for (zv = list_head(&zvol_state_list); zv != NULL;
ce37ebd2 938 zv = list_next(&zvol_state_list, zv)) {
60101509
BB
939 if (MINOR(zv->zv_dev) > MINOR(zv_insert->zv_dev))
940 break;
941 }
942
943 list_insert_before(&zvol_state_list, zv, zv_insert);
944}
945
946/*
947 * Simply remove the zvol from to list of zvols.
948 */
949static void
950zvol_remove(zvol_state_t *zv_remove)
951{
952 ASSERT(MUTEX_HELD(&zvol_state_lock));
953 list_remove(&zvol_state_list, zv_remove);
954}
955
956static int
957zvol_first_open(zvol_state_t *zv)
958{
959 objset_t *os;
960 uint64_t volsize;
961 int error;
962 uint64_t ro;
963
964 /* lie and say we're read-only */
965 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, 1, zvol_tag, &os);
966 if (error)
1ee159f4
BP
967 return (SET_ERROR(-error));
968
969 zv->zv_objset = os;
970
971 error = dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL);
972 if (error)
973 goto out_owned;
60101509
BB
974
975 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
1ee159f4
BP
976 if (error)
977 goto out_owned;
60101509 978
60101509 979 error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
1ee159f4
BP
980 if (error)
981 goto out_owned;
60101509
BB
982
983 set_capacity(zv->zv_disk, volsize >> 9);
984 zv->zv_volsize = volsize;
985 zv->zv_zilog = zil_open(os, zvol_get_data);
986
a4430fce
GW
987 if (ro || dmu_objset_is_snapshot(os) ||
988 !spa_writeable(dmu_objset_spa(os))) {
babf3f9b
MM
989 set_disk_ro(zv->zv_disk, 1);
990 zv->zv_flags |= ZVOL_RDONLY;
60101509 991 } else {
babf3f9b
MM
992 set_disk_ro(zv->zv_disk, 0);
993 zv->zv_flags &= ~ZVOL_RDONLY;
60101509
BB
994 }
995
1ee159f4
BP
996out_owned:
997 if (error) {
998 dmu_objset_disown(os, zvol_tag);
999 zv->zv_objset = NULL;
1000 }
babf3f9b 1001
ce37ebd2 1002 return (SET_ERROR(-error));
60101509
BB
1003}
1004
1005static void
1006zvol_last_close(zvol_state_t *zv)
1007{
1008 zil_close(zv->zv_zilog);
1009 zv->zv_zilog = NULL;
04434775 1010
60101509
BB
1011 dmu_buf_rele(zv->zv_dbuf, zvol_tag);
1012 zv->zv_dbuf = NULL;
04434775
MA
1013
1014 /*
1015 * Evict cached data
1016 */
1017 if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) &&
1018 !(zv->zv_flags & ZVOL_RDONLY))
1019 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1020 (void) dmu_objset_evict_dbufs(zv->zv_objset);
1021
60101509
BB
1022 dmu_objset_disown(zv->zv_objset, zvol_tag);
1023 zv->zv_objset = NULL;
1024}
1025
1026static int
1027zvol_open(struct block_device *bdev, fmode_t flag)
1028{
5428dc51 1029 zvol_state_t *zv;
60101509
BB
1030 int error = 0, drop_mutex = 0;
1031
1032 /*
1033 * If the caller is already holding the mutex do not take it
a0bd735a 1034 * again, this will happen as part of zvol_create_minor_impl().
60101509
BB
1035 * Once add_disk() is called the device is live and the kernel
1036 * will attempt to open it to read the partition information.
1037 */
1038 if (!mutex_owned(&zvol_state_lock)) {
1039 mutex_enter(&zvol_state_lock);
1040 drop_mutex = 1;
1041 }
1042
5428dc51
BP
1043 /*
1044 * Obtain a copy of private_data under the lock to make sure
1045 * that either the result of zvol_freeg() setting
1046 * bdev->bd_disk->private_data to NULL is observed, or zvol_free()
1047 * is not called on this zv because of the positive zv_open_count.
1048 */
1049 zv = bdev->bd_disk->private_data;
1050 if (zv == NULL) {
1051 error = -ENXIO;
1052 goto out_mutex;
1053 }
60101509
BB
1054
1055 if (zv->zv_open_count == 0) {
1056 error = zvol_first_open(zv);
1057 if (error)
1058 goto out_mutex;
1059 }
1060
ba6a2402 1061 if ((flag & FMODE_WRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
60101509
BB
1062 error = -EROFS;
1063 goto out_open_count;
1064 }
1065
1066 zv->zv_open_count++;
1067
5428dc51
BP
1068 check_disk_change(bdev);
1069
60101509
BB
1070out_open_count:
1071 if (zv->zv_open_count == 0)
1072 zvol_last_close(zv);
1073
1074out_mutex:
1075 if (drop_mutex)
1076 mutex_exit(&zvol_state_lock);
1077
ce37ebd2 1078 return (SET_ERROR(error));
60101509
BB
1079}
1080
a1d9543a
CD
1081#ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1082static void
1083#else
60101509 1084static int
a1d9543a 1085#endif
60101509
BB
1086zvol_release(struct gendisk *disk, fmode_t mode)
1087{
1088 zvol_state_t *zv = disk->private_data;
1089 int drop_mutex = 0;
1090
5428dc51
BP
1091 ASSERT(zv && zv->zv_open_count > 0);
1092
60101509
BB
1093 if (!mutex_owned(&zvol_state_lock)) {
1094 mutex_enter(&zvol_state_lock);
1095 drop_mutex = 1;
1096 }
1097
5428dc51
BP
1098 zv->zv_open_count--;
1099 if (zv->zv_open_count == 0)
1100 zvol_last_close(zv);
60101509
BB
1101
1102 if (drop_mutex)
1103 mutex_exit(&zvol_state_lock);
1104
a1d9543a 1105#ifndef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
60101509 1106 return (0);
a1d9543a 1107#endif
60101509
BB
1108}
1109
1110static int
1111zvol_ioctl(struct block_device *bdev, fmode_t mode,
ce37ebd2 1112 unsigned int cmd, unsigned long arg)
60101509
BB
1113{
1114 zvol_state_t *zv = bdev->bd_disk->private_data;
1115 int error = 0;
1116
5428dc51 1117 ASSERT(zv && zv->zv_open_count > 0);
60101509
BB
1118
1119 switch (cmd) {
1120 case BLKFLSBUF:
1121 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1122 break;
4c0d8e50
FN
1123 case BLKZNAME:
1124 error = copy_to_user((void *)arg, zv->zv_name, MAXNAMELEN);
1125 break;
60101509
BB
1126
1127 default:
1128 error = -ENOTTY;
1129 break;
1130
1131 }
1132
ce37ebd2 1133 return (SET_ERROR(error));
60101509
BB
1134}
1135
1136#ifdef CONFIG_COMPAT
1137static int
1138zvol_compat_ioctl(struct block_device *bdev, fmode_t mode,
ce37ebd2 1139 unsigned cmd, unsigned long arg)
60101509 1140{
ce37ebd2 1141 return (zvol_ioctl(bdev, mode, cmd, arg));
60101509
BB
1142}
1143#else
ce37ebd2 1144#define zvol_compat_ioctl NULL
60101509
BB
1145#endif
1146
1147static int zvol_media_changed(struct gendisk *disk)
1148{
1149 zvol_state_t *zv = disk->private_data;
1150
5428dc51
BP
1151 ASSERT(zv && zv->zv_open_count > 0);
1152
ce37ebd2 1153 return (zv->zv_changed);
60101509
BB
1154}
1155
1156static int zvol_revalidate_disk(struct gendisk *disk)
1157{
1158 zvol_state_t *zv = disk->private_data;
1159
5428dc51
BP
1160 ASSERT(zv && zv->zv_open_count > 0);
1161
60101509
BB
1162 zv->zv_changed = 0;
1163 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1164
ce37ebd2 1165 return (0);
60101509
BB
1166}
1167
1168/*
1169 * Provide a simple virtual geometry for legacy compatibility. For devices
1170 * smaller than 1 MiB a small head and sector count is used to allow very
1171 * tiny devices. For devices over 1 Mib a standard head and sector count
1172 * is used to keep the cylinders count reasonable.
1173 */
1174static int
1175zvol_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1176{
1177 zvol_state_t *zv = bdev->bd_disk->private_data;
5428dc51
BP
1178 sector_t sectors;
1179
1180 ASSERT(zv && zv->zv_open_count > 0);
1181
1182 sectors = get_capacity(zv->zv_disk);
60101509
BB
1183
1184 if (sectors > 2048) {
1185 geo->heads = 16;
1186 geo->sectors = 63;
1187 } else {
1188 geo->heads = 2;
1189 geo->sectors = 4;
1190 }
1191
1192 geo->start = 0;
1193 geo->cylinders = sectors / (geo->heads * geo->sectors);
1194
ce37ebd2 1195 return (0);
60101509
BB
1196}
1197
1198static struct kobject *
1199zvol_probe(dev_t dev, int *part, void *arg)
1200{
1201 zvol_state_t *zv;
1202 struct kobject *kobj;
1203
1204 mutex_enter(&zvol_state_lock);
1205 zv = zvol_find_by_dev(dev);
23a61ccc 1206 kobj = zv ? get_disk(zv->zv_disk) : NULL;
60101509
BB
1207 mutex_exit(&zvol_state_lock);
1208
ce37ebd2 1209 return (kobj);
60101509
BB
1210}
1211
1212#ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS
1213static struct block_device_operations zvol_ops = {
ce37ebd2
BB
1214 .open = zvol_open,
1215 .release = zvol_release,
1216 .ioctl = zvol_ioctl,
1217 .compat_ioctl = zvol_compat_ioctl,
1218 .media_changed = zvol_media_changed,
1219 .revalidate_disk = zvol_revalidate_disk,
1220 .getgeo = zvol_getgeo,
1221 .owner = THIS_MODULE,
60101509
BB
1222};
1223
1224#else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1225
1226static int
1227zvol_open_by_inode(struct inode *inode, struct file *file)
1228{
ce37ebd2 1229 return (zvol_open(inode->i_bdev, file->f_mode));
60101509
BB
1230}
1231
1232static int
1233zvol_release_by_inode(struct inode *inode, struct file *file)
1234{
ce37ebd2 1235 return (zvol_release(inode->i_bdev->bd_disk, file->f_mode));
60101509
BB
1236}
1237
1238static int
1239zvol_ioctl_by_inode(struct inode *inode, struct file *file,
ce37ebd2 1240 unsigned int cmd, unsigned long arg)
60101509 1241{
b1c58213 1242 if (file == NULL || inode == NULL)
ce37ebd2
BB
1243 return (SET_ERROR(-EINVAL));
1244
1245 return (zvol_ioctl(inode->i_bdev, file->f_mode, cmd, arg));
60101509
BB
1246}
1247
ce37ebd2 1248#ifdef CONFIG_COMPAT
60101509
BB
1249static long
1250zvol_compat_ioctl_by_inode(struct file *file,
ce37ebd2 1251 unsigned int cmd, unsigned long arg)
60101509 1252{
b1c58213 1253 if (file == NULL)
ce37ebd2
BB
1254 return (SET_ERROR(-EINVAL));
1255
1256 return (zvol_compat_ioctl(file->f_dentry->d_inode->i_bdev,
1257 file->f_mode, cmd, arg));
60101509 1258}
ce37ebd2
BB
1259#else
1260#define zvol_compat_ioctl_by_inode NULL
1261#endif
60101509
BB
1262
1263static struct block_device_operations zvol_ops = {
ce37ebd2
BB
1264 .open = zvol_open_by_inode,
1265 .release = zvol_release_by_inode,
1266 .ioctl = zvol_ioctl_by_inode,
1267 .compat_ioctl = zvol_compat_ioctl_by_inode,
1268 .media_changed = zvol_media_changed,
1269 .revalidate_disk = zvol_revalidate_disk,
1270 .getgeo = zvol_getgeo,
1271 .owner = THIS_MODULE,
60101509
BB
1272};
1273#endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1274
1275/*
1276 * Allocate memory for a new zvol_state_t and setup the required
1277 * request queue and generic disk structures for the block device.
1278 */
1279static zvol_state_t *
1280zvol_alloc(dev_t dev, const char *name)
1281{
1282 zvol_state_t *zv;
1283
79c76d5b 1284 zv = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
60101509 1285
2a3871d4
RY
1286 list_link_init(&zv->zv_next);
1287
37f9dac5 1288 zv->zv_queue = blk_alloc_queue(GFP_ATOMIC);
60101509
BB
1289 if (zv->zv_queue == NULL)
1290 goto out_kmem;
1291
37f9dac5 1292 blk_queue_make_request(zv->zv_queue, zvol_request);
7bd04f2d 1293
68e8f59a
CC
1294#ifdef HAVE_BLK_QUEUE_WRITE_CACHE
1295 blk_queue_write_cache(zv->zv_queue, B_TRUE, B_TRUE);
1296#elif defined(HAVE_BLK_QUEUE_FLUSH)
b18019d2
ED
1297 blk_queue_flush(zv->zv_queue, VDEV_REQ_FLUSH | VDEV_REQ_FUA);
1298#else
1299 blk_queue_ordered(zv->zv_queue, QUEUE_ORDERED_DRAIN, NULL);
1300#endif /* HAVE_BLK_QUEUE_FLUSH */
1301
60101509
BB
1302 zv->zv_disk = alloc_disk(ZVOL_MINORS);
1303 if (zv->zv_disk == NULL)
1304 goto out_queue;
1305
1306 zv->zv_queue->queuedata = zv;
1307 zv->zv_dev = dev;
1308 zv->zv_open_count = 0;
4c0d8e50 1309 strlcpy(zv->zv_name, name, MAXNAMELEN);
60101509 1310
d88895a0 1311 zfs_rlock_init(&zv->zv_range_lock);
3c4988c8 1312
60101509
BB
1313 zv->zv_disk->major = zvol_major;
1314 zv->zv_disk->first_minor = (dev & MINORMASK);
1315 zv->zv_disk->fops = &zvol_ops;
1316 zv->zv_disk->private_data = zv;
1317 zv->zv_disk->queue = zv->zv_queue;
4c0d8e50
FN
1318 snprintf(zv->zv_disk->disk_name, DISK_NAME_LEN, "%s%d",
1319 ZVOL_DEV_NAME, (dev & MINORMASK));
60101509 1320
ce37ebd2 1321 return (zv);
60101509
BB
1322
1323out_queue:
1324 blk_cleanup_queue(zv->zv_queue);
1325out_kmem:
1326 kmem_free(zv, sizeof (zvol_state_t));
0a6bef26 1327
ce37ebd2 1328 return (NULL);
60101509
BB
1329}
1330
1331/*
1332 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1333 */
1334static void
1335zvol_free(zvol_state_t *zv)
1336{
5428dc51
BP
1337 ASSERT(MUTEX_HELD(&zvol_state_lock));
1338 ASSERT(zv->zv_open_count == 0);
1339
d88895a0 1340 zfs_rlock_destroy(&zv->zv_range_lock);
60101509 1341
5428dc51
BP
1342 zv->zv_disk->private_data = NULL;
1343
60101509
BB
1344 del_gendisk(zv->zv_disk);
1345 blk_cleanup_queue(zv->zv_queue);
1346 put_disk(zv->zv_disk);
1347
1348 kmem_free(zv, sizeof (zvol_state_t));
1349}
1350
a0bd735a
BP
1351/*
1352 * Create a block device minor node and setup the linkage between it
1353 * and the specified volume. Once this function returns the block
1354 * device is live and ready for use.
1355 */
60101509 1356static int
a0bd735a 1357zvol_create_minor_impl(const char *name)
60101509
BB
1358{
1359 zvol_state_t *zv;
1360 objset_t *os;
1361 dmu_object_info_t *doi;
1362 uint64_t volsize;
9965059a 1363 uint64_t len;
60101509
BB
1364 unsigned minor = 0;
1365 int error = 0;
1366
a0bd735a 1367 mutex_enter(&zvol_state_lock);
60101509
BB
1368
1369 zv = zvol_find_by_name(name);
1370 if (zv) {
2e528b49 1371 error = SET_ERROR(EEXIST);
60101509
BB
1372 goto out;
1373 }
1374
79c76d5b 1375 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
60101509
BB
1376
1377 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
1378 if (error)
1379 goto out_doi;
1380
1381 error = dmu_object_info(os, ZVOL_OBJ, doi);
1382 if (error)
1383 goto out_dmu_objset_disown;
1384
1385 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
1386 if (error)
1387 goto out_dmu_objset_disown;
1388
1389 error = zvol_find_minor(&minor);
1390 if (error)
1391 goto out_dmu_objset_disown;
1392
1393 zv = zvol_alloc(MKDEV(zvol_major, minor), name);
1394 if (zv == NULL) {
2e528b49 1395 error = SET_ERROR(EAGAIN);
60101509
BB
1396 goto out_dmu_objset_disown;
1397 }
1398
1399 if (dmu_objset_is_snapshot(os))
1400 zv->zv_flags |= ZVOL_RDONLY;
1401
1402 zv->zv_volblocksize = doi->doi_data_block_size;
1403 zv->zv_volsize = volsize;
1404 zv->zv_objset = os;
1405
1406 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1407
c495fe2c 1408 blk_queue_max_hw_sectors(zv->zv_queue, (DMU_MAX_ACCESS / 4) >> 9);
34037afe
ED
1409 blk_queue_max_segments(zv->zv_queue, UINT16_MAX);
1410 blk_queue_max_segment_size(zv->zv_queue, UINT_MAX);
1411 blk_queue_physical_block_size(zv->zv_queue, zv->zv_volblocksize);
1412 blk_queue_io_opt(zv->zv_queue, zv->zv_volblocksize);
7c0e5708
ED
1413 blk_queue_max_discard_sectors(zv->zv_queue,
1414 (zvol_max_discard_blocks * zv->zv_volblocksize) >> 9);
ee5fd0bb 1415 blk_queue_discard_granularity(zv->zv_queue, zv->zv_volblocksize);
30930fba 1416 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zv->zv_queue);
37f9dac5 1417#ifdef QUEUE_FLAG_NONROT
34037afe
ED
1418 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zv->zv_queue);
1419#endif
c6a3a222
RY
1420#ifdef QUEUE_FLAG_ADD_RANDOM
1421 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, zv->zv_queue);
1422#endif
34037afe 1423
a4430fce
GW
1424 if (spa_writeable(dmu_objset_spa(os))) {
1425 if (zil_replay_disable)
1426 zil_destroy(dmu_objset_zil(os), B_FALSE);
1427 else
1428 zil_replay(os, zv, zvol_replay_vector);
1429 }
60101509 1430
9965059a
BB
1431 /*
1432 * When udev detects the addition of the device it will immediately
1433 * invoke blkid(8) to determine the type of content on the device.
1434 * Prefetching the blocks commonly scanned by blkid(8) will speed
1435 * up this process.
1436 */
1437 len = MIN(MAX(zvol_prefetch_bytes, 0), SPA_MAXBLOCKSIZE);
1438 if (len > 0) {
fcff0f35
PD
1439 dmu_prefetch(os, ZVOL_OBJ, 0, 0, len, ZIO_PRIORITY_SYNC_READ);
1440 dmu_prefetch(os, ZVOL_OBJ, 0, volsize - len, len,
1441 ZIO_PRIORITY_SYNC_READ);
9965059a
BB
1442 }
1443
f74a147c 1444 zv->zv_objset = NULL;
60101509
BB
1445out_dmu_objset_disown:
1446 dmu_objset_disown(os, zvol_tag);
60101509 1447out_doi:
ce37ebd2 1448 kmem_free(doi, sizeof (dmu_object_info_t));
60101509
BB
1449out:
1450
1451 if (error == 0) {
1452 zvol_insert(zv);
5428dc51
BP
1453 /*
1454 * Drop the lock to prevent deadlock with sys_open() ->
1455 * zvol_open(), which first takes bd_disk->bd_mutex and then
1456 * takes zvol_state_lock, whereas this code path first takes
1457 * zvol_state_lock, and then takes bd_disk->bd_mutex.
1458 */
1459 mutex_exit(&zvol_state_lock);
60101509 1460 add_disk(zv->zv_disk);
a0bd735a
BP
1461 } else {
1462 mutex_exit(&zvol_state_lock);
60101509
BB
1463 }
1464
ce37ebd2 1465 return (SET_ERROR(error));
60101509
BB
1466}
1467
ba6a2402
BB
1468/*
1469 * Rename a block device minor mode for the specified volume.
1470 */
1471static void
a0bd735a 1472zvol_rename_minor(zvol_state_t *zv, const char *newname)
ba6a2402
BB
1473{
1474 int readonly = get_disk_ro(zv->zv_disk);
1475
1476 ASSERT(MUTEX_HELD(&zvol_state_lock));
1477
1478 strlcpy(zv->zv_name, newname, sizeof (zv->zv_name));
1479
1480 /*
1481 * The block device's read-only state is briefly changed causing
1482 * a KOBJ_CHANGE uevent to be issued. This ensures udev detects
1483 * the name change and fixes the symlinks. This does not change
1484 * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
1485 * changes. This would normally be done using kobject_uevent() but
1486 * that is a GPL-only symbol which is why we need this workaround.
1487 */
1488 set_disk_ro(zv->zv_disk, !readonly);
1489 set_disk_ro(zv->zv_disk, readonly);
1490}
1491
a0bd735a
BP
1492
1493/*
1494 * Mask errors to continue dmu_objset_find() traversal
1495 */
1496static int
1497zvol_create_snap_minor_cb(const char *dsname, void *arg)
1498{
1499 const char *name = (const char *)arg;
1500
1ee159f4
BP
1501 ASSERT0(MUTEX_HELD(&spa_namespace_lock));
1502
a0bd735a
BP
1503 /* skip the designated dataset */
1504 if (name && strcmp(dsname, name) == 0)
1505 return (0);
1506
1507 /* at this point, the dsname should name a snapshot */
1508 if (strchr(dsname, '@') == 0) {
1509 dprintf("zvol_create_snap_minor_cb(): "
1510 "%s is not a shapshot name\n", dsname);
1511 } else {
1512 (void) zvol_create_minor_impl(dsname);
1513 }
1514
1515 return (0);
1516}
1517
1518/*
1519 * Mask errors to continue dmu_objset_find() traversal
1520 */
60101509 1521static int
13fe0198 1522zvol_create_minors_cb(const char *dsname, void *arg)
60101509 1523{
a0bd735a
BP
1524 uint64_t snapdev;
1525 int error;
1526
1ee159f4
BP
1527 ASSERT0(MUTEX_HELD(&spa_namespace_lock));
1528
a0bd735a
BP
1529 error = dsl_prop_get_integer(dsname, "snapdev", &snapdev, NULL);
1530 if (error)
1531 return (0);
1532
1533 /*
1534 * Given the name and the 'snapdev' property, create device minor nodes
1535 * with the linkages to zvols/snapshots as needed.
1536 * If the name represents a zvol, create a minor node for the zvol, then
1537 * check if its snapshots are 'visible', and if so, iterate over the
1538 * snapshots and create device minor nodes for those.
1539 */
1540 if (strchr(dsname, '@') == 0) {
1541 /* create minor for the 'dsname' explicitly */
1542 error = zvol_create_minor_impl(dsname);
1543 if ((error == 0 || error == EEXIST) &&
1544 (snapdev == ZFS_SNAPDEV_VISIBLE)) {
1545 fstrans_cookie_t cookie = spl_fstrans_mark();
1546 /*
1547 * traverse snapshots only, do not traverse children,
1548 * and skip the 'dsname'
1549 */
1550 error = dmu_objset_find((char *)dsname,
1551 zvol_create_snap_minor_cb, (void *)dsname,
1552 DS_FIND_SNAPSHOTS);
1553 spl_fstrans_unmark(cookie);
1554 }
1555 } else {
1556 dprintf("zvol_create_minors_cb(): %s is not a zvol name\n",
1557 dsname);
1558 }
60101509 1559
d5674448 1560 return (0);
60101509
BB
1561}
1562
1563/*
a0bd735a
BP
1564 * Create minors for the specified dataset, including children and snapshots.
1565 * Pay attention to the 'snapdev' property and iterate over the snapshots
1566 * only if they are 'visible'. This approach allows one to assure that the
1567 * snapshot metadata is read from disk only if it is needed.
1568 *
1569 * The name can represent a dataset to be recursively scanned for zvols and
1570 * their snapshots, or a single zvol snapshot. If the name represents a
1571 * dataset, the scan is performed in two nested stages:
1572 * - scan the dataset for zvols, and
1573 * - for each zvol, create a minor node, then check if the zvol's snapshots
1574 * are 'visible', and only then iterate over the snapshots if needed
1575 *
1576 * If the name represents a snapshot, a check is perfromed if the snapshot is
1577 * 'visible' (which also verifies that the parent is a zvol), and if so,
1578 * a minor node for that snapshot is created.
60101509 1579 */
a0bd735a
BP
1580static int
1581zvol_create_minors_impl(const char *name)
60101509 1582{
60101509 1583 int error = 0;
5428dc51 1584 fstrans_cookie_t cookie;
a0bd735a 1585 char *atp, *parent;
60101509 1586
5428dc51
BP
1587 if (zvol_inhibit_dev)
1588 return (0);
1589
a0bd735a
BP
1590 parent = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1591 (void) strlcpy(parent, name, MAXPATHLEN);
1592
1593 if ((atp = strrchr(parent, '@')) != NULL) {
1594 uint64_t snapdev;
1595
1596 *atp = '\0';
1597 error = dsl_prop_get_integer(parent, "snapdev",
1598 &snapdev, NULL);
1599
1600 if (error == 0 && snapdev == ZFS_SNAPDEV_VISIBLE)
1601 error = zvol_create_minor_impl(name);
1602 } else {
1603 cookie = spl_fstrans_mark();
1604 error = dmu_objset_find(parent, zvol_create_minors_cb,
1605 NULL, DS_FIND_CHILDREN);
1606 spl_fstrans_unmark(cookie);
1607 }
1608
1609 kmem_free(parent, MAXPATHLEN);
ba6a2402
BB
1610
1611 return (SET_ERROR(error));
1612}
1613
1614/*
1615 * Remove minors for specified dataset including children and snapshots.
1616 */
a0bd735a
BP
1617static void
1618zvol_remove_minors_impl(const char *name)
ba6a2402
BB
1619{
1620 zvol_state_t *zv, *zv_next;
1621 int namelen = ((name) ? strlen(name) : 0);
1622
74497b7a 1623 if (zvol_inhibit_dev)
ba6a2402 1624 return;
74497b7a 1625
60101509 1626 mutex_enter(&zvol_state_lock);
ba6a2402
BB
1627
1628 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1629 zv_next = list_next(&zvol_state_list, zv);
1630
1631 if (name == NULL || strcmp(zv->zv_name, name) == 0 ||
1632 (strncmp(zv->zv_name, name, namelen) == 0 &&
5428dc51
BP
1633 (zv->zv_name[namelen] == '/' ||
1634 zv->zv_name[namelen] == '@'))) {
1635
1636 /* If in use, leave alone */
1637 if (zv->zv_open_count > 0)
1638 continue;
1639
ba6a2402
BB
1640 zvol_remove(zv);
1641 zvol_free(zv);
60101509 1642 }
60101509 1643 }
60101509 1644
ba6a2402 1645 mutex_exit(&zvol_state_lock);
60101509
BB
1646}
1647
a0bd735a
BP
1648/* Remove minor for this specific snapshot only */
1649static void
1650zvol_remove_minor_impl(const char *name)
1651{
1652 zvol_state_t *zv, *zv_next;
1653
1654 if (zvol_inhibit_dev)
1655 return;
1656
1657 if (strchr(name, '@') == NULL)
1658 return;
1659
1660 mutex_enter(&zvol_state_lock);
1661
1662 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1663 zv_next = list_next(&zvol_state_list, zv);
1664
1665 if (strcmp(zv->zv_name, name) == 0) {
1666 /* If in use, leave alone */
1667 if (zv->zv_open_count > 0)
1668 continue;
1669 zvol_remove(zv);
1670 zvol_free(zv);
1671 break;
1672 }
1673 }
1674
1675 mutex_exit(&zvol_state_lock);
1676}
1677
60101509 1678/*
ba6a2402 1679 * Rename minors for specified dataset including children and snapshots.
60101509 1680 */
a0bd735a
BP
1681static void
1682zvol_rename_minors_impl(const char *oldname, const char *newname)
60101509
BB
1683{
1684 zvol_state_t *zv, *zv_next;
ba6a2402
BB
1685 int oldnamelen, newnamelen;
1686 char *name;
60101509 1687
74497b7a
DH
1688 if (zvol_inhibit_dev)
1689 return;
1690
ba6a2402
BB
1691 oldnamelen = strlen(oldname);
1692 newnamelen = strlen(newname);
79c76d5b 1693 name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
60101509
BB
1694
1695 mutex_enter(&zvol_state_lock);
ba6a2402 1696
60101509
BB
1697 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1698 zv_next = list_next(&zvol_state_list, zv);
1699
5428dc51
BP
1700 /* If in use, leave alone */
1701 if (zv->zv_open_count > 0)
1702 continue;
1703
ba6a2402 1704 if (strcmp(zv->zv_name, oldname) == 0) {
a0bd735a 1705 zvol_rename_minor(zv, newname);
ba6a2402
BB
1706 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 &&
1707 (zv->zv_name[oldnamelen] == '/' ||
1708 zv->zv_name[oldnamelen] == '@')) {
1709 snprintf(name, MAXNAMELEN, "%s%c%s", newname,
1710 zv->zv_name[oldnamelen],
1711 zv->zv_name + oldnamelen + 1);
a0bd735a 1712 zvol_rename_minor(zv, name);
60101509
BB
1713 }
1714 }
ba6a2402 1715
60101509 1716 mutex_exit(&zvol_state_lock);
ba6a2402
BB
1717
1718 kmem_free(name, MAXNAMELEN);
60101509
BB
1719}
1720
a0bd735a
BP
1721typedef struct zvol_snapdev_cb_arg {
1722 uint64_t snapdev;
1723} zvol_snapdev_cb_arg_t;
1724
0b4d1b58 1725static int
a0bd735a
BP
1726zvol_set_snapdev_cb(const char *dsname, void *param) {
1727 zvol_snapdev_cb_arg_t *arg = param;
0b4d1b58
ED
1728
1729 if (strchr(dsname, '@') == NULL)
ba6a2402 1730 return (0);
0b4d1b58 1731
a0bd735a 1732 switch (arg->snapdev) {
0b4d1b58 1733 case ZFS_SNAPDEV_VISIBLE:
a0bd735a 1734 (void) zvol_create_minor_impl(dsname);
0b4d1b58
ED
1735 break;
1736 case ZFS_SNAPDEV_HIDDEN:
a0bd735a 1737 (void) zvol_remove_minor_impl(dsname);
0b4d1b58
ED
1738 break;
1739 }
ba6a2402
BB
1740
1741 return (0);
0b4d1b58
ED
1742}
1743
a0bd735a
BP
1744static void
1745zvol_set_snapdev_impl(char *name, uint64_t snapdev)
1746{
1747 zvol_snapdev_cb_arg_t arg = {snapdev};
1748 fstrans_cookie_t cookie = spl_fstrans_mark();
1749 /*
1750 * The zvol_set_snapdev_sync() sets snapdev appropriately
1751 * in the dataset hierarchy. Here, we only scan snapshots.
1752 */
1753 dmu_objset_find(name, zvol_set_snapdev_cb, &arg, DS_FIND_SNAPSHOTS);
1754 spl_fstrans_unmark(cookie);
1755}
1756
1757static zvol_task_t *
1758zvol_task_alloc(zvol_async_op_t op, const char *name1, const char *name2,
1759 uint64_t snapdev)
1760{
1761 zvol_task_t *task;
1762 char *delim;
1763
1764 /* Never allow tasks on hidden names. */
1765 if (name1[0] == '$')
1766 return (NULL);
1767
1768 task = kmem_zalloc(sizeof (zvol_task_t), KM_SLEEP);
1769 task->op = op;
1770 task->snapdev = snapdev;
1771 delim = strchr(name1, '/');
1772 strlcpy(task->pool, name1, delim ? (delim - name1 + 1) : MAXNAMELEN);
1773
1774 strlcpy(task->name1, name1, MAXNAMELEN);
1775 if (name2 != NULL)
1776 strlcpy(task->name2, name2, MAXNAMELEN);
1777
1778 return (task);
1779}
1780
1781static void
1782zvol_task_free(zvol_task_t *task)
1783{
1784 kmem_free(task, sizeof (zvol_task_t));
1785}
1786
1787/*
1788 * The worker thread function performed asynchronously.
1789 */
1790static void
1791zvol_task_cb(void *param)
1792{
1793 zvol_task_t *task = (zvol_task_t *)param;
1794
1795 switch (task->op) {
1796 case ZVOL_ASYNC_CREATE_MINORS:
1797 (void) zvol_create_minors_impl(task->name1);
1798 break;
1799 case ZVOL_ASYNC_REMOVE_MINORS:
1800 zvol_remove_minors_impl(task->name1);
1801 break;
1802 case ZVOL_ASYNC_RENAME_MINORS:
1803 zvol_rename_minors_impl(task->name1, task->name2);
1804 break;
1805 case ZVOL_ASYNC_SET_SNAPDEV:
1806 zvol_set_snapdev_impl(task->name1, task->snapdev);
1807 break;
1808 default:
1809 VERIFY(0);
1810 break;
1811 }
1812
1813 zvol_task_free(task);
1814}
1815
1816typedef struct zvol_set_snapdev_arg {
1817 const char *zsda_name;
1818 uint64_t zsda_value;
1819 zprop_source_t zsda_source;
1820 dmu_tx_t *zsda_tx;
1821} zvol_set_snapdev_arg_t;
1822
1823/*
1824 * Sanity check the dataset for safe use by the sync task. No additional
1825 * conditions are imposed.
1826 */
1827static int
1828zvol_set_snapdev_check(void *arg, dmu_tx_t *tx)
1829{
1830 zvol_set_snapdev_arg_t *zsda = arg;
1831 dsl_pool_t *dp = dmu_tx_pool(tx);
1832 dsl_dir_t *dd;
1833 int error;
1834
1835 error = dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL);
1836 if (error != 0)
1837 return (error);
1838
1839 dsl_dir_rele(dd, FTAG);
1840
1841 return (error);
1842}
1843
1844static int
1845zvol_set_snapdev_sync_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1846{
1847 zvol_set_snapdev_arg_t *zsda = arg;
1848 char dsname[MAXNAMELEN];
1849 zvol_task_t *task;
1850
1851 dsl_dataset_name(ds, dsname);
1852 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_SNAPDEV),
1853 zsda->zsda_source, sizeof (zsda->zsda_value), 1,
1854 &zsda->zsda_value, zsda->zsda_tx);
1855
1856 task = zvol_task_alloc(ZVOL_ASYNC_SET_SNAPDEV, dsname,
1857 NULL, zsda->zsda_value);
1858 if (task == NULL)
1859 return (0);
1860
1861 (void) taskq_dispatch(dp->dp_spa->spa_zvol_taskq, zvol_task_cb,
1862 task, TQ_SLEEP);
1863 return (0);
1864}
1865
1866/*
1867 * Traverse all child snapshot datasets and apply snapdev appropriately.
1868 */
1869static void
1870zvol_set_snapdev_sync(void *arg, dmu_tx_t *tx)
1871{
1872 zvol_set_snapdev_arg_t *zsda = arg;
1873 dsl_pool_t *dp = dmu_tx_pool(tx);
1874 dsl_dir_t *dd;
1875
1876 VERIFY0(dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL));
1877 zsda->zsda_tx = tx;
1878
1879 dmu_objset_find_dp(dp, dd->dd_object, zvol_set_snapdev_sync_cb,
1880 zsda, DS_FIND_CHILDREN);
1881
1882 dsl_dir_rele(dd, FTAG);
1883}
1884
0b4d1b58 1885int
a0bd735a
BP
1886zvol_set_snapdev(const char *ddname, zprop_source_t source, uint64_t snapdev)
1887{
1888 zvol_set_snapdev_arg_t zsda;
5428dc51 1889
a0bd735a
BP
1890 zsda.zsda_name = ddname;
1891 zsda.zsda_source = source;
1892 zsda.zsda_value = snapdev;
5428dc51 1893
a0bd735a
BP
1894 return (dsl_sync_task(ddname, zvol_set_snapdev_check,
1895 zvol_set_snapdev_sync, &zsda, 0, ZFS_SPACE_CHECK_NONE));
1896}
1897
1898void
1899zvol_create_minors(spa_t *spa, const char *name, boolean_t async)
1900{
1901 zvol_task_t *task;
1902 taskqid_t id;
1903
1904 task = zvol_task_alloc(ZVOL_ASYNC_CREATE_MINORS, name, NULL, ~0ULL);
1905 if (task == NULL)
1906 return;
1907
1908 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
1909 if ((async == B_FALSE) && (id != 0))
1910 taskq_wait_id(spa->spa_zvol_taskq, id);
1911}
1912
1913void
1914zvol_remove_minors(spa_t *spa, const char *name, boolean_t async)
1915{
1916 zvol_task_t *task;
1917 taskqid_t id;
1918
1919 task = zvol_task_alloc(ZVOL_ASYNC_REMOVE_MINORS, name, NULL, ~0ULL);
1920 if (task == NULL)
1921 return;
5428dc51 1922
a0bd735a
BP
1923 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
1924 if ((async == B_FALSE) && (id != 0))
1925 taskq_wait_id(spa->spa_zvol_taskq, id);
1926}
1927
1928void
1929zvol_rename_minors(spa_t *spa, const char *name1, const char *name2,
1930 boolean_t async)
1931{
1932 zvol_task_t *task;
1933 taskqid_t id;
1934
1935 task = zvol_task_alloc(ZVOL_ASYNC_RENAME_MINORS, name1, name2, ~0ULL);
1936 if (task == NULL)
1937 return;
1938
1939 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
1940 if ((async == B_FALSE) && (id != 0))
1941 taskq_wait_id(spa->spa_zvol_taskq, id);
0b4d1b58
ED
1942}
1943
60101509
BB
1944int
1945zvol_init(void)
1946{
1947 int error;
1948
2a3871d4 1949 list_create(&zvol_state_list, sizeof (zvol_state_t),
ce37ebd2 1950 offsetof(zvol_state_t, zv_next));
2a3871d4
RY
1951 mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1952
60101509
BB
1953 error = register_blkdev(zvol_major, ZVOL_DRIVER);
1954 if (error) {
1955 printk(KERN_INFO "ZFS: register_blkdev() failed %d\n", error);
37f9dac5 1956 goto out;
60101509
BB
1957 }
1958
1959 blk_register_region(MKDEV(zvol_major, 0), 1UL << MINORBITS,
ce37ebd2 1960 THIS_MODULE, zvol_probe, NULL, NULL);
60101509 1961
60101509 1962 return (0);
2a3871d4 1963
37f9dac5 1964out:
2a3871d4
RY
1965 mutex_destroy(&zvol_state_lock);
1966 list_destroy(&zvol_state_list);
1967
ce37ebd2 1968 return (SET_ERROR(error));
60101509
BB
1969}
1970
1971void
1972zvol_fini(void)
1973{
a0bd735a
BP
1974 zvol_remove_minors_impl(NULL);
1975
60101509
BB
1976 blk_unregister_region(MKDEV(zvol_major, 0), 1UL << MINORBITS);
1977 unregister_blkdev(zvol_major, ZVOL_DRIVER);
a0bd735a 1978
60101509 1979 list_destroy(&zvol_state_list);
a0bd735a 1980 mutex_destroy(&zvol_state_lock);
60101509
BB
1981}
1982
74497b7a
DH
1983module_param(zvol_inhibit_dev, uint, 0644);
1984MODULE_PARM_DESC(zvol_inhibit_dev, "Do not create zvol device nodes");
1985
30a9524e 1986module_param(zvol_major, uint, 0444);
60101509
BB
1987MODULE_PARM_DESC(zvol_major, "Major number for zvol device");
1988
7c0e5708 1989module_param(zvol_max_discard_blocks, ulong, 0444);
ce37ebd2 1990MODULE_PARM_DESC(zvol_max_discard_blocks, "Max number of blocks to discard");
9965059a
BB
1991
1992module_param(zvol_prefetch_bytes, uint, 0644);
1993MODULE_PARM_DESC(zvol_prefetch_bytes, "Prefetch N bytes at zvol start+end");