dm-zero.c 1.5 KB

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