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
)
23 pages
= kmalloc_array(num_pages
, sizeof(*pages
), GFP_NOFS
);
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
);
41 ceph_put_page_vector(pages
, got
, false);
44 EXPORT_SYMBOL(ceph_get_direct_page_vector
);
46 void ceph_put_page_vector(struct page
**pages
, int num_pages
, bool dirty
)
50 for (i
= 0; i
< num_pages
; i
++) {
52 set_page_dirty_lock(pages
[i
]);
57 EXPORT_SYMBOL(ceph_put_page_vector
);
59 void ceph_release_page_vector(struct page
**pages
, int num_pages
)
63 for (i
= 0; i
< num_pages
; i
++)
64 __free_pages(pages
[i
], 0);
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
)
77 pages
= kmalloc_array(num_pages
, sizeof(*pages
), flags
);
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
);
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
)
99 int po
= off
& ~PAGE_MASK
;
104 l
= min_t(int, PAGE_SIZE
-po
, left
);
105 bad
= copy_from_user(page_address(pages
[i
]) + po
, data
, l
);
111 if (po
== PAGE_SIZE
) {
118 EXPORT_SYMBOL(ceph_copy_user_to_page_vector
);
120 void ceph_copy_to_page_vector(struct page
**pages
,
122 loff_t off
, size_t len
)
125 size_t po
= off
& ~PAGE_MASK
;
129 size_t l
= min_t(size_t, PAGE_SIZE
-po
, left
);
131 memcpy(page_address(pages
[i
]) + po
, data
, l
);
135 if (po
== PAGE_SIZE
) {
141 EXPORT_SYMBOL(ceph_copy_to_page_vector
);
143 void ceph_copy_from_page_vector(struct page
**pages
,
145 loff_t off
, size_t len
)
148 size_t po
= off
& ~PAGE_MASK
;
152 size_t l
= min_t(size_t, PAGE_SIZE
-po
, left
);
154 memcpy(data
, page_address(pages
[i
]) + po
, l
);
158 if (po
== PAGE_SIZE
) {
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
;
176 dout("zero_page_vector_page %u~%u\n", off
, len
);
178 /* leading partial page? */
180 int end
= min((int)PAGE_SIZE
, off
+ len
);
181 dout("zeroing %d %p head from %d\n", i
, pages
[i
],
183 zero_user_segment(pages
[i
], off
, end
);
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
);
193 /* trailing partial page? */
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
);