dm-snap-transient.c 3.7 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/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, int valid,
  45. void (*callback) (void *, int success),
  46. void *callback_context)
  47. {
  48. /* Just succeed */
  49. callback(callback_context, valid);
  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, char *options)
  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. }