dm-zero.c 1.5 KB

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