]>
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 | |
43838685 | 279 | if (rc < 0 && rc != ENOENT) |
f36132a7 | 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 | 442 | static struct ceph_snap_context *get_oldest_context(struct inode *inode, |
e1966b49 | 443 | loff_t *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); |
e1966b49 | 483 | loff_t snap_size = -1; |
2baba250 | 484 | long writeback_stat; |
e1966b49 | 485 | u64 truncate_size; |
fc2744aa YZ |
486 | u32 truncate_seq; |
487 | int err = 0, len = PAGE_CACHE_SIZE; | |
1d3576fd SW |
488 | |
489 | dout("writepage %p idx %lu\n", page, page->index); | |
490 | ||
491 | if (!page->mapping || !page->mapping->host) { | |
492 | dout("writepage %p - no mapping\n", page); | |
493 | return -EFAULT; | |
494 | } | |
495 | inode = page->mapping->host; | |
496 | ci = ceph_inode(inode); | |
3d14c5d2 YS |
497 | fsc = ceph_inode_to_client(inode); |
498 | osdc = &fsc->client->osdc; | |
1d3576fd SW |
499 | |
500 | /* verify this is a writeable snap context */ | |
61600ef8 | 501 | snapc = page_snap_context(page); |
1d3576fd SW |
502 | if (snapc == NULL) { |
503 | dout("writepage %p page %p not dirty?\n", inode, page); | |
504 | goto out; | |
505 | } | |
6298a337 SW |
506 | oldest = get_oldest_context(inode, &snap_size); |
507 | if (snapc->seq > oldest->seq) { | |
1d3576fd | 508 | dout("writepage %p page %p snapc %p not writeable - noop\n", |
61600ef8 | 509 | inode, page, snapc); |
1d3576fd SW |
510 | /* we should only noop if called by kswapd */ |
511 | WARN_ON((current->flags & PF_MEMALLOC) == 0); | |
6298a337 | 512 | ceph_put_snap_context(oldest); |
1d3576fd SW |
513 | goto out; |
514 | } | |
6298a337 | 515 | ceph_put_snap_context(oldest); |
1d3576fd | 516 | |
fc2744aa YZ |
517 | spin_lock(&ci->i_ceph_lock); |
518 | truncate_seq = ci->i_truncate_seq; | |
519 | truncate_size = ci->i_truncate_size; | |
e1966b49 | 520 | if (snap_size == -1) |
fc2744aa YZ |
521 | snap_size = i_size_read(inode); |
522 | spin_unlock(&ci->i_ceph_lock); | |
523 | ||
1d3576fd | 524 | /* is this a partial page at end of file? */ |
fc2744aa YZ |
525 | if (page_off >= snap_size) { |
526 | dout("%p page eof %llu\n", page, snap_size); | |
527 | goto out; | |
528 | } | |
529 | if (snap_size < page_off + len) | |
530 | len = snap_size - page_off; | |
1d3576fd | 531 | |
ae00d4f3 SW |
532 | dout("writepage %p page %p index %lu on %llu~%u snapc %p\n", |
533 | inode, page, page->index, page_off, len, snapc); | |
1d3576fd | 534 | |
3d14c5d2 | 535 | writeback_stat = atomic_long_inc_return(&fsc->writeback_count); |
2baba250 | 536 | if (writeback_stat > |
3d14c5d2 YS |
537 | CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb)) |
538 | set_bdi_congested(&fsc->backing_dev_info, BLK_RW_ASYNC); | |
2baba250 | 539 | |
99ccbd22 MT |
540 | ceph_readpage_to_fscache(inode, page); |
541 | ||
1d3576fd SW |
542 | set_page_writeback(page); |
543 | err = ceph_osdc_writepages(osdc, ceph_vino(inode), | |
544 | &ci->i_layout, snapc, | |
545 | page_off, len, | |
fc2744aa | 546 | truncate_seq, truncate_size, |
24808826 | 547 | &inode->i_mtime, &page, 1); |
1d3576fd SW |
548 | if (err < 0) { |
549 | dout("writepage setting page/mapping error %d %p\n", err, page); | |
550 | SetPageError(page); | |
551 | mapping_set_error(&inode->i_data, err); | |
552 | if (wbc) | |
553 | wbc->pages_skipped++; | |
554 | } else { | |
555 | dout("writepage cleaned page %p\n", page); | |
556 | err = 0; /* vfs expects us to return 0 */ | |
557 | } | |
558 | page->private = 0; | |
559 | ClearPagePrivate(page); | |
560 | end_page_writeback(page); | |
561 | ceph_put_wrbuffer_cap_refs(ci, 1, snapc); | |
6298a337 | 562 | ceph_put_snap_context(snapc); /* page's reference */ |
1d3576fd SW |
563 | out: |
564 | return err; | |
565 | } | |
566 | ||
567 | static int ceph_writepage(struct page *page, struct writeback_control *wbc) | |
568 | { | |
dbd646a8 YS |
569 | int err; |
570 | struct inode *inode = page->mapping->host; | |
571 | BUG_ON(!inode); | |
70b666c3 | 572 | ihold(inode); |
dbd646a8 | 573 | err = writepage_nounlock(page, wbc); |
1d3576fd | 574 | unlock_page(page); |
dbd646a8 | 575 | iput(inode); |
1d3576fd SW |
576 | return err; |
577 | } | |
578 | ||
579 | ||
580 | /* | |
581 | * lame release_pages helper. release_pages() isn't exported to | |
582 | * modules. | |
583 | */ | |
584 | static void ceph_release_pages(struct page **pages, int num) | |
585 | { | |
586 | struct pagevec pvec; | |
587 | int i; | |
588 | ||
589 | pagevec_init(&pvec, 0); | |
590 | for (i = 0; i < num; i++) { | |
591 | if (pagevec_add(&pvec, pages[i]) == 0) | |
592 | pagevec_release(&pvec); | |
593 | } | |
594 | pagevec_release(&pvec); | |
595 | } | |
596 | ||
1d3576fd SW |
597 | /* |
598 | * async writeback completion handler. | |
599 | * | |
600 | * If we get an error, set the mapping error bit, but not the individual | |
601 | * page error bits. | |
602 | */ | |
603 | static void writepages_finish(struct ceph_osd_request *req, | |
604 | struct ceph_msg *msg) | |
605 | { | |
606 | struct inode *inode = req->r_inode; | |
1d3576fd | 607 | struct ceph_inode_info *ci = ceph_inode(inode); |
87060c10 | 608 | struct ceph_osd_data *osd_data; |
1d3576fd | 609 | unsigned wrote; |
1d3576fd | 610 | struct page *page; |
e0c59487 | 611 | int num_pages; |
1d3576fd SW |
612 | int i; |
613 | struct ceph_snap_context *snapc = req->r_snapc; | |
614 | struct address_space *mapping = inode->i_mapping; | |
1b83bef2 | 615 | int rc = req->r_result; |
79528734 | 616 | u64 bytes = req->r_ops[0].extent.length; |
3d14c5d2 | 617 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
2baba250 | 618 | long writeback_stat; |
7ff899da | 619 | unsigned issued = ceph_caps_issued(ci); |
1d3576fd | 620 | |
406e2c9f | 621 | osd_data = osd_req_op_extent_osd_data(req, 0); |
87060c10 AE |
622 | BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES); |
623 | num_pages = calc_pages_for((u64)osd_data->alignment, | |
624 | (u64)osd_data->length); | |
1d3576fd | 625 | if (rc >= 0) { |
79788c69 SW |
626 | /* |
627 | * Assume we wrote the pages we originally sent. The | |
628 | * osd might reply with fewer pages if our writeback | |
629 | * raced with a truncation and was adjusted at the osd, | |
630 | * so don't believe the reply. | |
631 | */ | |
e0c59487 | 632 | wrote = num_pages; |
1d3576fd SW |
633 | } else { |
634 | wrote = 0; | |
635 | mapping_set_error(mapping, rc); | |
636 | } | |
637 | dout("writepages_finish %p rc %d bytes %llu wrote %d (pages)\n", | |
638 | inode, rc, bytes, wrote); | |
639 | ||
640 | /* clean all pages */ | |
e0c59487 | 641 | for (i = 0; i < num_pages; i++) { |
87060c10 | 642 | page = osd_data->pages[i]; |
1d3576fd SW |
643 | BUG_ON(!page); |
644 | WARN_ON(!PageUptodate(page)); | |
645 | ||
2baba250 | 646 | writeback_stat = |
3d14c5d2 | 647 | atomic_long_dec_return(&fsc->writeback_count); |
2baba250 | 648 | if (writeback_stat < |
3d14c5d2 YS |
649 | CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb)) |
650 | clear_bdi_congested(&fsc->backing_dev_info, | |
2baba250 YS |
651 | BLK_RW_ASYNC); |
652 | ||
61600ef8 | 653 | ceph_put_snap_context(page_snap_context(page)); |
1d3576fd SW |
654 | page->private = 0; |
655 | ClearPagePrivate(page); | |
1d3576fd SW |
656 | dout("unlocking %d %p\n", i, page); |
657 | end_page_writeback(page); | |
e63dc5c7 YS |
658 | |
659 | /* | |
660 | * We lost the cache cap, need to truncate the page before | |
661 | * it is unlocked, otherwise we'd truncate it later in the | |
662 | * page truncation thread, possibly losing some data that | |
663 | * raced its way in | |
664 | */ | |
2962507c | 665 | if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) |
e63dc5c7 YS |
666 | generic_error_remove_page(inode->i_mapping, page); |
667 | ||
1d3576fd SW |
668 | unlock_page(page); |
669 | } | |
670 | dout("%p wrote+cleaned %d pages\n", inode, wrote); | |
e0c59487 | 671 | ceph_put_wrbuffer_cap_refs(ci, num_pages, snapc); |
1d3576fd | 672 | |
87060c10 AE |
673 | ceph_release_pages(osd_data->pages, num_pages); |
674 | if (osd_data->pages_from_pool) | |
675 | mempool_free(osd_data->pages, | |
640ef79d | 676 | ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool); |
1d3576fd | 677 | else |
87060c10 | 678 | kfree(osd_data->pages); |
1d3576fd SW |
679 | ceph_osdc_put_request(req); |
680 | } | |
681 | ||
1d3576fd SW |
682 | /* |
683 | * initiate async writeback | |
684 | */ | |
685 | static int ceph_writepages_start(struct address_space *mapping, | |
686 | struct writeback_control *wbc) | |
687 | { | |
688 | struct inode *inode = mapping->host; | |
1d3576fd | 689 | struct ceph_inode_info *ci = ceph_inode(inode); |
fc2744aa YZ |
690 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
691 | struct ceph_vino vino = ceph_vino(inode); | |
1d3576fd SW |
692 | pgoff_t index, start, end; |
693 | int range_whole = 0; | |
694 | int should_loop = 1; | |
695 | pgoff_t max_pages = 0, max_pages_ever = 0; | |
80e755fe | 696 | struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc; |
1d3576fd SW |
697 | struct pagevec pvec; |
698 | int done = 0; | |
699 | int rc = 0; | |
700 | unsigned wsize = 1 << inode->i_blkbits; | |
701 | struct ceph_osd_request *req = NULL; | |
021b77be | 702 | int do_sync = 0; |
e1966b49 YZ |
703 | loff_t snap_size, i_size; |
704 | u64 truncate_size; | |
fc2744aa | 705 | u32 truncate_seq; |
1d3576fd SW |
706 | |
707 | /* | |
708 | * Include a 'sync' in the OSD request if this is a data | |
709 | * integrity write (e.g., O_SYNC write or fsync()), or if our | |
710 | * cap is being revoked. | |
711 | */ | |
c62988ec | 712 | if ((wbc->sync_mode == WB_SYNC_ALL) || |
713 | ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER)) | |
1d3576fd SW |
714 | do_sync = 1; |
715 | dout("writepages_start %p dosync=%d (mode=%s)\n", | |
716 | inode, do_sync, | |
717 | wbc->sync_mode == WB_SYNC_NONE ? "NONE" : | |
718 | (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD")); | |
719 | ||
48fec5d0 | 720 | if (ACCESS_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) { |
f3ae1b97 | 721 | pr_warn("writepage_start %p on forced umount\n", inode); |
a341d4df YZ |
722 | truncate_pagecache(inode, 0); |
723 | mapping_set_error(mapping, -EIO); | |
1d3576fd SW |
724 | return -EIO; /* we're in a forced umount, don't write! */ |
725 | } | |
3d14c5d2 YS |
726 | if (fsc->mount_options->wsize && fsc->mount_options->wsize < wsize) |
727 | wsize = fsc->mount_options->wsize; | |
1d3576fd SW |
728 | if (wsize < PAGE_CACHE_SIZE) |
729 | wsize = PAGE_CACHE_SIZE; | |
730 | max_pages_ever = wsize >> PAGE_CACHE_SHIFT; | |
731 | ||
732 | pagevec_init(&pvec, 0); | |
733 | ||
1d3576fd SW |
734 | /* where to start/end? */ |
735 | if (wbc->range_cyclic) { | |
736 | start = mapping->writeback_index; /* Start from prev offset */ | |
737 | end = -1; | |
738 | dout(" cyclic, start at %lu\n", start); | |
739 | } else { | |
740 | start = wbc->range_start >> PAGE_CACHE_SHIFT; | |
741 | end = wbc->range_end >> PAGE_CACHE_SHIFT; | |
742 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) | |
743 | range_whole = 1; | |
744 | should_loop = 0; | |
745 | dout(" not cyclic, %lu to %lu\n", start, end); | |
746 | } | |
747 | index = start; | |
748 | ||
749 | retry: | |
750 | /* find oldest snap context with dirty data */ | |
751 | ceph_put_snap_context(snapc); | |
e1966b49 | 752 | snap_size = -1; |
1d3576fd SW |
753 | snapc = get_oldest_context(inode, &snap_size); |
754 | if (!snapc) { | |
755 | /* hmm, why does writepages get called when there | |
756 | is no dirty data? */ | |
757 | dout(" no snap context with dirty data?\n"); | |
758 | goto out; | |
759 | } | |
760 | dout(" oldest snapc is %p seq %lld (%d snaps)\n", | |
761 | snapc, snapc->seq, snapc->num_snaps); | |
fc2744aa YZ |
762 | |
763 | spin_lock(&ci->i_ceph_lock); | |
764 | truncate_seq = ci->i_truncate_seq; | |
765 | truncate_size = ci->i_truncate_size; | |
e1966b49 | 766 | i_size = i_size_read(inode); |
fc2744aa YZ |
767 | spin_unlock(&ci->i_ceph_lock); |
768 | ||
1d3576fd SW |
769 | if (last_snapc && snapc != last_snapc) { |
770 | /* if we switched to a newer snapc, restart our scan at the | |
771 | * start of the original file range. */ | |
772 | dout(" snapc differs from last pass, restarting at %lu\n", | |
773 | index); | |
774 | index = start; | |
775 | } | |
776 | last_snapc = snapc; | |
777 | ||
778 | while (!done && index <= end) { | |
779 | unsigned i; | |
780 | int first; | |
781 | pgoff_t next; | |
782 | int pvec_pages, locked_pages; | |
e5975c7c AE |
783 | struct page **pages = NULL; |
784 | mempool_t *pool = NULL; /* Becomes non-null if mempool used */ | |
1d3576fd SW |
785 | struct page *page; |
786 | int want; | |
787 | u64 offset, len; | |
2baba250 | 788 | long writeback_stat; |
1d3576fd SW |
789 | |
790 | next = 0; | |
791 | locked_pages = 0; | |
792 | max_pages = max_pages_ever; | |
793 | ||
794 | get_more_pages: | |
795 | first = -1; | |
796 | want = min(end - index, | |
797 | min((pgoff_t)PAGEVEC_SIZE, | |
798 | max_pages - (pgoff_t)locked_pages) - 1) | |
799 | + 1; | |
800 | pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index, | |
801 | PAGECACHE_TAG_DIRTY, | |
802 | want); | |
803 | dout("pagevec_lookup_tag got %d\n", pvec_pages); | |
804 | if (!pvec_pages && !locked_pages) | |
805 | break; | |
806 | for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) { | |
807 | page = pvec.pages[i]; | |
808 | dout("? %p idx %lu\n", page, page->index); | |
809 | if (locked_pages == 0) | |
810 | lock_page(page); /* first page */ | |
811 | else if (!trylock_page(page)) | |
812 | break; | |
813 | ||
814 | /* only dirty pages, or our accounting breaks */ | |
815 | if (unlikely(!PageDirty(page)) || | |
816 | unlikely(page->mapping != mapping)) { | |
817 | dout("!dirty or !mapping %p\n", page); | |
818 | unlock_page(page); | |
819 | break; | |
820 | } | |
821 | if (!wbc->range_cyclic && page->index > end) { | |
822 | dout("end of range %p\n", page); | |
823 | done = 1; | |
824 | unlock_page(page); | |
825 | break; | |
826 | } | |
827 | if (next && (page->index != next)) { | |
828 | dout("not consecutive %p\n", page); | |
829 | unlock_page(page); | |
830 | break; | |
831 | } | |
832 | if (wbc->sync_mode != WB_SYNC_NONE) { | |
833 | dout("waiting on writeback %p\n", page); | |
834 | wait_on_page_writeback(page); | |
835 | } | |
e1966b49 YZ |
836 | if (page_offset(page) >= |
837 | (snap_size == -1 ? i_size : snap_size)) { | |
838 | dout("%p page eof %llu\n", page, | |
839 | (snap_size == -1 ? i_size : snap_size)); | |
1d3576fd SW |
840 | done = 1; |
841 | unlock_page(page); | |
842 | break; | |
843 | } | |
844 | if (PageWriteback(page)) { | |
845 | dout("%p under writeback\n", page); | |
846 | unlock_page(page); | |
847 | break; | |
848 | } | |
849 | ||
850 | /* only if matching snap context */ | |
61600ef8 | 851 | pgsnapc = page_snap_context(page); |
80e755fe SW |
852 | if (pgsnapc->seq > snapc->seq) { |
853 | dout("page snapc %p %lld > oldest %p %lld\n", | |
854 | pgsnapc, pgsnapc->seq, snapc, snapc->seq); | |
1d3576fd SW |
855 | unlock_page(page); |
856 | if (!locked_pages) | |
857 | continue; /* keep looking for snap */ | |
858 | break; | |
859 | } | |
860 | ||
861 | if (!clear_page_dirty_for_io(page)) { | |
862 | dout("%p !clear_page_dirty_for_io\n", page); | |
863 | unlock_page(page); | |
864 | break; | |
865 | } | |
866 | ||
e5975c7c AE |
867 | /* |
868 | * We have something to write. If this is | |
869 | * the first locked page this time through, | |
870 | * allocate an osd request and a page array | |
871 | * that it will use. | |
872 | */ | |
1d3576fd | 873 | if (locked_pages == 0) { |
e5975c7c | 874 | BUG_ON(pages); |
1d3576fd | 875 | /* prepare async write request */ |
e5975c7c | 876 | offset = (u64)page_offset(page); |
1d3576fd | 877 | len = wsize; |
fc2744aa YZ |
878 | req = ceph_osdc_new_request(&fsc->client->osdc, |
879 | &ci->i_layout, vino, | |
715e4cd4 YZ |
880 | offset, &len, 0, |
881 | do_sync ? 2 : 1, | |
fc2744aa YZ |
882 | CEPH_OSD_OP_WRITE, |
883 | CEPH_OSD_FLAG_WRITE | | |
884 | CEPH_OSD_FLAG_ONDISK, | |
885 | snapc, truncate_seq, | |
886 | truncate_size, true); | |
6816282d SW |
887 | if (IS_ERR(req)) { |
888 | rc = PTR_ERR(req); | |
8c71897b HC |
889 | unlock_page(page); |
890 | break; | |
891 | } | |
892 | ||
715e4cd4 | 893 | if (do_sync) |
144cba14 YZ |
894 | osd_req_op_init(req, 1, |
895 | CEPH_OSD_OP_STARTSYNC, 0); | |
715e4cd4 | 896 | |
88486957 AE |
897 | req->r_callback = writepages_finish; |
898 | req->r_inode = inode; | |
899 | ||
900 | max_pages = calc_pages_for(0, (u64)len); | |
fc2744aa YZ |
901 | pages = kmalloc(max_pages * sizeof (*pages), |
902 | GFP_NOFS); | |
88486957 AE |
903 | if (!pages) { |
904 | pool = fsc->wb_pagevec_pool; | |
88486957 | 905 | pages = mempool_alloc(pool, GFP_NOFS); |
e5975c7c | 906 | BUG_ON(!pages); |
88486957 | 907 | } |
1d3576fd SW |
908 | } |
909 | ||
910 | /* note position of first page in pvec */ | |
911 | if (first < 0) | |
912 | first = i; | |
913 | dout("%p will write page %p idx %lu\n", | |
914 | inode, page, page->index); | |
2baba250 | 915 | |
213c99ee | 916 | writeback_stat = |
3d14c5d2 | 917 | atomic_long_inc_return(&fsc->writeback_count); |
213c99ee | 918 | if (writeback_stat > CONGESTION_ON_THRESH( |
3d14c5d2 YS |
919 | fsc->mount_options->congestion_kb)) { |
920 | set_bdi_congested(&fsc->backing_dev_info, | |
213c99ee | 921 | BLK_RW_ASYNC); |
2baba250 YS |
922 | } |
923 | ||
1d3576fd | 924 | set_page_writeback(page); |
e5975c7c | 925 | pages[locked_pages] = page; |
1d3576fd SW |
926 | locked_pages++; |
927 | next = page->index + 1; | |
928 | } | |
929 | ||
930 | /* did we get anything? */ | |
931 | if (!locked_pages) | |
932 | goto release_pvec_pages; | |
933 | if (i) { | |
934 | int j; | |
935 | BUG_ON(!locked_pages || first < 0); | |
936 | ||
937 | if (pvec_pages && i == pvec_pages && | |
938 | locked_pages < max_pages) { | |
939 | dout("reached end pvec, trying for more\n"); | |
940 | pagevec_reinit(&pvec); | |
941 | goto get_more_pages; | |
942 | } | |
943 | ||
944 | /* shift unused pages over in the pvec... we | |
945 | * will need to release them below. */ | |
946 | for (j = i; j < pvec_pages; j++) { | |
947 | dout(" pvec leftover page %p\n", | |
948 | pvec.pages[j]); | |
949 | pvec.pages[j-i+first] = pvec.pages[j]; | |
950 | } | |
951 | pvec.nr -= i-first; | |
952 | } | |
953 | ||
e5975c7c | 954 | /* Format the osd request message and submit the write */ |
e5975c7c | 955 | offset = page_offset(pages[0]); |
e1966b49 YZ |
956 | len = (u64)locked_pages << PAGE_CACHE_SHIFT; |
957 | if (snap_size == -1) { | |
958 | len = min(len, (u64)i_size_read(inode) - offset); | |
959 | /* writepages_finish() clears writeback pages | |
960 | * according to the data length, so make sure | |
961 | * data length covers all locked pages */ | |
962 | len = max(len, 1 + | |
963 | ((u64)(locked_pages - 1) << PAGE_CACHE_SHIFT)); | |
964 | } else { | |
965 | len = min(len, snap_size - offset); | |
966 | } | |
1d3576fd SW |
967 | dout("writepages got %d pages at %llu~%llu\n", |
968 | locked_pages, offset, len); | |
969 | ||
406e2c9f | 970 | osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, |
a4ce40a9 | 971 | !!pool, false); |
e5975c7c AE |
972 | |
973 | pages = NULL; /* request message now owns the pages array */ | |
974 | pool = NULL; | |
975 | ||
976 | /* Update the write op length in case we changed it */ | |
977 | ||
c99d2d4a | 978 | osd_req_op_extent_update(req, 0, len); |
e5975c7c AE |
979 | |
980 | vino = ceph_vino(inode); | |
79528734 AE |
981 | ceph_osdc_build_request(req, offset, snapc, vino.snap, |
982 | &inode->i_mtime); | |
1d3576fd | 983 | |
9d6fcb08 SW |
984 | rc = ceph_osdc_start_request(&fsc->client->osdc, req, true); |
985 | BUG_ON(rc); | |
1d3576fd SW |
986 | req = NULL; |
987 | ||
988 | /* continue? */ | |
989 | index = next; | |
990 | wbc->nr_to_write -= locked_pages; | |
991 | if (wbc->nr_to_write <= 0) | |
992 | done = 1; | |
993 | ||
994 | release_pvec_pages: | |
995 | dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr, | |
996 | pvec.nr ? pvec.pages[0] : NULL); | |
997 | pagevec_release(&pvec); | |
998 | ||
999 | if (locked_pages && !done) | |
1000 | goto retry; | |
1001 | } | |
1002 | ||
1003 | if (should_loop && !done) { | |
1004 | /* more to do; loop back to beginning of file */ | |
1005 | dout("writepages looping back to beginning of file\n"); | |
1006 | should_loop = 0; | |
1007 | index = 0; | |
1008 | goto retry; | |
1009 | } | |
1010 | ||
1011 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) | |
1012 | mapping->writeback_index = index; | |
1013 | ||
1014 | out: | |
1015 | if (req) | |
1016 | ceph_osdc_put_request(req); | |
1d3576fd SW |
1017 | ceph_put_snap_context(snapc); |
1018 | dout("writepages done, rc = %d\n", rc); | |
1d3576fd SW |
1019 | return rc; |
1020 | } | |
1021 | ||
1022 | ||
1023 | ||
1024 | /* | |
1025 | * See if a given @snapc is either writeable, or already written. | |
1026 | */ | |
1027 | static int context_is_writeable_or_written(struct inode *inode, | |
1028 | struct ceph_snap_context *snapc) | |
1029 | { | |
1030 | struct ceph_snap_context *oldest = get_oldest_context(inode, NULL); | |
6298a337 SW |
1031 | int ret = !oldest || snapc->seq <= oldest->seq; |
1032 | ||
1033 | ceph_put_snap_context(oldest); | |
1034 | return ret; | |
1d3576fd SW |
1035 | } |
1036 | ||
1037 | /* | |
1038 | * We are only allowed to write into/dirty the page if the page is | |
1039 | * clean, or already dirty within the same snap context. | |
8f883c24 SW |
1040 | * |
1041 | * called with page locked. | |
1042 | * return success with page locked, | |
1043 | * or any failure (incl -EAGAIN) with page unlocked. | |
1d3576fd | 1044 | */ |
4af6b225 YS |
1045 | static int ceph_update_writeable_page(struct file *file, |
1046 | loff_t pos, unsigned len, | |
1047 | struct page *page) | |
1d3576fd | 1048 | { |
496ad9aa | 1049 | struct inode *inode = file_inode(file); |
1d3576fd | 1050 | struct ceph_inode_info *ci = ceph_inode(inode); |
1d3576fd SW |
1051 | loff_t page_off = pos & PAGE_CACHE_MASK; |
1052 | int pos_in_page = pos & ~PAGE_CACHE_MASK; | |
1053 | int end_in_page = pos_in_page + len; | |
1054 | loff_t i_size; | |
1d3576fd | 1055 | int r; |
80e755fe | 1056 | struct ceph_snap_context *snapc, *oldest; |
1d3576fd | 1057 | |
1d3576fd SW |
1058 | retry_locked: |
1059 | /* writepages currently holds page lock, but if we change that later, */ | |
1060 | wait_on_page_writeback(page); | |
1061 | ||
61600ef8 | 1062 | snapc = page_snap_context(page); |
80e755fe | 1063 | if (snapc && snapc != ci->i_head_snapc) { |
1d3576fd SW |
1064 | /* |
1065 | * this page is already dirty in another (older) snap | |
1066 | * context! is it writeable now? | |
1067 | */ | |
80e755fe | 1068 | oldest = get_oldest_context(inode, NULL); |
1d3576fd | 1069 | |
80e755fe | 1070 | if (snapc->seq > oldest->seq) { |
6298a337 | 1071 | ceph_put_snap_context(oldest); |
1d3576fd | 1072 | dout(" page %p snapc %p not current or oldest\n", |
6298a337 | 1073 | page, snapc); |
1d3576fd SW |
1074 | /* |
1075 | * queue for writeback, and wait for snapc to | |
1076 | * be writeable or written | |
1077 | */ | |
6298a337 | 1078 | snapc = ceph_get_snap_context(snapc); |
1d3576fd | 1079 | unlock_page(page); |
3c6f6b79 | 1080 | ceph_queue_writeback(inode); |
8f883c24 | 1081 | r = wait_event_interruptible(ci->i_cap_wq, |
1d3576fd SW |
1082 | context_is_writeable_or_written(inode, snapc)); |
1083 | ceph_put_snap_context(snapc); | |
8f883c24 SW |
1084 | if (r == -ERESTARTSYS) |
1085 | return r; | |
4af6b225 | 1086 | return -EAGAIN; |
1d3576fd | 1087 | } |
6298a337 | 1088 | ceph_put_snap_context(oldest); |
1d3576fd SW |
1089 | |
1090 | /* yay, writeable, do it now (without dropping page lock) */ | |
1091 | dout(" page %p snapc %p not current, but oldest\n", | |
1092 | page, snapc); | |
1093 | if (!clear_page_dirty_for_io(page)) | |
1094 | goto retry_locked; | |
1095 | r = writepage_nounlock(page, NULL); | |
1096 | if (r < 0) | |
1097 | goto fail_nosnap; | |
1098 | goto retry_locked; | |
1099 | } | |
1100 | ||
1101 | if (PageUptodate(page)) { | |
1102 | dout(" page %p already uptodate\n", page); | |
1103 | return 0; | |
1104 | } | |
1105 | ||
1106 | /* full page? */ | |
1107 | if (pos_in_page == 0 && len == PAGE_CACHE_SIZE) | |
1108 | return 0; | |
1109 | ||
1110 | /* past end of file? */ | |
99c88e69 | 1111 | i_size = i_size_read(inode); |
1d3576fd | 1112 | |
1d3576fd SW |
1113 | if (page_off >= i_size || |
1114 | (pos_in_page == 0 && (pos+len) >= i_size && | |
1115 | end_in_page - pos_in_page != PAGE_CACHE_SIZE)) { | |
1116 | dout(" zeroing %p 0 - %d and %d - %d\n", | |
1117 | page, pos_in_page, end_in_page, (int)PAGE_CACHE_SIZE); | |
1118 | zero_user_segments(page, | |
1119 | 0, pos_in_page, | |
1120 | end_in_page, PAGE_CACHE_SIZE); | |
1121 | return 0; | |
1122 | } | |
1123 | ||
1124 | /* we need to read it. */ | |
1d3576fd SW |
1125 | r = readpage_nounlock(file, page); |
1126 | if (r < 0) | |
1127 | goto fail_nosnap; | |
1128 | goto retry_locked; | |
1d3576fd SW |
1129 | fail_nosnap: |
1130 | unlock_page(page); | |
1131 | return r; | |
1132 | } | |
1133 | ||
4af6b225 YS |
1134 | /* |
1135 | * We are only allowed to write into/dirty the page if the page is | |
1136 | * clean, or already dirty within the same snap context. | |
1137 | */ | |
1138 | static int ceph_write_begin(struct file *file, struct address_space *mapping, | |
1139 | loff_t pos, unsigned len, unsigned flags, | |
1140 | struct page **pagep, void **fsdata) | |
1141 | { | |
496ad9aa | 1142 | struct inode *inode = file_inode(file); |
4af6b225 YS |
1143 | struct page *page; |
1144 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; | |
7971bd92 | 1145 | int r; |
4af6b225 YS |
1146 | |
1147 | do { | |
8f883c24 | 1148 | /* get a page */ |
4af6b225 | 1149 | page = grab_cache_page_write_begin(mapping, index, 0); |
7971bd92 SW |
1150 | if (!page) |
1151 | return -ENOMEM; | |
4af6b225 YS |
1152 | |
1153 | dout("write_begin file %p inode %p page %p %d~%d\n", file, | |
213c99ee | 1154 | inode, page, (int)pos, (int)len); |
4af6b225 YS |
1155 | |
1156 | r = ceph_update_writeable_page(file, pos, len, page); | |
c1d00b2d TK |
1157 | if (r < 0) |
1158 | page_cache_release(page); | |
1159 | else | |
1160 | *pagep = page; | |
4af6b225 YS |
1161 | } while (r == -EAGAIN); |
1162 | ||
1163 | return r; | |
1164 | } | |
1165 | ||
1d3576fd SW |
1166 | /* |
1167 | * we don't do anything in here that simple_write_end doesn't do | |
5dda377c | 1168 | * except adjust dirty page accounting |
1d3576fd SW |
1169 | */ |
1170 | static int ceph_write_end(struct file *file, struct address_space *mapping, | |
1171 | loff_t pos, unsigned len, unsigned copied, | |
1172 | struct page *page, void *fsdata) | |
1173 | { | |
496ad9aa | 1174 | struct inode *inode = file_inode(file); |
1d3576fd SW |
1175 | unsigned from = pos & (PAGE_CACHE_SIZE - 1); |
1176 | int check_cap = 0; | |
1177 | ||
1178 | dout("write_end file %p inode %p page %p %d~%d (%d)\n", file, | |
1179 | inode, page, (int)pos, (int)copied, (int)len); | |
1180 | ||
1181 | /* zero the stale part of the page if we did a short copy */ | |
1182 | if (copied < len) | |
1183 | zero_user_segment(page, from+copied, len); | |
1184 | ||
1185 | /* did file size increase? */ | |
99c88e69 | 1186 | if (pos+copied > i_size_read(inode)) |
1d3576fd SW |
1187 | check_cap = ceph_inode_set_size(inode, pos+copied); |
1188 | ||
1189 | if (!PageUptodate(page)) | |
1190 | SetPageUptodate(page); | |
1191 | ||
1192 | set_page_dirty(page); | |
1193 | ||
1194 | unlock_page(page); | |
1d3576fd SW |
1195 | page_cache_release(page); |
1196 | ||
1197 | if (check_cap) | |
1198 | ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL); | |
1199 | ||
1200 | return copied; | |
1201 | } | |
1202 | ||
1203 | /* | |
1204 | * we set .direct_IO to indicate direct io is supported, but since we | |
1205 | * intercept O_DIRECT reads and writes early, this function should | |
1206 | * never get called. | |
1207 | */ | |
22c6186e | 1208 | static ssize_t ceph_direct_io(struct kiocb *iocb, struct iov_iter *iter, |
d8d3d94b | 1209 | loff_t pos) |
1d3576fd SW |
1210 | { |
1211 | WARN_ON(1); | |
1212 | return -EINVAL; | |
1213 | } | |
1214 | ||
1215 | const struct address_space_operations ceph_aops = { | |
1216 | .readpage = ceph_readpage, | |
1217 | .readpages = ceph_readpages, | |
1218 | .writepage = ceph_writepage, | |
1219 | .writepages = ceph_writepages_start, | |
1220 | .write_begin = ceph_write_begin, | |
1221 | .write_end = ceph_write_end, | |
1222 | .set_page_dirty = ceph_set_page_dirty, | |
1223 | .invalidatepage = ceph_invalidatepage, | |
1224 | .releasepage = ceph_releasepage, | |
1225 | .direct_IO = ceph_direct_io, | |
1226 | }; | |
1227 | ||
1228 | ||
1229 | /* | |
1230 | * vm ops | |
1231 | */ | |
61f68816 YZ |
1232 | static int ceph_filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
1233 | { | |
1234 | struct inode *inode = file_inode(vma->vm_file); | |
1235 | struct ceph_inode_info *ci = ceph_inode(inode); | |
1236 | struct ceph_file_info *fi = vma->vm_file->private_data; | |
3738daa6 | 1237 | struct page *pinned_page = NULL; |
61f68816 YZ |
1238 | loff_t off = vmf->pgoff << PAGE_CACHE_SHIFT; |
1239 | int want, got, ret; | |
1240 | ||
1241 | dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n", | |
37b52fe6 | 1242 | inode, ceph_vinop(inode), off, (size_t)PAGE_CACHE_SIZE); |
61f68816 YZ |
1243 | if (fi->fmode & CEPH_FILE_MODE_LAZY) |
1244 | want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO; | |
1245 | else | |
1246 | want = CEPH_CAP_FILE_CACHE; | |
1247 | while (1) { | |
1248 | got = 0; | |
83701246 YZ |
1249 | ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, |
1250 | -1, &got, &pinned_page); | |
61f68816 YZ |
1251 | if (ret == 0) |
1252 | break; | |
1253 | if (ret != -ERESTARTSYS) { | |
1254 | WARN_ON(1); | |
1255 | return VM_FAULT_SIGBUS; | |
1256 | } | |
1257 | } | |
1258 | dout("filemap_fault %p %llu~%zd got cap refs on %s\n", | |
37b52fe6 | 1259 | inode, off, (size_t)PAGE_CACHE_SIZE, ceph_cap_string(got)); |
61f68816 | 1260 | |
83701246 YZ |
1261 | if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) || |
1262 | ci->i_inline_version == CEPH_INLINE_NONE) | |
1263 | ret = filemap_fault(vma, vmf); | |
1264 | else | |
1265 | ret = -EAGAIN; | |
61f68816 YZ |
1266 | |
1267 | dout("filemap_fault %p %llu~%zd dropping cap refs on %s ret %d\n", | |
37b52fe6 | 1268 | inode, off, (size_t)PAGE_CACHE_SIZE, ceph_cap_string(got), ret); |
3738daa6 YZ |
1269 | if (pinned_page) |
1270 | page_cache_release(pinned_page); | |
61f68816 YZ |
1271 | ceph_put_cap_refs(ci, got); |
1272 | ||
83701246 YZ |
1273 | if (ret != -EAGAIN) |
1274 | return ret; | |
1275 | ||
1276 | /* read inline data */ | |
1277 | if (off >= PAGE_CACHE_SIZE) { | |
1278 | /* does not support inline data > PAGE_SIZE */ | |
1279 | ret = VM_FAULT_SIGBUS; | |
1280 | } else { | |
1281 | int ret1; | |
1282 | struct address_space *mapping = inode->i_mapping; | |
1283 | struct page *page = find_or_create_page(mapping, 0, | |
c62d2555 MH |
1284 | mapping_gfp_constraint(mapping, |
1285 | ~__GFP_FS)); | |
83701246 YZ |
1286 | if (!page) { |
1287 | ret = VM_FAULT_OOM; | |
1288 | goto out; | |
1289 | } | |
1290 | ret1 = __ceph_do_getattr(inode, page, | |
1291 | CEPH_STAT_CAP_INLINE_DATA, true); | |
1292 | if (ret1 < 0 || off >= i_size_read(inode)) { | |
1293 | unlock_page(page); | |
1294 | page_cache_release(page); | |
1295 | ret = VM_FAULT_SIGBUS; | |
1296 | goto out; | |
1297 | } | |
1298 | if (ret1 < PAGE_CACHE_SIZE) | |
1299 | zero_user_segment(page, ret1, PAGE_CACHE_SIZE); | |
1300 | else | |
1301 | flush_dcache_page(page); | |
1302 | SetPageUptodate(page); | |
1303 | vmf->page = page; | |
1304 | ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED; | |
1305 | } | |
1306 | out: | |
1307 | dout("filemap_fault %p %llu~%zd read inline data ret %d\n", | |
1308 | inode, off, (size_t)PAGE_CACHE_SIZE, ret); | |
61f68816 YZ |
1309 | return ret; |
1310 | } | |
1d3576fd SW |
1311 | |
1312 | /* | |
1313 | * Reuse write_begin here for simplicity. | |
1314 | */ | |
1315 | static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) | |
1316 | { | |
496ad9aa | 1317 | struct inode *inode = file_inode(vma->vm_file); |
61f68816 YZ |
1318 | struct ceph_inode_info *ci = ceph_inode(inode); |
1319 | struct ceph_file_info *fi = vma->vm_file->private_data; | |
f66fd9f0 | 1320 | struct ceph_cap_flush *prealloc_cf; |
61f68816 | 1321 | struct page *page = vmf->page; |
6285bc23 | 1322 | loff_t off = page_offset(page); |
61f68816 YZ |
1323 | loff_t size = i_size_read(inode); |
1324 | size_t len; | |
1325 | int want, got, ret; | |
3ca9c3bd | 1326 | |
f66fd9f0 YZ |
1327 | prealloc_cf = ceph_alloc_cap_flush(); |
1328 | if (!prealloc_cf) | |
1329 | return VM_FAULT_SIGBUS; | |
1330 | ||
28127bdd YZ |
1331 | if (ci->i_inline_version != CEPH_INLINE_NONE) { |
1332 | struct page *locked_page = NULL; | |
1333 | if (off == 0) { | |
1334 | lock_page(page); | |
1335 | locked_page = page; | |
1336 | } | |
1337 | ret = ceph_uninline_data(vma->vm_file, locked_page); | |
1338 | if (locked_page) | |
1339 | unlock_page(locked_page); | |
f66fd9f0 YZ |
1340 | if (ret < 0) { |
1341 | ret = VM_FAULT_SIGBUS; | |
1342 | goto out_free; | |
1343 | } | |
28127bdd YZ |
1344 | } |
1345 | ||
1d3576fd SW |
1346 | if (off + PAGE_CACHE_SIZE <= size) |
1347 | len = PAGE_CACHE_SIZE; | |
1348 | else | |
1349 | len = size & ~PAGE_CACHE_MASK; | |
1350 | ||
61f68816 YZ |
1351 | dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n", |
1352 | inode, ceph_vinop(inode), off, len, size); | |
1353 | if (fi->fmode & CEPH_FILE_MODE_LAZY) | |
1354 | want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO; | |
1355 | else | |
1356 | want = CEPH_CAP_FILE_BUFFER; | |
1357 | while (1) { | |
1358 | got = 0; | |
3738daa6 YZ |
1359 | ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, off + len, |
1360 | &got, NULL); | |
61f68816 YZ |
1361 | if (ret == 0) |
1362 | break; | |
1363 | if (ret != -ERESTARTSYS) { | |
1364 | WARN_ON(1); | |
f66fd9f0 YZ |
1365 | ret = VM_FAULT_SIGBUS; |
1366 | goto out_free; | |
61f68816 YZ |
1367 | } |
1368 | } | |
1369 | dout("page_mkwrite %p %llu~%zd got cap refs on %s\n", | |
1370 | inode, off, len, ceph_cap_string(got)); | |
1371 | ||
1372 | /* Update time before taking page lock */ | |
1373 | file_update_time(vma->vm_file); | |
4af6b225 YS |
1374 | |
1375 | lock_page(page); | |
1376 | ||
1377 | ret = VM_FAULT_NOPAGE; | |
1378 | if ((off > size) || | |
f9cac5ac YZ |
1379 | (page->mapping != inode->i_mapping)) { |
1380 | unlock_page(page); | |
4af6b225 | 1381 | goto out; |
f9cac5ac | 1382 | } |
4af6b225 YS |
1383 | |
1384 | ret = ceph_update_writeable_page(vma->vm_file, off, len, page); | |
f9cac5ac | 1385 | if (ret >= 0) { |
4af6b225 | 1386 | /* success. we'll keep the page locked. */ |
1d3576fd | 1387 | set_page_dirty(page); |
1d3576fd SW |
1388 | ret = VM_FAULT_LOCKED; |
1389 | } else { | |
4af6b225 YS |
1390 | if (ret == -ENOMEM) |
1391 | ret = VM_FAULT_OOM; | |
1392 | else | |
1393 | ret = VM_FAULT_SIGBUS; | |
1d3576fd | 1394 | } |
4af6b225 | 1395 | out: |
28127bdd YZ |
1396 | if (ret == VM_FAULT_LOCKED || |
1397 | ci->i_inline_version != CEPH_INLINE_NONE) { | |
61f68816 YZ |
1398 | int dirty; |
1399 | spin_lock(&ci->i_ceph_lock); | |
28127bdd | 1400 | ci->i_inline_version = CEPH_INLINE_NONE; |
f66fd9f0 YZ |
1401 | dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR, |
1402 | &prealloc_cf); | |
61f68816 YZ |
1403 | spin_unlock(&ci->i_ceph_lock); |
1404 | if (dirty) | |
1405 | __mark_inode_dirty(inode, dirty); | |
1406 | } | |
1407 | ||
1408 | dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %d\n", | |
1409 | inode, off, len, ceph_cap_string(got), ret); | |
1410 | ceph_put_cap_refs(ci, got); | |
f66fd9f0 YZ |
1411 | out_free: |
1412 | ceph_free_cap_flush(prealloc_cf); | |
61f68816 | 1413 | |
1d3576fd SW |
1414 | return ret; |
1415 | } | |
1416 | ||
31c542a1 YZ |
1417 | void ceph_fill_inline_data(struct inode *inode, struct page *locked_page, |
1418 | char *data, size_t len) | |
1419 | { | |
1420 | struct address_space *mapping = inode->i_mapping; | |
1421 | struct page *page; | |
1422 | ||
1423 | if (locked_page) { | |
1424 | page = locked_page; | |
1425 | } else { | |
1426 | if (i_size_read(inode) == 0) | |
1427 | return; | |
1428 | page = find_or_create_page(mapping, 0, | |
c62d2555 MH |
1429 | mapping_gfp_constraint(mapping, |
1430 | ~__GFP_FS)); | |
31c542a1 YZ |
1431 | if (!page) |
1432 | return; | |
1433 | if (PageUptodate(page)) { | |
1434 | unlock_page(page); | |
1435 | page_cache_release(page); | |
1436 | return; | |
1437 | } | |
1438 | } | |
1439 | ||
0668ff52 | 1440 | dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n", |
31c542a1 YZ |
1441 | inode, ceph_vinop(inode), len, locked_page); |
1442 | ||
1443 | if (len > 0) { | |
1444 | void *kaddr = kmap_atomic(page); | |
1445 | memcpy(kaddr, data, len); | |
1446 | kunmap_atomic(kaddr); | |
1447 | } | |
1448 | ||
1449 | if (page != locked_page) { | |
1450 | if (len < PAGE_CACHE_SIZE) | |
1451 | zero_user_segment(page, len, PAGE_CACHE_SIZE); | |
1452 | else | |
1453 | flush_dcache_page(page); | |
1454 | ||
1455 | SetPageUptodate(page); | |
1456 | unlock_page(page); | |
1457 | page_cache_release(page); | |
1458 | } | |
1459 | } | |
1460 | ||
28127bdd YZ |
1461 | int ceph_uninline_data(struct file *filp, struct page *locked_page) |
1462 | { | |
1463 | struct inode *inode = file_inode(filp); | |
1464 | struct ceph_inode_info *ci = ceph_inode(inode); | |
1465 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); | |
1466 | struct ceph_osd_request *req; | |
1467 | struct page *page = NULL; | |
1468 | u64 len, inline_version; | |
1469 | int err = 0; | |
1470 | bool from_pagecache = false; | |
1471 | ||
1472 | spin_lock(&ci->i_ceph_lock); | |
1473 | inline_version = ci->i_inline_version; | |
1474 | spin_unlock(&ci->i_ceph_lock); | |
1475 | ||
1476 | dout("uninline_data %p %llx.%llx inline_version %llu\n", | |
1477 | inode, ceph_vinop(inode), inline_version); | |
1478 | ||
1479 | if (inline_version == 1 || /* initial version, no data */ | |
1480 | inline_version == CEPH_INLINE_NONE) | |
1481 | goto out; | |
1482 | ||
1483 | if (locked_page) { | |
1484 | page = locked_page; | |
1485 | WARN_ON(!PageUptodate(page)); | |
1486 | } else if (ceph_caps_issued(ci) & | |
1487 | (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) { | |
1488 | page = find_get_page(inode->i_mapping, 0); | |
1489 | if (page) { | |
1490 | if (PageUptodate(page)) { | |
1491 | from_pagecache = true; | |
1492 | lock_page(page); | |
1493 | } else { | |
1494 | page_cache_release(page); | |
1495 | page = NULL; | |
1496 | } | |
1497 | } | |
1498 | } | |
1499 | ||
1500 | if (page) { | |
1501 | len = i_size_read(inode); | |
1502 | if (len > PAGE_CACHE_SIZE) | |
1503 | len = PAGE_CACHE_SIZE; | |
1504 | } else { | |
1505 | page = __page_cache_alloc(GFP_NOFS); | |
1506 | if (!page) { | |
1507 | err = -ENOMEM; | |
1508 | goto out; | |
1509 | } | |
1510 | err = __ceph_do_getattr(inode, page, | |
1511 | CEPH_STAT_CAP_INLINE_DATA, true); | |
1512 | if (err < 0) { | |
1513 | /* no inline data */ | |
1514 | if (err == -ENODATA) | |
1515 | err = 0; | |
1516 | goto out; | |
1517 | } | |
1518 | len = err; | |
1519 | } | |
1520 | ||
1521 | req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, | |
1522 | ceph_vino(inode), 0, &len, 0, 1, | |
1523 | CEPH_OSD_OP_CREATE, | |
1524 | CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE, | |
7b06a826 | 1525 | ceph_empty_snapc, 0, 0, false); |
28127bdd YZ |
1526 | if (IS_ERR(req)) { |
1527 | err = PTR_ERR(req); | |
1528 | goto out; | |
1529 | } | |
1530 | ||
1531 | ceph_osdc_build_request(req, 0, NULL, CEPH_NOSNAP, &inode->i_mtime); | |
1532 | err = ceph_osdc_start_request(&fsc->client->osdc, req, false); | |
1533 | if (!err) | |
1534 | err = ceph_osdc_wait_request(&fsc->client->osdc, req); | |
1535 | ceph_osdc_put_request(req); | |
1536 | if (err < 0) | |
1537 | goto out; | |
1538 | ||
1539 | req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, | |
1540 | ceph_vino(inode), 0, &len, 1, 3, | |
1541 | CEPH_OSD_OP_WRITE, | |
1542 | CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE, | |
7b06a826 | 1543 | ceph_empty_snapc, |
28127bdd YZ |
1544 | ci->i_truncate_seq, ci->i_truncate_size, |
1545 | false); | |
1546 | if (IS_ERR(req)) { | |
1547 | err = PTR_ERR(req); | |
1548 | goto out; | |
1549 | } | |
1550 | ||
1551 | osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false); | |
1552 | ||
ec137c10 YZ |
1553 | { |
1554 | __le64 xattr_buf = cpu_to_le64(inline_version); | |
1555 | err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR, | |
1556 | "inline_version", &xattr_buf, | |
1557 | sizeof(xattr_buf), | |
1558 | CEPH_OSD_CMPXATTR_OP_GT, | |
1559 | CEPH_OSD_CMPXATTR_MODE_U64); | |
1560 | if (err) | |
1561 | goto out_put; | |
1562 | } | |
1563 | ||
1564 | { | |
1565 | char xattr_buf[32]; | |
1566 | int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf), | |
1567 | "%llu", inline_version); | |
1568 | err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR, | |
1569 | "inline_version", | |
1570 | xattr_buf, xattr_len, 0, 0); | |
1571 | if (err) | |
1572 | goto out_put; | |
1573 | } | |
28127bdd YZ |
1574 | |
1575 | ceph_osdc_build_request(req, 0, NULL, CEPH_NOSNAP, &inode->i_mtime); | |
1576 | err = ceph_osdc_start_request(&fsc->client->osdc, req, false); | |
1577 | if (!err) | |
1578 | err = ceph_osdc_wait_request(&fsc->client->osdc, req); | |
1579 | out_put: | |
1580 | ceph_osdc_put_request(req); | |
1581 | if (err == -ECANCELED) | |
1582 | err = 0; | |
1583 | out: | |
1584 | if (page && page != locked_page) { | |
1585 | if (from_pagecache) { | |
1586 | unlock_page(page); | |
1587 | page_cache_release(page); | |
1588 | } else | |
1589 | __free_pages(page, 0); | |
1590 | } | |
1591 | ||
1592 | dout("uninline_data %p %llx.%llx inline_version %llu = %d\n", | |
1593 | inode, ceph_vinop(inode), inline_version, err); | |
1594 | return err; | |
1595 | } | |
1596 | ||
7cbea8dc | 1597 | static const struct vm_operations_struct ceph_vmops = { |
61f68816 | 1598 | .fault = ceph_filemap_fault, |
1d3576fd SW |
1599 | .page_mkwrite = ceph_page_mkwrite, |
1600 | }; | |
1601 | ||
1602 | int ceph_mmap(struct file *file, struct vm_area_struct *vma) | |
1603 | { | |
1604 | struct address_space *mapping = file->f_mapping; | |
1605 | ||
1606 | if (!mapping->a_ops->readpage) | |
1607 | return -ENOEXEC; | |
1608 | file_accessed(file); | |
1609 | vma->vm_ops = &ceph_vmops; | |
1d3576fd SW |
1610 | return 0; |
1611 | } | |
10183a69 YZ |
1612 | |
1613 | enum { | |
1614 | POOL_READ = 1, | |
1615 | POOL_WRITE = 2, | |
1616 | }; | |
1617 | ||
1618 | static int __ceph_pool_perm_get(struct ceph_inode_info *ci, u32 pool) | |
1619 | { | |
1620 | struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode); | |
1621 | struct ceph_mds_client *mdsc = fsc->mdsc; | |
1622 | struct ceph_osd_request *rd_req = NULL, *wr_req = NULL; | |
1623 | struct rb_node **p, *parent; | |
1624 | struct ceph_pool_perm *perm; | |
1625 | struct page **pages; | |
1626 | int err = 0, err2 = 0, have = 0; | |
1627 | ||
1628 | down_read(&mdsc->pool_perm_rwsem); | |
1629 | p = &mdsc->pool_perm_tree.rb_node; | |
1630 | while (*p) { | |
1631 | perm = rb_entry(*p, struct ceph_pool_perm, node); | |
1632 | if (pool < perm->pool) | |
1633 | p = &(*p)->rb_left; | |
1634 | else if (pool > perm->pool) | |
1635 | p = &(*p)->rb_right; | |
1636 | else { | |
1637 | have = perm->perm; | |
1638 | break; | |
1639 | } | |
1640 | } | |
1641 | up_read(&mdsc->pool_perm_rwsem); | |
1642 | if (*p) | |
1643 | goto out; | |
1644 | ||
1645 | dout("__ceph_pool_perm_get pool %u no perm cached\n", pool); | |
1646 | ||
1647 | down_write(&mdsc->pool_perm_rwsem); | |
1648 | parent = NULL; | |
1649 | while (*p) { | |
1650 | parent = *p; | |
1651 | perm = rb_entry(parent, struct ceph_pool_perm, node); | |
1652 | if (pool < perm->pool) | |
1653 | p = &(*p)->rb_left; | |
1654 | else if (pool > perm->pool) | |
1655 | p = &(*p)->rb_right; | |
1656 | else { | |
1657 | have = perm->perm; | |
1658 | break; | |
1659 | } | |
1660 | } | |
1661 | if (*p) { | |
1662 | up_write(&mdsc->pool_perm_rwsem); | |
1663 | goto out; | |
1664 | } | |
1665 | ||
1666 | rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, | |
7b06a826 | 1667 | ceph_empty_snapc, |
10183a69 YZ |
1668 | 1, false, GFP_NOFS); |
1669 | if (!rd_req) { | |
1670 | err = -ENOMEM; | |
1671 | goto out_unlock; | |
1672 | } | |
1673 | ||
1674 | rd_req->r_flags = CEPH_OSD_FLAG_READ; | |
1675 | osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0); | |
1676 | rd_req->r_base_oloc.pool = pool; | |
1677 | snprintf(rd_req->r_base_oid.name, sizeof(rd_req->r_base_oid.name), | |
1678 | "%llx.00000000", ci->i_vino.ino); | |
1679 | rd_req->r_base_oid.name_len = strlen(rd_req->r_base_oid.name); | |
1680 | ||
1681 | wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, | |
7b06a826 | 1682 | ceph_empty_snapc, |
10183a69 YZ |
1683 | 1, false, GFP_NOFS); |
1684 | if (!wr_req) { | |
1685 | err = -ENOMEM; | |
1686 | goto out_unlock; | |
1687 | } | |
1688 | ||
1689 | wr_req->r_flags = CEPH_OSD_FLAG_WRITE | | |
1690 | CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK; | |
1691 | osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL); | |
1692 | wr_req->r_base_oloc.pool = pool; | |
1693 | wr_req->r_base_oid = rd_req->r_base_oid; | |
1694 | ||
1695 | /* one page should be large enough for STAT data */ | |
1696 | pages = ceph_alloc_page_vector(1, GFP_KERNEL); | |
1697 | if (IS_ERR(pages)) { | |
1698 | err = PTR_ERR(pages); | |
1699 | goto out_unlock; | |
1700 | } | |
1701 | ||
1702 | osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE, | |
1703 | 0, false, true); | |
1704 | ceph_osdc_build_request(rd_req, 0, NULL, CEPH_NOSNAP, | |
1705 | &ci->vfs_inode.i_mtime); | |
1706 | err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false); | |
1707 | ||
1708 | ceph_osdc_build_request(wr_req, 0, NULL, CEPH_NOSNAP, | |
1709 | &ci->vfs_inode.i_mtime); | |
1710 | err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false); | |
1711 | ||
1712 | if (!err) | |
1713 | err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req); | |
1714 | if (!err2) | |
1715 | err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req); | |
1716 | ||
1717 | if (err >= 0 || err == -ENOENT) | |
1718 | have |= POOL_READ; | |
1719 | else if (err != -EPERM) | |
1720 | goto out_unlock; | |
1721 | ||
1722 | if (err2 == 0 || err2 == -EEXIST) | |
1723 | have |= POOL_WRITE; | |
1724 | else if (err2 != -EPERM) { | |
1725 | err = err2; | |
1726 | goto out_unlock; | |
1727 | } | |
1728 | ||
1729 | perm = kmalloc(sizeof(*perm), GFP_NOFS); | |
1730 | if (!perm) { | |
1731 | err = -ENOMEM; | |
1732 | goto out_unlock; | |
1733 | } | |
1734 | ||
1735 | perm->pool = pool; | |
1736 | perm->perm = have; | |
1737 | rb_link_node(&perm->node, parent, p); | |
1738 | rb_insert_color(&perm->node, &mdsc->pool_perm_tree); | |
1739 | err = 0; | |
1740 | out_unlock: | |
1741 | up_write(&mdsc->pool_perm_rwsem); | |
1742 | ||
1743 | if (rd_req) | |
1744 | ceph_osdc_put_request(rd_req); | |
1745 | if (wr_req) | |
1746 | ceph_osdc_put_request(wr_req); | |
1747 | out: | |
1748 | if (!err) | |
1749 | err = have; | |
1750 | dout("__ceph_pool_perm_get pool %u result = %d\n", pool, err); | |
1751 | return err; | |
1752 | } | |
1753 | ||
1754 | int ceph_pool_perm_check(struct ceph_inode_info *ci, int need) | |
1755 | { | |
1756 | u32 pool; | |
1757 | int ret, flags; | |
1758 | ||
5ea5c5e0 YZ |
1759 | /* does not support pool namespace yet */ |
1760 | if (ci->i_pool_ns_len) | |
1761 | return -EIO; | |
1762 | ||
10183a69 YZ |
1763 | if (ceph_test_mount_opt(ceph_inode_to_client(&ci->vfs_inode), |
1764 | NOPOOLPERM)) | |
1765 | return 0; | |
1766 | ||
1767 | spin_lock(&ci->i_ceph_lock); | |
1768 | flags = ci->i_ceph_flags; | |
1769 | pool = ceph_file_layout_pg_pool(ci->i_layout); | |
1770 | spin_unlock(&ci->i_ceph_lock); | |
1771 | check: | |
1772 | if (flags & CEPH_I_POOL_PERM) { | |
1773 | if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) { | |
1774 | dout("ceph_pool_perm_check pool %u no read perm\n", | |
1775 | pool); | |
1776 | return -EPERM; | |
1777 | } | |
1778 | if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) { | |
1779 | dout("ceph_pool_perm_check pool %u no write perm\n", | |
1780 | pool); | |
1781 | return -EPERM; | |
1782 | } | |
1783 | return 0; | |
1784 | } | |
1785 | ||
1786 | ret = __ceph_pool_perm_get(ci, pool); | |
1787 | if (ret < 0) | |
1788 | return ret; | |
1789 | ||
1790 | flags = CEPH_I_POOL_PERM; | |
1791 | if (ret & POOL_READ) | |
1792 | flags |= CEPH_I_POOL_RD; | |
1793 | if (ret & POOL_WRITE) | |
1794 | flags |= CEPH_I_POOL_WR; | |
1795 | ||
1796 | spin_lock(&ci->i_ceph_lock); | |
1797 | if (pool == ceph_file_layout_pg_pool(ci->i_layout)) { | |
1798 | ci->i_ceph_flags = flags; | |
1799 | } else { | |
1800 | pool = ceph_file_layout_pg_pool(ci->i_layout); | |
1801 | flags = ci->i_ceph_flags; | |
1802 | } | |
1803 | spin_unlock(&ci->i_ceph_lock); | |
1804 | goto check; | |
1805 | } | |
1806 | ||
1807 | void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc) | |
1808 | { | |
1809 | struct ceph_pool_perm *perm; | |
1810 | struct rb_node *n; | |
1811 | ||
1812 | while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) { | |
1813 | n = rb_first(&mdsc->pool_perm_tree); | |
1814 | perm = rb_entry(n, struct ceph_pool_perm, node); | |
1815 | rb_erase(n, &mdsc->pool_perm_tree); | |
1816 | kfree(perm); | |
1817 | } | |
1818 | } |