pagelist.c 3.6 KB

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