aboutsummaryrefslogtreecommitdiffstats
path: root/net/ceph
diff options
context:
space:
mode:
authorGreg Farnum <gregf@hq.newdream.net>2010-09-17 13:10:55 -0400
committerSage Weil <sage@newdream.net>2010-10-20 18:38:16 -0400
commitac0b74d8a1ced8ea86147467daf06b15b130dd94 (patch)
treea71aec65be7f71b42ae78907acd5b0f4aeafd3a9 /net/ceph
parent18a38193efcaac1fb3c94ad8fa04bb117850a3c2 (diff)
ceph: add pagelist_reserve, pagelist_truncate, pagelist_set_cursor
These facilitate preallocation of pages so that we can encode into the pagelist in an atomic context. Signed-off-by: Greg Farnum <gregf@hq.newdream.net> Signed-off-by: Sage Weil <sage@newdream.net>
Diffstat (limited to 'net/ceph')
-rw-r--r--net/ceph/pagelist.c106
1 files changed, 97 insertions, 9 deletions
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);