18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci#include <linux/module.h> 38c2ecf20Sopenharmony_ci#include <linux/gfp.h> 48c2ecf20Sopenharmony_ci#include <linux/slab.h> 58c2ecf20Sopenharmony_ci#include <linux/pagemap.h> 68c2ecf20Sopenharmony_ci#include <linux/highmem.h> 78c2ecf20Sopenharmony_ci#include <linux/ceph/pagelist.h> 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_cistruct ceph_pagelist *ceph_pagelist_alloc(gfp_t gfp_flags) 108c2ecf20Sopenharmony_ci{ 118c2ecf20Sopenharmony_ci struct ceph_pagelist *pl; 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci pl = kmalloc(sizeof(*pl), gfp_flags); 148c2ecf20Sopenharmony_ci if (!pl) 158c2ecf20Sopenharmony_ci return NULL; 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&pl->head); 188c2ecf20Sopenharmony_ci pl->mapped_tail = NULL; 198c2ecf20Sopenharmony_ci pl->length = 0; 208c2ecf20Sopenharmony_ci pl->room = 0; 218c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&pl->free_list); 228c2ecf20Sopenharmony_ci pl->num_pages_free = 0; 238c2ecf20Sopenharmony_ci refcount_set(&pl->refcnt, 1); 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci return pl; 268c2ecf20Sopenharmony_ci} 278c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ceph_pagelist_alloc); 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_cistatic void ceph_pagelist_unmap_tail(struct ceph_pagelist *pl) 308c2ecf20Sopenharmony_ci{ 318c2ecf20Sopenharmony_ci if (pl->mapped_tail) { 328c2ecf20Sopenharmony_ci struct page *page = list_entry(pl->head.prev, struct page, lru); 338c2ecf20Sopenharmony_ci kunmap(page); 348c2ecf20Sopenharmony_ci pl->mapped_tail = NULL; 358c2ecf20Sopenharmony_ci } 368c2ecf20Sopenharmony_ci} 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_civoid ceph_pagelist_release(struct ceph_pagelist *pl) 398c2ecf20Sopenharmony_ci{ 408c2ecf20Sopenharmony_ci if (!refcount_dec_and_test(&pl->refcnt)) 418c2ecf20Sopenharmony_ci return; 428c2ecf20Sopenharmony_ci ceph_pagelist_unmap_tail(pl); 438c2ecf20Sopenharmony_ci while (!list_empty(&pl->head)) { 448c2ecf20Sopenharmony_ci struct page *page = list_first_entry(&pl->head, struct page, 458c2ecf20Sopenharmony_ci lru); 468c2ecf20Sopenharmony_ci list_del(&page->lru); 478c2ecf20Sopenharmony_ci __free_page(page); 488c2ecf20Sopenharmony_ci } 498c2ecf20Sopenharmony_ci ceph_pagelist_free_reserve(pl); 508c2ecf20Sopenharmony_ci kfree(pl); 518c2ecf20Sopenharmony_ci} 528c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ceph_pagelist_release); 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_cistatic int ceph_pagelist_addpage(struct ceph_pagelist *pl) 558c2ecf20Sopenharmony_ci{ 568c2ecf20Sopenharmony_ci struct page *page; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci if (!pl->num_pages_free) { 598c2ecf20Sopenharmony_ci page = __page_cache_alloc(GFP_NOFS); 608c2ecf20Sopenharmony_ci } else { 618c2ecf20Sopenharmony_ci page = list_first_entry(&pl->free_list, struct page, lru); 628c2ecf20Sopenharmony_ci list_del(&page->lru); 638c2ecf20Sopenharmony_ci --pl->num_pages_free; 648c2ecf20Sopenharmony_ci } 658c2ecf20Sopenharmony_ci if (!page) 668c2ecf20Sopenharmony_ci return -ENOMEM; 678c2ecf20Sopenharmony_ci pl->room += PAGE_SIZE; 688c2ecf20Sopenharmony_ci ceph_pagelist_unmap_tail(pl); 698c2ecf20Sopenharmony_ci list_add_tail(&page->lru, &pl->head); 708c2ecf20Sopenharmony_ci pl->mapped_tail = kmap(page); 718c2ecf20Sopenharmony_ci return 0; 728c2ecf20Sopenharmony_ci} 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ciint ceph_pagelist_append(struct ceph_pagelist *pl, const void *buf, size_t len) 758c2ecf20Sopenharmony_ci{ 768c2ecf20Sopenharmony_ci while (pl->room < len) { 778c2ecf20Sopenharmony_ci size_t bit = pl->room; 788c2ecf20Sopenharmony_ci int ret; 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci memcpy(pl->mapped_tail + (pl->length & ~PAGE_MASK), 818c2ecf20Sopenharmony_ci buf, bit); 828c2ecf20Sopenharmony_ci pl->length += bit; 838c2ecf20Sopenharmony_ci pl->room -= bit; 848c2ecf20Sopenharmony_ci buf += bit; 858c2ecf20Sopenharmony_ci len -= bit; 868c2ecf20Sopenharmony_ci ret = ceph_pagelist_addpage(pl); 878c2ecf20Sopenharmony_ci if (ret) 888c2ecf20Sopenharmony_ci return ret; 898c2ecf20Sopenharmony_ci } 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci memcpy(pl->mapped_tail + (pl->length & ~PAGE_MASK), buf, len); 928c2ecf20Sopenharmony_ci pl->length += len; 938c2ecf20Sopenharmony_ci pl->room -= len; 948c2ecf20Sopenharmony_ci return 0; 958c2ecf20Sopenharmony_ci} 968c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ceph_pagelist_append); 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci/* Allocate enough pages for a pagelist to append the given amount 998c2ecf20Sopenharmony_ci * of data without without allocating. 1008c2ecf20Sopenharmony_ci * Returns: 0 on success, -ENOMEM on error. 1018c2ecf20Sopenharmony_ci */ 1028c2ecf20Sopenharmony_ciint ceph_pagelist_reserve(struct ceph_pagelist *pl, size_t space) 1038c2ecf20Sopenharmony_ci{ 1048c2ecf20Sopenharmony_ci if (space <= pl->room) 1058c2ecf20Sopenharmony_ci return 0; 1068c2ecf20Sopenharmony_ci space -= pl->room; 1078c2ecf20Sopenharmony_ci space = (space + PAGE_SIZE - 1) >> PAGE_SHIFT; /* conv to num pages */ 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci while (space > pl->num_pages_free) { 1108c2ecf20Sopenharmony_ci struct page *page = __page_cache_alloc(GFP_NOFS); 1118c2ecf20Sopenharmony_ci if (!page) 1128c2ecf20Sopenharmony_ci return -ENOMEM; 1138c2ecf20Sopenharmony_ci list_add_tail(&page->lru, &pl->free_list); 1148c2ecf20Sopenharmony_ci ++pl->num_pages_free; 1158c2ecf20Sopenharmony_ci } 1168c2ecf20Sopenharmony_ci return 0; 1178c2ecf20Sopenharmony_ci} 1188c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ceph_pagelist_reserve); 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci/* Free any pages that have been preallocated. */ 1218c2ecf20Sopenharmony_ciint ceph_pagelist_free_reserve(struct ceph_pagelist *pl) 1228c2ecf20Sopenharmony_ci{ 1238c2ecf20Sopenharmony_ci while (!list_empty(&pl->free_list)) { 1248c2ecf20Sopenharmony_ci struct page *page = list_first_entry(&pl->free_list, 1258c2ecf20Sopenharmony_ci struct page, lru); 1268c2ecf20Sopenharmony_ci list_del(&page->lru); 1278c2ecf20Sopenharmony_ci __free_page(page); 1288c2ecf20Sopenharmony_ci --pl->num_pages_free; 1298c2ecf20Sopenharmony_ci } 1308c2ecf20Sopenharmony_ci BUG_ON(pl->num_pages_free); 1318c2ecf20Sopenharmony_ci return 0; 1328c2ecf20Sopenharmony_ci} 1338c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ceph_pagelist_free_reserve); 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci/* Create a truncation point. */ 1368c2ecf20Sopenharmony_civoid ceph_pagelist_set_cursor(struct ceph_pagelist *pl, 1378c2ecf20Sopenharmony_ci struct ceph_pagelist_cursor *c) 1388c2ecf20Sopenharmony_ci{ 1398c2ecf20Sopenharmony_ci c->pl = pl; 1408c2ecf20Sopenharmony_ci c->page_lru = pl->head.prev; 1418c2ecf20Sopenharmony_ci c->room = pl->room; 1428c2ecf20Sopenharmony_ci} 1438c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ceph_pagelist_set_cursor); 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci/* Truncate a pagelist to the given point. Move extra pages to reserve. 1468c2ecf20Sopenharmony_ci * This won't sleep. 1478c2ecf20Sopenharmony_ci * Returns: 0 on success, 1488c2ecf20Sopenharmony_ci * -EINVAL if the pagelist doesn't match the trunc point pagelist 1498c2ecf20Sopenharmony_ci */ 1508c2ecf20Sopenharmony_ciint ceph_pagelist_truncate(struct ceph_pagelist *pl, 1518c2ecf20Sopenharmony_ci struct ceph_pagelist_cursor *c) 1528c2ecf20Sopenharmony_ci{ 1538c2ecf20Sopenharmony_ci struct page *page; 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci if (pl != c->pl) 1568c2ecf20Sopenharmony_ci return -EINVAL; 1578c2ecf20Sopenharmony_ci ceph_pagelist_unmap_tail(pl); 1588c2ecf20Sopenharmony_ci while (pl->head.prev != c->page_lru) { 1598c2ecf20Sopenharmony_ci page = list_entry(pl->head.prev, struct page, lru); 1608c2ecf20Sopenharmony_ci /* move from pagelist to reserve */ 1618c2ecf20Sopenharmony_ci list_move_tail(&page->lru, &pl->free_list); 1628c2ecf20Sopenharmony_ci ++pl->num_pages_free; 1638c2ecf20Sopenharmony_ci } 1648c2ecf20Sopenharmony_ci pl->room = c->room; 1658c2ecf20Sopenharmony_ci if (!list_empty(&pl->head)) { 1668c2ecf20Sopenharmony_ci page = list_entry(pl->head.prev, struct page, lru); 1678c2ecf20Sopenharmony_ci pl->mapped_tail = kmap(page); 1688c2ecf20Sopenharmony_ci } 1698c2ecf20Sopenharmony_ci return 0; 1708c2ecf20Sopenharmony_ci} 1718c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ceph_pagelist_truncate); 172