blob: 766bc93006e616758fef0f37397a8cf5126bea3a [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{
38 switch(bio_rw(bio)) {
39 case READ:
40 zero_fill_bio(bio);
41 break;
42 case READA:
43 /* readahead of null bytes only wastes buffer cache */
44 return -EIO;
45 case WRITE:
46 /* writes get silently dropped */
47 break;
48 }
49
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020050 bio_endio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 /* accepted bio, don't make new request */
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -080053 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
56static struct target_type zero_target = {
57 .name = "zero",
Mikulas Patocka7de3ee52012-12-21 20:23:41 +000058 .version = {1, 1, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 .module = THIS_MODULE,
60 .ctr = zero_ctr,
61 .map = zero_map,
62};
63
Alasdair G Kergon5e198d92005-05-05 16:16:09 -070064static int __init dm_zero_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 int r = dm_register_target(&zero_target);
67
68 if (r < 0)
Alasdair G Kergon72d94862006-06-26 00:27:35 -070069 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 return r;
72}
73
Alasdair G Kergon5e198d92005-05-05 16:16:09 -070074static void __exit dm_zero_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +000076 dm_unregister_target(&zero_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
79module_init(dm_zero_init)
80module_exit(dm_zero_exit)
81
Jana Saoutbf142992014-06-24 14:27:04 -040082MODULE_AUTHOR("Jana Saout <jana@saout.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070083MODULE_DESCRIPTION(DM_NAME " dummy target returning zeros");
84MODULE_LICENSE("GPL");