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