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.
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.
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]
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>.
27 * ZFS volume emulation driver.
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:
32 * /dev/<pool_name>/<dataset_name>
34 * Volumes are persistent through reboot and module load. No user command
35 * needs to be run before opening and using a device.
39 #include <sys/dmu_traverse.h>
40 #include <sys/dsl_dataset.h>
41 #include <sys/dsl_prop.h>
43 #include <sys/zfeature.h>
44 #include <sys/zil_impl.h>
46 #include <sys/zfs_rlock.h>
47 #include <sys/zfs_znode.h>
49 #include <linux/blkdev_compat.h>
51 unsigned int zvol_inhibit_dev = 0;
52 unsigned int zvol_major = ZVOL_MAJOR;
53 unsigned int zvol_prefetch_bytes = (128 * 1024);
54 unsigned long zvol_max_discard_blocks = 16384;
56 static kmutex_t zvol_state_lock;
57 static list_t zvol_state_list;
58 static char *zvol_tag = "zvol_tag";
61 * The in-core state of each volume.
63 typedef struct zvol_state {
64 char zv_name[MAXNAMELEN]; /* name */
65 uint64_t zv_volsize; /* advertised space */
66 uint64_t zv_volblocksize; /* volume block size */
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 */
77 list_node_t zv_next; /* next zvol_state_t linkage */
80 #define ZVOL_RDONLY 0x1
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.
89 zvol_find_minor(unsigned *minor)
94 ASSERT(MUTEX_HELD(&zvol_state_lock));
95 for (zv = list_head(&zvol_state_list); zv != NULL;
96 zv = list_next(&zvol_state_list, zv), *minor += ZVOL_MINORS) {
97 if (MINOR(zv->zv_dev) != MINOR(*minor))
101 /* All minors are in use */
102 if (*minor >= (1 << MINORBITS))
103 return (SET_ERROR(ENXIO));
109 * Find a zvol_state_t given the full major+minor dev_t.
111 static zvol_state_t *
112 zvol_find_by_dev(dev_t dev)
116 ASSERT(MUTEX_HELD(&zvol_state_lock));
117 for (zv = list_head(&zvol_state_list); zv != NULL;
118 zv = list_next(&zvol_state_list, zv)) {
119 if (zv->zv_dev == dev)
127 * Find a zvol_state_t given the name provided at zvol_alloc() time.
129 static zvol_state_t *
130 zvol_find_by_name(const char *name)
134 ASSERT(MUTEX_HELD(&zvol_state_lock));
135 for (zv = list_head(&zvol_state_list); zv != NULL;
136 zv = list_next(&zvol_state_list, zv)) {
137 if (strncmp(zv->zv_name, name, MAXNAMELEN) == 0)
146 * Given a path, return TRUE if path is a ZVOL.
149 zvol_is_zvol(const char *device)
151 struct block_device *bdev;
154 bdev = lookup_bdev(device);
158 major = MAJOR(bdev->bd_dev);
161 if (major == zvol_major)
168 * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation.
171 zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
173 zfs_creat_t *zct = arg;
174 nvlist_t *nvprops = zct->zct_props;
176 uint64_t volblocksize, volsize;
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);
185 * These properties must be removed from the list so the generic
186 * property setting step won't apply to them.
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));
193 error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
197 error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
201 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
206 * ZFS_IOC_OBJSET_STATS entry point.
209 zvol_get_stats(objset_t *os, nvlist_t *nv)
212 dmu_object_info_t *doi;
215 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
217 return (SET_ERROR(error));
219 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
220 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
221 error = dmu_object_info(os, ZVOL_OBJ, doi);
224 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
225 doi->doi_data_block_size);
228 kmem_free(doi, sizeof (dmu_object_info_t));
230 return (SET_ERROR(error));
234 zvol_size_changed(zvol_state_t *zv, uint64_t volsize)
236 struct block_device *bdev;
238 bdev = bdget_disk(zv->zv_disk, 0);
241 set_capacity(zv->zv_disk, volsize >> 9);
242 zv->zv_volsize = volsize;
243 check_disk_size_change(zv->zv_disk, bdev);
249 * Sanity check volume size.
252 zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
255 return (SET_ERROR(EINVAL));
257 if (volsize % blocksize != 0)
258 return (SET_ERROR(EINVAL));
261 if (volsize - 1 > MAXOFFSET_T)
262 return (SET_ERROR(EOVERFLOW));
268 * Ensure the zap is flushed then inform the VFS of the capacity change.
271 zvol_update_volsize(uint64_t volsize, objset_t *os)
276 ASSERT(MUTEX_HELD(&zvol_state_lock));
278 tx = dmu_tx_create(os);
279 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
280 dmu_tx_mark_netfree(tx);
281 error = dmu_tx_assign(tx, TXG_WAIT);
284 return (SET_ERROR(error));
287 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
292 error = dmu_free_long_range(os,
293 ZVOL_OBJ, volsize, DMU_OBJECT_END);
299 zvol_update_live_volsize(zvol_state_t *zv, uint64_t volsize)
301 zvol_size_changed(zv, volsize);
304 * We should post a event here describing the expansion. However,
305 * the zfs_ereport_post() interface doesn't nicely support posting
306 * events for zvols, it assumes events relate to vdevs or zios.
313 * Set ZFS_PROP_VOLSIZE set entry point.
316 zvol_set_volsize(const char *name, uint64_t volsize)
318 zvol_state_t *zv = NULL;
321 dmu_object_info_t *doi;
323 boolean_t owned = B_FALSE;
325 error = dsl_prop_get_integer(name,
326 zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL);
328 return (SET_ERROR(error));
330 return (SET_ERROR(EROFS));
332 mutex_enter(&zvol_state_lock);
333 zv = zvol_find_by_name(name);
335 if (zv == NULL || zv->zv_objset == NULL) {
336 if ((error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE,
338 mutex_exit(&zvol_state_lock);
339 return (SET_ERROR(error));
348 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
350 if ((error = dmu_object_info(os, ZVOL_OBJ, doi)) ||
351 (error = zvol_check_volsize(volsize, doi->doi_data_block_size)))
354 error = zvol_update_volsize(volsize, os);
355 kmem_free(doi, sizeof (dmu_object_info_t));
357 if (error == 0 && zv != NULL)
358 error = zvol_update_live_volsize(zv, volsize);
361 dmu_objset_disown(os, FTAG);
363 zv->zv_objset = NULL;
365 mutex_exit(&zvol_state_lock);
370 * Sanity check volume block size.
373 zvol_check_volblocksize(const char *name, uint64_t volblocksize)
375 /* Record sizes above 128k need the feature to be enabled */
376 if (volblocksize > SPA_OLD_MAXBLOCKSIZE) {
380 if ((error = spa_open(name, &spa, FTAG)) != 0)
383 if (!spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
384 spa_close(spa, FTAG);
385 return (SET_ERROR(ENOTSUP));
389 * We don't allow setting the property above 1MB,
390 * unless the tunable has been changed.
392 if (volblocksize > zfs_max_recordsize)
393 return (SET_ERROR(EDOM));
395 spa_close(spa, FTAG);
398 if (volblocksize < SPA_MINBLOCKSIZE ||
399 volblocksize > SPA_MAXBLOCKSIZE ||
401 return (SET_ERROR(EDOM));
407 * Set ZFS_PROP_VOLBLOCKSIZE set entry point.
410 zvol_set_volblocksize(const char *name, uint64_t volblocksize)
416 mutex_enter(&zvol_state_lock);
418 zv = zvol_find_by_name(name);
420 error = SET_ERROR(ENXIO);
424 if (zv->zv_flags & ZVOL_RDONLY) {
425 error = SET_ERROR(EROFS);
429 tx = dmu_tx_create(zv->zv_objset);
430 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
431 error = dmu_tx_assign(tx, TXG_WAIT);
435 error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
436 volblocksize, 0, tx);
437 if (error == ENOTSUP)
438 error = SET_ERROR(EBUSY);
441 zv->zv_volblocksize = volblocksize;
444 mutex_exit(&zvol_state_lock);
446 return (SET_ERROR(error));
450 * Replay a TX_WRITE ZIL transaction that didn't get committed
451 * after a system failure
454 zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
456 objset_t *os = zv->zv_objset;
457 char *data = (char *)(lr + 1); /* data follows lr_write_t */
458 uint64_t off = lr->lr_offset;
459 uint64_t len = lr->lr_length;
464 byteswap_uint64_array(lr, sizeof (*lr));
466 tx = dmu_tx_create(os);
467 dmu_tx_hold_write(tx, ZVOL_OBJ, off, len);
468 error = dmu_tx_assign(tx, TXG_WAIT);
472 dmu_write(os, ZVOL_OBJ, off, len, data, tx);
476 return (SET_ERROR(error));
480 zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
482 return (SET_ERROR(ENOTSUP));
486 * Callback vectors for replaying records.
487 * Only TX_WRITE is needed for zvol.
489 zil_replay_func_t zvol_replay_vector[TX_MAX_TYPE] = {
490 (zil_replay_func_t)zvol_replay_err, /* no such transaction type */
491 (zil_replay_func_t)zvol_replay_err, /* TX_CREATE */
492 (zil_replay_func_t)zvol_replay_err, /* TX_MKDIR */
493 (zil_replay_func_t)zvol_replay_err, /* TX_MKXATTR */
494 (zil_replay_func_t)zvol_replay_err, /* TX_SYMLINK */
495 (zil_replay_func_t)zvol_replay_err, /* TX_REMOVE */
496 (zil_replay_func_t)zvol_replay_err, /* TX_RMDIR */
497 (zil_replay_func_t)zvol_replay_err, /* TX_LINK */
498 (zil_replay_func_t)zvol_replay_err, /* TX_RENAME */
499 (zil_replay_func_t)zvol_replay_write, /* TX_WRITE */
500 (zil_replay_func_t)zvol_replay_err, /* TX_TRUNCATE */
501 (zil_replay_func_t)zvol_replay_err, /* TX_SETATTR */
502 (zil_replay_func_t)zvol_replay_err, /* TX_ACL */
506 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
508 * We store data in the log buffers if it's small enough.
509 * Otherwise we will later flush the data out via dmu_sync().
511 ssize_t zvol_immediate_write_sz = 32768;
514 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, uint64_t offset,
515 uint64_t size, int sync)
517 uint32_t blocksize = zv->zv_volblocksize;
518 zilog_t *zilog = zv->zv_zilog;
520 ssize_t immediate_write_sz;
522 if (zil_replaying(zilog, tx))
525 immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
526 ? 0 : zvol_immediate_write_sz;
527 slogging = spa_has_slogs(zilog->zl_spa) &&
528 (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
534 itx_wr_state_t write_state;
537 * Unlike zfs_log_write() we can be called with
538 * up to DMU_MAX_ACCESS/2 (5MB) writes.
540 if (blocksize > immediate_write_sz && !slogging &&
541 size >= blocksize && offset % blocksize == 0) {
542 write_state = WR_INDIRECT; /* uses dmu_sync */
545 write_state = WR_COPIED;
546 len = MIN(ZIL_MAX_LOG_DATA, size);
548 write_state = WR_NEED_COPY;
549 len = MIN(ZIL_MAX_LOG_DATA, size);
552 itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
553 (write_state == WR_COPIED ? len : 0));
554 lr = (lr_write_t *)&itx->itx_lr;
555 if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
556 ZVOL_OBJ, offset, len, lr+1, DMU_READ_NO_PREFETCH) != 0) {
557 zil_itx_destroy(itx);
558 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
559 lr = (lr_write_t *)&itx->itx_lr;
560 write_state = WR_NEED_COPY;
563 itx->itx_wr_state = write_state;
564 if (write_state == WR_NEED_COPY)
566 lr->lr_foid = ZVOL_OBJ;
567 lr->lr_offset = offset;
570 BP_ZERO(&lr->lr_blkptr);
572 itx->itx_private = zv;
573 itx->itx_sync = sync;
575 (void) zil_itx_assign(zilog, itx, tx);
583 zvol_write(struct bio *bio)
585 zvol_state_t *zv = bio->bi_bdev->bd_disk->private_data;
586 uint64_t offset = BIO_BI_SECTOR(bio) << 9;
587 uint64_t size = BIO_BI_SIZE(bio);
593 if (bio->bi_rw & VDEV_REQ_FLUSH)
594 zil_commit(zv->zv_zilog, ZVOL_OBJ);
597 * Some requests are just for flush and nothing else.
602 uio.uio_bvec = &bio->bi_io_vec[BIO_BI_IDX(bio)];
603 uio.uio_skip = BIO_BI_SKIP(bio);
604 uio.uio_resid = size;
605 uio.uio_iovcnt = bio->bi_vcnt - BIO_BI_IDX(bio);
606 uio.uio_loffset = offset;
607 uio.uio_limit = MAXOFFSET_T;
608 uio.uio_segflg = UIO_BVEC;
610 rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_WRITER);
612 tx = dmu_tx_create(zv->zv_objset);
613 dmu_tx_hold_write(tx, ZVOL_OBJ, offset, size);
615 /* This will only fail for ENOSPC */
616 error = dmu_tx_assign(tx, TXG_WAIT);
619 zfs_range_unlock(rl);
623 error = dmu_write_uio(zv->zv_objset, ZVOL_OBJ, &uio, size, tx);
625 zvol_log_write(zv, tx, offset, size,
626 !!(bio->bi_rw & VDEV_REQ_FUA));
629 zfs_range_unlock(rl);
631 if ((bio->bi_rw & VDEV_REQ_FUA) ||
632 zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)
633 zil_commit(zv->zv_zilog, ZVOL_OBJ);
640 zvol_discard(struct bio *bio)
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;
649 if (end > zv->zv_volsize)
650 return (SET_ERROR(EIO));
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.
661 if (!(bio->bi_rw & REQ_SECURE)) {
662 start = P2ROUNDUP(start, zv->zv_volblocksize);
663 end = P2ALIGN(end, zv->zv_volblocksize);
671 rl = zfs_range_lock(&zv->zv_znode, start, size, RL_WRITER);
673 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, start, size);
676 * TODO: maybe we should add the operation to the log.
679 zfs_range_unlock(rl);
685 zvol_read(struct bio *bio)
687 zvol_state_t *zv = bio->bi_bdev->bd_disk->private_data;
688 uint64_t offset = BIO_BI_SECTOR(bio) << 9;
689 uint64_t size = BIO_BI_SIZE(bio);
697 uio.uio_bvec = &bio->bi_io_vec[BIO_BI_IDX(bio)];
698 uio.uio_skip = BIO_BI_SKIP(bio);
699 uio.uio_resid = size;
700 uio.uio_iovcnt = bio->bi_vcnt - BIO_BI_IDX(bio);
701 uio.uio_loffset = offset;
702 uio.uio_limit = MAXOFFSET_T;
703 uio.uio_segflg = UIO_BVEC;
705 rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
707 error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, &uio, size);
709 zfs_range_unlock(rl);
711 /* convert checksum errors into IO errors */
713 error = SET_ERROR(EIO);
718 static MAKE_REQUEST_FN_RET
719 zvol_request(struct request_queue *q, struct bio *bio)
721 zvol_state_t *zv = q->queuedata;
722 fstrans_cookie_t cookie = spl_fstrans_mark();
723 uint64_t offset = BIO_BI_SECTOR(bio);
724 unsigned int sectors = bio_sectors(bio);
725 int rw = bio_data_dir(bio);
726 #ifdef HAVE_GENERIC_IO_ACCT
727 unsigned long start = jiffies;
731 if (bio_has_data(bio) && offset + sectors >
732 get_capacity(zv->zv_disk)) {
734 "%s: bad access: block=%llu, count=%lu\n",
735 zv->zv_disk->disk_name,
736 (long long unsigned)offset,
737 (long unsigned)sectors);
738 error = SET_ERROR(EIO);
742 generic_start_io_acct(rw, sectors, &zv->zv_disk->part0);
745 if (unlikely(zv->zv_flags & ZVOL_RDONLY)) {
746 error = SET_ERROR(EROFS);
750 if (bio->bi_rw & VDEV_REQ_DISCARD) {
751 error = zvol_discard(bio);
755 error = zvol_write(bio);
757 error = zvol_read(bio);
760 generic_end_io_acct(rw, &zv->zv_disk->part0, start);
762 BIO_END_IO(bio, -error);
763 spl_fstrans_unmark(cookie);
764 #ifdef HAVE_MAKE_REQUEST_FN_RET_INT
766 #elif defined(HAVE_MAKE_REQUEST_FN_RET_QC)
767 return (BLK_QC_T_NONE);
772 zvol_get_done(zgd_t *zgd, int error)
775 dmu_buf_rele(zgd->zgd_db, zgd);
777 zfs_range_unlock(zgd->zgd_rl);
779 if (error == 0 && zgd->zgd_bp)
780 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
782 kmem_free(zgd, sizeof (zgd_t));
786 * Get data to generate a TX_WRITE intent log record.
789 zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
791 zvol_state_t *zv = arg;
792 objset_t *os = zv->zv_objset;
793 uint64_t object = ZVOL_OBJ;
794 uint64_t offset = lr->lr_offset;
795 uint64_t size = lr->lr_length;
796 blkptr_t *bp = &lr->lr_blkptr;
804 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
805 zgd->zgd_zilog = zv->zv_zilog;
806 zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
809 * Write records come in two flavors: immediate and indirect.
810 * For small writes it's cheaper to store the data with the
811 * log record (immediate); for large writes it's cheaper to
812 * sync the data and get a pointer to it (indirect) so that
813 * we don't have to write the data twice.
815 if (buf != NULL) { /* immediate write */
816 error = dmu_read(os, object, offset, size, buf,
817 DMU_READ_NO_PREFETCH);
819 size = zv->zv_volblocksize;
820 offset = P2ALIGN_TYPED(offset, size, uint64_t);
821 error = dmu_buf_hold(os, object, offset, zgd, &db,
822 DMU_READ_NO_PREFETCH);
824 blkptr_t *obp = dmu_buf_get_blkptr(db);
826 ASSERT(BP_IS_HOLE(bp));
831 zgd->zgd_bp = &lr->lr_blkptr;
834 ASSERT(db->db_offset == offset);
835 ASSERT(db->db_size == size);
837 error = dmu_sync(zio, lr->lr_common.lrc_txg,
845 zvol_get_done(zgd, error);
847 return (SET_ERROR(error));
851 * The zvol_state_t's are inserted in increasing MINOR(dev_t) order.
854 zvol_insert(zvol_state_t *zv_insert)
856 zvol_state_t *zv = NULL;
858 ASSERT(MUTEX_HELD(&zvol_state_lock));
859 ASSERT3U(MINOR(zv_insert->zv_dev) & ZVOL_MINOR_MASK, ==, 0);
860 for (zv = list_head(&zvol_state_list); zv != NULL;
861 zv = list_next(&zvol_state_list, zv)) {
862 if (MINOR(zv->zv_dev) > MINOR(zv_insert->zv_dev))
866 list_insert_before(&zvol_state_list, zv, zv_insert);
870 * Simply remove the zvol from to list of zvols.
873 zvol_remove(zvol_state_t *zv_remove)
875 ASSERT(MUTEX_HELD(&zvol_state_lock));
876 list_remove(&zvol_state_list, zv_remove);
880 zvol_first_open(zvol_state_t *zv)
889 * In all other cases the spa_namespace_lock is taken before the
890 * bdev->bd_mutex lock. But in this case the Linux __blkdev_get()
891 * function calls fops->open() with the bdev->bd_mutex lock held.
893 * To avoid a potential lock inversion deadlock we preemptively
894 * try to take the spa_namespace_lock(). Normally it will not
895 * be contended and this is safe because spa_open_common() handles
896 * the case where the caller already holds the spa_namespace_lock.
898 * When it is contended we risk a lock inversion if we were to
899 * block waiting for the lock. Luckily, the __blkdev_get()
900 * function allows us to return -ERESTARTSYS which will result in
901 * bdev->bd_mutex being dropped, reacquired, and fops->open() being
902 * called again. This process can be repeated safely until both
903 * locks are acquired.
905 if (!mutex_owned(&spa_namespace_lock)) {
906 locked = mutex_tryenter(&spa_namespace_lock);
908 return (-SET_ERROR(ERESTARTSYS));
911 error = dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL);
915 /* lie and say we're read-only */
916 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, 1, zvol_tag, &os);
920 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
922 dmu_objset_disown(os, zvol_tag);
927 error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
929 dmu_objset_disown(os, zvol_tag);
933 set_capacity(zv->zv_disk, volsize >> 9);
934 zv->zv_volsize = volsize;
935 zv->zv_zilog = zil_open(os, zvol_get_data);
937 if (ro || dmu_objset_is_snapshot(os) ||
938 !spa_writeable(dmu_objset_spa(os))) {
939 set_disk_ro(zv->zv_disk, 1);
940 zv->zv_flags |= ZVOL_RDONLY;
942 set_disk_ro(zv->zv_disk, 0);
943 zv->zv_flags &= ~ZVOL_RDONLY;
948 mutex_exit(&spa_namespace_lock);
950 return (SET_ERROR(-error));
954 zvol_last_close(zvol_state_t *zv)
956 zil_close(zv->zv_zilog);
959 dmu_buf_rele(zv->zv_dbuf, zvol_tag);
965 if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) &&
966 !(zv->zv_flags & ZVOL_RDONLY))
967 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
968 (void) dmu_objset_evict_dbufs(zv->zv_objset);
970 dmu_objset_disown(zv->zv_objset, zvol_tag);
971 zv->zv_objset = NULL;
975 zvol_open(struct block_device *bdev, fmode_t flag)
977 zvol_state_t *zv = bdev->bd_disk->private_data;
978 int error = 0, drop_mutex = 0;
981 * If the caller is already holding the mutex do not take it
982 * again, this will happen as part of zvol_create_minor().
983 * Once add_disk() is called the device is live and the kernel
984 * will attempt to open it to read the partition information.
986 if (!mutex_owned(&zvol_state_lock)) {
987 mutex_enter(&zvol_state_lock);
991 ASSERT3P(zv, !=, NULL);
993 if (zv->zv_open_count == 0) {
994 error = zvol_first_open(zv);
999 if ((flag & FMODE_WRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
1001 goto out_open_count;
1004 zv->zv_open_count++;
1007 if (zv->zv_open_count == 0)
1008 zvol_last_close(zv);
1012 mutex_exit(&zvol_state_lock);
1014 check_disk_change(bdev);
1016 return (SET_ERROR(error));
1019 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1024 zvol_release(struct gendisk *disk, fmode_t mode)
1026 zvol_state_t *zv = disk->private_data;
1029 if (!mutex_owned(&zvol_state_lock)) {
1030 mutex_enter(&zvol_state_lock);
1034 if (zv->zv_open_count > 0) {
1035 zv->zv_open_count--;
1036 if (zv->zv_open_count == 0)
1037 zvol_last_close(zv);
1041 mutex_exit(&zvol_state_lock);
1043 #ifndef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1049 zvol_ioctl(struct block_device *bdev, fmode_t mode,
1050 unsigned int cmd, unsigned long arg)
1052 zvol_state_t *zv = bdev->bd_disk->private_data;
1056 return (SET_ERROR(-ENXIO));
1060 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1063 error = copy_to_user((void *)arg, zv->zv_name, MAXNAMELEN);
1072 return (SET_ERROR(error));
1075 #ifdef CONFIG_COMPAT
1077 zvol_compat_ioctl(struct block_device *bdev, fmode_t mode,
1078 unsigned cmd, unsigned long arg)
1080 return (zvol_ioctl(bdev, mode, cmd, arg));
1083 #define zvol_compat_ioctl NULL
1086 static int zvol_media_changed(struct gendisk *disk)
1088 zvol_state_t *zv = disk->private_data;
1090 return (zv->zv_changed);
1093 static int zvol_revalidate_disk(struct gendisk *disk)
1095 zvol_state_t *zv = disk->private_data;
1098 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1104 * Provide a simple virtual geometry for legacy compatibility. For devices
1105 * smaller than 1 MiB a small head and sector count is used to allow very
1106 * tiny devices. For devices over 1 Mib a standard head and sector count
1107 * is used to keep the cylinders count reasonable.
1110 zvol_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1112 zvol_state_t *zv = bdev->bd_disk->private_data;
1113 sector_t sectors = get_capacity(zv->zv_disk);
1115 if (sectors > 2048) {
1124 geo->cylinders = sectors / (geo->heads * geo->sectors);
1129 static struct kobject *
1130 zvol_probe(dev_t dev, int *part, void *arg)
1133 struct kobject *kobj;
1135 mutex_enter(&zvol_state_lock);
1136 zv = zvol_find_by_dev(dev);
1137 kobj = zv ? get_disk(zv->zv_disk) : NULL;
1138 mutex_exit(&zvol_state_lock);
1143 #ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS
1144 static struct block_device_operations zvol_ops = {
1146 .release = zvol_release,
1147 .ioctl = zvol_ioctl,
1148 .compat_ioctl = zvol_compat_ioctl,
1149 .media_changed = zvol_media_changed,
1150 .revalidate_disk = zvol_revalidate_disk,
1151 .getgeo = zvol_getgeo,
1152 .owner = THIS_MODULE,
1155 #else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1158 zvol_open_by_inode(struct inode *inode, struct file *file)
1160 return (zvol_open(inode->i_bdev, file->f_mode));
1164 zvol_release_by_inode(struct inode *inode, struct file *file)
1166 return (zvol_release(inode->i_bdev->bd_disk, file->f_mode));
1170 zvol_ioctl_by_inode(struct inode *inode, struct file *file,
1171 unsigned int cmd, unsigned long arg)
1173 if (file == NULL || inode == NULL)
1174 return (SET_ERROR(-EINVAL));
1176 return (zvol_ioctl(inode->i_bdev, file->f_mode, cmd, arg));
1179 #ifdef CONFIG_COMPAT
1181 zvol_compat_ioctl_by_inode(struct file *file,
1182 unsigned int cmd, unsigned long arg)
1185 return (SET_ERROR(-EINVAL));
1187 return (zvol_compat_ioctl(file->f_dentry->d_inode->i_bdev,
1188 file->f_mode, cmd, arg));
1191 #define zvol_compat_ioctl_by_inode NULL
1194 static struct block_device_operations zvol_ops = {
1195 .open = zvol_open_by_inode,
1196 .release = zvol_release_by_inode,
1197 .ioctl = zvol_ioctl_by_inode,
1198 .compat_ioctl = zvol_compat_ioctl_by_inode,
1199 .media_changed = zvol_media_changed,
1200 .revalidate_disk = zvol_revalidate_disk,
1201 .getgeo = zvol_getgeo,
1202 .owner = THIS_MODULE,
1204 #endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1207 * Allocate memory for a new zvol_state_t and setup the required
1208 * request queue and generic disk structures for the block device.
1210 static zvol_state_t *
1211 zvol_alloc(dev_t dev, const char *name)
1215 zv = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
1217 list_link_init(&zv->zv_next);
1219 zv->zv_queue = blk_alloc_queue(GFP_ATOMIC);
1220 if (zv->zv_queue == NULL)
1223 blk_queue_make_request(zv->zv_queue, zvol_request);
1225 #ifdef HAVE_BLK_QUEUE_FLUSH
1226 blk_queue_flush(zv->zv_queue, VDEV_REQ_FLUSH | VDEV_REQ_FUA);
1228 blk_queue_ordered(zv->zv_queue, QUEUE_ORDERED_DRAIN, NULL);
1229 #endif /* HAVE_BLK_QUEUE_FLUSH */
1231 zv->zv_disk = alloc_disk(ZVOL_MINORS);
1232 if (zv->zv_disk == NULL)
1235 zv->zv_queue->queuedata = zv;
1237 zv->zv_open_count = 0;
1238 strlcpy(zv->zv_name, name, MAXNAMELEN);
1240 mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
1241 avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
1242 sizeof (rl_t), offsetof(rl_t, r_node));
1243 zv->zv_znode.z_is_zvol = TRUE;
1245 zv->zv_disk->major = zvol_major;
1246 zv->zv_disk->first_minor = (dev & MINORMASK);
1247 zv->zv_disk->fops = &zvol_ops;
1248 zv->zv_disk->private_data = zv;
1249 zv->zv_disk->queue = zv->zv_queue;
1250 snprintf(zv->zv_disk->disk_name, DISK_NAME_LEN, "%s%d",
1251 ZVOL_DEV_NAME, (dev & MINORMASK));
1256 blk_cleanup_queue(zv->zv_queue);
1258 kmem_free(zv, sizeof (zvol_state_t));
1264 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1267 zvol_free(zvol_state_t *zv)
1269 avl_destroy(&zv->zv_znode.z_range_avl);
1270 mutex_destroy(&zv->zv_znode.z_range_lock);
1272 del_gendisk(zv->zv_disk);
1273 blk_cleanup_queue(zv->zv_queue);
1274 put_disk(zv->zv_disk);
1276 kmem_free(zv, sizeof (zvol_state_t));
1280 __zvol_snapdev_hidden(const char *name)
1287 parent = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1288 (void) strlcpy(parent, name, MAXPATHLEN);
1290 if ((atp = strrchr(parent, '@')) != NULL) {
1292 error = dsl_prop_get_integer(parent, "snapdev", &snapdev, NULL);
1293 if ((error == 0) && (snapdev == ZFS_SNAPDEV_HIDDEN))
1294 error = SET_ERROR(ENODEV);
1297 kmem_free(parent, MAXPATHLEN);
1299 return (SET_ERROR(error));
1303 __zvol_create_minor(const char *name, boolean_t ignore_snapdev)
1307 dmu_object_info_t *doi;
1313 ASSERT(MUTEX_HELD(&zvol_state_lock));
1315 zv = zvol_find_by_name(name);
1317 error = SET_ERROR(EEXIST);
1321 if (ignore_snapdev == B_FALSE) {
1322 error = __zvol_snapdev_hidden(name);
1327 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
1329 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
1333 error = dmu_object_info(os, ZVOL_OBJ, doi);
1335 goto out_dmu_objset_disown;
1337 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
1339 goto out_dmu_objset_disown;
1341 error = zvol_find_minor(&minor);
1343 goto out_dmu_objset_disown;
1345 zv = zvol_alloc(MKDEV(zvol_major, minor), name);
1347 error = SET_ERROR(EAGAIN);
1348 goto out_dmu_objset_disown;
1351 if (dmu_objset_is_snapshot(os))
1352 zv->zv_flags |= ZVOL_RDONLY;
1354 zv->zv_volblocksize = doi->doi_data_block_size;
1355 zv->zv_volsize = volsize;
1358 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1360 blk_queue_max_hw_sectors(zv->zv_queue, (DMU_MAX_ACCESS / 4) >> 9);
1361 blk_queue_max_segments(zv->zv_queue, UINT16_MAX);
1362 blk_queue_max_segment_size(zv->zv_queue, UINT_MAX);
1363 blk_queue_physical_block_size(zv->zv_queue, zv->zv_volblocksize);
1364 blk_queue_io_opt(zv->zv_queue, zv->zv_volblocksize);
1365 blk_queue_max_discard_sectors(zv->zv_queue,
1366 (zvol_max_discard_blocks * zv->zv_volblocksize) >> 9);
1367 blk_queue_discard_granularity(zv->zv_queue, zv->zv_volblocksize);
1368 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zv->zv_queue);
1369 #ifdef QUEUE_FLAG_NONROT
1370 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zv->zv_queue);
1372 #ifdef QUEUE_FLAG_ADD_RANDOM
1373 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, zv->zv_queue);
1376 if (spa_writeable(dmu_objset_spa(os))) {
1377 if (zil_replay_disable)
1378 zil_destroy(dmu_objset_zil(os), B_FALSE);
1380 zil_replay(os, zv, zvol_replay_vector);
1384 * When udev detects the addition of the device it will immediately
1385 * invoke blkid(8) to determine the type of content on the device.
1386 * Prefetching the blocks commonly scanned by blkid(8) will speed
1389 len = MIN(MAX(zvol_prefetch_bytes, 0), SPA_MAXBLOCKSIZE);
1391 dmu_prefetch(os, ZVOL_OBJ, 0, 0, len, ZIO_PRIORITY_SYNC_READ);
1392 dmu_prefetch(os, ZVOL_OBJ, 0, volsize - len, len,
1393 ZIO_PRIORITY_SYNC_READ);
1396 zv->zv_objset = NULL;
1397 out_dmu_objset_disown:
1398 dmu_objset_disown(os, zvol_tag);
1400 kmem_free(doi, sizeof (dmu_object_info_t));
1405 add_disk(zv->zv_disk);
1408 return (SET_ERROR(error));
1412 * Create a block device minor node and setup the linkage between it
1413 * and the specified volume. Once this function returns the block
1414 * device is live and ready for use.
1417 zvol_create_minor(const char *name)
1421 mutex_enter(&zvol_state_lock);
1422 error = __zvol_create_minor(name, B_FALSE);
1423 mutex_exit(&zvol_state_lock);
1425 return (SET_ERROR(error));
1429 __zvol_remove_minor(const char *name)
1433 ASSERT(MUTEX_HELD(&zvol_state_lock));
1435 zv = zvol_find_by_name(name);
1437 return (SET_ERROR(ENXIO));
1439 if (zv->zv_open_count > 0)
1440 return (SET_ERROR(EBUSY));
1449 * Remove a block device minor node for the specified volume.
1452 zvol_remove_minor(const char *name)
1456 mutex_enter(&zvol_state_lock);
1457 error = __zvol_remove_minor(name);
1458 mutex_exit(&zvol_state_lock);
1460 return (SET_ERROR(error));
1464 * Rename a block device minor mode for the specified volume.
1467 __zvol_rename_minor(zvol_state_t *zv, const char *newname)
1469 int readonly = get_disk_ro(zv->zv_disk);
1471 ASSERT(MUTEX_HELD(&zvol_state_lock));
1473 strlcpy(zv->zv_name, newname, sizeof (zv->zv_name));
1476 * The block device's read-only state is briefly changed causing
1477 * a KOBJ_CHANGE uevent to be issued. This ensures udev detects
1478 * the name change and fixes the symlinks. This does not change
1479 * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
1480 * changes. This would normally be done using kobject_uevent() but
1481 * that is a GPL-only symbol which is why we need this workaround.
1483 set_disk_ro(zv->zv_disk, !readonly);
1484 set_disk_ro(zv->zv_disk, readonly);
1488 zvol_create_minors_cb(const char *dsname, void *arg)
1490 (void) zvol_create_minor(dsname);
1496 * Create minors for specified dataset including children and snapshots.
1499 zvol_create_minors(const char *name)
1503 if (!zvol_inhibit_dev)
1504 error = dmu_objset_find((char *)name, zvol_create_minors_cb,
1505 NULL, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1507 return (SET_ERROR(error));
1511 * Remove minors for specified dataset including children and snapshots.
1514 zvol_remove_minors(const char *name)
1516 zvol_state_t *zv, *zv_next;
1517 int namelen = ((name) ? strlen(name) : 0);
1519 if (zvol_inhibit_dev)
1522 mutex_enter(&zvol_state_lock);
1524 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1525 zv_next = list_next(&zvol_state_list, zv);
1527 if (name == NULL || strcmp(zv->zv_name, name) == 0 ||
1528 (strncmp(zv->zv_name, name, namelen) == 0 &&
1529 zv->zv_name[namelen] == '/')) {
1535 mutex_exit(&zvol_state_lock);
1539 * Rename minors for specified dataset including children and snapshots.
1542 zvol_rename_minors(const char *oldname, const char *newname)
1544 zvol_state_t *zv, *zv_next;
1545 int oldnamelen, newnamelen;
1548 if (zvol_inhibit_dev)
1551 oldnamelen = strlen(oldname);
1552 newnamelen = strlen(newname);
1553 name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1555 mutex_enter(&zvol_state_lock);
1557 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1558 zv_next = list_next(&zvol_state_list, zv);
1560 if (strcmp(zv->zv_name, oldname) == 0) {
1561 __zvol_rename_minor(zv, newname);
1562 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 &&
1563 (zv->zv_name[oldnamelen] == '/' ||
1564 zv->zv_name[oldnamelen] == '@')) {
1565 snprintf(name, MAXNAMELEN, "%s%c%s", newname,
1566 zv->zv_name[oldnamelen],
1567 zv->zv_name + oldnamelen + 1);
1568 __zvol_rename_minor(zv, name);
1572 mutex_exit(&zvol_state_lock);
1574 kmem_free(name, MAXNAMELEN);
1578 snapdev_snapshot_changed_cb(const char *dsname, void *arg) {
1579 uint64_t snapdev = *(uint64_t *) arg;
1581 if (strchr(dsname, '@') == NULL)
1585 case ZFS_SNAPDEV_VISIBLE:
1586 mutex_enter(&zvol_state_lock);
1587 (void) __zvol_create_minor(dsname, B_TRUE);
1588 mutex_exit(&zvol_state_lock);
1590 case ZFS_SNAPDEV_HIDDEN:
1591 (void) zvol_remove_minor(dsname);
1599 zvol_set_snapdev(const char *dsname, uint64_t snapdev) {
1600 (void) dmu_objset_find((char *) dsname, snapdev_snapshot_changed_cb,
1601 &snapdev, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
1602 /* caller should continue to modify snapdev property */
1611 list_create(&zvol_state_list, sizeof (zvol_state_t),
1612 offsetof(zvol_state_t, zv_next));
1614 mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1616 error = register_blkdev(zvol_major, ZVOL_DRIVER);
1618 printk(KERN_INFO "ZFS: register_blkdev() failed %d\n", error);
1622 blk_register_region(MKDEV(zvol_major, 0), 1UL << MINORBITS,
1623 THIS_MODULE, zvol_probe, NULL, NULL);
1628 mutex_destroy(&zvol_state_lock);
1629 list_destroy(&zvol_state_list);
1631 return (SET_ERROR(error));
1637 zvol_remove_minors(NULL);
1638 blk_unregister_region(MKDEV(zvol_major, 0), 1UL << MINORBITS);
1639 unregister_blkdev(zvol_major, ZVOL_DRIVER);
1640 mutex_destroy(&zvol_state_lock);
1641 list_destroy(&zvol_state_list);
1644 module_param(zvol_inhibit_dev, uint, 0644);
1645 MODULE_PARM_DESC(zvol_inhibit_dev, "Do not create zvol device nodes");
1647 module_param(zvol_major, uint, 0444);
1648 MODULE_PARM_DESC(zvol_major, "Major number for zvol device");
1650 module_param(zvol_max_discard_blocks, ulong, 0444);
1651 MODULE_PARM_DESC(zvol_max_discard_blocks, "Max number of blocks to discard");
1653 module_param(zvol_prefetch_bytes, uint, 0644);
1654 MODULE_PARM_DESC(zvol_prefetch_bytes, "Prefetch N bytes at zvol start+end");