aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/linux/ceph/pagelist.h21
-rw-r--r--net/ceph/pagelist.c106
2 files changed, 118 insertions, 9 deletions
diff --git a/include/linux/ceph/pagelist.h b/include/linux/ceph/pagelist.h
index cc9327aa1c98..9660d6b0a35d 100644
--- a/include/linux/ceph/pagelist.h
+++ b/include/linux/ceph/pagelist.h
@@ -8,6 +8,14 @@ struct ceph_pagelist {
8 void *mapped_tail; 8 void *mapped_tail;
9 size_t length; 9 size_t length;
10 size_t room; 10 size_t room;
11 struct list_head free_list;
12 size_t num_pages_free;
13};
14
15struct ceph_pagelist_cursor {
16 struct ceph_pagelist *pl; /* pagelist, for error checking */
17 struct list_head *page_lru; /* page in list */
18 size_t room; /* room remaining to reset to */
11}; 19};
12 20
13static inline void ceph_pagelist_init(struct ceph_pagelist *pl) 21static inline void ceph_pagelist_init(struct ceph_pagelist *pl)
@@ -16,11 +24,24 @@ static inline void ceph_pagelist_init(struct ceph_pagelist *pl)
16 pl->mapped_tail = NULL; 24 pl->mapped_tail = NULL;
17 pl->length = 0; 25 pl->length = 0;
18 pl->room = 0; 26 pl->room = 0;
27 INIT_LIST_HEAD(&pl->free_list);
28 pl->num_pages_free = 0;
19} 29}
30
20extern int ceph_pagelist_release(struct ceph_pagelist *pl); 31extern int ceph_pagelist_release(struct ceph_pagelist *pl);
21 32
22extern int ceph_pagelist_append(struct ceph_pagelist *pl, const void *d, size_t l); 33extern int ceph_pagelist_append(struct ceph_pagelist *pl, const void *d, size_t l);
23 34
35extern int ceph_pagelist_reserve(struct ceph_pagelist *pl, size_t space);
36
37extern int ceph_pagelist_free_reserve(struct ceph_pagelist *pl);
38
39extern void ceph_pagelist_set_cursor(struct ceph_pagelist *pl,
40 struct ceph_pagelist_cursor *c);
41
42extern int ceph_pagelist_truncate(struct ceph_pagelist *pl,
43 struct ceph_pagelist_cursor *c);
44
24static inline int ceph_pagelist_encode_64(struct ceph_pagelist *pl, u64 v) 45static inline int ceph_pagelist_encode_64(struct ceph_pagelist *pl, u64 v)
25{ 46{
26 __le64 ev = cpu_to_le64(v); 47 __le64 ev = cpu_to_le64(v);
diff --git a/net/ceph/pagelist.c b/net/ceph/pagelist.c
index 3b146cfac182..b8cbc456d0bb 100644
--- a/net/ceph/pagelist.c
+++ b/net/ceph/pagelist.c
@@ -7,35 +7,42 @@
7 7
8static void ceph_pagelist_unmap_tail(struct ceph_pagelist *pl) 8static void ceph_pagelist_unmap_tail(struct ceph_pagelist *pl)
9{ 9{
10 struct page *page = list_entry(pl->head.prev, struct page, 10 if (pl->mapped_tail) {
11 lru); 11 struct page *page = list_entry(pl->head.prev, struct page, lru);
12 kunmap(page); 12 kunmap(page);
13 pl->mapped_tail = NULL;
14 }
13} 15}
14 16
15int ceph_pagelist_release(struct ceph_pagelist *pl) 17int ceph_pagelist_release(struct ceph_pagelist *pl)
16{ 18{
17 if (pl->mapped_tail) 19 ceph_pagelist_unmap_tail(pl);
18 ceph_pagelist_unmap_tail(pl);
19
20 while (!list_empty(&pl->head)) { 20 while (!list_empty(&pl->head)) {
21 struct page *page = list_first_entry(&pl->head, struct page, 21 struct page *page = list_first_entry(&pl->head, struct page,
22 lru); 22 lru);
23 list_del(&page->lru); 23 list_del(&page->lru);
24 __free_page(page); 24 __free_page(page);
25 } 25 }
26 ceph_pagelist_free_reserve(pl);
26 return 0; 27 return 0;
27} 28}
28EXPORT_SYMBOL(ceph_pagelist_release); 29EXPORT_SYMBOL(ceph_pagelist_release);
29 30
30static int ceph_pagelist_addpage(struct ceph_pagelist *pl) 31static int ceph_pagelist_addpage(struct ceph_pagelist *pl)
31{ 32{
32 struct page *page = __page_cache_alloc(GFP_NOFS); 33 struct page *page;
34
35 if (!pl->num_pages_free) {
36 page = __page_cache_alloc(GFP_NOFS);
37 } else {
38 page = list_first_entry(&pl->free_list, struct page, lru);
39 list_del(&page->lru);
40 }
33 if (!page) 41 if (!page)
34 return -ENOMEM; 42 return -ENOMEM;
35 pl->room += PAGE_SIZE; 43 pl->room += PAGE_SIZE;
44 ceph_pagelist_unmap_tail(pl);
36 list_add_tail(&page->lru, &pl->head); 45 list_add_tail(&page->lru, &pl->head);
37 if (pl->mapped_tail)
38 ceph_pagelist_unmap_tail(pl);
39 pl->mapped_tail = kmap(page); 46 pl->mapped_tail = kmap(page);
40 return 0; 47 return 0;
41} 48}
@@ -63,3 +70,84 @@ int ceph_pagelist_append(struct ceph_pagelist *pl, const void *buf, size_t len)
63 return 0; 70 return 0;
64} 71}
65EXPORT_SYMBOL(ceph_pagelist_append); 72EXPORT_SYMBOL(ceph_pagelist_append);
73
74/**
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.
78 */
79int ceph_pagelist_reserve(struct ceph_pagelist *pl, size_t space)
80{
81 if (space <= pl->room)
82 return 0;
83 space -= pl->room;
84 space = (space + PAGE_SIZE - 1) >> PAGE_SHIFT; /* conv to num pages */
85
86 while (space > pl->num_pages_free) {
87 struct page *page = __page_cache_alloc(GFP_NOFS);
88 if (!page)
89 return -ENOMEM;
90 list_add_tail(&page->lru, &pl->free_list);
91 ++pl->num_pages_free;
92 }
93 return 0;
94}
95EXPORT_SYMBOL(ceph_pagelist_reserve);
96
97/**
98 * Free any pages that have been preallocated.
99 */
100int ceph_pagelist_free_reserve(struct ceph_pagelist *pl)
101{
102 while (!list_empty(&pl->free_list)) {
103 struct page *page = list_first_entry(&pl->free_list,
104 struct page, lru);
105 list_del(&page->lru);
106 __free_page(page);
107 --pl->num_pages_free;
108 }
109 BUG_ON(pl->num_pages_free);
110 return 0;
111}
112EXPORT_SYMBOL(ceph_pagelist_free_reserve);
113
114/**
115 * Create a truncation point.
116 */
117void ceph_pagelist_set_cursor(struct ceph_pagelist *pl,
118 struct ceph_pagelist_cursor *c)
119{
120 c->pl = pl;
121 c->page_lru = pl->head.prev;
122 c->room = pl->room;
123}
124EXPORT_SYMBOL(ceph_pagelist_set_cursor);
125
126/**
127 * Truncate a pagelist to the given point. Move extra pages to reserve.
128 * This won't sleep.
129 * Returns: 0 on success,
130 * -EINVAL if the pagelist doesn't match the trunc point pagelist
131 */
132int ceph_pagelist_truncate(struct ceph_pagelist *pl,
133 struct ceph_pagelist_cursor *c)
134{
135 struct page *page;
136
137 if (pl != c->pl)
138 return -EINVAL;
139 ceph_pagelist_unmap_tail(pl);
140 while (pl->head.prev != c->page_lru) {
141 page = list_entry(pl->head.prev, struct page, lru);
142 list_del(&page->lru); /* remove from pagelist */
143 list_add_tail(&page->lru, &pl->free_list); /* add to reserve */
144 ++pl->num_pages_free;
145 }
146 pl->room = c->room;
147 if (!list_empty(&pl->head)) {
148 page = list_entry(pl->head.prev, struct page, lru);
149 pl->mapped_tail = kmap(page);
150 }
151 return 0;
152}
153EXPORT_SYMBOL(ceph_pagelist_truncate);