dm-snap-transient.c 3.7 KB

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