pagelist.c 3.7 KB

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