pagelist.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include <linux/gfp.h>
  2. #include <linux/pagemap.h>
  3. #include <linux/highmem.h>
  4. #include "pagelist.h"
  5. static void ceph_pagelist_unmap_tail(struct ceph_pagelist *pl)
  6. {
  7. struct page *page = list_entry(pl->head.prev, struct page,
  8. lru);
  9. kunmap(page);
  10. }
  11. int ceph_pagelist_release(struct ceph_pagelist *pl)
  12. {
  13. if (pl->mapped_tail)
  14. ceph_pagelist_unmap_tail(pl);
  15. while (!list_empty(&pl->head)) {
  16. struct page *page = list_first_entry(&pl->head, struct page,
  17. lru);
  18. list_del(&page->lru);
  19. __free_page(page);
  20. }
  21. return 0;
  22. }
  23. static int ceph_pagelist_addpage(struct ceph_pagelist *pl)
  24. {
  25. struct page *page = __page_cache_alloc(GFP_NOFS);
  26. if (!page)
  27. return -ENOMEM;
  28. pl->room += PAGE_SIZE;
  29. list_add_tail(&page->lru, &pl->head);
  30. if (pl->mapped_tail)
  31. ceph_pagelist_unmap_tail(pl);
  32. pl->mapped_tail = kmap(page);
  33. return 0;
  34. }
  35. int ceph_pagelist_append(struct ceph_pagelist *pl, void *buf, size_t len)
  36. {
  37. while (pl->room < len) {
  38. size_t bit = pl->room;
  39. int ret;
  40. memcpy(pl->mapped_tail + (pl->length & ~PAGE_CACHE_MASK),
  41. buf, bit);
  42. pl->length += bit;
  43. pl->room -= bit;
  44. buf += bit;
  45. len -= bit;
  46. ret = ceph_pagelist_addpage(pl);
  47. if (ret)
  48. return ret;
  49. }
  50. memcpy(pl->mapped_tail + (pl->length & ~PAGE_CACHE_MASK), buf, len);
  51. pl->length += len;
  52. pl->room -= len;
  53. return 0;
  54. }