rbd: add new snapshots at the tail
[deliverable/linux.git] / net / ceph / pagelist.c
CommitLineData
58bb3b37 1
3d14c5d2 2#include <linux/module.h>
5a0e3ad6 3#include <linux/gfp.h>
58bb3b37
SW
4#include <linux/pagemap.h>
5#include <linux/highmem.h>
3d14c5d2 6#include <linux/ceph/pagelist.h>
58bb3b37 7
3d4401d9
YS
8static void ceph_pagelist_unmap_tail(struct ceph_pagelist *pl)
9{
ac0b74d8
GF
10 if (pl->mapped_tail) {
11 struct page *page = list_entry(pl->head.prev, struct page, lru);
12 kunmap(page);
13 pl->mapped_tail = NULL;
14 }
3d4401d9
YS
15}
16
58bb3b37
SW
17int ceph_pagelist_release(struct ceph_pagelist *pl)
18{
ac0b74d8 19 ceph_pagelist_unmap_tail(pl);
58bb3b37
SW
20 while (!list_empty(&pl->head)) {
21 struct page *page = list_first_entry(&pl->head, struct page,
22 lru);
23 list_del(&page->lru);
24 __free_page(page);
25 }
ac0b74d8 26 ceph_pagelist_free_reserve(pl);
58bb3b37
SW
27 return 0;
28}
3d14c5d2 29EXPORT_SYMBOL(ceph_pagelist_release);
58bb3b37
SW
30
31static int ceph_pagelist_addpage(struct ceph_pagelist *pl)
32{
ac0b74d8
GF
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);
240634e9 40 --pl->num_pages_free;
ac0b74d8 41 }
58bb3b37
SW
42 if (!page)
43 return -ENOMEM;
44 pl->room += PAGE_SIZE;
ac0b74d8 45 ceph_pagelist_unmap_tail(pl);
58bb3b37 46 list_add_tail(&page->lru, &pl->head);
58bb3b37
SW
47 pl->mapped_tail = kmap(page);
48 return 0;
49}
50
68b4476b 51int ceph_pagelist_append(struct ceph_pagelist *pl, const void *buf, size_t len)
58bb3b37
SW
52{
53 while (pl->room < len) {
54 size_t bit = pl->room;
55 int ret;
56
57 memcpy(pl->mapped_tail + (pl->length & ~PAGE_CACHE_MASK),
58 buf, bit);
59 pl->length += bit;
60 pl->room -= bit;
61 buf += bit;
62 len -= bit;
63 ret = ceph_pagelist_addpage(pl);
64 if (ret)
65 return ret;
66 }
67
68 memcpy(pl->mapped_tail + (pl->length & ~PAGE_CACHE_MASK), buf, len);
69 pl->length += len;
70 pl->room -= len;
71 return 0;
72}
3d14c5d2 73EXPORT_SYMBOL(ceph_pagelist_append);
ac0b74d8 74
ae86b9e3 75/* Allocate enough pages for a pagelist to append the given amount
ac0b74d8
GF
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
ae86b9e3 97/* Free any pages that have been preallocated. */
ac0b74d8
GF
98int ceph_pagelist_free_reserve(struct ceph_pagelist *pl)
99{
100 while (!list_empty(&pl->free_list)) {
101 struct page *page = list_first_entry(&pl->free_list,
102 struct page, lru);
103 list_del(&page->lru);
104 __free_page(page);
105 --pl->num_pages_free;
106 }
107 BUG_ON(pl->num_pages_free);
108 return 0;
109}
110EXPORT_SYMBOL(ceph_pagelist_free_reserve);
111
ae86b9e3 112/* Create a truncation point. */
ac0b74d8
GF
113void ceph_pagelist_set_cursor(struct ceph_pagelist *pl,
114 struct ceph_pagelist_cursor *c)
115{
116 c->pl = pl;
117 c->page_lru = pl->head.prev;
118 c->room = pl->room;
119}
120EXPORT_SYMBOL(ceph_pagelist_set_cursor);
121
ae86b9e3 122/* Truncate a pagelist to the given point. Move extra pages to reserve.
ac0b74d8
GF
123 * This won't sleep.
124 * Returns: 0 on success,
125 * -EINVAL if the pagelist doesn't match the trunc point pagelist
126 */
127int ceph_pagelist_truncate(struct ceph_pagelist *pl,
128 struct ceph_pagelist_cursor *c)
129{
130 struct page *page;
131
132 if (pl != c->pl)
133 return -EINVAL;
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;
140 }
141 pl->room = c->room;
142 if (!list_empty(&pl->head)) {
143 page = list_entry(pl->head.prev, struct page, lru);
144 pl->mapped_tail = kmap(page);
145 }
146 return 0;
147}
148EXPORT_SYMBOL(ceph_pagelist_truncate);
This page took 0.159384 seconds and 5 git commands to generate.