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.
38 #include <sys/dmu_traverse.h>
39 #include <sys/dsl_dataset.h>
40 #include <sys/dsl_prop.h>
42 #include <sys/zil_impl.h>
44 #include <sys/zfs_rlock.h>
45 #include <sys/zfs_znode.h>
47 #include <linux/blkdev_compat.h>
49 unsigned int zvol_major = ZVOL_MAJOR;
50 unsigned int zvol_threads = 32;
52 static taskq_t *zvol_taskq;
53 static kmutex_t zvol_state_lock;
54 static list_t zvol_state_list;
55 static char *zvol_tag = "zvol_tag";
58 * The in-core state of each volume.
60 typedef struct zvol_state {
61 char zv_name[MAXNAMELEN]; /* name */
62 uint64_t zv_volsize; /* advertised space */
63 uint64_t zv_volblocksize;/* volume block size */
64 objset_t *zv_objset; /* objset handle */
65 uint32_t zv_flags; /* ZVOL_* flags */
66 uint32_t zv_open_count; /* open counts */
67 uint32_t zv_changed; /* disk changed */
68 zilog_t *zv_zilog; /* ZIL handle */
69 znode_t zv_znode; /* for range locking */
70 dmu_buf_t *zv_dbuf; /* bonus handle */
71 dev_t zv_dev; /* device id */
72 struct gendisk *zv_disk; /* generic disk */
73 struct request_queue *zv_queue; /* request queue */
74 spinlock_t zv_lock; /* request queue lock */
75 list_node_t zv_next; /* next zvol_state_t linkage */
78 #define ZVOL_RDONLY 0x1
81 * Find the next available range of ZVOL_MINORS minor numbers. The
82 * zvol_state_list is kept in ascending minor order so we simply need
83 * to scan the list for the first gap in the sequence. This allows us
84 * to recycle minor number as devices are created and removed.
87 zvol_find_minor(unsigned *minor)
92 ASSERT(MUTEX_HELD(&zvol_state_lock));
93 for (zv = list_head(&zvol_state_list); zv != NULL;
94 zv = list_next(&zvol_state_list, zv), *minor += ZVOL_MINORS) {
95 if (MINOR(zv->zv_dev) != MINOR(*minor))
99 /* All minors are in use */
100 if (*minor >= (1 << MINORBITS))
107 * Find a zvol_state_t given the full major+minor dev_t.
109 static zvol_state_t *
110 zvol_find_by_dev(dev_t dev)
114 ASSERT(MUTEX_HELD(&zvol_state_lock));
115 for (zv = list_head(&zvol_state_list); zv != NULL;
116 zv = list_next(&zvol_state_list, zv)) {
117 if (zv->zv_dev == dev)
125 * Find a zvol_state_t given the name provided at zvol_alloc() time.
127 static zvol_state_t *
128 zvol_find_by_name(const char *name)
132 ASSERT(MUTEX_HELD(&zvol_state_lock));
133 for (zv = list_head(&zvol_state_list); zv != NULL;
134 zv = list_next(&zvol_state_list, zv)) {
135 if (!strncmp(zv->zv_name, name, MAXNAMELEN))
143 * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation.
146 zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
148 zfs_creat_t *zct = arg;
149 nvlist_t *nvprops = zct->zct_props;
151 uint64_t volblocksize, volsize;
153 VERIFY(nvlist_lookup_uint64(nvprops,
154 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
155 if (nvlist_lookup_uint64(nvprops,
156 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
157 volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
160 * These properties must be removed from the list so the generic
161 * property setting step won't apply to them.
163 VERIFY(nvlist_remove_all(nvprops,
164 zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
165 (void) nvlist_remove_all(nvprops,
166 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
168 error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
172 error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
176 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
181 * ZFS_IOC_OBJSET_STATS entry point.
184 zvol_get_stats(objset_t *os, nvlist_t *nv)
187 dmu_object_info_t *doi;
190 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
194 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
195 doi = kmem_alloc(sizeof(dmu_object_info_t), KM_SLEEP);
196 error = dmu_object_info(os, ZVOL_OBJ, doi);
199 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
200 doi->doi_data_block_size);
203 kmem_free(doi, sizeof(dmu_object_info_t));
209 * Sanity check volume size.
212 zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
217 if (volsize % blocksize != 0)
221 if (volsize - 1 > MAXOFFSET_T)
228 * Ensure the zap is flushed then inform the VFS of the capacity change.
231 zvol_update_volsize(zvol_state_t *zv, uint64_t volsize, objset_t *os)
233 struct block_device *bdev;
237 ASSERT(MUTEX_HELD(&zvol_state_lock));
239 tx = dmu_tx_create(os);
240 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
241 error = dmu_tx_assign(tx, TXG_WAIT);
247 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
254 error = dmu_free_long_range(os,
255 ZVOL_OBJ, volsize, DMU_OBJECT_END);
259 bdev = bdget_disk(zv->zv_disk, 0);
264 * Added check_disk_size_change() helper function.
266 #ifdef HAVE_CHECK_DISK_SIZE_CHANGE
267 set_capacity(zv->zv_disk, volsize >> 9);
268 zv->zv_volsize = volsize;
269 check_disk_size_change(zv->zv_disk, bdev);
271 zv->zv_volsize = volsize;
273 (void) check_disk_change(bdev);
274 #endif /* HAVE_CHECK_DISK_SIZE_CHANGE */
282 * Set ZFS_PROP_VOLSIZE set entry point.
285 zvol_set_volsize(const char *name, uint64_t volsize)
288 dmu_object_info_t *doi;
293 mutex_enter(&zvol_state_lock);
295 zv = zvol_find_by_name(name);
301 doi = kmem_alloc(sizeof(dmu_object_info_t), KM_SLEEP);
303 error = dmu_objset_hold(name, FTAG, &os);
307 if ((error = dmu_object_info(os, ZVOL_OBJ, doi)) != 0 ||
308 (error = zvol_check_volsize(volsize,doi->doi_data_block_size)) != 0)
311 VERIFY(dsl_prop_get_integer(name, "readonly", &readonly, NULL) == 0);
317 if (get_disk_ro(zv->zv_disk) || (zv->zv_flags & ZVOL_RDONLY)) {
322 error = zvol_update_volsize(zv, volsize, os);
324 kmem_free(doi, sizeof(dmu_object_info_t));
327 dmu_objset_rele(os, FTAG);
329 mutex_exit(&zvol_state_lock);
335 * Sanity check volume block size.
338 zvol_check_volblocksize(uint64_t volblocksize)
340 if (volblocksize < SPA_MINBLOCKSIZE ||
341 volblocksize > SPA_MAXBLOCKSIZE ||
349 * Set ZFS_PROP_VOLBLOCKSIZE set entry point.
352 zvol_set_volblocksize(const char *name, uint64_t volblocksize)
358 mutex_enter(&zvol_state_lock);
360 zv = zvol_find_by_name(name);
366 if (get_disk_ro(zv->zv_disk) || (zv->zv_flags & ZVOL_RDONLY)) {
371 tx = dmu_tx_create(zv->zv_objset);
372 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
373 error = dmu_tx_assign(tx, TXG_WAIT);
377 error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
378 volblocksize, 0, tx);
379 if (error == ENOTSUP)
383 zv->zv_volblocksize = volblocksize;
386 mutex_exit(&zvol_state_lock);
392 * Replay a TX_WRITE ZIL transaction that didn't get committed
393 * after a system failure
396 zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
398 objset_t *os = zv->zv_objset;
399 char *data = (char *)(lr + 1); /* data follows lr_write_t */
400 uint64_t off = lr->lr_offset;
401 uint64_t len = lr->lr_length;
406 byteswap_uint64_array(lr, sizeof (*lr));
408 tx = dmu_tx_create(os);
409 dmu_tx_hold_write(tx, ZVOL_OBJ, off, len);
410 error = dmu_tx_assign(tx, TXG_WAIT);
414 dmu_write(os, ZVOL_OBJ, off, len, data, tx);
422 zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
428 * Callback vectors for replaying records.
429 * Only TX_WRITE is needed for zvol.
431 zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
432 (zil_replay_func_t *)zvol_replay_err, /* no such transaction type */
433 (zil_replay_func_t *)zvol_replay_err, /* TX_CREATE */
434 (zil_replay_func_t *)zvol_replay_err, /* TX_MKDIR */
435 (zil_replay_func_t *)zvol_replay_err, /* TX_MKXATTR */
436 (zil_replay_func_t *)zvol_replay_err, /* TX_SYMLINK */
437 (zil_replay_func_t *)zvol_replay_err, /* TX_REMOVE */
438 (zil_replay_func_t *)zvol_replay_err, /* TX_RMDIR */
439 (zil_replay_func_t *)zvol_replay_err, /* TX_LINK */
440 (zil_replay_func_t *)zvol_replay_err, /* TX_RENAME */
441 (zil_replay_func_t *)zvol_replay_write, /* TX_WRITE */
442 (zil_replay_func_t *)zvol_replay_err, /* TX_TRUNCATE */
443 (zil_replay_func_t *)zvol_replay_err, /* TX_SETATTR */
444 (zil_replay_func_t *)zvol_replay_err, /* TX_ACL */
448 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
450 * We store data in the log buffers if it's small enough.
451 * Otherwise we will later flush the data out via dmu_sync().
453 ssize_t zvol_immediate_write_sz = 32768;
456 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx,
457 uint64_t offset, uint64_t size, int sync)
459 uint32_t blocksize = zv->zv_volblocksize;
460 zilog_t *zilog = zv->zv_zilog;
463 if (zil_replaying(zilog, tx))
466 slogging = spa_has_slogs(zilog->zl_spa);
472 itx_wr_state_t write_state;
475 * Unlike zfs_log_write() we can be called with
476 * up to DMU_MAX_ACCESS/2 (5MB) writes.
478 if (blocksize > zvol_immediate_write_sz && !slogging &&
479 size >= blocksize && offset % blocksize == 0) {
480 write_state = WR_INDIRECT; /* uses dmu_sync */
483 write_state = WR_COPIED;
484 len = MIN(ZIL_MAX_LOG_DATA, size);
486 write_state = WR_NEED_COPY;
487 len = MIN(ZIL_MAX_LOG_DATA, size);
490 itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
491 (write_state == WR_COPIED ? len : 0));
492 lr = (lr_write_t *)&itx->itx_lr;
493 if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
494 ZVOL_OBJ, offset, len, lr+1, DMU_READ_NO_PREFETCH) != 0) {
495 zil_itx_destroy(itx);
496 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
497 lr = (lr_write_t *)&itx->itx_lr;
498 write_state = WR_NEED_COPY;
501 itx->itx_wr_state = write_state;
502 if (write_state == WR_NEED_COPY)
504 lr->lr_foid = ZVOL_OBJ;
505 lr->lr_offset = offset;
508 BP_ZERO(&lr->lr_blkptr);
510 itx->itx_private = zv;
511 itx->itx_sync = sync;
513 (void) zil_itx_assign(zilog, itx, tx);
521 * Common write path running under the zvol taskq context. This function
522 * is responsible for copying the request structure data in to the DMU and
523 * signaling the request queue with the result of the copy.
526 zvol_write(void *arg)
528 struct request *req = (struct request *)arg;
529 struct request_queue *q = req->q;
530 zvol_state_t *zv = q->queuedata;
531 uint64_t offset = blk_rq_pos(req) << 9;
532 uint64_t size = blk_rq_bytes(req);
537 if (req->cmd_flags & VDEV_REQ_FLUSH)
538 zil_commit(zv->zv_zilog, ZVOL_OBJ);
541 * Some requests are just for flush and nothing else.
544 blk_end_request(req, 0, size);
548 rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_WRITER);
550 tx = dmu_tx_create(zv->zv_objset);
551 dmu_tx_hold_write(tx, ZVOL_OBJ, offset, size);
553 /* This will only fail for ENOSPC */
554 error = dmu_tx_assign(tx, TXG_WAIT);
557 zfs_range_unlock(rl);
558 blk_end_request(req, -error, size);
562 error = dmu_write_req(zv->zv_objset, ZVOL_OBJ, req, tx);
564 zvol_log_write(zv, tx, offset, size,
565 req->cmd_flags & VDEV_REQ_FUA);
568 zfs_range_unlock(rl);
570 if ((req->cmd_flags & VDEV_REQ_FUA) ||
571 zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)
572 zil_commit(zv->zv_zilog, ZVOL_OBJ);
574 blk_end_request(req, -error, size);
578 * Common read path running under the zvol taskq context. This function
579 * is responsible for copying the requested data out of the DMU and in to
580 * a linux request structure. It then must signal the request queue with
581 * an error code describing the result of the copy.
586 struct request *req = (struct request *)arg;
587 struct request_queue *q = req->q;
588 zvol_state_t *zv = q->queuedata;
589 uint64_t offset = blk_rq_pos(req) << 9;
590 uint64_t size = blk_rq_bytes(req);
595 blk_end_request(req, 0, size);
599 rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
601 error = dmu_read_req(zv->zv_objset, ZVOL_OBJ, req);
603 zfs_range_unlock(rl);
605 /* convert checksum errors into IO errors */
609 blk_end_request(req, -error, size);
613 * Request will be added back to the request queue and retried if
614 * it cannot be immediately dispatched to the taskq for handling
617 zvol_dispatch(task_func_t func, struct request *req)
619 if (!taskq_dispatch(zvol_taskq, func, (void *)req, TQ_NOSLEEP))
620 blk_requeue_request(req->q, req);
624 * Common request path. Rather than registering a custom make_request()
625 * function we use the generic Linux version. This is done because it allows
626 * us to easily merge read requests which would otherwise we performed
627 * synchronously by the DMU. This is less critical in write case where the
628 * DMU will perform the correct merging within a transaction group. Using
629 * the generic make_request() also let's use leverage the fact that the
630 * elevator with ensure correct ordering in regards to barrior IOs. On
631 * the downside it means that in the write case we end up doing request
632 * merging twice once in the elevator and once in the DMU.
634 * The request handler is called under a spin lock so all the real work
635 * is handed off to be done in the context of the zvol taskq. This function
636 * simply performs basic request sanity checking and hands off the request.
639 zvol_request(struct request_queue *q)
641 zvol_state_t *zv = q->queuedata;
645 while ((req = blk_fetch_request(q)) != NULL) {
646 size = blk_rq_bytes(req);
648 if (size != 0 && blk_rq_pos(req) + blk_rq_sectors(req) >
649 get_capacity(zv->zv_disk)) {
651 "%s: bad access: block=%llu, count=%lu\n",
652 req->rq_disk->disk_name,
653 (long long unsigned)blk_rq_pos(req),
654 (long unsigned)blk_rq_sectors(req));
655 __blk_end_request(req, -EIO, size);
659 if (!blk_fs_request(req)) {
660 printk(KERN_INFO "%s: non-fs cmd\n",
661 req->rq_disk->disk_name);
662 __blk_end_request(req, -EIO, size);
666 switch (rq_data_dir(req)) {
668 zvol_dispatch(zvol_read, req);
671 if (unlikely(get_disk_ro(zv->zv_disk)) ||
672 unlikely(zv->zv_flags & ZVOL_RDONLY)) {
673 __blk_end_request(req, -EROFS, size);
677 zvol_dispatch(zvol_write, req);
680 printk(KERN_INFO "%s: unknown cmd: %d\n",
681 req->rq_disk->disk_name, (int)rq_data_dir(req));
682 __blk_end_request(req, -EIO, size);
689 zvol_get_done(zgd_t *zgd, int error)
692 dmu_buf_rele(zgd->zgd_db, zgd);
694 zfs_range_unlock(zgd->zgd_rl);
696 if (error == 0 && zgd->zgd_bp)
697 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
699 kmem_free(zgd, sizeof (zgd_t));
703 * Get data to generate a TX_WRITE intent log record.
706 zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
708 zvol_state_t *zv = arg;
709 objset_t *os = zv->zv_objset;
710 uint64_t offset = lr->lr_offset;
711 uint64_t size = lr->lr_length;
719 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
720 zgd->zgd_zilog = zv->zv_zilog;
721 zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
724 * Write records come in two flavors: immediate and indirect.
725 * For small writes it's cheaper to store the data with the
726 * log record (immediate); for large writes it's cheaper to
727 * sync the data and get a pointer to it (indirect) so that
728 * we don't have to write the data twice.
730 if (buf != NULL) { /* immediate write */
731 error = dmu_read(os, ZVOL_OBJ, offset, size, buf,
732 DMU_READ_NO_PREFETCH);
734 size = zv->zv_volblocksize;
735 offset = P2ALIGN_TYPED(offset, size, uint64_t);
736 error = dmu_buf_hold(os, ZVOL_OBJ, offset, zgd, &db,
737 DMU_READ_NO_PREFETCH);
740 zgd->zgd_bp = &lr->lr_blkptr;
743 ASSERT(db->db_offset == offset);
744 ASSERT(db->db_size == size);
746 error = dmu_sync(zio, lr->lr_common.lrc_txg,
754 zvol_get_done(zgd, error);
760 * The zvol_state_t's are inserted in increasing MINOR(dev_t) order.
763 zvol_insert(zvol_state_t *zv_insert)
765 zvol_state_t *zv = NULL;
767 ASSERT(MUTEX_HELD(&zvol_state_lock));
768 ASSERT3U(MINOR(zv_insert->zv_dev) & ZVOL_MINOR_MASK, ==, 0);
769 for (zv = list_head(&zvol_state_list); zv != NULL;
770 zv = list_next(&zvol_state_list, zv)) {
771 if (MINOR(zv->zv_dev) > MINOR(zv_insert->zv_dev))
775 list_insert_before(&zvol_state_list, zv, zv_insert);
779 * Simply remove the zvol from to list of zvols.
782 zvol_remove(zvol_state_t *zv_remove)
784 ASSERT(MUTEX_HELD(&zvol_state_lock));
785 list_remove(&zvol_state_list, zv_remove);
789 zvol_first_open(zvol_state_t *zv)
796 /* lie and say we're read-only */
797 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, 1, zvol_tag, &os);
801 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
803 dmu_objset_disown(os, zvol_tag);
808 error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
810 dmu_objset_disown(os, zvol_tag);
814 set_capacity(zv->zv_disk, volsize >> 9);
815 zv->zv_volsize = volsize;
816 zv->zv_zilog = zil_open(os, zvol_get_data);
818 VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL) == 0);
819 if (ro || dmu_objset_is_snapshot(os)) {
820 set_disk_ro(zv->zv_disk, 1);
821 zv->zv_flags |= ZVOL_RDONLY;
823 set_disk_ro(zv->zv_disk, 0);
824 zv->zv_flags &= ~ZVOL_RDONLY;
831 zvol_last_close(zvol_state_t *zv)
833 zil_close(zv->zv_zilog);
835 dmu_buf_rele(zv->zv_dbuf, zvol_tag);
837 dmu_objset_disown(zv->zv_objset, zvol_tag);
838 zv->zv_objset = NULL;
842 zvol_open(struct block_device *bdev, fmode_t flag)
844 zvol_state_t *zv = bdev->bd_disk->private_data;
845 int error = 0, drop_mutex = 0;
848 * If the caller is already holding the mutex do not take it
849 * again, this will happen as part of zvol_create_minor().
850 * Once add_disk() is called the device is live and the kernel
851 * will attempt to open it to read the partition information.
853 if (!mutex_owned(&zvol_state_lock)) {
854 mutex_enter(&zvol_state_lock);
858 ASSERT3P(zv, !=, NULL);
860 if (zv->zv_open_count == 0) {
861 error = zvol_first_open(zv);
866 if ((flag & FMODE_WRITE) &&
867 (get_disk_ro(zv->zv_disk) || (zv->zv_flags & ZVOL_RDONLY))) {
875 if (zv->zv_open_count == 0)
880 mutex_exit(&zvol_state_lock);
882 check_disk_change(bdev);
888 zvol_release(struct gendisk *disk, fmode_t mode)
890 zvol_state_t *zv = disk->private_data;
893 if (!mutex_owned(&zvol_state_lock)) {
894 mutex_enter(&zvol_state_lock);
898 ASSERT3P(zv, !=, NULL);
899 ASSERT3U(zv->zv_open_count, >, 0);
901 if (zv->zv_open_count == 0)
905 mutex_exit(&zvol_state_lock);
911 zvol_ioctl(struct block_device *bdev, fmode_t mode,
912 unsigned int cmd, unsigned long arg)
914 zvol_state_t *zv = bdev->bd_disk->private_data;
922 zil_commit(zv->zv_zilog, ZVOL_OBJ);
925 error = copy_to_user((void *)arg, zv->zv_name, MAXNAMELEN);
939 zvol_compat_ioctl(struct block_device *bdev, fmode_t mode,
940 unsigned cmd, unsigned long arg)
942 return zvol_ioctl(bdev, mode, cmd, arg);
945 #define zvol_compat_ioctl NULL
948 static int zvol_media_changed(struct gendisk *disk)
950 zvol_state_t *zv = disk->private_data;
952 return zv->zv_changed;
955 static int zvol_revalidate_disk(struct gendisk *disk)
957 zvol_state_t *zv = disk->private_data;
960 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
966 * Provide a simple virtual geometry for legacy compatibility. For devices
967 * smaller than 1 MiB a small head and sector count is used to allow very
968 * tiny devices. For devices over 1 Mib a standard head and sector count
969 * is used to keep the cylinders count reasonable.
972 zvol_getgeo(struct block_device *bdev, struct hd_geometry *geo)
974 zvol_state_t *zv = bdev->bd_disk->private_data;
975 sector_t sectors = get_capacity(zv->zv_disk);
977 if (sectors > 2048) {
986 geo->cylinders = sectors / (geo->heads * geo->sectors);
991 static struct kobject *
992 zvol_probe(dev_t dev, int *part, void *arg)
995 struct kobject *kobj;
997 mutex_enter(&zvol_state_lock);
998 zv = zvol_find_by_dev(dev);
999 kobj = zv ? get_disk(zv->zv_disk) : ERR_PTR(-ENOENT);
1000 mutex_exit(&zvol_state_lock);
1005 #ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS
1006 static struct block_device_operations zvol_ops = {
1008 .release = zvol_release,
1009 .ioctl = zvol_ioctl,
1010 .compat_ioctl = zvol_compat_ioctl,
1011 .media_changed = zvol_media_changed,
1012 .revalidate_disk = zvol_revalidate_disk,
1013 .getgeo = zvol_getgeo,
1014 .owner = THIS_MODULE,
1017 #else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1020 zvol_open_by_inode(struct inode *inode, struct file *file)
1022 return zvol_open(inode->i_bdev, file->f_mode);
1026 zvol_release_by_inode(struct inode *inode, struct file *file)
1028 return zvol_release(inode->i_bdev->bd_disk, file->f_mode);
1032 zvol_ioctl_by_inode(struct inode *inode, struct file *file,
1033 unsigned int cmd, unsigned long arg)
1035 if (file == NULL || inode == NULL)
1037 return zvol_ioctl(inode->i_bdev, file->f_mode, cmd, arg);
1040 # ifdef CONFIG_COMPAT
1042 zvol_compat_ioctl_by_inode(struct file *file,
1043 unsigned int cmd, unsigned long arg)
1047 return zvol_compat_ioctl(file->f_dentry->d_inode->i_bdev,
1048 file->f_mode, cmd, arg);
1051 # define zvol_compat_ioctl_by_inode NULL
1054 static struct block_device_operations zvol_ops = {
1055 .open = zvol_open_by_inode,
1056 .release = zvol_release_by_inode,
1057 .ioctl = zvol_ioctl_by_inode,
1058 .compat_ioctl = zvol_compat_ioctl_by_inode,
1059 .media_changed = zvol_media_changed,
1060 .revalidate_disk = zvol_revalidate_disk,
1061 .getgeo = zvol_getgeo,
1062 .owner = THIS_MODULE,
1064 #endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1067 * Allocate memory for a new zvol_state_t and setup the required
1068 * request queue and generic disk structures for the block device.
1070 static zvol_state_t *
1071 zvol_alloc(dev_t dev, const char *name)
1075 zv = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
1079 zv->zv_queue = blk_init_queue(zvol_request, &zv->zv_lock);
1080 if (zv->zv_queue == NULL)
1083 #ifdef HAVE_BLK_QUEUE_FLUSH
1084 blk_queue_flush(zv->zv_queue, VDEV_REQ_FLUSH | VDEV_REQ_FUA);
1086 blk_queue_ordered(zv->zv_queue, QUEUE_ORDERED_DRAIN, NULL);
1087 #endif /* HAVE_BLK_QUEUE_FLUSH */
1089 zv->zv_disk = alloc_disk(ZVOL_MINORS);
1090 if (zv->zv_disk == NULL)
1093 zv->zv_queue->queuedata = zv;
1095 zv->zv_open_count = 0;
1096 strlcpy(zv->zv_name, name, MAXNAMELEN);
1098 mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
1099 avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
1100 sizeof (rl_t), offsetof(rl_t, r_node));
1101 zv->zv_znode.z_is_zvol = TRUE;
1103 spin_lock_init(&zv->zv_lock);
1104 list_link_init(&zv->zv_next);
1106 zv->zv_disk->major = zvol_major;
1107 zv->zv_disk->first_minor = (dev & MINORMASK);
1108 zv->zv_disk->fops = &zvol_ops;
1109 zv->zv_disk->private_data = zv;
1110 zv->zv_disk->queue = zv->zv_queue;
1111 snprintf(zv->zv_disk->disk_name, DISK_NAME_LEN, "%s%d",
1112 ZVOL_DEV_NAME, (dev & MINORMASK));
1117 blk_cleanup_queue(zv->zv_queue);
1119 kmem_free(zv, sizeof (zvol_state_t));
1125 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1128 zvol_free(zvol_state_t *zv)
1130 avl_destroy(&zv->zv_znode.z_range_avl);
1131 mutex_destroy(&zv->zv_znode.z_range_lock);
1133 del_gendisk(zv->zv_disk);
1134 blk_cleanup_queue(zv->zv_queue);
1135 put_disk(zv->zv_disk);
1137 kmem_free(zv, sizeof (zvol_state_t));
1141 __zvol_create_minor(const char *name)
1145 dmu_object_info_t *doi;
1150 ASSERT(MUTEX_HELD(&zvol_state_lock));
1152 zv = zvol_find_by_name(name);
1158 doi = kmem_alloc(sizeof(dmu_object_info_t), KM_SLEEP);
1160 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
1164 error = dmu_object_info(os, ZVOL_OBJ, doi);
1166 goto out_dmu_objset_disown;
1168 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
1170 goto out_dmu_objset_disown;
1172 error = zvol_find_minor(&minor);
1174 goto out_dmu_objset_disown;
1176 zv = zvol_alloc(MKDEV(zvol_major, minor), name);
1179 goto out_dmu_objset_disown;
1182 if (dmu_objset_is_snapshot(os))
1183 zv->zv_flags |= ZVOL_RDONLY;
1185 zv->zv_volblocksize = doi->doi_data_block_size;
1186 zv->zv_volsize = volsize;
1189 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1191 blk_queue_max_hw_sectors(zv->zv_queue, UINT_MAX);
1192 blk_queue_max_segments(zv->zv_queue, UINT16_MAX);
1193 blk_queue_max_segment_size(zv->zv_queue, UINT_MAX);
1194 blk_queue_physical_block_size(zv->zv_queue, zv->zv_volblocksize);
1195 blk_queue_io_opt(zv->zv_queue, zv->zv_volblocksize);
1196 #ifdef HAVE_BLK_QUEUE_NONROT
1197 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zv->zv_queue);
1200 if (zil_replay_disable)
1201 zil_destroy(dmu_objset_zil(os), B_FALSE);
1203 zil_replay(os, zv, zvol_replay_vector);
1205 out_dmu_objset_disown:
1206 dmu_objset_disown(os, zvol_tag);
1207 zv->zv_objset = NULL;
1209 kmem_free(doi, sizeof(dmu_object_info_t));
1214 add_disk(zv->zv_disk);
1221 * Create a block device minor node and setup the linkage between it
1222 * and the specified volume. Once this function returns the block
1223 * device is live and ready for use.
1226 zvol_create_minor(const char *name)
1230 mutex_enter(&zvol_state_lock);
1231 error = __zvol_create_minor(name);
1232 mutex_exit(&zvol_state_lock);
1238 __zvol_remove_minor(const char *name)
1242 ASSERT(MUTEX_HELD(&zvol_state_lock));
1244 zv = zvol_find_by_name(name);
1248 if (zv->zv_open_count > 0)
1258 * Remove a block device minor node for the specified volume.
1261 zvol_remove_minor(const char *name)
1265 mutex_enter(&zvol_state_lock);
1266 error = __zvol_remove_minor(name);
1267 mutex_exit(&zvol_state_lock);
1273 zvol_create_minors_cb(spa_t *spa, uint64_t dsobj,
1274 const char *dsname, void *arg)
1276 if (strchr(dsname, '/') == NULL)
1279 (void) __zvol_create_minor(dsname);
1284 * Create minors for specified pool, if pool is NULL create minors
1285 * for all available pools.
1288 zvol_create_minors(const char *pool)
1293 mutex_enter(&zvol_state_lock);
1295 error = dmu_objset_find_spa(NULL, pool, zvol_create_minors_cb,
1296 NULL, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1298 mutex_enter(&spa_namespace_lock);
1299 while ((spa = spa_next(spa)) != NULL) {
1300 error = dmu_objset_find_spa(NULL,
1301 spa_name(spa), zvol_create_minors_cb, NULL,
1302 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1306 mutex_exit(&spa_namespace_lock);
1308 mutex_exit(&zvol_state_lock);
1314 * Remove minors for specified pool, if pool is NULL remove all minors.
1317 zvol_remove_minors(const char *pool)
1319 zvol_state_t *zv, *zv_next;
1322 str = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
1324 (void) strncpy(str, pool, strlen(pool));
1325 (void) strcat(str, "/");
1328 mutex_enter(&zvol_state_lock);
1329 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1330 zv_next = list_next(&zvol_state_list, zv);
1332 if (pool == NULL || !strncmp(str, zv->zv_name, strlen(str))) {
1337 mutex_exit(&zvol_state_lock);
1338 kmem_free(str, MAXNAMELEN);
1346 zvol_taskq = taskq_create(ZVOL_DRIVER, zvol_threads, maxclsyspri,
1347 zvol_threads, INT_MAX, TASKQ_PREPOPULATE);
1348 if (zvol_taskq == NULL) {
1349 printk(KERN_INFO "ZFS: taskq_create() failed\n");
1353 error = register_blkdev(zvol_major, ZVOL_DRIVER);
1355 printk(KERN_INFO "ZFS: register_blkdev() failed %d\n", error);
1356 taskq_destroy(zvol_taskq);
1360 blk_register_region(MKDEV(zvol_major, 0), 1UL << MINORBITS,
1361 THIS_MODULE, zvol_probe, NULL, NULL);
1363 mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1364 list_create(&zvol_state_list, sizeof (zvol_state_t),
1365 offsetof(zvol_state_t, zv_next));
1367 (void) zvol_create_minors(NULL);
1375 zvol_remove_minors(NULL);
1376 blk_unregister_region(MKDEV(zvol_major, 0), 1UL << MINORBITS);
1377 unregister_blkdev(zvol_major, ZVOL_DRIVER);
1378 taskq_destroy(zvol_taskq);
1379 mutex_destroy(&zvol_state_lock);
1380 list_destroy(&zvol_state_list);
1383 module_param(zvol_major, uint, 0444);
1384 MODULE_PARM_DESC(zvol_major, "Major number for zvol device");
1386 module_param(zvol_threads, uint, 0444);
1387 MODULE_PARM_DESC(zvol_threads, "Number of threads for zvol device");