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