dm-snap-transient.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_snap_exception *e)
  33. {
  34. struct transient_c *tc = store->context;
  35. sector_t size = get_dev_size(store->cow->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_snap_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_fraction_full(struct dm_exception_store *store,
  51. sector_t *numerator, sector_t *denominator)
  52. {
  53. *numerator = ((struct transient_c *) store->context)->next_free;
  54. *denominator = get_dev_size(store->cow->bdev);
  55. }
  56. static int transient_ctr(struct dm_exception_store *store,
  57. unsigned argc, char **argv)
  58. {
  59. struct transient_c *tc;
  60. tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL);
  61. if (!tc)
  62. return -ENOMEM;
  63. tc->next_free = 0;
  64. store->context = tc;
  65. return 0;
  66. }
  67. static unsigned transient_status(struct dm_exception_store *store,
  68. status_type_t status, char *result,
  69. unsigned maxlen)
  70. {
  71. unsigned sz = 0;
  72. switch (status) {
  73. case STATUSTYPE_INFO:
  74. break;
  75. case STATUSTYPE_TABLE:
  76. DMEMIT(" %s N %llu", store->cow->name,
  77. (unsigned long long)store->chunk_size);
  78. }
  79. return sz;
  80. }
  81. static struct dm_exception_store_type _transient_type = {
  82. .name = "transient",
  83. .module = THIS_MODULE,
  84. .ctr = transient_ctr,
  85. .dtr = transient_dtr,
  86. .read_metadata = transient_read_metadata,
  87. .prepare_exception = transient_prepare_exception,
  88. .commit_exception = transient_commit_exception,
  89. .fraction_full = transient_fraction_full,
  90. .status = transient_status,
  91. };
  92. static struct dm_exception_store_type _transient_compat_type = {
  93. .name = "N",
  94. .module = THIS_MODULE,
  95. .ctr = transient_ctr,
  96. .dtr = transient_dtr,
  97. .read_metadata = transient_read_metadata,
  98. .prepare_exception = transient_prepare_exception,
  99. .commit_exception = transient_commit_exception,
  100. .fraction_full = transient_fraction_full,
  101. .status = transient_status,
  102. };
  103. int dm_transient_snapshot_init(void)
  104. {
  105. int r;
  106. r = dm_exception_store_type_register(&_transient_type);
  107. if (r) {
  108. DMWARN("Unable to register transient exception store type");
  109. return r;
  110. }
  111. r = dm_exception_store_type_register(&_transient_compat_type);
  112. if (r) {
  113. DMWARN("Unable to register old-style transient "
  114. "exception store type");
  115. dm_exception_store_type_unregister(&_transient_type);
  116. return r;
  117. }
  118. return r;
  119. }
  120. void dm_transient_snapshot_exit(void)
  121. {
  122. dm_exception_store_type_unregister(&_transient_type);
  123. dm_exception_store_type_unregister(&_transient_compat_type);
  124. }