dm-zero.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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_rw(bio)) {
  32. case READ:
  33. zero_fill_bio(bio);
  34. break;
  35. case READA:
  36. /* readahead of null bytes only wastes buffer cache */
  37. return -EIO;
  38. case WRITE:
  39. /* writes get silently dropped */
  40. break;
  41. }
  42. bio_endio(bio);
  43. /* accepted bio, don't make new request */
  44. return DM_MAPIO_SUBMITTED;
  45. }
  46. static struct target_type zero_target = {
  47. .name = "zero",
  48. .version = {1, 1, 0},
  49. .module = THIS_MODULE,
  50. .ctr = zero_ctr,
  51. .map = zero_map,
  52. };
  53. static int __init dm_zero_init(void)
  54. {
  55. int r = dm_register_target(&zero_target);
  56. if (r < 0)
  57. DMERR("register failed %d", r);
  58. return r;
  59. }
  60. static void __exit dm_zero_exit(void)
  61. {
  62. dm_unregister_target(&zero_target);
  63. }
  64. module_init(dm_zero_init)
  65. module_exit(dm_zero_exit)
  66. MODULE_AUTHOR("Jana Saout <jana@saout.de>");
  67. MODULE_DESCRIPTION(DM_NAME " dummy target returning zeros");
  68. MODULE_LICENSE("GPL");