blob: b616f11d84735a978b13e2a6392921bafc66b58b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jana Saoutbf142992014-06-24 14:27:04 -04002 * Copyright (C) 2003 Jana Saout <jana@saout.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * This file is released under the GPL.
5 */
6
Mikulas Patocka586e80e2008-10-21 17:44:59 +01007#include <linux/device-mapper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/bio.h>
12
Alasdair G Kergon72d94862006-06-26 00:27:35 -070013#define DM_MSG_PREFIX "zero"
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015/*
16 * Construct a dummy mapping that only returns zeros
17 */
18static int zero_ctr(struct dm_target *ti, unsigned int argc, char **argv)
19{
20 if (argc != 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -070021 ti->error = "No arguments required";
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 return -EINVAL;
23 }
24
Mike Snitzerf8facb62010-08-12 04:14:12 +010025 /*
26 * Silently drop discards, avoiding -EOPNOTSUPP.
27 */
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +000028 ti->num_discard_bios = 1;
Mike Snitzerf8facb62010-08-12 04:14:12 +010029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 return 0;
31}
32
33/*
34 * Return zeros only on reads
35 */
Mikulas Patocka7de3ee52012-12-21 20:23:41 +000036static int zero_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Christoph Hellwig70246282016-07-19 11:28:41 +020038 switch (bio_op(bio)) {
39 case REQ_OP_READ:
Jens Axboe1eff9d32016-08-05 15:35:16 -060040 if (bio->bi_opf & REQ_RAHEAD) {
Christoph Hellwig70246282016-07-19 11:28:41 +020041 /* readahead of null bytes only wastes buffer cache */
42 return -EIO;
43 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 zero_fill_bio(bio);
45 break;
Christoph Hellwig70246282016-07-19 11:28:41 +020046 case REQ_OP_WRITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 /* writes get silently dropped */
48 break;
Christoph Hellwig70246282016-07-19 11:28:41 +020049 default:
50 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 }
52
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020053 bio_endio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 /* accepted bio, don't make new request */
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -080056 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
59static struct target_type zero_target = {
60 .name = "zero",
Mikulas Patocka7de3ee52012-12-21 20:23:41 +000061 .version = {1, 1, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 .module = THIS_MODULE,
63 .ctr = zero_ctr,
64 .map = zero_map,
65};
66
Alasdair G Kergon5e198d92005-05-05 16:16:09 -070067static int __init dm_zero_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 int r = dm_register_target(&zero_target);
70
71 if (r < 0)
Alasdair G Kergon72d94862006-06-26 00:27:35 -070072 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 return r;
75}
76
Alasdair G Kergon5e198d92005-05-05 16:16:09 -070077static void __exit dm_zero_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +000079 dm_unregister_target(&zero_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
82module_init(dm_zero_init)
83module_exit(dm_zero_exit)
84
Jana Saoutbf142992014-06-24 14:27:04 -040085MODULE_AUTHOR("Jana Saout <jana@saout.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070086MODULE_DESCRIPTION(DM_NAME " dummy target returning zeros");
87MODULE_LICENSE("GPL");