1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
23d14c5d2SYehuda Sadeh #include <linux/module.h>
33d14c5d2SYehuda Sadeh #include <linux/gfp.h>
4e4339d28SYan, Zheng #include <linux/slab.h>
53d14c5d2SYehuda Sadeh #include <linux/pagemap.h>
63d14c5d2SYehuda Sadeh #include <linux/highmem.h>
73d14c5d2SYehuda Sadeh #include <linux/ceph/pagelist.h>
83d14c5d2SYehuda Sadeh
ceph_pagelist_alloc(gfp_t gfp_flags)933165d47SIlya Dryomov struct ceph_pagelist *ceph_pagelist_alloc(gfp_t gfp_flags)
1033165d47SIlya Dryomov {
1133165d47SIlya Dryomov struct ceph_pagelist *pl;
1233165d47SIlya Dryomov
1333165d47SIlya Dryomov pl = kmalloc(sizeof(*pl), gfp_flags);
1433165d47SIlya Dryomov if (!pl)
1533165d47SIlya Dryomov return NULL;
1633165d47SIlya Dryomov
1733165d47SIlya Dryomov INIT_LIST_HEAD(&pl->head);
1833165d47SIlya Dryomov pl->mapped_tail = NULL;
1933165d47SIlya Dryomov pl->length = 0;
2033165d47SIlya Dryomov pl->room = 0;
2133165d47SIlya Dryomov INIT_LIST_HEAD(&pl->free_list);
2233165d47SIlya Dryomov pl->num_pages_free = 0;
2333165d47SIlya Dryomov refcount_set(&pl->refcnt, 1);
2433165d47SIlya Dryomov
2533165d47SIlya Dryomov return pl;
2633165d47SIlya Dryomov }
2733165d47SIlya Dryomov EXPORT_SYMBOL(ceph_pagelist_alloc);
2833165d47SIlya Dryomov
ceph_pagelist_unmap_tail(struct ceph_pagelist * pl)293d14c5d2SYehuda Sadeh static void ceph_pagelist_unmap_tail(struct ceph_pagelist *pl)
303d14c5d2SYehuda Sadeh {
31ac0b74d8SGreg Farnum if (pl->mapped_tail) {
32ac0b74d8SGreg Farnum struct page *page = list_entry(pl->head.prev, struct page, lru);
333d14c5d2SYehuda Sadeh kunmap(page);
34ac0b74d8SGreg Farnum pl->mapped_tail = NULL;
35ac0b74d8SGreg Farnum }
363d14c5d2SYehuda Sadeh }
373d14c5d2SYehuda Sadeh
ceph_pagelist_release(struct ceph_pagelist * pl)38e4339d28SYan, Zheng void ceph_pagelist_release(struct ceph_pagelist *pl)
393d14c5d2SYehuda Sadeh {
400e1a5ee6SElena Reshetova if (!refcount_dec_and_test(&pl->refcnt))
41e4339d28SYan, Zheng return;
423d14c5d2SYehuda Sadeh ceph_pagelist_unmap_tail(pl);
433d14c5d2SYehuda Sadeh while (!list_empty(&pl->head)) {
443d14c5d2SYehuda Sadeh struct page *page = list_first_entry(&pl->head, struct page,
453d14c5d2SYehuda Sadeh lru);
463d14c5d2SYehuda Sadeh list_del(&page->lru);
473d14c5d2SYehuda Sadeh __free_page(page);
483d14c5d2SYehuda Sadeh }
49ac0b74d8SGreg Farnum ceph_pagelist_free_reserve(pl);
50e4339d28SYan, Zheng kfree(pl);
513d14c5d2SYehuda Sadeh }
523d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_pagelist_release);
533d14c5d2SYehuda Sadeh
ceph_pagelist_addpage(struct ceph_pagelist * pl)543d14c5d2SYehuda Sadeh static int ceph_pagelist_addpage(struct ceph_pagelist *pl)
553d14c5d2SYehuda Sadeh {
56ac0b74d8SGreg Farnum struct page *page;
57ac0b74d8SGreg Farnum
58ac0b74d8SGreg Farnum if (!pl->num_pages_free) {
59ac0b74d8SGreg Farnum page = __page_cache_alloc(GFP_NOFS);
60ac0b74d8SGreg Farnum } else {
61ac0b74d8SGreg Farnum page = list_first_entry(&pl->free_list, struct page, lru);
62ac0b74d8SGreg Farnum list_del(&page->lru);
63240634e9SSage Weil --pl->num_pages_free;
64ac0b74d8SGreg Farnum }
653d14c5d2SYehuda Sadeh if (!page)
663d14c5d2SYehuda Sadeh return -ENOMEM;
673d14c5d2SYehuda Sadeh pl->room += PAGE_SIZE;
683d14c5d2SYehuda Sadeh ceph_pagelist_unmap_tail(pl);
69ac0b74d8SGreg Farnum list_add_tail(&page->lru, &pl->head);
703d14c5d2SYehuda Sadeh pl->mapped_tail = kmap(page);
713d14c5d2SYehuda Sadeh return 0;
723d14c5d2SYehuda Sadeh }
733d14c5d2SYehuda Sadeh
ceph_pagelist_append(struct ceph_pagelist * pl,const void * buf,size_t len)743d14c5d2SYehuda Sadeh int ceph_pagelist_append(struct ceph_pagelist *pl, const void *buf, size_t len)
753d14c5d2SYehuda Sadeh {
763d14c5d2SYehuda Sadeh while (pl->room < len) {
773d14c5d2SYehuda Sadeh size_t bit = pl->room;
783d14c5d2SYehuda Sadeh int ret;
793d14c5d2SYehuda Sadeh
8009cbfeafSKirill A. Shutemov memcpy(pl->mapped_tail + (pl->length & ~PAGE_MASK),
813d14c5d2SYehuda Sadeh buf, bit);
823d14c5d2SYehuda Sadeh pl->length += bit;
833d14c5d2SYehuda Sadeh pl->room -= bit;
843d14c5d2SYehuda Sadeh buf += bit;
853d14c5d2SYehuda Sadeh len -= bit;
863d14c5d2SYehuda Sadeh ret = ceph_pagelist_addpage(pl);
873d14c5d2SYehuda Sadeh if (ret)
883d14c5d2SYehuda Sadeh return ret;
893d14c5d2SYehuda Sadeh }
903d14c5d2SYehuda Sadeh
9109cbfeafSKirill A. Shutemov memcpy(pl->mapped_tail + (pl->length & ~PAGE_MASK), buf, len);
923d14c5d2SYehuda Sadeh pl->length += len;
933d14c5d2SYehuda Sadeh pl->room -= len;
943d14c5d2SYehuda Sadeh return 0;
953d14c5d2SYehuda Sadeh }
963d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_pagelist_append);
97ac0b74d8SGreg Farnum
98ae86b9e3SBen Hutchings /* Allocate enough pages for a pagelist to append the given amount
99*4f886194SJason Wang * of data without allocating.
100ac0b74d8SGreg Farnum * Returns: 0 on success, -ENOMEM on error.
101ac0b74d8SGreg Farnum */
ceph_pagelist_reserve(struct ceph_pagelist * pl,size_t space)102ac0b74d8SGreg Farnum int ceph_pagelist_reserve(struct ceph_pagelist *pl, size_t space)
103ac0b74d8SGreg Farnum {
104ac0b74d8SGreg Farnum if (space <= pl->room)
105ac0b74d8SGreg Farnum return 0;
106ac0b74d8SGreg Farnum space -= pl->room;
107ac0b74d8SGreg Farnum space = (space + PAGE_SIZE - 1) >> PAGE_SHIFT; /* conv to num pages */
108ac0b74d8SGreg Farnum
109ac0b74d8SGreg Farnum while (space > pl->num_pages_free) {
110ac0b74d8SGreg Farnum struct page *page = __page_cache_alloc(GFP_NOFS);
111ac0b74d8SGreg Farnum if (!page)
112ac0b74d8SGreg Farnum return -ENOMEM;
113ac0b74d8SGreg Farnum list_add_tail(&page->lru, &pl->free_list);
114ac0b74d8SGreg Farnum ++pl->num_pages_free;
115ac0b74d8SGreg Farnum }
116ac0b74d8SGreg Farnum return 0;
117ac0b74d8SGreg Farnum }
118ac0b74d8SGreg Farnum EXPORT_SYMBOL(ceph_pagelist_reserve);
119ac0b74d8SGreg Farnum
120ae86b9e3SBen Hutchings /* Free any pages that have been preallocated. */
ceph_pagelist_free_reserve(struct ceph_pagelist * pl)121ac0b74d8SGreg Farnum int ceph_pagelist_free_reserve(struct ceph_pagelist *pl)
122ac0b74d8SGreg Farnum {
123ac0b74d8SGreg Farnum while (!list_empty(&pl->free_list)) {
124ac0b74d8SGreg Farnum struct page *page = list_first_entry(&pl->free_list,
125ac0b74d8SGreg Farnum struct page, lru);
126ac0b74d8SGreg Farnum list_del(&page->lru);
127ac0b74d8SGreg Farnum __free_page(page);
128ac0b74d8SGreg Farnum --pl->num_pages_free;
129ac0b74d8SGreg Farnum }
130ac0b74d8SGreg Farnum BUG_ON(pl->num_pages_free);
131ac0b74d8SGreg Farnum return 0;
132ac0b74d8SGreg Farnum }
133ac0b74d8SGreg Farnum EXPORT_SYMBOL(ceph_pagelist_free_reserve);
134ac0b74d8SGreg Farnum
135ae86b9e3SBen Hutchings /* Create a truncation point. */
ceph_pagelist_set_cursor(struct ceph_pagelist * pl,struct ceph_pagelist_cursor * c)136ac0b74d8SGreg Farnum void ceph_pagelist_set_cursor(struct ceph_pagelist *pl,
137ac0b74d8SGreg Farnum struct ceph_pagelist_cursor *c)
138ac0b74d8SGreg Farnum {
139ac0b74d8SGreg Farnum c->pl = pl;
140ac0b74d8SGreg Farnum c->page_lru = pl->head.prev;
141ac0b74d8SGreg Farnum c->room = pl->room;
142ac0b74d8SGreg Farnum }
143ac0b74d8SGreg Farnum EXPORT_SYMBOL(ceph_pagelist_set_cursor);
144ac0b74d8SGreg Farnum
145ae86b9e3SBen Hutchings /* Truncate a pagelist to the given point. Move extra pages to reserve.
146ac0b74d8SGreg Farnum * This won't sleep.
147ac0b74d8SGreg Farnum * Returns: 0 on success,
148ac0b74d8SGreg Farnum * -EINVAL if the pagelist doesn't match the trunc point pagelist
149ac0b74d8SGreg Farnum */
ceph_pagelist_truncate(struct ceph_pagelist * pl,struct ceph_pagelist_cursor * c)150ac0b74d8SGreg Farnum int ceph_pagelist_truncate(struct ceph_pagelist *pl,
151ac0b74d8SGreg Farnum struct ceph_pagelist_cursor *c)
152ac0b74d8SGreg Farnum {
153ac0b74d8SGreg Farnum struct page *page;
154ac0b74d8SGreg Farnum
155ac0b74d8SGreg Farnum if (pl != c->pl)
156ac0b74d8SGreg Farnum return -EINVAL;
157ac0b74d8SGreg Farnum ceph_pagelist_unmap_tail(pl);
158ac0b74d8SGreg Farnum while (pl->head.prev != c->page_lru) {
159ac0b74d8SGreg Farnum page = list_entry(pl->head.prev, struct page, lru);
160cc4829e5SWei Yongjun /* move from pagelist to reserve */
161cc4829e5SWei Yongjun list_move_tail(&page->lru, &pl->free_list);
162ac0b74d8SGreg Farnum ++pl->num_pages_free;
163ac0b74d8SGreg Farnum }
164ac0b74d8SGreg Farnum pl->room = c->room;
165ac0b74d8SGreg Farnum if (!list_empty(&pl->head)) {
166ac0b74d8SGreg Farnum page = list_entry(pl->head.prev, struct page, lru);
167ac0b74d8SGreg Farnum pl->mapped_tail = kmap(page);
168ac0b74d8SGreg Farnum }
169ac0b74d8SGreg Farnum return 0;
170ac0b74d8SGreg Farnum }
171ac0b74d8SGreg Farnum EXPORT_SYMBOL(ceph_pagelist_truncate);
172