blob: 92866bebb65fe2ac438a10c068f18da5a1912018 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09002#include <linux/gfp.h>
Sage Weil58bb3b32009-12-23 12:12:31 -08003#include <linux/pagemap.h>
4#include <linux/highmem.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07005#include <linux/ceph/pagelist.h>
Sage Weil58bb3b32009-12-23 12:12:31 -08006
Yehuda Sadeh3d4401d2010-09-03 12:57:11 -07007static void ceph_pagelist_unmap_tail(struct ceph_pagelist *pl)
8{
Greg Farnumac0b74d2010-09-17 10:10:55 -07009 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 }
Yehuda Sadeh3d4401d2010-09-03 12:57:11 -070014}
15
Sage Weil58bb3b32009-12-23 12:12:31 -080016int ceph_pagelist_release(struct ceph_pagelist *pl)
17{
Greg Farnumac0b74d2010-09-17 10:10:55 -070018 ceph_pagelist_unmap_tail(pl);
Sage Weil58bb3b32009-12-23 12:12:31 -080019 while (!list_empty(&pl->head)) {
20 struct page *page = list_first_entry(&pl->head, struct page,
21 lru);
22 list_del(&page->lru);
23 __free_page(page);
24 }
Greg Farnumac0b74d2010-09-17 10:10:55 -070025 ceph_pagelist_free_reserve(pl);
Sage Weil58bb3b32009-12-23 12:12:31 -080026 return 0;
27}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070028EXPORT_SYMBOL(ceph_pagelist_release);
Sage Weil58bb3b32009-12-23 12:12:31 -080029
30static int ceph_pagelist_addpage(struct ceph_pagelist *pl)
31{
Greg Farnumac0b74d2010-09-17 10:10:55 -070032 struct page *page;
33
34 if (!pl->num_pages_free) {
35 page = __page_cache_alloc(GFP_NOFS);
36 } else {
37 page = list_first_entry(&pl->free_list, struct page, lru);
38 list_del(&page->lru);
Sage Weil240634e2010-10-05 12:03:23 -070039 --pl->num_pages_free;
Greg Farnumac0b74d2010-09-17 10:10:55 -070040 }
Sage Weil58bb3b32009-12-23 12:12:31 -080041 if (!page)
42 return -ENOMEM;
43 pl->room += PAGE_SIZE;
Greg Farnumac0b74d2010-09-17 10:10:55 -070044 ceph_pagelist_unmap_tail(pl);
Sage Weil58bb3b32009-12-23 12:12:31 -080045 list_add_tail(&page->lru, &pl->head);
Sage Weil58bb3b32009-12-23 12:12:31 -080046 pl->mapped_tail = kmap(page);
47 return 0;
48}
49
Yehuda Sadeh68b44762010-04-06 15:01:27 -070050int ceph_pagelist_append(struct ceph_pagelist *pl, const void *buf, size_t len)
Sage Weil58bb3b32009-12-23 12:12:31 -080051{
52 while (pl->room < len) {
53 size_t bit = pl->room;
54 int ret;
55
56 memcpy(pl->mapped_tail + (pl->length & ~PAGE_CACHE_MASK),
57 buf, bit);
58 pl->length += bit;
59 pl->room -= bit;
60 buf += bit;
61 len -= bit;
62 ret = ceph_pagelist_addpage(pl);
63 if (ret)
64 return ret;
65 }
66
67 memcpy(pl->mapped_tail + (pl->length & ~PAGE_CACHE_MASK), buf, len);
68 pl->length += len;
69 pl->room -= len;
70 return 0;
71}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070072EXPORT_SYMBOL(ceph_pagelist_append);
Greg Farnumac0b74d2010-09-17 10:10:55 -070073
Ben Hutchingsae86b9e2012-07-10 10:55:35 +000074/* Allocate enough pages for a pagelist to append the given amount
Greg Farnumac0b74d2010-09-17 10:10:55 -070075 * of data without without allocating.
76 * Returns: 0 on success, -ENOMEM on error.
77 */
78int ceph_pagelist_reserve(struct ceph_pagelist *pl, size_t space)
79{
80 if (space <= pl->room)
81 return 0;
82 space -= pl->room;
83 space = (space + PAGE_SIZE - 1) >> PAGE_SHIFT; /* conv to num pages */
84
85 while (space > pl->num_pages_free) {
86 struct page *page = __page_cache_alloc(GFP_NOFS);
87 if (!page)
88 return -ENOMEM;
89 list_add_tail(&page->lru, &pl->free_list);
90 ++pl->num_pages_free;
91 }
92 return 0;
93}
94EXPORT_SYMBOL(ceph_pagelist_reserve);
95
Ben Hutchingsae86b9e2012-07-10 10:55:35 +000096/* Free any pages that have been preallocated. */
Greg Farnumac0b74d2010-09-17 10:10:55 -070097int ceph_pagelist_free_reserve(struct ceph_pagelist *pl)
98{
99 while (!list_empty(&pl->free_list)) {
100 struct page *page = list_first_entry(&pl->free_list,
101 struct page, lru);
102 list_del(&page->lru);
103 __free_page(page);
104 --pl->num_pages_free;
105 }
106 BUG_ON(pl->num_pages_free);
107 return 0;
108}
109EXPORT_SYMBOL(ceph_pagelist_free_reserve);
110
Ben Hutchingsae86b9e2012-07-10 10:55:35 +0000111/* Create a truncation point. */
Greg Farnumac0b74d2010-09-17 10:10:55 -0700112void ceph_pagelist_set_cursor(struct ceph_pagelist *pl,
113 struct ceph_pagelist_cursor *c)
114{
115 c->pl = pl;
116 c->page_lru = pl->head.prev;
117 c->room = pl->room;
118}
119EXPORT_SYMBOL(ceph_pagelist_set_cursor);
120
Ben Hutchingsae86b9e2012-07-10 10:55:35 +0000121/* Truncate a pagelist to the given point. Move extra pages to reserve.
Greg Farnumac0b74d2010-09-17 10:10:55 -0700122 * This won't sleep.
123 * Returns: 0 on success,
124 * -EINVAL if the pagelist doesn't match the trunc point pagelist
125 */
126int ceph_pagelist_truncate(struct ceph_pagelist *pl,
127 struct ceph_pagelist_cursor *c)
128{
129 struct page *page;
130
131 if (pl != c->pl)
132 return -EINVAL;
133 ceph_pagelist_unmap_tail(pl);
134 while (pl->head.prev != c->page_lru) {
135 page = list_entry(pl->head.prev, struct page, lru);
Wei Yongjuncc4829e2012-09-05 14:34:32 +0800136 /* move from pagelist to reserve */
137 list_move_tail(&page->lru, &pl->free_list);
Greg Farnumac0b74d2010-09-17 10:10:55 -0700138 ++pl->num_pages_free;
139 }
140 pl->room = c->room;
141 if (!list_empty(&pl->head)) {
142 page = list_entry(pl->head.prev, struct page, lru);
143 pl->mapped_tail = kmap(page);
144 }
145 return 0;
146}
147EXPORT_SYMBOL(ceph_pagelist_truncate);