dm-snap-transient.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 "dm-snap.h"
  9. #include <linux/mm.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/vmalloc.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_destroy(struct dm_exception_store *store)
  22. {
  23. kfree(store->context);
  24. }
  25. static int transient_read_metadata(struct dm_exception_store *store)
  26. {
  27. return 0;
  28. }
  29. static int transient_prepare(struct dm_exception_store *store,
  30. struct dm_snap_exception *e)
  31. {
  32. struct transient_c *tc = (struct transient_c *) store->context;
  33. sector_t size = get_dev_size(store->snap->cow->bdev);
  34. if (size < (tc->next_free + store->snap->chunk_size))
  35. return -1;
  36. e->new_chunk = sector_to_chunk(store->snap, tc->next_free);
  37. tc->next_free += store->snap->chunk_size;
  38. return 0;
  39. }
  40. static void transient_commit(struct dm_exception_store *store,
  41. struct dm_snap_exception *e,
  42. void (*callback) (void *, int success),
  43. void *callback_context)
  44. {
  45. /* Just succeed */
  46. callback(callback_context, 1);
  47. }
  48. static void transient_fraction_full(struct dm_exception_store *store,
  49. sector_t *numerator, sector_t *denominator)
  50. {
  51. *numerator = ((struct transient_c *) store->context)->next_free;
  52. *denominator = get_dev_size(store->snap->cow->bdev);
  53. }
  54. int dm_create_transient(struct dm_exception_store *store)
  55. {
  56. struct transient_c *tc;
  57. store->destroy = transient_destroy;
  58. store->read_metadata = transient_read_metadata;
  59. store->prepare_exception = transient_prepare;
  60. store->commit_exception = transient_commit;
  61. store->drop_snapshot = NULL;
  62. store->fraction_full = transient_fraction_full;
  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. int dm_transient_snapshot_init(void)
  71. {
  72. return 0;
  73. }
  74. void dm_transient_snapshot_exit(void)
  75. {
  76. }