1 #include <linux/ceph/ceph_debug.h>
3 #include <linux/module.h>
4 #include <linux/sched.h>
5 #include <linux/slab.h>
6 #include <linux/file.h>
7 #include <linux/namei.h>
8 #include <linux/writeback.h>
10 #include <linux/ceph/libceph.h>
13 * build a vector of user pages
15 struct page
**ceph_get_direct_page_vector(const char __user
*data
,
16 int num_pages
, bool write_page
)
21 pages
= kmalloc(sizeof(*pages
) * num_pages
, GFP_NOFS
);
23 return ERR_PTR(-ENOMEM
);
25 down_read(¤t
->mm
->mmap_sem
);
26 rc
= get_user_pages(current
, current
->mm
, (unsigned long)data
,
27 num_pages
, write_page
, 0, pages
, NULL
);
28 up_read(¤t
->mm
->mmap_sem
);
34 ceph_put_page_vector(pages
, rc
> 0 ? rc
: 0, false);
37 EXPORT_SYMBOL(ceph_get_direct_page_vector
);
39 void ceph_put_page_vector(struct page
**pages
, int num_pages
, bool dirty
)
43 for (i
= 0; i
< num_pages
; i
++) {
45 set_page_dirty_lock(pages
[i
]);
50 EXPORT_SYMBOL(ceph_put_page_vector
);
52 void ceph_release_page_vector(struct page
**pages
, int num_pages
)
56 for (i
= 0; i
< num_pages
; i
++)
57 __free_pages(pages
[i
], 0);
60 EXPORT_SYMBOL(ceph_release_page_vector
);
63 * allocate a vector new pages
65 struct page
**ceph_alloc_page_vector(int num_pages
, gfp_t flags
)
70 pages
= kmalloc(sizeof(*pages
) * num_pages
, flags
);
72 return ERR_PTR(-ENOMEM
);
73 for (i
= 0; i
< num_pages
; i
++) {
74 pages
[i
] = __page_cache_alloc(flags
);
75 if (pages
[i
] == NULL
) {
76 ceph_release_page_vector(pages
, i
);
77 return ERR_PTR(-ENOMEM
);
82 EXPORT_SYMBOL(ceph_alloc_page_vector
);
85 * copy user data into a page vector
87 int ceph_copy_user_to_page_vector(struct page
**pages
,
88 const char __user
*data
,
89 loff_t off
, size_t len
)
92 int po
= off
& ~PAGE_CACHE_MASK
;
97 l
= min_t(int, PAGE_CACHE_SIZE
-po
, left
);
98 bad
= copy_from_user(page_address(pages
[i
]) + po
, data
, l
);
104 if (po
== PAGE_CACHE_SIZE
) {
111 EXPORT_SYMBOL(ceph_copy_user_to_page_vector
);
113 int ceph_copy_to_page_vector(struct page
**pages
,
115 loff_t off
, size_t len
)
118 size_t po
= off
& ~PAGE_CACHE_MASK
;
123 l
= min_t(size_t, PAGE_CACHE_SIZE
-po
, left
);
124 memcpy(page_address(pages
[i
]) + po
, data
, l
);
128 if (po
== PAGE_CACHE_SIZE
) {
135 EXPORT_SYMBOL(ceph_copy_to_page_vector
);
137 int ceph_copy_from_page_vector(struct page
**pages
,
139 loff_t off
, size_t len
)
142 size_t po
= off
& ~PAGE_CACHE_MASK
;
147 l
= min_t(size_t, PAGE_CACHE_SIZE
-po
, left
);
148 memcpy(data
, page_address(pages
[i
]) + po
, l
);
152 if (po
== PAGE_CACHE_SIZE
) {
159 EXPORT_SYMBOL(ceph_copy_from_page_vector
);
162 * copy user data from a page vector into a user pointer
164 int ceph_copy_page_vector_to_user(struct page
**pages
,
166 loff_t off
, size_t len
)
169 int po
= off
& ~PAGE_CACHE_MASK
;
174 l
= min_t(int, left
, PAGE_CACHE_SIZE
-po
);
175 bad
= copy_to_user(data
, page_address(pages
[i
]) + po
, l
);
182 if (po
== PAGE_CACHE_SIZE
)
189 EXPORT_SYMBOL(ceph_copy_page_vector_to_user
);
192 * Zero an extent within a page vector. Offset is relative to the
193 * start of the first page.
195 void ceph_zero_page_vector_range(int off
, int len
, struct page
**pages
)
197 int i
= off
>> PAGE_CACHE_SHIFT
;
199 off
&= ~PAGE_CACHE_MASK
;
201 dout("zero_page_vector_page %u~%u\n", off
, len
);
203 /* leading partial page? */
205 int end
= min((int)PAGE_CACHE_SIZE
, off
+ len
);
206 dout("zeroing %d %p head from %d\n", i
, pages
[i
],
208 zero_user_segment(pages
[i
], off
, end
);
212 while (len
>= PAGE_CACHE_SIZE
) {
213 dout("zeroing %d %p len=%d\n", i
, pages
[i
], len
);
214 zero_user_segment(pages
[i
], 0, PAGE_CACHE_SIZE
);
215 len
-= PAGE_CACHE_SIZE
;
218 /* trailing partial page? */
220 dout("zeroing %d %p tail to %d\n", i
, pages
[i
], (int)len
);
221 zero_user_segment(pages
[i
], 0, len
);
224 EXPORT_SYMBOL(ceph_zero_page_vector_range
);