dm-zero.c 1.6 KB

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