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 | |
Yehuda Sadeh | 3d4401d | 2010-09-03 12:57:11 -0700 | [diff] [blame^] | 8 | static 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 Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 15 | int ceph_pagelist_release(struct ceph_pagelist *pl) |
| 16 | { |
| 17 | if (pl->mapped_tail) |
Yehuda Sadeh | 3d4401d | 2010-09-03 12:57:11 -0700 | [diff] [blame^] | 18 | ceph_pagelist_unmap_tail(pl); |
| 19 | |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 20 | 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 | |
| 29 | static int ceph_pagelist_addpage(struct ceph_pagelist *pl) |
| 30 | { |
Yehuda Sadeh | 31459fe | 2010-03-17 13:54:02 -0700 | [diff] [blame] | 31 | struct page *page = __page_cache_alloc(GFP_NOFS); |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 32 | if (!page) |
| 33 | return -ENOMEM; |
| 34 | pl->room += PAGE_SIZE; |
| 35 | list_add_tail(&page->lru, &pl->head); |
| 36 | if (pl->mapped_tail) |
Yehuda Sadeh | 3d4401d | 2010-09-03 12:57:11 -0700 | [diff] [blame^] | 37 | ceph_pagelist_unmap_tail(pl); |
Sage Weil | 58bb3b3 | 2009-12-23 12:12:31 -0800 | [diff] [blame] | 38 | pl->mapped_tail = kmap(page); |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | int 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 | } |