]>
Commit | Line | Data |
---|---|---|
3d14c5d2 | 1 | #include <linux/module.h> |
5a0e3ad6 | 2 | #include <linux/gfp.h> |
58bb3b37 SW |
3 | #include <linux/pagemap.h> |
4 | #include <linux/highmem.h> | |
3d14c5d2 | 5 | #include <linux/ceph/pagelist.h> |
58bb3b37 | 6 | |
3d4401d9 YS |
7 | static void ceph_pagelist_unmap_tail(struct ceph_pagelist *pl) |
8 | { | |
ac0b74d8 GF |
9 | 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 | } | |
3d4401d9 YS |
14 | } |
15 | ||
58bb3b37 SW |
16 | int ceph_pagelist_release(struct ceph_pagelist *pl) |
17 | { | |
ac0b74d8 | 18 | ceph_pagelist_unmap_tail(pl); |
58bb3b37 SW |
19 | 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 | } | |
ac0b74d8 | 25 | ceph_pagelist_free_reserve(pl); |
58bb3b37 SW |
26 | return 0; |
27 | } | |
3d14c5d2 | 28 | EXPORT_SYMBOL(ceph_pagelist_release); |
58bb3b37 SW |
29 | |
30 | static int ceph_pagelist_addpage(struct ceph_pagelist *pl) | |
31 | { | |
ac0b74d8 GF |
32 | 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); | |
240634e9 | 39 | --pl->num_pages_free; |
ac0b74d8 | 40 | } |
58bb3b37 SW |
41 | if (!page) |
42 | return -ENOMEM; | |
43 | pl->room += PAGE_SIZE; | |
ac0b74d8 | 44 | ceph_pagelist_unmap_tail(pl); |
58bb3b37 | 45 | list_add_tail(&page->lru, &pl->head); |
58bb3b37 SW |
46 | pl->mapped_tail = kmap(page); |
47 | return 0; | |
48 | } | |
49 | ||
68b4476b | 50 | int ceph_pagelist_append(struct ceph_pagelist *pl, const void *buf, size_t len) |
58bb3b37 SW |
51 | { |
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 | } | |
3d14c5d2 | 72 | EXPORT_SYMBOL(ceph_pagelist_append); |
ac0b74d8 | 73 | |
ae86b9e3 | 74 | /* Allocate enough pages for a pagelist to append the given amount |
ac0b74d8 GF |
75 | * of data without without allocating. |
76 | * Returns: 0 on success, -ENOMEM on error. | |
77 | */ | |
78 | int 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 | } | |
94 | EXPORT_SYMBOL(ceph_pagelist_reserve); | |
95 | ||
ae86b9e3 | 96 | /* Free any pages that have been preallocated. */ |
ac0b74d8 GF |
97 | int 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 | } | |
109 | EXPORT_SYMBOL(ceph_pagelist_free_reserve); | |
110 | ||
ae86b9e3 | 111 | /* Create a truncation point. */ |
ac0b74d8 GF |
112 | void 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 | } | |
119 | EXPORT_SYMBOL(ceph_pagelist_set_cursor); | |
120 | ||
ae86b9e3 | 121 | /* Truncate a pagelist to the given point. Move extra pages to reserve. |
ac0b74d8 GF |
122 | * This won't sleep. |
123 | * Returns: 0 on success, | |
124 | * -EINVAL if the pagelist doesn't match the trunc point pagelist | |
125 | */ | |
126 | int 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); | |
cc4829e5 WY |
136 | /* move from pagelist to reserve */ |
137 | list_move_tail(&page->lru, &pl->free_list); | |
ac0b74d8 GF |
138 | ++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 | } | |
147 | EXPORT_SYMBOL(ceph_pagelist_truncate); |