* needs to be run before opening and using a device.
*/
+#include <sys/dbuf.h>
#include <sys/dmu_traverse.h>
#include <sys/dsl_dataset.h>
#include <sys/dsl_prop.h>
*/
typedef struct zvol_state {
char zv_name[MAXNAMELEN]; /* name */
- uint64_t zv_volsize; /* advertised space */
- uint64_t zv_volblocksize;/* volume block size */
+ uint64_t zv_volsize; /* advertised space */
+ uint64_t zv_volblocksize; /* volume block size */
objset_t *zv_objset; /* objset handle */
uint32_t zv_flags; /* ZVOL_* flags */
uint32_t zv_open_count; /* open counts */
*minor = 0;
ASSERT(MUTEX_HELD(&zvol_state_lock));
for (zv = list_head(&zvol_state_list); zv != NULL;
- zv = list_next(&zvol_state_list, zv), *minor += ZVOL_MINORS) {
+ zv = list_next(&zvol_state_list, zv), *minor += ZVOL_MINORS) {
if (MINOR(zv->zv_dev) != MINOR(*minor))
break;
}
/* All minors are in use */
if (*minor >= (1 << MINORBITS))
- return ENXIO;
+ return (SET_ERROR(ENXIO));
- return 0;
+ return (0);
}
/*
ASSERT(MUTEX_HELD(&zvol_state_lock));
for (zv = list_head(&zvol_state_list); zv != NULL;
- zv = list_next(&zvol_state_list, zv)) {
+ zv = list_next(&zvol_state_list, zv)) {
if (zv->zv_dev == dev)
- return zv;
+ return (zv);
}
- return NULL;
+ return (NULL);
}
/*
ASSERT(MUTEX_HELD(&zvol_state_lock));
for (zv = list_head(&zvol_state_list); zv != NULL;
- zv = list_next(&zvol_state_list, zv)) {
- if (!strncmp(zv->zv_name, name, MAXNAMELEN))
- return zv;
+ zv = list_next(&zvol_state_list, zv)) {
+ if (strncmp(zv->zv_name, name, MAXNAMELEN) == 0)
+ return (zv);
}
- return NULL;
+ return (NULL);
}
bdput(bdev);
if (major == zvol_major)
- return (B_TRUE);
+ return (B_TRUE);
return (B_FALSE);
}
error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
if (error)
- return (error);
+ return (SET_ERROR(error));
dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
- doi = kmem_alloc(sizeof(dmu_object_info_t), KM_SLEEP);
+ doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
error = dmu_object_info(os, ZVOL_OBJ, doi);
if (error == 0) {
doi->doi_data_block_size);
}
- kmem_free(doi, sizeof(dmu_object_info_t));
+ kmem_free(doi, sizeof (dmu_object_info_t));
- return (error);
+ return (SET_ERROR(error));
+}
+
+static void
+zvol_size_changed(zvol_state_t *zv, uint64_t volsize)
+{
+ struct block_device *bdev;
+
+ bdev = bdget_disk(zv->zv_disk, 0);
+ if (bdev == NULL)
+ return;
+/*
+ * 2.6.28 API change
+ * Added check_disk_size_change() helper function.
+ */
+#ifdef HAVE_CHECK_DISK_SIZE_CHANGE
+ set_capacity(zv->zv_disk, volsize >> 9);
+ zv->zv_volsize = volsize;
+ check_disk_size_change(zv->zv_disk, bdev);
+#else
+ zv->zv_volsize = volsize;
+ zv->zv_changed = 1;
+ (void) check_disk_change(bdev);
+#endif /* HAVE_CHECK_DISK_SIZE_CHANGE */
+
+ bdput(bdev);
}
/*
zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
{
if (volsize == 0)
- return (EINVAL);
+ return (SET_ERROR(EINVAL));
if (volsize % blocksize != 0)
- return (EINVAL);
+ return (SET_ERROR(EINVAL));
#ifdef _ILP32
if (volsize - 1 > MAXOFFSET_T)
- return (EOVERFLOW);
+ return (SET_ERROR(EOVERFLOW));
#endif
return (0);
}
* Ensure the zap is flushed then inform the VFS of the capacity change.
*/
static int
-zvol_update_volsize(zvol_state_t *zv, uint64_t volsize, objset_t *os)
+zvol_update_volsize(uint64_t volsize, objset_t *os)
{
- struct block_device *bdev;
dmu_tx_t *tx;
int error;
error = dmu_tx_assign(tx, TXG_WAIT);
if (error) {
dmu_tx_abort(tx);
- return (error);
+ return (SET_ERROR(error));
}
error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
&volsize, tx);
dmu_tx_commit(tx);
- if (error)
- return (error);
+ if (error == 0)
+ error = dmu_free_long_range(os,
+ ZVOL_OBJ, volsize, DMU_OBJECT_END);
- error = dmu_free_long_range(os,
- ZVOL_OBJ, volsize, DMU_OBJECT_END);
- if (error)
- return (error);
+ return (error);
+}
- bdev = bdget_disk(zv->zv_disk, 0);
- if (!bdev)
- return (EIO);
-/*
- * 2.6.28 API change
- * Added check_disk_size_change() helper function.
- */
-#ifdef HAVE_CHECK_DISK_SIZE_CHANGE
- set_capacity(zv->zv_disk, volsize >> 9);
- zv->zv_volsize = volsize;
- check_disk_size_change(zv->zv_disk, bdev);
-#else
- zv->zv_volsize = volsize;
- zv->zv_changed = 1;
- (void) check_disk_change(bdev);
-#endif /* HAVE_CHECK_DISK_SIZE_CHANGE */
+static int
+zvol_update_live_volsize(zvol_state_t *zv, uint64_t volsize)
+{
+ zvol_size_changed(zv, volsize);
- bdput(bdev);
+ /*
+ * We should post a event here describing the expansion. However,
+ * the zfs_ereport_post() interface doesn't nicely support posting
+ * events for zvols, it assumes events relate to vdevs or zios.
+ */
return (0);
}
int
zvol_set_volsize(const char *name, uint64_t volsize)
{
- zvol_state_t *zv;
- dmu_object_info_t *doi;
+ zvol_state_t *zv = NULL;
objset_t *os = NULL;
- uint64_t readonly;
int error;
+ dmu_object_info_t *doi;
+ uint64_t readonly;
+ boolean_t owned = B_FALSE;
- mutex_enter(&zvol_state_lock);
+ error = dsl_prop_get_integer(name,
+ zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL);
+ if (error != 0)
+ return (SET_ERROR(error));
+ if (readonly)
+ return (SET_ERROR(EROFS));
+ mutex_enter(&zvol_state_lock);
zv = zvol_find_by_name(name);
- if (zv == NULL) {
- error = ENXIO;
- goto out;
- }
-
- doi = kmem_alloc(sizeof(dmu_object_info_t), KM_SLEEP);
- error = dmu_objset_hold(name, FTAG, &os);
- if (error)
- goto out_doi;
+ if (zv == NULL || zv->zv_objset == NULL) {
+ if ((error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE,
+ FTAG, &os)) != 0) {
+ mutex_exit(&zvol_state_lock);
+ return (SET_ERROR(error));
+ }
+ owned = B_TRUE;
+ if (zv != NULL)
+ zv->zv_objset = os;
+ } else {
+ os = zv->zv_objset;
+ }
- if ((error = dmu_object_info(os, ZVOL_OBJ, doi)) != 0 ||
- (error = zvol_check_volsize(volsize,doi->doi_data_block_size)) != 0)
- goto out_doi;
+ doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
- VERIFY(dsl_prop_get_integer(name, "readonly", &readonly, NULL) == 0);
- if (readonly) {
- error = EROFS;
- goto out_doi;
- }
+ if ((error = dmu_object_info(os, ZVOL_OBJ, doi)) ||
+ (error = zvol_check_volsize(volsize, doi->doi_data_block_size)))
+ goto out;
- if (get_disk_ro(zv->zv_disk) || (zv->zv_flags & ZVOL_RDONLY)) {
- error = EROFS;
- goto out_doi;
- }
+ error = zvol_update_volsize(volsize, os);
+ kmem_free(doi, sizeof (dmu_object_info_t));
- error = zvol_update_volsize(zv, volsize, os);
-out_doi:
- kmem_free(doi, sizeof(dmu_object_info_t));
+ if (error == 0 && zv != NULL)
+ error = zvol_update_live_volsize(zv, volsize);
out:
- if (os)
- dmu_objset_rele(os, FTAG);
-
+ if (owned) {
+ dmu_objset_disown(os, FTAG);
+ if (zv != NULL)
+ zv->zv_objset = NULL;
+ }
mutex_exit(&zvol_state_lock);
-
return (error);
}
if (volblocksize < SPA_MINBLOCKSIZE ||
volblocksize > SPA_MAXBLOCKSIZE ||
!ISP2(volblocksize))
- return (EDOM);
+ return (SET_ERROR(EDOM));
return (0);
}
zv = zvol_find_by_name(name);
if (zv == NULL) {
- error = ENXIO;
+ error = SET_ERROR(ENXIO);
goto out;
}
- if (get_disk_ro(zv->zv_disk) || (zv->zv_flags & ZVOL_RDONLY)) {
- error = EROFS;
+ if (zv->zv_flags & ZVOL_RDONLY) {
+ error = SET_ERROR(EROFS);
goto out;
}
error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
volblocksize, 0, tx);
if (error == ENOTSUP)
- error = EBUSY;
+ error = SET_ERROR(EBUSY);
dmu_tx_commit(tx);
if (error == 0)
zv->zv_volblocksize = volblocksize;
out:
mutex_exit(&zvol_state_lock);
- return (error);
+ return (SET_ERROR(error));
}
/*
dmu_tx_commit(tx);
}
- return (error);
+ return (SET_ERROR(error));
}
static int
zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
{
- return (ENOTSUP);
+ return (SET_ERROR(ENOTSUP));
}
/*
* Callback vectors for replaying records.
* Only TX_WRITE is needed for zvol.
*/
-zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
- (zil_replay_func_t *)zvol_replay_err, /* no such transaction type */
- (zil_replay_func_t *)zvol_replay_err, /* TX_CREATE */
- (zil_replay_func_t *)zvol_replay_err, /* TX_MKDIR */
- (zil_replay_func_t *)zvol_replay_err, /* TX_MKXATTR */
- (zil_replay_func_t *)zvol_replay_err, /* TX_SYMLINK */
- (zil_replay_func_t *)zvol_replay_err, /* TX_REMOVE */
- (zil_replay_func_t *)zvol_replay_err, /* TX_RMDIR */
- (zil_replay_func_t *)zvol_replay_err, /* TX_LINK */
- (zil_replay_func_t *)zvol_replay_err, /* TX_RENAME */
- (zil_replay_func_t *)zvol_replay_write, /* TX_WRITE */
- (zil_replay_func_t *)zvol_replay_err, /* TX_TRUNCATE */
- (zil_replay_func_t *)zvol_replay_err, /* TX_SETATTR */
- (zil_replay_func_t *)zvol_replay_err, /* TX_ACL */
+zil_replay_func_t zvol_replay_vector[TX_MAX_TYPE] = {
+ (zil_replay_func_t)zvol_replay_err, /* no such transaction type */
+ (zil_replay_func_t)zvol_replay_err, /* TX_CREATE */
+ (zil_replay_func_t)zvol_replay_err, /* TX_MKDIR */
+ (zil_replay_func_t)zvol_replay_err, /* TX_MKXATTR */
+ (zil_replay_func_t)zvol_replay_err, /* TX_SYMLINK */
+ (zil_replay_func_t)zvol_replay_err, /* TX_REMOVE */
+ (zil_replay_func_t)zvol_replay_err, /* TX_RMDIR */
+ (zil_replay_func_t)zvol_replay_err, /* TX_LINK */
+ (zil_replay_func_t)zvol_replay_err, /* TX_RENAME */
+ (zil_replay_func_t)zvol_replay_write, /* TX_WRITE */
+ (zil_replay_func_t)zvol_replay_err, /* TX_TRUNCATE */
+ (zil_replay_func_t)zvol_replay_err, /* TX_SETATTR */
+ (zil_replay_func_t)zvol_replay_err, /* TX_ACL */
};
/*
ssize_t zvol_immediate_write_sz = 32768;
static void
-zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx,
- uint64_t offset, uint64_t size, int sync)
+zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, uint64_t offset,
+ uint64_t size, int sync)
{
uint32_t blocksize = zv->zv_volblocksize;
zilog_t *zilog = zv->zv_zilog;
struct request *req = (struct request *)arg;
struct request_queue *q = req->q;
zvol_state_t *zv = q->queuedata;
+ fstrans_cookie_t cookie = spl_fstrans_mark();
uint64_t offset = blk_rq_pos(req) << 9;
uint64_t size = blk_rq_bytes(req);
int error = 0;
dmu_tx_t *tx;
rl_t *rl;
- /*
- * Annotate this call path with a flag that indicates that it is
- * unsafe to use KM_SLEEP during memory allocations due to the
- * potential for a deadlock. KM_PUSHPAGE should be used instead.
- */
- ASSERT(!(current->flags & PF_NOFS));
- current->flags |= PF_NOFS;
-
if (req->cmd_flags & VDEV_REQ_FLUSH)
zil_commit(zv->zv_zilog, ZVOL_OBJ);
* Some requests are just for flush and nothing else.
*/
if (size == 0) {
- blk_end_request(req, 0, size);
+ error = 0;
goto out;
}
if (error) {
dmu_tx_abort(tx);
zfs_range_unlock(rl);
- blk_end_request(req, -error, size);
goto out;
}
zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)
zil_commit(zv->zv_zilog, ZVOL_OBJ);
- blk_end_request(req, -error, size);
out:
- current->flags &= ~PF_NOFS;
+ blk_end_request(req, -error, size);
+ spl_fstrans_unmark(cookie);
}
#ifdef HAVE_BLK_QUEUE_DISCARD
struct request *req = (struct request *)arg;
struct request_queue *q = req->q;
zvol_state_t *zv = q->queuedata;
+ fstrans_cookie_t cookie = spl_fstrans_mark();
uint64_t start = blk_rq_pos(req) << 9;
uint64_t end = start + blk_rq_bytes(req);
int error;
rl_t *rl;
- /*
- * Annotate this call path with a flag that indicates that it is
- * unsafe to use KM_SLEEP during memory allocations due to the
- * potential for a deadlock. KM_PUSHPAGE should be used instead.
- */
- ASSERT(!(current->flags & PF_NOFS));
- current->flags |= PF_NOFS;
-
if (end > zv->zv_volsize) {
- blk_end_request(req, -EIO, blk_rq_bytes(req));
+ error = EIO;
goto out;
}
end = P2ALIGN(end, zv->zv_volblocksize);
if (start >= end) {
- blk_end_request(req, 0, blk_rq_bytes(req));
+ error = 0;
goto out;
}
rl = zfs_range_lock(&zv->zv_znode, start, end - start, RL_WRITER);
- error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, start, end - start);
+ error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, start, end-start);
/*
* TODO: maybe we should add the operation to the log.
*/
zfs_range_unlock(rl);
-
- blk_end_request(req, -error, blk_rq_bytes(req));
out:
- current->flags &= ~PF_NOFS;
+ blk_end_request(req, -error, blk_rq_bytes(req));
+ spl_fstrans_unmark(cookie);
}
#endif /* HAVE_BLK_QUEUE_DISCARD */
struct request *req = (struct request *)arg;
struct request_queue *q = req->q;
zvol_state_t *zv = q->queuedata;
+ fstrans_cookie_t cookie = spl_fstrans_mark();
uint64_t offset = blk_rq_pos(req) << 9;
uint64_t size = blk_rq_bytes(req);
int error;
rl_t *rl;
if (size == 0) {
- blk_end_request(req, 0, size);
- return;
+ error = 0;
+ goto out;
}
rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
/* convert checksum errors into IO errors */
if (error == ECKSUM)
- error = EIO;
+ error = SET_ERROR(EIO);
+out:
blk_end_request(req, -error, size);
+ spl_fstrans_unmark(cookie);
}
/*
if (size != 0 && blk_rq_pos(req) + blk_rq_sectors(req) >
get_capacity(zv->zv_disk)) {
printk(KERN_INFO
- "%s: bad access: block=%llu, count=%lu\n",
- req->rq_disk->disk_name,
- (long long unsigned)blk_rq_pos(req),
- (long unsigned)blk_rq_sectors(req));
+ "%s: bad access: block=%llu, count=%lu\n",
+ req->rq_disk->disk_name,
+ (long long unsigned)blk_rq_pos(req),
+ (long unsigned)blk_rq_sectors(req));
__blk_end_request(req, -EIO, size);
continue;
}
if (!blk_fs_request(req)) {
printk(KERN_INFO "%s: non-fs cmd\n",
- req->rq_disk->disk_name);
+ req->rq_disk->disk_name);
__blk_end_request(req, -EIO, size);
continue;
}
zvol_dispatch(zvol_read, req);
break;
case WRITE:
- if (unlikely(get_disk_ro(zv->zv_disk)) ||
- unlikely(zv->zv_flags & ZVOL_RDONLY)) {
+ if (unlikely(zv->zv_flags & ZVOL_RDONLY)) {
__blk_end_request(req, -EROFS, size);
break;
}
break;
default:
printk(KERN_INFO "%s: unknown cmd: %d\n",
- req->rq_disk->disk_name, (int)rq_data_dir(req));
+ req->rq_disk->disk_name, (int)rq_data_dir(req));
__blk_end_request(req, -EIO, size);
break;
}
{
zvol_state_t *zv = arg;
objset_t *os = zv->zv_objset;
+ uint64_t object = ZVOL_OBJ;
uint64_t offset = lr->lr_offset;
uint64_t size = lr->lr_length;
+ blkptr_t *bp = &lr->lr_blkptr;
dmu_buf_t *db;
zgd_t *zgd;
int error;
ASSERT(zio != NULL);
ASSERT(size != 0);
- zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_PUSHPAGE);
+ zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
zgd->zgd_zilog = zv->zv_zilog;
zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
* we don't have to write the data twice.
*/
if (buf != NULL) { /* immediate write */
- error = dmu_read(os, ZVOL_OBJ, offset, size, buf,
+ error = dmu_read(os, object, offset, size, buf,
DMU_READ_NO_PREFETCH);
} else {
size = zv->zv_volblocksize;
offset = P2ALIGN_TYPED(offset, size, uint64_t);
- error = dmu_buf_hold(os, ZVOL_OBJ, offset, zgd, &db,
+ error = dmu_buf_hold(os, object, offset, zgd, &db,
DMU_READ_NO_PREFETCH);
if (error == 0) {
+ blkptr_t *obp = dmu_buf_get_blkptr(db);
+ if (obp) {
+ ASSERT(BP_IS_HOLE(bp));
+ *bp = *obp;
+ }
+
zgd->zgd_db = db;
zgd->zgd_bp = &lr->lr_blkptr;
zvol_get_done(zgd, error);
- return (error);
+ return (SET_ERROR(error));
}
/*
ASSERT(MUTEX_HELD(&zvol_state_lock));
ASSERT3U(MINOR(zv_insert->zv_dev) & ZVOL_MINOR_MASK, ==, 0);
for (zv = list_head(&zvol_state_list); zv != NULL;
- zv = list_next(&zvol_state_list, zv)) {
+ zv = list_next(&zvol_state_list, zv)) {
if (MINOR(zv->zv_dev) > MINOR(zv_insert->zv_dev))
break;
}
{
objset_t *os;
uint64_t volsize;
+ int locked = 0;
int error;
uint64_t ro;
+ /*
+ * In all other cases the spa_namespace_lock is taken before the
+ * bdev->bd_mutex lock. But in this case the Linux __blkdev_get()
+ * function calls fops->open() with the bdev->bd_mutex lock held.
+ *
+ * To avoid a potential lock inversion deadlock we preemptively
+ * try to take the spa_namespace_lock(). Normally it will not
+ * be contended and this is safe because spa_open_common() handles
+ * the case where the caller already holds the spa_namespace_lock.
+ *
+ * When it is contended we risk a lock inversion if we were to
+ * block waiting for the lock. Luckily, the __blkdev_get()
+ * function allows us to return -ERESTARTSYS which will result in
+ * bdev->bd_mutex being dropped, reacquired, and fops->open() being
+ * called again. This process can be repeated safely until both
+ * locks are acquired.
+ */
+ if (!mutex_owned(&spa_namespace_lock)) {
+ locked = mutex_tryenter(&spa_namespace_lock);
+ if (!locked)
+ return (-SET_ERROR(ERESTARTSYS));
+ }
+
+ error = dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL);
+ if (error)
+ goto out_mutex;
+
/* lie and say we're read-only */
error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, 1, zvol_tag, &os);
if (error)
- return (-error);
+ goto out_mutex;
error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
if (error) {
- dmu_objset_disown(os, zvol_tag);
- return (-error);
+ dmu_objset_disown(os, zvol_tag);
+ goto out_mutex;
}
zv->zv_objset = os;
error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
if (error) {
- dmu_objset_disown(os, zvol_tag);
- return (-error);
+ dmu_objset_disown(os, zvol_tag);
+ goto out_mutex;
}
set_capacity(zv->zv_disk, volsize >> 9);
zv->zv_volsize = volsize;
zv->zv_zilog = zil_open(os, zvol_get_data);
- VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL) == 0);
- if (ro || dmu_objset_is_snapshot(os)) {
- set_disk_ro(zv->zv_disk, 1);
- zv->zv_flags |= ZVOL_RDONLY;
+ if (ro || dmu_objset_is_snapshot(os) ||
+ !spa_writeable(dmu_objset_spa(os))) {
+ set_disk_ro(zv->zv_disk, 1);
+ zv->zv_flags |= ZVOL_RDONLY;
} else {
- set_disk_ro(zv->zv_disk, 0);
- zv->zv_flags &= ~ZVOL_RDONLY;
+ set_disk_ro(zv->zv_disk, 0);
+ zv->zv_flags &= ~ZVOL_RDONLY;
}
- return (-error);
+out_mutex:
+ if (locked)
+ mutex_exit(&spa_namespace_lock);
+
+ return (SET_ERROR(-error));
}
static void
goto out_mutex;
}
- if ((flag & FMODE_WRITE) &&
- (get_disk_ro(zv->zv_disk) || (zv->zv_flags & ZVOL_RDONLY))) {
+ if ((flag & FMODE_WRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
error = -EROFS;
goto out_open_count;
}
check_disk_change(bdev);
- return (error);
+ return (SET_ERROR(error));
}
+#ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
+static void
+#else
static int
+#endif
zvol_release(struct gendisk *disk, fmode_t mode)
{
zvol_state_t *zv = disk->private_data;
drop_mutex = 1;
}
- ASSERT3P(zv, !=, NULL);
- ASSERT3U(zv->zv_open_count, >, 0);
- zv->zv_open_count--;
- if (zv->zv_open_count == 0)
- zvol_last_close(zv);
+ if (zv->zv_open_count > 0) {
+ zv->zv_open_count--;
+ if (zv->zv_open_count == 0)
+ zvol_last_close(zv);
+ }
if (drop_mutex)
mutex_exit(&zvol_state_lock);
+#ifndef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
return (0);
+#endif
}
static int
zvol_ioctl(struct block_device *bdev, fmode_t mode,
- unsigned int cmd, unsigned long arg)
+ unsigned int cmd, unsigned long arg)
{
zvol_state_t *zv = bdev->bd_disk->private_data;
int error = 0;
if (zv == NULL)
- return (-ENXIO);
+ return (SET_ERROR(-ENXIO));
switch (cmd) {
case BLKFLSBUF:
}
- return (error);
+ return (SET_ERROR(error));
}
#ifdef CONFIG_COMPAT
static int
zvol_compat_ioctl(struct block_device *bdev, fmode_t mode,
- unsigned cmd, unsigned long arg)
+ unsigned cmd, unsigned long arg)
{
- return zvol_ioctl(bdev, mode, cmd, arg);
+ return (zvol_ioctl(bdev, mode, cmd, arg));
}
#else
-#define zvol_compat_ioctl NULL
+#define zvol_compat_ioctl NULL
#endif
static int zvol_media_changed(struct gendisk *disk)
{
zvol_state_t *zv = disk->private_data;
- return zv->zv_changed;
+ return (zv->zv_changed);
}
static int zvol_revalidate_disk(struct gendisk *disk)
zv->zv_changed = 0;
set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
- return 0;
+ return (0);
}
/*
geo->start = 0;
geo->cylinders = sectors / (geo->heads * geo->sectors);
- return 0;
+ return (0);
}
static struct kobject *
kobj = zv ? get_disk(zv->zv_disk) : NULL;
mutex_exit(&zvol_state_lock);
- return kobj;
+ return (kobj);
}
#ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS
static struct block_device_operations zvol_ops = {
- .open = zvol_open,
- .release = zvol_release,
- .ioctl = zvol_ioctl,
- .compat_ioctl = zvol_compat_ioctl,
- .media_changed = zvol_media_changed,
- .revalidate_disk = zvol_revalidate_disk,
- .getgeo = zvol_getgeo,
- .owner = THIS_MODULE,
+ .open = zvol_open,
+ .release = zvol_release,
+ .ioctl = zvol_ioctl,
+ .compat_ioctl = zvol_compat_ioctl,
+ .media_changed = zvol_media_changed,
+ .revalidate_disk = zvol_revalidate_disk,
+ .getgeo = zvol_getgeo,
+ .owner = THIS_MODULE,
};
#else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
static int
zvol_open_by_inode(struct inode *inode, struct file *file)
{
- return zvol_open(inode->i_bdev, file->f_mode);
+ return (zvol_open(inode->i_bdev, file->f_mode));
}
static int
zvol_release_by_inode(struct inode *inode, struct file *file)
{
- return zvol_release(inode->i_bdev->bd_disk, file->f_mode);
+ return (zvol_release(inode->i_bdev->bd_disk, file->f_mode));
}
static int
zvol_ioctl_by_inode(struct inode *inode, struct file *file,
- unsigned int cmd, unsigned long arg)
+ unsigned int cmd, unsigned long arg)
{
if (file == NULL || inode == NULL)
- return -EINVAL;
- return zvol_ioctl(inode->i_bdev, file->f_mode, cmd, arg);
+ return (SET_ERROR(-EINVAL));
+
+ return (zvol_ioctl(inode->i_bdev, file->f_mode, cmd, arg));
}
-# ifdef CONFIG_COMPAT
+#ifdef CONFIG_COMPAT
static long
zvol_compat_ioctl_by_inode(struct file *file,
- unsigned int cmd, unsigned long arg)
+ unsigned int cmd, unsigned long arg)
{
if (file == NULL)
- return -EINVAL;
- return zvol_compat_ioctl(file->f_dentry->d_inode->i_bdev,
- file->f_mode, cmd, arg);
+ return (SET_ERROR(-EINVAL));
+
+ return (zvol_compat_ioctl(file->f_dentry->d_inode->i_bdev,
+ file->f_mode, cmd, arg));
}
-# else
-# define zvol_compat_ioctl_by_inode NULL
-# endif
+#else
+#define zvol_compat_ioctl_by_inode NULL
+#endif
static struct block_device_operations zvol_ops = {
- .open = zvol_open_by_inode,
- .release = zvol_release_by_inode,
- .ioctl = zvol_ioctl_by_inode,
- .compat_ioctl = zvol_compat_ioctl_by_inode,
- .media_changed = zvol_media_changed,
- .revalidate_disk = zvol_revalidate_disk,
- .getgeo = zvol_getgeo,
- .owner = THIS_MODULE,
+ .open = zvol_open_by_inode,
+ .release = zvol_release_by_inode,
+ .ioctl = zvol_ioctl_by_inode,
+ .compat_ioctl = zvol_compat_ioctl_by_inode,
+ .media_changed = zvol_media_changed,
+ .revalidate_disk = zvol_revalidate_disk,
+ .getgeo = zvol_getgeo,
+ .owner = THIS_MODULE,
};
#endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
int error = 0;
zv = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
- if (zv == NULL)
- goto out;
+
+ spin_lock_init(&zv->zv_lock);
+ list_link_init(&zv->zv_next);
zv->zv_queue = blk_init_queue(zvol_request, &zv->zv_lock);
if (zv->zv_queue == NULL)
sizeof (rl_t), offsetof(rl_t, r_node));
zv->zv_znode.z_is_zvol = TRUE;
- spin_lock_init(&zv->zv_lock);
- list_link_init(&zv->zv_next);
-
zv->zv_disk->major = zvol_major;
zv->zv_disk->first_minor = (dev & MINORMASK);
zv->zv_disk->fops = &zvol_ops;
snprintf(zv->zv_disk->disk_name, DISK_NAME_LEN, "%s%d",
ZVOL_DEV_NAME, (dev & MINORMASK));
- return zv;
+ return (zv);
out_queue:
blk_cleanup_queue(zv->zv_queue);
out_kmem:
kmem_free(zv, sizeof (zvol_state_t));
-out:
- return NULL;
+
+ return (NULL);
}
/*
}
static int
-__zvol_create_minor(const char *name)
+__zvol_snapdev_hidden(const char *name)
+{
+ uint64_t snapdev;
+ char *parent;
+ char *atp;
+ int error = 0;
+
+ parent = kmem_alloc(MAXPATHLEN, KM_SLEEP);
+ (void) strlcpy(parent, name, MAXPATHLEN);
+
+ if ((atp = strrchr(parent, '@')) != NULL) {
+ *atp = '\0';
+ error = dsl_prop_get_integer(parent, "snapdev", &snapdev, NULL);
+ if ((error == 0) && (snapdev == ZFS_SNAPDEV_HIDDEN))
+ error = SET_ERROR(ENODEV);
+ }
+
+ kmem_free(parent, MAXPATHLEN);
+
+ return (SET_ERROR(error));
+}
+
+static int
+__zvol_create_minor(const char *name, boolean_t ignore_snapdev)
{
zvol_state_t *zv;
objset_t *os;
zv = zvol_find_by_name(name);
if (zv) {
- error = EEXIST;
+ error = SET_ERROR(EEXIST);
goto out;
}
- doi = kmem_alloc(sizeof(dmu_object_info_t), KM_SLEEP);
+ if (ignore_snapdev == B_FALSE) {
+ error = __zvol_snapdev_hidden(name);
+ if (error)
+ goto out;
+ }
+
+ doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
if (error)
zv = zvol_alloc(MKDEV(zvol_major, minor), name);
if (zv == NULL) {
- error = EAGAIN;
+ error = SET_ERROR(EAGAIN);
goto out_dmu_objset_disown;
}
set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
- blk_queue_max_hw_sectors(zv->zv_queue, UINT_MAX);
+ blk_queue_max_hw_sectors(zv->zv_queue, DMU_MAX_ACCESS / 512);
blk_queue_max_segments(zv->zv_queue, UINT16_MAX);
blk_queue_max_segment_size(zv->zv_queue, UINT_MAX);
blk_queue_physical_block_size(zv->zv_queue, zv->zv_volblocksize);
queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zv->zv_queue);
#endif
- if (zil_replay_disable)
- zil_destroy(dmu_objset_zil(os), B_FALSE);
- else
- zil_replay(os, zv, zvol_replay_vector);
+ if (spa_writeable(dmu_objset_spa(os))) {
+ if (zil_replay_disable)
+ zil_destroy(dmu_objset_zil(os), B_FALSE);
+ else
+ zil_replay(os, zv, zvol_replay_vector);
+ }
zv->zv_objset = NULL;
out_dmu_objset_disown:
dmu_objset_disown(os, zvol_tag);
out_doi:
- kmem_free(doi, sizeof(dmu_object_info_t));
+ kmem_free(doi, sizeof (dmu_object_info_t));
out:
if (error == 0) {
add_disk(zv->zv_disk);
}
- return (error);
+ return (SET_ERROR(error));
}
/*
int error;
mutex_enter(&zvol_state_lock);
- error = __zvol_create_minor(name);
+ error = __zvol_create_minor(name, B_FALSE);
mutex_exit(&zvol_state_lock);
- return (error);
+ return (SET_ERROR(error));
}
static int
zv = zvol_find_by_name(name);
if (zv == NULL)
- return (ENXIO);
+ return (SET_ERROR(ENXIO));
if (zv->zv_open_count > 0)
- return (EBUSY);
+ return (SET_ERROR(EBUSY));
zvol_remove(zv);
zvol_free(zv);
error = __zvol_remove_minor(name);
mutex_exit(&zvol_state_lock);
- return (error);
+ return (SET_ERROR(error));
+}
+
+/*
+ * Rename a block device minor mode for the specified volume.
+ */
+static void
+__zvol_rename_minor(zvol_state_t *zv, const char *newname)
+{
+ int readonly = get_disk_ro(zv->zv_disk);
+
+ ASSERT(MUTEX_HELD(&zvol_state_lock));
+
+ strlcpy(zv->zv_name, newname, sizeof (zv->zv_name));
+
+ /*
+ * The block device's read-only state is briefly changed causing
+ * a KOBJ_CHANGE uevent to be issued. This ensures udev detects
+ * the name change and fixes the symlinks. This does not change
+ * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
+ * changes. This would normally be done using kobject_uevent() but
+ * that is a GPL-only symbol which is why we need this workaround.
+ */
+ set_disk_ro(zv->zv_disk, !readonly);
+ set_disk_ro(zv->zv_disk, readonly);
}
static int
-zvol_create_minors_cb(spa_t *spa, uint64_t dsobj,
- const char *dsname, void *arg)
+zvol_create_minors_cb(const char *dsname, void *arg)
{
- if (strchr(dsname, '/') == NULL)
- return 0;
+ (void) zvol_create_minor(dsname);
- (void) __zvol_create_minor(dsname);
return (0);
}
/*
- * Create minors for specified pool, if pool is NULL create minors
- * for all available pools.
+ * Create minors for specified dataset including children and snapshots.
*/
int
-zvol_create_minors(const char *pool)
+zvol_create_minors(const char *name)
{
- spa_t *spa = NULL;
int error = 0;
+ if (!zvol_inhibit_dev)
+ error = dmu_objset_find((char *)name, zvol_create_minors_cb,
+ NULL, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
+
+ return (SET_ERROR(error));
+}
+
+/*
+ * Remove minors for specified dataset including children and snapshots.
+ */
+void
+zvol_remove_minors(const char *name)
+{
+ zvol_state_t *zv, *zv_next;
+ int namelen = ((name) ? strlen(name) : 0);
+
if (zvol_inhibit_dev)
- return (0);
+ return;
mutex_enter(&zvol_state_lock);
- if (pool) {
- error = dmu_objset_find_spa(NULL, pool, zvol_create_minors_cb,
- NULL, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
- } else {
- mutex_enter(&spa_namespace_lock);
- while ((spa = spa_next(spa)) != NULL) {
- error = dmu_objset_find_spa(NULL,
- spa_name(spa), zvol_create_minors_cb, NULL,
- DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
- if (error)
- break;
+
+ for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
+ zv_next = list_next(&zvol_state_list, zv);
+
+ if (name == NULL || strcmp(zv->zv_name, name) == 0 ||
+ (strncmp(zv->zv_name, name, namelen) == 0 &&
+ zv->zv_name[namelen] == '/')) {
+ zvol_remove(zv);
+ zvol_free(zv);
}
- mutex_exit(&spa_namespace_lock);
}
- mutex_exit(&zvol_state_lock);
- return error;
+ mutex_exit(&zvol_state_lock);
}
/*
- * Remove minors for specified pool, if pool is NULL remove all minors.
+ * Rename minors for specified dataset including children and snapshots.
*/
void
-zvol_remove_minors(const char *pool)
+zvol_rename_minors(const char *oldname, const char *newname)
{
zvol_state_t *zv, *zv_next;
- char *str;
+ int oldnamelen, newnamelen;
+ char *name;
if (zvol_inhibit_dev)
return;
- str = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
- if (pool) {
- (void) strncpy(str, pool, strlen(pool));
- (void) strcat(str, "/");
- }
+ oldnamelen = strlen(oldname);
+ newnamelen = strlen(newname);
+ name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
mutex_enter(&zvol_state_lock);
+
for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
zv_next = list_next(&zvol_state_list, zv);
- if (pool == NULL || !strncmp(str, zv->zv_name, strlen(str))) {
- zvol_remove(zv);
- zvol_free(zv);
+ if (strcmp(zv->zv_name, oldname) == 0) {
+ __zvol_rename_minor(zv, newname);
+ } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 &&
+ (zv->zv_name[oldnamelen] == '/' ||
+ zv->zv_name[oldnamelen] == '@')) {
+ snprintf(name, MAXNAMELEN, "%s%c%s", newname,
+ zv->zv_name[oldnamelen],
+ zv->zv_name + oldnamelen + 1);
+ __zvol_rename_minor(zv, name);
}
}
+
mutex_exit(&zvol_state_lock);
- kmem_free(str, MAXNAMELEN);
+
+ kmem_free(name, MAXNAMELEN);
+}
+
+static int
+snapdev_snapshot_changed_cb(const char *dsname, void *arg) {
+ uint64_t snapdev = *(uint64_t *) arg;
+
+ if (strchr(dsname, '@') == NULL)
+ return (0);
+
+ switch (snapdev) {
+ case ZFS_SNAPDEV_VISIBLE:
+ mutex_enter(&zvol_state_lock);
+ (void) __zvol_create_minor(dsname, B_TRUE);
+ mutex_exit(&zvol_state_lock);
+ break;
+ case ZFS_SNAPDEV_HIDDEN:
+ (void) zvol_remove_minor(dsname);
+ break;
+ }
+
+ return (0);
+}
+
+int
+zvol_set_snapdev(const char *dsname, uint64_t snapdev) {
+ (void) dmu_objset_find((char *) dsname, snapdev_snapshot_changed_cb,
+ &snapdev, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
+ /* caller should continue to modify snapdev property */
+ return (-1);
}
int
{
int error;
+ list_create(&zvol_state_list, sizeof (zvol_state_t),
+ offsetof(zvol_state_t, zv_next));
+
+ mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
+
zvol_taskq = taskq_create(ZVOL_DRIVER, zvol_threads, maxclsyspri,
- zvol_threads, INT_MAX, TASKQ_PREPOPULATE);
+ zvol_threads, INT_MAX, TASKQ_PREPOPULATE);
if (zvol_taskq == NULL) {
printk(KERN_INFO "ZFS: taskq_create() failed\n");
- return (-ENOMEM);
+ error = -ENOMEM;
+ goto out1;
}
error = register_blkdev(zvol_major, ZVOL_DRIVER);
if (error) {
printk(KERN_INFO "ZFS: register_blkdev() failed %d\n", error);
- taskq_destroy(zvol_taskq);
- return (error);
+ goto out2;
}
blk_register_region(MKDEV(zvol_major, 0), 1UL << MINORBITS,
- THIS_MODULE, zvol_probe, NULL, NULL);
+ THIS_MODULE, zvol_probe, NULL, NULL);
- mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
- list_create(&zvol_state_list, sizeof (zvol_state_t),
- offsetof(zvol_state_t, zv_next));
+ return (0);
- (void) zvol_create_minors(NULL);
+out2:
+ taskq_destroy(zvol_taskq);
+out1:
+ mutex_destroy(&zvol_state_lock);
+ list_destroy(&zvol_state_list);
- return (0);
+ return (SET_ERROR(error));
}
void
MODULE_PARM_DESC(zvol_threads, "Number of threads for zvol device");
module_param(zvol_max_discard_blocks, ulong, 0444);
-MODULE_PARM_DESC(zvol_max_discard_blocks, "Max number of blocks to discard at once");
+MODULE_PARM_DESC(zvol_max_discard_blocks, "Max number of blocks to discard");