dm-snap-transient.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/slab.h>
  12. #include <linux/dm-io.h>
  13. #define DM_MSG_PREFIX "transient snapshot"
  14. /*-----------------------------------------------------------------
  15. * Implementation of the store for non-persistent snapshots.
  16. *---------------------------------------------------------------*/
  17. struct transient_c {
  18. sector_t next_free;
  19. };
  20. static void transient_dtr(struct dm_exception_store *store)
  21. {
  22. kfree(store->context);
  23. }
  24. static int transient_read_metadata(struct dm_exception_store *store,
  25. int (*callback)(void *callback_context,
  26. chunk_t old, chunk_t new),
  27. void *callback_context)
  28. {
  29. return 0;
  30. }
  31. static int transient_prepare_exception(struct dm_exception_store *store,
  32. struct dm_exception *e)
  33. {
  34. struct transient_c *tc = store->context;
  35. sector_t size = get_dev_size(dm_snap_cow(store->snap)->bdev);
  36. if (size < (tc->next_free + store->chunk_size))
  37. return -1;
  38. e->new_chunk = sector_to_chunk(store, tc->next_free);
  39. tc->next_free += store->chunk_size;
  40. return 0;
  41. }
  42. static void transient_commit_exception(struct dm_exception_store *store,
  43. struct dm_exception *e,
  44. void (*callback) (void *, int success),
  45. void *callback_context)
  46. {
  47. /* Just succeed */
  48. callback(callback_context, 1);
  49. }
  50. static void transient_usage(struct dm_exception_store *store,
  51. sector_t *total_sectors,
  52. sector_t *sectors_allocated,
  53. sector_t *metadata_sectors)
  54. {
  55. *sectors_allocated = ((struct transient_c *) store->context)->next_free;
  56. *total_sectors = get_dev_size(dm_snap_cow(store->snap)->bdev);
  57. *metadata_sectors = 0;
  58. }
  59. static int transient_ctr(struct dm_exception_store *store,
  60. unsigned argc, char **argv)
  61. {
  62. struct transient_c *tc;
  63. tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL);
  64. if (!tc)
  65. return -ENOMEM;
  66. tc->next_free = 0;
  67. store->context = tc;
  68. return 0;
  69. }
  70. static unsigned transient_status(struct dm_exception_store *store,
  71. status_type_t status, char *result,
  72. unsigned maxlen)
  73. {
  74. unsigned sz = 0;
  75. switch (status) {
  76. case STATUSTYPE_INFO:
  77. break;
  78. case STATUSTYPE_TABLE:
  79. DMEMIT(" N %llu", (unsigned long long)store->chunk_size);
  80. }
  81. return sz;
  82. }
  83. static struct dm_exception_store_type _transient_type = {
  84. .name = "transient",
  85. .module = THIS_MODULE,
  86. .ctr = transient_ctr,
  87. .dtr = transient_dtr,
  88. .read_metadata = transient_read_metadata,
  89. .prepare_exception = transient_prepare_exception,
  90. .commit_exception = transient_commit_exception,
  91. .usage = transient_usage,
  92. .status = transient_status,
  93. };
  94. static struct dm_exception_store_type _transient_compat_type = {
  95. .name = "N",
  96. .module = THIS_MODULE,
  97. .ctr = transient_ctr,
  98. .dtr = transient_dtr,
  99. .read_metadata = transient_read_metadata,
  100. .prepare_exception = transient_prepare_exception,
  101. .commit_exception = transient_commit_exception,
  102. .usage = transient_usage,
  103. .status = transient_status,
  104. };
  105. int dm_transient_snapshot_init(void)
  106. {
  107. int r;
  108. r = dm_exception_store_type_register(&_transient_type);
  109. if (r) {
  110. DMWARN("Unable to register transient exception store type");
  111. return r;
  112. }
  113. r = dm_exception_store_type_register(&_transient_compat_type);
  114. if (r) {
  115. DMWARN("Unable to register old-style transient "
  116. "exception store type");
  117. dm_exception_store_type_unregister(&_transient_type);
  118. return r;
  119. }
  120. return r;
  121. }
  122. void dm_transient_snapshot_exit(void)
  123. {
  124. dm_exception_store_type_unregister(&_transient_type);
  125. dm_exception_store_type_unregister(&_transient_compat_type);
  126. }