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 | 13 | #include "mds_client.h" |
99ccbd22 | 14 | #include "cache.h" |
3d14c5d2 | 15 | #include <linux/ceph/osd_client.h> |
1d3576fd SW |
16 | |
17 | /* | |
18 | * Ceph address space ops. | |
19 | * | |
20 | * There are a few funny things going on here. | |
21 | * | |
22 | * The page->private field is used to reference a struct | |
23 | * ceph_snap_context for _every_ dirty page. This indicates which | |
24 | * snapshot the page was logically dirtied in, and thus which snap | |
25 | * context needs to be associated with the osd write during writeback. | |
26 | * | |
27 | * Similarly, struct ceph_inode_info maintains a set of counters to | |
25985edc | 28 | * count dirty pages on the inode. In the absence of snapshots, |
1d3576fd SW |
29 | * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count. |
30 | * | |
31 | * When a snapshot is taken (that is, when the client receives | |
32 | * notification that a snapshot was taken), each inode with caps and | |
33 | * with dirty pages (dirty pages implies there is a cap) gets a new | |
34 | * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending | |
35 | * order, new snaps go to the tail). The i_wrbuffer_ref_head count is | |
36 | * moved to capsnap->dirty. (Unless a sync write is currently in | |
37 | * progress. In that case, the capsnap is said to be "pending", new | |
38 | * writes cannot start, and the capsnap isn't "finalized" until the | |
39 | * write completes (or fails) and a final size/mtime for the inode for | |
40 | * that snap can be settled upon.) i_wrbuffer_ref_head is reset to 0. | |
41 | * | |
42 | * On writeback, we must submit writes to the osd IN SNAP ORDER. So, | |
43 | * we look for the first capsnap in i_cap_snaps and write out pages in | |
44 | * that snap context _only_. Then we move on to the next capsnap, | |
45 | * eventually reaching the "live" or "head" context (i.e., pages that | |
46 | * are not yet snapped) and are writing the most recently dirtied | |
47 | * pages. | |
48 | * | |
49 | * Invalidate and so forth must take care to ensure the dirty page | |
50 | * accounting is preserved. | |
51 | */ | |
52 | ||
2baba250 YS |
53 | #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10)) |
54 | #define CONGESTION_OFF_THRESH(congestion_kb) \ | |
55 | (CONGESTION_ON_THRESH(congestion_kb) - \ | |
56 | (CONGESTION_ON_THRESH(congestion_kb) >> 2)) | |
57 | ||
61600ef8 YZ |
58 | static inline struct ceph_snap_context *page_snap_context(struct page *page) |
59 | { | |
60 | if (PagePrivate(page)) | |
61 | return (void *)page->private; | |
62 | return NULL; | |
63 | } | |
1d3576fd SW |
64 | |
65 | /* | |
66 | * Dirty a page. Optimistically adjust accounting, on the assumption | |
67 | * that we won't race with invalidate. If we do, readjust. | |
68 | */ | |
69 | static int ceph_set_page_dirty(struct page *page) | |
70 | { | |
71 | struct address_space *mapping = page->mapping; | |
72 | struct inode *inode; | |
73 | struct ceph_inode_info *ci; | |
1d3576fd | 74 | struct ceph_snap_context *snapc; |
7d6e1f54 | 75 | int ret; |
1d3576fd SW |
76 | |
77 | if (unlikely(!mapping)) | |
78 | return !TestSetPageDirty(page); | |
79 | ||
7d6e1f54 | 80 | if (PageDirty(page)) { |
1d3576fd SW |
81 | dout("%p set_page_dirty %p idx %lu -- already dirty\n", |
82 | mapping->host, page, page->index); | |
7d6e1f54 | 83 | BUG_ON(!PagePrivate(page)); |
1d3576fd SW |
84 | return 0; |
85 | } | |
86 | ||
87 | inode = mapping->host; | |
88 | ci = ceph_inode(inode); | |
89 | ||
1d3576fd | 90 | /* dirty the head */ |
be655596 | 91 | spin_lock(&ci->i_ceph_lock); |
5dda377c YZ |
92 | BUG_ON(ci->i_wr_ref == 0); // caller should hold Fw reference |
93 | if (__ceph_have_pending_cap_snap(ci)) { | |
94 | struct ceph_cap_snap *capsnap = | |
95 | list_last_entry(&ci->i_cap_snaps, | |
96 | struct ceph_cap_snap, | |
97 | ci_item); | |
98 | snapc = ceph_get_snap_context(capsnap->context); | |
99 | capsnap->dirty_pages++; | |
100 | } else { | |
101 | BUG_ON(!ci->i_head_snapc); | |
102 | snapc = ceph_get_snap_context(ci->i_head_snapc); | |
103 | ++ci->i_wrbuffer_ref_head; | |
104 | } | |
1d3576fd | 105 | if (ci->i_wrbuffer_ref == 0) |
0444d76a | 106 | ihold(inode); |
1d3576fd SW |
107 | ++ci->i_wrbuffer_ref; |
108 | dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d " | |
109 | "snapc %p seq %lld (%d snaps)\n", | |
110 | mapping->host, page, page->index, | |
111 | ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1, | |
112 | ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head, | |
113 | snapc, snapc->seq, snapc->num_snaps); | |
be655596 | 114 | spin_unlock(&ci->i_ceph_lock); |
1d3576fd | 115 | |
7d6e1f54 SZ |
116 | /* |
117 | * Reference snap context in page->private. Also set | |
118 | * PagePrivate so that we get invalidatepage callback. | |
119 | */ | |
120 | BUG_ON(PagePrivate(page)); | |
121 | page->private = (unsigned long)snapc; | |
122 | SetPagePrivate(page); | |
1d3576fd | 123 | |
7d6e1f54 SZ |
124 | ret = __set_page_dirty_nobuffers(page); |
125 | WARN_ON(!PageLocked(page)); | |
126 | WARN_ON(!page->mapping); | |
1d3576fd | 127 | |
7d6e1f54 | 128 | return ret; |
1d3576fd SW |
129 | } |
130 | ||
131 | /* | |
132 | * If we are truncating the full page (i.e. offset == 0), adjust the | |
133 | * dirty page counters appropriately. Only called if there is private | |
134 | * data on the page. | |
135 | */ | |
d47992f8 LC |
136 | static void ceph_invalidatepage(struct page *page, unsigned int offset, |
137 | unsigned int length) | |
1d3576fd | 138 | { |
4ce1e9ad | 139 | struct inode *inode; |
1d3576fd | 140 | struct ceph_inode_info *ci; |
61600ef8 | 141 | struct ceph_snap_context *snapc = page_snap_context(page); |
1d3576fd | 142 | |
4ce1e9ad | 143 | inode = page->mapping->host; |
b150f5c1 MT |
144 | ci = ceph_inode(inode); |
145 | ||
146 | if (offset != 0 || length != PAGE_CACHE_SIZE) { | |
147 | dout("%p invalidatepage %p idx %lu partial dirty page %u~%u\n", | |
148 | inode, page, page->index, offset, length); | |
149 | return; | |
150 | } | |
4ce1e9ad | 151 | |
99ccbd22 MT |
152 | ceph_invalidate_fscache_page(inode, page); |
153 | ||
154 | if (!PagePrivate(page)) | |
155 | return; | |
156 | ||
1d3576fd SW |
157 | /* |
158 | * We can get non-dirty pages here due to races between | |
159 | * set_page_dirty and truncate_complete_page; just spit out a | |
160 | * warning, in case we end up with accounting problems later. | |
161 | */ | |
162 | if (!PageDirty(page)) | |
163 | pr_err("%p invalidatepage %p page not dirty\n", inode, page); | |
164 | ||
b150f5c1 | 165 | ClearPageChecked(page); |
1d3576fd | 166 | |
b150f5c1 MT |
167 | dout("%p invalidatepage %p idx %lu full dirty page\n", |
168 | inode, page, page->index); | |
169 | ||
170 | ceph_put_wrbuffer_cap_refs(ci, 1, snapc); | |
171 | ceph_put_snap_context(snapc); | |
172 | page->private = 0; | |
173 | ClearPagePrivate(page); | |
1d3576fd SW |
174 | } |
175 | ||
1d3576fd SW |
176 | static int ceph_releasepage(struct page *page, gfp_t g) |
177 | { | |
178 | struct inode *inode = page->mapping ? page->mapping->host : NULL; | |
179 | dout("%p releasepage %p idx %lu\n", inode, page, page->index); | |
180 | WARN_ON(PageDirty(page)); | |
99ccbd22 MT |
181 | |
182 | /* Can we release the page from the cache? */ | |
183 | if (!ceph_release_fscache_page(page, g)) | |
184 | return 0; | |
185 | ||
186 | return !PagePrivate(page); | |
1d3576fd SW |
187 | } |
188 | ||
189 | /* | |
190 | * read a single page, without unlocking it. | |
191 | */ | |
192 | static int readpage_nounlock(struct file *filp, struct page *page) | |
193 | { | |
496ad9aa | 194 | struct inode *inode = file_inode(filp); |
1d3576fd | 195 | struct ceph_inode_info *ci = ceph_inode(inode); |
99ccbd22 | 196 | struct ceph_osd_client *osdc = |
3d14c5d2 | 197 | &ceph_inode_to_client(inode)->client->osdc; |
1d3576fd | 198 | int err = 0; |
83701246 | 199 | u64 off = page_offset(page); |
1d3576fd SW |
200 | u64 len = PAGE_CACHE_SIZE; |
201 | ||
83701246 | 202 | if (off >= i_size_read(inode)) { |
fcc02d2a | 203 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
83701246 YZ |
204 | SetPageUptodate(page); |
205 | return 0; | |
206 | } | |
99ccbd22 | 207 | |
fcc02d2a YZ |
208 | if (ci->i_inline_version != CEPH_INLINE_NONE) { |
209 | /* | |
210 | * Uptodate inline data should have been added | |
211 | * into page cache while getting Fcr caps. | |
212 | */ | |
213 | if (off == 0) | |
214 | return -EINVAL; | |
215 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); | |
216 | SetPageUptodate(page); | |
217 | return 0; | |
218 | } | |
83701246 YZ |
219 | |
220 | err = ceph_readpage_from_fscache(inode, page); | |
99ccbd22 MT |
221 | if (err == 0) |
222 | goto out; | |
223 | ||
1d3576fd SW |
224 | dout("readpage inode %p file %p page %p index %lu\n", |
225 | inode, filp, page, page->index); | |
226 | err = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout, | |
83701246 | 227 | off, &len, |
1d3576fd | 228 | ci->i_truncate_seq, ci->i_truncate_size, |
b7495fc2 | 229 | &page, 1, 0); |
1d3576fd SW |
230 | if (err == -ENOENT) |
231 | err = 0; | |
232 | if (err < 0) { | |
233 | SetPageError(page); | |
18302805 | 234 | ceph_fscache_readpage_cancel(inode, page); |
1d3576fd | 235 | goto out; |
1d3576fd | 236 | } |
23cd573b ZZ |
237 | if (err < PAGE_CACHE_SIZE) |
238 | /* zero fill remainder of page */ | |
239 | zero_user_segment(page, err, PAGE_CACHE_SIZE); | |
240 | else | |
241 | flush_dcache_page(page); | |
1d3576fd | 242 | |
23cd573b ZZ |
243 | SetPageUptodate(page); |
244 | ceph_readpage_to_fscache(inode, page); | |
99ccbd22 | 245 | |
1d3576fd SW |
246 | out: |
247 | return err < 0 ? err : 0; | |
248 | } | |
249 | ||
250 | static int ceph_readpage(struct file *filp, struct page *page) | |
251 | { | |
252 | int r = readpage_nounlock(filp, page); | |
253 | unlock_page(page); | |
254 | return r; | |
255 | } | |
256 | ||
257 | /* | |
7c272194 | 258 | * Finish an async read(ahead) op. |
1d3576fd | 259 | */ |
7c272194 | 260 | static void finish_read(struct ceph_osd_request *req, struct ceph_msg *msg) |
1d3576fd | 261 | { |
7c272194 | 262 | struct inode *inode = req->r_inode; |
87060c10 | 263 | struct ceph_osd_data *osd_data; |
1b83bef2 SW |
264 | int rc = req->r_result; |
265 | int bytes = le32_to_cpu(msg->hdr.data_len); | |
e0c59487 | 266 | int num_pages; |
7c272194 | 267 | int i; |
1d3576fd | 268 | |
7c272194 SW |
269 | dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes); |
270 | ||
271 | /* unlock all pages, zeroing any data we didn't read */ | |
406e2c9f | 272 | osd_data = osd_req_op_extent_osd_data(req, 0); |
87060c10 AE |
273 | BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES); |
274 | num_pages = calc_pages_for((u64)osd_data->alignment, | |
275 | (u64)osd_data->length); | |
e0c59487 | 276 | for (i = 0; i < num_pages; i++) { |
87060c10 | 277 | struct page *page = osd_data->pages[i]; |
7c272194 | 278 | |
f36132a7 LW |
279 | if (rc < 0) |
280 | goto unlock; | |
7c272194 SW |
281 | if (bytes < (int)PAGE_CACHE_SIZE) { |
282 | /* zero (remainder of) page */ | |
283 | int s = bytes < 0 ? 0 : bytes; | |
284 | zero_user_segment(page, s, PAGE_CACHE_SIZE); | |
1d3576fd | 285 | } |
7c272194 SW |
286 | dout("finish_read %p uptodate %p idx %lu\n", inode, page, |
287 | page->index); | |
288 | flush_dcache_page(page); | |
289 | SetPageUptodate(page); | |
99ccbd22 | 290 | ceph_readpage_to_fscache(inode, page); |
f36132a7 | 291 | unlock: |
7c272194 SW |
292 | unlock_page(page); |
293 | page_cache_release(page); | |
0fff87ec | 294 | bytes -= PAGE_CACHE_SIZE; |
1d3576fd | 295 | } |
87060c10 | 296 | kfree(osd_data->pages); |
1d3576fd SW |
297 | } |
298 | ||
8884d53d DZ |
299 | static void ceph_unlock_page_vector(struct page **pages, int num_pages) |
300 | { | |
301 | int i; | |
302 | ||
303 | for (i = 0; i < num_pages; i++) | |
304 | unlock_page(pages[i]); | |
305 | } | |
306 | ||
1d3576fd | 307 | /* |
7c272194 SW |
308 | * start an async read(ahead) operation. return nr_pages we submitted |
309 | * a read for on success, or negative error code. | |
1d3576fd | 310 | */ |
0d66a487 | 311 | static int start_read(struct inode *inode, struct list_head *page_list, int max) |
1d3576fd | 312 | { |
3d14c5d2 YS |
313 | struct ceph_osd_client *osdc = |
314 | &ceph_inode_to_client(inode)->client->osdc; | |
7c272194 SW |
315 | struct ceph_inode_info *ci = ceph_inode(inode); |
316 | struct page *page = list_entry(page_list->prev, struct page, lru); | |
acead002 | 317 | struct ceph_vino vino; |
7c272194 SW |
318 | struct ceph_osd_request *req; |
319 | u64 off; | |
1d3576fd | 320 | u64 len; |
7c272194 SW |
321 | int i; |
322 | struct page **pages; | |
323 | pgoff_t next_index; | |
324 | int nr_pages = 0; | |
325 | int ret; | |
1d3576fd | 326 | |
6285bc23 | 327 | off = (u64) page_offset(page); |
1d3576fd | 328 | |
7c272194 SW |
329 | /* count pages */ |
330 | next_index = page->index; | |
331 | list_for_each_entry_reverse(page, page_list, lru) { | |
332 | if (page->index != next_index) | |
333 | break; | |
334 | nr_pages++; | |
335 | next_index++; | |
0d66a487 SW |
336 | if (max && nr_pages == max) |
337 | break; | |
7c272194 | 338 | } |
1d3576fd | 339 | len = nr_pages << PAGE_CACHE_SHIFT; |
7c272194 SW |
340 | dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages, |
341 | off, len); | |
acead002 AE |
342 | vino = ceph_vino(inode); |
343 | req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len, | |
715e4cd4 | 344 | 0, 1, CEPH_OSD_OP_READ, |
acead002 | 345 | CEPH_OSD_FLAG_READ, NULL, |
7c272194 | 346 | ci->i_truncate_seq, ci->i_truncate_size, |
acead002 | 347 | false); |
6816282d SW |
348 | if (IS_ERR(req)) |
349 | return PTR_ERR(req); | |
1d3576fd | 350 | |
7c272194 | 351 | /* build page vector */ |
cf7b7e14 | 352 | nr_pages = calc_pages_for(0, len); |
687265e5 | 353 | pages = kmalloc(sizeof(*pages) * nr_pages, GFP_KERNEL); |
7c272194 SW |
354 | ret = -ENOMEM; |
355 | if (!pages) | |
356 | goto out; | |
357 | for (i = 0; i < nr_pages; ++i) { | |
358 | page = list_entry(page_list->prev, struct page, lru); | |
359 | BUG_ON(PageLocked(page)); | |
1d3576fd | 360 | list_del(&page->lru); |
99ccbd22 | 361 | |
7c272194 SW |
362 | dout("start_read %p adding %p idx %lu\n", inode, page, |
363 | page->index); | |
364 | if (add_to_page_cache_lru(page, &inode->i_data, page->index, | |
687265e5 | 365 | GFP_KERNEL)) { |
d4d3aa38 | 366 | ceph_fscache_uncache_page(inode, page); |
1d3576fd | 367 | page_cache_release(page); |
7c272194 | 368 | dout("start_read %p add_to_page_cache failed %p\n", |
1d3576fd | 369 | inode, page); |
7c272194 SW |
370 | nr_pages = i; |
371 | goto out_pages; | |
1d3576fd | 372 | } |
7c272194 | 373 | pages[i] = page; |
1d3576fd | 374 | } |
406e2c9f | 375 | osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false); |
7c272194 SW |
376 | req->r_callback = finish_read; |
377 | req->r_inode = inode; | |
378 | ||
79528734 | 379 | ceph_osdc_build_request(req, off, NULL, vino.snap, NULL); |
02ee07d3 | 380 | |
7c272194 SW |
381 | dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len); |
382 | ret = ceph_osdc_start_request(osdc, req, false); | |
383 | if (ret < 0) | |
384 | goto out_pages; | |
385 | ceph_osdc_put_request(req); | |
386 | return nr_pages; | |
387 | ||
388 | out_pages: | |
8884d53d | 389 | ceph_unlock_page_vector(pages, nr_pages); |
7c272194 | 390 | ceph_release_page_vector(pages, nr_pages); |
7c272194 SW |
391 | out: |
392 | ceph_osdc_put_request(req); | |
393 | return ret; | |
394 | } | |
1d3576fd | 395 | |
7c272194 SW |
396 | |
397 | /* | |
398 | * Read multiple pages. Leave pages we don't read + unlock in page_list; | |
399 | * the caller (VM) cleans them up. | |
400 | */ | |
401 | static int ceph_readpages(struct file *file, struct address_space *mapping, | |
402 | struct list_head *page_list, unsigned nr_pages) | |
403 | { | |
496ad9aa | 404 | struct inode *inode = file_inode(file); |
0d66a487 | 405 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
7c272194 | 406 | int rc = 0; |
0d66a487 SW |
407 | int max = 0; |
408 | ||
83701246 YZ |
409 | if (ceph_inode(inode)->i_inline_version != CEPH_INLINE_NONE) |
410 | return -EINVAL; | |
411 | ||
99ccbd22 MT |
412 | rc = ceph_readpages_from_fscache(mapping->host, mapping, page_list, |
413 | &nr_pages); | |
414 | ||
415 | if (rc == 0) | |
416 | goto out; | |
417 | ||
0d66a487 SW |
418 | if (fsc->mount_options->rsize >= PAGE_CACHE_SIZE) |
419 | max = (fsc->mount_options->rsize + PAGE_CACHE_SIZE - 1) | |
420 | >> PAGE_SHIFT; | |
7c272194 | 421 | |
2794a82a AE |
422 | dout("readpages %p file %p nr_pages %d max %d\n", inode, |
423 | file, nr_pages, | |
0d66a487 | 424 | max); |
7c272194 | 425 | while (!list_empty(page_list)) { |
0d66a487 | 426 | rc = start_read(inode, page_list, max); |
7c272194 SW |
427 | if (rc < 0) |
428 | goto out; | |
429 | BUG_ON(rc == 0); | |
430 | } | |
1d3576fd | 431 | out: |
76be778b MT |
432 | ceph_fscache_readpages_cancel(inode, page_list); |
433 | ||
7c272194 | 434 | dout("readpages %p file %p ret %d\n", inode, file, rc); |
1d3576fd SW |
435 | return rc; |
436 | } | |
437 | ||
438 | /* | |
439 | * Get ref for the oldest snapc for an inode with dirty data... that is, the | |
440 | * only snap context we are allowed to write back. | |
1d3576fd | 441 | */ |
6298a337 SW |
442 | static struct ceph_snap_context *get_oldest_context(struct inode *inode, |
443 | u64 *snap_size) | |
1d3576fd SW |
444 | { |
445 | struct ceph_inode_info *ci = ceph_inode(inode); | |
446 | struct ceph_snap_context *snapc = NULL; | |
447 | struct ceph_cap_snap *capsnap = NULL; | |
448 | ||
be655596 | 449 | spin_lock(&ci->i_ceph_lock); |
1d3576fd SW |
450 | list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { |
451 | dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap, | |
452 | capsnap->context, capsnap->dirty_pages); | |
453 | if (capsnap->dirty_pages) { | |
454 | snapc = ceph_get_snap_context(capsnap->context); | |
455 | if (snap_size) | |
456 | *snap_size = capsnap->size; | |
457 | break; | |
458 | } | |
459 | } | |
7d8cb26d | 460 | if (!snapc && ci->i_wrbuffer_ref_head) { |
80e755fe | 461 | snapc = ceph_get_snap_context(ci->i_head_snapc); |
1d3576fd SW |
462 | dout(" head snapc %p has %d dirty pages\n", |
463 | snapc, ci->i_wrbuffer_ref_head); | |
464 | } | |
be655596 | 465 | spin_unlock(&ci->i_ceph_lock); |
1d3576fd SW |
466 | return snapc; |
467 | } | |
468 | ||
469 | /* | |
470 | * Write a single page, but leave the page locked. | |
471 | * | |
472 | * If we get a write error, set the page error bit, but still adjust the | |
473 | * dirty page accounting (i.e., page is no longer dirty). | |
474 | */ | |
475 | static int writepage_nounlock(struct page *page, struct writeback_control *wbc) | |
476 | { | |
477 | struct inode *inode; | |
478 | struct ceph_inode_info *ci; | |
3d14c5d2 | 479 | struct ceph_fs_client *fsc; |
1d3576fd | 480 | struct ceph_osd_client *osdc; |
6298a337 | 481 | struct ceph_snap_context *snapc, *oldest; |
fc2744aa | 482 | loff_t page_off = page_offset(page); |
2baba250 | 483 | long writeback_stat; |
fc2744aa YZ |
484 | u64 truncate_size, snap_size = 0; |
485 | u32 truncate_seq; | |
486 | int err = 0, len = PAGE_CACHE_SIZE; | |
1d3576fd SW |
487 | |
488 | dout("writepage %p idx %lu\n", page, page->index); | |
489 | ||
490 | if (!page->mapping || !page->mapping->host) { | |
491 | dout("writepage %p - no mapping\n", page); | |
492 | return -EFAULT; | |
493 | } | |
494 | inode = page->mapping->host; | |
495 | ci = ceph_inode(inode); | |
3d14c5d2 YS |
496 | fsc = ceph_inode_to_client(inode); |
497 | osdc = &fsc->client->osdc; | |
1d3576fd SW |
498 | |
499 | /* verify this is a writeable snap context */ | |
61600ef8 | 500 | snapc = page_snap_context(page); |
1d3576fd SW |
501 | if (snapc == NULL) { |
502 | dout("writepage %p page %p not dirty?\n", inode, page); | |
503 | goto out; | |
504 | } | |
6298a337 SW |
505 | oldest = get_oldest_context(inode, &snap_size); |
506 | if (snapc->seq > oldest->seq) { | |
1d3576fd | 507 | dout("writepage %p page %p snapc %p not writeable - noop\n", |
61600ef8 | 508 | inode, page, snapc); |
1d3576fd SW |
509 | /* we should only noop if called by kswapd */ |
510 | WARN_ON((current->flags & PF_MEMALLOC) == 0); | |
6298a337 | 511 | ceph_put_snap_context(oldest); |
1d3576fd SW |
512 | goto out; |
513 | } | |
6298a337 | 514 | ceph_put_snap_context(oldest); |
1d3576fd | 515 | |
fc2744aa YZ |
516 | spin_lock(&ci->i_ceph_lock); |
517 | truncate_seq = ci->i_truncate_seq; | |
518 | truncate_size = ci->i_truncate_size; | |
519 | if (!snap_size) | |
520 | snap_size = i_size_read(inode); | |
521 | spin_unlock(&ci->i_ceph_lock); | |
522 | ||
1d3576fd | 523 | /* is this a partial page at end of file? */ |
fc2744aa YZ |
524 | if (page_off >= snap_size) { |
525 | dout("%p page eof %llu\n", page, snap_size); | |
526 | goto out; | |
527 | } | |
528 | if (snap_size < page_off + len) | |
529 | len = snap_size - page_off; | |
1d3576fd | 530 | |
ae00d4f3 SW |
531 | dout("writepage %p page %p index %lu on %llu~%u snapc %p\n", |
532 | inode, page, page->index, page_off, len, snapc); | |
1d3576fd | 533 | |
3d14c5d2 | 534 | writeback_stat = atomic_long_inc_return(&fsc->writeback_count); |
2baba250 | 535 | if (writeback_stat > |
3d14c5d2 YS |
536 | CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb)) |
537 | set_bdi_congested(&fsc->backing_dev_info, BLK_RW_ASYNC); | |
2baba250 | 538 | |
99ccbd22 MT |
539 | ceph_readpage_to_fscache(inode, page); |
540 | ||
1d3576fd SW |
541 | set_page_writeback(page); |
542 | err = ceph_osdc_writepages(osdc, ceph_vino(inode), | |
543 | &ci->i_layout, snapc, | |
544 | page_off, len, | |
fc2744aa | 545 | truncate_seq, truncate_size, |
24808826 | 546 | &inode->i_mtime, &page, 1); |
1d3576fd SW |
547 | if (err < 0) { |
548 | dout("writepage setting page/mapping error %d %p\n", err, page); | |
549 | SetPageError(page); | |
550 | mapping_set_error(&inode->i_data, err); | |
551 | if (wbc) | |
552 | wbc->pages_skipped++; | |
553 | } else { | |
554 | dout("writepage cleaned page %p\n", page); | |
555 | err = 0; /* vfs expects us to return 0 */ | |
556 | } | |
557 | page->private = 0; | |
558 | ClearPagePrivate(page); | |
559 | end_page_writeback(page); | |
560 | ceph_put_wrbuffer_cap_refs(ci, 1, snapc); | |
6298a337 | 561 | ceph_put_snap_context(snapc); /* page's reference */ |
1d3576fd SW |
562 | out: |
563 | return err; | |
564 | } | |
565 | ||
566 | static int ceph_writepage(struct page *page, struct writeback_control *wbc) | |
567 | { | |
dbd646a8 YS |
568 | int err; |
569 | struct inode *inode = page->mapping->host; | |
570 | BUG_ON(!inode); | |
70b666c3 | 571 | ihold(inode); |
dbd646a8 | 572 | err = writepage_nounlock(page, wbc); |
1d3576fd | 573 | unlock_page(page); |
dbd646a8 | 574 | iput(inode); |
1d3576fd SW |
575 | return err; |
576 | } | |
577 | ||
578 | ||
579 | /* | |
580 | * lame release_pages helper. release_pages() isn't exported to | |
581 | * modules. | |
582 | */ | |
583 | static void ceph_release_pages(struct page **pages, int num) | |
584 | { | |
585 | struct pagevec pvec; | |
586 | int i; | |
587 | ||
588 | pagevec_init(&pvec, 0); | |
589 | for (i = 0; i < num; i++) { | |
590 | if (pagevec_add(&pvec, pages[i]) == 0) | |
591 | pagevec_release(&pvec); | |
592 | } | |
593 | pagevec_release(&pvec); | |
594 | } | |
595 | ||
1d3576fd SW |
596 | /* |
597 | * async writeback completion handler. | |
598 | * | |
599 | * If we get an error, set the mapping error bit, but not the individual | |
600 | * page error bits. | |
601 | */ | |
602 | static void writepages_finish(struct ceph_osd_request *req, | |
603 | struct ceph_msg *msg) | |
604 | { | |
605 | struct inode *inode = req->r_inode; | |
1d3576fd | 606 | struct ceph_inode_info *ci = ceph_inode(inode); |
87060c10 | 607 | struct ceph_osd_data *osd_data; |
1d3576fd | 608 | unsigned wrote; |
1d3576fd | 609 | struct page *page; |
e0c59487 | 610 | int num_pages; |
1d3576fd SW |
611 | int i; |
612 | struct ceph_snap_context *snapc = req->r_snapc; | |
613 | struct address_space *mapping = inode->i_mapping; | |
1b83bef2 | 614 | int rc = req->r_result; |
79528734 | 615 | u64 bytes = req->r_ops[0].extent.length; |
3d14c5d2 | 616 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
2baba250 | 617 | long writeback_stat; |
7ff899da | 618 | unsigned issued = ceph_caps_issued(ci); |
1d3576fd | 619 | |
406e2c9f | 620 | osd_data = osd_req_op_extent_osd_data(req, 0); |
87060c10 AE |
621 | BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES); |
622 | num_pages = calc_pages_for((u64)osd_data->alignment, | |
623 | (u64)osd_data->length); | |
1d3576fd | 624 | if (rc >= 0) { |
79788c69 SW |
625 | /* |
626 | * Assume we wrote the pages we originally sent. The | |
627 | * osd might reply with fewer pages if our writeback | |
628 | * raced with a truncation and was adjusted at the osd, | |
629 | * so don't believe the reply. | |
630 | */ | |
e0c59487 | 631 | wrote = num_pages; |
1d3576fd SW |
632 | } else { |
633 | wrote = 0; | |
634 | mapping_set_error(mapping, rc); | |
635 | } | |
636 | dout("writepages_finish %p rc %d bytes %llu wrote %d (pages)\n", | |
637 | inode, rc, bytes, wrote); | |
638 | ||
639 | /* clean all pages */ | |
e0c59487 | 640 | for (i = 0; i < num_pages; i++) { |
87060c10 | 641 | page = osd_data->pages[i]; |
1d3576fd SW |
642 | BUG_ON(!page); |
643 | WARN_ON(!PageUptodate(page)); | |
644 | ||
2baba250 | 645 | writeback_stat = |
3d14c5d2 | 646 | atomic_long_dec_return(&fsc->writeback_count); |
2baba250 | 647 | if (writeback_stat < |
3d14c5d2 YS |
648 | CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb)) |
649 | clear_bdi_congested(&fsc->backing_dev_info, | |
2baba250 YS |
650 | BLK_RW_ASYNC); |
651 | ||
61600ef8 | 652 | ceph_put_snap_context(page_snap_context(page)); |
1d3576fd SW |
653 | page->private = 0; |
654 | ClearPagePrivate(page); | |
1d3576fd SW |
655 | dout("unlocking %d %p\n", i, page); |
656 | end_page_writeback(page); | |
e63dc5c7 YS |
657 | |
658 | /* | |
659 | * We lost the cache cap, need to truncate the page before | |
660 | * it is unlocked, otherwise we'd truncate it later in the | |
661 | * page truncation thread, possibly losing some data that | |
662 | * raced its way in | |
663 | */ | |
2962507c | 664 | if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) |
e63dc5c7 YS |
665 | generic_error_remove_page(inode->i_mapping, page); |
666 | ||
1d3576fd SW |
667 | unlock_page(page); |
668 | } | |
669 | dout("%p wrote+cleaned %d pages\n", inode, wrote); | |
e0c59487 | 670 | ceph_put_wrbuffer_cap_refs(ci, num_pages, snapc); |
1d3576fd | 671 | |
87060c10 AE |
672 | ceph_release_pages(osd_data->pages, num_pages); |
673 | if (osd_data->pages_from_pool) | |
674 | mempool_free(osd_data->pages, | |
640ef79d | 675 | ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool); |
1d3576fd | 676 | else |
87060c10 | 677 | kfree(osd_data->pages); |
1d3576fd SW |
678 | ceph_osdc_put_request(req); |
679 | } | |
680 | ||
1d3576fd SW |
681 | /* |
682 | * initiate async writeback | |
683 | */ | |
684 | static int ceph_writepages_start(struct address_space *mapping, | |
685 | struct writeback_control *wbc) | |
686 | { | |
687 | struct inode *inode = mapping->host; | |
1d3576fd | 688 | struct ceph_inode_info *ci = ceph_inode(inode); |
fc2744aa YZ |
689 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
690 | struct ceph_vino vino = ceph_vino(inode); | |
1d3576fd SW |
691 | pgoff_t index, start, end; |
692 | int range_whole = 0; | |
693 | int should_loop = 1; | |
694 | pgoff_t max_pages = 0, max_pages_ever = 0; | |
80e755fe | 695 | struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc; |
1d3576fd SW |
696 | struct pagevec pvec; |
697 | int done = 0; | |
698 | int rc = 0; | |
699 | unsigned wsize = 1 << inode->i_blkbits; | |
700 | struct ceph_osd_request *req = NULL; | |
021b77be | 701 | int do_sync = 0; |
fc2744aa YZ |
702 | u64 truncate_size, snap_size; |
703 | u32 truncate_seq; | |
1d3576fd SW |
704 | |
705 | /* | |
706 | * Include a 'sync' in the OSD request if this is a data | |
707 | * integrity write (e.g., O_SYNC write or fsync()), or if our | |
708 | * cap is being revoked. | |
709 | */ | |
c62988ec | 710 | if ((wbc->sync_mode == WB_SYNC_ALL) || |
711 | ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER)) | |
1d3576fd SW |
712 | do_sync = 1; |
713 | dout("writepages_start %p dosync=%d (mode=%s)\n", | |
714 | inode, do_sync, | |
715 | wbc->sync_mode == WB_SYNC_NONE ? "NONE" : | |
716 | (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD")); | |
717 | ||
3d14c5d2 | 718 | if (fsc->mount_state == CEPH_MOUNT_SHUTDOWN) { |
f3ae1b97 | 719 | pr_warn("writepage_start %p on forced umount\n", inode); |
1d3576fd SW |
720 | return -EIO; /* we're in a forced umount, don't write! */ |
721 | } | |
3d14c5d2 YS |
722 | if (fsc->mount_options->wsize && fsc->mount_options->wsize < wsize) |
723 | wsize = fsc->mount_options->wsize; | |
1d3576fd SW |
724 | if (wsize < PAGE_CACHE_SIZE) |
725 | wsize = PAGE_CACHE_SIZE; | |
726 | max_pages_ever = wsize >> PAGE_CACHE_SHIFT; | |
727 | ||
728 | pagevec_init(&pvec, 0); | |
729 | ||
1d3576fd SW |
730 | /* where to start/end? */ |
731 | if (wbc->range_cyclic) { | |
732 | start = mapping->writeback_index; /* Start from prev offset */ | |
733 | end = -1; | |
734 | dout(" cyclic, start at %lu\n", start); | |
735 | } else { | |
736 | start = wbc->range_start >> PAGE_CACHE_SHIFT; | |
737 | end = wbc->range_end >> PAGE_CACHE_SHIFT; | |
738 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) | |
739 | range_whole = 1; | |
740 | should_loop = 0; | |
741 | dout(" not cyclic, %lu to %lu\n", start, end); | |
742 | } | |
743 | index = start; | |
744 | ||
745 | retry: | |
746 | /* find oldest snap context with dirty data */ | |
747 | ceph_put_snap_context(snapc); | |
1ac0fc8a | 748 | snap_size = 0; |
1d3576fd SW |
749 | snapc = get_oldest_context(inode, &snap_size); |
750 | if (!snapc) { | |
751 | /* hmm, why does writepages get called when there | |
752 | is no dirty data? */ | |
753 | dout(" no snap context with dirty data?\n"); | |
754 | goto out; | |
755 | } | |
1ac0fc8a YZ |
756 | if (snap_size == 0) |
757 | snap_size = i_size_read(inode); | |
1d3576fd SW |
758 | dout(" oldest snapc is %p seq %lld (%d snaps)\n", |
759 | snapc, snapc->seq, snapc->num_snaps); | |
fc2744aa YZ |
760 | |
761 | spin_lock(&ci->i_ceph_lock); | |
762 | truncate_seq = ci->i_truncate_seq; | |
763 | truncate_size = ci->i_truncate_size; | |
764 | if (!snap_size) | |
765 | snap_size = i_size_read(inode); | |
766 | spin_unlock(&ci->i_ceph_lock); | |
767 | ||
1d3576fd SW |
768 | if (last_snapc && snapc != last_snapc) { |
769 | /* if we switched to a newer snapc, restart our scan at the | |
770 | * start of the original file range. */ | |
771 | dout(" snapc differs from last pass, restarting at %lu\n", | |
772 | index); | |
773 | index = start; | |
774 | } | |
775 | last_snapc = snapc; | |
776 | ||
777 | while (!done && index <= end) { | |
778 | unsigned i; | |
779 | int first; | |
780 | pgoff_t next; | |
781 | int pvec_pages, locked_pages; | |
e5975c7c AE |
782 | struct page **pages = NULL; |
783 | mempool_t *pool = NULL; /* Becomes non-null if mempool used */ | |
1d3576fd SW |
784 | struct page *page; |
785 | int want; | |
786 | u64 offset, len; | |
2baba250 | 787 | long writeback_stat; |
1d3576fd SW |
788 | |
789 | next = 0; | |
790 | locked_pages = 0; | |
791 | max_pages = max_pages_ever; | |
792 | ||
793 | get_more_pages: | |
794 | first = -1; | |
795 | want = min(end - index, | |
796 | min((pgoff_t)PAGEVEC_SIZE, | |
797 | max_pages - (pgoff_t)locked_pages) - 1) | |
798 | + 1; | |
799 | pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index, | |
800 | PAGECACHE_TAG_DIRTY, | |
801 | want); | |
802 | dout("pagevec_lookup_tag got %d\n", pvec_pages); | |
803 | if (!pvec_pages && !locked_pages) | |
804 | break; | |
805 | for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) { | |
806 | page = pvec.pages[i]; | |
807 | dout("? %p idx %lu\n", page, page->index); | |
808 | if (locked_pages == 0) | |
809 | lock_page(page); /* first page */ | |
810 | else if (!trylock_page(page)) | |
811 | break; | |
812 | ||
813 | /* only dirty pages, or our accounting breaks */ | |
814 | if (unlikely(!PageDirty(page)) || | |
815 | unlikely(page->mapping != mapping)) { | |
816 | dout("!dirty or !mapping %p\n", page); | |
817 | unlock_page(page); | |
818 | break; | |
819 | } | |
820 | if (!wbc->range_cyclic && page->index > end) { | |
821 | dout("end of range %p\n", page); | |
822 | done = 1; | |
823 | unlock_page(page); | |
824 | break; | |
825 | } | |
826 | if (next && (page->index != next)) { | |
827 | dout("not consecutive %p\n", page); | |
828 | unlock_page(page); | |
829 | break; | |
830 | } | |
831 | if (wbc->sync_mode != WB_SYNC_NONE) { | |
832 | dout("waiting on writeback %p\n", page); | |
833 | wait_on_page_writeback(page); | |
834 | } | |
1ac0fc8a YZ |
835 | if (page_offset(page) >= snap_size) { |
836 | dout("%p page eof %llu\n", page, snap_size); | |
1d3576fd SW |
837 | done = 1; |
838 | unlock_page(page); | |
839 | break; | |
840 | } | |
841 | if (PageWriteback(page)) { | |
842 | dout("%p under writeback\n", page); | |
843 | unlock_page(page); | |
844 | break; | |
845 | } | |
846 | ||
847 | /* only if matching snap context */ | |
61600ef8 | 848 | pgsnapc = page_snap_context(page); |
80e755fe SW |
849 | if (pgsnapc->seq > snapc->seq) { |
850 | dout("page snapc %p %lld > oldest %p %lld\n", | |
851 | pgsnapc, pgsnapc->seq, snapc, snapc->seq); | |
1d3576fd SW |
852 | unlock_page(page); |
853 | if (!locked_pages) | |
854 | continue; /* keep looking for snap */ | |
855 | break; | |
856 | } | |
857 | ||
858 | if (!clear_page_dirty_for_io(page)) { | |
859 | dout("%p !clear_page_dirty_for_io\n", page); | |
860 | unlock_page(page); | |
861 | break; | |
862 | } | |
863 | ||
e5975c7c AE |
864 | /* |
865 | * We have something to write. If this is | |
866 | * the first locked page this time through, | |
867 | * allocate an osd request and a page array | |
868 | * that it will use. | |
869 | */ | |
1d3576fd | 870 | if (locked_pages == 0) { |
e5975c7c | 871 | BUG_ON(pages); |
1d3576fd | 872 | /* prepare async write request */ |
e5975c7c | 873 | offset = (u64)page_offset(page); |
1d3576fd | 874 | len = wsize; |
fc2744aa YZ |
875 | req = ceph_osdc_new_request(&fsc->client->osdc, |
876 | &ci->i_layout, vino, | |
715e4cd4 YZ |
877 | offset, &len, 0, |
878 | do_sync ? 2 : 1, | |
fc2744aa YZ |
879 | CEPH_OSD_OP_WRITE, |
880 | CEPH_OSD_FLAG_WRITE | | |
881 | CEPH_OSD_FLAG_ONDISK, | |
882 | snapc, truncate_seq, | |
883 | truncate_size, true); | |
6816282d SW |
884 | if (IS_ERR(req)) { |
885 | rc = PTR_ERR(req); | |
8c71897b HC |
886 | unlock_page(page); |
887 | break; | |
888 | } | |
889 | ||
715e4cd4 | 890 | if (do_sync) |
144cba14 YZ |
891 | osd_req_op_init(req, 1, |
892 | CEPH_OSD_OP_STARTSYNC, 0); | |
715e4cd4 | 893 | |
88486957 AE |
894 | req->r_callback = writepages_finish; |
895 | req->r_inode = inode; | |
896 | ||
897 | max_pages = calc_pages_for(0, (u64)len); | |
fc2744aa YZ |
898 | pages = kmalloc(max_pages * sizeof (*pages), |
899 | GFP_NOFS); | |
88486957 AE |
900 | if (!pages) { |
901 | pool = fsc->wb_pagevec_pool; | |
88486957 | 902 | pages = mempool_alloc(pool, GFP_NOFS); |
e5975c7c | 903 | BUG_ON(!pages); |
88486957 | 904 | } |
1d3576fd SW |
905 | } |
906 | ||
907 | /* note position of first page in pvec */ | |
908 | if (first < 0) | |
909 | first = i; | |
910 | dout("%p will write page %p idx %lu\n", | |
911 | inode, page, page->index); | |
2baba250 | 912 | |
213c99ee | 913 | writeback_stat = |
3d14c5d2 | 914 | atomic_long_inc_return(&fsc->writeback_count); |
213c99ee | 915 | if (writeback_stat > CONGESTION_ON_THRESH( |
3d14c5d2 YS |
916 | fsc->mount_options->congestion_kb)) { |
917 | set_bdi_congested(&fsc->backing_dev_info, | |
213c99ee | 918 | BLK_RW_ASYNC); |
2baba250 YS |
919 | } |
920 | ||
1d3576fd | 921 | set_page_writeback(page); |
e5975c7c | 922 | pages[locked_pages] = page; |
1d3576fd SW |
923 | locked_pages++; |
924 | next = page->index + 1; | |
925 | } | |
926 | ||
927 | /* did we get anything? */ | |
928 | if (!locked_pages) | |
929 | goto release_pvec_pages; | |
930 | if (i) { | |
931 | int j; | |
932 | BUG_ON(!locked_pages || first < 0); | |
933 | ||
934 | if (pvec_pages && i == pvec_pages && | |
935 | locked_pages < max_pages) { | |
936 | dout("reached end pvec, trying for more\n"); | |
937 | pagevec_reinit(&pvec); | |
938 | goto get_more_pages; | |
939 | } | |
940 | ||
941 | /* shift unused pages over in the pvec... we | |
942 | * will need to release them below. */ | |
943 | for (j = i; j < pvec_pages; j++) { | |
944 | dout(" pvec leftover page %p\n", | |
945 | pvec.pages[j]); | |
946 | pvec.pages[j-i+first] = pvec.pages[j]; | |
947 | } | |
948 | pvec.nr -= i-first; | |
949 | } | |
950 | ||
e5975c7c AE |
951 | /* Format the osd request message and submit the write */ |
952 | ||
953 | offset = page_offset(pages[0]); | |
1ac0fc8a | 954 | len = min(snap_size - offset, |
1d3576fd SW |
955 | (u64)locked_pages << PAGE_CACHE_SHIFT); |
956 | dout("writepages got %d pages at %llu~%llu\n", | |
957 | locked_pages, offset, len); | |
958 | ||
406e2c9f | 959 | osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, |
a4ce40a9 | 960 | !!pool, false); |
e5975c7c AE |
961 | |
962 | pages = NULL; /* request message now owns the pages array */ | |
963 | pool = NULL; | |
964 | ||
965 | /* Update the write op length in case we changed it */ | |
966 | ||
c99d2d4a | 967 | osd_req_op_extent_update(req, 0, len); |
e5975c7c AE |
968 | |
969 | vino = ceph_vino(inode); | |
79528734 AE |
970 | ceph_osdc_build_request(req, offset, snapc, vino.snap, |
971 | &inode->i_mtime); | |
1d3576fd | 972 | |
9d6fcb08 SW |
973 | rc = ceph_osdc_start_request(&fsc->client->osdc, req, true); |
974 | BUG_ON(rc); | |
1d3576fd SW |
975 | req = NULL; |
976 | ||
977 | /* continue? */ | |
978 | index = next; | |
979 | wbc->nr_to_write -= locked_pages; | |
980 | if (wbc->nr_to_write <= 0) | |
981 | done = 1; | |
982 | ||
983 | release_pvec_pages: | |
984 | dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr, | |
985 | pvec.nr ? pvec.pages[0] : NULL); | |
986 | pagevec_release(&pvec); | |
987 | ||
988 | if (locked_pages && !done) | |
989 | goto retry; | |
990 | } | |
991 | ||
992 | if (should_loop && !done) { | |
993 | /* more to do; loop back to beginning of file */ | |
994 | dout("writepages looping back to beginning of file\n"); | |
995 | should_loop = 0; | |
996 | index = 0; | |
997 | goto retry; | |
998 | } | |
999 | ||
1000 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) | |
1001 | mapping->writeback_index = index; | |
1002 | ||
1003 | out: | |
1004 | if (req) | |
1005 | ceph_osdc_put_request(req); | |
1d3576fd SW |
1006 | ceph_put_snap_context(snapc); |
1007 | dout("writepages done, rc = %d\n", rc); | |
1d3576fd SW |
1008 | return rc; |
1009 | } | |
1010 | ||
1011 | ||
1012 | ||
1013 | /* | |
1014 | * See if a given @snapc is either writeable, or already written. | |
1015 | */ | |
1016 | static int context_is_writeable_or_written(struct inode *inode, | |
1017 | struct ceph_snap_context *snapc) | |
1018 | { | |
1019 | struct ceph_snap_context *oldest = get_oldest_context(inode, NULL); | |
6298a337 SW |
1020 | int ret = !oldest || snapc->seq <= oldest->seq; |
1021 | ||
1022 | ceph_put_snap_context(oldest); | |
1023 | return ret; | |
1d3576fd SW |
1024 | } |
1025 | ||
1026 | /* | |
1027 | * We are only allowed to write into/dirty the page if the page is | |
1028 | * clean, or already dirty within the same snap context. | |
8f883c24 SW |
1029 | * |
1030 | * called with page locked. | |
1031 | * return success with page locked, | |
1032 | * or any failure (incl -EAGAIN) with page unlocked. | |
1d3576fd | 1033 | */ |
4af6b225 YS |
1034 | static int ceph_update_writeable_page(struct file *file, |
1035 | loff_t pos, unsigned len, | |
1036 | struct page *page) | |
1d3576fd | 1037 | { |
496ad9aa | 1038 | struct inode *inode = file_inode(file); |
1d3576fd | 1039 | struct ceph_inode_info *ci = ceph_inode(inode); |
1d3576fd SW |
1040 | loff_t page_off = pos & PAGE_CACHE_MASK; |
1041 | int pos_in_page = pos & ~PAGE_CACHE_MASK; | |
1042 | int end_in_page = pos_in_page + len; | |
1043 | loff_t i_size; | |
1d3576fd | 1044 | int r; |
80e755fe | 1045 | struct ceph_snap_context *snapc, *oldest; |
1d3576fd | 1046 | |
1d3576fd SW |
1047 | retry_locked: |
1048 | /* writepages currently holds page lock, but if we change that later, */ | |
1049 | wait_on_page_writeback(page); | |
1050 | ||
61600ef8 | 1051 | snapc = page_snap_context(page); |
80e755fe | 1052 | if (snapc && snapc != ci->i_head_snapc) { |
1d3576fd SW |
1053 | /* |
1054 | * this page is already dirty in another (older) snap | |
1055 | * context! is it writeable now? | |
1056 | */ | |
80e755fe | 1057 | oldest = get_oldest_context(inode, NULL); |
1d3576fd | 1058 | |
80e755fe | 1059 | if (snapc->seq > oldest->seq) { |
6298a337 | 1060 | ceph_put_snap_context(oldest); |
1d3576fd | 1061 | dout(" page %p snapc %p not current or oldest\n", |
6298a337 | 1062 | page, snapc); |
1d3576fd SW |
1063 | /* |
1064 | * queue for writeback, and wait for snapc to | |
1065 | * be writeable or written | |
1066 | */ | |
6298a337 | 1067 | snapc = ceph_get_snap_context(snapc); |
1d3576fd | 1068 | unlock_page(page); |
3c6f6b79 | 1069 | ceph_queue_writeback(inode); |
8f883c24 | 1070 | r = wait_event_interruptible(ci->i_cap_wq, |
1d3576fd SW |
1071 | context_is_writeable_or_written(inode, snapc)); |
1072 | ceph_put_snap_context(snapc); | |
8f883c24 SW |
1073 | if (r == -ERESTARTSYS) |
1074 | return r; | |
4af6b225 | 1075 | return -EAGAIN; |
1d3576fd | 1076 | } |
6298a337 | 1077 | ceph_put_snap_context(oldest); |
1d3576fd SW |
1078 | |
1079 | /* yay, writeable, do it now (without dropping page lock) */ | |
1080 | dout(" page %p snapc %p not current, but oldest\n", | |
1081 | page, snapc); | |
1082 | if (!clear_page_dirty_for_io(page)) | |
1083 | goto retry_locked; | |
1084 | r = writepage_nounlock(page, NULL); | |
1085 | if (r < 0) | |
1086 | goto fail_nosnap; | |
1087 | goto retry_locked; | |
1088 | } | |
1089 | ||
1090 | if (PageUptodate(page)) { | |
1091 | dout(" page %p already uptodate\n", page); | |
1092 | return 0; | |
1093 | } | |
1094 | ||
1095 | /* full page? */ | |
1096 | if (pos_in_page == 0 && len == PAGE_CACHE_SIZE) | |
1097 | return 0; | |
1098 | ||
1099 | /* past end of file? */ | |
1100 | i_size = inode->i_size; /* caller holds i_mutex */ | |
1101 | ||
1d3576fd SW |
1102 | if (page_off >= i_size || |
1103 | (pos_in_page == 0 && (pos+len) >= i_size && | |
1104 | end_in_page - pos_in_page != PAGE_CACHE_SIZE)) { | |
1105 | dout(" zeroing %p 0 - %d and %d - %d\n", | |
1106 | page, pos_in_page, end_in_page, (int)PAGE_CACHE_SIZE); | |
1107 | zero_user_segments(page, | |
1108 | 0, pos_in_page, | |
1109 | end_in_page, PAGE_CACHE_SIZE); | |
1110 | return 0; | |
1111 | } | |
1112 | ||
1113 | /* we need to read it. */ | |
1d3576fd SW |
1114 | r = readpage_nounlock(file, page); |
1115 | if (r < 0) | |
1116 | goto fail_nosnap; | |
1117 | goto retry_locked; | |
1d3576fd SW |
1118 | fail_nosnap: |
1119 | unlock_page(page); | |
1120 | return r; | |
1121 | } | |
1122 | ||
4af6b225 YS |
1123 | /* |
1124 | * We are only allowed to write into/dirty the page if the page is | |
1125 | * clean, or already dirty within the same snap context. | |
1126 | */ | |
1127 | static int ceph_write_begin(struct file *file, struct address_space *mapping, | |
1128 | loff_t pos, unsigned len, unsigned flags, | |
1129 | struct page **pagep, void **fsdata) | |
1130 | { | |
496ad9aa | 1131 | struct inode *inode = file_inode(file); |
4af6b225 YS |
1132 | struct page *page; |
1133 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; | |
7971bd92 | 1134 | int r; |
4af6b225 YS |
1135 | |
1136 | do { | |
8f883c24 | 1137 | /* get a page */ |
4af6b225 | 1138 | page = grab_cache_page_write_begin(mapping, index, 0); |
7971bd92 SW |
1139 | if (!page) |
1140 | return -ENOMEM; | |
1141 | *pagep = page; | |
4af6b225 YS |
1142 | |
1143 | dout("write_begin file %p inode %p page %p %d~%d\n", file, | |
213c99ee | 1144 | inode, page, (int)pos, (int)len); |
4af6b225 YS |
1145 | |
1146 | r = ceph_update_writeable_page(file, pos, len, page); | |
c1d00b2d TK |
1147 | if (r < 0) |
1148 | page_cache_release(page); | |
1149 | else | |
1150 | *pagep = page; | |
4af6b225 YS |
1151 | } while (r == -EAGAIN); |
1152 | ||
1153 | return r; | |
1154 | } | |
1155 | ||
1d3576fd SW |
1156 | /* |
1157 | * we don't do anything in here that simple_write_end doesn't do | |
5dda377c | 1158 | * except adjust dirty page accounting |
1d3576fd SW |
1159 | */ |
1160 | static int ceph_write_end(struct file *file, struct address_space *mapping, | |
1161 | loff_t pos, unsigned len, unsigned copied, | |
1162 | struct page *page, void *fsdata) | |
1163 | { | |
496ad9aa | 1164 | struct inode *inode = file_inode(file); |
1d3576fd SW |
1165 | unsigned from = pos & (PAGE_CACHE_SIZE - 1); |
1166 | int check_cap = 0; | |
1167 | ||
1168 | dout("write_end file %p inode %p page %p %d~%d (%d)\n", file, | |
1169 | inode, page, (int)pos, (int)copied, (int)len); | |
1170 | ||
1171 | /* zero the stale part of the page if we did a short copy */ | |
1172 | if (copied < len) | |
1173 | zero_user_segment(page, from+copied, len); | |
1174 | ||
1175 | /* did file size increase? */ | |
1176 | /* (no need for i_size_read(); we caller holds i_mutex */ | |
1177 | if (pos+copied > inode->i_size) | |
1178 | check_cap = ceph_inode_set_size(inode, pos+copied); | |
1179 | ||
1180 | if (!PageUptodate(page)) | |
1181 | SetPageUptodate(page); | |
1182 | ||
1183 | set_page_dirty(page); | |
1184 | ||
1185 | unlock_page(page); | |
1d3576fd SW |
1186 | page_cache_release(page); |
1187 | ||
1188 | if (check_cap) | |
1189 | ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL); | |
1190 | ||
1191 | return copied; | |
1192 | } | |
1193 | ||
1194 | /* | |
1195 | * we set .direct_IO to indicate direct io is supported, but since we | |
1196 | * intercept O_DIRECT reads and writes early, this function should | |
1197 | * never get called. | |
1198 | */ | |
22c6186e | 1199 | static ssize_t ceph_direct_io(struct kiocb *iocb, struct iov_iter *iter, |
d8d3d94b | 1200 | loff_t pos) |
1d3576fd SW |
1201 | { |
1202 | WARN_ON(1); | |
1203 | return -EINVAL; | |
1204 | } | |
1205 | ||
1206 | const struct address_space_operations ceph_aops = { | |
1207 | .readpage = ceph_readpage, | |
1208 | .readpages = ceph_readpages, | |
1209 | .writepage = ceph_writepage, | |
1210 | .writepages = ceph_writepages_start, | |
1211 | .write_begin = ceph_write_begin, | |
1212 | .write_end = ceph_write_end, | |
1213 | .set_page_dirty = ceph_set_page_dirty, | |
1214 | .invalidatepage = ceph_invalidatepage, | |
1215 | .releasepage = ceph_releasepage, | |
1216 | .direct_IO = ceph_direct_io, | |
1217 | }; | |
1218 | ||
1219 | ||
1220 | /* | |
1221 | * vm ops | |
1222 | */ | |
61f68816 YZ |
1223 | static int ceph_filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
1224 | { | |
1225 | struct inode *inode = file_inode(vma->vm_file); | |
1226 | struct ceph_inode_info *ci = ceph_inode(inode); | |
1227 | struct ceph_file_info *fi = vma->vm_file->private_data; | |
3738daa6 | 1228 | struct page *pinned_page = NULL; |
61f68816 YZ |
1229 | loff_t off = vmf->pgoff << PAGE_CACHE_SHIFT; |
1230 | int want, got, ret; | |
1231 | ||
1232 | dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n", | |
37b52fe6 | 1233 | inode, ceph_vinop(inode), off, (size_t)PAGE_CACHE_SIZE); |
61f68816 YZ |
1234 | if (fi->fmode & CEPH_FILE_MODE_LAZY) |
1235 | want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO; | |
1236 | else | |
1237 | want = CEPH_CAP_FILE_CACHE; | |
1238 | while (1) { | |
1239 | got = 0; | |
83701246 YZ |
1240 | ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, |
1241 | -1, &got, &pinned_page); | |
61f68816 YZ |
1242 | if (ret == 0) |
1243 | break; | |
1244 | if (ret != -ERESTARTSYS) { | |
1245 | WARN_ON(1); | |
1246 | return VM_FAULT_SIGBUS; | |
1247 | } | |
1248 | } | |
1249 | dout("filemap_fault %p %llu~%zd got cap refs on %s\n", | |
37b52fe6 | 1250 | inode, off, (size_t)PAGE_CACHE_SIZE, ceph_cap_string(got)); |
61f68816 | 1251 | |
83701246 YZ |
1252 | if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) || |
1253 | ci->i_inline_version == CEPH_INLINE_NONE) | |
1254 | ret = filemap_fault(vma, vmf); | |
1255 | else | |
1256 | ret = -EAGAIN; | |
61f68816 YZ |
1257 | |
1258 | dout("filemap_fault %p %llu~%zd dropping cap refs on %s ret %d\n", | |
37b52fe6 | 1259 | inode, off, (size_t)PAGE_CACHE_SIZE, ceph_cap_string(got), ret); |
3738daa6 YZ |
1260 | if (pinned_page) |
1261 | page_cache_release(pinned_page); | |
61f68816 YZ |
1262 | ceph_put_cap_refs(ci, got); |
1263 | ||
83701246 YZ |
1264 | if (ret != -EAGAIN) |
1265 | return ret; | |
1266 | ||
1267 | /* read inline data */ | |
1268 | if (off >= PAGE_CACHE_SIZE) { | |
1269 | /* does not support inline data > PAGE_SIZE */ | |
1270 | ret = VM_FAULT_SIGBUS; | |
1271 | } else { | |
1272 | int ret1; | |
1273 | struct address_space *mapping = inode->i_mapping; | |
1274 | struct page *page = find_or_create_page(mapping, 0, | |
1275 | mapping_gfp_mask(mapping) & | |
1276 | ~__GFP_FS); | |
1277 | if (!page) { | |
1278 | ret = VM_FAULT_OOM; | |
1279 | goto out; | |
1280 | } | |
1281 | ret1 = __ceph_do_getattr(inode, page, | |
1282 | CEPH_STAT_CAP_INLINE_DATA, true); | |
1283 | if (ret1 < 0 || off >= i_size_read(inode)) { | |
1284 | unlock_page(page); | |
1285 | page_cache_release(page); | |
1286 | ret = VM_FAULT_SIGBUS; | |
1287 | goto out; | |
1288 | } | |
1289 | if (ret1 < PAGE_CACHE_SIZE) | |
1290 | zero_user_segment(page, ret1, PAGE_CACHE_SIZE); | |
1291 | else | |
1292 | flush_dcache_page(page); | |
1293 | SetPageUptodate(page); | |
1294 | vmf->page = page; | |
1295 | ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED; | |
1296 | } | |
1297 | out: | |
1298 | dout("filemap_fault %p %llu~%zd read inline data ret %d\n", | |
1299 | inode, off, (size_t)PAGE_CACHE_SIZE, ret); | |
61f68816 YZ |
1300 | return ret; |
1301 | } | |
1d3576fd SW |
1302 | |
1303 | /* | |
1304 | * Reuse write_begin here for simplicity. | |
1305 | */ | |
1306 | static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) | |
1307 | { | |
496ad9aa | 1308 | struct inode *inode = file_inode(vma->vm_file); |
61f68816 YZ |
1309 | struct ceph_inode_info *ci = ceph_inode(inode); |
1310 | struct ceph_file_info *fi = vma->vm_file->private_data; | |
f66fd9f0 | 1311 | struct ceph_cap_flush *prealloc_cf; |
61f68816 | 1312 | struct page *page = vmf->page; |
6285bc23 | 1313 | loff_t off = page_offset(page); |
61f68816 YZ |
1314 | loff_t size = i_size_read(inode); |
1315 | size_t len; | |
1316 | int want, got, ret; | |
3ca9c3bd | 1317 | |
f66fd9f0 YZ |
1318 | prealloc_cf = ceph_alloc_cap_flush(); |
1319 | if (!prealloc_cf) | |
1320 | return VM_FAULT_SIGBUS; | |
1321 | ||
28127bdd YZ |
1322 | if (ci->i_inline_version != CEPH_INLINE_NONE) { |
1323 | struct page *locked_page = NULL; | |
1324 | if (off == 0) { | |
1325 | lock_page(page); | |
1326 | locked_page = page; | |
1327 | } | |
1328 | ret = ceph_uninline_data(vma->vm_file, locked_page); | |
1329 | if (locked_page) | |
1330 | unlock_page(locked_page); | |
f66fd9f0 YZ |
1331 | if (ret < 0) { |
1332 | ret = VM_FAULT_SIGBUS; | |
1333 | goto out_free; | |
1334 | } | |
28127bdd YZ |
1335 | } |
1336 | ||
1d3576fd SW |
1337 | if (off + PAGE_CACHE_SIZE <= size) |
1338 | len = PAGE_CACHE_SIZE; | |
1339 | else | |
1340 | len = size & ~PAGE_CACHE_MASK; | |
1341 | ||
61f68816 YZ |
1342 | dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n", |
1343 | inode, ceph_vinop(inode), off, len, size); | |
1344 | if (fi->fmode & CEPH_FILE_MODE_LAZY) | |
1345 | want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO; | |
1346 | else | |
1347 | want = CEPH_CAP_FILE_BUFFER; | |
1348 | while (1) { | |
1349 | got = 0; | |
3738daa6 YZ |
1350 | ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, off + len, |
1351 | &got, NULL); | |
61f68816 YZ |
1352 | if (ret == 0) |
1353 | break; | |
1354 | if (ret != -ERESTARTSYS) { | |
1355 | WARN_ON(1); | |
f66fd9f0 YZ |
1356 | ret = VM_FAULT_SIGBUS; |
1357 | goto out_free; | |
61f68816 YZ |
1358 | } |
1359 | } | |
1360 | dout("page_mkwrite %p %llu~%zd got cap refs on %s\n", | |
1361 | inode, off, len, ceph_cap_string(got)); | |
1362 | ||
1363 | /* Update time before taking page lock */ | |
1364 | file_update_time(vma->vm_file); | |
4af6b225 YS |
1365 | |
1366 | lock_page(page); | |
1367 | ||
1368 | ret = VM_FAULT_NOPAGE; | |
1369 | if ((off > size) || | |
1370 | (page->mapping != inode->i_mapping)) | |
1371 | goto out; | |
1372 | ||
1373 | ret = ceph_update_writeable_page(vma->vm_file, off, len, page); | |
1374 | if (ret == 0) { | |
1375 | /* success. we'll keep the page locked. */ | |
1d3576fd | 1376 | set_page_dirty(page); |
1d3576fd SW |
1377 | ret = VM_FAULT_LOCKED; |
1378 | } else { | |
4af6b225 YS |
1379 | if (ret == -ENOMEM) |
1380 | ret = VM_FAULT_OOM; | |
1381 | else | |
1382 | ret = VM_FAULT_SIGBUS; | |
1d3576fd | 1383 | } |
4af6b225 | 1384 | out: |
28127bdd | 1385 | if (ret != VM_FAULT_LOCKED) |
4af6b225 | 1386 | unlock_page(page); |
28127bdd YZ |
1387 | if (ret == VM_FAULT_LOCKED || |
1388 | ci->i_inline_version != CEPH_INLINE_NONE) { | |
61f68816 YZ |
1389 | int dirty; |
1390 | spin_lock(&ci->i_ceph_lock); | |
28127bdd | 1391 | ci->i_inline_version = CEPH_INLINE_NONE; |
f66fd9f0 YZ |
1392 | dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR, |
1393 | &prealloc_cf); | |
61f68816 YZ |
1394 | spin_unlock(&ci->i_ceph_lock); |
1395 | if (dirty) | |
1396 | __mark_inode_dirty(inode, dirty); | |
1397 | } | |
1398 | ||
1399 | dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %d\n", | |
1400 | inode, off, len, ceph_cap_string(got), ret); | |
1401 | ceph_put_cap_refs(ci, got); | |
f66fd9f0 YZ |
1402 | out_free: |
1403 | ceph_free_cap_flush(prealloc_cf); | |
61f68816 | 1404 | |
1d3576fd SW |
1405 | return ret; |
1406 | } | |
1407 | ||
31c542a1 YZ |
1408 | void ceph_fill_inline_data(struct inode *inode, struct page *locked_page, |
1409 | char *data, size_t len) | |
1410 | { | |
1411 | struct address_space *mapping = inode->i_mapping; | |
1412 | struct page *page; | |
1413 | ||
1414 | if (locked_page) { | |
1415 | page = locked_page; | |
1416 | } else { | |
1417 | if (i_size_read(inode) == 0) | |
1418 | return; | |
1419 | page = find_or_create_page(mapping, 0, | |
1420 | mapping_gfp_mask(mapping) & ~__GFP_FS); | |
1421 | if (!page) | |
1422 | return; | |
1423 | if (PageUptodate(page)) { | |
1424 | unlock_page(page); | |
1425 | page_cache_release(page); | |
1426 | return; | |
1427 | } | |
1428 | } | |
1429 | ||
0668ff52 | 1430 | dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n", |
31c542a1 YZ |
1431 | inode, ceph_vinop(inode), len, locked_page); |
1432 | ||
1433 | if (len > 0) { | |
1434 | void *kaddr = kmap_atomic(page); | |
1435 | memcpy(kaddr, data, len); | |
1436 | kunmap_atomic(kaddr); | |
1437 | } | |
1438 | ||
1439 | if (page != locked_page) { | |
1440 | if (len < PAGE_CACHE_SIZE) | |
1441 | zero_user_segment(page, len, PAGE_CACHE_SIZE); | |
1442 | else | |
1443 | flush_dcache_page(page); | |
1444 | ||
1445 | SetPageUptodate(page); | |
1446 | unlock_page(page); | |
1447 | page_cache_release(page); | |
1448 | } | |
1449 | } | |
1450 | ||
28127bdd YZ |
1451 | int ceph_uninline_data(struct file *filp, struct page *locked_page) |
1452 | { | |
1453 | struct inode *inode = file_inode(filp); | |
1454 | struct ceph_inode_info *ci = ceph_inode(inode); | |
1455 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); | |
1456 | struct ceph_osd_request *req; | |
1457 | struct page *page = NULL; | |
1458 | u64 len, inline_version; | |
1459 | int err = 0; | |
1460 | bool from_pagecache = false; | |
1461 | ||
1462 | spin_lock(&ci->i_ceph_lock); | |
1463 | inline_version = ci->i_inline_version; | |
1464 | spin_unlock(&ci->i_ceph_lock); | |
1465 | ||
1466 | dout("uninline_data %p %llx.%llx inline_version %llu\n", | |
1467 | inode, ceph_vinop(inode), inline_version); | |
1468 | ||
1469 | if (inline_version == 1 || /* initial version, no data */ | |
1470 | inline_version == CEPH_INLINE_NONE) | |
1471 | goto out; | |
1472 | ||
1473 | if (locked_page) { | |
1474 | page = locked_page; | |
1475 | WARN_ON(!PageUptodate(page)); | |
1476 | } else if (ceph_caps_issued(ci) & | |
1477 | (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) { | |
1478 | page = find_get_page(inode->i_mapping, 0); | |
1479 | if (page) { | |
1480 | if (PageUptodate(page)) { | |
1481 | from_pagecache = true; | |
1482 | lock_page(page); | |
1483 | } else { | |
1484 | page_cache_release(page); | |
1485 | page = NULL; | |
1486 | } | |
1487 | } | |
1488 | } | |
1489 | ||
1490 | if (page) { | |
1491 | len = i_size_read(inode); | |
1492 | if (len > PAGE_CACHE_SIZE) | |
1493 | len = PAGE_CACHE_SIZE; | |
1494 | } else { | |
1495 | page = __page_cache_alloc(GFP_NOFS); | |
1496 | if (!page) { | |
1497 | err = -ENOMEM; | |
1498 | goto out; | |
1499 | } | |
1500 | err = __ceph_do_getattr(inode, page, | |
1501 | CEPH_STAT_CAP_INLINE_DATA, true); | |
1502 | if (err < 0) { | |
1503 | /* no inline data */ | |
1504 | if (err == -ENODATA) | |
1505 | err = 0; | |
1506 | goto out; | |
1507 | } | |
1508 | len = err; | |
1509 | } | |
1510 | ||
1511 | req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, | |
1512 | ceph_vino(inode), 0, &len, 0, 1, | |
1513 | CEPH_OSD_OP_CREATE, | |
1514 | CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE, | |
7b06a826 | 1515 | ceph_empty_snapc, 0, 0, false); |
28127bdd YZ |
1516 | if (IS_ERR(req)) { |
1517 | err = PTR_ERR(req); | |
1518 | goto out; | |
1519 | } | |
1520 | ||
1521 | ceph_osdc_build_request(req, 0, NULL, CEPH_NOSNAP, &inode->i_mtime); | |
1522 | err = ceph_osdc_start_request(&fsc->client->osdc, req, false); | |
1523 | if (!err) | |
1524 | err = ceph_osdc_wait_request(&fsc->client->osdc, req); | |
1525 | ceph_osdc_put_request(req); | |
1526 | if (err < 0) | |
1527 | goto out; | |
1528 | ||
1529 | req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, | |
1530 | ceph_vino(inode), 0, &len, 1, 3, | |
1531 | CEPH_OSD_OP_WRITE, | |
1532 | CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE, | |
7b06a826 | 1533 | ceph_empty_snapc, |
28127bdd YZ |
1534 | ci->i_truncate_seq, ci->i_truncate_size, |
1535 | false); | |
1536 | if (IS_ERR(req)) { | |
1537 | err = PTR_ERR(req); | |
1538 | goto out; | |
1539 | } | |
1540 | ||
1541 | osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false); | |
1542 | ||
ec137c10 YZ |
1543 | { |
1544 | __le64 xattr_buf = cpu_to_le64(inline_version); | |
1545 | err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR, | |
1546 | "inline_version", &xattr_buf, | |
1547 | sizeof(xattr_buf), | |
1548 | CEPH_OSD_CMPXATTR_OP_GT, | |
1549 | CEPH_OSD_CMPXATTR_MODE_U64); | |
1550 | if (err) | |
1551 | goto out_put; | |
1552 | } | |
1553 | ||
1554 | { | |
1555 | char xattr_buf[32]; | |
1556 | int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf), | |
1557 | "%llu", inline_version); | |
1558 | err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR, | |
1559 | "inline_version", | |
1560 | xattr_buf, xattr_len, 0, 0); | |
1561 | if (err) | |
1562 | goto out_put; | |
1563 | } | |
28127bdd YZ |
1564 | |
1565 | ceph_osdc_build_request(req, 0, NULL, CEPH_NOSNAP, &inode->i_mtime); | |
1566 | err = ceph_osdc_start_request(&fsc->client->osdc, req, false); | |
1567 | if (!err) | |
1568 | err = ceph_osdc_wait_request(&fsc->client->osdc, req); | |
1569 | out_put: | |
1570 | ceph_osdc_put_request(req); | |
1571 | if (err == -ECANCELED) | |
1572 | err = 0; | |
1573 | out: | |
1574 | if (page && page != locked_page) { | |
1575 | if (from_pagecache) { | |
1576 | unlock_page(page); | |
1577 | page_cache_release(page); | |
1578 | } else | |
1579 | __free_pages(page, 0); | |
1580 | } | |
1581 | ||
1582 | dout("uninline_data %p %llx.%llx inline_version %llu = %d\n", | |
1583 | inode, ceph_vinop(inode), inline_version, err); | |
1584 | return err; | |
1585 | } | |
1586 | ||
1d3576fd | 1587 | static struct vm_operations_struct ceph_vmops = { |
61f68816 | 1588 | .fault = ceph_filemap_fault, |
1d3576fd SW |
1589 | .page_mkwrite = ceph_page_mkwrite, |
1590 | }; | |
1591 | ||
1592 | int ceph_mmap(struct file *file, struct vm_area_struct *vma) | |
1593 | { | |
1594 | struct address_space *mapping = file->f_mapping; | |
1595 | ||
1596 | if (!mapping->a_ops->readpage) | |
1597 | return -ENOEXEC; | |
1598 | file_accessed(file); | |
1599 | vma->vm_ops = &ceph_vmops; | |
1d3576fd SW |
1600 | return 0; |
1601 | } | |
10183a69 YZ |
1602 | |
1603 | enum { | |
1604 | POOL_READ = 1, | |
1605 | POOL_WRITE = 2, | |
1606 | }; | |
1607 | ||
1608 | static int __ceph_pool_perm_get(struct ceph_inode_info *ci, u32 pool) | |
1609 | { | |
1610 | struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode); | |
1611 | struct ceph_mds_client *mdsc = fsc->mdsc; | |
1612 | struct ceph_osd_request *rd_req = NULL, *wr_req = NULL; | |
1613 | struct rb_node **p, *parent; | |
1614 | struct ceph_pool_perm *perm; | |
1615 | struct page **pages; | |
1616 | int err = 0, err2 = 0, have = 0; | |
1617 | ||
1618 | down_read(&mdsc->pool_perm_rwsem); | |
1619 | p = &mdsc->pool_perm_tree.rb_node; | |
1620 | while (*p) { | |
1621 | perm = rb_entry(*p, struct ceph_pool_perm, node); | |
1622 | if (pool < perm->pool) | |
1623 | p = &(*p)->rb_left; | |
1624 | else if (pool > perm->pool) | |
1625 | p = &(*p)->rb_right; | |
1626 | else { | |
1627 | have = perm->perm; | |
1628 | break; | |
1629 | } | |
1630 | } | |
1631 | up_read(&mdsc->pool_perm_rwsem); | |
1632 | if (*p) | |
1633 | goto out; | |
1634 | ||
1635 | dout("__ceph_pool_perm_get pool %u no perm cached\n", pool); | |
1636 | ||
1637 | down_write(&mdsc->pool_perm_rwsem); | |
1638 | parent = NULL; | |
1639 | while (*p) { | |
1640 | parent = *p; | |
1641 | perm = rb_entry(parent, struct ceph_pool_perm, node); | |
1642 | if (pool < perm->pool) | |
1643 | p = &(*p)->rb_left; | |
1644 | else if (pool > perm->pool) | |
1645 | p = &(*p)->rb_right; | |
1646 | else { | |
1647 | have = perm->perm; | |
1648 | break; | |
1649 | } | |
1650 | } | |
1651 | if (*p) { | |
1652 | up_write(&mdsc->pool_perm_rwsem); | |
1653 | goto out; | |
1654 | } | |
1655 | ||
1656 | rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, | |
7b06a826 | 1657 | ceph_empty_snapc, |
10183a69 YZ |
1658 | 1, false, GFP_NOFS); |
1659 | if (!rd_req) { | |
1660 | err = -ENOMEM; | |
1661 | goto out_unlock; | |
1662 | } | |
1663 | ||
1664 | rd_req->r_flags = CEPH_OSD_FLAG_READ; | |
1665 | osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0); | |
1666 | rd_req->r_base_oloc.pool = pool; | |
1667 | snprintf(rd_req->r_base_oid.name, sizeof(rd_req->r_base_oid.name), | |
1668 | "%llx.00000000", ci->i_vino.ino); | |
1669 | rd_req->r_base_oid.name_len = strlen(rd_req->r_base_oid.name); | |
1670 | ||
1671 | wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, | |
7b06a826 | 1672 | ceph_empty_snapc, |
10183a69 YZ |
1673 | 1, false, GFP_NOFS); |
1674 | if (!wr_req) { | |
1675 | err = -ENOMEM; | |
1676 | goto out_unlock; | |
1677 | } | |
1678 | ||
1679 | wr_req->r_flags = CEPH_OSD_FLAG_WRITE | | |
1680 | CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK; | |
1681 | osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL); | |
1682 | wr_req->r_base_oloc.pool = pool; | |
1683 | wr_req->r_base_oid = rd_req->r_base_oid; | |
1684 | ||
1685 | /* one page should be large enough for STAT data */ | |
1686 | pages = ceph_alloc_page_vector(1, GFP_KERNEL); | |
1687 | if (IS_ERR(pages)) { | |
1688 | err = PTR_ERR(pages); | |
1689 | goto out_unlock; | |
1690 | } | |
1691 | ||
1692 | osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE, | |
1693 | 0, false, true); | |
1694 | ceph_osdc_build_request(rd_req, 0, NULL, CEPH_NOSNAP, | |
1695 | &ci->vfs_inode.i_mtime); | |
1696 | err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false); | |
1697 | ||
1698 | ceph_osdc_build_request(wr_req, 0, NULL, CEPH_NOSNAP, | |
1699 | &ci->vfs_inode.i_mtime); | |
1700 | err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false); | |
1701 | ||
1702 | if (!err) | |
1703 | err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req); | |
1704 | if (!err2) | |
1705 | err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req); | |
1706 | ||
1707 | if (err >= 0 || err == -ENOENT) | |
1708 | have |= POOL_READ; | |
1709 | else if (err != -EPERM) | |
1710 | goto out_unlock; | |
1711 | ||
1712 | if (err2 == 0 || err2 == -EEXIST) | |
1713 | have |= POOL_WRITE; | |
1714 | else if (err2 != -EPERM) { | |
1715 | err = err2; | |
1716 | goto out_unlock; | |
1717 | } | |
1718 | ||
1719 | perm = kmalloc(sizeof(*perm), GFP_NOFS); | |
1720 | if (!perm) { | |
1721 | err = -ENOMEM; | |
1722 | goto out_unlock; | |
1723 | } | |
1724 | ||
1725 | perm->pool = pool; | |
1726 | perm->perm = have; | |
1727 | rb_link_node(&perm->node, parent, p); | |
1728 | rb_insert_color(&perm->node, &mdsc->pool_perm_tree); | |
1729 | err = 0; | |
1730 | out_unlock: | |
1731 | up_write(&mdsc->pool_perm_rwsem); | |
1732 | ||
1733 | if (rd_req) | |
1734 | ceph_osdc_put_request(rd_req); | |
1735 | if (wr_req) | |
1736 | ceph_osdc_put_request(wr_req); | |
1737 | out: | |
1738 | if (!err) | |
1739 | err = have; | |
1740 | dout("__ceph_pool_perm_get pool %u result = %d\n", pool, err); | |
1741 | return err; | |
1742 | } | |
1743 | ||
1744 | int ceph_pool_perm_check(struct ceph_inode_info *ci, int need) | |
1745 | { | |
1746 | u32 pool; | |
1747 | int ret, flags; | |
1748 | ||
1749 | if (ceph_test_mount_opt(ceph_inode_to_client(&ci->vfs_inode), | |
1750 | NOPOOLPERM)) | |
1751 | return 0; | |
1752 | ||
1753 | spin_lock(&ci->i_ceph_lock); | |
1754 | flags = ci->i_ceph_flags; | |
1755 | pool = ceph_file_layout_pg_pool(ci->i_layout); | |
1756 | spin_unlock(&ci->i_ceph_lock); | |
1757 | check: | |
1758 | if (flags & CEPH_I_POOL_PERM) { | |
1759 | if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) { | |
1760 | dout("ceph_pool_perm_check pool %u no read perm\n", | |
1761 | pool); | |
1762 | return -EPERM; | |
1763 | } | |
1764 | if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) { | |
1765 | dout("ceph_pool_perm_check pool %u no write perm\n", | |
1766 | pool); | |
1767 | return -EPERM; | |
1768 | } | |
1769 | return 0; | |
1770 | } | |
1771 | ||
1772 | ret = __ceph_pool_perm_get(ci, pool); | |
1773 | if (ret < 0) | |
1774 | return ret; | |
1775 | ||
1776 | flags = CEPH_I_POOL_PERM; | |
1777 | if (ret & POOL_READ) | |
1778 | flags |= CEPH_I_POOL_RD; | |
1779 | if (ret & POOL_WRITE) | |
1780 | flags |= CEPH_I_POOL_WR; | |
1781 | ||
1782 | spin_lock(&ci->i_ceph_lock); | |
1783 | if (pool == ceph_file_layout_pg_pool(ci->i_layout)) { | |
1784 | ci->i_ceph_flags = flags; | |
1785 | } else { | |
1786 | pool = ceph_file_layout_pg_pool(ci->i_layout); | |
1787 | flags = ci->i_ceph_flags; | |
1788 | } | |
1789 | spin_unlock(&ci->i_ceph_lock); | |
1790 | goto check; | |
1791 | } | |
1792 | ||
1793 | void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc) | |
1794 | { | |
1795 | struct ceph_pool_perm *perm; | |
1796 | struct rb_node *n; | |
1797 | ||
1798 | while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) { | |
1799 | n = rb_first(&mdsc->pool_perm_tree); | |
1800 | perm = rb_entry(n, struct ceph_pool_perm, node); | |
1801 | rb_erase(n, &mdsc->pool_perm_tree); | |
1802 | kfree(perm); | |
1803 | } | |
1804 | } |