tty: serial: lpuart: avoid leaking struct tty_struct
[linux/fpc-iii.git] / net / ceph / pagevec.c
blobd3736f5bffec695aa94ea6dfb05be8319a3d195f
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
4 #include <linux/module.h>
5 #include <linux/sched.h>
6 #include <linux/slab.h>
7 #include <linux/file.h>
8 #include <linux/namei.h>
9 #include <linux/writeback.h>
11 #include <linux/ceph/libceph.h>
14 * build a vector of user pages
16 struct page **ceph_get_direct_page_vector(const void __user *data,
17 int num_pages, bool write_page)
19 struct page **pages;
20 int got = 0;
21 int rc = 0;
23 pages = kmalloc_array(num_pages, sizeof(*pages), GFP_NOFS);
24 if (!pages)
25 return ERR_PTR(-ENOMEM);
27 while (got < num_pages) {
28 rc = get_user_pages_fast(
29 (unsigned long)data + ((unsigned long)got * PAGE_SIZE),
30 num_pages - got, write_page, pages + got);
31 if (rc < 0)
32 break;
33 BUG_ON(rc == 0);
34 got += rc;
36 if (rc < 0)
37 goto fail;
38 return pages;
40 fail:
41 ceph_put_page_vector(pages, got, false);
42 return ERR_PTR(rc);
44 EXPORT_SYMBOL(ceph_get_direct_page_vector);
46 void ceph_put_page_vector(struct page **pages, int num_pages, bool dirty)
48 int i;
50 for (i = 0; i < num_pages; i++) {
51 if (dirty)
52 set_page_dirty_lock(pages[i]);
53 put_page(pages[i]);
55 kvfree(pages);
57 EXPORT_SYMBOL(ceph_put_page_vector);
59 void ceph_release_page_vector(struct page **pages, int num_pages)
61 int i;
63 for (i = 0; i < num_pages; i++)
64 __free_pages(pages[i], 0);
65 kfree(pages);
67 EXPORT_SYMBOL(ceph_release_page_vector);
70 * allocate a vector new pages
72 struct page **ceph_alloc_page_vector(int num_pages, gfp_t flags)
74 struct page **pages;
75 int i;
77 pages = kmalloc_array(num_pages, sizeof(*pages), flags);
78 if (!pages)
79 return ERR_PTR(-ENOMEM);
80 for (i = 0; i < num_pages; i++) {
81 pages[i] = __page_cache_alloc(flags);
82 if (pages[i] == NULL) {
83 ceph_release_page_vector(pages, i);
84 return ERR_PTR(-ENOMEM);
87 return pages;
89 EXPORT_SYMBOL(ceph_alloc_page_vector);
92 * copy user data into a page vector
94 int ceph_copy_user_to_page_vector(struct page **pages,
95 const void __user *data,
96 loff_t off, size_t len)
98 int i = 0;
99 int po = off & ~PAGE_MASK;
100 int left = len;
101 int l, bad;
103 while (left > 0) {
104 l = min_t(int, PAGE_SIZE-po, left);
105 bad = copy_from_user(page_address(pages[i]) + po, data, l);
106 if (bad == l)
107 return -EFAULT;
108 data += l - bad;
109 left -= l - bad;
110 po += l - bad;
111 if (po == PAGE_SIZE) {
112 po = 0;
113 i++;
116 return len;
118 EXPORT_SYMBOL(ceph_copy_user_to_page_vector);
120 void ceph_copy_to_page_vector(struct page **pages,
121 const void *data,
122 loff_t off, size_t len)
124 int i = 0;
125 size_t po = off & ~PAGE_MASK;
126 size_t left = len;
128 while (left > 0) {
129 size_t l = min_t(size_t, PAGE_SIZE-po, left);
131 memcpy(page_address(pages[i]) + po, data, l);
132 data += l;
133 left -= l;
134 po += l;
135 if (po == PAGE_SIZE) {
136 po = 0;
137 i++;
141 EXPORT_SYMBOL(ceph_copy_to_page_vector);
143 void ceph_copy_from_page_vector(struct page **pages,
144 void *data,
145 loff_t off, size_t len)
147 int i = 0;
148 size_t po = off & ~PAGE_MASK;
149 size_t left = len;
151 while (left > 0) {
152 size_t l = min_t(size_t, PAGE_SIZE-po, left);
154 memcpy(data, page_address(pages[i]) + po, l);
155 data += l;
156 left -= l;
157 po += l;
158 if (po == PAGE_SIZE) {
159 po = 0;
160 i++;
164 EXPORT_SYMBOL(ceph_copy_from_page_vector);
167 * Zero an extent within a page vector. Offset is relative to the
168 * start of the first page.
170 void ceph_zero_page_vector_range(int off, int len, struct page **pages)
172 int i = off >> PAGE_SHIFT;
174 off &= ~PAGE_MASK;
176 dout("zero_page_vector_page %u~%u\n", off, len);
178 /* leading partial page? */
179 if (off) {
180 int end = min((int)PAGE_SIZE, off + len);
181 dout("zeroing %d %p head from %d\n", i, pages[i],
182 (int)off);
183 zero_user_segment(pages[i], off, end);
184 len -= (end - off);
185 i++;
187 while (len >= PAGE_SIZE) {
188 dout("zeroing %d %p len=%d\n", i, pages[i], len);
189 zero_user_segment(pages[i], 0, PAGE_SIZE);
190 len -= PAGE_SIZE;
191 i++;
193 /* trailing partial page? */
194 if (len) {
195 dout("zeroing %d %p tail to %d\n", i, pages[i], (int)len);
196 zero_user_segment(pages[i], 0, len);
199 EXPORT_SYMBOL(ceph_zero_page_vector_range);