dm-snap-transient.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <linux/mm.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/vmalloc.h>
  11. #include <linux/export.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_exception *e)
  34. {
  35. struct transient_c *tc = store->context;
  36. sector_t size = get_dev_size(dm_snap_cow(store->snap)->bdev);
  37. if (size < (tc->next_free + store->chunk_size))
  38. return -1;
  39. e->new_chunk = sector_to_chunk(store, 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_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_usage(struct dm_exception_store *store,
  52. sector_t *total_sectors,
  53. sector_t *sectors_allocated,
  54. sector_t *metadata_sectors)
  55. {
  56. *sectors_allocated = ((struct transient_c *) store->context)->next_free;
  57. *total_sectors = get_dev_size(dm_snap_cow(store->snap)->bdev);
  58. *metadata_sectors = 0;
  59. }
  60. static int transient_ctr(struct dm_exception_store *store,
  61. unsigned argc, char **argv)
  62. {
  63. struct transient_c *tc;
  64. tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL);
  65. if (!tc)
  66. return -ENOMEM;
  67. tc->next_free = 0;
  68. store->context = tc;
  69. return 0;
  70. }
  71. static unsigned transient_status(struct dm_exception_store *store,
  72. status_type_t status, char *result,
  73. unsigned maxlen)
  74. {
  75. unsigned sz = 0;
  76. switch (status) {
  77. case STATUSTYPE_INFO:
  78. break;
  79. case STATUSTYPE_TABLE:
  80. DMEMIT(" N %llu", (unsigned long long)store->chunk_size);
  81. }
  82. return sz;
  83. }
  84. static struct dm_exception_store_type _transient_type = {
  85. .name = "transient",
  86. .module = THIS_MODULE,
  87. .ctr = transient_ctr,
  88. .dtr = transient_dtr,
  89. .read_metadata = transient_read_metadata,
  90. .prepare_exception = transient_prepare_exception,
  91. .commit_exception = transient_commit_exception,
  92. .usage = transient_usage,
  93. .status = transient_status,
  94. };
  95. static struct dm_exception_store_type _transient_compat_type = {
  96. .name = "N",
  97. .module = THIS_MODULE,
  98. .ctr = transient_ctr,
  99. .dtr = transient_dtr,
  100. .read_metadata = transient_read_metadata,
  101. .prepare_exception = transient_prepare_exception,
  102. .commit_exception = transient_commit_exception,
  103. .usage = transient_usage,
  104. .status = transient_status,
  105. };
  106. int dm_transient_snapshot_init(void)
  107. {
  108. int r;
  109. r = dm_exception_store_type_register(&_transient_type);
  110. if (r) {
  111. DMWARN("Unable to register transient exception store type");
  112. return r;
  113. }
  114. r = dm_exception_store_type_register(&_transient_compat_type);
  115. if (r) {
  116. DMWARN("Unable to register old-style transient "
  117. "exception store type");
  118. dm_exception_store_type_unregister(&_transient_type);
  119. return r;
  120. }
  121. return r;
  122. }
  123. void dm_transient_snapshot_exit(void)
  124. {
  125. dm_exception_store_type_unregister(&_transient_type);
  126. dm_exception_store_type_unregister(&_transient_compat_type);
  127. }