~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ceph/pagelist.c

Version: ~ [ linux-5.1-rc1 ] ~ [ linux-5.0.3 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.30 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.107 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.164 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.176 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.136 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.63 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 #include <linux/module.h>
  3 #include <linux/gfp.h>
  4 #include <linux/slab.h>
  5 #include <linux/pagemap.h>
  6 #include <linux/highmem.h>
  7 #include <linux/ceph/pagelist.h>
  8 
  9 static void ceph_pagelist_unmap_tail(struct ceph_pagelist *pl)
 10 {
 11         if (pl->mapped_tail) {
 12                 struct page *page = list_entry(pl->head.prev, struct page, lru);
 13                 kunmap(page);
 14                 pl->mapped_tail = NULL;
 15         }
 16 }
 17 
 18 void ceph_pagelist_release(struct ceph_pagelist *pl)
 19 {
 20         if (!refcount_dec_and_test(&pl->refcnt))
 21                 return;
 22         ceph_pagelist_unmap_tail(pl);
 23         while (!list_empty(&pl->head)) {
 24                 struct page *page = list_first_entry(&pl->head, struct page,
 25                                                      lru);
 26                 list_del(&page->lru);
 27                 __free_page(page);
 28         }
 29         ceph_pagelist_free_reserve(pl);
 30         kfree(pl);
 31 }
 32 EXPORT_SYMBOL(ceph_pagelist_release);
 33 
 34 static int ceph_pagelist_addpage(struct ceph_pagelist *pl)
 35 {
 36         struct page *page;
 37 
 38         if (!pl->num_pages_free) {
 39                 page = __page_cache_alloc(GFP_NOFS);
 40         } else {
 41                 page = list_first_entry(&pl->free_list, struct page, lru);
 42                 list_del(&page->lru);
 43                 --pl->num_pages_free;
 44         }
 45         if (!page)
 46                 return -ENOMEM;
 47         pl->room += PAGE_SIZE;
 48         ceph_pagelist_unmap_tail(pl);
 49         list_add_tail(&page->lru, &pl->head);
 50         pl->mapped_tail = kmap(page);
 51         return 0;
 52 }
 53 
 54 int ceph_pagelist_append(struct ceph_pagelist *pl, const void *buf, size_t len)
 55 {
 56         while (pl->room < len) {
 57                 size_t bit = pl->room;
 58                 int ret;
 59 
 60                 memcpy(pl->mapped_tail + (pl->length & ~PAGE_MASK),
 61                        buf, bit);
 62                 pl->length += bit;
 63                 pl->room -= bit;
 64                 buf += bit;
 65                 len -= bit;
 66                 ret = ceph_pagelist_addpage(pl);
 67                 if (ret)
 68                         return ret;
 69         }
 70 
 71         memcpy(pl->mapped_tail + (pl->length & ~PAGE_MASK), buf, len);
 72         pl->length += len;
 73         pl->room -= len;
 74         return 0;
 75 }
 76 EXPORT_SYMBOL(ceph_pagelist_append);
 77 
 78 /* Allocate enough pages for a pagelist to append the given amount
 79  * of data without without allocating.
 80  * Returns: 0 on success, -ENOMEM on error.
 81  */
 82 int ceph_pagelist_reserve(struct ceph_pagelist *pl, size_t space)
 83 {
 84         if (space <= pl->room)
 85                 return 0;
 86         space -= pl->room;
 87         space = (space + PAGE_SIZE - 1) >> PAGE_SHIFT;   /* conv to num pages */
 88 
 89         while (space > pl->num_pages_free) {
 90                 struct page *page = __page_cache_alloc(GFP_NOFS);
 91                 if (!page)
 92                         return -ENOMEM;
 93                 list_add_tail(&page->lru, &pl->free_list);
 94                 ++pl->num_pages_free;
 95         }
 96         return 0;
 97 }
 98 EXPORT_SYMBOL(ceph_pagelist_reserve);
 99 
100 /* Free any pages that have been preallocated. */
101 int ceph_pagelist_free_reserve(struct ceph_pagelist *pl)
102 {
103         while (!list_empty(&pl->free_list)) {
104                 struct page *page = list_first_entry(&pl->free_list,
105                                                      struct page, lru);
106                 list_del(&page->lru);
107                 __free_page(page);
108                 --pl->num_pages_free;
109         }
110         BUG_ON(pl->num_pages_free);
111         return 0;
112 }
113 EXPORT_SYMBOL(ceph_pagelist_free_reserve);
114 
115 /* Create a truncation point. */
116 void ceph_pagelist_set_cursor(struct ceph_pagelist *pl,
117                               struct ceph_pagelist_cursor *c)
118 {
119         c->pl = pl;
120         c->page_lru = pl->head.prev;
121         c->room = pl->room;
122 }
123 EXPORT_SYMBOL(ceph_pagelist_set_cursor);
124 
125 /* Truncate a pagelist to the given point. Move extra pages to reserve.
126  * This won't sleep.
127  * Returns: 0 on success,
128  *          -EINVAL if the pagelist doesn't match the trunc point pagelist
129  */
130 int ceph_pagelist_truncate(struct ceph_pagelist *pl,
131                            struct ceph_pagelist_cursor *c)
132 {
133         struct page *page;
134 
135         if (pl != c->pl)
136                 return -EINVAL;
137         ceph_pagelist_unmap_tail(pl);
138         while (pl->head.prev != c->page_lru) {
139                 page = list_entry(pl->head.prev, struct page, lru);
140                 /* move from pagelist to reserve */
141                 list_move_tail(&page->lru, &pl->free_list);
142                 ++pl->num_pages_free;
143         }
144         pl->room = c->room;
145         if (!list_empty(&pl->head)) {
146                 page = list_entry(pl->head.prev, struct page, lru);
147                 pl->mapped_tail = kmap(page);
148         }
149         return 0;
150 }
151 EXPORT_SYMBOL(ceph_pagelist_truncate);
152 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp