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/zil_impl.h>
45 #include <sys/zfs_rlock.h>
46 #include <sys/zfs_znode.h>
48 #include <linux/blkdev_compat.h>
50 unsigned int zvol_inhibit_dev = 0;
51 unsigned int zvol_major = ZVOL_MAJOR;
52 unsigned int zvol_threads = 32;
53 unsigned long zvol_max_discard_blocks = 16384;
55 static taskq_t *zvol_taskq;
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 spinlock_t zv_lock; /* request queue lock */
78 list_node_t zv_next; /* next zvol_state_t linkage */
81 #define ZVOL_RDONLY 0x1
84 * Find the next available range of ZVOL_MINORS minor numbers. The
85 * zvol_state_list is kept in ascending minor order so we simply need
86 * to scan the list for the first gap in the sequence. This allows us
87 * to recycle minor number as devices are created and removed.
90 zvol_find_minor(unsigned *minor)
95 ASSERT(MUTEX_HELD(&zvol_state_lock));
96 for (zv = list_head(&zvol_state_list); zv != NULL;
97 zv = list_next(&zvol_state_list, zv), *minor += ZVOL_MINORS) {
98 if (MINOR(zv->zv_dev) != MINOR(*minor))
102 /* All minors are in use */
103 if (*minor >= (1 << MINORBITS))
104 return (SET_ERROR(ENXIO));
110 * Find a zvol_state_t given the full major+minor dev_t.
112 static zvol_state_t *
113 zvol_find_by_dev(dev_t dev)
117 ASSERT(MUTEX_HELD(&zvol_state_lock));
118 for (zv = list_head(&zvol_state_list); zv != NULL;
119 zv = list_next(&zvol_state_list, zv)) {
120 if (zv->zv_dev == dev)
128 * Find a zvol_state_t given the name provided at zvol_alloc() time.
130 static zvol_state_t *
131 zvol_find_by_name(const char *name)
135 ASSERT(MUTEX_HELD(&zvol_state_lock));
136 for (zv = list_head(&zvol_state_list); zv != NULL;
137 zv = list_next(&zvol_state_list, zv)) {
138 if (strncmp(zv->zv_name, name, MAXNAMELEN) == 0)
147 * Given a path, return TRUE if path is a ZVOL.
150 zvol_is_zvol(const char *device)
152 struct block_device *bdev;
155 bdev = lookup_bdev(device);
159 major = MAJOR(bdev->bd_dev);
162 if (major == zvol_major)
169 * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation.
172 zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
174 zfs_creat_t *zct = arg;
175 nvlist_t *nvprops = zct->zct_props;
177 uint64_t volblocksize, volsize;
179 VERIFY(nvlist_lookup_uint64(nvprops,
180 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
181 if (nvlist_lookup_uint64(nvprops,
182 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
183 volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
186 * These properties must be removed from the list so the generic
187 * property setting step won't apply to them.
189 VERIFY(nvlist_remove_all(nvprops,
190 zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
191 (void) nvlist_remove_all(nvprops,
192 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
194 error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
198 error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
202 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
207 * ZFS_IOC_OBJSET_STATS entry point.
210 zvol_get_stats(objset_t *os, nvlist_t *nv)
213 dmu_object_info_t *doi;
216 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
218 return (SET_ERROR(error));
220 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
221 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
222 error = dmu_object_info(os, ZVOL_OBJ, doi);
225 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
226 doi->doi_data_block_size);
229 kmem_free(doi, sizeof (dmu_object_info_t));
231 return (SET_ERROR(error));
235 zvol_size_changed(zvol_state_t *zv, uint64_t volsize)
237 struct block_device *bdev;
239 bdev = bdget_disk(zv->zv_disk, 0);
244 * Added check_disk_size_change() helper function.
246 #ifdef HAVE_CHECK_DISK_SIZE_CHANGE
247 set_capacity(zv->zv_disk, volsize >> 9);
248 zv->zv_volsize = volsize;
249 check_disk_size_change(zv->zv_disk, bdev);
251 zv->zv_volsize = volsize;
253 (void) check_disk_change(bdev);
254 #endif /* HAVE_CHECK_DISK_SIZE_CHANGE */
260 * Sanity check volume size.
263 zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
266 return (SET_ERROR(EINVAL));
268 if (volsize % blocksize != 0)
269 return (SET_ERROR(EINVAL));
272 if (volsize - 1 > MAXOFFSET_T)
273 return (SET_ERROR(EOVERFLOW));
279 * Ensure the zap is flushed then inform the VFS of the capacity change.
282 zvol_update_volsize(uint64_t volsize, objset_t *os)
287 ASSERT(MUTEX_HELD(&zvol_state_lock));
289 tx = dmu_tx_create(os);
290 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
291 error = dmu_tx_assign(tx, TXG_WAIT);
294 return (SET_ERROR(error));
297 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
302 error = dmu_free_long_range(os,
303 ZVOL_OBJ, volsize, DMU_OBJECT_END);
309 zvol_update_live_volsize(zvol_state_t *zv, uint64_t volsize)
311 zvol_size_changed(zv, volsize);
314 * We should post a event here describing the expansion. However,
315 * the zfs_ereport_post() interface doesn't nicely support posting
316 * events for zvols, it assumes events relate to vdevs or zios.
323 * Set ZFS_PROP_VOLSIZE set entry point.
326 zvol_set_volsize(const char *name, uint64_t volsize)
328 zvol_state_t *zv = NULL;
331 dmu_object_info_t *doi;
333 boolean_t owned = B_FALSE;
335 error = dsl_prop_get_integer(name,
336 zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL);
338 return (SET_ERROR(error));
340 return (SET_ERROR(EROFS));
342 mutex_enter(&zvol_state_lock);
343 zv = zvol_find_by_name(name);
345 if (zv == NULL || zv->zv_objset == NULL) {
346 if ((error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE,
348 mutex_exit(&zvol_state_lock);
349 return (SET_ERROR(error));
358 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
360 if ((error = dmu_object_info(os, ZVOL_OBJ, doi)) ||
361 (error = zvol_check_volsize(volsize, doi->doi_data_block_size)))
364 error = zvol_update_volsize(volsize, os);
365 kmem_free(doi, sizeof (dmu_object_info_t));
367 if (error == 0 && zv != NULL)
368 error = zvol_update_live_volsize(zv, volsize);
371 dmu_objset_disown(os, FTAG);
373 zv->zv_objset = NULL;
375 mutex_exit(&zvol_state_lock);
380 * Sanity check volume block size.
383 zvol_check_volblocksize(uint64_t volblocksize)
385 if (volblocksize < SPA_MINBLOCKSIZE ||
386 volblocksize > SPA_MAXBLOCKSIZE ||
388 return (SET_ERROR(EDOM));
394 * Set ZFS_PROP_VOLBLOCKSIZE set entry point.
397 zvol_set_volblocksize(const char *name, uint64_t volblocksize)
403 mutex_enter(&zvol_state_lock);
405 zv = zvol_find_by_name(name);
407 error = SET_ERROR(ENXIO);
411 if (zv->zv_flags & ZVOL_RDONLY) {
412 error = SET_ERROR(EROFS);
416 tx = dmu_tx_create(zv->zv_objset);
417 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
418 error = dmu_tx_assign(tx, TXG_WAIT);
422 error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
423 volblocksize, 0, tx);
424 if (error == ENOTSUP)
425 error = SET_ERROR(EBUSY);
428 zv->zv_volblocksize = volblocksize;
431 mutex_exit(&zvol_state_lock);
433 return (SET_ERROR(error));
437 * Replay a TX_WRITE ZIL transaction that didn't get committed
438 * after a system failure
441 zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
443 objset_t *os = zv->zv_objset;
444 char *data = (char *)(lr + 1); /* data follows lr_write_t */
445 uint64_t off = lr->lr_offset;
446 uint64_t len = lr->lr_length;
451 byteswap_uint64_array(lr, sizeof (*lr));
453 tx = dmu_tx_create(os);
454 dmu_tx_hold_write(tx, ZVOL_OBJ, off, len);
455 error = dmu_tx_assign(tx, TXG_WAIT);
459 dmu_write(os, ZVOL_OBJ, off, len, data, tx);
463 return (SET_ERROR(error));
467 zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
469 return (SET_ERROR(ENOTSUP));
473 * Callback vectors for replaying records.
474 * Only TX_WRITE is needed for zvol.
476 zil_replay_func_t zvol_replay_vector[TX_MAX_TYPE] = {
477 (zil_replay_func_t)zvol_replay_err, /* no such transaction type */
478 (zil_replay_func_t)zvol_replay_err, /* TX_CREATE */
479 (zil_replay_func_t)zvol_replay_err, /* TX_MKDIR */
480 (zil_replay_func_t)zvol_replay_err, /* TX_MKXATTR */
481 (zil_replay_func_t)zvol_replay_err, /* TX_SYMLINK */
482 (zil_replay_func_t)zvol_replay_err, /* TX_REMOVE */
483 (zil_replay_func_t)zvol_replay_err, /* TX_RMDIR */
484 (zil_replay_func_t)zvol_replay_err, /* TX_LINK */
485 (zil_replay_func_t)zvol_replay_err, /* TX_RENAME */
486 (zil_replay_func_t)zvol_replay_write, /* TX_WRITE */
487 (zil_replay_func_t)zvol_replay_err, /* TX_TRUNCATE */
488 (zil_replay_func_t)zvol_replay_err, /* TX_SETATTR */
489 (zil_replay_func_t)zvol_replay_err, /* TX_ACL */
493 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
495 * We store data in the log buffers if it's small enough.
496 * Otherwise we will later flush the data out via dmu_sync().
498 ssize_t zvol_immediate_write_sz = 32768;
501 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, uint64_t offset,
502 uint64_t size, int sync)
504 uint32_t blocksize = zv->zv_volblocksize;
505 zilog_t *zilog = zv->zv_zilog;
507 ssize_t immediate_write_sz;
509 if (zil_replaying(zilog, tx))
512 immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
513 ? 0 : zvol_immediate_write_sz;
514 slogging = spa_has_slogs(zilog->zl_spa) &&
515 (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
521 itx_wr_state_t write_state;
524 * Unlike zfs_log_write() we can be called with
525 * up to DMU_MAX_ACCESS/2 (5MB) writes.
527 if (blocksize > immediate_write_sz && !slogging &&
528 size >= blocksize && offset % blocksize == 0) {
529 write_state = WR_INDIRECT; /* uses dmu_sync */
532 write_state = WR_COPIED;
533 len = MIN(ZIL_MAX_LOG_DATA, size);
535 write_state = WR_NEED_COPY;
536 len = MIN(ZIL_MAX_LOG_DATA, size);
539 itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
540 (write_state == WR_COPIED ? len : 0));
541 lr = (lr_write_t *)&itx->itx_lr;
542 if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
543 ZVOL_OBJ, offset, len, lr+1, DMU_READ_NO_PREFETCH) != 0) {
544 zil_itx_destroy(itx);
545 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
546 lr = (lr_write_t *)&itx->itx_lr;
547 write_state = WR_NEED_COPY;
550 itx->itx_wr_state = write_state;
551 if (write_state == WR_NEED_COPY)
553 lr->lr_foid = ZVOL_OBJ;
554 lr->lr_offset = offset;
557 BP_ZERO(&lr->lr_blkptr);
559 itx->itx_private = zv;
560 itx->itx_sync = sync;
562 (void) zil_itx_assign(zilog, itx, tx);
570 * Common write path running under the zvol taskq context. This function
571 * is responsible for copying the request structure data in to the DMU and
572 * signaling the request queue with the result of the copy.
575 zvol_write(void *arg)
577 struct request *req = (struct request *)arg;
578 struct request_queue *q = req->q;
579 zvol_state_t *zv = q->queuedata;
580 fstrans_cookie_t cookie = spl_fstrans_mark();
581 uint64_t offset = blk_rq_pos(req) << 9;
582 uint64_t size = blk_rq_bytes(req);
587 if (req->cmd_flags & VDEV_REQ_FLUSH)
588 zil_commit(zv->zv_zilog, ZVOL_OBJ);
591 * Some requests are just for flush and nothing else.
598 rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_WRITER);
600 tx = dmu_tx_create(zv->zv_objset);
601 dmu_tx_hold_write(tx, ZVOL_OBJ, offset, size);
603 /* This will only fail for ENOSPC */
604 error = dmu_tx_assign(tx, TXG_WAIT);
607 zfs_range_unlock(rl);
611 error = dmu_write_req(zv->zv_objset, ZVOL_OBJ, req, tx);
613 zvol_log_write(zv, tx, offset, size,
614 req->cmd_flags & VDEV_REQ_FUA);
617 zfs_range_unlock(rl);
619 if ((req->cmd_flags & VDEV_REQ_FUA) ||
620 zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)
621 zil_commit(zv->zv_zilog, ZVOL_OBJ);
624 blk_end_request(req, -error, size);
625 spl_fstrans_unmark(cookie);
628 #ifdef HAVE_BLK_QUEUE_DISCARD
630 zvol_discard(void *arg)
632 struct request *req = (struct request *)arg;
633 struct request_queue *q = req->q;
634 zvol_state_t *zv = q->queuedata;
635 fstrans_cookie_t cookie = spl_fstrans_mark();
636 uint64_t start = blk_rq_pos(req) << 9;
637 uint64_t end = start + blk_rq_bytes(req);
641 if (end > zv->zv_volsize) {
647 * Align the request to volume block boundaries. If we don't,
648 * then this will force dnode_free_range() to zero out the
649 * unaligned parts, which is slow (read-modify-write) and
650 * useless since we are not freeing any space by doing so.
652 start = P2ROUNDUP(start, zv->zv_volblocksize);
653 end = P2ALIGN(end, zv->zv_volblocksize);
660 rl = zfs_range_lock(&zv->zv_znode, start, end - start, RL_WRITER);
662 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, start, end-start);
665 * TODO: maybe we should add the operation to the log.
668 zfs_range_unlock(rl);
670 blk_end_request(req, -error, blk_rq_bytes(req));
671 spl_fstrans_unmark(cookie);
673 #endif /* HAVE_BLK_QUEUE_DISCARD */
676 * Common read path running under the zvol taskq context. This function
677 * is responsible for copying the requested data out of the DMU and in to
678 * a linux request structure. It then must signal the request queue with
679 * an error code describing the result of the copy.
684 struct request *req = (struct request *)arg;
685 struct request_queue *q = req->q;
686 zvol_state_t *zv = q->queuedata;
687 fstrans_cookie_t cookie = spl_fstrans_mark();
688 uint64_t offset = blk_rq_pos(req) << 9;
689 uint64_t size = blk_rq_bytes(req);
698 rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
700 error = dmu_read_req(zv->zv_objset, ZVOL_OBJ, req);
702 zfs_range_unlock(rl);
704 /* convert checksum errors into IO errors */
706 error = SET_ERROR(EIO);
709 blk_end_request(req, -error, size);
710 spl_fstrans_unmark(cookie);
714 * Request will be added back to the request queue and retried if
715 * it cannot be immediately dispatched to the taskq for handling
718 zvol_dispatch(task_func_t func, struct request *req)
720 if (!taskq_dispatch(zvol_taskq, func, (void *)req, TQ_NOSLEEP))
721 blk_requeue_request(req->q, req);
725 * Common request path. Rather than registering a custom make_request()
726 * function we use the generic Linux version. This is done because it allows
727 * us to easily merge read requests which would otherwise we performed
728 * synchronously by the DMU. This is less critical in write case where the
729 * DMU will perform the correct merging within a transaction group. Using
730 * the generic make_request() also let's use leverage the fact that the
731 * elevator with ensure correct ordering in regards to barrior IOs. On
732 * the downside it means that in the write case we end up doing request
733 * merging twice once in the elevator and once in the DMU.
735 * The request handler is called under a spin lock so all the real work
736 * is handed off to be done in the context of the zvol taskq. This function
737 * simply performs basic request sanity checking and hands off the request.
740 zvol_request(struct request_queue *q)
742 zvol_state_t *zv = q->queuedata;
746 while ((req = blk_fetch_request(q)) != NULL) {
747 size = blk_rq_bytes(req);
749 if (size != 0 && blk_rq_pos(req) + blk_rq_sectors(req) >
750 get_capacity(zv->zv_disk)) {
752 "%s: bad access: block=%llu, count=%lu\n",
753 req->rq_disk->disk_name,
754 (long long unsigned)blk_rq_pos(req),
755 (long unsigned)blk_rq_sectors(req));
756 __blk_end_request(req, -EIO, size);
760 if (!blk_fs_request(req)) {
761 printk(KERN_INFO "%s: non-fs cmd\n",
762 req->rq_disk->disk_name);
763 __blk_end_request(req, -EIO, size);
767 switch ((int)rq_data_dir(req)) {
769 zvol_dispatch(zvol_read, req);
772 if (unlikely(zv->zv_flags & ZVOL_RDONLY)) {
773 __blk_end_request(req, -EROFS, size);
777 #ifdef HAVE_BLK_QUEUE_DISCARD
778 if (req->cmd_flags & VDEV_REQ_DISCARD) {
779 zvol_dispatch(zvol_discard, req);
782 #endif /* HAVE_BLK_QUEUE_DISCARD */
784 zvol_dispatch(zvol_write, req);
787 printk(KERN_INFO "%s: unknown cmd: %d\n",
788 req->rq_disk->disk_name, (int)rq_data_dir(req));
789 __blk_end_request(req, -EIO, size);
796 zvol_get_done(zgd_t *zgd, int error)
799 dmu_buf_rele(zgd->zgd_db, zgd);
801 zfs_range_unlock(zgd->zgd_rl);
803 if (error == 0 && zgd->zgd_bp)
804 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
806 kmem_free(zgd, sizeof (zgd_t));
810 * Get data to generate a TX_WRITE intent log record.
813 zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
815 zvol_state_t *zv = arg;
816 objset_t *os = zv->zv_objset;
817 uint64_t object = ZVOL_OBJ;
818 uint64_t offset = lr->lr_offset;
819 uint64_t size = lr->lr_length;
820 blkptr_t *bp = &lr->lr_blkptr;
828 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
829 zgd->zgd_zilog = zv->zv_zilog;
830 zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
833 * Write records come in two flavors: immediate and indirect.
834 * For small writes it's cheaper to store the data with the
835 * log record (immediate); for large writes it's cheaper to
836 * sync the data and get a pointer to it (indirect) so that
837 * we don't have to write the data twice.
839 if (buf != NULL) { /* immediate write */
840 error = dmu_read(os, object, offset, size, buf,
841 DMU_READ_NO_PREFETCH);
843 size = zv->zv_volblocksize;
844 offset = P2ALIGN_TYPED(offset, size, uint64_t);
845 error = dmu_buf_hold(os, object, offset, zgd, &db,
846 DMU_READ_NO_PREFETCH);
848 blkptr_t *obp = dmu_buf_get_blkptr(db);
850 ASSERT(BP_IS_HOLE(bp));
855 zgd->zgd_bp = &lr->lr_blkptr;
858 ASSERT(db->db_offset == offset);
859 ASSERT(db->db_size == size);
861 error = dmu_sync(zio, lr->lr_common.lrc_txg,
869 zvol_get_done(zgd, error);
871 return (SET_ERROR(error));
875 * The zvol_state_t's are inserted in increasing MINOR(dev_t) order.
878 zvol_insert(zvol_state_t *zv_insert)
880 zvol_state_t *zv = NULL;
882 ASSERT(MUTEX_HELD(&zvol_state_lock));
883 ASSERT3U(MINOR(zv_insert->zv_dev) & ZVOL_MINOR_MASK, ==, 0);
884 for (zv = list_head(&zvol_state_list); zv != NULL;
885 zv = list_next(&zvol_state_list, zv)) {
886 if (MINOR(zv->zv_dev) > MINOR(zv_insert->zv_dev))
890 list_insert_before(&zvol_state_list, zv, zv_insert);
894 * Simply remove the zvol from to list of zvols.
897 zvol_remove(zvol_state_t *zv_remove)
899 ASSERT(MUTEX_HELD(&zvol_state_lock));
900 list_remove(&zvol_state_list, zv_remove);
904 zvol_first_open(zvol_state_t *zv)
913 * In all other cases the spa_namespace_lock is taken before the
914 * bdev->bd_mutex lock. But in this case the Linux __blkdev_get()
915 * function calls fops->open() with the bdev->bd_mutex lock held.
917 * To avoid a potential lock inversion deadlock we preemptively
918 * try to take the spa_namespace_lock(). Normally it will not
919 * be contended and this is safe because spa_open_common() handles
920 * the case where the caller already holds the spa_namespace_lock.
922 * When it is contended we risk a lock inversion if we were to
923 * block waiting for the lock. Luckily, the __blkdev_get()
924 * function allows us to return -ERESTARTSYS which will result in
925 * bdev->bd_mutex being dropped, reacquired, and fops->open() being
926 * called again. This process can be repeated safely until both
927 * locks are acquired.
929 if (!mutex_owned(&spa_namespace_lock)) {
930 locked = mutex_tryenter(&spa_namespace_lock);
932 return (-SET_ERROR(ERESTARTSYS));
935 error = dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL);
939 /* lie and say we're read-only */
940 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, 1, zvol_tag, &os);
944 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
946 dmu_objset_disown(os, zvol_tag);
951 error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
953 dmu_objset_disown(os, zvol_tag);
957 set_capacity(zv->zv_disk, volsize >> 9);
958 zv->zv_volsize = volsize;
959 zv->zv_zilog = zil_open(os, zvol_get_data);
961 if (ro || dmu_objset_is_snapshot(os) ||
962 !spa_writeable(dmu_objset_spa(os))) {
963 set_disk_ro(zv->zv_disk, 1);
964 zv->zv_flags |= ZVOL_RDONLY;
966 set_disk_ro(zv->zv_disk, 0);
967 zv->zv_flags &= ~ZVOL_RDONLY;
972 mutex_exit(&spa_namespace_lock);
974 return (SET_ERROR(-error));
978 zvol_last_close(zvol_state_t *zv)
980 zil_close(zv->zv_zilog);
983 dmu_buf_rele(zv->zv_dbuf, zvol_tag);
989 if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) &&
990 !(zv->zv_flags & ZVOL_RDONLY))
991 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
992 (void) dmu_objset_evict_dbufs(zv->zv_objset);
994 dmu_objset_disown(zv->zv_objset, zvol_tag);
995 zv->zv_objset = NULL;
999 zvol_open(struct block_device *bdev, fmode_t flag)
1001 zvol_state_t *zv = bdev->bd_disk->private_data;
1002 int error = 0, drop_mutex = 0;
1005 * If the caller is already holding the mutex do not take it
1006 * again, this will happen as part of zvol_create_minor().
1007 * Once add_disk() is called the device is live and the kernel
1008 * will attempt to open it to read the partition information.
1010 if (!mutex_owned(&zvol_state_lock)) {
1011 mutex_enter(&zvol_state_lock);
1015 ASSERT3P(zv, !=, NULL);
1017 if (zv->zv_open_count == 0) {
1018 error = zvol_first_open(zv);
1023 if ((flag & FMODE_WRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
1025 goto out_open_count;
1028 zv->zv_open_count++;
1031 if (zv->zv_open_count == 0)
1032 zvol_last_close(zv);
1036 mutex_exit(&zvol_state_lock);
1038 check_disk_change(bdev);
1040 return (SET_ERROR(error));
1043 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1048 zvol_release(struct gendisk *disk, fmode_t mode)
1050 zvol_state_t *zv = disk->private_data;
1053 if (!mutex_owned(&zvol_state_lock)) {
1054 mutex_enter(&zvol_state_lock);
1058 if (zv->zv_open_count > 0) {
1059 zv->zv_open_count--;
1060 if (zv->zv_open_count == 0)
1061 zvol_last_close(zv);
1065 mutex_exit(&zvol_state_lock);
1067 #ifndef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1073 zvol_ioctl(struct block_device *bdev, fmode_t mode,
1074 unsigned int cmd, unsigned long arg)
1076 zvol_state_t *zv = bdev->bd_disk->private_data;
1080 return (SET_ERROR(-ENXIO));
1084 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1087 error = copy_to_user((void *)arg, zv->zv_name, MAXNAMELEN);
1096 return (SET_ERROR(error));
1099 #ifdef CONFIG_COMPAT
1101 zvol_compat_ioctl(struct block_device *bdev, fmode_t mode,
1102 unsigned cmd, unsigned long arg)
1104 return (zvol_ioctl(bdev, mode, cmd, arg));
1107 #define zvol_compat_ioctl NULL
1110 static int zvol_media_changed(struct gendisk *disk)
1112 zvol_state_t *zv = disk->private_data;
1114 return (zv->zv_changed);
1117 static int zvol_revalidate_disk(struct gendisk *disk)
1119 zvol_state_t *zv = disk->private_data;
1122 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1128 * Provide a simple virtual geometry for legacy compatibility. For devices
1129 * smaller than 1 MiB a small head and sector count is used to allow very
1130 * tiny devices. For devices over 1 Mib a standard head and sector count
1131 * is used to keep the cylinders count reasonable.
1134 zvol_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1136 zvol_state_t *zv = bdev->bd_disk->private_data;
1137 sector_t sectors = get_capacity(zv->zv_disk);
1139 if (sectors > 2048) {
1148 geo->cylinders = sectors / (geo->heads * geo->sectors);
1153 static struct kobject *
1154 zvol_probe(dev_t dev, int *part, void *arg)
1157 struct kobject *kobj;
1159 mutex_enter(&zvol_state_lock);
1160 zv = zvol_find_by_dev(dev);
1161 kobj = zv ? get_disk(zv->zv_disk) : NULL;
1162 mutex_exit(&zvol_state_lock);
1167 #ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS
1168 static struct block_device_operations zvol_ops = {
1170 .release = zvol_release,
1171 .ioctl = zvol_ioctl,
1172 .compat_ioctl = zvol_compat_ioctl,
1173 .media_changed = zvol_media_changed,
1174 .revalidate_disk = zvol_revalidate_disk,
1175 .getgeo = zvol_getgeo,
1176 .owner = THIS_MODULE,
1179 #else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1182 zvol_open_by_inode(struct inode *inode, struct file *file)
1184 return (zvol_open(inode->i_bdev, file->f_mode));
1188 zvol_release_by_inode(struct inode *inode, struct file *file)
1190 return (zvol_release(inode->i_bdev->bd_disk, file->f_mode));
1194 zvol_ioctl_by_inode(struct inode *inode, struct file *file,
1195 unsigned int cmd, unsigned long arg)
1197 if (file == NULL || inode == NULL)
1198 return (SET_ERROR(-EINVAL));
1200 return (zvol_ioctl(inode->i_bdev, file->f_mode, cmd, arg));
1203 #ifdef CONFIG_COMPAT
1205 zvol_compat_ioctl_by_inode(struct file *file,
1206 unsigned int cmd, unsigned long arg)
1209 return (SET_ERROR(-EINVAL));
1211 return (zvol_compat_ioctl(file->f_dentry->d_inode->i_bdev,
1212 file->f_mode, cmd, arg));
1215 #define zvol_compat_ioctl_by_inode NULL
1218 static struct block_device_operations zvol_ops = {
1219 .open = zvol_open_by_inode,
1220 .release = zvol_release_by_inode,
1221 .ioctl = zvol_ioctl_by_inode,
1222 .compat_ioctl = zvol_compat_ioctl_by_inode,
1223 .media_changed = zvol_media_changed,
1224 .revalidate_disk = zvol_revalidate_disk,
1225 .getgeo = zvol_getgeo,
1226 .owner = THIS_MODULE,
1228 #endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1231 * Allocate memory for a new zvol_state_t and setup the required
1232 * request queue and generic disk structures for the block device.
1234 static zvol_state_t *
1235 zvol_alloc(dev_t dev, const char *name)
1240 zv = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
1242 spin_lock_init(&zv->zv_lock);
1243 list_link_init(&zv->zv_next);
1245 zv->zv_queue = blk_init_queue(zvol_request, &zv->zv_lock);
1246 if (zv->zv_queue == NULL)
1249 #ifdef HAVE_ELEVATOR_CHANGE
1250 error = elevator_change(zv->zv_queue, "noop");
1251 #endif /* HAVE_ELEVATOR_CHANGE */
1253 printk("ZFS: Unable to set \"%s\" scheduler for zvol %s: %d\n",
1254 "noop", name, error);
1258 #ifdef HAVE_BLK_QUEUE_FLUSH
1259 blk_queue_flush(zv->zv_queue, VDEV_REQ_FLUSH | VDEV_REQ_FUA);
1261 blk_queue_ordered(zv->zv_queue, QUEUE_ORDERED_DRAIN, NULL);
1262 #endif /* HAVE_BLK_QUEUE_FLUSH */
1264 zv->zv_disk = alloc_disk(ZVOL_MINORS);
1265 if (zv->zv_disk == NULL)
1268 zv->zv_queue->queuedata = zv;
1270 zv->zv_open_count = 0;
1271 strlcpy(zv->zv_name, name, MAXNAMELEN);
1273 mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
1274 avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
1275 sizeof (rl_t), offsetof(rl_t, r_node));
1276 zv->zv_znode.z_is_zvol = TRUE;
1278 zv->zv_disk->major = zvol_major;
1279 zv->zv_disk->first_minor = (dev & MINORMASK);
1280 zv->zv_disk->fops = &zvol_ops;
1281 zv->zv_disk->private_data = zv;
1282 zv->zv_disk->queue = zv->zv_queue;
1283 snprintf(zv->zv_disk->disk_name, DISK_NAME_LEN, "%s%d",
1284 ZVOL_DEV_NAME, (dev & MINORMASK));
1289 blk_cleanup_queue(zv->zv_queue);
1291 kmem_free(zv, sizeof (zvol_state_t));
1297 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1300 zvol_free(zvol_state_t *zv)
1302 avl_destroy(&zv->zv_znode.z_range_avl);
1303 mutex_destroy(&zv->zv_znode.z_range_lock);
1305 del_gendisk(zv->zv_disk);
1306 blk_cleanup_queue(zv->zv_queue);
1307 put_disk(zv->zv_disk);
1309 kmem_free(zv, sizeof (zvol_state_t));
1313 __zvol_snapdev_hidden(const char *name)
1320 parent = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1321 (void) strlcpy(parent, name, MAXPATHLEN);
1323 if ((atp = strrchr(parent, '@')) != NULL) {
1325 error = dsl_prop_get_integer(parent, "snapdev", &snapdev, NULL);
1326 if ((error == 0) && (snapdev == ZFS_SNAPDEV_HIDDEN))
1327 error = SET_ERROR(ENODEV);
1330 kmem_free(parent, MAXPATHLEN);
1332 return (SET_ERROR(error));
1336 __zvol_create_minor(const char *name, boolean_t ignore_snapdev)
1340 dmu_object_info_t *doi;
1345 ASSERT(MUTEX_HELD(&zvol_state_lock));
1347 zv = zvol_find_by_name(name);
1349 error = SET_ERROR(EEXIST);
1353 if (ignore_snapdev == B_FALSE) {
1354 error = __zvol_snapdev_hidden(name);
1359 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
1361 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
1365 error = dmu_object_info(os, ZVOL_OBJ, doi);
1367 goto out_dmu_objset_disown;
1369 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
1371 goto out_dmu_objset_disown;
1373 error = zvol_find_minor(&minor);
1375 goto out_dmu_objset_disown;
1377 zv = zvol_alloc(MKDEV(zvol_major, minor), name);
1379 error = SET_ERROR(EAGAIN);
1380 goto out_dmu_objset_disown;
1383 if (dmu_objset_is_snapshot(os))
1384 zv->zv_flags |= ZVOL_RDONLY;
1386 zv->zv_volblocksize = doi->doi_data_block_size;
1387 zv->zv_volsize = volsize;
1390 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1392 blk_queue_max_hw_sectors(zv->zv_queue, (DMU_MAX_ACCESS / 4) >> 9);
1393 blk_queue_max_segments(zv->zv_queue, UINT16_MAX);
1394 blk_queue_max_segment_size(zv->zv_queue, UINT_MAX);
1395 blk_queue_physical_block_size(zv->zv_queue, zv->zv_volblocksize);
1396 blk_queue_io_opt(zv->zv_queue, zv->zv_volblocksize);
1397 #ifdef HAVE_BLK_QUEUE_DISCARD
1398 blk_queue_max_discard_sectors(zv->zv_queue,
1399 (zvol_max_discard_blocks * zv->zv_volblocksize) >> 9);
1400 blk_queue_discard_granularity(zv->zv_queue, zv->zv_volblocksize);
1401 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zv->zv_queue);
1403 #ifdef HAVE_BLK_QUEUE_NONROT
1404 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zv->zv_queue);
1407 if (spa_writeable(dmu_objset_spa(os))) {
1408 if (zil_replay_disable)
1409 zil_destroy(dmu_objset_zil(os), B_FALSE);
1411 zil_replay(os, zv, zvol_replay_vector);
1414 zv->zv_objset = NULL;
1415 out_dmu_objset_disown:
1416 dmu_objset_disown(os, zvol_tag);
1418 kmem_free(doi, sizeof (dmu_object_info_t));
1423 add_disk(zv->zv_disk);
1426 return (SET_ERROR(error));
1430 * Create a block device minor node and setup the linkage between it
1431 * and the specified volume. Once this function returns the block
1432 * device is live and ready for use.
1435 zvol_create_minor(const char *name)
1439 mutex_enter(&zvol_state_lock);
1440 error = __zvol_create_minor(name, B_FALSE);
1441 mutex_exit(&zvol_state_lock);
1443 return (SET_ERROR(error));
1447 __zvol_remove_minor(const char *name)
1451 ASSERT(MUTEX_HELD(&zvol_state_lock));
1453 zv = zvol_find_by_name(name);
1455 return (SET_ERROR(ENXIO));
1457 if (zv->zv_open_count > 0)
1458 return (SET_ERROR(EBUSY));
1467 * Remove a block device minor node for the specified volume.
1470 zvol_remove_minor(const char *name)
1474 mutex_enter(&zvol_state_lock);
1475 error = __zvol_remove_minor(name);
1476 mutex_exit(&zvol_state_lock);
1478 return (SET_ERROR(error));
1482 * Rename a block device minor mode for the specified volume.
1485 __zvol_rename_minor(zvol_state_t *zv, const char *newname)
1487 int readonly = get_disk_ro(zv->zv_disk);
1489 ASSERT(MUTEX_HELD(&zvol_state_lock));
1491 strlcpy(zv->zv_name, newname, sizeof (zv->zv_name));
1494 * The block device's read-only state is briefly changed causing
1495 * a KOBJ_CHANGE uevent to be issued. This ensures udev detects
1496 * the name change and fixes the symlinks. This does not change
1497 * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
1498 * changes. This would normally be done using kobject_uevent() but
1499 * that is a GPL-only symbol which is why we need this workaround.
1501 set_disk_ro(zv->zv_disk, !readonly);
1502 set_disk_ro(zv->zv_disk, readonly);
1506 zvol_create_minors_cb(const char *dsname, void *arg)
1508 (void) zvol_create_minor(dsname);
1514 * Create minors for specified dataset including children and snapshots.
1517 zvol_create_minors(const char *name)
1521 if (!zvol_inhibit_dev)
1522 error = dmu_objset_find((char *)name, zvol_create_minors_cb,
1523 NULL, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1525 return (SET_ERROR(error));
1529 * Remove minors for specified dataset including children and snapshots.
1532 zvol_remove_minors(const char *name)
1534 zvol_state_t *zv, *zv_next;
1535 int namelen = ((name) ? strlen(name) : 0);
1537 if (zvol_inhibit_dev)
1540 mutex_enter(&zvol_state_lock);
1542 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1543 zv_next = list_next(&zvol_state_list, zv);
1545 if (name == NULL || strcmp(zv->zv_name, name) == 0 ||
1546 (strncmp(zv->zv_name, name, namelen) == 0 &&
1547 zv->zv_name[namelen] == '/')) {
1553 mutex_exit(&zvol_state_lock);
1557 * Rename minors for specified dataset including children and snapshots.
1560 zvol_rename_minors(const char *oldname, const char *newname)
1562 zvol_state_t *zv, *zv_next;
1563 int oldnamelen, newnamelen;
1566 if (zvol_inhibit_dev)
1569 oldnamelen = strlen(oldname);
1570 newnamelen = strlen(newname);
1571 name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1573 mutex_enter(&zvol_state_lock);
1575 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1576 zv_next = list_next(&zvol_state_list, zv);
1578 if (strcmp(zv->zv_name, oldname) == 0) {
1579 __zvol_rename_minor(zv, newname);
1580 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 &&
1581 (zv->zv_name[oldnamelen] == '/' ||
1582 zv->zv_name[oldnamelen] == '@')) {
1583 snprintf(name, MAXNAMELEN, "%s%c%s", newname,
1584 zv->zv_name[oldnamelen],
1585 zv->zv_name + oldnamelen + 1);
1586 __zvol_rename_minor(zv, name);
1590 mutex_exit(&zvol_state_lock);
1592 kmem_free(name, MAXNAMELEN);
1596 snapdev_snapshot_changed_cb(const char *dsname, void *arg) {
1597 uint64_t snapdev = *(uint64_t *) arg;
1599 if (strchr(dsname, '@') == NULL)
1603 case ZFS_SNAPDEV_VISIBLE:
1604 mutex_enter(&zvol_state_lock);
1605 (void) __zvol_create_minor(dsname, B_TRUE);
1606 mutex_exit(&zvol_state_lock);
1608 case ZFS_SNAPDEV_HIDDEN:
1609 (void) zvol_remove_minor(dsname);
1617 zvol_set_snapdev(const char *dsname, uint64_t snapdev) {
1618 (void) dmu_objset_find((char *) dsname, snapdev_snapshot_changed_cb,
1619 &snapdev, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
1620 /* caller should continue to modify snapdev property */
1627 int threads = MIN(MAX(zvol_threads, 1), 1024);
1630 list_create(&zvol_state_list, sizeof (zvol_state_t),
1631 offsetof(zvol_state_t, zv_next));
1633 mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1635 zvol_taskq = taskq_create(ZVOL_DRIVER, threads, maxclsyspri,
1636 threads * 2, INT_MAX, TASKQ_PREPOPULATE | TASKQ_DYNAMIC);
1637 if (zvol_taskq == NULL) {
1638 printk(KERN_INFO "ZFS: taskq_create() failed\n");
1643 error = register_blkdev(zvol_major, ZVOL_DRIVER);
1645 printk(KERN_INFO "ZFS: register_blkdev() failed %d\n", error);
1649 blk_register_region(MKDEV(zvol_major, 0), 1UL << MINORBITS,
1650 THIS_MODULE, zvol_probe, NULL, NULL);
1655 taskq_destroy(zvol_taskq);
1657 mutex_destroy(&zvol_state_lock);
1658 list_destroy(&zvol_state_list);
1660 return (SET_ERROR(error));
1666 zvol_remove_minors(NULL);
1667 blk_unregister_region(MKDEV(zvol_major, 0), 1UL << MINORBITS);
1668 unregister_blkdev(zvol_major, ZVOL_DRIVER);
1669 taskq_destroy(zvol_taskq);
1670 mutex_destroy(&zvol_state_lock);
1671 list_destroy(&zvol_state_list);
1674 module_param(zvol_inhibit_dev, uint, 0644);
1675 MODULE_PARM_DESC(zvol_inhibit_dev, "Do not create zvol device nodes");
1677 module_param(zvol_major, uint, 0444);
1678 MODULE_PARM_DESC(zvol_major, "Major number for zvol device");
1680 module_param(zvol_threads, uint, 0444);
1681 MODULE_PARM_DESC(zvol_threads, "Max number of threads to handle I/O requests");
1683 module_param(zvol_max_discard_blocks, ulong, 0444);
1684 MODULE_PARM_DESC(zvol_max_discard_blocks, "Max number of blocks to discard");