Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 3 | #include <linux/gfp.h> |
Yan, Zheng | e4339d28 | 2014-09-16 17:50:45 +0800 | [diff] [blame] | 4 | #include <linux/slab.h> |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 5 | #include <linux/pagemap.h> |
| 6 | #include <linux/highmem.h> |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 7 | #include <linux/ceph/pagelist.h> |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 8 | |
Ilya Dryomov | 33165d4 | 2018-09-28 15:38:34 +0200 | [diff] [blame] | 9 | struct ceph_pagelist *ceph_pagelist_alloc(gfp_t gfp_flags) |
| 10 | { |
| 11 | struct ceph_pagelist *pl; |
| 12 | |
| 13 | pl = kmalloc(sizeof(*pl), gfp_flags); |
| 14 | if (!pl) |
| 15 | return NULL; |
| 16 | |
| 17 | INIT_LIST_HEAD(&pl->head); |
| 18 | pl->mapped_tail = NULL; |
| 19 | pl->length = 0; |
| 20 | pl->room = 0; |
| 21 | INIT_LIST_HEAD(&pl->free_list); |
| 22 | pl->num_pages_free = 0; |
| 23 | refcount_set(&pl->refcnt, 1); |
| 24 | |
| 25 | return pl; |
| 26 | } |
| 27 | EXPORT_SYMBOL(ceph_pagelist_alloc); |
| 28 | |
Yehuda Sadeh | 3d4401d | 2010-09-03 12:57:11 -0700 | [diff] [blame] | 29 | static void ceph_pagelist_unmap_tail(struct ceph_pagelist *pl) |
| 30 | { |
Greg Farnum | ac0b74d | 2010-09-17 10:10:55 -0700 | [diff] [blame] | 31 | if (pl->mapped_tail) { |
| 32 | struct page *page = list_entry(pl->head.prev, struct page, lru); |
| 33 | kunmap(page); |
| 34 | pl->mapped_tail = NULL; |
| 35 | } |
Yehuda Sadeh | 3d4401d | 2010-09-03 12:57:11 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Yan, Zheng | e4339d28 | 2014-09-16 17:50:45 +0800 | [diff] [blame] | 38 | void ceph_pagelist_release(struct ceph_pagelist *pl) |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 39 | { |
Elena Reshetova | 0e1a5ee | 2017-03-17 14:10:29 +0200 | [diff] [blame] | 40 | if (!refcount_dec_and_test(&pl->refcnt)) |
Yan, Zheng | e4339d28 | 2014-09-16 17:50:45 +0800 | [diff] [blame] | 41 | return; |
Greg Farnum | ac0b74d | 2010-09-17 10:10:55 -0700 | [diff] [blame] | 42 | ceph_pagelist_unmap_tail(pl); |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 43 | while (!list_empty(&pl->head)) { |
| 44 | struct page *page = list_first_entry(&pl->head, struct page, |
| 45 | lru); |
| 46 | list_del(&page->lru); |
| 47 | __free_page(page); |
| 48 | } |
Greg Farnum | ac0b74d | 2010-09-17 10:10:55 -0700 | [diff] [blame] | 49 | ceph_pagelist_free_reserve(pl); |
Yan, Zheng | e4339d28 | 2014-09-16 17:50:45 +0800 | [diff] [blame] | 50 | kfree(pl); |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 51 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 52 | EXPORT_SYMBOL(ceph_pagelist_release); |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 53 | |
| 54 | static int ceph_pagelist_addpage(struct ceph_pagelist *pl) |
| 55 | { |
Greg Farnum | ac0b74d | 2010-09-17 10:10:55 -0700 | [diff] [blame] | 56 | struct page *page; |
| 57 | |
| 58 | if (!pl->num_pages_free) { |
| 59 | page = __page_cache_alloc(GFP_NOFS); |
| 60 | } else { |
| 61 | page = list_first_entry(&pl->free_list, struct page, lru); |
| 62 | list_del(&page->lru); |
Sage Weil | 240634e | 2010-10-05 12:03:23 -0700 | [diff] [blame] | 63 | --pl->num_pages_free; |
Greg Farnum | ac0b74d | 2010-09-17 10:10:55 -0700 | [diff] [blame] | 64 | } |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 65 | if (!page) |
| 66 | return -ENOMEM; |
| 67 | pl->room += PAGE_SIZE; |
Greg Farnum | ac0b74d | 2010-09-17 10:10:55 -0700 | [diff] [blame] | 68 | ceph_pagelist_unmap_tail(pl); |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 69 | list_add_tail(&page->lru, &pl->head); |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 70 | pl->mapped_tail = kmap(page); |
| 71 | return 0; |
| 72 | } |
| 73 | |
Yehuda Sadeh | 68b4476 | 2010-04-06 15:01:27 -0700 | [diff] [blame] | 74 | int ceph_pagelist_append(struct ceph_pagelist *pl, const void *buf, size_t len) |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 75 | { |
| 76 | while (pl->room < len) { |
| 77 | size_t bit = pl->room; |
| 78 | int ret; |
| 79 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 80 | memcpy(pl->mapped_tail + (pl->length & ~PAGE_MASK), |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 81 | buf, bit); |
| 82 | pl->length += bit; |
| 83 | pl->room -= bit; |
| 84 | buf += bit; |
| 85 | len -= bit; |
| 86 | ret = ceph_pagelist_addpage(pl); |
| 87 | if (ret) |
| 88 | return ret; |
| 89 | } |
| 90 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 91 | memcpy(pl->mapped_tail + (pl->length & ~PAGE_MASK), buf, len); |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 92 | pl->length += len; |
| 93 | pl->room -= len; |
| 94 | return 0; |
| 95 | } |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 96 | EXPORT_SYMBOL(ceph_pagelist_append); |
Greg Farnum | ac0b74d | 2010-09-17 10:10:55 -0700 | [diff] [blame] | 97 | |
Ben Hutchings | ae86b9e | 2012-07-10 10:55:35 +0000 | [diff] [blame] | 98 | /* Allocate enough pages for a pagelist to append the given amount |
Greg Farnum | ac0b74d | 2010-09-17 10:10:55 -0700 | [diff] [blame] | 99 | * of data without without allocating. |
| 100 | * Returns: 0 on success, -ENOMEM on error. |
| 101 | */ |
| 102 | int ceph_pagelist_reserve(struct ceph_pagelist *pl, size_t space) |
| 103 | { |
| 104 | if (space <= pl->room) |
| 105 | return 0; |
| 106 | space -= pl->room; |
| 107 | space = (space + PAGE_SIZE - 1) >> PAGE_SHIFT; /* conv to num pages */ |
| 108 | |
| 109 | while (space > pl->num_pages_free) { |
| 110 | struct page *page = __page_cache_alloc(GFP_NOFS); |
| 111 | if (!page) |
| 112 | return -ENOMEM; |
| 113 | list_add_tail(&page->lru, &pl->free_list); |
| 114 | ++pl->num_pages_free; |
| 115 | } |
| 116 | return 0; |
| 117 | } |
| 118 | EXPORT_SYMBOL(ceph_pagelist_reserve); |
| 119 | |
Ben Hutchings | ae86b9e | 2012-07-10 10:55:35 +0000 | [diff] [blame] | 120 | /* Free any pages that have been preallocated. */ |
Greg Farnum | ac0b74d | 2010-09-17 10:10:55 -0700 | [diff] [blame] | 121 | int ceph_pagelist_free_reserve(struct ceph_pagelist *pl) |
| 122 | { |
| 123 | while (!list_empty(&pl->free_list)) { |
| 124 | struct page *page = list_first_entry(&pl->free_list, |
| 125 | struct page, lru); |
| 126 | list_del(&page->lru); |
| 127 | __free_page(page); |
| 128 | --pl->num_pages_free; |
| 129 | } |
| 130 | BUG_ON(pl->num_pages_free); |
| 131 | return 0; |
| 132 | } |
| 133 | EXPORT_SYMBOL(ceph_pagelist_free_reserve); |
| 134 | |
Ben Hutchings | ae86b9e | 2012-07-10 10:55:35 +0000 | [diff] [blame] | 135 | /* Create a truncation point. */ |
Greg Farnum | ac0b74d | 2010-09-17 10:10:55 -0700 | [diff] [blame] | 136 | void ceph_pagelist_set_cursor(struct ceph_pagelist *pl, |
| 137 | struct ceph_pagelist_cursor *c) |
| 138 | { |
| 139 | c->pl = pl; |
| 140 | c->page_lru = pl->head.prev; |
| 141 | c->room = pl->room; |
| 142 | } |
| 143 | EXPORT_SYMBOL(ceph_pagelist_set_cursor); |
| 144 | |
Ben Hutchings | ae86b9e | 2012-07-10 10:55:35 +0000 | [diff] [blame] | 145 | /* Truncate a pagelist to the given point. Move extra pages to reserve. |
Greg Farnum | ac0b74d | 2010-09-17 10:10:55 -0700 | [diff] [blame] | 146 | * This won't sleep. |
| 147 | * Returns: 0 on success, |
| 148 | * -EINVAL if the pagelist doesn't match the trunc point pagelist |
| 149 | */ |
| 150 | int ceph_pagelist_truncate(struct ceph_pagelist *pl, |
| 151 | struct ceph_pagelist_cursor *c) |
| 152 | { |
| 153 | struct page *page; |
| 154 | |
| 155 | if (pl != c->pl) |
| 156 | return -EINVAL; |
| 157 | ceph_pagelist_unmap_tail(pl); |
| 158 | while (pl->head.prev != c->page_lru) { |
| 159 | page = list_entry(pl->head.prev, struct page, lru); |
Wei Yongjun | cc4829e | 2012-09-05 14:34:32 +0800 | [diff] [blame] | 160 | /* move from pagelist to reserve */ |
| 161 | list_move_tail(&page->lru, &pl->free_list); |
Greg Farnum | ac0b74d | 2010-09-17 10:10:55 -0700 | [diff] [blame] | 162 | ++pl->num_pages_free; |
| 163 | } |
| 164 | pl->room = c->room; |
| 165 | if (!list_empty(&pl->head)) { |
| 166 | page = list_entry(pl->head.prev, struct page, lru); |
| 167 | pl->mapped_tail = kmap(page); |
| 168 | } |
| 169 | return 0; |
| 170 | } |
| 171 | EXPORT_SYMBOL(ceph_pagelist_truncate); |