dm-zero.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2003 Jana Saout <jana@saout.de>
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include <linux/device-mapper.h>
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/bio.h>
  11. #define DM_MSG_PREFIX "zero"
  12. /*
  13. * Construct a dummy mapping that only returns zeros
  14. */
  15. static int zero_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  16. {
  17. if (argc != 0) {
  18. ti->error = "No arguments required";
  19. return -EINVAL;
  20. }
  21. /*
  22. * Silently drop discards, avoiding -EOPNOTSUPP.
  23. */
  24. ti->num_discard_bios = 1;
  25. ti->discards_supported = true;
  26. return 0;
  27. }
  28. /*
  29. * Return zeros only on reads
  30. */
  31. static int zero_map(struct dm_target *ti, struct bio *bio)
  32. {
  33. switch (bio_op(bio)) {
  34. case REQ_OP_READ:
  35. if (bio->bi_opf & REQ_RAHEAD) {
  36. /* readahead of null bytes only wastes buffer cache */
  37. return DM_MAPIO_KILL;
  38. }
  39. zero_fill_bio(bio);
  40. break;
  41. case REQ_OP_WRITE:
  42. case REQ_OP_DISCARD:
  43. /* writes get silently dropped */
  44. break;
  45. default:
  46. return DM_MAPIO_KILL;
  47. }
  48. bio_endio(bio);
  49. /* accepted bio, don't make new request */
  50. return DM_MAPIO_SUBMITTED;
  51. }
  52. static void zero_io_hints(struct dm_target *ti, struct queue_limits *limits)
  53. {
  54. limits->max_hw_discard_sectors = UINT_MAX;
  55. limits->discard_granularity = 512;
  56. }
  57. static struct target_type zero_target = {
  58. .name = "zero",
  59. .version = {1, 2, 0},
  60. .features = DM_TARGET_NOWAIT,
  61. .module = THIS_MODULE,
  62. .ctr = zero_ctr,
  63. .map = zero_map,
  64. .io_hints = zero_io_hints,
  65. };
  66. module_dm(zero);
  67. MODULE_AUTHOR("Jana Saout <jana@saout.de>");
  68. MODULE_DESCRIPTION(DM_NAME " dummy target returning zeros");
  69. MODULE_LICENSE("GPL");