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