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);
243 * Added check_disk_size_change() helper function.
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);
250 zv->zv_volsize = volsize;
252 (void) check_disk_change(bdev);
253 #endif /* HAVE_CHECK_DISK_SIZE_CHANGE */
259 * Sanity check volume size.
262 zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
265 return (SET_ERROR(EINVAL));
267 if (volsize % blocksize != 0)
268 return (SET_ERROR(EINVAL));
271 if (volsize - 1 > MAXOFFSET_T)
272 return (SET_ERROR(EOVERFLOW));
278 * Ensure the zap is flushed then inform the VFS of the capacity change.
281 zvol_update_volsize(uint64_t volsize, objset_t *os)
286 ASSERT(MUTEX_HELD(&zvol_state_lock));
288 tx = dmu_tx_create(os);
289 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
290 error = dmu_tx_assign(tx, TXG_WAIT);
293 return (SET_ERROR(error));
296 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
301 error = dmu_free_long_range(os,
302 ZVOL_OBJ, volsize, DMU_OBJECT_END);
308 zvol_update_live_volsize(zvol_state_t *zv, uint64_t volsize)
310 zvol_size_changed(zv, volsize);
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.
322 * Set ZFS_PROP_VOLSIZE set entry point.
325 zvol_set_volsize(const char *name, uint64_t volsize)
327 zvol_state_t *zv = NULL;
330 dmu_object_info_t *doi;
332 boolean_t owned = B_FALSE;
334 error = dsl_prop_get_integer(name,
335 zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL);
337 return (SET_ERROR(error));
339 return (SET_ERROR(EROFS));
341 mutex_enter(&zvol_state_lock);
342 zv = zvol_find_by_name(name);
344 if (zv == NULL || zv->zv_objset == NULL) {
345 if ((error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE,
347 mutex_exit(&zvol_state_lock);
348 return (SET_ERROR(error));
357 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
359 if ((error = dmu_object_info(os, ZVOL_OBJ, doi)) ||
360 (error = zvol_check_volsize(volsize, doi->doi_data_block_size)))
363 error = zvol_update_volsize(volsize, os);
364 kmem_free(doi, sizeof (dmu_object_info_t));
366 if (error == 0 && zv != NULL)
367 error = zvol_update_live_volsize(zv, volsize);
370 dmu_objset_disown(os, FTAG);
372 zv->zv_objset = NULL;
374 mutex_exit(&zvol_state_lock);
379 * Sanity check volume block size.
382 zvol_check_volblocksize(const char *name, uint64_t volblocksize)
384 /* Record sizes above 128k need the feature to be enabled */
385 if (volblocksize > SPA_OLD_MAXBLOCKSIZE) {
389 if ((error = spa_open(name, &spa, FTAG)) != 0)
392 if (!spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
393 spa_close(spa, FTAG);
394 return (SET_ERROR(ENOTSUP));
398 * We don't allow setting the property above 1MB,
399 * unless the tunable has been changed.
401 if (volblocksize > zfs_max_recordsize)
402 return (SET_ERROR(EDOM));
404 spa_close(spa, FTAG);
407 if (volblocksize < SPA_MINBLOCKSIZE ||
408 volblocksize > SPA_MAXBLOCKSIZE ||
410 return (SET_ERROR(EDOM));
416 * Set ZFS_PROP_VOLBLOCKSIZE set entry point.
419 zvol_set_volblocksize(const char *name, uint64_t volblocksize)
425 mutex_enter(&zvol_state_lock);
427 zv = zvol_find_by_name(name);
429 error = SET_ERROR(ENXIO);
433 if (zv->zv_flags & ZVOL_RDONLY) {
434 error = SET_ERROR(EROFS);
438 tx = dmu_tx_create(zv->zv_objset);
439 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
440 error = dmu_tx_assign(tx, TXG_WAIT);
444 error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
445 volblocksize, 0, tx);
446 if (error == ENOTSUP)
447 error = SET_ERROR(EBUSY);
450 zv->zv_volblocksize = volblocksize;
453 mutex_exit(&zvol_state_lock);
455 return (SET_ERROR(error));
459 * Replay a TX_WRITE ZIL transaction that didn't get committed
460 * after a system failure
463 zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
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;
473 byteswap_uint64_array(lr, sizeof (*lr));
475 tx = dmu_tx_create(os);
476 dmu_tx_hold_write(tx, ZVOL_OBJ, off, len);
477 error = dmu_tx_assign(tx, TXG_WAIT);
481 dmu_write(os, ZVOL_OBJ, off, len, data, tx);
485 return (SET_ERROR(error));
489 zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
491 return (SET_ERROR(ENOTSUP));
495 * Callback vectors for replaying records.
496 * Only TX_WRITE is needed for zvol.
498 zil_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 */
515 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
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().
520 ssize_t zvol_immediate_write_sz = 32768;
523 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, uint64_t offset,
524 uint64_t size, int sync)
526 uint32_t blocksize = zv->zv_volblocksize;
527 zilog_t *zilog = zv->zv_zilog;
529 ssize_t immediate_write_sz;
531 if (zil_replaying(zilog, tx))
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);
543 itx_wr_state_t write_state;
546 * Unlike zfs_log_write() we can be called with
547 * up to DMU_MAX_ACCESS/2 (5MB) writes.
549 if (blocksize > immediate_write_sz && !slogging &&
550 size >= blocksize && offset % blocksize == 0) {
551 write_state = WR_INDIRECT; /* uses dmu_sync */
554 write_state = WR_COPIED;
555 len = MIN(ZIL_MAX_LOG_DATA, size);
557 write_state = WR_NEED_COPY;
558 len = MIN(ZIL_MAX_LOG_DATA, size);
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;
572 itx->itx_wr_state = write_state;
573 if (write_state == WR_NEED_COPY)
575 lr->lr_foid = ZVOL_OBJ;
576 lr->lr_offset = offset;
579 BP_ZERO(&lr->lr_blkptr);
581 itx->itx_private = zv;
582 itx->itx_sync = sync;
584 (void) zil_itx_assign(zilog, itx, tx);
592 zvol_write(struct bio *bio)
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);
601 if (bio->bi_rw & VDEV_REQ_FLUSH)
602 zil_commit(zv->zv_zilog, ZVOL_OBJ);
605 * Some requests are just for flush and nothing else.
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_bio(zv->zv_objset, ZVOL_OBJ, bio, 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 len = BIO_BI_SIZE(bio);
697 rl = zfs_range_lock(&zv->zv_znode, offset, len, RL_READER);
699 error = dmu_read_bio(zv->zv_objset, ZVOL_OBJ, bio);
701 zfs_range_unlock(rl);
703 /* convert checksum errors into IO errors */
705 error = SET_ERROR(EIO);
710 static MAKE_REQUEST_FN_RET
711 zvol_request(struct request_queue *q, struct bio *bio)
713 zvol_state_t *zv = q->queuedata;
714 fstrans_cookie_t cookie = spl_fstrans_mark();
715 uint64_t offset = BIO_BI_SECTOR(bio);
716 unsigned int sectors = bio_sectors(bio);
717 int rw = bio_data_dir(bio);
718 #ifdef HAVE_GENERIC_IO_ACCT
719 unsigned long start = jiffies;
723 if (bio_has_data(bio) && offset + sectors >
724 get_capacity(zv->zv_disk)) {
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);
734 generic_start_io_acct(rw, sectors, &zv->zv_disk->part0);
737 if (unlikely(zv->zv_flags & ZVOL_RDONLY)) {
738 error = SET_ERROR(EROFS);
742 if (bio->bi_rw & VDEV_REQ_DISCARD) {
743 error = zvol_discard(bio);
747 error = zvol_write(bio);
749 error = zvol_read(bio);
752 generic_end_io_acct(rw, &zv->zv_disk->part0, start);
754 BIO_END_IO(bio, -error);
755 spl_fstrans_unmark(cookie);
756 #ifdef HAVE_MAKE_REQUEST_FN_RET_INT
758 #elif defined(HAVE_MAKE_REQUEST_FN_RET_QC)
759 return (BLK_QC_T_NONE);
764 zvol_get_done(zgd_t *zgd, int error)
767 dmu_buf_rele(zgd->zgd_db, zgd);
769 zfs_range_unlock(zgd->zgd_rl);
771 if (error == 0 && zgd->zgd_bp)
772 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
774 kmem_free(zgd, sizeof (zgd_t));
778 * Get data to generate a TX_WRITE intent log record.
781 zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
783 zvol_state_t *zv = arg;
784 objset_t *os = zv->zv_objset;
785 uint64_t object = ZVOL_OBJ;
786 uint64_t offset = lr->lr_offset;
787 uint64_t size = lr->lr_length;
788 blkptr_t *bp = &lr->lr_blkptr;
796 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
797 zgd->zgd_zilog = zv->zv_zilog;
798 zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
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.
807 if (buf != NULL) { /* immediate write */
808 error = dmu_read(os, object, offset, size, buf,
809 DMU_READ_NO_PREFETCH);
811 size = zv->zv_volblocksize;
812 offset = P2ALIGN_TYPED(offset, size, uint64_t);
813 error = dmu_buf_hold(os, object, offset, zgd, &db,
814 DMU_READ_NO_PREFETCH);
816 blkptr_t *obp = dmu_buf_get_blkptr(db);
818 ASSERT(BP_IS_HOLE(bp));
823 zgd->zgd_bp = &lr->lr_blkptr;
826 ASSERT(db->db_offset == offset);
827 ASSERT(db->db_size == size);
829 error = dmu_sync(zio, lr->lr_common.lrc_txg,
837 zvol_get_done(zgd, error);
839 return (SET_ERROR(error));
843 * The zvol_state_t's are inserted in increasing MINOR(dev_t) order.
846 zvol_insert(zvol_state_t *zv_insert)
848 zvol_state_t *zv = NULL;
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;
853 zv = list_next(&zvol_state_list, zv)) {
854 if (MINOR(zv->zv_dev) > MINOR(zv_insert->zv_dev))
858 list_insert_before(&zvol_state_list, zv, zv_insert);
862 * Simply remove the zvol from to list of zvols.
865 zvol_remove(zvol_state_t *zv_remove)
867 ASSERT(MUTEX_HELD(&zvol_state_lock));
868 list_remove(&zvol_state_list, zv_remove);
872 zvol_first_open(zvol_state_t *zv)
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.
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.
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.
897 if (!mutex_owned(&spa_namespace_lock)) {
898 locked = mutex_tryenter(&spa_namespace_lock);
900 return (-SET_ERROR(ERESTARTSYS));
903 error = dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL);
907 /* lie and say we're read-only */
908 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, 1, zvol_tag, &os);
912 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
914 dmu_objset_disown(os, zvol_tag);
919 error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
921 dmu_objset_disown(os, zvol_tag);
925 set_capacity(zv->zv_disk, volsize >> 9);
926 zv->zv_volsize = volsize;
927 zv->zv_zilog = zil_open(os, zvol_get_data);
929 if (ro || dmu_objset_is_snapshot(os) ||
930 !spa_writeable(dmu_objset_spa(os))) {
931 set_disk_ro(zv->zv_disk, 1);
932 zv->zv_flags |= ZVOL_RDONLY;
934 set_disk_ro(zv->zv_disk, 0);
935 zv->zv_flags &= ~ZVOL_RDONLY;
940 mutex_exit(&spa_namespace_lock);
942 return (SET_ERROR(-error));
946 zvol_last_close(zvol_state_t *zv)
948 zil_close(zv->zv_zilog);
951 dmu_buf_rele(zv->zv_dbuf, zvol_tag);
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);
962 dmu_objset_disown(zv->zv_objset, zvol_tag);
963 zv->zv_objset = NULL;
967 zvol_open(struct block_device *bdev, fmode_t flag)
969 zvol_state_t *zv = bdev->bd_disk->private_data;
970 int error = 0, drop_mutex = 0;
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.
978 if (!mutex_owned(&zvol_state_lock)) {
979 mutex_enter(&zvol_state_lock);
983 ASSERT3P(zv, !=, NULL);
985 if (zv->zv_open_count == 0) {
986 error = zvol_first_open(zv);
991 if ((flag & FMODE_WRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
999 if (zv->zv_open_count == 0)
1000 zvol_last_close(zv);
1004 mutex_exit(&zvol_state_lock);
1006 check_disk_change(bdev);
1008 return (SET_ERROR(error));
1011 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1016 zvol_release(struct gendisk *disk, fmode_t mode)
1018 zvol_state_t *zv = disk->private_data;
1021 if (!mutex_owned(&zvol_state_lock)) {
1022 mutex_enter(&zvol_state_lock);
1026 if (zv->zv_open_count > 0) {
1027 zv->zv_open_count--;
1028 if (zv->zv_open_count == 0)
1029 zvol_last_close(zv);
1033 mutex_exit(&zvol_state_lock);
1035 #ifndef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1041 zvol_ioctl(struct block_device *bdev, fmode_t mode,
1042 unsigned int cmd, unsigned long arg)
1044 zvol_state_t *zv = bdev->bd_disk->private_data;
1048 return (SET_ERROR(-ENXIO));
1052 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1055 error = copy_to_user((void *)arg, zv->zv_name, MAXNAMELEN);
1064 return (SET_ERROR(error));
1067 #ifdef CONFIG_COMPAT
1069 zvol_compat_ioctl(struct block_device *bdev, fmode_t mode,
1070 unsigned cmd, unsigned long arg)
1072 return (zvol_ioctl(bdev, mode, cmd, arg));
1075 #define zvol_compat_ioctl NULL
1078 static int zvol_media_changed(struct gendisk *disk)
1080 zvol_state_t *zv = disk->private_data;
1082 return (zv->zv_changed);
1085 static int zvol_revalidate_disk(struct gendisk *disk)
1087 zvol_state_t *zv = disk->private_data;
1090 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
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.
1102 zvol_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1104 zvol_state_t *zv = bdev->bd_disk->private_data;
1105 sector_t sectors = get_capacity(zv->zv_disk);
1107 if (sectors > 2048) {
1116 geo->cylinders = sectors / (geo->heads * geo->sectors);
1121 static struct kobject *
1122 zvol_probe(dev_t dev, int *part, void *arg)
1125 struct kobject *kobj;
1127 mutex_enter(&zvol_state_lock);
1128 zv = zvol_find_by_dev(dev);
1129 kobj = zv ? get_disk(zv->zv_disk) : NULL;
1130 mutex_exit(&zvol_state_lock);
1135 #ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS
1136 static struct block_device_operations zvol_ops = {
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,
1147 #else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1150 zvol_open_by_inode(struct inode *inode, struct file *file)
1152 return (zvol_open(inode->i_bdev, file->f_mode));
1156 zvol_release_by_inode(struct inode *inode, struct file *file)
1158 return (zvol_release(inode->i_bdev->bd_disk, file->f_mode));
1162 zvol_ioctl_by_inode(struct inode *inode, struct file *file,
1163 unsigned int cmd, unsigned long arg)
1165 if (file == NULL || inode == NULL)
1166 return (SET_ERROR(-EINVAL));
1168 return (zvol_ioctl(inode->i_bdev, file->f_mode, cmd, arg));
1171 #ifdef CONFIG_COMPAT
1173 zvol_compat_ioctl_by_inode(struct file *file,
1174 unsigned int cmd, unsigned long arg)
1177 return (SET_ERROR(-EINVAL));
1179 return (zvol_compat_ioctl(file->f_dentry->d_inode->i_bdev,
1180 file->f_mode, cmd, arg));
1183 #define zvol_compat_ioctl_by_inode NULL
1186 static struct block_device_operations zvol_ops = {
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,
1196 #endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1199 * Allocate memory for a new zvol_state_t and setup the required
1200 * request queue and generic disk structures for the block device.
1202 static zvol_state_t *
1203 zvol_alloc(dev_t dev, const char *name)
1207 zv = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
1209 list_link_init(&zv->zv_next);
1211 zv->zv_queue = blk_alloc_queue(GFP_ATOMIC);
1212 if (zv->zv_queue == NULL)
1215 blk_queue_make_request(zv->zv_queue, zvol_request);
1217 #ifdef HAVE_BLK_QUEUE_FLUSH
1218 blk_queue_flush(zv->zv_queue, VDEV_REQ_FLUSH | VDEV_REQ_FUA);
1220 blk_queue_ordered(zv->zv_queue, QUEUE_ORDERED_DRAIN, NULL);
1221 #endif /* HAVE_BLK_QUEUE_FLUSH */
1223 zv->zv_disk = alloc_disk(ZVOL_MINORS);
1224 if (zv->zv_disk == NULL)
1227 zv->zv_queue->queuedata = zv;
1229 zv->zv_open_count = 0;
1230 strlcpy(zv->zv_name, name, MAXNAMELEN);
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));
1235 zv->zv_znode.z_is_zvol = TRUE;
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;
1242 snprintf(zv->zv_disk->disk_name, DISK_NAME_LEN, "%s%d",
1243 ZVOL_DEV_NAME, (dev & MINORMASK));
1248 blk_cleanup_queue(zv->zv_queue);
1250 kmem_free(zv, sizeof (zvol_state_t));
1256 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1259 zvol_free(zvol_state_t *zv)
1261 avl_destroy(&zv->zv_znode.z_range_avl);
1262 mutex_destroy(&zv->zv_znode.z_range_lock);
1264 del_gendisk(zv->zv_disk);
1265 blk_cleanup_queue(zv->zv_queue);
1266 put_disk(zv->zv_disk);
1268 kmem_free(zv, sizeof (zvol_state_t));
1272 __zvol_snapdev_hidden(const char *name)
1279 parent = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1280 (void) strlcpy(parent, name, MAXPATHLEN);
1282 if ((atp = strrchr(parent, '@')) != NULL) {
1284 error = dsl_prop_get_integer(parent, "snapdev", &snapdev, NULL);
1285 if ((error == 0) && (snapdev == ZFS_SNAPDEV_HIDDEN))
1286 error = SET_ERROR(ENODEV);
1289 kmem_free(parent, MAXPATHLEN);
1291 return (SET_ERROR(error));
1295 __zvol_create_minor(const char *name, boolean_t ignore_snapdev)
1299 dmu_object_info_t *doi;
1305 ASSERT(MUTEX_HELD(&zvol_state_lock));
1307 zv = zvol_find_by_name(name);
1309 error = SET_ERROR(EEXIST);
1313 if (ignore_snapdev == B_FALSE) {
1314 error = __zvol_snapdev_hidden(name);
1319 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
1321 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
1325 error = dmu_object_info(os, ZVOL_OBJ, doi);
1327 goto out_dmu_objset_disown;
1329 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
1331 goto out_dmu_objset_disown;
1333 error = zvol_find_minor(&minor);
1335 goto out_dmu_objset_disown;
1337 zv = zvol_alloc(MKDEV(zvol_major, minor), name);
1339 error = SET_ERROR(EAGAIN);
1340 goto out_dmu_objset_disown;
1343 if (dmu_objset_is_snapshot(os))
1344 zv->zv_flags |= ZVOL_RDONLY;
1346 zv->zv_volblocksize = doi->doi_data_block_size;
1347 zv->zv_volsize = volsize;
1350 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1352 blk_queue_max_hw_sectors(zv->zv_queue, (DMU_MAX_ACCESS / 4) >> 9);
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);
1357 blk_queue_max_discard_sectors(zv->zv_queue,
1358 (zvol_max_discard_blocks * zv->zv_volblocksize) >> 9);
1359 blk_queue_discard_granularity(zv->zv_queue, zv->zv_volblocksize);
1360 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zv->zv_queue);
1361 #ifdef QUEUE_FLAG_NONROT
1362 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zv->zv_queue);
1364 #ifdef QUEUE_FLAG_ADD_RANDOM
1365 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, zv->zv_queue);
1368 if (spa_writeable(dmu_objset_spa(os))) {
1369 if (zil_replay_disable)
1370 zil_destroy(dmu_objset_zil(os), B_FALSE);
1372 zil_replay(os, zv, zvol_replay_vector);
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
1381 len = MIN(MAX(zvol_prefetch_bytes, 0), SPA_MAXBLOCKSIZE);
1383 dmu_prefetch(os, ZVOL_OBJ, 0, len);
1384 dmu_prefetch(os, ZVOL_OBJ, volsize - len, len);
1387 zv->zv_objset = NULL;
1388 out_dmu_objset_disown:
1389 dmu_objset_disown(os, zvol_tag);
1391 kmem_free(doi, sizeof (dmu_object_info_t));
1396 add_disk(zv->zv_disk);
1399 return (SET_ERROR(error));
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.
1408 zvol_create_minor(const char *name)
1412 mutex_enter(&zvol_state_lock);
1413 error = __zvol_create_minor(name, B_FALSE);
1414 mutex_exit(&zvol_state_lock);
1416 return (SET_ERROR(error));
1420 __zvol_remove_minor(const char *name)
1424 ASSERT(MUTEX_HELD(&zvol_state_lock));
1426 zv = zvol_find_by_name(name);
1428 return (SET_ERROR(ENXIO));
1430 if (zv->zv_open_count > 0)
1431 return (SET_ERROR(EBUSY));
1440 * Remove a block device minor node for the specified volume.
1443 zvol_remove_minor(const char *name)
1447 mutex_enter(&zvol_state_lock);
1448 error = __zvol_remove_minor(name);
1449 mutex_exit(&zvol_state_lock);
1451 return (SET_ERROR(error));
1455 * Rename a block device minor mode for the specified volume.
1458 __zvol_rename_minor(zvol_state_t *zv, const char *newname)
1460 int readonly = get_disk_ro(zv->zv_disk);
1462 ASSERT(MUTEX_HELD(&zvol_state_lock));
1464 strlcpy(zv->zv_name, newname, sizeof (zv->zv_name));
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.
1474 set_disk_ro(zv->zv_disk, !readonly);
1475 set_disk_ro(zv->zv_disk, readonly);
1479 zvol_create_minors_cb(const char *dsname, void *arg)
1481 (void) zvol_create_minor(dsname);
1487 * Create minors for specified dataset including children and snapshots.
1490 zvol_create_minors(const char *name)
1494 if (!zvol_inhibit_dev)
1495 error = dmu_objset_find((char *)name, zvol_create_minors_cb,
1496 NULL, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1498 return (SET_ERROR(error));
1502 * Remove minors for specified dataset including children and snapshots.
1505 zvol_remove_minors(const char *name)
1507 zvol_state_t *zv, *zv_next;
1508 int namelen = ((name) ? strlen(name) : 0);
1510 if (zvol_inhibit_dev)
1513 mutex_enter(&zvol_state_lock);
1515 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1516 zv_next = list_next(&zvol_state_list, zv);
1518 if (name == NULL || strcmp(zv->zv_name, name) == 0 ||
1519 (strncmp(zv->zv_name, name, namelen) == 0 &&
1520 zv->zv_name[namelen] == '/')) {
1526 mutex_exit(&zvol_state_lock);
1530 * Rename minors for specified dataset including children and snapshots.
1533 zvol_rename_minors(const char *oldname, const char *newname)
1535 zvol_state_t *zv, *zv_next;
1536 int oldnamelen, newnamelen;
1539 if (zvol_inhibit_dev)
1542 oldnamelen = strlen(oldname);
1543 newnamelen = strlen(newname);
1544 name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1546 mutex_enter(&zvol_state_lock);
1548 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1549 zv_next = list_next(&zvol_state_list, zv);
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);
1563 mutex_exit(&zvol_state_lock);
1565 kmem_free(name, MAXNAMELEN);
1569 snapdev_snapshot_changed_cb(const char *dsname, void *arg) {
1570 uint64_t snapdev = *(uint64_t *) arg;
1572 if (strchr(dsname, '@') == NULL)
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);
1581 case ZFS_SNAPDEV_HIDDEN:
1582 (void) zvol_remove_minor(dsname);
1590 zvol_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 */
1602 list_create(&zvol_state_list, sizeof (zvol_state_t),
1603 offsetof(zvol_state_t, zv_next));
1605 mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1607 error = register_blkdev(zvol_major, ZVOL_DRIVER);
1609 printk(KERN_INFO "ZFS: register_blkdev() failed %d\n", error);
1613 blk_register_region(MKDEV(zvol_major, 0), 1UL << MINORBITS,
1614 THIS_MODULE, zvol_probe, NULL, NULL);
1619 mutex_destroy(&zvol_state_lock);
1620 list_destroy(&zvol_state_list);
1622 return (SET_ERROR(error));
1628 zvol_remove_minors(NULL);
1629 blk_unregister_region(MKDEV(zvol_major, 0), 1UL << MINORBITS);
1630 unregister_blkdev(zvol_major, ZVOL_DRIVER);
1631 mutex_destroy(&zvol_state_lock);
1632 list_destroy(&zvol_state_list);
1635 module_param(zvol_inhibit_dev, uint, 0644);
1636 MODULE_PARM_DESC(zvol_inhibit_dev, "Do not create zvol device nodes");
1638 module_param(zvol_major, uint, 0444);
1639 MODULE_PARM_DESC(zvol_major, "Major number for zvol device");
1641 module_param(zvol_max_discard_blocks, ulong, 0444);
1642 MODULE_PARM_DESC(zvol_max_discard_blocks, "Max number of blocks to discard");
1644 module_param(zvol_prefetch_bytes, uint, 0644);
1645 MODULE_PARM_DESC(zvol_prefetch_bytes, "Prefetch N bytes at zvol start+end");