pagelist.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include <linux/module.h>
  2. #include <linux/gfp.h>
  3. #include <linux/pagemap.h>
  4. #include <linux/highmem.h>
  5. #include <linux/ceph/pagelist.h>
  6. static void ceph_pagelist_unmap_tail(struct ceph_pagelist *pl)
  7. {
  8. if (pl->mapped_tail) {
  9. struct page *page = list_entry(pl->head.prev, struct page, lru);
  10. kunmap(page);
  11. pl->mapped_tail = NULL;
  12. }
  13. }
  14. int ceph_pagelist_release(struct ceph_pagelist *pl)
  15. {
  16. ceph_pagelist_unmap_tail(pl);
  17. while (!list_empty(&pl->head)) {
  18. struct page *page = list_first_entry(&pl->head, struct page,
  19. lru);
  20. list_del(&page->lru);
  21. __free_page(page);
  22. }
  23. ceph_pagelist_free_reserve(pl);
  24. return 0;
  25. }
  26. EXPORT_SYMBOL(ceph_pagelist_release);
  27. static int ceph_pagelist_addpage(struct ceph_pagelist *pl)
  28. {
  29. struct page *page;
  30. if (!pl->num_pages_free) {
  31. page = __page_cache_alloc(GFP_NOFS);
  32. } else {
  33. page = list_first_entry(&pl->free_list, struct page, lru);
  34. list_del(&page->lru);
  35. --pl->num_pages_free;
  36. }
  37. if (!page)
  38. return -ENOMEM;
  39. pl->room += PAGE_SIZE;
  40. ceph_pagelist_unmap_tail(pl);
  41. list_add_tail(&page->lru, &pl->head);
  42. pl->mapped_tail = kmap(page);
  43. return 0;
  44. }
  45. int ceph_pagelist_append(struct ceph_pagelist *pl, const void *buf, size_t len)
  46. {
  47. while (pl->room < len) {
  48. size_t bit = pl->room;
  49. int ret;
  50. memcpy(pl->mapped_tail + (pl->length & ~PAGE_CACHE_MASK),
  51. buf, bit);
  52. pl->length += bit;
  53. pl->room -= bit;
  54. buf += bit;
  55. len -= bit;
  56. ret = ceph_pagelist_addpage(pl);
  57. if (ret)
  58. return ret;
  59. }
  60. memcpy(pl->mapped_tail + (pl->length & ~PAGE_CACHE_MASK), buf, len);
  61. pl->length += len;
  62. pl->room -= len;
  63. return 0;
  64. }
  65. EXPORT_SYMBOL(ceph_pagelist_append);
  66. /**
  67. * Allocate enough pages for a pagelist to append the given amount
  68. * of data without without allocating.
  69. * Returns: 0 on success, -ENOMEM on error.
  70. */
  71. int ceph_pagelist_reserve(struct ceph_pagelist *pl, size_t space)
  72. {
  73. if (space <= pl->room)
  74. return 0;
  75. space -= pl->room;
  76. space = (space + PAGE_SIZE - 1) >> PAGE_SHIFT; /* conv to num pages */
  77. while (space > pl->num_pages_free) {
  78. struct page *page = __page_cache_alloc(GFP_NOFS);
  79. if (!page)
  80. return -ENOMEM;
  81. list_add_tail(&page->lru, &pl->free_list);
  82. ++pl->num_pages_free;
  83. }
  84. return 0;
  85. }
  86. EXPORT_SYMBOL(ceph_pagelist_reserve);
  87. /**
  88. * Free any pages that have been preallocated.
  89. */
  90. int ceph_pagelist_free_reserve(struct ceph_pagelist *pl)
  91. {
  92. while (!list_empty(&pl->free_list)) {
  93. struct page *page = list_first_entry(&pl->free_list,
  94. struct page, lru);
  95. list_del(&page->lru);
  96. __free_page(page);
  97. --pl->num_pages_free;
  98. }
  99. BUG_ON(pl->num_pages_free);
  100. return 0;
  101. }
  102. EXPORT_SYMBOL(ceph_pagelist_free_reserve);
  103. /**
  104. * Create a truncation point.
  105. */
  106. void ceph_pagelist_set_cursor(struct ceph_pagelist *pl,
  107. struct ceph_pagelist_cursor *c)
  108. {
  109. c->pl = pl;
  110. c->page_lru = pl->head.prev;
  111. c->room = pl->room;
  112. }
  113. EXPORT_SYMBOL(ceph_pagelist_set_cursor);
  114. /**
  115. * Truncate a pagelist to the given point. Move extra pages to reserve.
  116. * This won't sleep.
  117. * Returns: 0 on success,
  118. * -EINVAL if the pagelist doesn't match the trunc point pagelist
  119. */
  120. int ceph_pagelist_truncate(struct ceph_pagelist *pl,
  121. struct ceph_pagelist_cursor *c)
  122. {
  123. struct page *page;
  124. if (pl != c->pl)
  125. return -EINVAL;
  126. ceph_pagelist_unmap_tail(pl);
  127. while (pl->head.prev != c->page_lru) {
  128. page = list_entry(pl->head.prev, struct page, lru);
  129. list_del(&page->lru); /* remove from pagelist */
  130. list_add_tail(&page->lru, &pl->free_list); /* add to reserve */
  131. ++pl->num_pages_free;
  132. }
  133. pl->room = c->room;
  134. if (!list_empty(&pl->head)) {
  135. page = list_entry(pl->head.prev, struct page, lru);
  136. pl->mapped_tail = kmap(page);
  137. }
  138. return 0;
  139. }
  140. EXPORT_SYMBOL(ceph_pagelist_truncate);