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