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