]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zvol.c
Fix uio_prefaultpages for 0 length iovec
[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.
36 */
37
03c6040b 38#include <sys/dbuf.h>
60101509
BB
39#include <sys/dmu_traverse.h>
40#include <sys/dsl_dataset.h>
41#include <sys/dsl_prop.h>
42#include <sys/zap.h>
4cb7b9c5 43#include <sys/zfeature.h>
60101509
BB
44#include <sys/zil_impl.h>
45#include <sys/zio.h>
46#include <sys/zfs_rlock.h>
47#include <sys/zfs_znode.h>
48#include <sys/zvol.h>
61e90960 49#include <linux/blkdev_compat.h>
60101509 50
74497b7a 51unsigned int zvol_inhibit_dev = 0;
60101509 52unsigned int zvol_major = ZVOL_MAJOR;
9965059a 53unsigned int zvol_prefetch_bytes = (128 * 1024);
7c0e5708 54unsigned long zvol_max_discard_blocks = 16384;
60101509 55
60101509
BB
56static kmutex_t zvol_state_lock;
57static list_t zvol_state_list;
58static char *zvol_tag = "zvol_tag";
59
60/*
61 * The in-core state of each volume.
62 */
63typedef struct zvol_state {
4c0d8e50 64 char zv_name[MAXNAMELEN]; /* name */
ce37ebd2
BB
65 uint64_t zv_volsize; /* advertised space */
66 uint64_t zv_volblocksize; /* volume block size */
60101509
BB
67 objset_t *zv_objset; /* objset handle */
68 uint32_t zv_flags; /* ZVOL_* flags */
69 uint32_t zv_open_count; /* open counts */
70 uint32_t zv_changed; /* disk changed */
71 zilog_t *zv_zilog; /* ZIL handle */
72 znode_t zv_znode; /* for range locking */
73 dmu_buf_t *zv_dbuf; /* bonus handle */
74 dev_t zv_dev; /* device id */
75 struct gendisk *zv_disk; /* generic disk */
76 struct request_queue *zv_queue; /* request queue */
60101509
BB
77 list_node_t zv_next; /* next zvol_state_t linkage */
78} zvol_state_t;
79
80#define ZVOL_RDONLY 0x1
81
82/*
83 * Find the next available range of ZVOL_MINORS minor numbers. The
84 * zvol_state_list is kept in ascending minor order so we simply need
85 * to scan the list for the first gap in the sequence. This allows us
86 * to recycle minor number as devices are created and removed.
87 */
88static int
89zvol_find_minor(unsigned *minor)
90{
91 zvol_state_t *zv;
92
93 *minor = 0;
94 ASSERT(MUTEX_HELD(&zvol_state_lock));
95 for (zv = list_head(&zvol_state_list); zv != NULL;
ce37ebd2 96 zv = list_next(&zvol_state_list, zv), *minor += ZVOL_MINORS) {
60101509
BB
97 if (MINOR(zv->zv_dev) != MINOR(*minor))
98 break;
99 }
100
101 /* All minors are in use */
102 if (*minor >= (1 << MINORBITS))
ce37ebd2 103 return (SET_ERROR(ENXIO));
60101509 104
ce37ebd2 105 return (0);
60101509
BB
106}
107
108/*
109 * Find a zvol_state_t given the full major+minor dev_t.
110 */
111static zvol_state_t *
112zvol_find_by_dev(dev_t dev)
113{
114 zvol_state_t *zv;
115
116 ASSERT(MUTEX_HELD(&zvol_state_lock));
117 for (zv = list_head(&zvol_state_list); zv != NULL;
ce37ebd2 118 zv = list_next(&zvol_state_list, zv)) {
60101509 119 if (zv->zv_dev == dev)
ce37ebd2 120 return (zv);
60101509
BB
121 }
122
ce37ebd2 123 return (NULL);
60101509
BB
124}
125
126/*
127 * Find a zvol_state_t given the name provided at zvol_alloc() time.
128 */
129static zvol_state_t *
130zvol_find_by_name(const char *name)
131{
132 zvol_state_t *zv;
133
134 ASSERT(MUTEX_HELD(&zvol_state_lock));
135 for (zv = list_head(&zvol_state_list); zv != NULL;
ce37ebd2
BB
136 zv = list_next(&zvol_state_list, zv)) {
137 if (strncmp(zv->zv_name, name, MAXNAMELEN) == 0)
138 return (zv);
60101509
BB
139 }
140
ce37ebd2 141 return (NULL);
60101509
BB
142}
143
6c285672
JL
144
145/*
146 * Given a path, return TRUE if path is a ZVOL.
147 */
148boolean_t
149zvol_is_zvol(const char *device)
150{
151 struct block_device *bdev;
152 unsigned int major;
153
154 bdev = lookup_bdev(device);
155 if (IS_ERR(bdev))
156 return (B_FALSE);
157
158 major = MAJOR(bdev->bd_dev);
159 bdput(bdev);
160
161 if (major == zvol_major)
ce37ebd2 162 return (B_TRUE);
6c285672
JL
163
164 return (B_FALSE);
165}
166
60101509
BB
167/*
168 * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation.
169 */
170void
171zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
172{
173 zfs_creat_t *zct = arg;
174 nvlist_t *nvprops = zct->zct_props;
175 int error;
176 uint64_t volblocksize, volsize;
177
178 VERIFY(nvlist_lookup_uint64(nvprops,
179 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
180 if (nvlist_lookup_uint64(nvprops,
181 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
182 volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
183
184 /*
185 * These properties must be removed from the list so the generic
186 * property setting step won't apply to them.
187 */
188 VERIFY(nvlist_remove_all(nvprops,
189 zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
190 (void) nvlist_remove_all(nvprops,
191 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
192
193 error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
194 DMU_OT_NONE, 0, tx);
195 ASSERT(error == 0);
196
197 error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
198 DMU_OT_NONE, 0, tx);
199 ASSERT(error == 0);
200
201 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
202 ASSERT(error == 0);
203}
204
205/*
206 * ZFS_IOC_OBJSET_STATS entry point.
207 */
208int
209zvol_get_stats(objset_t *os, nvlist_t *nv)
210{
211 int error;
212 dmu_object_info_t *doi;
213 uint64_t val;
214
215 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
216 if (error)
ce37ebd2 217 return (SET_ERROR(error));
60101509
BB
218
219 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
ce37ebd2 220 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
60101509
BB
221 error = dmu_object_info(os, ZVOL_OBJ, doi);
222
223 if (error == 0) {
224 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
225 doi->doi_data_block_size);
226 }
227
ce37ebd2 228 kmem_free(doi, sizeof (dmu_object_info_t));
60101509 229
ce37ebd2 230 return (SET_ERROR(error));
60101509
BB
231}
232
35d3e322
BB
233static void
234zvol_size_changed(zvol_state_t *zv, uint64_t volsize)
235{
236 struct block_device *bdev;
237
238 bdev = bdget_disk(zv->zv_disk, 0);
239 if (bdev == NULL)
240 return;
241/*
242 * 2.6.28 API change
243 * Added check_disk_size_change() helper function.
244 */
245#ifdef HAVE_CHECK_DISK_SIZE_CHANGE
246 set_capacity(zv->zv_disk, volsize >> 9);
247 zv->zv_volsize = volsize;
248 check_disk_size_change(zv->zv_disk, bdev);
249#else
250 zv->zv_volsize = volsize;
251 zv->zv_changed = 1;
252 (void) check_disk_change(bdev);
253#endif /* HAVE_CHECK_DISK_SIZE_CHANGE */
254
255 bdput(bdev);
256}
257
60101509
BB
258/*
259 * Sanity check volume size.
260 */
261int
262zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
263{
264 if (volsize == 0)
2e528b49 265 return (SET_ERROR(EINVAL));
60101509
BB
266
267 if (volsize % blocksize != 0)
2e528b49 268 return (SET_ERROR(EINVAL));
60101509
BB
269
270#ifdef _ILP32
271 if (volsize - 1 > MAXOFFSET_T)
2e528b49 272 return (SET_ERROR(EOVERFLOW));
60101509
BB
273#endif
274 return (0);
275}
276
277/*
278 * Ensure the zap is flushed then inform the VFS of the capacity change.
279 */
280static int
35d3e322 281zvol_update_volsize(uint64_t volsize, objset_t *os)
60101509 282{
60101509
BB
283 dmu_tx_t *tx;
284 int error;
285
286 ASSERT(MUTEX_HELD(&zvol_state_lock));
287
df554c14 288 tx = dmu_tx_create(os);
60101509
BB
289 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
290 error = dmu_tx_assign(tx, TXG_WAIT);
291 if (error) {
292 dmu_tx_abort(tx);
ce37ebd2 293 return (SET_ERROR(error));
60101509
BB
294 }
295
df554c14 296 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
60101509
BB
297 &volsize, tx);
298 dmu_tx_commit(tx);
299
35d3e322
BB
300 if (error == 0)
301 error = dmu_free_long_range(os,
302 ZVOL_OBJ, volsize, DMU_OBJECT_END);
60101509 303
35d3e322
BB
304 return (error);
305}
60101509 306
35d3e322
BB
307static int
308zvol_update_live_volsize(zvol_state_t *zv, uint64_t volsize)
309{
310 zvol_size_changed(zv, volsize);
60101509 311
35d3e322
BB
312 /*
313 * We should post a event here describing the expansion. However,
314 * the zfs_ereport_post() interface doesn't nicely support posting
315 * events for zvols, it assumes events relate to vdevs or zios.
316 */
60101509
BB
317
318 return (0);
319}
320
321/*
322 * Set ZFS_PROP_VOLSIZE set entry point.
323 */
324int
325zvol_set_volsize(const char *name, uint64_t volsize)
326{
35d3e322 327 zvol_state_t *zv = NULL;
60101509 328 objset_t *os = NULL;
60101509 329 int error;
35d3e322
BB
330 dmu_object_info_t *doi;
331 uint64_t readonly;
332 boolean_t owned = B_FALSE;
60101509 333
13fe0198
MA
334 error = dsl_prop_get_integer(name,
335 zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL);
336 if (error != 0)
ce37ebd2 337 return (SET_ERROR(error));
13fe0198 338 if (readonly)
2e528b49 339 return (SET_ERROR(EROFS));
13fe0198 340
60101509 341 mutex_enter(&zvol_state_lock);
60101509 342 zv = zvol_find_by_name(name);
35d3e322
BB
343
344 if (zv == NULL || zv->zv_objset == NULL) {
345 if ((error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE,
346 FTAG, &os)) != 0) {
347 mutex_exit(&zvol_state_lock);
348 return (SET_ERROR(error));
349 }
350 owned = B_TRUE;
351 if (zv != NULL)
352 zv->zv_objset = os;
353 } else {
354 os = zv->zv_objset;
60101509
BB
355 }
356
ce37ebd2 357 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
60101509 358
ce37ebd2
BB
359 if ((error = dmu_object_info(os, ZVOL_OBJ, doi)) ||
360 (error = zvol_check_volsize(volsize, doi->doi_data_block_size)))
35d3e322 361 goto out;
60101509 362
35d3e322 363 error = zvol_update_volsize(volsize, os);
ce37ebd2 364 kmem_free(doi, sizeof (dmu_object_info_t));
60101509 365
35d3e322
BB
366 if (error == 0 && zv != NULL)
367 error = zvol_update_live_volsize(zv, volsize);
368out:
369 if (owned) {
370 dmu_objset_disown(os, FTAG);
371 if (zv != NULL)
372 zv->zv_objset = NULL;
373 }
60101509 374 mutex_exit(&zvol_state_lock);
35d3e322 375 return (error);
60101509
BB
376}
377
378/*
379 * Sanity check volume block size.
380 */
381int
4cb7b9c5 382zvol_check_volblocksize(const char *name, uint64_t volblocksize)
60101509 383{
4cb7b9c5
BB
384 /* Record sizes above 128k need the feature to be enabled */
385 if (volblocksize > SPA_OLD_MAXBLOCKSIZE) {
386 spa_t *spa;
387 int error;
388
389 if ((error = spa_open(name, &spa, FTAG)) != 0)
390 return (error);
391
392 if (!spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
393 spa_close(spa, FTAG);
394 return (SET_ERROR(ENOTSUP));
395 }
396
397 /*
398 * We don't allow setting the property above 1MB,
399 * unless the tunable has been changed.
400 */
401 if (volblocksize > zfs_max_recordsize)
402 return (SET_ERROR(EDOM));
403
404 spa_close(spa, FTAG);
405 }
406
60101509
BB
407 if (volblocksize < SPA_MINBLOCKSIZE ||
408 volblocksize > SPA_MAXBLOCKSIZE ||
409 !ISP2(volblocksize))
2e528b49 410 return (SET_ERROR(EDOM));
60101509
BB
411
412 return (0);
413}
414
415/*
416 * Set ZFS_PROP_VOLBLOCKSIZE set entry point.
417 */
418int
419zvol_set_volblocksize(const char *name, uint64_t volblocksize)
420{
421 zvol_state_t *zv;
422 dmu_tx_t *tx;
423 int error;
424
425 mutex_enter(&zvol_state_lock);
426
427 zv = zvol_find_by_name(name);
428 if (zv == NULL) {
2e528b49 429 error = SET_ERROR(ENXIO);
60101509
BB
430 goto out;
431 }
432
ba6a2402 433 if (zv->zv_flags & ZVOL_RDONLY) {
2e528b49 434 error = SET_ERROR(EROFS);
60101509
BB
435 goto out;
436 }
437
438 tx = dmu_tx_create(zv->zv_objset);
439 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
440 error = dmu_tx_assign(tx, TXG_WAIT);
441 if (error) {
442 dmu_tx_abort(tx);
443 } else {
444 error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
445 volblocksize, 0, tx);
446 if (error == ENOTSUP)
2e528b49 447 error = SET_ERROR(EBUSY);
60101509
BB
448 dmu_tx_commit(tx);
449 if (error == 0)
450 zv->zv_volblocksize = volblocksize;
451 }
452out:
453 mutex_exit(&zvol_state_lock);
454
ce37ebd2 455 return (SET_ERROR(error));
60101509
BB
456}
457
458/*
459 * Replay a TX_WRITE ZIL transaction that didn't get committed
460 * after a system failure
461 */
462static int
463zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
464{
465 objset_t *os = zv->zv_objset;
466 char *data = (char *)(lr + 1); /* data follows lr_write_t */
467 uint64_t off = lr->lr_offset;
468 uint64_t len = lr->lr_length;
469 dmu_tx_t *tx;
470 int error;
471
472 if (byteswap)
473 byteswap_uint64_array(lr, sizeof (*lr));
474
475 tx = dmu_tx_create(os);
476 dmu_tx_hold_write(tx, ZVOL_OBJ, off, len);
477 error = dmu_tx_assign(tx, TXG_WAIT);
478 if (error) {
479 dmu_tx_abort(tx);
480 } else {
481 dmu_write(os, ZVOL_OBJ, off, len, data, tx);
482 dmu_tx_commit(tx);
483 }
484
ce37ebd2 485 return (SET_ERROR(error));
60101509
BB
486}
487
488static int
489zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
490{
2e528b49 491 return (SET_ERROR(ENOTSUP));
60101509
BB
492}
493
494/*
495 * Callback vectors for replaying records.
496 * Only TX_WRITE is needed for zvol.
497 */
b01615d5
RY
498zil_replay_func_t zvol_replay_vector[TX_MAX_TYPE] = {
499 (zil_replay_func_t)zvol_replay_err, /* no such transaction type */
500 (zil_replay_func_t)zvol_replay_err, /* TX_CREATE */
501 (zil_replay_func_t)zvol_replay_err, /* TX_MKDIR */
502 (zil_replay_func_t)zvol_replay_err, /* TX_MKXATTR */
503 (zil_replay_func_t)zvol_replay_err, /* TX_SYMLINK */
504 (zil_replay_func_t)zvol_replay_err, /* TX_REMOVE */
505 (zil_replay_func_t)zvol_replay_err, /* TX_RMDIR */
506 (zil_replay_func_t)zvol_replay_err, /* TX_LINK */
507 (zil_replay_func_t)zvol_replay_err, /* TX_RENAME */
508 (zil_replay_func_t)zvol_replay_write, /* TX_WRITE */
509 (zil_replay_func_t)zvol_replay_err, /* TX_TRUNCATE */
510 (zil_replay_func_t)zvol_replay_err, /* TX_SETATTR */
511 (zil_replay_func_t)zvol_replay_err, /* TX_ACL */
60101509
BB
512};
513
514/*
515 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
516 *
517 * We store data in the log buffers if it's small enough.
518 * Otherwise we will later flush the data out via dmu_sync().
519 */
520ssize_t zvol_immediate_write_sz = 32768;
521
522static void
ce37ebd2
BB
523zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, uint64_t offset,
524 uint64_t size, int sync)
60101509
BB
525{
526 uint32_t blocksize = zv->zv_volblocksize;
527 zilog_t *zilog = zv->zv_zilog;
528 boolean_t slogging;
ab85f845 529 ssize_t immediate_write_sz;
60101509
BB
530
531 if (zil_replaying(zilog, tx))
532 return;
533
ab85f845
ED
534 immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
535 ? 0 : zvol_immediate_write_sz;
536 slogging = spa_has_slogs(zilog->zl_spa) &&
537 (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
60101509
BB
538
539 while (size) {
540 itx_t *itx;
541 lr_write_t *lr;
542 ssize_t len;
543 itx_wr_state_t write_state;
544
545 /*
546 * Unlike zfs_log_write() we can be called with
547 * up to DMU_MAX_ACCESS/2 (5MB) writes.
548 */
ab85f845 549 if (blocksize > immediate_write_sz && !slogging &&
60101509
BB
550 size >= blocksize && offset % blocksize == 0) {
551 write_state = WR_INDIRECT; /* uses dmu_sync */
552 len = blocksize;
553 } else if (sync) {
554 write_state = WR_COPIED;
555 len = MIN(ZIL_MAX_LOG_DATA, size);
556 } else {
557 write_state = WR_NEED_COPY;
558 len = MIN(ZIL_MAX_LOG_DATA, size);
559 }
560
561 itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
562 (write_state == WR_COPIED ? len : 0));
563 lr = (lr_write_t *)&itx->itx_lr;
564 if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
565 ZVOL_OBJ, offset, len, lr+1, DMU_READ_NO_PREFETCH) != 0) {
566 zil_itx_destroy(itx);
567 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
568 lr = (lr_write_t *)&itx->itx_lr;
569 write_state = WR_NEED_COPY;
570 }
571
572 itx->itx_wr_state = write_state;
573 if (write_state == WR_NEED_COPY)
574 itx->itx_sod += len;
575 lr->lr_foid = ZVOL_OBJ;
576 lr->lr_offset = offset;
577 lr->lr_length = len;
578 lr->lr_blkoff = 0;
579 BP_ZERO(&lr->lr_blkptr);
580
581 itx->itx_private = zv;
582 itx->itx_sync = sync;
583
584 (void) zil_itx_assign(zilog, itx, tx);
585
586 offset += len;
587 size -= len;
588 }
589}
590
37f9dac5
RY
591static int
592zvol_write(struct bio *bio)
60101509 593{
37f9dac5
RY
594 zvol_state_t *zv = bio->bi_bdev->bd_disk->private_data;
595 uint64_t offset = BIO_BI_SECTOR(bio) << 9;
596 uint64_t size = BIO_BI_SIZE(bio);
60101509
BB
597 int error = 0;
598 dmu_tx_t *tx;
599 rl_t *rl;
600
37f9dac5 601 if (bio->bi_rw & VDEV_REQ_FLUSH)
b18019d2
ED
602 zil_commit(zv->zv_zilog, ZVOL_OBJ);
603
604 /*
605 * Some requests are just for flush and nothing else.
606 */
37f9dac5 607 if (size == 0)
8630650a 608 goto out;
b18019d2 609
60101509
BB
610 rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_WRITER);
611
612 tx = dmu_tx_create(zv->zv_objset);
613 dmu_tx_hold_write(tx, ZVOL_OBJ, offset, size);
614
615 /* This will only fail for ENOSPC */
616 error = dmu_tx_assign(tx, TXG_WAIT);
617 if (error) {
618 dmu_tx_abort(tx);
619 zfs_range_unlock(rl);
8630650a 620 goto out;
60101509
BB
621 }
622
37f9dac5 623 error = dmu_write_bio(zv->zv_objset, ZVOL_OBJ, bio, tx);
60101509 624 if (error == 0)
b18019d2 625 zvol_log_write(zv, tx, offset, size,
37f9dac5 626 !!(bio->bi_rw & VDEV_REQ_FUA));
60101509
BB
627
628 dmu_tx_commit(tx);
629 zfs_range_unlock(rl);
630
37f9dac5 631 if ((bio->bi_rw & VDEV_REQ_FUA) ||
b18019d2 632 zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)
60101509
BB
633 zil_commit(zv->zv_zilog, ZVOL_OBJ);
634
8630650a 635out:
37f9dac5 636 return (error);
60101509
BB
637}
638
37f9dac5
RY
639static int
640zvol_discard(struct bio *bio)
30930fba 641{
37f9dac5
RY
642 zvol_state_t *zv = bio->bi_bdev->bd_disk->private_data;
643 uint64_t start = BIO_BI_SECTOR(bio) << 9;
644 uint64_t size = BIO_BI_SIZE(bio);
645 uint64_t end = start + size;
30930fba
ED
646 int error;
647 rl_t *rl;
648
37f9dac5
RY
649 if (end > zv->zv_volsize)
650 return (SET_ERROR(EIO));
30930fba 651
089fa91b 652 /*
fa565676
RY
653 * Align the request to volume block boundaries when REQ_SECURE is
654 * available, but not requested. If we don't, then this will force
655 * dnode_free_range() to zero out the unaligned parts, which is slow
656 * (read-modify-write) and useless since we are not freeing any space
657 * by doing so. Kernels that do not support REQ_SECURE (2.6.32 through
658 * 2.6.35) will not receive this optimization.
089fa91b 659 */
fa565676
RY
660#ifdef REQ_SECURE
661 if (!(bio->bi_rw & REQ_SECURE)) {
662 start = P2ROUNDUP(start, zv->zv_volblocksize);
663 end = P2ALIGN(end, zv->zv_volblocksize);
f52ebcb3 664 size = end - start;
fa565676
RY
665 }
666#endif
089fa91b 667
37f9dac5
RY
668 if (start >= end)
669 return (0);
30930fba 670
37f9dac5 671 rl = zfs_range_lock(&zv->zv_znode, start, size, RL_WRITER);
30930fba 672
37f9dac5 673 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, start, size);
30930fba
ED
674
675 /*
676 * TODO: maybe we should add the operation to the log.
677 */
678
679 zfs_range_unlock(rl);
37f9dac5
RY
680
681 return (error);
30930fba 682}
30930fba 683
37f9dac5
RY
684static int
685zvol_read(struct bio *bio)
60101509 686{
37f9dac5
RY
687 zvol_state_t *zv = bio->bi_bdev->bd_disk->private_data;
688 uint64_t offset = BIO_BI_SECTOR(bio) << 9;
689 uint64_t len = BIO_BI_SIZE(bio);
60101509
BB
690 int error;
691 rl_t *rl;
692
37f9dac5
RY
693 if (len == 0)
694 return (0);
695
b18019d2 696
37f9dac5 697 rl = zfs_range_lock(&zv->zv_znode, offset, len, RL_READER);
60101509 698
37f9dac5 699 error = dmu_read_bio(zv->zv_objset, ZVOL_OBJ, bio);
60101509
BB
700
701 zfs_range_unlock(rl);
702
703 /* convert checksum errors into IO errors */
704 if (error == ECKSUM)
2e528b49 705 error = SET_ERROR(EIO);
60101509 706
37f9dac5 707 return (error);
60101509
BB
708}
709
37f9dac5
RY
710static MAKE_REQUEST_FN_RET
711zvol_request(struct request_queue *q, struct bio *bio)
60101509
BB
712{
713 zvol_state_t *zv = q->queuedata;
37f9dac5
RY
714 fstrans_cookie_t cookie = spl_fstrans_mark();
715 uint64_t offset = BIO_BI_SECTOR(bio);
716 unsigned int sectors = bio_sectors(bio);
8198d18c
RY
717 int rw = bio_data_dir(bio);
718#ifdef HAVE_GENERIC_IO_ACCT
719 unsigned long start = jiffies;
720#endif
37f9dac5 721 int error = 0;
60101509 722
37f9dac5
RY
723 if (bio_has_data(bio) && offset + sectors >
724 get_capacity(zv->zv_disk)) {
725 printk(KERN_INFO
726 "%s: bad access: block=%llu, count=%lu\n",
727 zv->zv_disk->disk_name,
728 (long long unsigned)offset,
729 (long unsigned)sectors);
730 error = SET_ERROR(EIO);
8198d18c 731 goto out1;
37f9dac5
RY
732 }
733
8198d18c
RY
734 generic_start_io_acct(rw, sectors, &zv->zv_disk->part0);
735
736 if (rw == WRITE) {
37f9dac5
RY
737 if (unlikely(zv->zv_flags & ZVOL_RDONLY)) {
738 error = SET_ERROR(EROFS);
8198d18c 739 goto out2;
60101509
BB
740 }
741
37f9dac5
RY
742 if (bio->bi_rw & VDEV_REQ_DISCARD) {
743 error = zvol_discard(bio);
8198d18c 744 goto out2;
37f9dac5 745 }
60101509 746
37f9dac5
RY
747 error = zvol_write(bio);
748 } else
749 error = zvol_read(bio);
30930fba 750
8198d18c
RY
751out2:
752 generic_end_io_acct(rw, &zv->zv_disk->part0, start);
753out1:
784a7fe5 754 BIO_END_IO(bio, -error);
37f9dac5
RY
755 spl_fstrans_unmark(cookie);
756#ifdef HAVE_MAKE_REQUEST_FN_RET_INT
757 return (0);
1a093716
CC
758#elif defined(HAVE_MAKE_REQUEST_FN_RET_QC)
759 return (BLK_QC_T_NONE);
37f9dac5 760#endif
60101509
BB
761}
762
763static void
764zvol_get_done(zgd_t *zgd, int error)
765{
766 if (zgd->zgd_db)
767 dmu_buf_rele(zgd->zgd_db, zgd);
768
769 zfs_range_unlock(zgd->zgd_rl);
770
771 if (error == 0 && zgd->zgd_bp)
772 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
773
774 kmem_free(zgd, sizeof (zgd_t));
775}
776
777/*
778 * Get data to generate a TX_WRITE intent log record.
779 */
780static int
781zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
782{
783 zvol_state_t *zv = arg;
784 objset_t *os = zv->zv_objset;
03c6040b 785 uint64_t object = ZVOL_OBJ;
60101509
BB
786 uint64_t offset = lr->lr_offset;
787 uint64_t size = lr->lr_length;
03c6040b 788 blkptr_t *bp = &lr->lr_blkptr;
60101509
BB
789 dmu_buf_t *db;
790 zgd_t *zgd;
791 int error;
792
793 ASSERT(zio != NULL);
794 ASSERT(size != 0);
795
79c76d5b 796 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
60101509
BB
797 zgd->zgd_zilog = zv->zv_zilog;
798 zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
799
800 /*
801 * Write records come in two flavors: immediate and indirect.
802 * For small writes it's cheaper to store the data with the
803 * log record (immediate); for large writes it's cheaper to
804 * sync the data and get a pointer to it (indirect) so that
805 * we don't have to write the data twice.
806 */
807 if (buf != NULL) { /* immediate write */
03c6040b 808 error = dmu_read(os, object, offset, size, buf,
60101509
BB
809 DMU_READ_NO_PREFETCH);
810 } else {
811 size = zv->zv_volblocksize;
812 offset = P2ALIGN_TYPED(offset, size, uint64_t);
03c6040b 813 error = dmu_buf_hold(os, object, offset, zgd, &db,
60101509
BB
814 DMU_READ_NO_PREFETCH);
815 if (error == 0) {
03c6040b
GW
816 blkptr_t *obp = dmu_buf_get_blkptr(db);
817 if (obp) {
818 ASSERT(BP_IS_HOLE(bp));
819 *bp = *obp;
820 }
821
60101509
BB
822 zgd->zgd_db = db;
823 zgd->zgd_bp = &lr->lr_blkptr;
824
825 ASSERT(db != NULL);
826 ASSERT(db->db_offset == offset);
827 ASSERT(db->db_size == size);
828
829 error = dmu_sync(zio, lr->lr_common.lrc_txg,
830 zvol_get_done, zgd);
831
832 if (error == 0)
833 return (0);
834 }
835 }
836
837 zvol_get_done(zgd, error);
838
ce37ebd2 839 return (SET_ERROR(error));
60101509
BB
840}
841
842/*
843 * The zvol_state_t's are inserted in increasing MINOR(dev_t) order.
844 */
845static void
846zvol_insert(zvol_state_t *zv_insert)
847{
848 zvol_state_t *zv = NULL;
849
850 ASSERT(MUTEX_HELD(&zvol_state_lock));
851 ASSERT3U(MINOR(zv_insert->zv_dev) & ZVOL_MINOR_MASK, ==, 0);
852 for (zv = list_head(&zvol_state_list); zv != NULL;
ce37ebd2 853 zv = list_next(&zvol_state_list, zv)) {
60101509
BB
854 if (MINOR(zv->zv_dev) > MINOR(zv_insert->zv_dev))
855 break;
856 }
857
858 list_insert_before(&zvol_state_list, zv, zv_insert);
859}
860
861/*
862 * Simply remove the zvol from to list of zvols.
863 */
864static void
865zvol_remove(zvol_state_t *zv_remove)
866{
867 ASSERT(MUTEX_HELD(&zvol_state_lock));
868 list_remove(&zvol_state_list, zv_remove);
869}
870
871static int
872zvol_first_open(zvol_state_t *zv)
873{
874 objset_t *os;
875 uint64_t volsize;
65d56083 876 int locked = 0;
60101509
BB
877 int error;
878 uint64_t ro;
879
65d56083
BB
880 /*
881 * In all other cases the spa_namespace_lock is taken before the
882 * bdev->bd_mutex lock. But in this case the Linux __blkdev_get()
883 * function calls fops->open() with the bdev->bd_mutex lock held.
884 *
885 * To avoid a potential lock inversion deadlock we preemptively
886 * try to take the spa_namespace_lock(). Normally it will not
887 * be contended and this is safe because spa_open_common() handles
888 * the case where the caller already holds the spa_namespace_lock.
889 *
890 * When it is contended we risk a lock inversion if we were to
891 * block waiting for the lock. Luckily, the __blkdev_get()
892 * function allows us to return -ERESTARTSYS which will result in
893 * bdev->bd_mutex being dropped, reacquired, and fops->open() being
894 * called again. This process can be repeated safely until both
895 * locks are acquired.
896 */
897 if (!mutex_owned(&spa_namespace_lock)) {
898 locked = mutex_tryenter(&spa_namespace_lock);
899 if (!locked)
2e528b49 900 return (-SET_ERROR(ERESTARTSYS));
65d56083
BB
901 }
902
a127e841
BB
903 error = dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL);
904 if (error)
905 goto out_mutex;
906
60101509
BB
907 /* lie and say we're read-only */
908 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, 1, zvol_tag, &os);
909 if (error)
babf3f9b 910 goto out_mutex;
60101509
BB
911
912 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
913 if (error) {
babf3f9b
MM
914 dmu_objset_disown(os, zvol_tag);
915 goto out_mutex;
60101509
BB
916 }
917
918 zv->zv_objset = os;
919 error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
920 if (error) {
babf3f9b
MM
921 dmu_objset_disown(os, zvol_tag);
922 goto out_mutex;
60101509
BB
923 }
924
925 set_capacity(zv->zv_disk, volsize >> 9);
926 zv->zv_volsize = volsize;
927 zv->zv_zilog = zil_open(os, zvol_get_data);
928
a4430fce
GW
929 if (ro || dmu_objset_is_snapshot(os) ||
930 !spa_writeable(dmu_objset_spa(os))) {
babf3f9b
MM
931 set_disk_ro(zv->zv_disk, 1);
932 zv->zv_flags |= ZVOL_RDONLY;
60101509 933 } else {
babf3f9b
MM
934 set_disk_ro(zv->zv_disk, 0);
935 zv->zv_flags &= ~ZVOL_RDONLY;
60101509
BB
936 }
937
babf3f9b
MM
938out_mutex:
939 if (locked)
940 mutex_exit(&spa_namespace_lock);
941
ce37ebd2 942 return (SET_ERROR(-error));
60101509
BB
943}
944
945static void
946zvol_last_close(zvol_state_t *zv)
947{
948 zil_close(zv->zv_zilog);
949 zv->zv_zilog = NULL;
04434775 950
60101509
BB
951 dmu_buf_rele(zv->zv_dbuf, zvol_tag);
952 zv->zv_dbuf = NULL;
04434775
MA
953
954 /*
955 * Evict cached data
956 */
957 if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) &&
958 !(zv->zv_flags & ZVOL_RDONLY))
959 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
960 (void) dmu_objset_evict_dbufs(zv->zv_objset);
961
60101509
BB
962 dmu_objset_disown(zv->zv_objset, zvol_tag);
963 zv->zv_objset = NULL;
964}
965
966static int
967zvol_open(struct block_device *bdev, fmode_t flag)
968{
969 zvol_state_t *zv = bdev->bd_disk->private_data;
970 int error = 0, drop_mutex = 0;
971
972 /*
973 * If the caller is already holding the mutex do not take it
974 * again, this will happen as part of zvol_create_minor().
975 * Once add_disk() is called the device is live and the kernel
976 * will attempt to open it to read the partition information.
977 */
978 if (!mutex_owned(&zvol_state_lock)) {
979 mutex_enter(&zvol_state_lock);
980 drop_mutex = 1;
981 }
982
983 ASSERT3P(zv, !=, NULL);
984
985 if (zv->zv_open_count == 0) {
986 error = zvol_first_open(zv);
987 if (error)
988 goto out_mutex;
989 }
990
ba6a2402 991 if ((flag & FMODE_WRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
60101509
BB
992 error = -EROFS;
993 goto out_open_count;
994 }
995
996 zv->zv_open_count++;
997
998out_open_count:
999 if (zv->zv_open_count == 0)
1000 zvol_last_close(zv);
1001
1002out_mutex:
1003 if (drop_mutex)
1004 mutex_exit(&zvol_state_lock);
1005
1006 check_disk_change(bdev);
1007
ce37ebd2 1008 return (SET_ERROR(error));
60101509
BB
1009}
1010
a1d9543a
CD
1011#ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1012static void
1013#else
60101509 1014static int
a1d9543a 1015#endif
60101509
BB
1016zvol_release(struct gendisk *disk, fmode_t mode)
1017{
1018 zvol_state_t *zv = disk->private_data;
1019 int drop_mutex = 0;
1020
1021 if (!mutex_owned(&zvol_state_lock)) {
1022 mutex_enter(&zvol_state_lock);
1023 drop_mutex = 1;
1024 }
1025
0365064a
BB
1026 if (zv->zv_open_count > 0) {
1027 zv->zv_open_count--;
1028 if (zv->zv_open_count == 0)
1029 zvol_last_close(zv);
1030 }
60101509
BB
1031
1032 if (drop_mutex)
1033 mutex_exit(&zvol_state_lock);
1034
a1d9543a 1035#ifndef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
60101509 1036 return (0);
a1d9543a 1037#endif
60101509
BB
1038}
1039
1040static int
1041zvol_ioctl(struct block_device *bdev, fmode_t mode,
ce37ebd2 1042 unsigned int cmd, unsigned long arg)
60101509
BB
1043{
1044 zvol_state_t *zv = bdev->bd_disk->private_data;
1045 int error = 0;
1046
1047 if (zv == NULL)
ce37ebd2 1048 return (SET_ERROR(-ENXIO));
60101509
BB
1049
1050 switch (cmd) {
1051 case BLKFLSBUF:
1052 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1053 break;
4c0d8e50
FN
1054 case BLKZNAME:
1055 error = copy_to_user((void *)arg, zv->zv_name, MAXNAMELEN);
1056 break;
60101509
BB
1057
1058 default:
1059 error = -ENOTTY;
1060 break;
1061
1062 }
1063
ce37ebd2 1064 return (SET_ERROR(error));
60101509
BB
1065}
1066
1067#ifdef CONFIG_COMPAT
1068static int
1069zvol_compat_ioctl(struct block_device *bdev, fmode_t mode,
ce37ebd2 1070 unsigned cmd, unsigned long arg)
60101509 1071{
ce37ebd2 1072 return (zvol_ioctl(bdev, mode, cmd, arg));
60101509
BB
1073}
1074#else
ce37ebd2 1075#define zvol_compat_ioctl NULL
60101509
BB
1076#endif
1077
1078static int zvol_media_changed(struct gendisk *disk)
1079{
1080 zvol_state_t *zv = disk->private_data;
1081
ce37ebd2 1082 return (zv->zv_changed);
60101509
BB
1083}
1084
1085static int zvol_revalidate_disk(struct gendisk *disk)
1086{
1087 zvol_state_t *zv = disk->private_data;
1088
1089 zv->zv_changed = 0;
1090 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1091
ce37ebd2 1092 return (0);
60101509
BB
1093}
1094
1095/*
1096 * Provide a simple virtual geometry for legacy compatibility. For devices
1097 * smaller than 1 MiB a small head and sector count is used to allow very
1098 * tiny devices. For devices over 1 Mib a standard head and sector count
1099 * is used to keep the cylinders count reasonable.
1100 */
1101static int
1102zvol_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1103{
1104 zvol_state_t *zv = bdev->bd_disk->private_data;
1105 sector_t sectors = get_capacity(zv->zv_disk);
1106
1107 if (sectors > 2048) {
1108 geo->heads = 16;
1109 geo->sectors = 63;
1110 } else {
1111 geo->heads = 2;
1112 geo->sectors = 4;
1113 }
1114
1115 geo->start = 0;
1116 geo->cylinders = sectors / (geo->heads * geo->sectors);
1117
ce37ebd2 1118 return (0);
60101509
BB
1119}
1120
1121static struct kobject *
1122zvol_probe(dev_t dev, int *part, void *arg)
1123{
1124 zvol_state_t *zv;
1125 struct kobject *kobj;
1126
1127 mutex_enter(&zvol_state_lock);
1128 zv = zvol_find_by_dev(dev);
23a61ccc 1129 kobj = zv ? get_disk(zv->zv_disk) : NULL;
60101509
BB
1130 mutex_exit(&zvol_state_lock);
1131
ce37ebd2 1132 return (kobj);
60101509
BB
1133}
1134
1135#ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS
1136static struct block_device_operations zvol_ops = {
ce37ebd2
BB
1137 .open = zvol_open,
1138 .release = zvol_release,
1139 .ioctl = zvol_ioctl,
1140 .compat_ioctl = zvol_compat_ioctl,
1141 .media_changed = zvol_media_changed,
1142 .revalidate_disk = zvol_revalidate_disk,
1143 .getgeo = zvol_getgeo,
1144 .owner = THIS_MODULE,
60101509
BB
1145};
1146
1147#else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1148
1149static int
1150zvol_open_by_inode(struct inode *inode, struct file *file)
1151{
ce37ebd2 1152 return (zvol_open(inode->i_bdev, file->f_mode));
60101509
BB
1153}
1154
1155static int
1156zvol_release_by_inode(struct inode *inode, struct file *file)
1157{
ce37ebd2 1158 return (zvol_release(inode->i_bdev->bd_disk, file->f_mode));
60101509
BB
1159}
1160
1161static int
1162zvol_ioctl_by_inode(struct inode *inode, struct file *file,
ce37ebd2 1163 unsigned int cmd, unsigned long arg)
60101509 1164{
b1c58213 1165 if (file == NULL || inode == NULL)
ce37ebd2
BB
1166 return (SET_ERROR(-EINVAL));
1167
1168 return (zvol_ioctl(inode->i_bdev, file->f_mode, cmd, arg));
60101509
BB
1169}
1170
ce37ebd2 1171#ifdef CONFIG_COMPAT
60101509
BB
1172static long
1173zvol_compat_ioctl_by_inode(struct file *file,
ce37ebd2 1174 unsigned int cmd, unsigned long arg)
60101509 1175{
b1c58213 1176 if (file == NULL)
ce37ebd2
BB
1177 return (SET_ERROR(-EINVAL));
1178
1179 return (zvol_compat_ioctl(file->f_dentry->d_inode->i_bdev,
1180 file->f_mode, cmd, arg));
60101509 1181}
ce37ebd2
BB
1182#else
1183#define zvol_compat_ioctl_by_inode NULL
1184#endif
60101509
BB
1185
1186static struct block_device_operations zvol_ops = {
ce37ebd2
BB
1187 .open = zvol_open_by_inode,
1188 .release = zvol_release_by_inode,
1189 .ioctl = zvol_ioctl_by_inode,
1190 .compat_ioctl = zvol_compat_ioctl_by_inode,
1191 .media_changed = zvol_media_changed,
1192 .revalidate_disk = zvol_revalidate_disk,
1193 .getgeo = zvol_getgeo,
1194 .owner = THIS_MODULE,
60101509
BB
1195};
1196#endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1197
1198/*
1199 * Allocate memory for a new zvol_state_t and setup the required
1200 * request queue and generic disk structures for the block device.
1201 */
1202static zvol_state_t *
1203zvol_alloc(dev_t dev, const char *name)
1204{
1205 zvol_state_t *zv;
1206
79c76d5b 1207 zv = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
60101509 1208
2a3871d4
RY
1209 list_link_init(&zv->zv_next);
1210
37f9dac5 1211 zv->zv_queue = blk_alloc_queue(GFP_ATOMIC);
60101509
BB
1212 if (zv->zv_queue == NULL)
1213 goto out_kmem;
1214
37f9dac5 1215 blk_queue_make_request(zv->zv_queue, zvol_request);
7bd04f2d 1216
b18019d2
ED
1217#ifdef HAVE_BLK_QUEUE_FLUSH
1218 blk_queue_flush(zv->zv_queue, VDEV_REQ_FLUSH | VDEV_REQ_FUA);
1219#else
1220 blk_queue_ordered(zv->zv_queue, QUEUE_ORDERED_DRAIN, NULL);
1221#endif /* HAVE_BLK_QUEUE_FLUSH */
1222
60101509
BB
1223 zv->zv_disk = alloc_disk(ZVOL_MINORS);
1224 if (zv->zv_disk == NULL)
1225 goto out_queue;
1226
1227 zv->zv_queue->queuedata = zv;
1228 zv->zv_dev = dev;
1229 zv->zv_open_count = 0;
4c0d8e50 1230 strlcpy(zv->zv_name, name, MAXNAMELEN);
60101509
BB
1231
1232 mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
1233 avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
1234 sizeof (rl_t), offsetof(rl_t, r_node));
3c4988c8
BB
1235 zv->zv_znode.z_is_zvol = TRUE;
1236
60101509
BB
1237 zv->zv_disk->major = zvol_major;
1238 zv->zv_disk->first_minor = (dev & MINORMASK);
1239 zv->zv_disk->fops = &zvol_ops;
1240 zv->zv_disk->private_data = zv;
1241 zv->zv_disk->queue = zv->zv_queue;
4c0d8e50
FN
1242 snprintf(zv->zv_disk->disk_name, DISK_NAME_LEN, "%s%d",
1243 ZVOL_DEV_NAME, (dev & MINORMASK));
60101509 1244
ce37ebd2 1245 return (zv);
60101509
BB
1246
1247out_queue:
1248 blk_cleanup_queue(zv->zv_queue);
1249out_kmem:
1250 kmem_free(zv, sizeof (zvol_state_t));
0a6bef26 1251
ce37ebd2 1252 return (NULL);
60101509
BB
1253}
1254
1255/*
1256 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1257 */
1258static void
1259zvol_free(zvol_state_t *zv)
1260{
1261 avl_destroy(&zv->zv_znode.z_range_avl);
1262 mutex_destroy(&zv->zv_znode.z_range_lock);
1263
1264 del_gendisk(zv->zv_disk);
1265 blk_cleanup_queue(zv->zv_queue);
1266 put_disk(zv->zv_disk);
1267
1268 kmem_free(zv, sizeof (zvol_state_t));
1269}
1270
1271static int
0b4d1b58
ED
1272__zvol_snapdev_hidden(const char *name)
1273{
ce37ebd2
BB
1274 uint64_t snapdev;
1275 char *parent;
1276 char *atp;
1277 int error = 0;
1278
79c76d5b 1279 parent = kmem_alloc(MAXPATHLEN, KM_SLEEP);
ce37ebd2
BB
1280 (void) strlcpy(parent, name, MAXPATHLEN);
1281
1282 if ((atp = strrchr(parent, '@')) != NULL) {
1283 *atp = '\0';
1284 error = dsl_prop_get_integer(parent, "snapdev", &snapdev, NULL);
1285 if ((error == 0) && (snapdev == ZFS_SNAPDEV_HIDDEN))
1286 error = SET_ERROR(ENODEV);
1287 }
1288
1289 kmem_free(parent, MAXPATHLEN);
1290
1291 return (SET_ERROR(error));
0b4d1b58
ED
1292}
1293
1294static int
1295__zvol_create_minor(const char *name, boolean_t ignore_snapdev)
60101509
BB
1296{
1297 zvol_state_t *zv;
1298 objset_t *os;
1299 dmu_object_info_t *doi;
1300 uint64_t volsize;
9965059a 1301 uint64_t len;
60101509
BB
1302 unsigned minor = 0;
1303 int error = 0;
1304
1305 ASSERT(MUTEX_HELD(&zvol_state_lock));
1306
1307 zv = zvol_find_by_name(name);
1308 if (zv) {
2e528b49 1309 error = SET_ERROR(EEXIST);
60101509
BB
1310 goto out;
1311 }
1312
0b4d1b58
ED
1313 if (ignore_snapdev == B_FALSE) {
1314 error = __zvol_snapdev_hidden(name);
1315 if (error)
1316 goto out;
1317 }
1318
79c76d5b 1319 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
60101509
BB
1320
1321 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
1322 if (error)
1323 goto out_doi;
1324
1325 error = dmu_object_info(os, ZVOL_OBJ, doi);
1326 if (error)
1327 goto out_dmu_objset_disown;
1328
1329 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
1330 if (error)
1331 goto out_dmu_objset_disown;
1332
1333 error = zvol_find_minor(&minor);
1334 if (error)
1335 goto out_dmu_objset_disown;
1336
1337 zv = zvol_alloc(MKDEV(zvol_major, minor), name);
1338 if (zv == NULL) {
2e528b49 1339 error = SET_ERROR(EAGAIN);
60101509
BB
1340 goto out_dmu_objset_disown;
1341 }
1342
1343 if (dmu_objset_is_snapshot(os))
1344 zv->zv_flags |= ZVOL_RDONLY;
1345
1346 zv->zv_volblocksize = doi->doi_data_block_size;
1347 zv->zv_volsize = volsize;
1348 zv->zv_objset = os;
1349
1350 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1351
c495fe2c 1352 blk_queue_max_hw_sectors(zv->zv_queue, (DMU_MAX_ACCESS / 4) >> 9);
34037afe
ED
1353 blk_queue_max_segments(zv->zv_queue, UINT16_MAX);
1354 blk_queue_max_segment_size(zv->zv_queue, UINT_MAX);
1355 blk_queue_physical_block_size(zv->zv_queue, zv->zv_volblocksize);
1356 blk_queue_io_opt(zv->zv_queue, zv->zv_volblocksize);
7c0e5708
ED
1357 blk_queue_max_discard_sectors(zv->zv_queue,
1358 (zvol_max_discard_blocks * zv->zv_volblocksize) >> 9);
ee5fd0bb 1359 blk_queue_discard_granularity(zv->zv_queue, zv->zv_volblocksize);
30930fba 1360 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zv->zv_queue);
37f9dac5 1361#ifdef QUEUE_FLAG_NONROT
34037afe
ED
1362 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zv->zv_queue);
1363#endif
c6a3a222
RY
1364#ifdef QUEUE_FLAG_ADD_RANDOM
1365 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, zv->zv_queue);
1366#endif
34037afe 1367
a4430fce
GW
1368 if (spa_writeable(dmu_objset_spa(os))) {
1369 if (zil_replay_disable)
1370 zil_destroy(dmu_objset_zil(os), B_FALSE);
1371 else
1372 zil_replay(os, zv, zvol_replay_vector);
1373 }
60101509 1374
9965059a
BB
1375 /*
1376 * When udev detects the addition of the device it will immediately
1377 * invoke blkid(8) to determine the type of content on the device.
1378 * Prefetching the blocks commonly scanned by blkid(8) will speed
1379 * up this process.
1380 */
1381 len = MIN(MAX(zvol_prefetch_bytes, 0), SPA_MAXBLOCKSIZE);
1382 if (len > 0) {
1383 dmu_prefetch(os, ZVOL_OBJ, 0, len);
1384 dmu_prefetch(os, ZVOL_OBJ, volsize - len, len);
1385 }
1386
f74a147c 1387 zv->zv_objset = NULL;
60101509
BB
1388out_dmu_objset_disown:
1389 dmu_objset_disown(os, zvol_tag);
60101509 1390out_doi:
ce37ebd2 1391 kmem_free(doi, sizeof (dmu_object_info_t));
60101509
BB
1392out:
1393
1394 if (error == 0) {
1395 zvol_insert(zv);
1396 add_disk(zv->zv_disk);
1397 }
1398
ce37ebd2 1399 return (SET_ERROR(error));
60101509
BB
1400}
1401
1402/*
1403 * Create a block device minor node and setup the linkage between it
1404 * and the specified volume. Once this function returns the block
1405 * device is live and ready for use.
1406 */
1407int
1408zvol_create_minor(const char *name)
1409{
1410 int error;
1411
1412 mutex_enter(&zvol_state_lock);
0b4d1b58 1413 error = __zvol_create_minor(name, B_FALSE);
60101509
BB
1414 mutex_exit(&zvol_state_lock);
1415
ce37ebd2 1416 return (SET_ERROR(error));
60101509
BB
1417}
1418
1419static int
1420__zvol_remove_minor(const char *name)
1421{
1422 zvol_state_t *zv;
1423
1424 ASSERT(MUTEX_HELD(&zvol_state_lock));
1425
1426 zv = zvol_find_by_name(name);
1427 if (zv == NULL)
2e528b49 1428 return (SET_ERROR(ENXIO));
60101509
BB
1429
1430 if (zv->zv_open_count > 0)
2e528b49 1431 return (SET_ERROR(EBUSY));
60101509
BB
1432
1433 zvol_remove(zv);
1434 zvol_free(zv);
1435
1436 return (0);
1437}
1438
1439/*
1440 * Remove a block device minor node for the specified volume.
1441 */
1442int
1443zvol_remove_minor(const char *name)
1444{
1445 int error;
1446
1447 mutex_enter(&zvol_state_lock);
1448 error = __zvol_remove_minor(name);
1449 mutex_exit(&zvol_state_lock);
1450
ce37ebd2 1451 return (SET_ERROR(error));
60101509
BB
1452}
1453
ba6a2402
BB
1454/*
1455 * Rename a block device minor mode for the specified volume.
1456 */
1457static void
1458__zvol_rename_minor(zvol_state_t *zv, const char *newname)
1459{
1460 int readonly = get_disk_ro(zv->zv_disk);
1461
1462 ASSERT(MUTEX_HELD(&zvol_state_lock));
1463
1464 strlcpy(zv->zv_name, newname, sizeof (zv->zv_name));
1465
1466 /*
1467 * The block device's read-only state is briefly changed causing
1468 * a KOBJ_CHANGE uevent to be issued. This ensures udev detects
1469 * the name change and fixes the symlinks. This does not change
1470 * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
1471 * changes. This would normally be done using kobject_uevent() but
1472 * that is a GPL-only symbol which is why we need this workaround.
1473 */
1474 set_disk_ro(zv->zv_disk, !readonly);
1475 set_disk_ro(zv->zv_disk, readonly);
1476}
1477
60101509 1478static int
13fe0198 1479zvol_create_minors_cb(const char *dsname, void *arg)
60101509 1480{
ba6a2402 1481 (void) zvol_create_minor(dsname);
60101509 1482
d5674448 1483 return (0);
60101509
BB
1484}
1485
1486/*
ba6a2402 1487 * Create minors for specified dataset including children and snapshots.
60101509
BB
1488 */
1489int
ba6a2402 1490zvol_create_minors(const char *name)
60101509 1491{
60101509
BB
1492 int error = 0;
1493
ba6a2402
BB
1494 if (!zvol_inhibit_dev)
1495 error = dmu_objset_find((char *)name, zvol_create_minors_cb,
1496 NULL, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1497
1498 return (SET_ERROR(error));
1499}
1500
1501/*
1502 * Remove minors for specified dataset including children and snapshots.
1503 */
1504void
1505zvol_remove_minors(const char *name)
1506{
1507 zvol_state_t *zv, *zv_next;
1508 int namelen = ((name) ? strlen(name) : 0);
1509
74497b7a 1510 if (zvol_inhibit_dev)
ba6a2402 1511 return;
74497b7a 1512
60101509 1513 mutex_enter(&zvol_state_lock);
ba6a2402
BB
1514
1515 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1516 zv_next = list_next(&zvol_state_list, zv);
1517
1518 if (name == NULL || strcmp(zv->zv_name, name) == 0 ||
1519 (strncmp(zv->zv_name, name, namelen) == 0 &&
1520 zv->zv_name[namelen] == '/')) {
1521 zvol_remove(zv);
1522 zvol_free(zv);
60101509 1523 }
60101509 1524 }
60101509 1525
ba6a2402 1526 mutex_exit(&zvol_state_lock);
60101509
BB
1527}
1528
1529/*
ba6a2402 1530 * Rename minors for specified dataset including children and snapshots.
60101509
BB
1531 */
1532void
ba6a2402 1533zvol_rename_minors(const char *oldname, const char *newname)
60101509
BB
1534{
1535 zvol_state_t *zv, *zv_next;
ba6a2402
BB
1536 int oldnamelen, newnamelen;
1537 char *name;
60101509 1538
74497b7a
DH
1539 if (zvol_inhibit_dev)
1540 return;
1541
ba6a2402
BB
1542 oldnamelen = strlen(oldname);
1543 newnamelen = strlen(newname);
79c76d5b 1544 name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
60101509
BB
1545
1546 mutex_enter(&zvol_state_lock);
ba6a2402 1547
60101509
BB
1548 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1549 zv_next = list_next(&zvol_state_list, zv);
1550
ba6a2402
BB
1551 if (strcmp(zv->zv_name, oldname) == 0) {
1552 __zvol_rename_minor(zv, newname);
1553 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 &&
1554 (zv->zv_name[oldnamelen] == '/' ||
1555 zv->zv_name[oldnamelen] == '@')) {
1556 snprintf(name, MAXNAMELEN, "%s%c%s", newname,
1557 zv->zv_name[oldnamelen],
1558 zv->zv_name + oldnamelen + 1);
1559 __zvol_rename_minor(zv, name);
60101509
BB
1560 }
1561 }
ba6a2402 1562
60101509 1563 mutex_exit(&zvol_state_lock);
ba6a2402
BB
1564
1565 kmem_free(name, MAXNAMELEN);
60101509
BB
1566}
1567
0b4d1b58
ED
1568static int
1569snapdev_snapshot_changed_cb(const char *dsname, void *arg) {
1570 uint64_t snapdev = *(uint64_t *) arg;
1571
1572 if (strchr(dsname, '@') == NULL)
ba6a2402 1573 return (0);
0b4d1b58
ED
1574
1575 switch (snapdev) {
1576 case ZFS_SNAPDEV_VISIBLE:
1577 mutex_enter(&zvol_state_lock);
1578 (void) __zvol_create_minor(dsname, B_TRUE);
1579 mutex_exit(&zvol_state_lock);
1580 break;
1581 case ZFS_SNAPDEV_HIDDEN:
1582 (void) zvol_remove_minor(dsname);
1583 break;
1584 }
ba6a2402
BB
1585
1586 return (0);
0b4d1b58
ED
1587}
1588
1589int
1590zvol_set_snapdev(const char *dsname, uint64_t snapdev) {
1591 (void) dmu_objset_find((char *) dsname, snapdev_snapshot_changed_cb,
1592 &snapdev, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
1593 /* caller should continue to modify snapdev property */
1594 return (-1);
1595}
1596
60101509
BB
1597int
1598zvol_init(void)
1599{
1600 int error;
1601
2a3871d4 1602 list_create(&zvol_state_list, sizeof (zvol_state_t),
ce37ebd2
BB
1603 offsetof(zvol_state_t, zv_next));
1604
2a3871d4
RY
1605 mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1606
60101509
BB
1607 error = register_blkdev(zvol_major, ZVOL_DRIVER);
1608 if (error) {
1609 printk(KERN_INFO "ZFS: register_blkdev() failed %d\n", error);
37f9dac5 1610 goto out;
60101509
BB
1611 }
1612
1613 blk_register_region(MKDEV(zvol_major, 0), 1UL << MINORBITS,
ce37ebd2 1614 THIS_MODULE, zvol_probe, NULL, NULL);
60101509 1615
60101509 1616 return (0);
2a3871d4 1617
37f9dac5 1618out:
2a3871d4
RY
1619 mutex_destroy(&zvol_state_lock);
1620 list_destroy(&zvol_state_list);
1621
ce37ebd2 1622 return (SET_ERROR(error));
60101509
BB
1623}
1624
1625void
1626zvol_fini(void)
1627{
1628 zvol_remove_minors(NULL);
1629 blk_unregister_region(MKDEV(zvol_major, 0), 1UL << MINORBITS);
1630 unregister_blkdev(zvol_major, ZVOL_DRIVER);
60101509
BB
1631 mutex_destroy(&zvol_state_lock);
1632 list_destroy(&zvol_state_list);
1633}
1634
74497b7a
DH
1635module_param(zvol_inhibit_dev, uint, 0644);
1636MODULE_PARM_DESC(zvol_inhibit_dev, "Do not create zvol device nodes");
1637
30a9524e 1638module_param(zvol_major, uint, 0444);
60101509
BB
1639MODULE_PARM_DESC(zvol_major, "Major number for zvol device");
1640
7c0e5708 1641module_param(zvol_max_discard_blocks, ulong, 0444);
ce37ebd2 1642MODULE_PARM_DESC(zvol_max_discard_blocks, "Max number of blocks to discard");
9965059a
BB
1643
1644module_param(zvol_prefetch_bytes, uint, 0644);
1645MODULE_PARM_DESC(zvol_prefetch_bytes, "Prefetch N bytes at zvol start+end");