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