blob: 64305e7056a1c738ef178d59544822af8ce49ea4 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
3
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>
10
11#include <linux/ceph/libceph.h>
12
Henry C Changb6aa5902010-12-15 20:45:41 -080013void ceph_put_page_vector(struct page **pages, int num_pages, bool dirty)
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070014{
15 int i;
16
Henry C Changb6aa5902010-12-15 20:45:41 -080017 for (i = 0; i < num_pages; i++) {
18 if (dirty)
19 set_page_dirty_lock(pages[i]);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070020 put_page(pages[i]);
Henry C Changb6aa5902010-12-15 20:45:41 -080021 }
Ilya Dryomovb01da6a2015-05-04 14:10:11 +030022 kvfree(pages);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070023}
24EXPORT_SYMBOL(ceph_put_page_vector);
25
26void ceph_release_page_vector(struct page **pages, int num_pages)
27{
28 int i;
29
30 for (i = 0; i < num_pages; i++)
31 __free_pages(pages[i], 0);
32 kfree(pages);
33}
34EXPORT_SYMBOL(ceph_release_page_vector);
35
36/*
37 * allocate a vector new pages
38 */
39struct page **ceph_alloc_page_vector(int num_pages, gfp_t flags)
40{
41 struct page **pages;
42 int i;
43
Kees Cook6da2ec52018-06-12 13:55:00 -070044 pages = kmalloc_array(num_pages, sizeof(*pages), flags);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070045 if (!pages)
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);
52 }
53 }
54 return pages;
55}
56EXPORT_SYMBOL(ceph_alloc_page_vector);
57
58/*
59 * copy user data into a page vector
60 */
61int ceph_copy_user_to_page_vector(struct page **pages,
Alex Elderb3248142013-02-06 13:11:38 -060062 const void __user *data,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070063 loff_t off, size_t len)
64{
65 int i = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030066 int po = off & ~PAGE_MASK;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070067 int left = len;
68 int l, bad;
69
70 while (left > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030071 l = min_t(int, PAGE_SIZE-po, left);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070072 bad = copy_from_user(page_address(pages[i]) + po, data, l);
73 if (bad == l)
74 return -EFAULT;
75 data += l - bad;
76 left -= l - bad;
77 po += l - bad;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030078 if (po == PAGE_SIZE) {
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070079 po = 0;
80 i++;
81 }
82 }
83 return len;
84}
85EXPORT_SYMBOL(ceph_copy_user_to_page_vector);
86
Alex Elder903bb322013-02-06 13:11:38 -060087void ceph_copy_to_page_vector(struct page **pages,
Alex Elderb3248142013-02-06 13:11:38 -060088 const void *data,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070089 loff_t off, size_t len)
90{
91 int i = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030092 size_t po = off & ~PAGE_MASK;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070093 size_t left = len;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070094
95 while (left > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030096 size_t l = min_t(size_t, PAGE_SIZE-po, left);
Alex Elder903bb322013-02-06 13:11:38 -060097
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070098 memcpy(page_address(pages[i]) + po, data, l);
99 data += l;
100 left -= l;
101 po += l;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300102 if (po == PAGE_SIZE) {
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700103 po = 0;
104 i++;
105 }
106 }
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700107}
108EXPORT_SYMBOL(ceph_copy_to_page_vector);
109
Alex Elder903bb322013-02-06 13:11:38 -0600110void ceph_copy_from_page_vector(struct page **pages,
Alex Elderb3248142013-02-06 13:11:38 -0600111 void *data,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700112 loff_t off, size_t len)
113{
114 int i = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300115 size_t po = off & ~PAGE_MASK;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700116 size_t left = len;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700117
118 while (left > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300119 size_t l = min_t(size_t, PAGE_SIZE-po, left);
Alex Elder903bb322013-02-06 13:11:38 -0600120
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700121 memcpy(data, page_address(pages[i]) + po, l);
122 data += l;
123 left -= l;
124 po += l;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300125 if (po == PAGE_SIZE) {
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700126 po = 0;
127 i++;
128 }
129 }
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700130}
131EXPORT_SYMBOL(ceph_copy_from_page_vector);
132
133/*
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700134 * Zero an extent within a page vector. Offset is relative to the
135 * start of the first page.
136 */
137void ceph_zero_page_vector_range(int off, int len, struct page **pages)
138{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300139 int i = off >> PAGE_SHIFT;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700140
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300141 off &= ~PAGE_MASK;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700142
143 dout("zero_page_vector_page %u~%u\n", off, len);
144
145 /* leading partial page? */
146 if (off) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300147 int end = min((int)PAGE_SIZE, off + len);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700148 dout("zeroing %d %p head from %d\n", i, pages[i],
149 (int)off);
150 zero_user_segment(pages[i], off, end);
151 len -= (end - off);
152 i++;
153 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300154 while (len >= PAGE_SIZE) {
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700155 dout("zeroing %d %p len=%d\n", i, pages[i], len);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300156 zero_user_segment(pages[i], 0, PAGE_SIZE);
157 len -= PAGE_SIZE;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700158 i++;
159 }
160 /* trailing partial page? */
161 if (len) {
162 dout("zeroing %d %p tail to %d\n", i, pages[i], (int)len);
163 zero_user_segment(pages[i], 0, len);
164 }
165}
166EXPORT_SYMBOL(ceph_zero_page_vector_range);