]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zvol.c
Suppress cppcheck nullPointer error in zfs_write
[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 1217
040dab99 1218 rw_enter(&zv->zv_suspend_lock, RW_READER);
60101509
BB
1219 switch (cmd) {
1220 case BLKFLSBUF:
1221 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1222 break;
4c0d8e50
FN
1223 case BLKZNAME:
1224 error = copy_to_user((void *)arg, zv->zv_name, MAXNAMELEN);
1225 break;
60101509
BB
1226
1227 default:
1228 error = -ENOTTY;
1229 break;
60101509 1230 }
040dab99 1231 rw_exit(&zv->zv_suspend_lock);
60101509 1232
ce37ebd2 1233 return (SET_ERROR(error));
60101509
BB
1234}
1235
1236#ifdef CONFIG_COMPAT
1237static int
1238zvol_compat_ioctl(struct block_device *bdev, fmode_t mode,
ce37ebd2 1239 unsigned cmd, unsigned long arg)
60101509 1240{
ce37ebd2 1241 return (zvol_ioctl(bdev, mode, cmd, arg));
60101509
BB
1242}
1243#else
ce37ebd2 1244#define zvol_compat_ioctl NULL
60101509
BB
1245#endif
1246
1247static int zvol_media_changed(struct gendisk *disk)
1248{
1249 zvol_state_t *zv = disk->private_data;
1250
5428dc51
BP
1251 ASSERT(zv && zv->zv_open_count > 0);
1252
ce37ebd2 1253 return (zv->zv_changed);
60101509
BB
1254}
1255
1256static int zvol_revalidate_disk(struct gendisk *disk)
1257{
1258 zvol_state_t *zv = disk->private_data;
1259
5428dc51
BP
1260 ASSERT(zv && zv->zv_open_count > 0);
1261
60101509
BB
1262 zv->zv_changed = 0;
1263 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1264
ce37ebd2 1265 return (0);
60101509
BB
1266}
1267
1268/*
1269 * Provide a simple virtual geometry for legacy compatibility. For devices
1270 * smaller than 1 MiB a small head and sector count is used to allow very
1271 * tiny devices. For devices over 1 Mib a standard head and sector count
1272 * is used to keep the cylinders count reasonable.
1273 */
1274static int
1275zvol_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1276{
1277 zvol_state_t *zv = bdev->bd_disk->private_data;
5428dc51
BP
1278 sector_t sectors;
1279
1280 ASSERT(zv && zv->zv_open_count > 0);
1281
1282 sectors = get_capacity(zv->zv_disk);
60101509
BB
1283
1284 if (sectors > 2048) {
1285 geo->heads = 16;
1286 geo->sectors = 63;
1287 } else {
1288 geo->heads = 2;
1289 geo->sectors = 4;
1290 }
1291
1292 geo->start = 0;
1293 geo->cylinders = sectors / (geo->heads * geo->sectors);
1294
ce37ebd2 1295 return (0);
60101509
BB
1296}
1297
1298static struct kobject *
1299zvol_probe(dev_t dev, int *part, void *arg)
1300{
1301 zvol_state_t *zv;
1302 struct kobject *kobj;
1303
1304 mutex_enter(&zvol_state_lock);
1305 zv = zvol_find_by_dev(dev);
23a61ccc 1306 kobj = zv ? get_disk(zv->zv_disk) : NULL;
60101509
BB
1307 mutex_exit(&zvol_state_lock);
1308
ce37ebd2 1309 return (kobj);
60101509
BB
1310}
1311
1312#ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS
1313static struct block_device_operations zvol_ops = {
ce37ebd2
BB
1314 .open = zvol_open,
1315 .release = zvol_release,
1316 .ioctl = zvol_ioctl,
1317 .compat_ioctl = zvol_compat_ioctl,
1318 .media_changed = zvol_media_changed,
1319 .revalidate_disk = zvol_revalidate_disk,
1320 .getgeo = zvol_getgeo,
1321 .owner = THIS_MODULE,
60101509
BB
1322};
1323
1324#else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1325
1326static int
1327zvol_open_by_inode(struct inode *inode, struct file *file)
1328{
ce37ebd2 1329 return (zvol_open(inode->i_bdev, file->f_mode));
60101509
BB
1330}
1331
1332static int
1333zvol_release_by_inode(struct inode *inode, struct file *file)
1334{
ce37ebd2 1335 return (zvol_release(inode->i_bdev->bd_disk, file->f_mode));
60101509
BB
1336}
1337
1338static int
1339zvol_ioctl_by_inode(struct inode *inode, struct file *file,
ce37ebd2 1340 unsigned int cmd, unsigned long arg)
60101509 1341{
b1c58213 1342 if (file == NULL || inode == NULL)
ce37ebd2
BB
1343 return (SET_ERROR(-EINVAL));
1344
1345 return (zvol_ioctl(inode->i_bdev, file->f_mode, cmd, arg));
60101509
BB
1346}
1347
ce37ebd2 1348#ifdef CONFIG_COMPAT
60101509
BB
1349static long
1350zvol_compat_ioctl_by_inode(struct file *file,
ce37ebd2 1351 unsigned int cmd, unsigned long arg)
60101509 1352{
b1c58213 1353 if (file == NULL)
ce37ebd2
BB
1354 return (SET_ERROR(-EINVAL));
1355
1356 return (zvol_compat_ioctl(file->f_dentry->d_inode->i_bdev,
1357 file->f_mode, cmd, arg));
60101509 1358}
ce37ebd2
BB
1359#else
1360#define zvol_compat_ioctl_by_inode NULL
1361#endif
60101509
BB
1362
1363static struct block_device_operations zvol_ops = {
ce37ebd2
BB
1364 .open = zvol_open_by_inode,
1365 .release = zvol_release_by_inode,
1366 .ioctl = zvol_ioctl_by_inode,
1367 .compat_ioctl = zvol_compat_ioctl_by_inode,
1368 .media_changed = zvol_media_changed,
1369 .revalidate_disk = zvol_revalidate_disk,
1370 .getgeo = zvol_getgeo,
1371 .owner = THIS_MODULE,
60101509
BB
1372};
1373#endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1374
1375/*
1376 * Allocate memory for a new zvol_state_t and setup the required
1377 * request queue and generic disk structures for the block device.
1378 */
1379static zvol_state_t *
1380zvol_alloc(dev_t dev, const char *name)
1381{
1382 zvol_state_t *zv;
1383
79c76d5b 1384 zv = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
60101509 1385
2a3871d4
RY
1386 list_link_init(&zv->zv_next);
1387
37f9dac5 1388 zv->zv_queue = blk_alloc_queue(GFP_ATOMIC);
60101509
BB
1389 if (zv->zv_queue == NULL)
1390 goto out_kmem;
1391
37f9dac5 1392 blk_queue_make_request(zv->zv_queue, zvol_request);
cf41432c 1393 blk_queue_set_write_cache(zv->zv_queue, B_TRUE, B_TRUE);
b18019d2 1394
60101509
BB
1395 zv->zv_disk = alloc_disk(ZVOL_MINORS);
1396 if (zv->zv_disk == NULL)
1397 goto out_queue;
1398
1399 zv->zv_queue->queuedata = zv;
1400 zv->zv_dev = dev;
1401 zv->zv_open_count = 0;
4c0d8e50 1402 strlcpy(zv->zv_name, name, MAXNAMELEN);
60101509 1403
d88895a0 1404 zfs_rlock_init(&zv->zv_range_lock);
040dab99 1405 rw_init(&zv->zv_suspend_lock, NULL, RW_DEFAULT, NULL);
3c4988c8 1406
60101509
BB
1407 zv->zv_disk->major = zvol_major;
1408 zv->zv_disk->first_minor = (dev & MINORMASK);
1409 zv->zv_disk->fops = &zvol_ops;
1410 zv->zv_disk->private_data = zv;
1411 zv->zv_disk->queue = zv->zv_queue;
4c0d8e50
FN
1412 snprintf(zv->zv_disk->disk_name, DISK_NAME_LEN, "%s%d",
1413 ZVOL_DEV_NAME, (dev & MINORMASK));
60101509 1414
ce37ebd2 1415 return (zv);
60101509
BB
1416
1417out_queue:
1418 blk_cleanup_queue(zv->zv_queue);
1419out_kmem:
1420 kmem_free(zv, sizeof (zvol_state_t));
0a6bef26 1421
ce37ebd2 1422 return (NULL);
60101509
BB
1423}
1424
1425/*
899662e3
CC
1426 * Used for taskq, if used out side zvol_state_lock, you need to clear
1427 * zv_disk->private_data inside lock first.
60101509
BB
1428 */
1429static void
899662e3 1430zvol_free_impl(void *arg)
60101509 1431{
899662e3 1432 zvol_state_t *zv = arg;
5428dc51
BP
1433 ASSERT(zv->zv_open_count == 0);
1434
040dab99 1435 rw_destroy(&zv->zv_suspend_lock);
d88895a0 1436 zfs_rlock_destroy(&zv->zv_range_lock);
60101509 1437
5428dc51
BP
1438 zv->zv_disk->private_data = NULL;
1439
60101509
BB
1440 del_gendisk(zv->zv_disk);
1441 blk_cleanup_queue(zv->zv_queue);
1442 put_disk(zv->zv_disk);
1443
d45e010d 1444 ida_simple_remove(&zvol_ida, MINOR(zv->zv_dev) >> ZVOL_MINOR_BITS);
60101509
BB
1445 kmem_free(zv, sizeof (zvol_state_t));
1446}
1447
899662e3
CC
1448/*
1449 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1450 */
1451static void
1452zvol_free(zvol_state_t *zv)
1453{
1454 ASSERT(MUTEX_HELD(&zvol_state_lock));
1455 zvol_free_impl(zv);
1456}
1457
a0bd735a
BP
1458/*
1459 * Create a block device minor node and setup the linkage between it
1460 * and the specified volume. Once this function returns the block
1461 * device is live and ready for use.
1462 */
60101509 1463static int
a0bd735a 1464zvol_create_minor_impl(const char *name)
60101509
BB
1465{
1466 zvol_state_t *zv;
1467 objset_t *os;
1468 dmu_object_info_t *doi;
1469 uint64_t volsize;
9965059a 1470 uint64_t len;
60101509
BB
1471 unsigned minor = 0;
1472 int error = 0;
d45e010d
CC
1473 int idx;
1474 uint64_t hash = zvol_name_hash(name);
1475
1476 idx = ida_simple_get(&zvol_ida, 0, 0, kmem_flags_convert(KM_SLEEP));
1477 if (idx < 0)
1478 return (SET_ERROR(-idx));
1479 minor = idx << ZVOL_MINOR_BITS;
60101509 1480
a0bd735a 1481 mutex_enter(&zvol_state_lock);
60101509 1482
d45e010d 1483 zv = zvol_find_by_name_hash(name, hash);
60101509 1484 if (zv) {
2e528b49 1485 error = SET_ERROR(EEXIST);
60101509
BB
1486 goto out;
1487 }
1488
79c76d5b 1489 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
60101509 1490
040dab99 1491 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, FTAG, &os);
60101509
BB
1492 if (error)
1493 goto out_doi;
1494
1495 error = dmu_object_info(os, ZVOL_OBJ, doi);
1496 if (error)
1497 goto out_dmu_objset_disown;
1498
1499 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
1500 if (error)
1501 goto out_dmu_objset_disown;
1502
60101509
BB
1503 zv = zvol_alloc(MKDEV(zvol_major, minor), name);
1504 if (zv == NULL) {
2e528b49 1505 error = SET_ERROR(EAGAIN);
60101509
BB
1506 goto out_dmu_objset_disown;
1507 }
d45e010d 1508 zv->zv_hash = hash;
60101509
BB
1509
1510 if (dmu_objset_is_snapshot(os))
1511 zv->zv_flags |= ZVOL_RDONLY;
1512
1513 zv->zv_volblocksize = doi->doi_data_block_size;
1514 zv->zv_volsize = volsize;
1515 zv->zv_objset = os;
1516
1517 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1518
c495fe2c 1519 blk_queue_max_hw_sectors(zv->zv_queue, (DMU_MAX_ACCESS / 4) >> 9);
34037afe
ED
1520 blk_queue_max_segments(zv->zv_queue, UINT16_MAX);
1521 blk_queue_max_segment_size(zv->zv_queue, UINT_MAX);
1522 blk_queue_physical_block_size(zv->zv_queue, zv->zv_volblocksize);
1523 blk_queue_io_opt(zv->zv_queue, zv->zv_volblocksize);
7c0e5708
ED
1524 blk_queue_max_discard_sectors(zv->zv_queue,
1525 (zvol_max_discard_blocks * zv->zv_volblocksize) >> 9);
ee5fd0bb 1526 blk_queue_discard_granularity(zv->zv_queue, zv->zv_volblocksize);
30930fba 1527 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zv->zv_queue);
37f9dac5 1528#ifdef QUEUE_FLAG_NONROT
34037afe
ED
1529 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zv->zv_queue);
1530#endif
c6a3a222
RY
1531#ifdef QUEUE_FLAG_ADD_RANDOM
1532 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, zv->zv_queue);
1533#endif
34037afe 1534
a4430fce
GW
1535 if (spa_writeable(dmu_objset_spa(os))) {
1536 if (zil_replay_disable)
1537 zil_destroy(dmu_objset_zil(os), B_FALSE);
1538 else
1539 zil_replay(os, zv, zvol_replay_vector);
1540 }
60101509 1541
9965059a
BB
1542 /*
1543 * When udev detects the addition of the device it will immediately
1544 * invoke blkid(8) to determine the type of content on the device.
1545 * Prefetching the blocks commonly scanned by blkid(8) will speed
1546 * up this process.
1547 */
1548 len = MIN(MAX(zvol_prefetch_bytes, 0), SPA_MAXBLOCKSIZE);
1549 if (len > 0) {
fcff0f35
PD
1550 dmu_prefetch(os, ZVOL_OBJ, 0, 0, len, ZIO_PRIORITY_SYNC_READ);
1551 dmu_prefetch(os, ZVOL_OBJ, 0, volsize - len, len,
02730c33 1552 ZIO_PRIORITY_SYNC_READ);
9965059a
BB
1553 }
1554
f74a147c 1555 zv->zv_objset = NULL;
60101509 1556out_dmu_objset_disown:
040dab99 1557 dmu_objset_disown(os, FTAG);
60101509 1558out_doi:
ce37ebd2 1559 kmem_free(doi, sizeof (dmu_object_info_t));
60101509
BB
1560out:
1561
1562 if (error == 0) {
1563 zvol_insert(zv);
5428dc51
BP
1564 /*
1565 * Drop the lock to prevent deadlock with sys_open() ->
1566 * zvol_open(), which first takes bd_disk->bd_mutex and then
1567 * takes zvol_state_lock, whereas this code path first takes
1568 * zvol_state_lock, and then takes bd_disk->bd_mutex.
1569 */
1570 mutex_exit(&zvol_state_lock);
60101509 1571 add_disk(zv->zv_disk);
a0bd735a
BP
1572 } else {
1573 mutex_exit(&zvol_state_lock);
d45e010d 1574 ida_simple_remove(&zvol_ida, idx);
60101509
BB
1575 }
1576
ce37ebd2 1577 return (SET_ERROR(error));
60101509
BB
1578}
1579
ba6a2402
BB
1580/*
1581 * Rename a block device minor mode for the specified volume.
1582 */
1583static void
a0bd735a 1584zvol_rename_minor(zvol_state_t *zv, const char *newname)
ba6a2402
BB
1585{
1586 int readonly = get_disk_ro(zv->zv_disk);
1587
1588 ASSERT(MUTEX_HELD(&zvol_state_lock));
1589
040dab99 1590 rw_enter(&zv->zv_suspend_lock, RW_READER);
ba6a2402 1591 strlcpy(zv->zv_name, newname, sizeof (zv->zv_name));
040dab99
CC
1592 rw_exit(&zv->zv_suspend_lock);
1593
1594 /* move to new hashtable entry */
1595 zv->zv_hash = zvol_name_hash(zv->zv_name);
1596 hlist_del(&zv->zv_hlink);
1597 hlist_add_head(&zv->zv_hlink, ZVOL_HT_HEAD(zv->zv_hash));
ba6a2402
BB
1598
1599 /*
1600 * The block device's read-only state is briefly changed causing
1601 * a KOBJ_CHANGE uevent to be issued. This ensures udev detects
1602 * the name change and fixes the symlinks. This does not change
1603 * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
1604 * changes. This would normally be done using kobject_uevent() but
1605 * that is a GPL-only symbol which is why we need this workaround.
1606 */
1607 set_disk_ro(zv->zv_disk, !readonly);
1608 set_disk_ro(zv->zv_disk, readonly);
1609}
1610
7ac557ce
CC
1611typedef struct minors_job {
1612 list_t *list;
1613 list_node_t link;
1614 /* input */
1615 char *name;
1616 /* output */
1617 int error;
1618} minors_job_t;
1619
1620/*
1621 * Prefetch zvol dnodes for the minors_job
1622 */
1623static void
1624zvol_prefetch_minors_impl(void *arg)
1625{
1626 minors_job_t *job = arg;
1627 char *dsname = job->name;
1628 objset_t *os = NULL;
1629
040dab99 1630 job->error = dmu_objset_own(dsname, DMU_OST_ZVOL, B_TRUE, FTAG,
7ac557ce
CC
1631 &os);
1632 if (job->error == 0) {
1633 dmu_prefetch(os, ZVOL_OBJ, 0, 0, 0, ZIO_PRIORITY_SYNC_READ);
040dab99 1634 dmu_objset_disown(os, FTAG);
7ac557ce
CC
1635 }
1636}
a0bd735a
BP
1637
1638/*
1639 * Mask errors to continue dmu_objset_find() traversal
1640 */
1641static int
1642zvol_create_snap_minor_cb(const char *dsname, void *arg)
1643{
7ac557ce
CC
1644 minors_job_t *j = arg;
1645 list_t *minors_list = j->list;
1646 const char *name = j->name;
a0bd735a 1647
1ee159f4
BP
1648 ASSERT0(MUTEX_HELD(&spa_namespace_lock));
1649
a0bd735a
BP
1650 /* skip the designated dataset */
1651 if (name && strcmp(dsname, name) == 0)
1652 return (0);
1653
1654 /* at this point, the dsname should name a snapshot */
1655 if (strchr(dsname, '@') == 0) {
1656 dprintf("zvol_create_snap_minor_cb(): "
02730c33 1657 "%s is not a shapshot name\n", dsname);
a0bd735a 1658 } else {
7ac557ce
CC
1659 minors_job_t *job;
1660 char *n = strdup(dsname);
1661 if (n == NULL)
1662 return (0);
1663
1664 job = kmem_alloc(sizeof (minors_job_t), KM_SLEEP);
1665 job->name = n;
1666 job->list = minors_list;
1667 job->error = 0;
1668 list_insert_tail(minors_list, job);
1669 /* don't care if dispatch fails, because job->error is 0 */
1670 taskq_dispatch(system_taskq, zvol_prefetch_minors_impl, job,
1671 TQ_SLEEP);
a0bd735a
BP
1672 }
1673
1674 return (0);
1675}
1676
1677/*
1678 * Mask errors to continue dmu_objset_find() traversal
1679 */
60101509 1680static int
13fe0198 1681zvol_create_minors_cb(const char *dsname, void *arg)
60101509 1682{
a0bd735a
BP
1683 uint64_t snapdev;
1684 int error;
7ac557ce 1685 list_t *minors_list = arg;
a0bd735a 1686
1ee159f4
BP
1687 ASSERT0(MUTEX_HELD(&spa_namespace_lock));
1688
a0bd735a
BP
1689 error = dsl_prop_get_integer(dsname, "snapdev", &snapdev, NULL);
1690 if (error)
1691 return (0);
1692
1693 /*
1694 * Given the name and the 'snapdev' property, create device minor nodes
1695 * with the linkages to zvols/snapshots as needed.
1696 * If the name represents a zvol, create a minor node for the zvol, then
1697 * check if its snapshots are 'visible', and if so, iterate over the
1698 * snapshots and create device minor nodes for those.
1699 */
1700 if (strchr(dsname, '@') == 0) {
7ac557ce
CC
1701 minors_job_t *job;
1702 char *n = strdup(dsname);
1703 if (n == NULL)
1704 return (0);
1705
1706 job = kmem_alloc(sizeof (minors_job_t), KM_SLEEP);
1707 job->name = n;
1708 job->list = minors_list;
1709 job->error = 0;
1710 list_insert_tail(minors_list, job);
1711 /* don't care if dispatch fails, because job->error is 0 */
1712 taskq_dispatch(system_taskq, zvol_prefetch_minors_impl, job,
1713 TQ_SLEEP);
1714
1715 if (snapdev == ZFS_SNAPDEV_VISIBLE) {
a0bd735a
BP
1716 /*
1717 * traverse snapshots only, do not traverse children,
1718 * and skip the 'dsname'
1719 */
1720 error = dmu_objset_find((char *)dsname,
7ac557ce 1721 zvol_create_snap_minor_cb, (void *)job,
a0bd735a 1722 DS_FIND_SNAPSHOTS);
a0bd735a
BP
1723 }
1724 } else {
1725 dprintf("zvol_create_minors_cb(): %s is not a zvol name\n",
02730c33 1726 dsname);
a0bd735a 1727 }
60101509 1728
d5674448 1729 return (0);
60101509
BB
1730}
1731
1732/*
a0bd735a
BP
1733 * Create minors for the specified dataset, including children and snapshots.
1734 * Pay attention to the 'snapdev' property and iterate over the snapshots
1735 * only if they are 'visible'. This approach allows one to assure that the
1736 * snapshot metadata is read from disk only if it is needed.
1737 *
1738 * The name can represent a dataset to be recursively scanned for zvols and
1739 * their snapshots, or a single zvol snapshot. If the name represents a
1740 * dataset, the scan is performed in two nested stages:
1741 * - scan the dataset for zvols, and
1742 * - for each zvol, create a minor node, then check if the zvol's snapshots
1743 * are 'visible', and only then iterate over the snapshots if needed
1744 *
4e33ba4c 1745 * If the name represents a snapshot, a check is performed if the snapshot is
a0bd735a
BP
1746 * 'visible' (which also verifies that the parent is a zvol), and if so,
1747 * a minor node for that snapshot is created.
60101509 1748 */
a0bd735a
BP
1749static int
1750zvol_create_minors_impl(const char *name)
60101509 1751{
60101509 1752 int error = 0;
5428dc51 1753 fstrans_cookie_t cookie;
a0bd735a 1754 char *atp, *parent;
7ac557ce
CC
1755 list_t minors_list;
1756 minors_job_t *job;
60101509 1757
5428dc51
BP
1758 if (zvol_inhibit_dev)
1759 return (0);
1760
7ac557ce
CC
1761 /*
1762 * This is the list for prefetch jobs. Whenever we found a match
1763 * during dmu_objset_find, we insert a minors_job to the list and do
1764 * taskq_dispatch to parallel prefetch zvol dnodes. Note we don't need
1765 * any lock because all list operation is done on the current thread.
1766 *
1767 * We will use this list to do zvol_create_minor_impl after prefetch
1768 * so we don't have to traverse using dmu_objset_find again.
1769 */
1770 list_create(&minors_list, sizeof (minors_job_t),
1771 offsetof(minors_job_t, link));
1772
a0bd735a
BP
1773 parent = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1774 (void) strlcpy(parent, name, MAXPATHLEN);
1775
1776 if ((atp = strrchr(parent, '@')) != NULL) {
1777 uint64_t snapdev;
1778
1779 *atp = '\0';
1780 error = dsl_prop_get_integer(parent, "snapdev",
1781 &snapdev, NULL);
1782
1783 if (error == 0 && snapdev == ZFS_SNAPDEV_VISIBLE)
1784 error = zvol_create_minor_impl(name);
1785 } else {
1786 cookie = spl_fstrans_mark();
1787 error = dmu_objset_find(parent, zvol_create_minors_cb,
7ac557ce 1788 &minors_list, DS_FIND_CHILDREN);
a0bd735a
BP
1789 spl_fstrans_unmark(cookie);
1790 }
1791
1792 kmem_free(parent, MAXPATHLEN);
7ac557ce
CC
1793 taskq_wait_outstanding(system_taskq, 0);
1794
1795 /*
1796 * Prefetch is completed, we can do zvol_create_minor_impl
1797 * sequentially.
1798 */
1799 while ((job = list_head(&minors_list)) != NULL) {
1800 list_remove(&minors_list, job);
1801 if (!job->error)
1802 zvol_create_minor_impl(job->name);
1803 strfree(job->name);
1804 kmem_free(job, sizeof (minors_job_t));
1805 }
1806
1807 list_destroy(&minors_list);
ba6a2402
BB
1808
1809 return (SET_ERROR(error));
1810}
1811
1812/*
1813 * Remove minors for specified dataset including children and snapshots.
1814 */
a0bd735a
BP
1815static void
1816zvol_remove_minors_impl(const char *name)
ba6a2402
BB
1817{
1818 zvol_state_t *zv, *zv_next;
1819 int namelen = ((name) ? strlen(name) : 0);
899662e3 1820 taskqid_t t, tid = TASKQID_INVALID;
ba6a2402 1821
74497b7a 1822 if (zvol_inhibit_dev)
ba6a2402 1823 return;
74497b7a 1824
60101509 1825 mutex_enter(&zvol_state_lock);
ba6a2402
BB
1826
1827 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1828 zv_next = list_next(&zvol_state_list, zv);
1829
1830 if (name == NULL || strcmp(zv->zv_name, name) == 0 ||
1831 (strncmp(zv->zv_name, name, namelen) == 0 &&
5428dc51
BP
1832 (zv->zv_name[namelen] == '/' ||
1833 zv->zv_name[namelen] == '@'))) {
1834
1835 /* If in use, leave alone */
040dab99
CC
1836 if (zv->zv_open_count > 0 ||
1837 atomic_read(&zv->zv_suspend_ref))
5428dc51
BP
1838 continue;
1839
ba6a2402 1840 zvol_remove(zv);
899662e3
CC
1841
1842 /* clear this so zvol_open won't open it */
1843 zv->zv_disk->private_data = NULL;
1844
1845 /* try parallel zv_free, if failed do it in place */
1846 t = taskq_dispatch(system_taskq, zvol_free_impl, zv,
1847 TQ_SLEEP);
1848 if (t == TASKQID_INVALID)
1849 zvol_free(zv);
1850 else
1851 tid = t;
60101509 1852 }
60101509 1853 }
ba6a2402 1854 mutex_exit(&zvol_state_lock);
899662e3
CC
1855 if (tid != TASKQID_INVALID)
1856 taskq_wait_outstanding(system_taskq, tid);
60101509
BB
1857}
1858
a0bd735a
BP
1859/* Remove minor for this specific snapshot only */
1860static void
1861zvol_remove_minor_impl(const char *name)
1862{
1863 zvol_state_t *zv, *zv_next;
1864
1865 if (zvol_inhibit_dev)
1866 return;
1867
1868 if (strchr(name, '@') == NULL)
1869 return;
1870
1871 mutex_enter(&zvol_state_lock);
1872
1873 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1874 zv_next = list_next(&zvol_state_list, zv);
1875
1876 if (strcmp(zv->zv_name, name) == 0) {
1877 /* If in use, leave alone */
040dab99
CC
1878 if (zv->zv_open_count > 0 ||
1879 atomic_read(&zv->zv_suspend_ref))
a0bd735a
BP
1880 continue;
1881 zvol_remove(zv);
1882 zvol_free(zv);
1883 break;
1884 }
1885 }
1886
1887 mutex_exit(&zvol_state_lock);
1888}
1889
60101509 1890/*
ba6a2402 1891 * Rename minors for specified dataset including children and snapshots.
60101509 1892 */
a0bd735a
BP
1893static void
1894zvol_rename_minors_impl(const char *oldname, const char *newname)
60101509
BB
1895{
1896 zvol_state_t *zv, *zv_next;
ba6a2402
BB
1897 int oldnamelen, newnamelen;
1898 char *name;
60101509 1899
74497b7a
DH
1900 if (zvol_inhibit_dev)
1901 return;
1902
ba6a2402
BB
1903 oldnamelen = strlen(oldname);
1904 newnamelen = strlen(newname);
79c76d5b 1905 name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
60101509
BB
1906
1907 mutex_enter(&zvol_state_lock);
ba6a2402 1908
60101509
BB
1909 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1910 zv_next = list_next(&zvol_state_list, zv);
1911
5428dc51
BP
1912 /* If in use, leave alone */
1913 if (zv->zv_open_count > 0)
1914 continue;
1915
ba6a2402 1916 if (strcmp(zv->zv_name, oldname) == 0) {
a0bd735a 1917 zvol_rename_minor(zv, newname);
ba6a2402
BB
1918 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 &&
1919 (zv->zv_name[oldnamelen] == '/' ||
1920 zv->zv_name[oldnamelen] == '@')) {
1921 snprintf(name, MAXNAMELEN, "%s%c%s", newname,
1922 zv->zv_name[oldnamelen],
1923 zv->zv_name + oldnamelen + 1);
a0bd735a 1924 zvol_rename_minor(zv, name);
60101509
BB
1925 }
1926 }
ba6a2402 1927
60101509 1928 mutex_exit(&zvol_state_lock);
ba6a2402
BB
1929
1930 kmem_free(name, MAXNAMELEN);
60101509
BB
1931}
1932
a0bd735a
BP
1933typedef struct zvol_snapdev_cb_arg {
1934 uint64_t snapdev;
1935} zvol_snapdev_cb_arg_t;
1936
0b4d1b58 1937static int
4ea3f864
GM
1938zvol_set_snapdev_cb(const char *dsname, void *param)
1939{
a0bd735a 1940 zvol_snapdev_cb_arg_t *arg = param;
0b4d1b58
ED
1941
1942 if (strchr(dsname, '@') == NULL)
ba6a2402 1943 return (0);
0b4d1b58 1944
a0bd735a 1945 switch (arg->snapdev) {
0b4d1b58 1946 case ZFS_SNAPDEV_VISIBLE:
a0bd735a 1947 (void) zvol_create_minor_impl(dsname);
0b4d1b58
ED
1948 break;
1949 case ZFS_SNAPDEV_HIDDEN:
a0bd735a 1950 (void) zvol_remove_minor_impl(dsname);
0b4d1b58
ED
1951 break;
1952 }
ba6a2402
BB
1953
1954 return (0);
0b4d1b58
ED
1955}
1956
a0bd735a
BP
1957static void
1958zvol_set_snapdev_impl(char *name, uint64_t snapdev)
1959{
1960 zvol_snapdev_cb_arg_t arg = {snapdev};
1961 fstrans_cookie_t cookie = spl_fstrans_mark();
1962 /*
1963 * The zvol_set_snapdev_sync() sets snapdev appropriately
1964 * in the dataset hierarchy. Here, we only scan snapshots.
1965 */
1966 dmu_objset_find(name, zvol_set_snapdev_cb, &arg, DS_FIND_SNAPSHOTS);
1967 spl_fstrans_unmark(cookie);
1968}
1969
1970static zvol_task_t *
1971zvol_task_alloc(zvol_async_op_t op, const char *name1, const char *name2,
1972 uint64_t snapdev)
1973{
1974 zvol_task_t *task;
1975 char *delim;
1976
1977 /* Never allow tasks on hidden names. */
1978 if (name1[0] == '$')
1979 return (NULL);
1980
1981 task = kmem_zalloc(sizeof (zvol_task_t), KM_SLEEP);
1982 task->op = op;
1983 task->snapdev = snapdev;
1984 delim = strchr(name1, '/');
1985 strlcpy(task->pool, name1, delim ? (delim - name1 + 1) : MAXNAMELEN);
1986
1987 strlcpy(task->name1, name1, MAXNAMELEN);
1988 if (name2 != NULL)
1989 strlcpy(task->name2, name2, MAXNAMELEN);
1990
1991 return (task);
1992}
1993
1994static void
1995zvol_task_free(zvol_task_t *task)
1996{
1997 kmem_free(task, sizeof (zvol_task_t));
1998}
1999
2000/*
2001 * The worker thread function performed asynchronously.
2002 */
2003static void
2004zvol_task_cb(void *param)
2005{
2006 zvol_task_t *task = (zvol_task_t *)param;
2007
2008 switch (task->op) {
2009 case ZVOL_ASYNC_CREATE_MINORS:
2010 (void) zvol_create_minors_impl(task->name1);
2011 break;
2012 case ZVOL_ASYNC_REMOVE_MINORS:
2013 zvol_remove_minors_impl(task->name1);
2014 break;
2015 case ZVOL_ASYNC_RENAME_MINORS:
2016 zvol_rename_minors_impl(task->name1, task->name2);
2017 break;
2018 case ZVOL_ASYNC_SET_SNAPDEV:
2019 zvol_set_snapdev_impl(task->name1, task->snapdev);
2020 break;
2021 default:
2022 VERIFY(0);
2023 break;
2024 }
2025
2026 zvol_task_free(task);
2027}
2028
2029typedef struct zvol_set_snapdev_arg {
2030 const char *zsda_name;
2031 uint64_t zsda_value;
2032 zprop_source_t zsda_source;
2033 dmu_tx_t *zsda_tx;
2034} zvol_set_snapdev_arg_t;
2035
2036/*
2037 * Sanity check the dataset for safe use by the sync task. No additional
2038 * conditions are imposed.
2039 */
2040static int
2041zvol_set_snapdev_check(void *arg, dmu_tx_t *tx)
2042{
2043 zvol_set_snapdev_arg_t *zsda = arg;
2044 dsl_pool_t *dp = dmu_tx_pool(tx);
2045 dsl_dir_t *dd;
2046 int error;
2047
2048 error = dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL);
2049 if (error != 0)
2050 return (error);
2051
2052 dsl_dir_rele(dd, FTAG);
2053
2054 return (error);
2055}
2056
2057static int
2058zvol_set_snapdev_sync_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
2059{
2060 zvol_set_snapdev_arg_t *zsda = arg;
2061 char dsname[MAXNAMELEN];
2062 zvol_task_t *task;
2063
2064 dsl_dataset_name(ds, dsname);
2065 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_SNAPDEV),
2066 zsda->zsda_source, sizeof (zsda->zsda_value), 1,
2067 &zsda->zsda_value, zsda->zsda_tx);
2068
2069 task = zvol_task_alloc(ZVOL_ASYNC_SET_SNAPDEV, dsname,
2070 NULL, zsda->zsda_value);
2071 if (task == NULL)
2072 return (0);
2073
2074 (void) taskq_dispatch(dp->dp_spa->spa_zvol_taskq, zvol_task_cb,
02730c33 2075 task, TQ_SLEEP);
a0bd735a
BP
2076 return (0);
2077}
2078
2079/*
2080 * Traverse all child snapshot datasets and apply snapdev appropriately.
2081 */
2082static void
2083zvol_set_snapdev_sync(void *arg, dmu_tx_t *tx)
2084{
2085 zvol_set_snapdev_arg_t *zsda = arg;
2086 dsl_pool_t *dp = dmu_tx_pool(tx);
2087 dsl_dir_t *dd;
2088
2089 VERIFY0(dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL));
2090 zsda->zsda_tx = tx;
2091
2092 dmu_objset_find_dp(dp, dd->dd_object, zvol_set_snapdev_sync_cb,
2093 zsda, DS_FIND_CHILDREN);
2094
2095 dsl_dir_rele(dd, FTAG);
2096}
2097
0b4d1b58 2098int
a0bd735a
BP
2099zvol_set_snapdev(const char *ddname, zprop_source_t source, uint64_t snapdev)
2100{
2101 zvol_set_snapdev_arg_t zsda;
5428dc51 2102
a0bd735a
BP
2103 zsda.zsda_name = ddname;
2104 zsda.zsda_source = source;
2105 zsda.zsda_value = snapdev;
5428dc51 2106
a0bd735a
BP
2107 return (dsl_sync_task(ddname, zvol_set_snapdev_check,
2108 zvol_set_snapdev_sync, &zsda, 0, ZFS_SPACE_CHECK_NONE));
2109}
2110
2111void
2112zvol_create_minors(spa_t *spa, const char *name, boolean_t async)
2113{
2114 zvol_task_t *task;
2115 taskqid_t id;
2116
2117 task = zvol_task_alloc(ZVOL_ASYNC_CREATE_MINORS, name, NULL, ~0ULL);
2118 if (task == NULL)
2119 return;
2120
2121 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
48d3eb40 2122 if ((async == B_FALSE) && (id != TASKQID_INVALID))
a0bd735a
BP
2123 taskq_wait_id(spa->spa_zvol_taskq, id);
2124}
2125
2126void
2127zvol_remove_minors(spa_t *spa, const char *name, boolean_t async)
2128{
2129 zvol_task_t *task;
2130 taskqid_t id;
2131
2132 task = zvol_task_alloc(ZVOL_ASYNC_REMOVE_MINORS, name, NULL, ~0ULL);
2133 if (task == NULL)
2134 return;
5428dc51 2135
a0bd735a 2136 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
48d3eb40 2137 if ((async == B_FALSE) && (id != TASKQID_INVALID))
a0bd735a
BP
2138 taskq_wait_id(spa->spa_zvol_taskq, id);
2139}
2140
2141void
2142zvol_rename_minors(spa_t *spa, const char *name1, const char *name2,
2143 boolean_t async)
2144{
2145 zvol_task_t *task;
2146 taskqid_t id;
2147
2148 task = zvol_task_alloc(ZVOL_ASYNC_RENAME_MINORS, name1, name2, ~0ULL);
2149 if (task == NULL)
2150 return;
2151
2152 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
48d3eb40 2153 if ((async == B_FALSE) && (id != TASKQID_INVALID))
a0bd735a 2154 taskq_wait_id(spa->spa_zvol_taskq, id);
0b4d1b58
ED
2155}
2156
60101509
BB
2157int
2158zvol_init(void)
2159{
d45e010d 2160 int i, error;
60101509 2161
2a3871d4 2162 list_create(&zvol_state_list, sizeof (zvol_state_t),
ce37ebd2 2163 offsetof(zvol_state_t, zv_next));
2a3871d4 2164 mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
4a5d7f82 2165 ida_init(&zvol_ida);
2a3871d4 2166
d45e010d
CC
2167 zvol_htable = kmem_alloc(ZVOL_HT_SIZE * sizeof (struct hlist_head),
2168 KM_SLEEP);
2169 if (!zvol_htable) {
2170 error = ENOMEM;
2171 goto out;
2172 }
2173 for (i = 0; i < ZVOL_HT_SIZE; i++)
2174 INIT_HLIST_HEAD(&zvol_htable[i]);
2175
60101509
BB
2176 error = register_blkdev(zvol_major, ZVOL_DRIVER);
2177 if (error) {
2178 printk(KERN_INFO "ZFS: register_blkdev() failed %d\n", error);
d45e010d 2179 goto out_free;
60101509
BB
2180 }
2181
2182 blk_register_region(MKDEV(zvol_major, 0), 1UL << MINORBITS,
ce37ebd2 2183 THIS_MODULE, zvol_probe, NULL, NULL);
60101509 2184
60101509 2185 return (0);
2a3871d4 2186
d45e010d
CC
2187out_free:
2188 kmem_free(zvol_htable, ZVOL_HT_SIZE * sizeof (struct hlist_head));
37f9dac5 2189out:
2a3871d4
RY
2190 mutex_destroy(&zvol_state_lock);
2191 list_destroy(&zvol_state_list);
2192
ce37ebd2 2193 return (SET_ERROR(error));
60101509
BB
2194}
2195
2196void
2197zvol_fini(void)
2198{
a0bd735a
BP
2199 zvol_remove_minors_impl(NULL);
2200
60101509
BB
2201 blk_unregister_region(MKDEV(zvol_major, 0), 1UL << MINORBITS);
2202 unregister_blkdev(zvol_major, ZVOL_DRIVER);
d45e010d 2203 kmem_free(zvol_htable, ZVOL_HT_SIZE * sizeof (struct hlist_head));
a0bd735a 2204
60101509 2205 list_destroy(&zvol_state_list);
a0bd735a 2206 mutex_destroy(&zvol_state_lock);
f2d8bdc6
CC
2207
2208 ida_destroy(&zvol_ida);
60101509
BB
2209}
2210
02730c33 2211/* BEGIN CSTYLED */
74497b7a
DH
2212module_param(zvol_inhibit_dev, uint, 0644);
2213MODULE_PARM_DESC(zvol_inhibit_dev, "Do not create zvol device nodes");
2214
30a9524e 2215module_param(zvol_major, uint, 0444);
60101509
BB
2216MODULE_PARM_DESC(zvol_major, "Major number for zvol device");
2217
7c0e5708 2218module_param(zvol_max_discard_blocks, ulong, 0444);
ce37ebd2 2219MODULE_PARM_DESC(zvol_max_discard_blocks, "Max number of blocks to discard");
9965059a
BB
2220
2221module_param(zvol_prefetch_bytes, uint, 0644);
2222MODULE_PARM_DESC(zvol_prefetch_bytes, "Prefetch N bytes at zvol start+end");
02730c33 2223/* END CSTYLED */