blob: 46a368b6dce5378f3b7a486b60700c52f65a809b [file] [log] [blame]
Sage Weil58bb3b32009-12-23 12:12:31 -08001
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09002#include <linux/gfp.h>
Sage Weil58bb3b32009-12-23 12:12:31 -08003#include <linux/pagemap.h>
4#include <linux/highmem.h>
5
6#include "pagelist.h"
7
Yehuda Sadeh3d4401d2010-09-03 12:57:11 -07008static void ceph_pagelist_unmap_tail(struct ceph_pagelist *pl)
9{
10 struct page *page = list_entry(pl->head.prev, struct page,
11 lru);
12 kunmap(page);
13}
14
Sage Weil58bb3b32009-12-23 12:12:31 -080015int ceph_pagelist_release(struct ceph_pagelist *pl)
16{
17 if (pl->mapped_tail)
Yehuda Sadeh3d4401d2010-09-03 12:57:11 -070018 ceph_pagelist_unmap_tail(pl);
19
Sage Weil58bb3b32009-12-23 12:12:31 -080020 while (!list_empty(&pl->head)) {
21 struct page *page = list_first_entry(&pl->head, struct page,
22 lru);
23 list_del(&page->lru);
24 __free_page(page);
25 }
26 return 0;
27}
28
29static int ceph_pagelist_addpage(struct ceph_pagelist *pl)
30{
Yehuda Sadeh31459fe2010-03-17 13:54:02 -070031 struct page *page = __page_cache_alloc(GFP_NOFS);
Sage Weil58bb3b32009-12-23 12:12:31 -080032 if (!page)
33 return -ENOMEM;
34 pl->room += PAGE_SIZE;
35 list_add_tail(&page->lru, &pl->head);
36 if (pl->mapped_tail)
Yehuda Sadeh3d4401d2010-09-03 12:57:11 -070037 ceph_pagelist_unmap_tail(pl);
Sage Weil58bb3b32009-12-23 12:12:31 -080038 pl->mapped_tail = kmap(page);
39 return 0;
40}
41
42int ceph_pagelist_append(struct ceph_pagelist *pl, void *buf, size_t len)
43{
44 while (pl->room < len) {
45 size_t bit = pl->room;
46 int ret;
47
48 memcpy(pl->mapped_tail + (pl->length & ~PAGE_CACHE_MASK),
49 buf, bit);
50 pl->length += bit;
51 pl->room -= bit;
52 buf += bit;
53 len -= bit;
54 ret = ceph_pagelist_addpage(pl);
55 if (ret)
56 return ret;
57 }
58
59 memcpy(pl->mapped_tail + (pl->length & ~PAGE_CACHE_MASK), buf, len);
60 pl->length += len;
61 pl->room -= len;
62 return 0;
63}