xref: /openbmc/linux/drivers/md/dm-zero.c (revision 410fe220078c3e3ed6f4167fb4de65139cfec631)
11da177e4SLinus Torvalds /*
2bf14299fSJana Saout  * Copyright (C) 2003 Jana Saout <jana@saout.de>
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * This file is released under the GPL.
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
7586e80e6SMikulas Patocka #include <linux/device-mapper.h>
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/module.h>
101da177e4SLinus Torvalds #include <linux/init.h>
111da177e4SLinus Torvalds #include <linux/bio.h>
121da177e4SLinus Torvalds 
1372d94861SAlasdair G Kergon #define DM_MSG_PREFIX "zero"
1472d94861SAlasdair G Kergon 
151da177e4SLinus Torvalds /*
161da177e4SLinus Torvalds  * Construct a dummy mapping that only returns zeros
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds static int zero_ctr(struct dm_target *ti, unsigned int argc, char **argv)
191da177e4SLinus Torvalds {
201da177e4SLinus Torvalds 	if (argc != 0) {
2172d94861SAlasdair G Kergon 		ti->error = "No arguments required";
221da177e4SLinus Torvalds 		return -EINVAL;
231da177e4SLinus Torvalds 	}
241da177e4SLinus Torvalds 
25f8facb61SMike Snitzer 	/*
26f8facb61SMike Snitzer 	 * Silently drop discards, avoiding -EOPNOTSUPP.
27f8facb61SMike Snitzer 	 */
2855a62eefSAlasdair G Kergon 	ti->num_discard_bios = 1;
29f8facb61SMike Snitzer 
301da177e4SLinus Torvalds 	return 0;
311da177e4SLinus Torvalds }
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds /*
341da177e4SLinus Torvalds  * Return zeros only on reads
351da177e4SLinus Torvalds  */
367de3ee57SMikulas Patocka static int zero_map(struct dm_target *ti, struct bio *bio)
371da177e4SLinus Torvalds {
3870246286SChristoph Hellwig 	switch (bio_op(bio)) {
3970246286SChristoph Hellwig 	case REQ_OP_READ:
401eff9d32SJens Axboe 		if (bio->bi_opf & REQ_RAHEAD) {
411da177e4SLinus Torvalds 			/* readahead of null bytes only wastes buffer cache */
42846785e6SChristoph Hellwig 			return DM_MAPIO_KILL;
4370246286SChristoph Hellwig 		}
4470246286SChristoph Hellwig 		zero_fill_bio(bio);
4570246286SChristoph Hellwig 		break;
4670246286SChristoph Hellwig 	case REQ_OP_WRITE:
471da177e4SLinus Torvalds 		/* writes get silently dropped */
481da177e4SLinus Torvalds 		break;
4970246286SChristoph Hellwig 	default:
50846785e6SChristoph Hellwig 		return DM_MAPIO_KILL;
511da177e4SLinus Torvalds 	}
521da177e4SLinus Torvalds 
534246a0b6SChristoph Hellwig 	bio_endio(bio);
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 	/* accepted bio, don't make new request */
56d2a7ad29SKiyoshi Ueda 	return DM_MAPIO_SUBMITTED;
571da177e4SLinus Torvalds }
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds static struct target_type zero_target = {
601da177e4SLinus Torvalds 	.name   = "zero",
617de3ee57SMikulas Patocka 	.version = {1, 1, 0},
62*410fe220SJeffle Xu 	.features = DM_TARGET_NOWAIT,
631da177e4SLinus Torvalds 	.module = THIS_MODULE,
641da177e4SLinus Torvalds 	.ctr    = zero_ctr,
651da177e4SLinus Torvalds 	.map    = zero_map,
661da177e4SLinus Torvalds };
671da177e4SLinus Torvalds 
685e198d94SAlasdair G Kergon static int __init dm_zero_init(void)
691da177e4SLinus Torvalds {
701da177e4SLinus Torvalds 	int r = dm_register_target(&zero_target);
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 	if (r < 0)
7372d94861SAlasdair G Kergon 		DMERR("register failed %d", r);
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds 	return r;
761da177e4SLinus Torvalds }
771da177e4SLinus Torvalds 
785e198d94SAlasdair G Kergon static void __exit dm_zero_exit(void)
791da177e4SLinus Torvalds {
8010d3bd09SMikulas Patocka 	dm_unregister_target(&zero_target);
811da177e4SLinus Torvalds }
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds module_init(dm_zero_init)
841da177e4SLinus Torvalds module_exit(dm_zero_exit)
851da177e4SLinus Torvalds 
86bf14299fSJana Saout MODULE_AUTHOR("Jana Saout <jana@saout.de>");
871da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " dummy target returning zeros");
881da177e4SLinus Torvalds MODULE_LICENSE("GPL");
89