2 #include <linux/module.h>
4 #include <linux/pagemap.h>
5 #include <linux/highmem.h>
6 #include <linux/ceph/pagelist.h>
8 static void ceph_pagelist_unmap_tail(struct ceph_pagelist *pl)
10 if (pl->mapped_tail) {
11 struct page *page = list_entry(pl->head.prev, struct page, lru);
13 pl->mapped_tail = NULL;
17 int ceph_pagelist_release(struct ceph_pagelist *pl)
19 ceph_pagelist_unmap_tail(pl);
20 while (!list_empty(&pl->head)) {
21 struct page *page = list_first_entry(&pl->head, struct page,
26 ceph_pagelist_free_reserve(pl);
29 EXPORT_SYMBOL(ceph_pagelist_release);
31 static int ceph_pagelist_addpage(struct ceph_pagelist *pl)
35 if (!pl->num_pages_free) {
36 page = __page_cache_alloc(GFP_NOFS);
38 page = list_first_entry(&pl->free_list, struct page, lru);
44 pl->room += PAGE_SIZE;
45 ceph_pagelist_unmap_tail(pl);
46 list_add_tail(&page->lru, &pl->head);
47 pl->mapped_tail = kmap(page);
51 int ceph_pagelist_append(struct ceph_pagelist *pl, const void *buf, size_t len)
53 while (pl->room < len) {
54 size_t bit = pl->room;
57 memcpy(pl->mapped_tail + (pl->length & ~PAGE_CACHE_MASK),
63 ret = ceph_pagelist_addpage(pl);
68 memcpy(pl->mapped_tail + (pl->length & ~PAGE_CACHE_MASK), buf, len);
73 EXPORT_SYMBOL(ceph_pagelist_append);
75 /* Allocate enough pages for a pagelist to append the given amount
76 * of data without without allocating.
77 * Returns: 0 on success, -ENOMEM on error.
79 int ceph_pagelist_reserve(struct ceph_pagelist *pl, size_t space)
81 if (space <= pl->room)
84 space = (space + PAGE_SIZE - 1) >> PAGE_SHIFT; /* conv to num pages */
86 while (space > pl->num_pages_free) {
87 struct page *page = __page_cache_alloc(GFP_NOFS);
90 list_add_tail(&page->lru, &pl->free_list);
95 EXPORT_SYMBOL(ceph_pagelist_reserve);
97 /* Free any pages that have been preallocated. */
98 int ceph_pagelist_free_reserve(struct ceph_pagelist *pl)
100 while (!list_empty(&pl->free_list)) {
101 struct page *page = list_first_entry(&pl->free_list,
103 list_del(&page->lru);
105 --pl->num_pages_free;
107 BUG_ON(pl->num_pages_free);
110 EXPORT_SYMBOL(ceph_pagelist_free_reserve);
112 /* Create a truncation point. */
113 void ceph_pagelist_set_cursor(struct ceph_pagelist *pl,
114 struct ceph_pagelist_cursor *c)
117 c->page_lru = pl->head.prev;
120 EXPORT_SYMBOL(ceph_pagelist_set_cursor);
122 /* Truncate a pagelist to the given point. Move extra pages to reserve.
124 * Returns: 0 on success,
125 * -EINVAL if the pagelist doesn't match the trunc point pagelist
127 int ceph_pagelist_truncate(struct ceph_pagelist *pl,
128 struct ceph_pagelist_cursor *c)
134 ceph_pagelist_unmap_tail(pl);
135 while (pl->head.prev != c->page_lru) {
136 page = list_entry(pl->head.prev, struct page, lru);
137 list_del(&page->lru); /* remove from pagelist */
138 list_add_tail(&page->lru, &pl->free_list); /* add to reserve */
139 ++pl->num_pages_free;
142 if (!list_empty(&pl->head)) {
143 page = list_entry(pl->head.prev, struct page, lru);
144 pl->mapped_tail = kmap(page);
148 EXPORT_SYMBOL(ceph_pagelist_truncate);