Skip to content

Commit

Permalink
Merge git://git.kernel.org/pub/scm/linux/kernel/git/agk/linux-2.6-dm
Browse files Browse the repository at this point in the history
* git://git.kernel.org/pub/scm/linux/kernel/git/agk/linux-2.6-dm:
  dm stripe: implement merge method
  dm mpath: allow table load with no priority groups
  dm mpath: fail message ioctl if specified path is not valid
  dm ioctl: add flag to wipe buffers for secure data
  dm ioctl: prepare for crypt key wiping
  dm crypt: wipe keys string immediately after key is set
  dm: add flakey target
  dm: fix opening log and cow devices for read only tables
  • Loading branch information
Linus Torvalds committed Mar 26, 2011
2 parents 24c6d02 + 2991520 commit 44bbd7a
Show file tree
Hide file tree
Showing 11 changed files with 326 additions and 31 deletions.
17 changes: 17 additions & 0 deletions Documentation/device-mapper/dm-flakey.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
dm-flakey
=========

This target is the same as the linear target except that it returns I/O
errors periodically. It's been found useful in simulating failing
devices for testing purposes.

Starting from the time the table is loaded, the device is available for
<up interval> seconds, then returns errors for <down interval> seconds,
and then this cycle repeats.

Parameters: <dev path> <offset> <up interval> <down interval>
<dev path>: Full pathname to the underlying block-device, or a
"major:minor" device-number.
<offset>: Starting sector within the device.
<up interval>: Number of seconds device is available.
<down interval>: Number of seconds device returns errors.
6 changes: 6 additions & 0 deletions drivers/md/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,10 @@ config DM_UEVENT
---help---
Generate udev events for DM events.

config DM_FLAKEY
tristate "Flakey target (EXPERIMENTAL)"
depends on BLK_DEV_DM && EXPERIMENTAL
---help---
A target that intermittently fails I/O for debugging purposes.

endif # MD
1 change: 1 addition & 0 deletions drivers/md/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ obj-$(CONFIG_BLK_DEV_MD) += md-mod.o
obj-$(CONFIG_BLK_DEV_DM) += dm-mod.o
obj-$(CONFIG_DM_CRYPT) += dm-crypt.o
obj-$(CONFIG_DM_DELAY) += dm-delay.o
obj-$(CONFIG_DM_FLAKEY) += dm-flakey.o
obj-$(CONFIG_DM_MULTIPATH) += dm-multipath.o dm-round-robin.o
obj-$(CONFIG_DM_MULTIPATH_QL) += dm-queue-length.o
obj-$(CONFIG_DM_MULTIPATH_ST) += dm-service-time.o
Expand Down
19 changes: 14 additions & 5 deletions drivers/md/dm-crypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1324,20 +1324,29 @@ static int crypt_setkey_allcpus(struct crypt_config *cc)

static int crypt_set_key(struct crypt_config *cc, char *key)
{
int r = -EINVAL;
int key_string_len = strlen(key);

/* The key size may not be changed. */
if (cc->key_size != (strlen(key) >> 1))
return -EINVAL;
if (cc->key_size != (key_string_len >> 1))
goto out;

/* Hyphen (which gives a key_size of zero) means there is no key. */
if (!cc->key_size && strcmp(key, "-"))
return -EINVAL;
goto out;

if (cc->key_size && crypt_decode_key(cc->key, key, cc->key_size) < 0)
return -EINVAL;
goto out;

set_bit(DM_CRYPT_KEY_VALID, &cc->flags);

return crypt_setkey_allcpus(cc);
r = crypt_setkey_allcpus(cc);

out:
/* Hex key string not needed after here, so wipe it. */
memset(key, '0', key_string_len);

return r;
}

static int crypt_wipe_key(struct crypt_config *cc)
Expand Down
212 changes: 212 additions & 0 deletions drivers/md/dm-flakey.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
/*
* Copyright (C) 2003 Sistina Software (UK) Limited.
* Copyright (C) 2004, 2010 Red Hat, Inc. All rights reserved.
*
* This file is released under the GPL.
*/

#include <linux/device-mapper.h>

#include <linux/module.h>
#include <linux/init.h>
#include <linux/blkdev.h>
#include <linux/bio.h>
#include <linux/slab.h>

#define DM_MSG_PREFIX "flakey"

/*
* Flakey: Used for testing only, simulates intermittent,
* catastrophic device failure.
*/
struct flakey_c {
struct dm_dev *dev;
unsigned long start_time;
sector_t start;
unsigned up_interval;
unsigned down_interval;
};

/*
* Construct a flakey mapping: <dev_path> <offset> <up interval> <down interval>
*/
static int flakey_ctr(struct dm_target *ti, unsigned int argc, char **argv)
{
struct flakey_c *fc;
unsigned long long tmp;

if (argc != 4) {
ti->error = "dm-flakey: Invalid argument count";
return -EINVAL;
}

fc = kmalloc(sizeof(*fc), GFP_KERNEL);
if (!fc) {
ti->error = "dm-flakey: Cannot allocate linear context";
return -ENOMEM;
}
fc->start_time = jiffies;

if (sscanf(argv[1], "%llu", &tmp) != 1) {
ti->error = "dm-flakey: Invalid device sector";
goto bad;
}
fc->start = tmp;

if (sscanf(argv[2], "%u", &fc->up_interval) != 1) {
ti->error = "dm-flakey: Invalid up interval";
goto bad;
}

if (sscanf(argv[3], "%u", &fc->down_interval) != 1) {
ti->error = "dm-flakey: Invalid down interval";
goto bad;
}

if (!(fc->up_interval + fc->down_interval)) {
ti->error = "dm-flakey: Total (up + down) interval is zero";
goto bad;
}

if (fc->up_interval + fc->down_interval < fc->up_interval) {
ti->error = "dm-flakey: Interval overflow";
goto bad;
}

if (dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &fc->dev)) {
ti->error = "dm-flakey: Device lookup failed";
goto bad;
}

ti->num_flush_requests = 1;
ti->private = fc;
return 0;

bad:
kfree(fc);
return -EINVAL;
}

static void flakey_dtr(struct dm_target *ti)
{
struct flakey_c *fc = ti->private;

dm_put_device(ti, fc->dev);
kfree(fc);
}

static sector_t flakey_map_sector(struct dm_target *ti, sector_t bi_sector)
{
struct flakey_c *fc = ti->private;

return fc->start + (bi_sector - ti->begin);
}

static void flakey_map_bio(struct dm_target *ti, struct bio *bio)
{
struct flakey_c *fc = ti->private;

bio->bi_bdev = fc->dev->bdev;
if (bio_sectors(bio))
bio->bi_sector = flakey_map_sector(ti, bio->bi_sector);
}

static int flakey_map(struct dm_target *ti, struct bio *bio,
union map_info *map_context)
{
struct flakey_c *fc = ti->private;
unsigned elapsed;

/* Are we alive ? */
elapsed = (jiffies - fc->start_time) / HZ;
if (elapsed % (fc->up_interval + fc->down_interval) >= fc->up_interval)
return -EIO;

flakey_map_bio(ti, bio);

return DM_MAPIO_REMAPPED;
}

static int flakey_status(struct dm_target *ti, status_type_t type,
char *result, unsigned int maxlen)
{
struct flakey_c *fc = ti->private;

switch (type) {
case STATUSTYPE_INFO:
result[0] = '\0';
break;

case STATUSTYPE_TABLE:
snprintf(result, maxlen, "%s %llu %u %u", fc->dev->name,
(unsigned long long)fc->start, fc->up_interval,
fc->down_interval);
break;
}
return 0;
}

static int flakey_ioctl(struct dm_target *ti, unsigned int cmd, unsigned long arg)
{
struct flakey_c *fc = ti->private;

return __blkdev_driver_ioctl(fc->dev->bdev, fc->dev->mode, cmd, arg);
}

static int flakey_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
struct bio_vec *biovec, int max_size)
{
struct flakey_c *fc = ti->private;
struct request_queue *q = bdev_get_queue(fc->dev->bdev);

if (!q->merge_bvec_fn)
return max_size;

bvm->bi_bdev = fc->dev->bdev;
bvm->bi_sector = flakey_map_sector(ti, bvm->bi_sector);

return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
}

static int flakey_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data)
{
struct flakey_c *fc = ti->private;

return fn(ti, fc->dev, fc->start, ti->len, data);
}

static struct target_type flakey_target = {
.name = "flakey",
.version = {1, 1, 0},
.module = THIS_MODULE,
.ctr = flakey_ctr,
.dtr = flakey_dtr,
.map = flakey_map,
.status = flakey_status,
.ioctl = flakey_ioctl,
.merge = flakey_merge,
.iterate_devices = flakey_iterate_devices,
};

static int __init dm_flakey_init(void)
{
int r = dm_register_target(&flakey_target);

if (r < 0)
DMERR("register failed %d", r);

return r;
}

static void __exit dm_flakey_exit(void)
{
dm_unregister_target(&flakey_target);
}

/* Module hooks */
module_init(dm_flakey_init);
module_exit(dm_flakey_exit);

MODULE_DESCRIPTION(DM_NAME " flakey target");
MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
MODULE_LICENSE("GPL");
46 changes: 31 additions & 15 deletions drivers/md/dm-ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1501,39 +1501,49 @@ static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
return r;
}

static void free_params(struct dm_ioctl *param)
{
vfree(param);
}

static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param)
{
struct dm_ioctl tmp, *dmi;
int secure_data;

if (copy_from_user(&tmp, user, sizeof(tmp) - sizeof(tmp.data)))
return -EFAULT;

if (tmp.data_size < (sizeof(tmp) - sizeof(tmp.data)))
return -EINVAL;

secure_data = tmp.flags & DM_SECURE_DATA_FLAG;

dmi = vmalloc(tmp.data_size);
if (!dmi)
if (!dmi) {
if (secure_data && clear_user(user, tmp.data_size))
return -EFAULT;
return -ENOMEM;

if (copy_from_user(dmi, user, tmp.data_size)) {
vfree(dmi);
return -EFAULT;
}

if (copy_from_user(dmi, user, tmp.data_size))
goto bad;

/* Wipe the user buffer so we do not return it to userspace */
if (secure_data && clear_user(user, tmp.data_size))
goto bad;

*param = dmi;
return 0;

bad:
if (secure_data)
memset(dmi, 0, tmp.data_size);
vfree(dmi);
return -EFAULT;
}

static int validate_params(uint cmd, struct dm_ioctl *param)
{
/* Always clear this flag */
param->flags &= ~DM_BUFFER_FULL_FLAG;
param->flags &= ~DM_UEVENT_GENERATED_FLAG;
param->flags &= ~DM_SECURE_DATA_FLAG;

/* Ignores parameters */
if (cmd == DM_REMOVE_ALL_CMD ||
Expand Down Expand Up @@ -1561,10 +1571,11 @@ static int validate_params(uint cmd, struct dm_ioctl *param)
static int ctl_ioctl(uint command, struct dm_ioctl __user *user)
{
int r = 0;
int wipe_buffer;
unsigned int cmd;
struct dm_ioctl *uninitialized_var(param);
ioctl_fn fn = NULL;
size_t param_size;
size_t input_param_size;

/* only root can play with this */
if (!capable(CAP_SYS_ADMIN))
Expand Down Expand Up @@ -1611,22 +1622,27 @@ static int ctl_ioctl(uint command, struct dm_ioctl __user *user)
if (r)
return r;

input_param_size = param->data_size;
wipe_buffer = param->flags & DM_SECURE_DATA_FLAG;

r = validate_params(cmd, param);
if (r)
goto out;

param_size = param->data_size;
param->data_size = sizeof(*param);
r = fn(param, param_size);
r = fn(param, input_param_size);

/*
* Copy the results back to userland.
*/
if (!r && copy_to_user(user, param, param->data_size))
r = -EFAULT;

out:
free_params(param);
out:
if (wipe_buffer)
memset(param, 0, input_param_size);

vfree(param);
return r;
}

Expand Down
2 changes: 1 addition & 1 deletion drivers/md/dm-log.c
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ static int disk_ctr(struct dm_dirty_log *log, struct dm_target *ti,
return -EINVAL;
}

r = dm_get_device(ti, argv[0], FMODE_READ | FMODE_WRITE, &dev);
r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &dev);
if (r)
return r;

Expand Down
Loading

0 comments on commit 44bbd7a

Please sign in to comment.