Commit | Line | Data |
---|---|---|
3d14c5d2 | 1 | #include <linux/ceph/ceph_debug.h> |
1d3576fd SW |
2 | |
3 | #include <linux/backing-dev.h> | |
4 | #include <linux/fs.h> | |
5 | #include <linux/mm.h> | |
6 | #include <linux/pagemap.h> | |
7 | #include <linux/writeback.h> /* generic_writepages */ | |
5a0e3ad6 | 8 | #include <linux/slab.h> |
1d3576fd SW |
9 | #include <linux/pagevec.h> |
10 | #include <linux/task_io_accounting_ops.h> | |
11 | ||
12 | #include "super.h" | |
3d14c5d2 YS |
13 | #include "mds_client.h" |
14 | #include <linux/ceph/osd_client.h> | |
1d3576fd SW |
15 | |
16 | /* | |
17 | * Ceph address space ops. | |
18 | * | |
19 | * There are a few funny things going on here. | |
20 | * | |
21 | * The page->private field is used to reference a struct | |
22 | * ceph_snap_context for _every_ dirty page. This indicates which | |
23 | * snapshot the page was logically dirtied in, and thus which snap | |
24 | * context needs to be associated with the osd write during writeback. | |
25 | * | |
26 | * Similarly, struct ceph_inode_info maintains a set of counters to | |
25985edc | 27 | * count dirty pages on the inode. In the absence of snapshots, |
1d3576fd SW |
28 | * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count. |
29 | * | |
30 | * When a snapshot is taken (that is, when the client receives | |
31 | * notification that a snapshot was taken), each inode with caps and | |
32 | * with dirty pages (dirty pages implies there is a cap) gets a new | |
33 | * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending | |
34 | * order, new snaps go to the tail). The i_wrbuffer_ref_head count is | |
35 | * moved to capsnap->dirty. (Unless a sync write is currently in | |
36 | * progress. In that case, the capsnap is said to be "pending", new | |
37 | * writes cannot start, and the capsnap isn't "finalized" until the | |
38 | * write completes (or fails) and a final size/mtime for the inode for | |
39 | * that snap can be settled upon.) i_wrbuffer_ref_head is reset to 0. | |
40 | * | |
41 | * On writeback, we must submit writes to the osd IN SNAP ORDER. So, | |
42 | * we look for the first capsnap in i_cap_snaps and write out pages in | |
43 | * that snap context _only_. Then we move on to the next capsnap, | |
44 | * eventually reaching the "live" or "head" context (i.e., pages that | |
45 | * are not yet snapped) and are writing the most recently dirtied | |
46 | * pages. | |
47 | * | |
48 | * Invalidate and so forth must take care to ensure the dirty page | |
49 | * accounting is preserved. | |
50 | */ | |
51 | ||
2baba250 YS |
52 | #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10)) |
53 | #define CONGESTION_OFF_THRESH(congestion_kb) \ | |
54 | (CONGESTION_ON_THRESH(congestion_kb) - \ | |
55 | (CONGESTION_ON_THRESH(congestion_kb) >> 2)) | |
56 | ||
61600ef8 YZ |
57 | static inline struct ceph_snap_context *page_snap_context(struct page *page) |
58 | { | |
59 | if (PagePrivate(page)) | |
60 | return (void *)page->private; | |
61 | return NULL; | |
62 | } | |
1d3576fd SW |
63 | |
64 | /* | |
65 | * Dirty a page. Optimistically adjust accounting, on the assumption | |
66 | * that we won't race with invalidate. If we do, readjust. | |
67 | */ | |
68 | static int ceph_set_page_dirty(struct page *page) | |
69 | { | |
70 | struct address_space *mapping = page->mapping; | |
71 | struct inode *inode; | |
72 | struct ceph_inode_info *ci; | |
1d3576fd | 73 | struct ceph_snap_context *snapc; |
7d6e1f54 | 74 | int ret; |
1d3576fd SW |
75 | |
76 | if (unlikely(!mapping)) | |
77 | return !TestSetPageDirty(page); | |
78 | ||
7d6e1f54 | 79 | if (PageDirty(page)) { |
1d3576fd SW |
80 | dout("%p set_page_dirty %p idx %lu -- already dirty\n", |
81 | mapping->host, page, page->index); | |
7d6e1f54 | 82 | BUG_ON(!PagePrivate(page)); |
1d3576fd SW |
83 | return 0; |
84 | } | |
85 | ||
86 | inode = mapping->host; | |
87 | ci = ceph_inode(inode); | |
88 | ||
89 | /* | |
90 | * Note that we're grabbing a snapc ref here without holding | |
91 | * any locks! | |
92 | */ | |
93 | snapc = ceph_get_snap_context(ci->i_snap_realm->cached_context); | |
94 | ||
95 | /* dirty the head */ | |
be655596 | 96 | spin_lock(&ci->i_ceph_lock); |
7d8cb26d | 97 | if (ci->i_head_snapc == NULL) |
1d3576fd SW |
98 | ci->i_head_snapc = ceph_get_snap_context(snapc); |
99 | ++ci->i_wrbuffer_ref_head; | |
100 | if (ci->i_wrbuffer_ref == 0) | |
0444d76a | 101 | ihold(inode); |
1d3576fd SW |
102 | ++ci->i_wrbuffer_ref; |
103 | dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d " | |
104 | "snapc %p seq %lld (%d snaps)\n", | |
105 | mapping->host, page, page->index, | |
106 | ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1, | |
107 | ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head, | |
108 | snapc, snapc->seq, snapc->num_snaps); | |
be655596 | 109 | spin_unlock(&ci->i_ceph_lock); |
1d3576fd | 110 | |
7d6e1f54 SZ |
111 | /* |
112 | * Reference snap context in page->private. Also set | |
113 | * PagePrivate so that we get invalidatepage callback. | |
114 | */ | |
115 | BUG_ON(PagePrivate(page)); | |
116 | page->private = (unsigned long)snapc; | |
117 | SetPagePrivate(page); | |
1d3576fd | 118 | |
7d6e1f54 SZ |
119 | ret = __set_page_dirty_nobuffers(page); |
120 | WARN_ON(!PageLocked(page)); | |
121 | WARN_ON(!page->mapping); | |
1d3576fd | 122 | |
7d6e1f54 | 123 | return ret; |
1d3576fd SW |
124 | } |
125 | ||
126 | /* | |
127 | * If we are truncating the full page (i.e. offset == 0), adjust the | |
128 | * dirty page counters appropriately. Only called if there is private | |
129 | * data on the page. | |
130 | */ | |
d47992f8 LC |
131 | static void ceph_invalidatepage(struct page *page, unsigned int offset, |
132 | unsigned int length) | |
1d3576fd | 133 | { |
4ce1e9ad | 134 | struct inode *inode; |
1d3576fd | 135 | struct ceph_inode_info *ci; |
61600ef8 | 136 | struct ceph_snap_context *snapc = page_snap_context(page); |
1d3576fd | 137 | |
4ce1e9ad | 138 | inode = page->mapping->host; |
b150f5c1 MT |
139 | ci = ceph_inode(inode); |
140 | ||
141 | if (offset != 0 || length != PAGE_CACHE_SIZE) { | |
142 | dout("%p invalidatepage %p idx %lu partial dirty page %u~%u\n", | |
143 | inode, page, page->index, offset, length); | |
144 | return; | |
145 | } | |
4ce1e9ad | 146 | |
1d3576fd SW |
147 | /* |
148 | * We can get non-dirty pages here due to races between | |
149 | * set_page_dirty and truncate_complete_page; just spit out a | |
150 | * warning, in case we end up with accounting problems later. | |
151 | */ | |
152 | if (!PageDirty(page)) | |
153 | pr_err("%p invalidatepage %p page not dirty\n", inode, page); | |
154 | ||
b150f5c1 | 155 | ClearPageChecked(page); |
1d3576fd | 156 | |
b150f5c1 MT |
157 | dout("%p invalidatepage %p idx %lu full dirty page\n", |
158 | inode, page, page->index); | |
159 | ||
160 | ceph_put_wrbuffer_cap_refs(ci, 1, snapc); | |
161 | ceph_put_snap_context(snapc); | |
162 | page->private = 0; | |
163 | ClearPagePrivate(page); | |
1d3576fd SW |
164 | } |
165 | ||
166 | /* just a sanity check */ | |
167 | static int ceph_releasepage(struct page *page, gfp_t g) | |
168 | { | |
169 | struct inode *inode = page->mapping ? page->mapping->host : NULL; | |
170 | dout("%p releasepage %p idx %lu\n", inode, page, page->index); | |
171 | WARN_ON(PageDirty(page)); | |
1d3576fd SW |
172 | WARN_ON(PagePrivate(page)); |
173 | return 0; | |
174 | } | |
175 | ||
176 | /* | |
177 | * read a single page, without unlocking it. | |
178 | */ | |
179 | static int readpage_nounlock(struct file *filp, struct page *page) | |
180 | { | |
496ad9aa | 181 | struct inode *inode = file_inode(filp); |
1d3576fd | 182 | struct ceph_inode_info *ci = ceph_inode(inode); |
3d14c5d2 YS |
183 | struct ceph_osd_client *osdc = |
184 | &ceph_inode_to_client(inode)->client->osdc; | |
1d3576fd SW |
185 | int err = 0; |
186 | u64 len = PAGE_CACHE_SIZE; | |
187 | ||
188 | dout("readpage inode %p file %p page %p index %lu\n", | |
189 | inode, filp, page, page->index); | |
190 | err = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout, | |
6285bc23 | 191 | (u64) page_offset(page), &len, |
1d3576fd | 192 | ci->i_truncate_seq, ci->i_truncate_size, |
b7495fc2 | 193 | &page, 1, 0); |
1d3576fd SW |
194 | if (err == -ENOENT) |
195 | err = 0; | |
196 | if (err < 0) { | |
197 | SetPageError(page); | |
198 | goto out; | |
199 | } else if (err < PAGE_CACHE_SIZE) { | |
200 | /* zero fill remainder of page */ | |
201 | zero_user_segment(page, err, PAGE_CACHE_SIZE); | |
202 | } | |
203 | SetPageUptodate(page); | |
204 | ||
205 | out: | |
206 | return err < 0 ? err : 0; | |
207 | } | |
208 | ||
209 | static int ceph_readpage(struct file *filp, struct page *page) | |
210 | { | |
211 | int r = readpage_nounlock(filp, page); | |
212 | unlock_page(page); | |
213 | return r; | |
214 | } | |
215 | ||
216 | /* | |
7c272194 | 217 | * Finish an async read(ahead) op. |
1d3576fd | 218 | */ |
7c272194 | 219 | static void finish_read(struct ceph_osd_request *req, struct ceph_msg *msg) |
1d3576fd | 220 | { |
7c272194 | 221 | struct inode *inode = req->r_inode; |
87060c10 | 222 | struct ceph_osd_data *osd_data; |
1b83bef2 SW |
223 | int rc = req->r_result; |
224 | int bytes = le32_to_cpu(msg->hdr.data_len); | |
e0c59487 | 225 | int num_pages; |
7c272194 | 226 | int i; |
1d3576fd | 227 | |
7c272194 SW |
228 | dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes); |
229 | ||
230 | /* unlock all pages, zeroing any data we didn't read */ | |
406e2c9f | 231 | osd_data = osd_req_op_extent_osd_data(req, 0); |
87060c10 AE |
232 | BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES); |
233 | num_pages = calc_pages_for((u64)osd_data->alignment, | |
234 | (u64)osd_data->length); | |
e0c59487 | 235 | for (i = 0; i < num_pages; i++) { |
87060c10 | 236 | struct page *page = osd_data->pages[i]; |
7c272194 SW |
237 | |
238 | if (bytes < (int)PAGE_CACHE_SIZE) { | |
239 | /* zero (remainder of) page */ | |
240 | int s = bytes < 0 ? 0 : bytes; | |
241 | zero_user_segment(page, s, PAGE_CACHE_SIZE); | |
1d3576fd | 242 | } |
7c272194 SW |
243 | dout("finish_read %p uptodate %p idx %lu\n", inode, page, |
244 | page->index); | |
245 | flush_dcache_page(page); | |
246 | SetPageUptodate(page); | |
247 | unlock_page(page); | |
248 | page_cache_release(page); | |
0fff87ec | 249 | bytes -= PAGE_CACHE_SIZE; |
1d3576fd | 250 | } |
87060c10 | 251 | kfree(osd_data->pages); |
1d3576fd SW |
252 | } |
253 | ||
8884d53d DZ |
254 | static void ceph_unlock_page_vector(struct page **pages, int num_pages) |
255 | { | |
256 | int i; | |
257 | ||
258 | for (i = 0; i < num_pages; i++) | |
259 | unlock_page(pages[i]); | |
260 | } | |
261 | ||
1d3576fd | 262 | /* |
7c272194 SW |
263 | * start an async read(ahead) operation. return nr_pages we submitted |
264 | * a read for on success, or negative error code. | |
1d3576fd | 265 | */ |
0d66a487 | 266 | static int start_read(struct inode *inode, struct list_head *page_list, int max) |
1d3576fd | 267 | { |
3d14c5d2 YS |
268 | struct ceph_osd_client *osdc = |
269 | &ceph_inode_to_client(inode)->client->osdc; | |
7c272194 SW |
270 | struct ceph_inode_info *ci = ceph_inode(inode); |
271 | struct page *page = list_entry(page_list->prev, struct page, lru); | |
acead002 | 272 | struct ceph_vino vino; |
7c272194 SW |
273 | struct ceph_osd_request *req; |
274 | u64 off; | |
1d3576fd | 275 | u64 len; |
7c272194 SW |
276 | int i; |
277 | struct page **pages; | |
278 | pgoff_t next_index; | |
279 | int nr_pages = 0; | |
280 | int ret; | |
1d3576fd | 281 | |
6285bc23 | 282 | off = (u64) page_offset(page); |
1d3576fd | 283 | |
7c272194 SW |
284 | /* count pages */ |
285 | next_index = page->index; | |
286 | list_for_each_entry_reverse(page, page_list, lru) { | |
287 | if (page->index != next_index) | |
288 | break; | |
289 | nr_pages++; | |
290 | next_index++; | |
0d66a487 SW |
291 | if (max && nr_pages == max) |
292 | break; | |
7c272194 | 293 | } |
1d3576fd | 294 | len = nr_pages << PAGE_CACHE_SHIFT; |
7c272194 SW |
295 | dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages, |
296 | off, len); | |
acead002 AE |
297 | vino = ceph_vino(inode); |
298 | req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len, | |
79528734 | 299 | 1, CEPH_OSD_OP_READ, |
acead002 | 300 | CEPH_OSD_FLAG_READ, NULL, |
7c272194 | 301 | ci->i_truncate_seq, ci->i_truncate_size, |
acead002 | 302 | false); |
6816282d SW |
303 | if (IS_ERR(req)) |
304 | return PTR_ERR(req); | |
1d3576fd | 305 | |
7c272194 | 306 | /* build page vector */ |
cf7b7e14 | 307 | nr_pages = calc_pages_for(0, len); |
7c272194 SW |
308 | pages = kmalloc(sizeof(*pages) * nr_pages, GFP_NOFS); |
309 | ret = -ENOMEM; | |
310 | if (!pages) | |
311 | goto out; | |
312 | for (i = 0; i < nr_pages; ++i) { | |
313 | page = list_entry(page_list->prev, struct page, lru); | |
314 | BUG_ON(PageLocked(page)); | |
1d3576fd | 315 | list_del(&page->lru); |
7c272194 SW |
316 | |
317 | dout("start_read %p adding %p idx %lu\n", inode, page, | |
318 | page->index); | |
319 | if (add_to_page_cache_lru(page, &inode->i_data, page->index, | |
213c99ee | 320 | GFP_NOFS)) { |
1d3576fd | 321 | page_cache_release(page); |
7c272194 | 322 | dout("start_read %p add_to_page_cache failed %p\n", |
1d3576fd | 323 | inode, page); |
7c272194 SW |
324 | nr_pages = i; |
325 | goto out_pages; | |
1d3576fd | 326 | } |
7c272194 | 327 | pages[i] = page; |
1d3576fd | 328 | } |
406e2c9f | 329 | osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false); |
7c272194 SW |
330 | req->r_callback = finish_read; |
331 | req->r_inode = inode; | |
332 | ||
79528734 | 333 | ceph_osdc_build_request(req, off, NULL, vino.snap, NULL); |
02ee07d3 | 334 | |
7c272194 SW |
335 | dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len); |
336 | ret = ceph_osdc_start_request(osdc, req, false); | |
337 | if (ret < 0) | |
338 | goto out_pages; | |
339 | ceph_osdc_put_request(req); | |
340 | return nr_pages; | |
341 | ||
342 | out_pages: | |
8884d53d | 343 | ceph_unlock_page_vector(pages, nr_pages); |
7c272194 | 344 | ceph_release_page_vector(pages, nr_pages); |
7c272194 SW |
345 | out: |
346 | ceph_osdc_put_request(req); | |
347 | return ret; | |
348 | } | |
1d3576fd | 349 | |
7c272194 SW |
350 | |
351 | /* | |
352 | * Read multiple pages. Leave pages we don't read + unlock in page_list; | |
353 | * the caller (VM) cleans them up. | |
354 | */ | |
355 | static int ceph_readpages(struct file *file, struct address_space *mapping, | |
356 | struct list_head *page_list, unsigned nr_pages) | |
357 | { | |
496ad9aa | 358 | struct inode *inode = file_inode(file); |
0d66a487 | 359 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
7c272194 | 360 | int rc = 0; |
0d66a487 SW |
361 | int max = 0; |
362 | ||
363 | if (fsc->mount_options->rsize >= PAGE_CACHE_SIZE) | |
364 | max = (fsc->mount_options->rsize + PAGE_CACHE_SIZE - 1) | |
365 | >> PAGE_SHIFT; | |
7c272194 | 366 | |
2794a82a AE |
367 | dout("readpages %p file %p nr_pages %d max %d\n", inode, |
368 | file, nr_pages, | |
0d66a487 | 369 | max); |
7c272194 | 370 | while (!list_empty(page_list)) { |
0d66a487 | 371 | rc = start_read(inode, page_list, max); |
7c272194 SW |
372 | if (rc < 0) |
373 | goto out; | |
374 | BUG_ON(rc == 0); | |
375 | } | |
1d3576fd | 376 | out: |
7c272194 | 377 | dout("readpages %p file %p ret %d\n", inode, file, rc); |
1d3576fd SW |
378 | return rc; |
379 | } | |
380 | ||
381 | /* | |
382 | * Get ref for the oldest snapc for an inode with dirty data... that is, the | |
383 | * only snap context we are allowed to write back. | |
1d3576fd | 384 | */ |
6298a337 SW |
385 | static struct ceph_snap_context *get_oldest_context(struct inode *inode, |
386 | u64 *snap_size) | |
1d3576fd SW |
387 | { |
388 | struct ceph_inode_info *ci = ceph_inode(inode); | |
389 | struct ceph_snap_context *snapc = NULL; | |
390 | struct ceph_cap_snap *capsnap = NULL; | |
391 | ||
be655596 | 392 | spin_lock(&ci->i_ceph_lock); |
1d3576fd SW |
393 | list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { |
394 | dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap, | |
395 | capsnap->context, capsnap->dirty_pages); | |
396 | if (capsnap->dirty_pages) { | |
397 | snapc = ceph_get_snap_context(capsnap->context); | |
398 | if (snap_size) | |
399 | *snap_size = capsnap->size; | |
400 | break; | |
401 | } | |
402 | } | |
7d8cb26d | 403 | if (!snapc && ci->i_wrbuffer_ref_head) { |
80e755fe | 404 | snapc = ceph_get_snap_context(ci->i_head_snapc); |
1d3576fd SW |
405 | dout(" head snapc %p has %d dirty pages\n", |
406 | snapc, ci->i_wrbuffer_ref_head); | |
407 | } | |
be655596 | 408 | spin_unlock(&ci->i_ceph_lock); |
1d3576fd SW |
409 | return snapc; |
410 | } | |
411 | ||
412 | /* | |
413 | * Write a single page, but leave the page locked. | |
414 | * | |
415 | * If we get a write error, set the page error bit, but still adjust the | |
416 | * dirty page accounting (i.e., page is no longer dirty). | |
417 | */ | |
418 | static int writepage_nounlock(struct page *page, struct writeback_control *wbc) | |
419 | { | |
420 | struct inode *inode; | |
421 | struct ceph_inode_info *ci; | |
3d14c5d2 | 422 | struct ceph_fs_client *fsc; |
1d3576fd | 423 | struct ceph_osd_client *osdc; |
6298a337 | 424 | struct ceph_snap_context *snapc, *oldest; |
fc2744aa | 425 | loff_t page_off = page_offset(page); |
2baba250 | 426 | long writeback_stat; |
fc2744aa YZ |
427 | u64 truncate_size, snap_size = 0; |
428 | u32 truncate_seq; | |
429 | int err = 0, len = PAGE_CACHE_SIZE; | |
1d3576fd SW |
430 | |
431 | dout("writepage %p idx %lu\n", page, page->index); | |
432 | ||
433 | if (!page->mapping || !page->mapping->host) { | |
434 | dout("writepage %p - no mapping\n", page); | |
435 | return -EFAULT; | |
436 | } | |
437 | inode = page->mapping->host; | |
438 | ci = ceph_inode(inode); | |
3d14c5d2 YS |
439 | fsc = ceph_inode_to_client(inode); |
440 | osdc = &fsc->client->osdc; | |
1d3576fd SW |
441 | |
442 | /* verify this is a writeable snap context */ | |
61600ef8 | 443 | snapc = page_snap_context(page); |
1d3576fd SW |
444 | if (snapc == NULL) { |
445 | dout("writepage %p page %p not dirty?\n", inode, page); | |
446 | goto out; | |
447 | } | |
6298a337 SW |
448 | oldest = get_oldest_context(inode, &snap_size); |
449 | if (snapc->seq > oldest->seq) { | |
1d3576fd | 450 | dout("writepage %p page %p snapc %p not writeable - noop\n", |
61600ef8 | 451 | inode, page, snapc); |
1d3576fd SW |
452 | /* we should only noop if called by kswapd */ |
453 | WARN_ON((current->flags & PF_MEMALLOC) == 0); | |
6298a337 | 454 | ceph_put_snap_context(oldest); |
1d3576fd SW |
455 | goto out; |
456 | } | |
6298a337 | 457 | ceph_put_snap_context(oldest); |
1d3576fd | 458 | |
fc2744aa YZ |
459 | spin_lock(&ci->i_ceph_lock); |
460 | truncate_seq = ci->i_truncate_seq; | |
461 | truncate_size = ci->i_truncate_size; | |
462 | if (!snap_size) | |
463 | snap_size = i_size_read(inode); | |
464 | spin_unlock(&ci->i_ceph_lock); | |
465 | ||
1d3576fd | 466 | /* is this a partial page at end of file? */ |
fc2744aa YZ |
467 | if (page_off >= snap_size) { |
468 | dout("%p page eof %llu\n", page, snap_size); | |
469 | goto out; | |
470 | } | |
471 | if (snap_size < page_off + len) | |
472 | len = snap_size - page_off; | |
1d3576fd | 473 | |
ae00d4f3 SW |
474 | dout("writepage %p page %p index %lu on %llu~%u snapc %p\n", |
475 | inode, page, page->index, page_off, len, snapc); | |
1d3576fd | 476 | |
3d14c5d2 | 477 | writeback_stat = atomic_long_inc_return(&fsc->writeback_count); |
2baba250 | 478 | if (writeback_stat > |
3d14c5d2 YS |
479 | CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb)) |
480 | set_bdi_congested(&fsc->backing_dev_info, BLK_RW_ASYNC); | |
2baba250 | 481 | |
1d3576fd SW |
482 | set_page_writeback(page); |
483 | err = ceph_osdc_writepages(osdc, ceph_vino(inode), | |
484 | &ci->i_layout, snapc, | |
485 | page_off, len, | |
fc2744aa | 486 | truncate_seq, truncate_size, |
24808826 | 487 | &inode->i_mtime, &page, 1); |
1d3576fd SW |
488 | if (err < 0) { |
489 | dout("writepage setting page/mapping error %d %p\n", err, page); | |
490 | SetPageError(page); | |
491 | mapping_set_error(&inode->i_data, err); | |
492 | if (wbc) | |
493 | wbc->pages_skipped++; | |
494 | } else { | |
495 | dout("writepage cleaned page %p\n", page); | |
496 | err = 0; /* vfs expects us to return 0 */ | |
497 | } | |
498 | page->private = 0; | |
499 | ClearPagePrivate(page); | |
500 | end_page_writeback(page); | |
501 | ceph_put_wrbuffer_cap_refs(ci, 1, snapc); | |
6298a337 | 502 | ceph_put_snap_context(snapc); /* page's reference */ |
1d3576fd SW |
503 | out: |
504 | return err; | |
505 | } | |
506 | ||
507 | static int ceph_writepage(struct page *page, struct writeback_control *wbc) | |
508 | { | |
dbd646a8 YS |
509 | int err; |
510 | struct inode *inode = page->mapping->host; | |
511 | BUG_ON(!inode); | |
70b666c3 | 512 | ihold(inode); |
dbd646a8 | 513 | err = writepage_nounlock(page, wbc); |
1d3576fd | 514 | unlock_page(page); |
dbd646a8 | 515 | iput(inode); |
1d3576fd SW |
516 | return err; |
517 | } | |
518 | ||
519 | ||
520 | /* | |
521 | * lame release_pages helper. release_pages() isn't exported to | |
522 | * modules. | |
523 | */ | |
524 | static void ceph_release_pages(struct page **pages, int num) | |
525 | { | |
526 | struct pagevec pvec; | |
527 | int i; | |
528 | ||
529 | pagevec_init(&pvec, 0); | |
530 | for (i = 0; i < num; i++) { | |
531 | if (pagevec_add(&pvec, pages[i]) == 0) | |
532 | pagevec_release(&pvec); | |
533 | } | |
534 | pagevec_release(&pvec); | |
535 | } | |
536 | ||
537 | ||
538 | /* | |
539 | * async writeback completion handler. | |
540 | * | |
541 | * If we get an error, set the mapping error bit, but not the individual | |
542 | * page error bits. | |
543 | */ | |
544 | static void writepages_finish(struct ceph_osd_request *req, | |
545 | struct ceph_msg *msg) | |
546 | { | |
547 | struct inode *inode = req->r_inode; | |
1d3576fd | 548 | struct ceph_inode_info *ci = ceph_inode(inode); |
87060c10 | 549 | struct ceph_osd_data *osd_data; |
1d3576fd | 550 | unsigned wrote; |
1d3576fd | 551 | struct page *page; |
e0c59487 | 552 | int num_pages; |
1d3576fd SW |
553 | int i; |
554 | struct ceph_snap_context *snapc = req->r_snapc; | |
555 | struct address_space *mapping = inode->i_mapping; | |
1b83bef2 | 556 | int rc = req->r_result; |
79528734 | 557 | u64 bytes = req->r_ops[0].extent.length; |
3d14c5d2 | 558 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
2baba250 | 559 | long writeback_stat; |
7ff899da | 560 | unsigned issued = ceph_caps_issued(ci); |
1d3576fd | 561 | |
406e2c9f | 562 | osd_data = osd_req_op_extent_osd_data(req, 0); |
87060c10 AE |
563 | BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES); |
564 | num_pages = calc_pages_for((u64)osd_data->alignment, | |
565 | (u64)osd_data->length); | |
1d3576fd | 566 | if (rc >= 0) { |
79788c69 SW |
567 | /* |
568 | * Assume we wrote the pages we originally sent. The | |
569 | * osd might reply with fewer pages if our writeback | |
570 | * raced with a truncation and was adjusted at the osd, | |
571 | * so don't believe the reply. | |
572 | */ | |
e0c59487 | 573 | wrote = num_pages; |
1d3576fd SW |
574 | } else { |
575 | wrote = 0; | |
576 | mapping_set_error(mapping, rc); | |
577 | } | |
578 | dout("writepages_finish %p rc %d bytes %llu wrote %d (pages)\n", | |
579 | inode, rc, bytes, wrote); | |
580 | ||
581 | /* clean all pages */ | |
e0c59487 | 582 | for (i = 0; i < num_pages; i++) { |
87060c10 | 583 | page = osd_data->pages[i]; |
1d3576fd SW |
584 | BUG_ON(!page); |
585 | WARN_ON(!PageUptodate(page)); | |
586 | ||
2baba250 | 587 | writeback_stat = |
3d14c5d2 | 588 | atomic_long_dec_return(&fsc->writeback_count); |
2baba250 | 589 | if (writeback_stat < |
3d14c5d2 YS |
590 | CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb)) |
591 | clear_bdi_congested(&fsc->backing_dev_info, | |
2baba250 YS |
592 | BLK_RW_ASYNC); |
593 | ||
61600ef8 | 594 | ceph_put_snap_context(page_snap_context(page)); |
1d3576fd SW |
595 | page->private = 0; |
596 | ClearPagePrivate(page); | |
1d3576fd SW |
597 | dout("unlocking %d %p\n", i, page); |
598 | end_page_writeback(page); | |
e63dc5c7 YS |
599 | |
600 | /* | |
601 | * We lost the cache cap, need to truncate the page before | |
602 | * it is unlocked, otherwise we'd truncate it later in the | |
603 | * page truncation thread, possibly losing some data that | |
604 | * raced its way in | |
605 | */ | |
2962507c | 606 | if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) |
e63dc5c7 YS |
607 | generic_error_remove_page(inode->i_mapping, page); |
608 | ||
1d3576fd SW |
609 | unlock_page(page); |
610 | } | |
611 | dout("%p wrote+cleaned %d pages\n", inode, wrote); | |
e0c59487 | 612 | ceph_put_wrbuffer_cap_refs(ci, num_pages, snapc); |
1d3576fd | 613 | |
87060c10 AE |
614 | ceph_release_pages(osd_data->pages, num_pages); |
615 | if (osd_data->pages_from_pool) | |
616 | mempool_free(osd_data->pages, | |
640ef79d | 617 | ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool); |
1d3576fd | 618 | else |
87060c10 | 619 | kfree(osd_data->pages); |
1d3576fd SW |
620 | ceph_osdc_put_request(req); |
621 | } | |
622 | ||
1d3576fd SW |
623 | /* |
624 | * initiate async writeback | |
625 | */ | |
626 | static int ceph_writepages_start(struct address_space *mapping, | |
627 | struct writeback_control *wbc) | |
628 | { | |
629 | struct inode *inode = mapping->host; | |
1d3576fd | 630 | struct ceph_inode_info *ci = ceph_inode(inode); |
fc2744aa YZ |
631 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
632 | struct ceph_vino vino = ceph_vino(inode); | |
1d3576fd SW |
633 | pgoff_t index, start, end; |
634 | int range_whole = 0; | |
635 | int should_loop = 1; | |
636 | pgoff_t max_pages = 0, max_pages_ever = 0; | |
80e755fe | 637 | struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc; |
1d3576fd SW |
638 | struct pagevec pvec; |
639 | int done = 0; | |
640 | int rc = 0; | |
641 | unsigned wsize = 1 << inode->i_blkbits; | |
642 | struct ceph_osd_request *req = NULL; | |
643 | int do_sync; | |
fc2744aa YZ |
644 | u64 truncate_size, snap_size; |
645 | u32 truncate_seq; | |
1d3576fd SW |
646 | |
647 | /* | |
648 | * Include a 'sync' in the OSD request if this is a data | |
649 | * integrity write (e.g., O_SYNC write or fsync()), or if our | |
650 | * cap is being revoked. | |
651 | */ | |
c62988ec | 652 | if ((wbc->sync_mode == WB_SYNC_ALL) || |
653 | ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER)) | |
1d3576fd SW |
654 | do_sync = 1; |
655 | dout("writepages_start %p dosync=%d (mode=%s)\n", | |
656 | inode, do_sync, | |
657 | wbc->sync_mode == WB_SYNC_NONE ? "NONE" : | |
658 | (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD")); | |
659 | ||
3d14c5d2 | 660 | if (fsc->mount_state == CEPH_MOUNT_SHUTDOWN) { |
1d3576fd SW |
661 | pr_warning("writepage_start %p on forced umount\n", inode); |
662 | return -EIO; /* we're in a forced umount, don't write! */ | |
663 | } | |
3d14c5d2 YS |
664 | if (fsc->mount_options->wsize && fsc->mount_options->wsize < wsize) |
665 | wsize = fsc->mount_options->wsize; | |
1d3576fd SW |
666 | if (wsize < PAGE_CACHE_SIZE) |
667 | wsize = PAGE_CACHE_SIZE; | |
668 | max_pages_ever = wsize >> PAGE_CACHE_SHIFT; | |
669 | ||
670 | pagevec_init(&pvec, 0); | |
671 | ||
1d3576fd SW |
672 | /* where to start/end? */ |
673 | if (wbc->range_cyclic) { | |
674 | start = mapping->writeback_index; /* Start from prev offset */ | |
675 | end = -1; | |
676 | dout(" cyclic, start at %lu\n", start); | |
677 | } else { | |
678 | start = wbc->range_start >> PAGE_CACHE_SHIFT; | |
679 | end = wbc->range_end >> PAGE_CACHE_SHIFT; | |
680 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) | |
681 | range_whole = 1; | |
682 | should_loop = 0; | |
683 | dout(" not cyclic, %lu to %lu\n", start, end); | |
684 | } | |
685 | index = start; | |
686 | ||
687 | retry: | |
688 | /* find oldest snap context with dirty data */ | |
689 | ceph_put_snap_context(snapc); | |
1ac0fc8a | 690 | snap_size = 0; |
1d3576fd SW |
691 | snapc = get_oldest_context(inode, &snap_size); |
692 | if (!snapc) { | |
693 | /* hmm, why does writepages get called when there | |
694 | is no dirty data? */ | |
695 | dout(" no snap context with dirty data?\n"); | |
696 | goto out; | |
697 | } | |
1ac0fc8a YZ |
698 | if (snap_size == 0) |
699 | snap_size = i_size_read(inode); | |
1d3576fd SW |
700 | dout(" oldest snapc is %p seq %lld (%d snaps)\n", |
701 | snapc, snapc->seq, snapc->num_snaps); | |
fc2744aa YZ |
702 | |
703 | spin_lock(&ci->i_ceph_lock); | |
704 | truncate_seq = ci->i_truncate_seq; | |
705 | truncate_size = ci->i_truncate_size; | |
706 | if (!snap_size) | |
707 | snap_size = i_size_read(inode); | |
708 | spin_unlock(&ci->i_ceph_lock); | |
709 | ||
1d3576fd SW |
710 | if (last_snapc && snapc != last_snapc) { |
711 | /* if we switched to a newer snapc, restart our scan at the | |
712 | * start of the original file range. */ | |
713 | dout(" snapc differs from last pass, restarting at %lu\n", | |
714 | index); | |
715 | index = start; | |
716 | } | |
717 | last_snapc = snapc; | |
718 | ||
719 | while (!done && index <= end) { | |
e5975c7c | 720 | int num_ops = do_sync ? 2 : 1; |
1d3576fd SW |
721 | unsigned i; |
722 | int first; | |
723 | pgoff_t next; | |
724 | int pvec_pages, locked_pages; | |
e5975c7c AE |
725 | struct page **pages = NULL; |
726 | mempool_t *pool = NULL; /* Becomes non-null if mempool used */ | |
1d3576fd SW |
727 | struct page *page; |
728 | int want; | |
729 | u64 offset, len; | |
2baba250 | 730 | long writeback_stat; |
1d3576fd SW |
731 | |
732 | next = 0; | |
733 | locked_pages = 0; | |
734 | max_pages = max_pages_ever; | |
735 | ||
736 | get_more_pages: | |
737 | first = -1; | |
738 | want = min(end - index, | |
739 | min((pgoff_t)PAGEVEC_SIZE, | |
740 | max_pages - (pgoff_t)locked_pages) - 1) | |
741 | + 1; | |
742 | pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index, | |
743 | PAGECACHE_TAG_DIRTY, | |
744 | want); | |
745 | dout("pagevec_lookup_tag got %d\n", pvec_pages); | |
746 | if (!pvec_pages && !locked_pages) | |
747 | break; | |
748 | for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) { | |
749 | page = pvec.pages[i]; | |
750 | dout("? %p idx %lu\n", page, page->index); | |
751 | if (locked_pages == 0) | |
752 | lock_page(page); /* first page */ | |
753 | else if (!trylock_page(page)) | |
754 | break; | |
755 | ||
756 | /* only dirty pages, or our accounting breaks */ | |
757 | if (unlikely(!PageDirty(page)) || | |
758 | unlikely(page->mapping != mapping)) { | |
759 | dout("!dirty or !mapping %p\n", page); | |
760 | unlock_page(page); | |
761 | break; | |
762 | } | |
763 | if (!wbc->range_cyclic && page->index > end) { | |
764 | dout("end of range %p\n", page); | |
765 | done = 1; | |
766 | unlock_page(page); | |
767 | break; | |
768 | } | |
769 | if (next && (page->index != next)) { | |
770 | dout("not consecutive %p\n", page); | |
771 | unlock_page(page); | |
772 | break; | |
773 | } | |
774 | if (wbc->sync_mode != WB_SYNC_NONE) { | |
775 | dout("waiting on writeback %p\n", page); | |
776 | wait_on_page_writeback(page); | |
777 | } | |
1ac0fc8a YZ |
778 | if (page_offset(page) >= snap_size) { |
779 | dout("%p page eof %llu\n", page, snap_size); | |
1d3576fd SW |
780 | done = 1; |
781 | unlock_page(page); | |
782 | break; | |
783 | } | |
784 | if (PageWriteback(page)) { | |
785 | dout("%p under writeback\n", page); | |
786 | unlock_page(page); | |
787 | break; | |
788 | } | |
789 | ||
790 | /* only if matching snap context */ | |
61600ef8 | 791 | pgsnapc = page_snap_context(page); |
80e755fe SW |
792 | if (pgsnapc->seq > snapc->seq) { |
793 | dout("page snapc %p %lld > oldest %p %lld\n", | |
794 | pgsnapc, pgsnapc->seq, snapc, snapc->seq); | |
1d3576fd SW |
795 | unlock_page(page); |
796 | if (!locked_pages) | |
797 | continue; /* keep looking for snap */ | |
798 | break; | |
799 | } | |
800 | ||
801 | if (!clear_page_dirty_for_io(page)) { | |
802 | dout("%p !clear_page_dirty_for_io\n", page); | |
803 | unlock_page(page); | |
804 | break; | |
805 | } | |
806 | ||
e5975c7c AE |
807 | /* |
808 | * We have something to write. If this is | |
809 | * the first locked page this time through, | |
810 | * allocate an osd request and a page array | |
811 | * that it will use. | |
812 | */ | |
1d3576fd | 813 | if (locked_pages == 0) { |
e5975c7c | 814 | BUG_ON(pages); |
1d3576fd | 815 | /* prepare async write request */ |
e5975c7c | 816 | offset = (u64)page_offset(page); |
1d3576fd | 817 | len = wsize; |
fc2744aa YZ |
818 | req = ceph_osdc_new_request(&fsc->client->osdc, |
819 | &ci->i_layout, vino, | |
820 | offset, &len, num_ops, | |
821 | CEPH_OSD_OP_WRITE, | |
822 | CEPH_OSD_FLAG_WRITE | | |
823 | CEPH_OSD_FLAG_ONDISK, | |
824 | snapc, truncate_seq, | |
825 | truncate_size, true); | |
6816282d SW |
826 | if (IS_ERR(req)) { |
827 | rc = PTR_ERR(req); | |
8c71897b HC |
828 | unlock_page(page); |
829 | break; | |
830 | } | |
831 | ||
88486957 AE |
832 | req->r_callback = writepages_finish; |
833 | req->r_inode = inode; | |
834 | ||
835 | max_pages = calc_pages_for(0, (u64)len); | |
fc2744aa YZ |
836 | pages = kmalloc(max_pages * sizeof (*pages), |
837 | GFP_NOFS); | |
88486957 AE |
838 | if (!pages) { |
839 | pool = fsc->wb_pagevec_pool; | |
88486957 | 840 | pages = mempool_alloc(pool, GFP_NOFS); |
e5975c7c | 841 | BUG_ON(!pages); |
88486957 | 842 | } |
1d3576fd SW |
843 | } |
844 | ||
845 | /* note position of first page in pvec */ | |
846 | if (first < 0) | |
847 | first = i; | |
848 | dout("%p will write page %p idx %lu\n", | |
849 | inode, page, page->index); | |
2baba250 | 850 | |
213c99ee | 851 | writeback_stat = |
3d14c5d2 | 852 | atomic_long_inc_return(&fsc->writeback_count); |
213c99ee | 853 | if (writeback_stat > CONGESTION_ON_THRESH( |
3d14c5d2 YS |
854 | fsc->mount_options->congestion_kb)) { |
855 | set_bdi_congested(&fsc->backing_dev_info, | |
213c99ee | 856 | BLK_RW_ASYNC); |
2baba250 YS |
857 | } |
858 | ||
1d3576fd | 859 | set_page_writeback(page); |
e5975c7c | 860 | pages[locked_pages] = page; |
1d3576fd SW |
861 | locked_pages++; |
862 | next = page->index + 1; | |
863 | } | |
864 | ||
865 | /* did we get anything? */ | |
866 | if (!locked_pages) | |
867 | goto release_pvec_pages; | |
868 | if (i) { | |
869 | int j; | |
870 | BUG_ON(!locked_pages || first < 0); | |
871 | ||
872 | if (pvec_pages && i == pvec_pages && | |
873 | locked_pages < max_pages) { | |
874 | dout("reached end pvec, trying for more\n"); | |
875 | pagevec_reinit(&pvec); | |
876 | goto get_more_pages; | |
877 | } | |
878 | ||
879 | /* shift unused pages over in the pvec... we | |
880 | * will need to release them below. */ | |
881 | for (j = i; j < pvec_pages; j++) { | |
882 | dout(" pvec leftover page %p\n", | |
883 | pvec.pages[j]); | |
884 | pvec.pages[j-i+first] = pvec.pages[j]; | |
885 | } | |
886 | pvec.nr -= i-first; | |
887 | } | |
888 | ||
e5975c7c AE |
889 | /* Format the osd request message and submit the write */ |
890 | ||
891 | offset = page_offset(pages[0]); | |
1ac0fc8a | 892 | len = min(snap_size - offset, |
1d3576fd SW |
893 | (u64)locked_pages << PAGE_CACHE_SHIFT); |
894 | dout("writepages got %d pages at %llu~%llu\n", | |
895 | locked_pages, offset, len); | |
896 | ||
406e2c9f | 897 | osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, |
a4ce40a9 | 898 | !!pool, false); |
e5975c7c AE |
899 | |
900 | pages = NULL; /* request message now owns the pages array */ | |
901 | pool = NULL; | |
902 | ||
903 | /* Update the write op length in case we changed it */ | |
904 | ||
c99d2d4a | 905 | osd_req_op_extent_update(req, 0, len); |
e5975c7c AE |
906 | |
907 | vino = ceph_vino(inode); | |
79528734 AE |
908 | ceph_osdc_build_request(req, offset, snapc, vino.snap, |
909 | &inode->i_mtime); | |
1d3576fd | 910 | |
9d6fcb08 SW |
911 | rc = ceph_osdc_start_request(&fsc->client->osdc, req, true); |
912 | BUG_ON(rc); | |
1d3576fd SW |
913 | req = NULL; |
914 | ||
915 | /* continue? */ | |
916 | index = next; | |
917 | wbc->nr_to_write -= locked_pages; | |
918 | if (wbc->nr_to_write <= 0) | |
919 | done = 1; | |
920 | ||
921 | release_pvec_pages: | |
922 | dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr, | |
923 | pvec.nr ? pvec.pages[0] : NULL); | |
924 | pagevec_release(&pvec); | |
925 | ||
926 | if (locked_pages && !done) | |
927 | goto retry; | |
928 | } | |
929 | ||
930 | if (should_loop && !done) { | |
931 | /* more to do; loop back to beginning of file */ | |
932 | dout("writepages looping back to beginning of file\n"); | |
933 | should_loop = 0; | |
934 | index = 0; | |
935 | goto retry; | |
936 | } | |
937 | ||
938 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) | |
939 | mapping->writeback_index = index; | |
940 | ||
941 | out: | |
942 | if (req) | |
943 | ceph_osdc_put_request(req); | |
1d3576fd SW |
944 | ceph_put_snap_context(snapc); |
945 | dout("writepages done, rc = %d\n", rc); | |
1d3576fd SW |
946 | return rc; |
947 | } | |
948 | ||
949 | ||
950 | ||
951 | /* | |
952 | * See if a given @snapc is either writeable, or already written. | |
953 | */ | |
954 | static int context_is_writeable_or_written(struct inode *inode, | |
955 | struct ceph_snap_context *snapc) | |
956 | { | |
957 | struct ceph_snap_context *oldest = get_oldest_context(inode, NULL); | |
6298a337 SW |
958 | int ret = !oldest || snapc->seq <= oldest->seq; |
959 | ||
960 | ceph_put_snap_context(oldest); | |
961 | return ret; | |
1d3576fd SW |
962 | } |
963 | ||
964 | /* | |
965 | * We are only allowed to write into/dirty the page if the page is | |
966 | * clean, or already dirty within the same snap context. | |
8f883c24 SW |
967 | * |
968 | * called with page locked. | |
969 | * return success with page locked, | |
970 | * or any failure (incl -EAGAIN) with page unlocked. | |
1d3576fd | 971 | */ |
4af6b225 YS |
972 | static int ceph_update_writeable_page(struct file *file, |
973 | loff_t pos, unsigned len, | |
974 | struct page *page) | |
1d3576fd | 975 | { |
496ad9aa | 976 | struct inode *inode = file_inode(file); |
1d3576fd | 977 | struct ceph_inode_info *ci = ceph_inode(inode); |
3d14c5d2 | 978 | struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc; |
1d3576fd SW |
979 | loff_t page_off = pos & PAGE_CACHE_MASK; |
980 | int pos_in_page = pos & ~PAGE_CACHE_MASK; | |
981 | int end_in_page = pos_in_page + len; | |
982 | loff_t i_size; | |
1d3576fd | 983 | int r; |
80e755fe | 984 | struct ceph_snap_context *snapc, *oldest; |
1d3576fd | 985 | |
1d3576fd SW |
986 | retry_locked: |
987 | /* writepages currently holds page lock, but if we change that later, */ | |
988 | wait_on_page_writeback(page); | |
989 | ||
990 | /* check snap context */ | |
991 | BUG_ON(!ci->i_snap_realm); | |
992 | down_read(&mdsc->snap_rwsem); | |
993 | BUG_ON(!ci->i_snap_realm->cached_context); | |
61600ef8 | 994 | snapc = page_snap_context(page); |
80e755fe | 995 | if (snapc && snapc != ci->i_head_snapc) { |
1d3576fd SW |
996 | /* |
997 | * this page is already dirty in another (older) snap | |
998 | * context! is it writeable now? | |
999 | */ | |
80e755fe | 1000 | oldest = get_oldest_context(inode, NULL); |
1d3576fd SW |
1001 | up_read(&mdsc->snap_rwsem); |
1002 | ||
80e755fe | 1003 | if (snapc->seq > oldest->seq) { |
6298a337 | 1004 | ceph_put_snap_context(oldest); |
1d3576fd | 1005 | dout(" page %p snapc %p not current or oldest\n", |
6298a337 | 1006 | page, snapc); |
1d3576fd SW |
1007 | /* |
1008 | * queue for writeback, and wait for snapc to | |
1009 | * be writeable or written | |
1010 | */ | |
6298a337 | 1011 | snapc = ceph_get_snap_context(snapc); |
1d3576fd | 1012 | unlock_page(page); |
3c6f6b79 | 1013 | ceph_queue_writeback(inode); |
8f883c24 | 1014 | r = wait_event_interruptible(ci->i_cap_wq, |
1d3576fd SW |
1015 | context_is_writeable_or_written(inode, snapc)); |
1016 | ceph_put_snap_context(snapc); | |
8f883c24 SW |
1017 | if (r == -ERESTARTSYS) |
1018 | return r; | |
4af6b225 | 1019 | return -EAGAIN; |
1d3576fd | 1020 | } |
6298a337 | 1021 | ceph_put_snap_context(oldest); |
1d3576fd SW |
1022 | |
1023 | /* yay, writeable, do it now (without dropping page lock) */ | |
1024 | dout(" page %p snapc %p not current, but oldest\n", | |
1025 | page, snapc); | |
1026 | if (!clear_page_dirty_for_io(page)) | |
1027 | goto retry_locked; | |
1028 | r = writepage_nounlock(page, NULL); | |
1029 | if (r < 0) | |
1030 | goto fail_nosnap; | |
1031 | goto retry_locked; | |
1032 | } | |
1033 | ||
1034 | if (PageUptodate(page)) { | |
1035 | dout(" page %p already uptodate\n", page); | |
1036 | return 0; | |
1037 | } | |
1038 | ||
1039 | /* full page? */ | |
1040 | if (pos_in_page == 0 && len == PAGE_CACHE_SIZE) | |
1041 | return 0; | |
1042 | ||
1043 | /* past end of file? */ | |
1044 | i_size = inode->i_size; /* caller holds i_mutex */ | |
1045 | ||
1046 | if (i_size + len > inode->i_sb->s_maxbytes) { | |
1047 | /* file is too big */ | |
1048 | r = -EINVAL; | |
1049 | goto fail; | |
1050 | } | |
1051 | ||
1052 | if (page_off >= i_size || | |
1053 | (pos_in_page == 0 && (pos+len) >= i_size && | |
1054 | end_in_page - pos_in_page != PAGE_CACHE_SIZE)) { | |
1055 | dout(" zeroing %p 0 - %d and %d - %d\n", | |
1056 | page, pos_in_page, end_in_page, (int)PAGE_CACHE_SIZE); | |
1057 | zero_user_segments(page, | |
1058 | 0, pos_in_page, | |
1059 | end_in_page, PAGE_CACHE_SIZE); | |
1060 | return 0; | |
1061 | } | |
1062 | ||
1063 | /* we need to read it. */ | |
1064 | up_read(&mdsc->snap_rwsem); | |
1065 | r = readpage_nounlock(file, page); | |
1066 | if (r < 0) | |
1067 | goto fail_nosnap; | |
1068 | goto retry_locked; | |
1069 | ||
1070 | fail: | |
1071 | up_read(&mdsc->snap_rwsem); | |
1072 | fail_nosnap: | |
1073 | unlock_page(page); | |
1074 | return r; | |
1075 | } | |
1076 | ||
4af6b225 YS |
1077 | /* |
1078 | * We are only allowed to write into/dirty the page if the page is | |
1079 | * clean, or already dirty within the same snap context. | |
1080 | */ | |
1081 | static int ceph_write_begin(struct file *file, struct address_space *mapping, | |
1082 | loff_t pos, unsigned len, unsigned flags, | |
1083 | struct page **pagep, void **fsdata) | |
1084 | { | |
496ad9aa | 1085 | struct inode *inode = file_inode(file); |
4af6b225 YS |
1086 | struct page *page; |
1087 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; | |
7971bd92 | 1088 | int r; |
4af6b225 YS |
1089 | |
1090 | do { | |
8f883c24 | 1091 | /* get a page */ |
4af6b225 | 1092 | page = grab_cache_page_write_begin(mapping, index, 0); |
7971bd92 SW |
1093 | if (!page) |
1094 | return -ENOMEM; | |
1095 | *pagep = page; | |
4af6b225 YS |
1096 | |
1097 | dout("write_begin file %p inode %p page %p %d~%d\n", file, | |
213c99ee | 1098 | inode, page, (int)pos, (int)len); |
4af6b225 YS |
1099 | |
1100 | r = ceph_update_writeable_page(file, pos, len, page); | |
1101 | } while (r == -EAGAIN); | |
1102 | ||
1103 | return r; | |
1104 | } | |
1105 | ||
1d3576fd SW |
1106 | /* |
1107 | * we don't do anything in here that simple_write_end doesn't do | |
1108 | * except adjust dirty page accounting and drop read lock on | |
1109 | * mdsc->snap_rwsem. | |
1110 | */ | |
1111 | static int ceph_write_end(struct file *file, struct address_space *mapping, | |
1112 | loff_t pos, unsigned len, unsigned copied, | |
1113 | struct page *page, void *fsdata) | |
1114 | { | |
496ad9aa | 1115 | struct inode *inode = file_inode(file); |
3d14c5d2 YS |
1116 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
1117 | struct ceph_mds_client *mdsc = fsc->mdsc; | |
1d3576fd SW |
1118 | unsigned from = pos & (PAGE_CACHE_SIZE - 1); |
1119 | int check_cap = 0; | |
1120 | ||
1121 | dout("write_end file %p inode %p page %p %d~%d (%d)\n", file, | |
1122 | inode, page, (int)pos, (int)copied, (int)len); | |
1123 | ||
1124 | /* zero the stale part of the page if we did a short copy */ | |
1125 | if (copied < len) | |
1126 | zero_user_segment(page, from+copied, len); | |
1127 | ||
1128 | /* did file size increase? */ | |
1129 | /* (no need for i_size_read(); we caller holds i_mutex */ | |
1130 | if (pos+copied > inode->i_size) | |
1131 | check_cap = ceph_inode_set_size(inode, pos+copied); | |
1132 | ||
1133 | if (!PageUptodate(page)) | |
1134 | SetPageUptodate(page); | |
1135 | ||
1136 | set_page_dirty(page); | |
1137 | ||
1138 | unlock_page(page); | |
1139 | up_read(&mdsc->snap_rwsem); | |
1140 | page_cache_release(page); | |
1141 | ||
1142 | if (check_cap) | |
1143 | ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL); | |
1144 | ||
1145 | return copied; | |
1146 | } | |
1147 | ||
1148 | /* | |
1149 | * we set .direct_IO to indicate direct io is supported, but since we | |
1150 | * intercept O_DIRECT reads and writes early, this function should | |
1151 | * never get called. | |
1152 | */ | |
1153 | static ssize_t ceph_direct_io(int rw, struct kiocb *iocb, | |
1154 | const struct iovec *iov, | |
1155 | loff_t pos, unsigned long nr_segs) | |
1156 | { | |
1157 | WARN_ON(1); | |
1158 | return -EINVAL; | |
1159 | } | |
1160 | ||
1161 | const struct address_space_operations ceph_aops = { | |
1162 | .readpage = ceph_readpage, | |
1163 | .readpages = ceph_readpages, | |
1164 | .writepage = ceph_writepage, | |
1165 | .writepages = ceph_writepages_start, | |
1166 | .write_begin = ceph_write_begin, | |
1167 | .write_end = ceph_write_end, | |
1168 | .set_page_dirty = ceph_set_page_dirty, | |
1169 | .invalidatepage = ceph_invalidatepage, | |
1170 | .releasepage = ceph_releasepage, | |
1171 | .direct_IO = ceph_direct_io, | |
1172 | }; | |
1173 | ||
1174 | ||
1175 | /* | |
1176 | * vm ops | |
1177 | */ | |
1178 | ||
1179 | /* | |
1180 | * Reuse write_begin here for simplicity. | |
1181 | */ | |
1182 | static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) | |
1183 | { | |
496ad9aa | 1184 | struct inode *inode = file_inode(vma->vm_file); |
1d3576fd | 1185 | struct page *page = vmf->page; |
3d14c5d2 | 1186 | struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc; |
6285bc23 | 1187 | loff_t off = page_offset(page); |
1d3576fd | 1188 | loff_t size, len; |
1d3576fd SW |
1189 | int ret; |
1190 | ||
3ca9c3bd JK |
1191 | /* Update time before taking page lock */ |
1192 | file_update_time(vma->vm_file); | |
1193 | ||
1d3576fd SW |
1194 | size = i_size_read(inode); |
1195 | if (off + PAGE_CACHE_SIZE <= size) | |
1196 | len = PAGE_CACHE_SIZE; | |
1197 | else | |
1198 | len = size & ~PAGE_CACHE_MASK; | |
1199 | ||
1200 | dout("page_mkwrite %p %llu~%llu page %p idx %lu\n", inode, | |
1201 | off, len, page, page->index); | |
4af6b225 YS |
1202 | |
1203 | lock_page(page); | |
1204 | ||
1205 | ret = VM_FAULT_NOPAGE; | |
1206 | if ((off > size) || | |
1207 | (page->mapping != inode->i_mapping)) | |
1208 | goto out; | |
1209 | ||
1210 | ret = ceph_update_writeable_page(vma->vm_file, off, len, page); | |
1211 | if (ret == 0) { | |
1212 | /* success. we'll keep the page locked. */ | |
1d3576fd SW |
1213 | set_page_dirty(page); |
1214 | up_read(&mdsc->snap_rwsem); | |
1d3576fd SW |
1215 | ret = VM_FAULT_LOCKED; |
1216 | } else { | |
4af6b225 YS |
1217 | if (ret == -ENOMEM) |
1218 | ret = VM_FAULT_OOM; | |
1219 | else | |
1220 | ret = VM_FAULT_SIGBUS; | |
1d3576fd | 1221 | } |
4af6b225 | 1222 | out: |
1d3576fd | 1223 | dout("page_mkwrite %p %llu~%llu = %d\n", inode, off, len, ret); |
4af6b225 YS |
1224 | if (ret != VM_FAULT_LOCKED) |
1225 | unlock_page(page); | |
1d3576fd SW |
1226 | return ret; |
1227 | } | |
1228 | ||
1229 | static struct vm_operations_struct ceph_vmops = { | |
1230 | .fault = filemap_fault, | |
1231 | .page_mkwrite = ceph_page_mkwrite, | |
0b173bc4 | 1232 | .remap_pages = generic_file_remap_pages, |
1d3576fd SW |
1233 | }; |
1234 | ||
1235 | int ceph_mmap(struct file *file, struct vm_area_struct *vma) | |
1236 | { | |
1237 | struct address_space *mapping = file->f_mapping; | |
1238 | ||
1239 | if (!mapping->a_ops->readpage) | |
1240 | return -ENOEXEC; | |
1241 | file_accessed(file); | |
1242 | vma->vm_ops = &ceph_vmops; | |
1d3576fd SW |
1243 | return 0; |
1244 | } |