tuxonice_extent.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * kernel/power/tuxonice_extent.c
  3. *
  4. * Copyright (C) 2003-2014 Nigel Cunningham (nigel at tuxonice net)
  5. *
  6. * Distributed under GPLv2.
  7. *
  8. * These functions encapsulate the manipulation of storage metadata.
  9. */
  10. #include <linux/suspend.h>
  11. #include "tuxonice_modules.h"
  12. #include "tuxonice_extent.h"
  13. #include "tuxonice_alloc.h"
  14. #include "tuxonice_ui.h"
  15. #include "tuxonice.h"
  16. /**
  17. * toi_get_extent - return a free extent
  18. *
  19. * May fail, returning NULL instead.
  20. **/
  21. static struct hibernate_extent *toi_get_extent(void)
  22. {
  23. return (struct hibernate_extent *) toi_kzalloc(2,
  24. sizeof(struct hibernate_extent), TOI_ATOMIC_GFP);
  25. }
  26. /**
  27. * toi_put_extent_chain - free a whole chain of extents
  28. * @chain: Chain to free.
  29. **/
  30. void toi_put_extent_chain(struct hibernate_extent_chain *chain)
  31. {
  32. struct hibernate_extent *this;
  33. this = chain->first;
  34. while (this) {
  35. struct hibernate_extent *next = this->next;
  36. toi_kfree(2, this, sizeof(*this));
  37. chain->num_extents--;
  38. this = next;
  39. }
  40. chain->first = NULL;
  41. chain->last_touched = NULL;
  42. chain->current_extent = NULL;
  43. chain->size = 0;
  44. }
  45. EXPORT_SYMBOL_GPL(toi_put_extent_chain);
  46. /**
  47. * toi_add_to_extent_chain - add an extent to an existing chain
  48. * @chain: Chain to which the extend should be added
  49. * @start: Start of the extent (first physical block)
  50. * @end: End of the extent (last physical block)
  51. *
  52. * The chain information is updated if the insertion is successful.
  53. **/
  54. int toi_add_to_extent_chain(struct hibernate_extent_chain *chain,
  55. unsigned long start, unsigned long end)
  56. {
  57. struct hibernate_extent *new_ext = NULL, *cur_ext = NULL;
  58. toi_message(TOI_IO, TOI_VERBOSE, 0,
  59. "Adding extent %lu-%lu to chain %p.\n", start, end, chain);
  60. /* Find the right place in the chain */
  61. if (chain->last_touched && chain->last_touched->start < start)
  62. cur_ext = chain->last_touched;
  63. else if (chain->first && chain->first->start < start)
  64. cur_ext = chain->first;
  65. if (cur_ext) {
  66. while (cur_ext->next && cur_ext->next->start < start)
  67. cur_ext = cur_ext->next;
  68. if (cur_ext->end == (start - 1)) {
  69. struct hibernate_extent *next_ext = cur_ext->next;
  70. cur_ext->end = end;
  71. /* Merge with the following one? */
  72. if (next_ext && cur_ext->end + 1 == next_ext->start) {
  73. cur_ext->end = next_ext->end;
  74. cur_ext->next = next_ext->next;
  75. toi_kfree(2, next_ext, sizeof(*next_ext));
  76. chain->num_extents--;
  77. }
  78. chain->last_touched = cur_ext;
  79. chain->size += (end - start + 1);
  80. return 0;
  81. }
  82. }
  83. new_ext = toi_get_extent();
  84. if (!new_ext) {
  85. pr_warn("Error unable to append a new extent to the chain.\n");
  86. return -ENOMEM;
  87. }
  88. chain->num_extents++;
  89. chain->size += (end - start + 1);
  90. new_ext->start = start;
  91. new_ext->end = end;
  92. chain->last_touched = new_ext;
  93. if (cur_ext) {
  94. new_ext->next = cur_ext->next;
  95. cur_ext->next = new_ext;
  96. } else {
  97. if (chain->first)
  98. new_ext->next = chain->first;
  99. chain->first = new_ext;
  100. }
  101. return 0;
  102. }
  103. EXPORT_SYMBOL_GPL(toi_add_to_extent_chain);