Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Jana Saout | bf14299 | 2014-06-24 14:27:04 -0400 | [diff] [blame] | 2 | * Copyright (C) 2003 Jana Saout <jana@saout.de> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 28 | ti->num_discard_bios = 1; |
Mike Snitzer | f8facb6 | 2010-08-12 04:14:12 +0100 | [diff] [blame] | 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 | */ |
Mikulas Patocka | 7de3ee5 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 36 | static int zero_map(struct dm_target *ti, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | { |
Christoph Hellwig | 7024628 | 2016-07-19 11:28:41 +0200 | [diff] [blame] | 38 | switch (bio_op(bio)) { |
| 39 | case REQ_OP_READ: |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 40 | if (bio->bi_opf & REQ_RAHEAD) { |
Christoph Hellwig | 7024628 | 2016-07-19 11:28:41 +0200 | [diff] [blame] | 41 | /* readahead of null bytes only wastes buffer cache */ |
| 42 | return -EIO; |
| 43 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | zero_fill_bio(bio); |
| 45 | break; |
Christoph Hellwig | 7024628 | 2016-07-19 11:28:41 +0200 | [diff] [blame] | 46 | case REQ_OP_WRITE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | /* writes get silently dropped */ |
| 48 | break; |
Christoph Hellwig | 7024628 | 2016-07-19 11:28:41 +0200 | [diff] [blame] | 49 | default: |
| 50 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 53 | bio_endio(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | /* accepted bio, don't make new request */ |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 56 | return DM_MAPIO_SUBMITTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static struct target_type zero_target = { |
| 60 | .name = "zero", |
Mikulas Patocka | 7de3ee5 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 61 | .version = {1, 1, 0}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | .module = THIS_MODULE, |
| 63 | .ctr = zero_ctr, |
| 64 | .map = zero_map, |
| 65 | }; |
| 66 | |
Alasdair G Kergon | 5e198d9 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 67 | static int __init dm_zero_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
| 69 | int r = dm_register_target(&zero_target); |
| 70 | |
| 71 | if (r < 0) |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 72 | DMERR("register failed %d", r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | return r; |
| 75 | } |
| 76 | |
Alasdair G Kergon | 5e198d9 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 77 | static void __exit dm_zero_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { |
Mikulas Patocka | 10d3bd0 | 2009-01-06 03:04:58 +0000 | [diff] [blame] | 79 | dm_unregister_target(&zero_target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | module_init(dm_zero_init) |
| 83 | module_exit(dm_zero_exit) |
| 84 | |
Jana Saout | bf14299 | 2014-06-24 14:27:04 -0400 | [diff] [blame] | 85 | MODULE_AUTHOR("Jana Saout <jana@saout.de>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | MODULE_DESCRIPTION(DM_NAME " dummy target returning zeros"); |
| 87 | MODULE_LICENSE("GPL"); |