dm-snap-transient.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
  3. * Copyright (C) 2006-2008 Red Hat GmbH
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include "dm-exception-store.h"
  8. #include "dm-snap.h"
  9. #include <linux/mm.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/slab.h>
  13. #include <linux/dm-io.h>
  14. #define DM_MSG_PREFIX "transient snapshot"
  15. /*-----------------------------------------------------------------
  16. * Implementation of the store for non-persistent snapshots.
  17. *---------------------------------------------------------------*/
  18. struct transient_c {
  19. sector_t next_free;
  20. };
  21. static void transient_dtr(struct dm_exception_store *store)
  22. {
  23. kfree(store->context);
  24. }
  25. static int transient_read_metadata(struct dm_exception_store *store,
  26. int (*callback)(void *callback_context,
  27. chunk_t old, chunk_t new),
  28. void *callback_context)
  29. {
  30. return 0;
  31. }
  32. static int transient_prepare_exception(struct dm_exception_store *store,
  33. struct dm_snap_exception *e)
  34. {
  35. struct transient_c *tc = store->context;
  36. sector_t size = get_dev_size(store->snap->cow->bdev);
  37. if (size < (tc->next_free + store->chunk_size))
  38. return -1;
  39. e->new_chunk = sector_to_chunk(store->snap, tc->next_free);
  40. tc->next_free += store->chunk_size;
  41. return 0;
  42. }
  43. static void transient_commit_exception(struct dm_exception_store *store,
  44. struct dm_snap_exception *e,
  45. void (*callback) (void *, int success),
  46. void *callback_context)
  47. {
  48. /* Just succeed */
  49. callback(callback_context, 1);
  50. }
  51. static void transient_fraction_full(struct dm_exception_store *store,
  52. sector_t *numerator, sector_t *denominator)
  53. {
  54. *numerator = ((struct transient_c *) store->context)->next_free;
  55. *denominator = get_dev_size(store->snap->cow->bdev);
  56. }
  57. static int transient_ctr(struct dm_exception_store *store,
  58. unsigned argc, char **argv)
  59. {
  60. struct transient_c *tc;
  61. tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL);
  62. if (!tc)
  63. return -ENOMEM;
  64. tc->next_free = 0;
  65. store->context = tc;
  66. return 0;
  67. }
  68. static int transient_status(struct dm_exception_store *store,
  69. status_type_t status, char *result,
  70. unsigned maxlen)
  71. {
  72. int sz = 0;
  73. return sz;
  74. }
  75. static struct dm_exception_store_type _transient_type = {
  76. .name = "transient",
  77. .module = THIS_MODULE,
  78. .ctr = transient_ctr,
  79. .dtr = transient_dtr,
  80. .read_metadata = transient_read_metadata,
  81. .prepare_exception = transient_prepare_exception,
  82. .commit_exception = transient_commit_exception,
  83. .fraction_full = transient_fraction_full,
  84. .status = transient_status,
  85. };
  86. static struct dm_exception_store_type _transient_compat_type = {
  87. .name = "N",
  88. .module = THIS_MODULE,
  89. .ctr = transient_ctr,
  90. .dtr = transient_dtr,
  91. .read_metadata = transient_read_metadata,
  92. .prepare_exception = transient_prepare_exception,
  93. .commit_exception = transient_commit_exception,
  94. .fraction_full = transient_fraction_full,
  95. .status = transient_status,
  96. };
  97. int dm_transient_snapshot_init(void)
  98. {
  99. int r;
  100. r = dm_exception_store_type_register(&_transient_type);
  101. if (r) {
  102. DMWARN("Unable to register transient exception store type");
  103. return r;
  104. }
  105. r = dm_exception_store_type_register(&_transient_compat_type);
  106. if (r) {
  107. DMWARN("Unable to register old-style transient "
  108. "exception store type");
  109. dm_exception_store_type_unregister(&_transient_type);
  110. return r;
  111. }
  112. return r;
  113. }
  114. void dm_transient_snapshot_exit(void)
  115. {
  116. dm_exception_store_type_unregister(&_transient_type);
  117. dm_exception_store_type_unregister(&_transient_compat_type);
  118. }