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 * Sanity check volume size.
238 zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
241 return (SET_ERROR(EINVAL));
243 if (volsize % blocksize != 0)
244 return (SET_ERROR(EINVAL));
247 if (volsize - 1 > MAXOFFSET_T)
248 return (SET_ERROR(EOVERFLOW));
254 * Ensure the zap is flushed then inform the VFS of the capacity change.
257 zvol_update_volsize(zvol_state_t *zv, uint64_t volsize, objset_t *os)
259 struct block_device *bdev;
263 ASSERT(MUTEX_HELD(&zvol_state_lock));
265 tx = dmu_tx_create(os);
266 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
267 error = dmu_tx_assign(tx, TXG_WAIT);
270 return (SET_ERROR(error));
273 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
278 return (SET_ERROR(error));
280 error = dmu_free_long_range(os,
281 ZVOL_OBJ, volsize, DMU_OBJECT_END);
283 return (SET_ERROR(error));
285 bdev = bdget_disk(zv->zv_disk, 0);
287 return (SET_ERROR(EIO));
290 * Added check_disk_size_change() helper function.
292 #ifdef HAVE_CHECK_DISK_SIZE_CHANGE
293 set_capacity(zv->zv_disk, volsize >> 9);
294 zv->zv_volsize = volsize;
295 check_disk_size_change(zv->zv_disk, bdev);
297 zv->zv_volsize = volsize;
299 (void) check_disk_change(bdev);
300 #endif /* HAVE_CHECK_DISK_SIZE_CHANGE */
308 * Set ZFS_PROP_VOLSIZE set entry point.
311 zvol_set_volsize(const char *name, uint64_t volsize)
314 dmu_object_info_t *doi;
319 error = dsl_prop_get_integer(name,
320 zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL);
322 return (SET_ERROR(error));
324 return (SET_ERROR(EROFS));
326 mutex_enter(&zvol_state_lock);
328 zv = zvol_find_by_name(name);
330 error = SET_ERROR(ENXIO);
334 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
336 error = dmu_objset_hold(name, FTAG, &os);
340 if ((error = dmu_object_info(os, ZVOL_OBJ, doi)) ||
341 (error = zvol_check_volsize(volsize, doi->doi_data_block_size)))
344 VERIFY(dsl_prop_get_integer(name, "readonly", &readonly, NULL) == 0);
346 error = SET_ERROR(EROFS);
350 if (zv->zv_flags & ZVOL_RDONLY) {
351 error = SET_ERROR(EROFS);
355 error = zvol_update_volsize(zv, volsize, os);
357 kmem_free(doi, sizeof (dmu_object_info_t));
360 dmu_objset_rele(os, FTAG);
362 mutex_exit(&zvol_state_lock);
364 return (SET_ERROR(error));
368 * Sanity check volume block size.
371 zvol_check_volblocksize(uint64_t volblocksize)
373 if (volblocksize < SPA_MINBLOCKSIZE ||
374 volblocksize > SPA_MAXBLOCKSIZE ||
376 return (SET_ERROR(EDOM));
382 * Set ZFS_PROP_VOLBLOCKSIZE set entry point.
385 zvol_set_volblocksize(const char *name, uint64_t volblocksize)
391 mutex_enter(&zvol_state_lock);
393 zv = zvol_find_by_name(name);
395 error = SET_ERROR(ENXIO);
399 if (zv->zv_flags & ZVOL_RDONLY) {
400 error = SET_ERROR(EROFS);
404 tx = dmu_tx_create(zv->zv_objset);
405 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
406 error = dmu_tx_assign(tx, TXG_WAIT);
410 error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
411 volblocksize, 0, tx);
412 if (error == ENOTSUP)
413 error = SET_ERROR(EBUSY);
416 zv->zv_volblocksize = volblocksize;
419 mutex_exit(&zvol_state_lock);
421 return (SET_ERROR(error));
425 * Replay a TX_WRITE ZIL transaction that didn't get committed
426 * after a system failure
429 zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
431 objset_t *os = zv->zv_objset;
432 char *data = (char *)(lr + 1); /* data follows lr_write_t */
433 uint64_t off = lr->lr_offset;
434 uint64_t len = lr->lr_length;
439 byteswap_uint64_array(lr, sizeof (*lr));
441 tx = dmu_tx_create(os);
442 dmu_tx_hold_write(tx, ZVOL_OBJ, off, len);
443 error = dmu_tx_assign(tx, TXG_WAIT);
447 dmu_write(os, ZVOL_OBJ, off, len, data, tx);
451 return (SET_ERROR(error));
455 zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
457 return (SET_ERROR(ENOTSUP));
461 * Callback vectors for replaying records.
462 * Only TX_WRITE is needed for zvol.
464 zil_replay_func_t zvol_replay_vector[TX_MAX_TYPE] = {
465 (zil_replay_func_t)zvol_replay_err, /* no such transaction type */
466 (zil_replay_func_t)zvol_replay_err, /* TX_CREATE */
467 (zil_replay_func_t)zvol_replay_err, /* TX_MKDIR */
468 (zil_replay_func_t)zvol_replay_err, /* TX_MKXATTR */
469 (zil_replay_func_t)zvol_replay_err, /* TX_SYMLINK */
470 (zil_replay_func_t)zvol_replay_err, /* TX_REMOVE */
471 (zil_replay_func_t)zvol_replay_err, /* TX_RMDIR */
472 (zil_replay_func_t)zvol_replay_err, /* TX_LINK */
473 (zil_replay_func_t)zvol_replay_err, /* TX_RENAME */
474 (zil_replay_func_t)zvol_replay_write, /* TX_WRITE */
475 (zil_replay_func_t)zvol_replay_err, /* TX_TRUNCATE */
476 (zil_replay_func_t)zvol_replay_err, /* TX_SETATTR */
477 (zil_replay_func_t)zvol_replay_err, /* TX_ACL */
481 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
483 * We store data in the log buffers if it's small enough.
484 * Otherwise we will later flush the data out via dmu_sync().
486 ssize_t zvol_immediate_write_sz = 32768;
489 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, uint64_t offset,
490 uint64_t size, int sync)
492 uint32_t blocksize = zv->zv_volblocksize;
493 zilog_t *zilog = zv->zv_zilog;
495 ssize_t immediate_write_sz;
497 if (zil_replaying(zilog, tx))
500 immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
501 ? 0 : zvol_immediate_write_sz;
502 slogging = spa_has_slogs(zilog->zl_spa) &&
503 (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
509 itx_wr_state_t write_state;
512 * Unlike zfs_log_write() we can be called with
513 * up to DMU_MAX_ACCESS/2 (5MB) writes.
515 if (blocksize > immediate_write_sz && !slogging &&
516 size >= blocksize && offset % blocksize == 0) {
517 write_state = WR_INDIRECT; /* uses dmu_sync */
520 write_state = WR_COPIED;
521 len = MIN(ZIL_MAX_LOG_DATA, size);
523 write_state = WR_NEED_COPY;
524 len = MIN(ZIL_MAX_LOG_DATA, size);
527 itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
528 (write_state == WR_COPIED ? len : 0));
529 lr = (lr_write_t *)&itx->itx_lr;
530 if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
531 ZVOL_OBJ, offset, len, lr+1, DMU_READ_NO_PREFETCH) != 0) {
532 zil_itx_destroy(itx);
533 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
534 lr = (lr_write_t *)&itx->itx_lr;
535 write_state = WR_NEED_COPY;
538 itx->itx_wr_state = write_state;
539 if (write_state == WR_NEED_COPY)
541 lr->lr_foid = ZVOL_OBJ;
542 lr->lr_offset = offset;
545 BP_ZERO(&lr->lr_blkptr);
547 itx->itx_private = zv;
548 itx->itx_sync = sync;
550 (void) zil_itx_assign(zilog, itx, tx);
558 * Common write path running under the zvol taskq context. This function
559 * is responsible for copying the request structure data in to the DMU and
560 * signaling the request queue with the result of the copy.
563 zvol_write(void *arg)
565 struct request *req = (struct request *)arg;
566 struct request_queue *q = req->q;
567 zvol_state_t *zv = q->queuedata;
568 uint64_t offset = blk_rq_pos(req) << 9;
569 uint64_t size = blk_rq_bytes(req);
575 * Annotate this call path with a flag that indicates that it is
576 * unsafe to use KM_SLEEP during memory allocations due to the
577 * potential for a deadlock. KM_PUSHPAGE should be used instead.
579 ASSERT(!(current->flags & PF_NOFS));
580 current->flags |= PF_NOFS;
582 if (req->cmd_flags & VDEV_REQ_FLUSH)
583 zil_commit(zv->zv_zilog, ZVOL_OBJ);
586 * Some requests are just for flush and nothing else.
589 blk_end_request(req, 0, size);
593 rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_WRITER);
595 tx = dmu_tx_create(zv->zv_objset);
596 dmu_tx_hold_write(tx, ZVOL_OBJ, offset, size);
598 /* This will only fail for ENOSPC */
599 error = dmu_tx_assign(tx, TXG_WAIT);
602 zfs_range_unlock(rl);
603 blk_end_request(req, -error, size);
607 error = dmu_write_req(zv->zv_objset, ZVOL_OBJ, req, tx);
609 zvol_log_write(zv, tx, offset, size,
610 req->cmd_flags & VDEV_REQ_FUA);
613 zfs_range_unlock(rl);
615 if ((req->cmd_flags & VDEV_REQ_FUA) ||
616 zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)
617 zil_commit(zv->zv_zilog, ZVOL_OBJ);
619 blk_end_request(req, -error, size);
621 current->flags &= ~PF_NOFS;
624 #ifdef HAVE_BLK_QUEUE_DISCARD
626 zvol_discard(void *arg)
628 struct request *req = (struct request *)arg;
629 struct request_queue *q = req->q;
630 zvol_state_t *zv = q->queuedata;
631 uint64_t start = blk_rq_pos(req) << 9;
632 uint64_t end = start + blk_rq_bytes(req);
637 * Annotate this call path with a flag that indicates that it is
638 * unsafe to use KM_SLEEP during memory allocations due to the
639 * potential for a deadlock. KM_PUSHPAGE should be used instead.
641 ASSERT(!(current->flags & PF_NOFS));
642 current->flags |= PF_NOFS;
644 if (end > zv->zv_volsize) {
645 blk_end_request(req, -EIO, blk_rq_bytes(req));
650 * Align the request to volume block boundaries. If we don't,
651 * then this will force dnode_free_range() to zero out the
652 * unaligned parts, which is slow (read-modify-write) and
653 * useless since we are not freeing any space by doing so.
655 start = P2ROUNDUP(start, zv->zv_volblocksize);
656 end = P2ALIGN(end, zv->zv_volblocksize);
659 blk_end_request(req, 0, blk_rq_bytes(req));
663 rl = zfs_range_lock(&zv->zv_znode, start, end - start, RL_WRITER);
665 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, start, end-start);
668 * TODO: maybe we should add the operation to the log.
671 zfs_range_unlock(rl);
673 blk_end_request(req, -error, blk_rq_bytes(req));
675 current->flags &= ~PF_NOFS;
677 #endif /* HAVE_BLK_QUEUE_DISCARD */
680 * Common read path running under the zvol taskq context. This function
681 * is responsible for copying the requested data out of the DMU and in to
682 * a linux request structure. It then must signal the request queue with
683 * an error code describing the result of the copy.
688 struct request *req = (struct request *)arg;
689 struct request_queue *q = req->q;
690 zvol_state_t *zv = q->queuedata;
691 uint64_t offset = blk_rq_pos(req) << 9;
692 uint64_t size = blk_rq_bytes(req);
697 blk_end_request(req, 0, size);
701 rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
703 error = dmu_read_req(zv->zv_objset, ZVOL_OBJ, req);
705 zfs_range_unlock(rl);
707 /* convert checksum errors into IO errors */
709 error = SET_ERROR(EIO);
711 blk_end_request(req, -error, size);
715 * Request will be added back to the request queue and retried if
716 * it cannot be immediately dispatched to the taskq for handling
719 zvol_dispatch(task_func_t func, struct request *req)
721 if (!taskq_dispatch(zvol_taskq, func, (void *)req, TQ_NOSLEEP))
722 blk_requeue_request(req->q, req);
726 * Common request path. Rather than registering a custom make_request()
727 * function we use the generic Linux version. This is done because it allows
728 * us to easily merge read requests which would otherwise we performed
729 * synchronously by the DMU. This is less critical in write case where the
730 * DMU will perform the correct merging within a transaction group. Using
731 * the generic make_request() also let's use leverage the fact that the
732 * elevator with ensure correct ordering in regards to barrior IOs. On
733 * the downside it means that in the write case we end up doing request
734 * merging twice once in the elevator and once in the DMU.
736 * The request handler is called under a spin lock so all the real work
737 * is handed off to be done in the context of the zvol taskq. This function
738 * simply performs basic request sanity checking and hands off the request.
741 zvol_request(struct request_queue *q)
743 zvol_state_t *zv = q->queuedata;
747 while ((req = blk_fetch_request(q)) != NULL) {
748 size = blk_rq_bytes(req);
750 if (size != 0 && blk_rq_pos(req) + blk_rq_sectors(req) >
751 get_capacity(zv->zv_disk)) {
753 "%s: bad access: block=%llu, count=%lu\n",
754 req->rq_disk->disk_name,
755 (long long unsigned)blk_rq_pos(req),
756 (long unsigned)blk_rq_sectors(req));
757 __blk_end_request(req, -EIO, size);
761 if (!blk_fs_request(req)) {
762 printk(KERN_INFO "%s: non-fs cmd\n",
763 req->rq_disk->disk_name);
764 __blk_end_request(req, -EIO, size);
768 switch (rq_data_dir(req)) {
770 zvol_dispatch(zvol_read, req);
773 if (unlikely(zv->zv_flags & ZVOL_RDONLY)) {
774 __blk_end_request(req, -EROFS, size);
778 #ifdef HAVE_BLK_QUEUE_DISCARD
779 if (req->cmd_flags & VDEV_REQ_DISCARD) {
780 zvol_dispatch(zvol_discard, req);
783 #endif /* HAVE_BLK_QUEUE_DISCARD */
785 zvol_dispatch(zvol_write, req);
788 printk(KERN_INFO "%s: unknown cmd: %d\n",
789 req->rq_disk->disk_name, (int)rq_data_dir(req));
790 __blk_end_request(req, -EIO, size);
797 zvol_get_done(zgd_t *zgd, int error)
800 dmu_buf_rele(zgd->zgd_db, zgd);
802 zfs_range_unlock(zgd->zgd_rl);
804 if (error == 0 && zgd->zgd_bp)
805 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
807 kmem_free(zgd, sizeof (zgd_t));
811 * Get data to generate a TX_WRITE intent log record.
814 zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
816 zvol_state_t *zv = arg;
817 objset_t *os = zv->zv_objset;
818 uint64_t object = ZVOL_OBJ;
819 uint64_t offset = lr->lr_offset;
820 uint64_t size = lr->lr_length;
821 blkptr_t *bp = &lr->lr_blkptr;
829 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_PUSHPAGE);
830 zgd->zgd_zilog = zv->zv_zilog;
831 zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
834 * Write records come in two flavors: immediate and indirect.
835 * For small writes it's cheaper to store the data with the
836 * log record (immediate); for large writes it's cheaper to
837 * sync the data and get a pointer to it (indirect) so that
838 * we don't have to write the data twice.
840 if (buf != NULL) { /* immediate write */
841 error = dmu_read(os, object, offset, size, buf,
842 DMU_READ_NO_PREFETCH);
844 size = zv->zv_volblocksize;
845 offset = P2ALIGN_TYPED(offset, size, uint64_t);
846 error = dmu_buf_hold(os, object, offset, zgd, &db,
847 DMU_READ_NO_PREFETCH);
849 blkptr_t *obp = dmu_buf_get_blkptr(db);
851 ASSERT(BP_IS_HOLE(bp));
856 zgd->zgd_bp = &lr->lr_blkptr;
859 ASSERT(db->db_offset == offset);
860 ASSERT(db->db_size == size);
862 error = dmu_sync(zio, lr->lr_common.lrc_txg,
870 zvol_get_done(zgd, error);
872 return (SET_ERROR(error));
876 * The zvol_state_t's are inserted in increasing MINOR(dev_t) order.
879 zvol_insert(zvol_state_t *zv_insert)
881 zvol_state_t *zv = NULL;
883 ASSERT(MUTEX_HELD(&zvol_state_lock));
884 ASSERT3U(MINOR(zv_insert->zv_dev) & ZVOL_MINOR_MASK, ==, 0);
885 for (zv = list_head(&zvol_state_list); zv != NULL;
886 zv = list_next(&zvol_state_list, zv)) {
887 if (MINOR(zv->zv_dev) > MINOR(zv_insert->zv_dev))
891 list_insert_before(&zvol_state_list, zv, zv_insert);
895 * Simply remove the zvol from to list of zvols.
898 zvol_remove(zvol_state_t *zv_remove)
900 ASSERT(MUTEX_HELD(&zvol_state_lock));
901 list_remove(&zvol_state_list, zv_remove);
905 zvol_first_open(zvol_state_t *zv)
914 * In all other cases the spa_namespace_lock is taken before the
915 * bdev->bd_mutex lock. But in this case the Linux __blkdev_get()
916 * function calls fops->open() with the bdev->bd_mutex lock held.
918 * To avoid a potential lock inversion deadlock we preemptively
919 * try to take the spa_namespace_lock(). Normally it will not
920 * be contended and this is safe because spa_open_common() handles
921 * the case where the caller already holds the spa_namespace_lock.
923 * When it is contended we risk a lock inversion if we were to
924 * block waiting for the lock. Luckily, the __blkdev_get()
925 * function allows us to return -ERESTARTSYS which will result in
926 * bdev->bd_mutex being dropped, reacquired, and fops->open() being
927 * called again. This process can be repeated safely until both
928 * locks are acquired.
930 if (!mutex_owned(&spa_namespace_lock)) {
931 locked = mutex_tryenter(&spa_namespace_lock);
933 return (-SET_ERROR(ERESTARTSYS));
936 /* lie and say we're read-only */
937 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, 1, zvol_tag, &os);
941 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
943 dmu_objset_disown(os, zvol_tag);
948 error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
950 dmu_objset_disown(os, zvol_tag);
954 set_capacity(zv->zv_disk, volsize >> 9);
955 zv->zv_volsize = volsize;
956 zv->zv_zilog = zil_open(os, zvol_get_data);
958 VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL) == 0);
959 if (ro || dmu_objset_is_snapshot(os) ||
960 !spa_writeable(dmu_objset_spa(os))) {
961 set_disk_ro(zv->zv_disk, 1);
962 zv->zv_flags |= ZVOL_RDONLY;
964 set_disk_ro(zv->zv_disk, 0);
965 zv->zv_flags &= ~ZVOL_RDONLY;
970 mutex_exit(&spa_namespace_lock);
972 return (SET_ERROR(-error));
976 zvol_last_close(zvol_state_t *zv)
978 zil_close(zv->zv_zilog);
981 dmu_buf_rele(zv->zv_dbuf, zvol_tag);
987 if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) &&
988 !(zv->zv_flags & ZVOL_RDONLY))
989 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
990 (void) dmu_objset_evict_dbufs(zv->zv_objset);
992 dmu_objset_disown(zv->zv_objset, zvol_tag);
993 zv->zv_objset = NULL;
997 zvol_open(struct block_device *bdev, fmode_t flag)
999 zvol_state_t *zv = bdev->bd_disk->private_data;
1000 int error = 0, drop_mutex = 0;
1003 * If the caller is already holding the mutex do not take it
1004 * again, this will happen as part of zvol_create_minor().
1005 * Once add_disk() is called the device is live and the kernel
1006 * will attempt to open it to read the partition information.
1008 if (!mutex_owned(&zvol_state_lock)) {
1009 mutex_enter(&zvol_state_lock);
1013 ASSERT3P(zv, !=, NULL);
1015 if (zv->zv_open_count == 0) {
1016 error = zvol_first_open(zv);
1021 if ((flag & FMODE_WRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
1023 goto out_open_count;
1026 zv->zv_open_count++;
1029 if (zv->zv_open_count == 0)
1030 zvol_last_close(zv);
1034 mutex_exit(&zvol_state_lock);
1036 check_disk_change(bdev);
1038 return (SET_ERROR(error));
1041 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1046 zvol_release(struct gendisk *disk, fmode_t mode)
1048 zvol_state_t *zv = disk->private_data;
1051 if (!mutex_owned(&zvol_state_lock)) {
1052 mutex_enter(&zvol_state_lock);
1056 ASSERT3P(zv, !=, NULL);
1057 ASSERT3U(zv->zv_open_count, >, 0);
1058 zv->zv_open_count--;
1059 if (zv->zv_open_count == 0)
1060 zvol_last_close(zv);
1063 mutex_exit(&zvol_state_lock);
1065 #ifndef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1071 zvol_ioctl(struct block_device *bdev, fmode_t mode,
1072 unsigned int cmd, unsigned long arg)
1074 zvol_state_t *zv = bdev->bd_disk->private_data;
1078 return (SET_ERROR(-ENXIO));
1082 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1085 error = copy_to_user((void *)arg, zv->zv_name, MAXNAMELEN);
1094 return (SET_ERROR(error));
1097 #ifdef CONFIG_COMPAT
1099 zvol_compat_ioctl(struct block_device *bdev, fmode_t mode,
1100 unsigned cmd, unsigned long arg)
1102 return (zvol_ioctl(bdev, mode, cmd, arg));
1105 #define zvol_compat_ioctl NULL
1108 static int zvol_media_changed(struct gendisk *disk)
1110 zvol_state_t *zv = disk->private_data;
1112 return (zv->zv_changed);
1115 static int zvol_revalidate_disk(struct gendisk *disk)
1117 zvol_state_t *zv = disk->private_data;
1120 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1126 * Provide a simple virtual geometry for legacy compatibility. For devices
1127 * smaller than 1 MiB a small head and sector count is used to allow very
1128 * tiny devices. For devices over 1 Mib a standard head and sector count
1129 * is used to keep the cylinders count reasonable.
1132 zvol_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1134 zvol_state_t *zv = bdev->bd_disk->private_data;
1135 sector_t sectors = get_capacity(zv->zv_disk);
1137 if (sectors > 2048) {
1146 geo->cylinders = sectors / (geo->heads * geo->sectors);
1151 static struct kobject *
1152 zvol_probe(dev_t dev, int *part, void *arg)
1155 struct kobject *kobj;
1157 mutex_enter(&zvol_state_lock);
1158 zv = zvol_find_by_dev(dev);
1159 kobj = zv ? get_disk(zv->zv_disk) : NULL;
1160 mutex_exit(&zvol_state_lock);
1165 #ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS
1166 static struct block_device_operations zvol_ops = {
1168 .release = zvol_release,
1169 .ioctl = zvol_ioctl,
1170 .compat_ioctl = zvol_compat_ioctl,
1171 .media_changed = zvol_media_changed,
1172 .revalidate_disk = zvol_revalidate_disk,
1173 .getgeo = zvol_getgeo,
1174 .owner = THIS_MODULE,
1177 #else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1180 zvol_open_by_inode(struct inode *inode, struct file *file)
1182 return (zvol_open(inode->i_bdev, file->f_mode));
1186 zvol_release_by_inode(struct inode *inode, struct file *file)
1188 return (zvol_release(inode->i_bdev->bd_disk, file->f_mode));
1192 zvol_ioctl_by_inode(struct inode *inode, struct file *file,
1193 unsigned int cmd, unsigned long arg)
1195 if (file == NULL || inode == NULL)
1196 return (SET_ERROR(-EINVAL));
1198 return (zvol_ioctl(inode->i_bdev, file->f_mode, cmd, arg));
1201 #ifdef CONFIG_COMPAT
1203 zvol_compat_ioctl_by_inode(struct file *file,
1204 unsigned int cmd, unsigned long arg)
1207 return (SET_ERROR(-EINVAL));
1209 return (zvol_compat_ioctl(file->f_dentry->d_inode->i_bdev,
1210 file->f_mode, cmd, arg));
1213 #define zvol_compat_ioctl_by_inode NULL
1216 static struct block_device_operations zvol_ops = {
1217 .open = zvol_open_by_inode,
1218 .release = zvol_release_by_inode,
1219 .ioctl = zvol_ioctl_by_inode,
1220 .compat_ioctl = zvol_compat_ioctl_by_inode,
1221 .media_changed = zvol_media_changed,
1222 .revalidate_disk = zvol_revalidate_disk,
1223 .getgeo = zvol_getgeo,
1224 .owner = THIS_MODULE,
1226 #endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1229 * Allocate memory for a new zvol_state_t and setup the required
1230 * request queue and generic disk structures for the block device.
1232 static zvol_state_t *
1233 zvol_alloc(dev_t dev, const char *name)
1238 zv = kmem_zalloc(sizeof (zvol_state_t), KM_PUSHPAGE);
1240 spin_lock_init(&zv->zv_lock);
1241 list_link_init(&zv->zv_next);
1243 zv->zv_queue = blk_init_queue(zvol_request, &zv->zv_lock);
1244 if (zv->zv_queue == NULL)
1247 #ifdef HAVE_ELEVATOR_CHANGE
1248 error = elevator_change(zv->zv_queue, "noop");
1249 #endif /* HAVE_ELEVATOR_CHANGE */
1251 printk("ZFS: Unable to set \"%s\" scheduler for zvol %s: %d\n",
1252 "noop", name, error);
1256 #ifdef HAVE_BLK_QUEUE_FLUSH
1257 blk_queue_flush(zv->zv_queue, VDEV_REQ_FLUSH | VDEV_REQ_FUA);
1259 blk_queue_ordered(zv->zv_queue, QUEUE_ORDERED_DRAIN, NULL);
1260 #endif /* HAVE_BLK_QUEUE_FLUSH */
1262 zv->zv_disk = alloc_disk(ZVOL_MINORS);
1263 if (zv->zv_disk == NULL)
1266 zv->zv_queue->queuedata = zv;
1268 zv->zv_open_count = 0;
1269 strlcpy(zv->zv_name, name, MAXNAMELEN);
1271 mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
1272 avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
1273 sizeof (rl_t), offsetof(rl_t, r_node));
1274 zv->zv_znode.z_is_zvol = TRUE;
1276 zv->zv_disk->major = zvol_major;
1277 zv->zv_disk->first_minor = (dev & MINORMASK);
1278 zv->zv_disk->fops = &zvol_ops;
1279 zv->zv_disk->private_data = zv;
1280 zv->zv_disk->queue = zv->zv_queue;
1281 snprintf(zv->zv_disk->disk_name, DISK_NAME_LEN, "%s%d",
1282 ZVOL_DEV_NAME, (dev & MINORMASK));
1287 blk_cleanup_queue(zv->zv_queue);
1289 kmem_free(zv, sizeof (zvol_state_t));
1295 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1298 zvol_free(zvol_state_t *zv)
1300 avl_destroy(&zv->zv_znode.z_range_avl);
1301 mutex_destroy(&zv->zv_znode.z_range_lock);
1303 del_gendisk(zv->zv_disk);
1304 blk_cleanup_queue(zv->zv_queue);
1305 put_disk(zv->zv_disk);
1307 kmem_free(zv, sizeof (zvol_state_t));
1311 __zvol_snapdev_hidden(const char *name)
1318 parent = kmem_alloc(MAXPATHLEN, KM_PUSHPAGE);
1319 (void) strlcpy(parent, name, MAXPATHLEN);
1321 if ((atp = strrchr(parent, '@')) != NULL) {
1323 error = dsl_prop_get_integer(parent, "snapdev", &snapdev, NULL);
1324 if ((error == 0) && (snapdev == ZFS_SNAPDEV_HIDDEN))
1325 error = SET_ERROR(ENODEV);
1328 kmem_free(parent, MAXPATHLEN);
1330 return (SET_ERROR(error));
1334 __zvol_create_minor(const char *name, boolean_t ignore_snapdev)
1338 dmu_object_info_t *doi;
1343 ASSERT(MUTEX_HELD(&zvol_state_lock));
1345 zv = zvol_find_by_name(name);
1347 error = SET_ERROR(EEXIST);
1351 if (ignore_snapdev == B_FALSE) {
1352 error = __zvol_snapdev_hidden(name);
1357 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_PUSHPAGE);
1359 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
1363 error = dmu_object_info(os, ZVOL_OBJ, doi);
1365 goto out_dmu_objset_disown;
1367 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
1369 goto out_dmu_objset_disown;
1371 error = zvol_find_minor(&minor);
1373 goto out_dmu_objset_disown;
1375 zv = zvol_alloc(MKDEV(zvol_major, minor), name);
1377 error = SET_ERROR(EAGAIN);
1378 goto out_dmu_objset_disown;
1381 if (dmu_objset_is_snapshot(os))
1382 zv->zv_flags |= ZVOL_RDONLY;
1384 zv->zv_volblocksize = doi->doi_data_block_size;
1385 zv->zv_volsize = volsize;
1388 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1390 blk_queue_max_hw_sectors(zv->zv_queue, UINT_MAX);
1391 blk_queue_max_segments(zv->zv_queue, UINT16_MAX);
1392 blk_queue_max_segment_size(zv->zv_queue, UINT_MAX);
1393 blk_queue_physical_block_size(zv->zv_queue, zv->zv_volblocksize);
1394 blk_queue_io_opt(zv->zv_queue, zv->zv_volblocksize);
1395 #ifdef HAVE_BLK_QUEUE_DISCARD
1396 blk_queue_max_discard_sectors(zv->zv_queue,
1397 (zvol_max_discard_blocks * zv->zv_volblocksize) >> 9);
1398 blk_queue_discard_granularity(zv->zv_queue, zv->zv_volblocksize);
1399 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zv->zv_queue);
1401 #ifdef HAVE_BLK_QUEUE_NONROT
1402 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zv->zv_queue);
1405 if (spa_writeable(dmu_objset_spa(os))) {
1406 if (zil_replay_disable)
1407 zil_destroy(dmu_objset_zil(os), B_FALSE);
1409 zil_replay(os, zv, zvol_replay_vector);
1412 zv->zv_objset = NULL;
1413 out_dmu_objset_disown:
1414 dmu_objset_disown(os, zvol_tag);
1416 kmem_free(doi, sizeof (dmu_object_info_t));
1421 add_disk(zv->zv_disk);
1424 return (SET_ERROR(error));
1428 * Create a block device minor node and setup the linkage between it
1429 * and the specified volume. Once this function returns the block
1430 * device is live and ready for use.
1433 zvol_create_minor(const char *name)
1437 mutex_enter(&zvol_state_lock);
1438 error = __zvol_create_minor(name, B_FALSE);
1439 mutex_exit(&zvol_state_lock);
1441 return (SET_ERROR(error));
1445 __zvol_remove_minor(const char *name)
1449 ASSERT(MUTEX_HELD(&zvol_state_lock));
1451 zv = zvol_find_by_name(name);
1453 return (SET_ERROR(ENXIO));
1455 if (zv->zv_open_count > 0)
1456 return (SET_ERROR(EBUSY));
1465 * Remove a block device minor node for the specified volume.
1468 zvol_remove_minor(const char *name)
1472 mutex_enter(&zvol_state_lock);
1473 error = __zvol_remove_minor(name);
1474 mutex_exit(&zvol_state_lock);
1476 return (SET_ERROR(error));
1480 * Rename a block device minor mode for the specified volume.
1483 __zvol_rename_minor(zvol_state_t *zv, const char *newname)
1485 int readonly = get_disk_ro(zv->zv_disk);
1487 ASSERT(MUTEX_HELD(&zvol_state_lock));
1489 strlcpy(zv->zv_name, newname, sizeof (zv->zv_name));
1492 * The block device's read-only state is briefly changed causing
1493 * a KOBJ_CHANGE uevent to be issued. This ensures udev detects
1494 * the name change and fixes the symlinks. This does not change
1495 * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
1496 * changes. This would normally be done using kobject_uevent() but
1497 * that is a GPL-only symbol which is why we need this workaround.
1499 set_disk_ro(zv->zv_disk, !readonly);
1500 set_disk_ro(zv->zv_disk, readonly);
1504 zvol_create_minors_cb(const char *dsname, void *arg)
1506 (void) zvol_create_minor(dsname);
1512 * Create minors for specified dataset including children and snapshots.
1515 zvol_create_minors(const char *name)
1519 if (!zvol_inhibit_dev)
1520 error = dmu_objset_find((char *)name, zvol_create_minors_cb,
1521 NULL, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1523 return (SET_ERROR(error));
1527 * Remove minors for specified dataset including children and snapshots.
1530 zvol_remove_minors(const char *name)
1532 zvol_state_t *zv, *zv_next;
1533 int namelen = ((name) ? strlen(name) : 0);
1535 if (zvol_inhibit_dev)
1538 mutex_enter(&zvol_state_lock);
1540 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1541 zv_next = list_next(&zvol_state_list, zv);
1543 if (name == NULL || strcmp(zv->zv_name, name) == 0 ||
1544 (strncmp(zv->zv_name, name, namelen) == 0 &&
1545 zv->zv_name[namelen] == '/')) {
1551 mutex_exit(&zvol_state_lock);
1555 * Rename minors for specified dataset including children and snapshots.
1558 zvol_rename_minors(const char *oldname, const char *newname)
1560 zvol_state_t *zv, *zv_next;
1561 int oldnamelen, newnamelen;
1564 if (zvol_inhibit_dev)
1567 oldnamelen = strlen(oldname);
1568 newnamelen = strlen(newname);
1569 name = kmem_alloc(MAXNAMELEN, KM_PUSHPAGE);
1571 mutex_enter(&zvol_state_lock);
1573 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1574 zv_next = list_next(&zvol_state_list, zv);
1576 if (strcmp(zv->zv_name, oldname) == 0) {
1577 __zvol_rename_minor(zv, newname);
1578 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 &&
1579 (zv->zv_name[oldnamelen] == '/' ||
1580 zv->zv_name[oldnamelen] == '@')) {
1581 snprintf(name, MAXNAMELEN, "%s%c%s", newname,
1582 zv->zv_name[oldnamelen],
1583 zv->zv_name + oldnamelen + 1);
1584 __zvol_rename_minor(zv, name);
1588 mutex_exit(&zvol_state_lock);
1590 kmem_free(name, MAXNAMELEN);
1594 snapdev_snapshot_changed_cb(const char *dsname, void *arg) {
1595 uint64_t snapdev = *(uint64_t *) arg;
1597 if (strchr(dsname, '@') == NULL)
1601 case ZFS_SNAPDEV_VISIBLE:
1602 mutex_enter(&zvol_state_lock);
1603 (void) __zvol_create_minor(dsname, B_TRUE);
1604 mutex_exit(&zvol_state_lock);
1606 case ZFS_SNAPDEV_HIDDEN:
1607 (void) zvol_remove_minor(dsname);
1615 zvol_set_snapdev(const char *dsname, uint64_t snapdev) {
1616 (void) dmu_objset_find((char *) dsname, snapdev_snapshot_changed_cb,
1617 &snapdev, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
1618 /* caller should continue to modify snapdev property */
1627 list_create(&zvol_state_list, sizeof (zvol_state_t),
1628 offsetof(zvol_state_t, zv_next));
1630 mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1632 zvol_taskq = taskq_create(ZVOL_DRIVER, zvol_threads, maxclsyspri,
1633 zvol_threads, INT_MAX, TASKQ_PREPOPULATE);
1634 if (zvol_taskq == NULL) {
1635 printk(KERN_INFO "ZFS: taskq_create() failed\n");
1640 error = register_blkdev(zvol_major, ZVOL_DRIVER);
1642 printk(KERN_INFO "ZFS: register_blkdev() failed %d\n", error);
1646 blk_register_region(MKDEV(zvol_major, 0), 1UL << MINORBITS,
1647 THIS_MODULE, zvol_probe, NULL, NULL);
1652 taskq_destroy(zvol_taskq);
1654 mutex_destroy(&zvol_state_lock);
1655 list_destroy(&zvol_state_list);
1657 return (SET_ERROR(error));
1663 zvol_remove_minors(NULL);
1664 blk_unregister_region(MKDEV(zvol_major, 0), 1UL << MINORBITS);
1665 unregister_blkdev(zvol_major, ZVOL_DRIVER);
1666 taskq_destroy(zvol_taskq);
1667 mutex_destroy(&zvol_state_lock);
1668 list_destroy(&zvol_state_list);
1671 module_param(zvol_inhibit_dev, uint, 0644);
1672 MODULE_PARM_DESC(zvol_inhibit_dev, "Do not create zvol device nodes");
1674 module_param(zvol_major, uint, 0444);
1675 MODULE_PARM_DESC(zvol_major, "Major number for zvol device");
1677 module_param(zvol_threads, uint, 0444);
1678 MODULE_PARM_DESC(zvol_threads, "Number of threads for zvol device");
1680 module_param(zvol_max_discard_blocks, ulong, 0444);
1681 MODULE_PARM_DESC(zvol_max_discard_blocks, "Max number of blocks to discard");