blob: 766bc93006e616758fef0f37397a8cf5126bea3a [file] [log] [blame]
Kyle Swenson8d8f6542021-03-15 11:02:55 -06001/*
2 * Copyright (C) 2003 Jana Saout <jana@saout.de>
3 *
4 * This file is released under the GPL.
5 */
6
7#include <linux/device-mapper.h>
8
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/bio.h>
12
13#define DM_MSG_PREFIX "zero"
14
15/*
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) {
21 ti->error = "No arguments required";
22 return -EINVAL;
23 }
24
25 /*
26 * Silently drop discards, avoiding -EOPNOTSUPP.
27 */
28 ti->num_discard_bios = 1;
29
30 return 0;
31}
32
33/*
34 * Return zeros only on reads
35 */
36static int zero_map(struct dm_target *ti, struct bio *bio)
37{
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
50 bio_endio(bio);
51
52 /* accepted bio, don't make new request */
53 return DM_MAPIO_SUBMITTED;
54}
55
56static struct target_type zero_target = {
57 .name = "zero",
58 .version = {1, 1, 0},
59 .module = THIS_MODULE,
60 .ctr = zero_ctr,
61 .map = zero_map,
62};
63
64static int __init dm_zero_init(void)
65{
66 int r = dm_register_target(&zero_target);
67
68 if (r < 0)
69 DMERR("register failed %d", r);
70
71 return r;
72}
73
74static void __exit dm_zero_exit(void)
75{
76 dm_unregister_target(&zero_target);
77}
78
79module_init(dm_zero_init)
80module_exit(dm_zero_exit)
81
82MODULE_AUTHOR("Jana Saout <jana@saout.de>");
83MODULE_DESCRIPTION(DM_NAME " dummy target returning zeros");
84MODULE_LICENSE("GPL");