Blame view

net/ceph/pagevec.c 4.2 KB
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  #include <linux/ceph/ceph_debug.h>
  
  #include <linux/module.h>
  #include <linux/sched.h>
  #include <linux/slab.h>
  #include <linux/file.h>
  #include <linux/namei.h>
  #include <linux/writeback.h>
  
  #include <linux/ceph/libceph.h>
  
  /*
   * build a vector of user pages
   */
b324814e8   Alex Elder   libceph: use void...
15
  struct page **ceph_get_direct_page_vector(const void __user *data,
b6aa5901c   Henry C Chang   ceph: mark user p...
16
  					  int num_pages, bool write_page)
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
17
18
  {
  	struct page **pages;
38815b780   Sage Weil   libceph: fix hand...
19
20
  	int got = 0;
  	int rc = 0;
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
21
22
23
24
25
26
  
  	pages = kmalloc(sizeof(*pages) * num_pages, GFP_NOFS);
  	if (!pages)
  		return ERR_PTR(-ENOMEM);
  
  	down_read(&current->mm->mmap_sem);
38815b780   Sage Weil   libceph: fix hand...
27
28
29
30
31
32
33
34
35
  	while (got < num_pages) {
  		rc = get_user_pages(current, current->mm,
  		    (unsigned long)data + ((unsigned long)got * PAGE_SIZE),
  		    num_pages - got, write_page, 0, pages + got, NULL);
  		if (rc < 0)
  			break;
  		BUG_ON(rc == 0);
  		got += rc;
  	}
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
36
  	up_read(&current->mm->mmap_sem);
38815b780   Sage Weil   libceph: fix hand...
37
  	if (rc < 0)
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
38
39
40
41
  		goto fail;
  	return pages;
  
  fail:
38815b780   Sage Weil   libceph: fix hand...
42
  	ceph_put_page_vector(pages, got, false);
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
43
44
45
  	return ERR_PTR(rc);
  }
  EXPORT_SYMBOL(ceph_get_direct_page_vector);
b6aa5901c   Henry C Chang   ceph: mark user p...
46
  void ceph_put_page_vector(struct page **pages, int num_pages, bool dirty)
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
47
48
  {
  	int i;
b6aa5901c   Henry C Chang   ceph: mark user p...
49
50
51
  	for (i = 0; i < num_pages; i++) {
  		if (dirty)
  			set_page_dirty_lock(pages[i]);
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
52
  		put_page(pages[i]);
b6aa5901c   Henry C Chang   ceph: mark user p...
53
  	}
2b777c9dd   Al Viro   ceph_sync_read: s...
54
55
56
57
  	if (is_vmalloc_addr(pages))
  		vfree(pages);
  	else
  		kfree(pages);
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  }
  EXPORT_SYMBOL(ceph_put_page_vector);
  
  void ceph_release_page_vector(struct page **pages, int num_pages)
  {
  	int i;
  
  	for (i = 0; i < num_pages; i++)
  		__free_pages(pages[i], 0);
  	kfree(pages);
  }
  EXPORT_SYMBOL(ceph_release_page_vector);
  
  /*
   * allocate a vector new pages
   */
  struct page **ceph_alloc_page_vector(int num_pages, gfp_t flags)
  {
  	struct page **pages;
  	int i;
  
  	pages = kmalloc(sizeof(*pages) * num_pages, flags);
  	if (!pages)
  		return ERR_PTR(-ENOMEM);
  	for (i = 0; i < num_pages; i++) {
  		pages[i] = __page_cache_alloc(flags);
  		if (pages[i] == NULL) {
  			ceph_release_page_vector(pages, i);
  			return ERR_PTR(-ENOMEM);
  		}
  	}
  	return pages;
  }
  EXPORT_SYMBOL(ceph_alloc_page_vector);
  
  /*
   * copy user data into a page vector
   */
  int ceph_copy_user_to_page_vector(struct page **pages,
b324814e8   Alex Elder   libceph: use void...
97
  					 const void __user *data,
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  					 loff_t off, size_t len)
  {
  	int i = 0;
  	int po = off & ~PAGE_CACHE_MASK;
  	int left = len;
  	int l, bad;
  
  	while (left > 0) {
  		l = min_t(int, PAGE_CACHE_SIZE-po, left);
  		bad = copy_from_user(page_address(pages[i]) + po, data, l);
  		if (bad == l)
  			return -EFAULT;
  		data += l - bad;
  		left -= l - bad;
  		po += l - bad;
  		if (po == PAGE_CACHE_SIZE) {
  			po = 0;
  			i++;
  		}
  	}
  	return len;
  }
  EXPORT_SYMBOL(ceph_copy_user_to_page_vector);
903bb32e8   Alex Elder   libceph: drop ret...
121
  void ceph_copy_to_page_vector(struct page **pages,
b324814e8   Alex Elder   libceph: use void...
122
  				    const void *data,
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
123
124
125
126
127
  				    loff_t off, size_t len)
  {
  	int i = 0;
  	size_t po = off & ~PAGE_CACHE_MASK;
  	size_t left = len;
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
128
129
  
  	while (left > 0) {
903bb32e8   Alex Elder   libceph: drop ret...
130
  		size_t l = min_t(size_t, PAGE_CACHE_SIZE-po, left);
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
131
132
133
134
135
136
137
138
139
  		memcpy(page_address(pages[i]) + po, data, l);
  		data += l;
  		left -= l;
  		po += l;
  		if (po == PAGE_CACHE_SIZE) {
  			po = 0;
  			i++;
  		}
  	}
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
140
141
  }
  EXPORT_SYMBOL(ceph_copy_to_page_vector);
903bb32e8   Alex Elder   libceph: drop ret...
142
  void ceph_copy_from_page_vector(struct page **pages,
b324814e8   Alex Elder   libceph: use void...
143
  				    void *data,
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
144
145
146
147
148
  				    loff_t off, size_t len)
  {
  	int i = 0;
  	size_t po = off & ~PAGE_CACHE_MASK;
  	size_t left = len;
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
149
150
  
  	while (left > 0) {
903bb32e8   Alex Elder   libceph: drop ret...
151
  		size_t l = min_t(size_t, PAGE_CACHE_SIZE-po, left);
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
152
153
154
155
156
157
158
159
160
  		memcpy(data, page_address(pages[i]) + po, l);
  		data += l;
  		left -= l;
  		po += l;
  		if (po == PAGE_CACHE_SIZE) {
  			po = 0;
  			i++;
  		}
  	}
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
161
162
163
164
  }
  EXPORT_SYMBOL(ceph_copy_from_page_vector);
  
  /*
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
   * Zero an extent within a page vector.  Offset is relative to the
   * start of the first page.
   */
  void ceph_zero_page_vector_range(int off, int len, struct page **pages)
  {
  	int i = off >> PAGE_CACHE_SHIFT;
  
  	off &= ~PAGE_CACHE_MASK;
  
  	dout("zero_page_vector_page %u~%u
  ", off, len);
  
  	/* leading partial page? */
  	if (off) {
  		int end = min((int)PAGE_CACHE_SIZE, off + len);
  		dout("zeroing %d %p head from %d
  ", i, pages[i],
  		     (int)off);
  		zero_user_segment(pages[i], off, end);
  		len -= (end - off);
  		i++;
  	}
  	while (len >= PAGE_CACHE_SIZE) {
  		dout("zeroing %d %p len=%d
  ", i, pages[i], len);
  		zero_user_segment(pages[i], 0, PAGE_CACHE_SIZE);
  		len -= PAGE_CACHE_SIZE;
  		i++;
  	}
  	/* trailing partial page? */
  	if (len) {
  		dout("zeroing %d %p tail to %d
  ", i, pages[i], (int)len);
  		zero_user_segment(pages[i], 0, len);
  	}
  }
  EXPORT_SYMBOL(ceph_zero_page_vector_range);