pagelist.c 1.4 KB
Newer Older
1

2
#include <linux/module.h>
3
#include <linux/gfp.h>
4 5
#include <linux/pagemap.h>
#include <linux/highmem.h>
6
#include <linux/ceph/pagelist.h>
7

Y
Yehuda Sadeh 已提交
8 9 10 11 12 13 14
static void ceph_pagelist_unmap_tail(struct ceph_pagelist *pl)
{
	struct page *page = list_entry(pl->head.prev, struct page,
				       lru);
	kunmap(page);
}

15 16 17
int ceph_pagelist_release(struct ceph_pagelist *pl)
{
	if (pl->mapped_tail)
Y
Yehuda Sadeh 已提交
18 19
		ceph_pagelist_unmap_tail(pl);

20 21 22 23 24 25 26 27
	while (!list_empty(&pl->head)) {
		struct page *page = list_first_entry(&pl->head, struct page,
						     lru);
		list_del(&page->lru);
		__free_page(page);
	}
	return 0;
}
28
EXPORT_SYMBOL(ceph_pagelist_release);
29 30 31

static int ceph_pagelist_addpage(struct ceph_pagelist *pl)
{
32
	struct page *page = __page_cache_alloc(GFP_NOFS);
33 34 35 36 37
	if (!page)
		return -ENOMEM;
	pl->room += PAGE_SIZE;
	list_add_tail(&page->lru, &pl->head);
	if (pl->mapped_tail)
Y
Yehuda Sadeh 已提交
38
		ceph_pagelist_unmap_tail(pl);
39 40 41 42
	pl->mapped_tail = kmap(page);
	return 0;
}

43
int ceph_pagelist_append(struct ceph_pagelist *pl, const void *buf, size_t len)
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
{
	while (pl->room < len) {
		size_t bit = pl->room;
		int ret;

		memcpy(pl->mapped_tail + (pl->length & ~PAGE_CACHE_MASK),
		       buf, bit);
		pl->length += bit;
		pl->room -= bit;
		buf += bit;
		len -= bit;
		ret = ceph_pagelist_addpage(pl);
		if (ret)
			return ret;
	}

	memcpy(pl->mapped_tail + (pl->length & ~PAGE_CACHE_MASK), buf, len);
	pl->length += len;
	pl->room -= len;
	return 0;
}
65
EXPORT_SYMBOL(ceph_pagelist_append);