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>
48 unsigned int zvol_major = ZVOL_MAJOR;
49 unsigned int zvol_threads = 0;
51 static taskq_t *zvol_taskq;
52 static kmutex_t zvol_state_lock;
53 static list_t zvol_state_list;
54 static char *zvol_tag = "zvol_tag";
57 * The in-core state of each volume.
59 typedef struct zvol_state {
60 char zv_name[DISK_NAME_LEN]; /* name */
61 uint64_t zv_volsize; /* advertised space */
62 uint64_t zv_volblocksize;/* volume block size */
63 objset_t *zv_objset; /* objset handle */
64 uint32_t zv_flags; /* ZVOL_* flags */
65 uint32_t zv_open_count; /* open counts */
66 uint32_t zv_changed; /* disk changed */
67 zilog_t *zv_zilog; /* ZIL handle */
68 znode_t zv_znode; /* for range locking */
69 dmu_buf_t *zv_dbuf; /* bonus handle */
70 dev_t zv_dev; /* device id */
71 struct gendisk *zv_disk; /* generic disk */
72 struct request_queue *zv_queue; /* request queue */
73 spinlock_t zv_lock; /* request queue lock */
74 list_node_t zv_next; /* next zvol_state_t linkage */
77 #define ZVOL_RDONLY 0x1
80 * Find the next available range of ZVOL_MINORS minor numbers. The
81 * zvol_state_list is kept in ascending minor order so we simply need
82 * to scan the list for the first gap in the sequence. This allows us
83 * to recycle minor number as devices are created and removed.
86 zvol_find_minor(unsigned *minor)
91 ASSERT(MUTEX_HELD(&zvol_state_lock));
92 for (zv = list_head(&zvol_state_list); zv != NULL;
93 zv = list_next(&zvol_state_list, zv), *minor += ZVOL_MINORS) {
94 if (MINOR(zv->zv_dev) != MINOR(*minor))
98 /* All minors are in use */
99 if (*minor >= (1 << MINORBITS))
106 * Find a zvol_state_t given the full major+minor dev_t.
108 static zvol_state_t *
109 zvol_find_by_dev(dev_t dev)
113 ASSERT(MUTEX_HELD(&zvol_state_lock));
114 for (zv = list_head(&zvol_state_list); zv != NULL;
115 zv = list_next(&zvol_state_list, zv)) {
116 if (zv->zv_dev == dev)
124 * Find a zvol_state_t given the name provided at zvol_alloc() time.
126 static zvol_state_t *
127 zvol_find_by_name(const char *name)
131 ASSERT(MUTEX_HELD(&zvol_state_lock));
132 for (zv = list_head(&zvol_state_list); zv != NULL;
133 zv = list_next(&zvol_state_list, zv)) {
134 if (!strncmp(zv->zv_name, name, DISK_NAME_LEN))
142 * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation.
145 zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
147 zfs_creat_t *zct = arg;
148 nvlist_t *nvprops = zct->zct_props;
150 uint64_t volblocksize, volsize;
152 VERIFY(nvlist_lookup_uint64(nvprops,
153 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
154 if (nvlist_lookup_uint64(nvprops,
155 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
156 volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
159 * These properties must be removed from the list so the generic
160 * property setting step won't apply to them.
162 VERIFY(nvlist_remove_all(nvprops,
163 zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
164 (void) nvlist_remove_all(nvprops,
165 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
167 error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
171 error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
175 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
180 * ZFS_IOC_OBJSET_STATS entry point.
183 zvol_get_stats(objset_t *os, nvlist_t *nv)
186 dmu_object_info_t *doi;
189 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
193 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
194 doi = kmem_alloc(sizeof(dmu_object_info_t), KM_SLEEP);
195 error = dmu_object_info(os, ZVOL_OBJ, doi);
198 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
199 doi->doi_data_block_size);
202 kmem_free(doi, sizeof(dmu_object_info_t));
208 * Sanity check volume size.
211 zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
216 if (volsize % blocksize != 0)
220 if (volsize - 1 > MAXOFFSET_T)
227 * Ensure the zap is flushed then inform the VFS of the capacity change.
230 zvol_update_volsize(zvol_state_t *zv, uint64_t volsize)
232 struct block_device *bdev;
236 ASSERT(MUTEX_HELD(&zvol_state_lock));
238 tx = dmu_tx_create(zv->zv_objset);
239 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
240 error = dmu_tx_assign(tx, TXG_WAIT);
246 error = zap_update(zv->zv_objset, ZVOL_ZAP_OBJ, "size", 8, 1,
253 error = dmu_free_long_range(zv->zv_objset,
254 ZVOL_OBJ, volsize, DMU_OBJECT_END);
258 zv->zv_volsize = volsize;
261 bdev = bdget_disk(zv->zv_disk, 0);
265 error = check_disk_change(bdev);
266 ASSERT3U(error, !=, 0);
273 * Set ZFS_PROP_VOLSIZE set entry point.
276 zvol_set_volsize(const char *name, uint64_t volsize)
279 dmu_object_info_t *doi;
284 mutex_enter(&zvol_state_lock);
286 zv = zvol_find_by_name(name);
292 doi = kmem_alloc(sizeof(dmu_object_info_t), KM_SLEEP);
294 error = dmu_objset_hold(name, FTAG, &os);
298 if ((error = dmu_object_info(os, ZVOL_OBJ, doi)) != 0 ||
299 (error = zvol_check_volsize(volsize,doi->doi_data_block_size)) != 0)
302 VERIFY(dsl_prop_get_integer(name, "readonly", &readonly, NULL) == 0);
308 if (get_disk_ro(zv->zv_disk) || (zv->zv_flags & ZVOL_RDONLY)) {
313 error = zvol_update_volsize(zv, volsize);
315 kmem_free(doi, sizeof(dmu_object_info_t));
318 dmu_objset_rele(os, FTAG);
320 mutex_exit(&zvol_state_lock);
326 * Sanity check volume block size.
329 zvol_check_volblocksize(uint64_t volblocksize)
331 if (volblocksize < SPA_MINBLOCKSIZE ||
332 volblocksize > SPA_MAXBLOCKSIZE ||
340 * Set ZFS_PROP_VOLBLOCKSIZE set entry point.
343 zvol_set_volblocksize(const char *name, uint64_t volblocksize)
349 mutex_enter(&zvol_state_lock);
351 zv = zvol_find_by_name(name);
357 if (get_disk_ro(zv->zv_disk) || (zv->zv_flags & ZVOL_RDONLY)) {
362 tx = dmu_tx_create(zv->zv_objset);
363 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
364 error = dmu_tx_assign(tx, TXG_WAIT);
368 error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
369 volblocksize, 0, tx);
370 if (error == ENOTSUP)
374 zv->zv_volblocksize = volblocksize;
377 mutex_exit(&zvol_state_lock);
383 * Replay a TX_WRITE ZIL transaction that didn't get committed
384 * after a system failure
387 zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
389 objset_t *os = zv->zv_objset;
390 char *data = (char *)(lr + 1); /* data follows lr_write_t */
391 uint64_t off = lr->lr_offset;
392 uint64_t len = lr->lr_length;
397 byteswap_uint64_array(lr, sizeof (*lr));
399 tx = dmu_tx_create(os);
400 dmu_tx_hold_write(tx, ZVOL_OBJ, off, len);
401 error = dmu_tx_assign(tx, TXG_WAIT);
405 dmu_write(os, ZVOL_OBJ, off, len, data, tx);
413 zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
419 * Callback vectors for replaying records.
420 * Only TX_WRITE is needed for zvol.
422 zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
423 (zil_replay_func_t *)zvol_replay_err, /* no such transaction type */
424 (zil_replay_func_t *)zvol_replay_err, /* TX_CREATE */
425 (zil_replay_func_t *)zvol_replay_err, /* TX_MKDIR */
426 (zil_replay_func_t *)zvol_replay_err, /* TX_MKXATTR */
427 (zil_replay_func_t *)zvol_replay_err, /* TX_SYMLINK */
428 (zil_replay_func_t *)zvol_replay_err, /* TX_REMOVE */
429 (zil_replay_func_t *)zvol_replay_err, /* TX_RMDIR */
430 (zil_replay_func_t *)zvol_replay_err, /* TX_LINK */
431 (zil_replay_func_t *)zvol_replay_err, /* TX_RENAME */
432 (zil_replay_func_t *)zvol_replay_write, /* TX_WRITE */
433 (zil_replay_func_t *)zvol_replay_err, /* TX_TRUNCATE */
434 (zil_replay_func_t *)zvol_replay_err, /* TX_SETATTR */
435 (zil_replay_func_t *)zvol_replay_err, /* TX_ACL */
439 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
441 * We store data in the log buffers if it's small enough.
442 * Otherwise we will later flush the data out via dmu_sync().
444 ssize_t zvol_immediate_write_sz = 32768;
447 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx,
448 uint64_t offset, uint64_t size, int sync)
450 uint32_t blocksize = zv->zv_volblocksize;
451 zilog_t *zilog = zv->zv_zilog;
454 if (zil_replaying(zilog, tx))
457 slogging = spa_has_slogs(zilog->zl_spa);
463 itx_wr_state_t write_state;
466 * Unlike zfs_log_write() we can be called with
467 * up to DMU_MAX_ACCESS/2 (5MB) writes.
469 if (blocksize > zvol_immediate_write_sz && !slogging &&
470 size >= blocksize && offset % blocksize == 0) {
471 write_state = WR_INDIRECT; /* uses dmu_sync */
474 write_state = WR_COPIED;
475 len = MIN(ZIL_MAX_LOG_DATA, size);
477 write_state = WR_NEED_COPY;
478 len = MIN(ZIL_MAX_LOG_DATA, size);
481 itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
482 (write_state == WR_COPIED ? len : 0));
483 lr = (lr_write_t *)&itx->itx_lr;
484 if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
485 ZVOL_OBJ, offset, len, lr+1, DMU_READ_NO_PREFETCH) != 0) {
486 zil_itx_destroy(itx);
487 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
488 lr = (lr_write_t *)&itx->itx_lr;
489 write_state = WR_NEED_COPY;
492 itx->itx_wr_state = write_state;
493 if (write_state == WR_NEED_COPY)
495 lr->lr_foid = ZVOL_OBJ;
496 lr->lr_offset = offset;
499 BP_ZERO(&lr->lr_blkptr);
501 itx->itx_private = zv;
502 itx->itx_sync = sync;
504 (void) zil_itx_assign(zilog, itx, tx);
512 * Common write path running under the zvol taskq context. This function
513 * is responsible for copying the request structure data in to the DMU and
514 * signaling the request queue with the result of the copy.
517 zvol_write(void *arg)
519 struct request *req = (struct request *)arg;
520 struct request_queue *q = req->q;
521 zvol_state_t *zv = q->queuedata;
522 uint64_t offset = blk_rq_pos(req) << 9;
523 uint64_t size = blk_rq_bytes(req);
528 rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_WRITER);
530 tx = dmu_tx_create(zv->zv_objset);
531 dmu_tx_hold_write(tx, ZVOL_OBJ, offset, size);
533 /* This will only fail for ENOSPC */
534 error = dmu_tx_assign(tx, TXG_WAIT);
537 zfs_range_unlock(rl);
538 blk_end_request(req, -error, size);
542 error = dmu_write_req(zv->zv_objset, ZVOL_OBJ, req, tx);
544 zvol_log_write(zv, tx, offset, size, rq_is_sync(req));
547 zfs_range_unlock(rl);
550 zil_commit(zv->zv_zilog, ZVOL_OBJ);
552 blk_end_request(req, -error, size);
556 * Common read path running under the zvol taskq context. This function
557 * is responsible for copying the requested data out of the DMU and in to
558 * a linux request structure. It then must signal the request queue with
559 * an error code describing the result of the copy.
564 struct request *req = (struct request *)arg;
565 struct request_queue *q = req->q;
566 zvol_state_t *zv = q->queuedata;
567 uint64_t offset = blk_rq_pos(req) << 9;
568 uint64_t size = blk_rq_bytes(req);
572 rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
574 error = dmu_read_req(zv->zv_objset, ZVOL_OBJ, req);
576 zfs_range_unlock(rl);
578 /* convert checksum errors into IO errors */
582 blk_end_request(req, -error, size);
586 * Request will be added back to the request queue and retried if
587 * it cannot be immediately dispatched to the taskq for handling
590 zvol_dispatch(task_func_t func, struct request *req)
592 if (!taskq_dispatch(zvol_taskq, func, (void *)req, TQ_NOSLEEP))
593 blk_requeue_request(req->q, req);
597 * Common request path. Rather than registering a custom make_request()
598 * function we use the generic Linux version. This is done because it allows
599 * us to easily merge read requests which would otherwise we performed
600 * synchronously by the DMU. This is less critical in write case where the
601 * DMU will perform the correct merging within a transaction group. Using
602 * the generic make_request() also let's use leverage the fact that the
603 * elevator with ensure correct ordering in regards to barrior IOs. On
604 * the downside it means that in the write case we end up doing request
605 * merging twice once in the elevator and once in the DMU.
607 * The request handler is called under a spin lock so all the real work
608 * is handed off to be done in the context of the zvol taskq. This function
609 * simply performs basic request sanity checking and hands off the request.
612 zvol_request(struct request_queue *q)
614 zvol_state_t *zv = q->queuedata;
618 while ((req = blk_fetch_request(q)) != NULL) {
619 size = blk_rq_bytes(req);
621 if (blk_rq_pos(req) + blk_rq_sectors(req) >
622 get_capacity(zv->zv_disk)) {
624 "%s: bad access: block=%llu, count=%lu\n",
625 req->rq_disk->disk_name,
626 (long long unsigned)blk_rq_pos(req),
627 (long unsigned)blk_rq_sectors(req));
628 __blk_end_request(req, -EIO, size);
632 if (!blk_fs_request(req)) {
633 printk(KERN_INFO "%s: non-fs cmd\n",
634 req->rq_disk->disk_name);
635 __blk_end_request(req, -EIO, size);
639 switch (rq_data_dir(req)) {
641 zvol_dispatch(zvol_read, req);
644 if (unlikely(get_disk_ro(zv->zv_disk)) ||
645 unlikely(zv->zv_flags & ZVOL_RDONLY)) {
646 __blk_end_request(req, -EROFS, size);
650 zvol_dispatch(zvol_write, req);
653 printk(KERN_INFO "%s: unknown cmd: %d\n",
654 req->rq_disk->disk_name, (int)rq_data_dir(req));
655 __blk_end_request(req, -EIO, size);
662 zvol_get_done(zgd_t *zgd, int error)
665 dmu_buf_rele(zgd->zgd_db, zgd);
667 zfs_range_unlock(zgd->zgd_rl);
669 if (error == 0 && zgd->zgd_bp)
670 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
672 kmem_free(zgd, sizeof (zgd_t));
676 * Get data to generate a TX_WRITE intent log record.
679 zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
681 zvol_state_t *zv = arg;
682 objset_t *os = zv->zv_objset;
683 uint64_t offset = lr->lr_offset;
684 uint64_t size = lr->lr_length;
692 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
693 zgd->zgd_zilog = zv->zv_zilog;
694 zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
697 * Write records come in two flavors: immediate and indirect.
698 * For small writes it's cheaper to store the data with the
699 * log record (immediate); for large writes it's cheaper to
700 * sync the data and get a pointer to it (indirect) so that
701 * we don't have to write the data twice.
703 if (buf != NULL) { /* immediate write */
704 error = dmu_read(os, ZVOL_OBJ, offset, size, buf,
705 DMU_READ_NO_PREFETCH);
707 size = zv->zv_volblocksize;
708 offset = P2ALIGN_TYPED(offset, size, uint64_t);
709 error = dmu_buf_hold(os, ZVOL_OBJ, offset, zgd, &db,
710 DMU_READ_NO_PREFETCH);
713 zgd->zgd_bp = &lr->lr_blkptr;
716 ASSERT(db->db_offset == offset);
717 ASSERT(db->db_size == size);
719 error = dmu_sync(zio, lr->lr_common.lrc_txg,
727 zvol_get_done(zgd, error);
733 * The zvol_state_t's are inserted in increasing MINOR(dev_t) order.
736 zvol_insert(zvol_state_t *zv_insert)
738 zvol_state_t *zv = NULL;
740 ASSERT(MUTEX_HELD(&zvol_state_lock));
741 ASSERT3U(MINOR(zv_insert->zv_dev) & ZVOL_MINOR_MASK, ==, 0);
742 for (zv = list_head(&zvol_state_list); zv != NULL;
743 zv = list_next(&zvol_state_list, zv)) {
744 if (MINOR(zv->zv_dev) > MINOR(zv_insert->zv_dev))
748 list_insert_before(&zvol_state_list, zv, zv_insert);
752 * Simply remove the zvol from to list of zvols.
755 zvol_remove(zvol_state_t *zv_remove)
757 ASSERT(MUTEX_HELD(&zvol_state_lock));
758 list_remove(&zvol_state_list, zv_remove);
762 zvol_first_open(zvol_state_t *zv)
769 /* lie and say we're read-only */
770 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, 1, zvol_tag, &os);
774 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
776 dmu_objset_disown(os, zvol_tag);
781 error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
783 dmu_objset_disown(os, zvol_tag);
787 set_capacity(zv->zv_disk, volsize >> 9);
788 zv->zv_volsize = volsize;
789 zv->zv_zilog = zil_open(os, zvol_get_data);
791 VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL) == 0);
792 if (ro || dmu_objset_is_snapshot(os)) {
793 set_disk_ro(zv->zv_disk, 1);
794 zv->zv_flags |= ZVOL_RDONLY;
796 set_disk_ro(zv->zv_disk, 0);
797 zv->zv_flags &= ~ZVOL_RDONLY;
804 zvol_last_close(zvol_state_t *zv)
806 zil_close(zv->zv_zilog);
808 dmu_buf_rele(zv->zv_dbuf, zvol_tag);
810 dmu_objset_disown(zv->zv_objset, zvol_tag);
811 zv->zv_objset = NULL;
815 zvol_open(struct block_device *bdev, fmode_t flag)
817 zvol_state_t *zv = bdev->bd_disk->private_data;
818 int error = 0, drop_mutex = 0;
821 * If the caller is already holding the mutex do not take it
822 * again, this will happen as part of zvol_create_minor().
823 * Once add_disk() is called the device is live and the kernel
824 * will attempt to open it to read the partition information.
826 if (!mutex_owned(&zvol_state_lock)) {
827 mutex_enter(&zvol_state_lock);
831 ASSERT3P(zv, !=, NULL);
833 if (zv->zv_open_count == 0) {
834 error = zvol_first_open(zv);
839 if ((flag & FMODE_WRITE) &&
840 (get_disk_ro(zv->zv_disk) || (zv->zv_flags & ZVOL_RDONLY))) {
848 if (zv->zv_open_count == 0)
853 mutex_exit(&zvol_state_lock);
855 check_disk_change(bdev);
861 zvol_release(struct gendisk *disk, fmode_t mode)
863 zvol_state_t *zv = disk->private_data;
866 if (!mutex_owned(&zvol_state_lock)) {
867 mutex_enter(&zvol_state_lock);
871 ASSERT3P(zv, !=, NULL);
872 ASSERT3U(zv->zv_open_count, >, 0);
874 if (zv->zv_open_count == 0)
878 mutex_exit(&zvol_state_lock);
884 zvol_ioctl(struct block_device *bdev, fmode_t mode,
885 unsigned int cmd, unsigned long arg)
887 zvol_state_t *zv = bdev->bd_disk->private_data;
895 zil_commit(zv->zv_zilog, ZVOL_OBJ);
909 zvol_compat_ioctl(struct block_device *bdev, fmode_t mode,
910 unsigned cmd, unsigned long arg)
912 return zvol_ioctl(bdev, mode, cmd, arg);
915 #define zvol_compat_ioctl NULL
918 static int zvol_media_changed(struct gendisk *disk)
920 zvol_state_t *zv = disk->private_data;
922 return zv->zv_changed;
925 static int zvol_revalidate_disk(struct gendisk *disk)
927 zvol_state_t *zv = disk->private_data;
930 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
936 * Provide a simple virtual geometry for legacy compatibility. For devices
937 * smaller than 1 MiB a small head and sector count is used to allow very
938 * tiny devices. For devices over 1 Mib a standard head and sector count
939 * is used to keep the cylinders count reasonable.
942 zvol_getgeo(struct block_device *bdev, struct hd_geometry *geo)
944 zvol_state_t *zv = bdev->bd_disk->private_data;
945 sector_t sectors = get_capacity(zv->zv_disk);
947 if (sectors > 2048) {
956 geo->cylinders = sectors / (geo->heads * geo->sectors);
961 static struct kobject *
962 zvol_probe(dev_t dev, int *part, void *arg)
965 struct kobject *kobj;
967 mutex_enter(&zvol_state_lock);
968 zv = zvol_find_by_dev(dev);
969 kobj = zv ? get_disk(zv->zv_disk) : ERR_PTR(-ENOENT);
970 mutex_exit(&zvol_state_lock);
975 #ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS
976 static struct block_device_operations zvol_ops = {
978 .release = zvol_release,
980 .compat_ioctl = zvol_compat_ioctl,
981 .media_changed = zvol_media_changed,
982 .revalidate_disk = zvol_revalidate_disk,
983 .getgeo = zvol_getgeo,
984 .owner = THIS_MODULE,
987 #else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
990 zvol_open_by_inode(struct inode *inode, struct file *file)
992 return zvol_open(inode->i_bdev, file->f_mode);
996 zvol_release_by_inode(struct inode *inode, struct file *file)
998 return zvol_release(inode->i_bdev->bd_disk, file->f_mode);
1002 zvol_ioctl_by_inode(struct inode *inode, struct file *file,
1003 unsigned int cmd, unsigned long arg)
1005 if (file == NULL || inode == NULL)
1007 return zvol_ioctl(inode->i_bdev, file->f_mode, cmd, arg);
1010 # ifdef CONFIG_COMPAT
1012 zvol_compat_ioctl_by_inode(struct file *file,
1013 unsigned int cmd, unsigned long arg)
1017 return zvol_compat_ioctl(file->f_dentry->d_inode->i_bdev,
1018 file->f_mode, cmd, arg);
1021 # define zvol_compat_ioctl_by_inode NULL
1024 static struct block_device_operations zvol_ops = {
1025 .open = zvol_open_by_inode,
1026 .release = zvol_release_by_inode,
1027 .ioctl = zvol_ioctl_by_inode,
1028 .compat_ioctl = zvol_compat_ioctl_by_inode,
1029 .media_changed = zvol_media_changed,
1030 .revalidate_disk = zvol_revalidate_disk,
1031 .getgeo = zvol_getgeo,
1032 .owner = THIS_MODULE,
1034 #endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1037 * Allocate memory for a new zvol_state_t and setup the required
1038 * request queue and generic disk structures for the block device.
1040 static zvol_state_t *
1041 zvol_alloc(dev_t dev, const char *name)
1045 zv = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
1049 zv->zv_queue = blk_init_queue(zvol_request, &zv->zv_lock);
1050 if (zv->zv_queue == NULL)
1053 zv->zv_disk = alloc_disk(ZVOL_MINORS);
1054 if (zv->zv_disk == NULL)
1057 zv->zv_queue->queuedata = zv;
1059 zv->zv_open_count = 0;
1060 strlcpy(zv->zv_name, name, DISK_NAME_LEN);
1062 mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
1063 avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
1064 sizeof (rl_t), offsetof(rl_t, r_node));
1065 zv->zv_znode.z_is_zvol = TRUE;
1067 spin_lock_init(&zv->zv_lock);
1068 list_link_init(&zv->zv_next);
1070 zv->zv_disk->major = zvol_major;
1071 zv->zv_disk->first_minor = (dev & MINORMASK);
1072 zv->zv_disk->fops = &zvol_ops;
1073 zv->zv_disk->private_data = zv;
1074 zv->zv_disk->queue = zv->zv_queue;
1075 snprintf(zv->zv_disk->disk_name, DISK_NAME_LEN, "%s", name);
1080 blk_cleanup_queue(zv->zv_queue);
1082 kmem_free(zv, sizeof (zvol_state_t));
1088 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1091 zvol_free(zvol_state_t *zv)
1093 avl_destroy(&zv->zv_znode.z_range_avl);
1094 mutex_destroy(&zv->zv_znode.z_range_lock);
1096 del_gendisk(zv->zv_disk);
1097 blk_cleanup_queue(zv->zv_queue);
1098 put_disk(zv->zv_disk);
1100 kmem_free(zv, sizeof (zvol_state_t));
1104 __zvol_create_minor(const char *name)
1108 dmu_object_info_t *doi;
1113 ASSERT(MUTEX_HELD(&zvol_state_lock));
1115 zv = zvol_find_by_name(name);
1121 doi = kmem_alloc(sizeof(dmu_object_info_t), KM_SLEEP);
1123 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
1127 error = dmu_object_info(os, ZVOL_OBJ, doi);
1129 goto out_dmu_objset_disown;
1131 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
1133 goto out_dmu_objset_disown;
1135 error = zvol_find_minor(&minor);
1137 goto out_dmu_objset_disown;
1139 zv = zvol_alloc(MKDEV(zvol_major, minor), name);
1142 goto out_dmu_objset_disown;
1145 if (dmu_objset_is_snapshot(os))
1146 zv->zv_flags |= ZVOL_RDONLY;
1148 zv->zv_volblocksize = doi->doi_data_block_size;
1149 zv->zv_volsize = volsize;
1152 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1154 if (zil_replay_disable)
1155 zil_destroy(dmu_objset_zil(os), B_FALSE);
1157 zil_replay(os, zv, zvol_replay_vector);
1159 out_dmu_objset_disown:
1160 dmu_objset_disown(os, zvol_tag);
1161 zv->zv_objset = NULL;
1163 kmem_free(doi, sizeof(dmu_object_info_t));
1168 add_disk(zv->zv_disk);
1175 * Create a block device minor node and setup the linkage between it
1176 * and the specified volume. Once this function returns the block
1177 * device is live and ready for use.
1180 zvol_create_minor(const char *name)
1184 mutex_enter(&zvol_state_lock);
1185 error = __zvol_create_minor(name);
1186 mutex_exit(&zvol_state_lock);
1192 __zvol_remove_minor(const char *name)
1196 ASSERT(MUTEX_HELD(&zvol_state_lock));
1198 zv = zvol_find_by_name(name);
1202 if (zv->zv_open_count > 0)
1212 * Remove a block device minor node for the specified volume.
1215 zvol_remove_minor(const char *name)
1219 mutex_enter(&zvol_state_lock);
1220 error = __zvol_remove_minor(name);
1221 mutex_exit(&zvol_state_lock);
1227 zvol_create_minors_cb(spa_t *spa, uint64_t dsobj,
1228 const char *dsname, void *arg)
1230 if (strchr(dsname, '/') == NULL)
1233 return __zvol_create_minor(dsname);
1237 * Create minors for specified pool, if pool is NULL create minors
1238 * for all available pools.
1241 zvol_create_minors(const char *pool)
1246 mutex_enter(&zvol_state_lock);
1248 error = dmu_objset_find_spa(NULL, pool, zvol_create_minors_cb,
1249 NULL, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1251 mutex_enter(&spa_namespace_lock);
1252 while ((spa = spa_next(spa)) != NULL) {
1253 error = dmu_objset_find_spa(NULL,
1254 spa_name(spa), zvol_create_minors_cb, NULL,
1255 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1259 mutex_exit(&spa_namespace_lock);
1261 mutex_exit(&zvol_state_lock);
1267 * Remove minors for specified pool, if pool is NULL remove all minors.
1270 zvol_remove_minors(const char *pool)
1272 zvol_state_t *zv, *zv_next;
1275 str = kmem_zalloc(DISK_NAME_LEN, KM_SLEEP);
1277 (void) strncpy(str, pool, strlen(pool));
1278 (void) strcat(str, "/");
1281 mutex_enter(&zvol_state_lock);
1282 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1283 zv_next = list_next(&zvol_state_list, zv);
1285 if (pool == NULL || !strncmp(str, zv->zv_name, strlen(str))) {
1290 mutex_exit(&zvol_state_lock);
1291 kmem_free(str, DISK_NAME_LEN);
1300 zvol_threads = num_online_cpus();
1302 zvol_taskq = taskq_create(ZVOL_DRIVER, zvol_threads, maxclsyspri,
1303 zvol_threads, INT_MAX, TASKQ_PREPOPULATE);
1304 if (zvol_taskq == NULL) {
1305 printk(KERN_INFO "ZFS: taskq_create() failed\n");
1309 error = register_blkdev(zvol_major, ZVOL_DRIVER);
1311 printk(KERN_INFO "ZFS: register_blkdev() failed %d\n", error);
1312 taskq_destroy(zvol_taskq);
1316 blk_register_region(MKDEV(zvol_major, 0), 1UL << MINORBITS,
1317 THIS_MODULE, zvol_probe, NULL, NULL);
1319 mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1320 list_create(&zvol_state_list, sizeof (zvol_state_t),
1321 offsetof(zvol_state_t, zv_next));
1323 (void) zvol_create_minors(NULL);
1331 zvol_remove_minors(NULL);
1332 blk_unregister_region(MKDEV(zvol_major, 0), 1UL << MINORBITS);
1333 unregister_blkdev(zvol_major, ZVOL_DRIVER);
1334 taskq_destroy(zvol_taskq);
1335 mutex_destroy(&zvol_state_lock);
1336 list_destroy(&zvol_state_list);
1339 module_param(zvol_major, uint, 0);
1340 MODULE_PARM_DESC(zvol_major, "Major number for zvol device");
1342 module_param(zvol_threads, uint, 0);
1343 MODULE_PARM_DESC(zvol_threads, "Number of threads for zvol device");