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>
13 void ceph_put_page_vector(struct page **pages, int num_pages, bool dirty)
17 for (i = 0; i < num_pages; i++) {
19 set_page_dirty_lock(pages[i]);
24 EXPORT_SYMBOL(ceph_put_page_vector);
26 void ceph_release_page_vector(struct page **pages, int num_pages)
30 for (i = 0; i < num_pages; i++)
31 __free_pages(pages[i], 0);
34 EXPORT_SYMBOL(ceph_release_page_vector);
37 * allocate a vector new pages
39 struct page **ceph_alloc_page_vector(int num_pages, gfp_t flags)
44 pages = kmalloc_array(num_pages, sizeof(*pages), flags);
46 return ERR_PTR(-ENOMEM);
47 for (i = 0; i < num_pages; i++) {
48 pages[i] = __page_cache_alloc(flags);
49 if (pages[i] == NULL) {
50 ceph_release_page_vector(pages, i);
51 return ERR_PTR(-ENOMEM);
56 EXPORT_SYMBOL(ceph_alloc_page_vector);
59 * copy user data into a page vector
61 int ceph_copy_user_to_page_vector(struct page **pages,
62 const void __user *data,
63 loff_t off, size_t len)
66 int po = off & ~PAGE_MASK;
71 l = min_t(int, PAGE_SIZE-po, left);
72 bad = copy_from_user(page_address(pages[i]) + po, data, l);
78 if (po == PAGE_SIZE) {
85 EXPORT_SYMBOL(ceph_copy_user_to_page_vector);
87 void ceph_copy_to_page_vector(struct page **pages,
89 loff_t off, size_t len)
92 size_t po = off & ~PAGE_MASK;
96 size_t l = min_t(size_t, PAGE_SIZE-po, left);
98 memcpy(page_address(pages[i]) + po, data, l);
102 if (po == PAGE_SIZE) {
108 EXPORT_SYMBOL(ceph_copy_to_page_vector);
110 void ceph_copy_from_page_vector(struct page **pages,
112 loff_t off, size_t len)
115 size_t po = off & ~PAGE_MASK;
119 size_t l = min_t(size_t, PAGE_SIZE-po, left);
121 memcpy(data, page_address(pages[i]) + po, l);
125 if (po == PAGE_SIZE) {
131 EXPORT_SYMBOL(ceph_copy_from_page_vector);
134 * Zero an extent within a page vector. Offset is relative to the
135 * start of the first page.
137 void ceph_zero_page_vector_range(int off, int len, struct page **pages)
139 int i = off >> PAGE_SHIFT;
143 dout("zero_page_vector_page %u~%u\n", off, len);
145 /* leading partial page? */
147 int end = min((int)PAGE_SIZE, off + len);
148 dout("zeroing %d %p head from %d\n", i, pages[i],
150 zero_user_segment(pages[i], off, end);
154 while (len >= PAGE_SIZE) {
155 dout("zeroing %d %p len=%d\n", i, pages[i], len);
156 zero_user_segment(pages[i], 0, PAGE_SIZE);
160 /* trailing partial page? */
162 dout("zeroing %d %p tail to %d\n", i, pages[i], (int)len);
163 zero_user_segment(pages[i], 0, len);
166 EXPORT_SYMBOL(ceph_zero_page_vector_range);