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