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