blob: 2ea0564771d2d4428be2a3e9642056b85d3b306f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09003#include <linux/gfp.h>
Yan, Zhenge4339d282014-09-16 17:50:45 +08004#include <linux/slab.h>
Sage Weil58bb3b32009-12-23 12:12:31 -08005#include <linux/pagemap.h>
6#include <linux/highmem.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07007#include <linux/ceph/pagelist.h>
Sage Weil58bb3b32009-12-23 12:12:31 -08008
Yehuda Sadeh3d4401d2010-09-03 12:57:11 -07009static void ceph_pagelist_unmap_tail(struct ceph_pagelist *pl)
10{
Greg Farnumac0b74d2010-09-17 10:10:55 -070011 if (pl->mapped_tail) {
12 struct page *page = list_entry(pl->head.prev, struct page, lru);
13 kunmap(page);
14 pl->mapped_tail = NULL;
15 }
Yehuda Sadeh3d4401d2010-09-03 12:57:11 -070016}
17
Yan, Zhenge4339d282014-09-16 17:50:45 +080018void ceph_pagelist_release(struct ceph_pagelist *pl)
Sage Weil58bb3b32009-12-23 12:12:31 -080019{
Elena Reshetova0e1a5ee2017-03-17 14:10:29 +020020 if (!refcount_dec_and_test(&pl->refcnt))
Yan, Zhenge4339d282014-09-16 17:50:45 +080021 return;
Greg Farnumac0b74d2010-09-17 10:10:55 -070022 ceph_pagelist_unmap_tail(pl);
Sage Weil58bb3b32009-12-23 12:12:31 -080023 while (!list_empty(&pl->head)) {
24 struct page *page = list_first_entry(&pl->head, struct page,
25 lru);
26 list_del(&page->lru);
27 __free_page(page);
28 }
Greg Farnumac0b74d2010-09-17 10:10:55 -070029 ceph_pagelist_free_reserve(pl);
Yan, Zhenge4339d282014-09-16 17:50:45 +080030 kfree(pl);
Sage Weil58bb3b32009-12-23 12:12:31 -080031}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070032EXPORT_SYMBOL(ceph_pagelist_release);
Sage Weil58bb3b32009-12-23 12:12:31 -080033
34static int ceph_pagelist_addpage(struct ceph_pagelist *pl)
35{
Greg Farnumac0b74d2010-09-17 10:10:55 -070036 struct page *page;
37
38 if (!pl->num_pages_free) {
39 page = __page_cache_alloc(GFP_NOFS);
40 } else {
41 page = list_first_entry(&pl->free_list, struct page, lru);
42 list_del(&page->lru);
Sage Weil240634e2010-10-05 12:03:23 -070043 --pl->num_pages_free;
Greg Farnumac0b74d2010-09-17 10:10:55 -070044 }
Sage Weil58bb3b32009-12-23 12:12:31 -080045 if (!page)
46 return -ENOMEM;
47 pl->room += PAGE_SIZE;
Greg Farnumac0b74d2010-09-17 10:10:55 -070048 ceph_pagelist_unmap_tail(pl);
Sage Weil58bb3b32009-12-23 12:12:31 -080049 list_add_tail(&page->lru, &pl->head);
Sage Weil58bb3b32009-12-23 12:12:31 -080050 pl->mapped_tail = kmap(page);
51 return 0;
52}
53
Yehuda Sadeh68b44762010-04-06 15:01:27 -070054int ceph_pagelist_append(struct ceph_pagelist *pl, const void *buf, size_t len)
Sage Weil58bb3b32009-12-23 12:12:31 -080055{
56 while (pl->room < len) {
57 size_t bit = pl->room;
58 int ret;
59
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030060 memcpy(pl->mapped_tail + (pl->length & ~PAGE_MASK),
Sage Weil58bb3b32009-12-23 12:12:31 -080061 buf, bit);
62 pl->length += bit;
63 pl->room -= bit;
64 buf += bit;
65 len -= bit;
66 ret = ceph_pagelist_addpage(pl);
67 if (ret)
68 return ret;
69 }
70
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030071 memcpy(pl->mapped_tail + (pl->length & ~PAGE_MASK), buf, len);
Sage Weil58bb3b32009-12-23 12:12:31 -080072 pl->length += len;
73 pl->room -= len;
74 return 0;
75}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070076EXPORT_SYMBOL(ceph_pagelist_append);
Greg Farnumac0b74d2010-09-17 10:10:55 -070077
Ben Hutchingsae86b9e2012-07-10 10:55:35 +000078/* Allocate enough pages for a pagelist to append the given amount
Greg Farnumac0b74d2010-09-17 10:10:55 -070079 * of data without without allocating.
80 * Returns: 0 on success, -ENOMEM on error.
81 */
82int ceph_pagelist_reserve(struct ceph_pagelist *pl, size_t space)
83{
84 if (space <= pl->room)
85 return 0;
86 space -= pl->room;
87 space = (space + PAGE_SIZE - 1) >> PAGE_SHIFT; /* conv to num pages */
88
89 while (space > pl->num_pages_free) {
90 struct page *page = __page_cache_alloc(GFP_NOFS);
91 if (!page)
92 return -ENOMEM;
93 list_add_tail(&page->lru, &pl->free_list);
94 ++pl->num_pages_free;
95 }
96 return 0;
97}
98EXPORT_SYMBOL(ceph_pagelist_reserve);
99
Ben Hutchingsae86b9e2012-07-10 10:55:35 +0000100/* Free any pages that have been preallocated. */
Greg Farnumac0b74d2010-09-17 10:10:55 -0700101int ceph_pagelist_free_reserve(struct ceph_pagelist *pl)
102{
103 while (!list_empty(&pl->free_list)) {
104 struct page *page = list_first_entry(&pl->free_list,
105 struct page, lru);
106 list_del(&page->lru);
107 __free_page(page);
108 --pl->num_pages_free;
109 }
110 BUG_ON(pl->num_pages_free);
111 return 0;
112}
113EXPORT_SYMBOL(ceph_pagelist_free_reserve);
114
Ben Hutchingsae86b9e2012-07-10 10:55:35 +0000115/* Create a truncation point. */
Greg Farnumac0b74d2010-09-17 10:10:55 -0700116void ceph_pagelist_set_cursor(struct ceph_pagelist *pl,
117 struct ceph_pagelist_cursor *c)
118{
119 c->pl = pl;
120 c->page_lru = pl->head.prev;
121 c->room = pl->room;
122}
123EXPORT_SYMBOL(ceph_pagelist_set_cursor);
124
Ben Hutchingsae86b9e2012-07-10 10:55:35 +0000125/* Truncate a pagelist to the given point. Move extra pages to reserve.
Greg Farnumac0b74d2010-09-17 10:10:55 -0700126 * This won't sleep.
127 * Returns: 0 on success,
128 * -EINVAL if the pagelist doesn't match the trunc point pagelist
129 */
130int ceph_pagelist_truncate(struct ceph_pagelist *pl,
131 struct ceph_pagelist_cursor *c)
132{
133 struct page *page;
134
135 if (pl != c->pl)
136 return -EINVAL;
137 ceph_pagelist_unmap_tail(pl);
138 while (pl->head.prev != c->page_lru) {
139 page = list_entry(pl->head.prev, struct page, lru);
Wei Yongjuncc4829e2012-09-05 14:34:32 +0800140 /* move from pagelist to reserve */
141 list_move_tail(&page->lru, &pl->free_list);
Greg Farnumac0b74d2010-09-17 10:10:55 -0700142 ++pl->num_pages_free;
143 }
144 pl->room = c->room;
145 if (!list_empty(&pl->head)) {
146 page = list_entry(pl->head.prev, struct page, lru);
147 pl->mapped_tail = kmap(page);
148 }
149 return 0;
150}
151EXPORT_SYMBOL(ceph_pagelist_truncate);