dm-zero.c 1.5 KB

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