Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 1 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 2 | #include <linux/gfp.h> |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 3 | #include <linux/pagemap.h> |
| 4 | #include <linux/highmem.h> |
| 5 | |
| 6 | #include "pagelist.h" |
| 7 | |
| 8 | int ceph_pagelist_release(struct ceph_pagelist *pl) |
| 9 | { |
| 10 | if (pl->mapped_tail) |
| 11 | kunmap(pl->mapped_tail); |
| 12 | while (!list_empty(&pl->head)) { |
| 13 | struct page *page = list_first_entry(&pl->head, struct page, |
| 14 | lru); |
| 15 | list_del(&page->lru); |
| 16 | __free_page(page); |
| 17 | } |
| 18 | return 0; |
| 19 | } |
| 20 | |
| 21 | static int ceph_pagelist_addpage(struct ceph_pagelist *pl) |
| 22 | { |
Yehuda Sadeh | 31459fe | 2010-03-17 13:54:02 -0700 | [diff] [blame] | 23 | struct page *page = __page_cache_alloc(GFP_NOFS); |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 24 | if (!page) |
| 25 | return -ENOMEM; |
| 26 | pl->room += PAGE_SIZE; |
| 27 | list_add_tail(&page->lru, &pl->head); |
| 28 | if (pl->mapped_tail) |
| 29 | kunmap(pl->mapped_tail); |
| 30 | pl->mapped_tail = kmap(page); |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | int ceph_pagelist_append(struct ceph_pagelist *pl, void *buf, size_t len) |
| 35 | { |
| 36 | while (pl->room < len) { |
| 37 | size_t bit = pl->room; |
| 38 | int ret; |
| 39 | |
| 40 | memcpy(pl->mapped_tail + (pl->length & ~PAGE_CACHE_MASK), |
| 41 | buf, bit); |
| 42 | pl->length += bit; |
| 43 | pl->room -= bit; |
| 44 | buf += bit; |
| 45 | len -= bit; |
| 46 | ret = ceph_pagelist_addpage(pl); |
| 47 | if (ret) |
| 48 | return ret; |
| 49 | } |
| 50 | |
| 51 | memcpy(pl->mapped_tail + (pl->length & ~PAGE_CACHE_MASK), buf, len); |
| 52 | pl->length += len; |
| 53 | pl->room -= len; |
| 54 | return 0; |
| 55 | } |