Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2003 Christophe Saout <christophe@saout.de> |
| 3 | * |
| 4 | * This file is released under the GPL. |
| 5 | */ |
| 6 | |
Mikulas Patocka | 586e80e | 2008-10-21 17:44:59 +0100 | [diff] [blame] | 7 | #include <linux/device-mapper.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/bio.h> |
| 12 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 13 | #define DM_MSG_PREFIX "zero" |
| 14 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | /* |
| 16 | * Construct a dummy mapping that only returns zeros |
| 17 | */ |
| 18 | static int zero_ctr(struct dm_target *ti, unsigned int argc, char **argv) |
| 19 | { |
| 20 | if (argc != 0) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 21 | ti->error = "No arguments required"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | return -EINVAL; |
| 23 | } |
| 24 | |
Mike Snitzer | f8facb6 | 2010-08-12 04:14:12 +0100 | [diff] [blame^] | 25 | /* |
| 26 | * Silently drop discards, avoiding -EOPNOTSUPP. |
| 27 | */ |
| 28 | ti->num_discard_requests = 1; |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | /* |
| 34 | * Return zeros only on reads |
| 35 | */ |
| 36 | static int zero_map(struct dm_target *ti, struct bio *bio, |
| 37 | union map_info *map_context) |
| 38 | { |
| 39 | switch(bio_rw(bio)) { |
| 40 | case READ: |
| 41 | zero_fill_bio(bio); |
| 42 | break; |
| 43 | case READA: |
| 44 | /* readahead of null bytes only wastes buffer cache */ |
| 45 | return -EIO; |
| 46 | case WRITE: |
| 47 | /* writes get silently dropped */ |
| 48 | break; |
| 49 | } |
| 50 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 51 | bio_endio(bio, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | /* accepted bio, don't make new request */ |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 54 | return DM_MAPIO_SUBMITTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static struct target_type zero_target = { |
| 58 | .name = "zero", |
| 59 | .version = {1, 0, 0}, |
| 60 | .module = THIS_MODULE, |
| 61 | .ctr = zero_ctr, |
| 62 | .map = zero_map, |
| 63 | }; |
| 64 | |
Alasdair G Kergon | 5e198d9 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 65 | static int __init dm_zero_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
| 67 | int r = dm_register_target(&zero_target); |
| 68 | |
| 69 | if (r < 0) |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 70 | DMERR("register failed %d", r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | return r; |
| 73 | } |
| 74 | |
Alasdair G Kergon | 5e198d9 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 75 | static void __exit dm_zero_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
Mikulas Patocka | 10d3bd0 | 2009-01-06 03:04:58 +0000 | [diff] [blame] | 77 | dm_unregister_target(&zero_target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | module_init(dm_zero_init) |
| 81 | module_exit(dm_zero_exit) |
| 82 | |
| 83 | MODULE_AUTHOR("Christophe Saout <christophe@saout.de>"); |
| 84 | MODULE_DESCRIPTION(DM_NAME " dummy target returning zeros"); |
| 85 | MODULE_LICENSE("GPL"); |