Merge tag 'tpmdd-v6.4-rc1-fix-v2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-block.git] / drivers / md / dm-zero.c
CommitLineData
3bd94003 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4 2/*
bf14299f 3 * Copyright (C) 2003 Jana Saout <jana@saout.de>
1da177e4
LT
4 *
5 * This file is released under the GPL.
6 */
7
586e80e6 8#include <linux/device-mapper.h>
1da177e4
LT
9
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/bio.h>
13
72d94861
AK
14#define DM_MSG_PREFIX "zero"
15
1da177e4
LT
16/*
17 * Construct a dummy mapping that only returns zeros
18 */
19static int zero_ctr(struct dm_target *ti, unsigned int argc, char **argv)
20{
21 if (argc != 0) {
72d94861 22 ti->error = "No arguments required";
1da177e4
LT
23 return -EINVAL;
24 }
25
f8facb61
MS
26 /*
27 * Silently drop discards, avoiding -EOPNOTSUPP.
28 */
55a62eef 29 ti->num_discard_bios = 1;
00065f92 30 ti->discards_supported = true;
f8facb61 31
1da177e4
LT
32 return 0;
33}
34
35/*
36 * Return zeros only on reads
37 */
7de3ee57 38static int zero_map(struct dm_target *ti, struct bio *bio)
1da177e4 39{
70246286
CH
40 switch (bio_op(bio)) {
41 case REQ_OP_READ:
1eff9d32 42 if (bio->bi_opf & REQ_RAHEAD) {
70246286 43 /* readahead of null bytes only wastes buffer cache */
846785e6 44 return DM_MAPIO_KILL;
70246286 45 }
1da177e4
LT
46 zero_fill_bio(bio);
47 break;
70246286 48 case REQ_OP_WRITE:
00065f92 49 case REQ_OP_DISCARD:
1da177e4
LT
50 /* writes get silently dropped */
51 break;
70246286 52 default:
846785e6 53 return DM_MAPIO_KILL;
1da177e4
LT
54 }
55
4246a0b6 56 bio_endio(bio);
1da177e4
LT
57
58 /* accepted bio, don't make new request */
d2a7ad29 59 return DM_MAPIO_SUBMITTED;
1da177e4
LT
60}
61
00065f92
MP
62static void zero_io_hints(struct dm_target *ti, struct queue_limits *limits)
63{
64 limits->max_discard_sectors = UINT_MAX;
65 limits->max_hw_discard_sectors = UINT_MAX;
66 limits->discard_granularity = 512;
67}
68
1da177e4
LT
69static struct target_type zero_target = {
70 .name = "zero",
00065f92 71 .version = {1, 2, 0},
410fe220 72 .features = DM_TARGET_NOWAIT,
1da177e4
LT
73 .module = THIS_MODULE,
74 .ctr = zero_ctr,
75 .map = zero_map,
00065f92 76 .io_hints = zero_io_hints,
1da177e4 77};
3664ff82 78module_dm(zero);
1da177e4 79
bf14299f 80MODULE_AUTHOR("Jana Saout <jana@saout.de>");
1da177e4
LT
81MODULE_DESCRIPTION(DM_NAME " dummy target returning zeros");
82MODULE_LICENSE("GPL");