HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / fs / ceph / addr.c
blob22bae2b434e2cbff092af7ba7acb575fd5ea46fb
1 #include <linux/ceph/ceph_debug.h>
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 */
8 #include <linux/slab.h>
9 #include <linux/pagevec.h>
10 #include <linux/task_io_accounting_ops.h>
12 #include "super.h"
13 #include "mds_client.h"
14 #include "cache.h"
15 #include <linux/ceph/osd_client.h>
18 * Ceph address space ops.
20 * There are a few funny things going on here.
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.
27 * Similarly, struct ceph_inode_info maintains a set of counters to
28 * count dirty pages on the inode. In the absence of snapshots,
29 * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count.
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.
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.
49 * Invalidate and so forth must take care to ensure the dirty page
50 * accounting is preserved.
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))
58 static inline struct ceph_snap_context *page_snap_context(struct page *page)
60 if (PagePrivate(page))
61 return (void *)page->private;
62 return NULL;
66 * Dirty a page. Optimistically adjust accounting, on the assumption
67 * that we won't race with invalidate. If we do, readjust.
69 static int ceph_set_page_dirty(struct page *page)
71 struct address_space *mapping = page->mapping;
72 struct inode *inode;
73 struct ceph_inode_info *ci;
74 struct ceph_snap_context *snapc;
75 int ret;
77 if (unlikely(!mapping))
78 return !TestSetPageDirty(page);
80 if (PageDirty(page)) {
81 dout("%p set_page_dirty %p idx %lu -- already dirty\n",
82 mapping->host, page, page->index);
83 BUG_ON(!PagePrivate(page));
84 return 0;
87 inode = mapping->host;
88 ci = ceph_inode(inode);
90 /* dirty the head */
91 spin_lock(&ci->i_ceph_lock);
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;
105 if (ci->i_wrbuffer_ref == 0)
106 ihold(inode);
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);
114 spin_unlock(&ci->i_ceph_lock);
117 * Reference snap context in page->private. Also set
118 * PagePrivate so that we get invalidatepage callback.
120 BUG_ON(PagePrivate(page));
121 page->private = (unsigned long)snapc;
122 SetPagePrivate(page);
124 ret = __set_page_dirty_nobuffers(page);
125 WARN_ON(!PageLocked(page));
126 WARN_ON(!page->mapping);
128 return ret;
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.
136 static void ceph_invalidatepage(struct page *page, unsigned int offset,
137 unsigned int length)
139 struct inode *inode;
140 struct ceph_inode_info *ci;
141 struct ceph_snap_context *snapc = page_snap_context(page);
143 inode = page->mapping->host;
144 ci = ceph_inode(inode);
146 if (offset != 0 || length != PAGE_CACHE_SIZE) {
147 dout("%p invalidatepage %p idx %lu partial dirty page %u~%u\n",
148 inode, page, page->index, offset, length);
149 return;
152 ceph_invalidate_fscache_page(inode, page);
154 if (!PagePrivate(page))
155 return;
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.
162 if (!PageDirty(page))
163 pr_err("%p invalidatepage %p page not dirty\n", inode, page);
165 ClearPageChecked(page);
167 dout("%p invalidatepage %p idx %lu full dirty page\n",
168 inode, page, page->index);
170 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
171 ceph_put_snap_context(snapc);
172 page->private = 0;
173 ClearPagePrivate(page);
176 static int ceph_releasepage(struct page *page, gfp_t g)
178 struct inode *inode = page->mapping ? page->mapping->host : NULL;
179 dout("%p releasepage %p idx %lu\n", inode, page, page->index);
180 WARN_ON(PageDirty(page));
182 /* Can we release the page from the cache? */
183 if (!ceph_release_fscache_page(page, g))
184 return 0;
186 return !PagePrivate(page);
190 * read a single page, without unlocking it.
192 static int ceph_do_readpage(struct file *filp, struct page *page)
194 struct inode *inode = file_inode(filp);
195 struct ceph_inode_info *ci = ceph_inode(inode);
196 struct ceph_osd_client *osdc =
197 &ceph_inode_to_client(inode)->client->osdc;
198 int err = 0;
199 u64 off = page_offset(page);
200 u64 len = PAGE_CACHE_SIZE;
202 if (off >= i_size_read(inode)) {
203 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
204 SetPageUptodate(page);
205 return 0;
208 if (ci->i_inline_version != CEPH_INLINE_NONE) {
210 * Uptodate inline data should have been added
211 * into page cache while getting Fcr caps.
213 if (off == 0)
214 return -EINVAL;
215 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
216 SetPageUptodate(page);
217 return 0;
220 err = ceph_readpage_from_fscache(inode, page);
221 if (err == 0)
222 return -EINPROGRESS;
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,
227 off, &len,
228 ci->i_truncate_seq, ci->i_truncate_size,
229 &page, 1, 0);
230 if (err == -ENOENT)
231 err = 0;
232 if (err < 0) {
233 SetPageError(page);
234 ceph_fscache_readpage_cancel(inode, page);
235 goto out;
237 if (err < PAGE_CACHE_SIZE)
238 /* zero fill remainder of page */
239 zero_user_segment(page, err, PAGE_CACHE_SIZE);
240 else
241 flush_dcache_page(page);
243 SetPageUptodate(page);
244 ceph_readpage_to_fscache(inode, page);
246 out:
247 return err < 0 ? err : 0;
250 static int ceph_readpage(struct file *filp, struct page *page)
252 int r = ceph_do_readpage(filp, page);
253 if (r != -EINPROGRESS)
254 unlock_page(page);
255 else
256 r = 0;
257 return r;
261 * Finish an async read(ahead) op.
263 static void finish_read(struct ceph_osd_request *req, struct ceph_msg *msg)
265 struct inode *inode = req->r_inode;
266 struct ceph_osd_data *osd_data;
267 int rc = req->r_result;
268 int bytes = le32_to_cpu(msg->hdr.data_len);
269 int num_pages;
270 int i;
272 dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes);
274 /* unlock all pages, zeroing any data we didn't read */
275 osd_data = osd_req_op_extent_osd_data(req, 0);
276 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
277 num_pages = calc_pages_for((u64)osd_data->alignment,
278 (u64)osd_data->length);
279 for (i = 0; i < num_pages; i++) {
280 struct page *page = osd_data->pages[i];
282 if (rc < 0 && rc != ENOENT)
283 goto unlock;
284 if (bytes < (int)PAGE_CACHE_SIZE) {
285 /* zero (remainder of) page */
286 int s = bytes < 0 ? 0 : bytes;
287 zero_user_segment(page, s, PAGE_CACHE_SIZE);
289 dout("finish_read %p uptodate %p idx %lu\n", inode, page,
290 page->index);
291 flush_dcache_page(page);
292 SetPageUptodate(page);
293 ceph_readpage_to_fscache(inode, page);
294 unlock:
295 unlock_page(page);
296 page_cache_release(page);
297 bytes -= PAGE_CACHE_SIZE;
299 kfree(osd_data->pages);
302 static void ceph_unlock_page_vector(struct page **pages, int num_pages)
304 int i;
306 for (i = 0; i < num_pages; i++)
307 unlock_page(pages[i]);
311 * start an async read(ahead) operation. return nr_pages we submitted
312 * a read for on success, or negative error code.
314 static int start_read(struct inode *inode, struct list_head *page_list, int max)
316 struct ceph_osd_client *osdc =
317 &ceph_inode_to_client(inode)->client->osdc;
318 struct ceph_inode_info *ci = ceph_inode(inode);
319 struct page *page = list_entry(page_list->prev, struct page, lru);
320 struct ceph_vino vino;
321 struct ceph_osd_request *req;
322 u64 off;
323 u64 len;
324 int i;
325 struct page **pages;
326 pgoff_t next_index;
327 int nr_pages = 0;
328 int ret;
330 off = (u64) page_offset(page);
332 /* count pages */
333 next_index = page->index;
334 list_for_each_entry_reverse(page, page_list, lru) {
335 if (page->index != next_index)
336 break;
337 nr_pages++;
338 next_index++;
339 if (max && nr_pages == max)
340 break;
342 len = nr_pages << PAGE_CACHE_SHIFT;
343 dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages,
344 off, len);
345 vino = ceph_vino(inode);
346 req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len,
347 0, 1, CEPH_OSD_OP_READ,
348 CEPH_OSD_FLAG_READ, NULL,
349 ci->i_truncate_seq, ci->i_truncate_size,
350 false);
351 if (IS_ERR(req))
352 return PTR_ERR(req);
354 /* build page vector */
355 nr_pages = calc_pages_for(0, len);
356 pages = kmalloc(sizeof(*pages) * nr_pages, GFP_KERNEL);
357 ret = -ENOMEM;
358 if (!pages)
359 goto out;
360 for (i = 0; i < nr_pages; ++i) {
361 page = list_entry(page_list->prev, struct page, lru);
362 BUG_ON(PageLocked(page));
363 list_del(&page->lru);
365 dout("start_read %p adding %p idx %lu\n", inode, page,
366 page->index);
367 if (add_to_page_cache_lru(page, &inode->i_data, page->index,
368 GFP_KERNEL)) {
369 ceph_fscache_uncache_page(inode, page);
370 page_cache_release(page);
371 dout("start_read %p add_to_page_cache failed %p\n",
372 inode, page);
373 nr_pages = i;
374 goto out_pages;
376 pages[i] = page;
378 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false);
379 req->r_callback = finish_read;
380 req->r_inode = inode;
382 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
384 dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len);
385 ret = ceph_osdc_start_request(osdc, req, false);
386 if (ret < 0)
387 goto out_pages;
388 ceph_osdc_put_request(req);
389 return nr_pages;
391 out_pages:
392 ceph_unlock_page_vector(pages, nr_pages);
393 ceph_release_page_vector(pages, nr_pages);
394 out:
395 ceph_osdc_put_request(req);
396 return ret;
401 * Read multiple pages. Leave pages we don't read + unlock in page_list;
402 * the caller (VM) cleans them up.
404 static int ceph_readpages(struct file *file, struct address_space *mapping,
405 struct list_head *page_list, unsigned nr_pages)
407 struct inode *inode = file_inode(file);
408 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
409 int rc = 0;
410 int max = 0;
412 if (ceph_inode(inode)->i_inline_version != CEPH_INLINE_NONE)
413 return -EINVAL;
415 rc = ceph_readpages_from_fscache(mapping->host, mapping, page_list,
416 &nr_pages);
418 if (rc == 0)
419 goto out;
421 if (fsc->mount_options->rsize >= PAGE_CACHE_SIZE)
422 max = (fsc->mount_options->rsize + PAGE_CACHE_SIZE - 1)
423 >> PAGE_SHIFT;
425 dout("readpages %p file %p nr_pages %d max %d\n", inode,
426 file, nr_pages,
427 max);
428 while (!list_empty(page_list)) {
429 rc = start_read(inode, page_list, max);
430 if (rc < 0)
431 goto out;
432 BUG_ON(rc == 0);
434 out:
435 ceph_fscache_readpages_cancel(inode, page_list);
437 dout("readpages %p file %p ret %d\n", inode, file, rc);
438 return rc;
442 * Get ref for the oldest snapc for an inode with dirty data... that is, the
443 * only snap context we are allowed to write back.
445 static struct ceph_snap_context *get_oldest_context(struct inode *inode,
446 loff_t *snap_size)
448 struct ceph_inode_info *ci = ceph_inode(inode);
449 struct ceph_snap_context *snapc = NULL;
450 struct ceph_cap_snap *capsnap = NULL;
452 spin_lock(&ci->i_ceph_lock);
453 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
454 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
455 capsnap->context, capsnap->dirty_pages);
456 if (capsnap->dirty_pages) {
457 snapc = ceph_get_snap_context(capsnap->context);
458 if (snap_size)
459 *snap_size = capsnap->size;
460 break;
463 if (!snapc && ci->i_wrbuffer_ref_head) {
464 snapc = ceph_get_snap_context(ci->i_head_snapc);
465 dout(" head snapc %p has %d dirty pages\n",
466 snapc, ci->i_wrbuffer_ref_head);
468 spin_unlock(&ci->i_ceph_lock);
469 return snapc;
473 * Write a single page, but leave the page locked.
475 * If we get a write error, set the page error bit, but still adjust the
476 * dirty page accounting (i.e., page is no longer dirty).
478 static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
480 struct inode *inode;
481 struct ceph_inode_info *ci;
482 struct ceph_fs_client *fsc;
483 struct ceph_osd_client *osdc;
484 struct ceph_snap_context *snapc, *oldest;
485 loff_t page_off = page_offset(page);
486 loff_t snap_size = -1;
487 long writeback_stat;
488 u64 truncate_size;
489 u32 truncate_seq;
490 int err = 0, len = PAGE_CACHE_SIZE;
492 dout("writepage %p idx %lu\n", page, page->index);
494 if (!page->mapping || !page->mapping->host) {
495 dout("writepage %p - no mapping\n", page);
496 return -EFAULT;
498 inode = page->mapping->host;
499 ci = ceph_inode(inode);
500 fsc = ceph_inode_to_client(inode);
501 osdc = &fsc->client->osdc;
503 /* verify this is a writeable snap context */
504 snapc = page_snap_context(page);
505 if (snapc == NULL) {
506 dout("writepage %p page %p not dirty?\n", inode, page);
507 goto out;
509 oldest = get_oldest_context(inode, &snap_size);
510 if (snapc->seq > oldest->seq) {
511 dout("writepage %p page %p snapc %p not writeable - noop\n",
512 inode, page, snapc);
513 /* we should only noop if called by kswapd */
514 WARN_ON((current->flags & PF_MEMALLOC) == 0);
515 ceph_put_snap_context(oldest);
516 goto out;
518 ceph_put_snap_context(oldest);
520 spin_lock(&ci->i_ceph_lock);
521 truncate_seq = ci->i_truncate_seq;
522 truncate_size = ci->i_truncate_size;
523 if (snap_size == -1)
524 snap_size = i_size_read(inode);
525 spin_unlock(&ci->i_ceph_lock);
527 /* is this a partial page at end of file? */
528 if (page_off >= snap_size) {
529 dout("%p page eof %llu\n", page, snap_size);
530 goto out;
532 if (snap_size < page_off + len)
533 len = snap_size - page_off;
535 dout("writepage %p page %p index %lu on %llu~%u snapc %p\n",
536 inode, page, page->index, page_off, len, snapc);
538 writeback_stat = atomic_long_inc_return(&fsc->writeback_count);
539 if (writeback_stat >
540 CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
541 set_bdi_congested(&fsc->backing_dev_info, BLK_RW_ASYNC);
543 ceph_readpage_to_fscache(inode, page);
545 set_page_writeback(page);
546 err = ceph_osdc_writepages(osdc, ceph_vino(inode),
547 &ci->i_layout, snapc,
548 page_off, len,
549 truncate_seq, truncate_size,
550 &inode->i_mtime, &page, 1);
551 if (err < 0) {
552 dout("writepage setting page/mapping error %d %p\n", err, page);
553 SetPageError(page);
554 mapping_set_error(&inode->i_data, err);
555 if (wbc)
556 wbc->pages_skipped++;
557 } else {
558 dout("writepage cleaned page %p\n", page);
559 err = 0; /* vfs expects us to return 0 */
561 page->private = 0;
562 ClearPagePrivate(page);
563 end_page_writeback(page);
564 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
565 ceph_put_snap_context(snapc); /* page's reference */
566 out:
567 return err;
570 static int ceph_writepage(struct page *page, struct writeback_control *wbc)
572 int err;
573 struct inode *inode = page->mapping->host;
574 BUG_ON(!inode);
575 ihold(inode);
576 err = writepage_nounlock(page, wbc);
577 unlock_page(page);
578 iput(inode);
579 return err;
584 * lame release_pages helper. release_pages() isn't exported to
585 * modules.
587 static void ceph_release_pages(struct page **pages, int num)
589 struct pagevec pvec;
590 int i;
592 pagevec_init(&pvec, 0);
593 for (i = 0; i < num; i++) {
594 if (pagevec_add(&pvec, pages[i]) == 0)
595 pagevec_release(&pvec);
597 pagevec_release(&pvec);
601 * async writeback completion handler.
603 * If we get an error, set the mapping error bit, but not the individual
604 * page error bits.
606 static void writepages_finish(struct ceph_osd_request *req,
607 struct ceph_msg *msg)
609 struct inode *inode = req->r_inode;
610 struct ceph_inode_info *ci = ceph_inode(inode);
611 struct ceph_osd_data *osd_data;
612 unsigned wrote;
613 struct page *page;
614 int num_pages;
615 int i;
616 struct ceph_snap_context *snapc = req->r_snapc;
617 struct address_space *mapping = inode->i_mapping;
618 int rc = req->r_result;
619 u64 bytes = req->r_ops[0].extent.length;
620 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
621 long writeback_stat;
622 unsigned issued = ceph_caps_issued(ci);
624 osd_data = osd_req_op_extent_osd_data(req, 0);
625 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
626 num_pages = calc_pages_for((u64)osd_data->alignment,
627 (u64)osd_data->length);
628 if (rc >= 0) {
630 * Assume we wrote the pages we originally sent. The
631 * osd might reply with fewer pages if our writeback
632 * raced with a truncation and was adjusted at the osd,
633 * so don't believe the reply.
635 wrote = num_pages;
636 } else {
637 wrote = 0;
638 mapping_set_error(mapping, rc);
640 dout("writepages_finish %p rc %d bytes %llu wrote %d (pages)\n",
641 inode, rc, bytes, wrote);
643 /* clean all pages */
644 for (i = 0; i < num_pages; i++) {
645 page = osd_data->pages[i];
646 BUG_ON(!page);
647 WARN_ON(!PageUptodate(page));
649 writeback_stat =
650 atomic_long_dec_return(&fsc->writeback_count);
651 if (writeback_stat <
652 CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
653 clear_bdi_congested(&fsc->backing_dev_info,
654 BLK_RW_ASYNC);
656 ceph_put_snap_context(page_snap_context(page));
657 page->private = 0;
658 ClearPagePrivate(page);
659 dout("unlocking %d %p\n", i, page);
660 end_page_writeback(page);
663 * We lost the cache cap, need to truncate the page before
664 * it is unlocked, otherwise we'd truncate it later in the
665 * page truncation thread, possibly losing some data that
666 * raced its way in
668 if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0)
669 generic_error_remove_page(inode->i_mapping, page);
671 unlock_page(page);
673 dout("%p wrote+cleaned %d pages\n", inode, wrote);
674 ceph_put_wrbuffer_cap_refs(ci, num_pages, snapc);
676 ceph_release_pages(osd_data->pages, num_pages);
677 if (osd_data->pages_from_pool)
678 mempool_free(osd_data->pages,
679 ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool);
680 else
681 kfree(osd_data->pages);
682 ceph_osdc_put_request(req);
686 * initiate async writeback
688 static int ceph_writepages_start(struct address_space *mapping,
689 struct writeback_control *wbc)
691 struct inode *inode = mapping->host;
692 struct ceph_inode_info *ci = ceph_inode(inode);
693 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
694 struct ceph_vino vino = ceph_vino(inode);
695 pgoff_t index, start, end;
696 int range_whole = 0;
697 int should_loop = 1;
698 pgoff_t max_pages = 0, max_pages_ever = 0;
699 struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
700 struct pagevec pvec;
701 int done = 0;
702 int rc = 0;
703 unsigned int wsize = i_blocksize(inode);
704 struct ceph_osd_request *req = NULL;
705 int do_sync = 0;
706 loff_t snap_size, i_size;
707 u64 truncate_size;
708 u32 truncate_seq;
711 * Include a 'sync' in the OSD request if this is a data
712 * integrity write (e.g., O_SYNC write or fsync()), or if our
713 * cap is being revoked.
715 if ((wbc->sync_mode == WB_SYNC_ALL) ||
716 ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER))
717 do_sync = 1;
718 dout("writepages_start %p dosync=%d (mode=%s)\n",
719 inode, do_sync,
720 wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
721 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
723 if (ACCESS_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
724 pr_warn("writepage_start %p on forced umount\n", inode);
725 truncate_pagecache(inode, 0);
726 mapping_set_error(mapping, -EIO);
727 return -EIO; /* we're in a forced umount, don't write! */
729 if (fsc->mount_options->wsize && fsc->mount_options->wsize < wsize)
730 wsize = fsc->mount_options->wsize;
731 if (wsize < PAGE_CACHE_SIZE)
732 wsize = PAGE_CACHE_SIZE;
733 max_pages_ever = wsize >> PAGE_CACHE_SHIFT;
735 pagevec_init(&pvec, 0);
737 /* where to start/end? */
738 if (wbc->range_cyclic) {
739 start = mapping->writeback_index; /* Start from prev offset */
740 end = -1;
741 dout(" cyclic, start at %lu\n", start);
742 } else {
743 start = wbc->range_start >> PAGE_CACHE_SHIFT;
744 end = wbc->range_end >> PAGE_CACHE_SHIFT;
745 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
746 range_whole = 1;
747 should_loop = 0;
748 dout(" not cyclic, %lu to %lu\n", start, end);
750 index = start;
752 retry:
753 /* find oldest snap context with dirty data */
754 ceph_put_snap_context(snapc);
755 snap_size = -1;
756 snapc = get_oldest_context(inode, &snap_size);
757 if (!snapc) {
758 /* hmm, why does writepages get called when there
759 is no dirty data? */
760 dout(" no snap context with dirty data?\n");
761 goto out;
763 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
764 snapc, snapc->seq, snapc->num_snaps);
766 spin_lock(&ci->i_ceph_lock);
767 truncate_seq = ci->i_truncate_seq;
768 truncate_size = ci->i_truncate_size;
769 i_size = i_size_read(inode);
770 spin_unlock(&ci->i_ceph_lock);
772 if (last_snapc && snapc != last_snapc) {
773 /* if we switched to a newer snapc, restart our scan at the
774 * start of the original file range. */
775 dout(" snapc differs from last pass, restarting at %lu\n",
776 index);
777 index = start;
779 last_snapc = snapc;
781 while (!done && index <= end) {
782 unsigned i;
783 int first;
784 pgoff_t next;
785 int pvec_pages, locked_pages;
786 struct page **pages = NULL;
787 mempool_t *pool = NULL; /* Becomes non-null if mempool used */
788 struct page *page;
789 int want;
790 u64 offset, len;
791 long writeback_stat;
793 next = 0;
794 locked_pages = 0;
795 max_pages = max_pages_ever;
797 get_more_pages:
798 first = -1;
799 want = min(end - index,
800 min((pgoff_t)PAGEVEC_SIZE,
801 max_pages - (pgoff_t)locked_pages) - 1)
802 + 1;
803 pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index,
804 PAGECACHE_TAG_DIRTY,
805 want);
806 dout("pagevec_lookup_tag got %d\n", pvec_pages);
807 if (!pvec_pages && !locked_pages)
808 break;
809 for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
810 page = pvec.pages[i];
811 dout("? %p idx %lu\n", page, page->index);
812 if (locked_pages == 0)
813 lock_page(page); /* first page */
814 else if (!trylock_page(page))
815 break;
817 /* only dirty pages, or our accounting breaks */
818 if (unlikely(!PageDirty(page)) ||
819 unlikely(page->mapping != mapping)) {
820 dout("!dirty or !mapping %p\n", page);
821 unlock_page(page);
822 break;
824 if (!wbc->range_cyclic && page->index > end) {
825 dout("end of range %p\n", page);
826 done = 1;
827 unlock_page(page);
828 break;
830 if (next && (page->index != next)) {
831 dout("not consecutive %p\n", page);
832 unlock_page(page);
833 break;
835 if (wbc->sync_mode != WB_SYNC_NONE) {
836 dout("waiting on writeback %p\n", page);
837 wait_on_page_writeback(page);
839 if (page_offset(page) >=
840 (snap_size == -1 ? i_size : snap_size)) {
841 dout("%p page eof %llu\n", page,
842 (snap_size == -1 ? i_size : snap_size));
843 done = 1;
844 unlock_page(page);
845 break;
847 if (PageWriteback(page)) {
848 dout("%p under writeback\n", page);
849 unlock_page(page);
850 break;
853 /* only if matching snap context */
854 pgsnapc = page_snap_context(page);
855 if (pgsnapc->seq > snapc->seq) {
856 dout("page snapc %p %lld > oldest %p %lld\n",
857 pgsnapc, pgsnapc->seq, snapc, snapc->seq);
858 unlock_page(page);
859 if (!locked_pages)
860 continue; /* keep looking for snap */
861 break;
864 if (!clear_page_dirty_for_io(page)) {
865 dout("%p !clear_page_dirty_for_io\n", page);
866 unlock_page(page);
867 break;
871 * We have something to write. If this is
872 * the first locked page this time through,
873 * allocate an osd request and a page array
874 * that it will use.
876 if (locked_pages == 0) {
877 BUG_ON(pages);
878 /* prepare async write request */
879 offset = (u64)page_offset(page);
880 len = wsize;
881 req = ceph_osdc_new_request(&fsc->client->osdc,
882 &ci->i_layout, vino,
883 offset, &len, 0,
884 do_sync ? 2 : 1,
885 CEPH_OSD_OP_WRITE,
886 CEPH_OSD_FLAG_WRITE |
887 CEPH_OSD_FLAG_ONDISK,
888 snapc, truncate_seq,
889 truncate_size, true);
890 if (IS_ERR(req)) {
891 rc = PTR_ERR(req);
892 unlock_page(page);
893 break;
896 if (do_sync)
897 osd_req_op_init(req, 1,
898 CEPH_OSD_OP_STARTSYNC, 0);
900 req->r_callback = writepages_finish;
901 req->r_inode = inode;
903 max_pages = calc_pages_for(0, (u64)len);
904 pages = kmalloc(max_pages * sizeof (*pages),
905 GFP_NOFS);
906 if (!pages) {
907 pool = fsc->wb_pagevec_pool;
908 pages = mempool_alloc(pool, GFP_NOFS);
909 BUG_ON(!pages);
913 /* note position of first page in pvec */
914 if (first < 0)
915 first = i;
916 dout("%p will write page %p idx %lu\n",
917 inode, page, page->index);
919 writeback_stat =
920 atomic_long_inc_return(&fsc->writeback_count);
921 if (writeback_stat > CONGESTION_ON_THRESH(
922 fsc->mount_options->congestion_kb)) {
923 set_bdi_congested(&fsc->backing_dev_info,
924 BLK_RW_ASYNC);
927 set_page_writeback(page);
928 pages[locked_pages] = page;
929 locked_pages++;
930 next = page->index + 1;
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);
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;
947 /* shift unused pages over in the pvec... we
948 * will need to release them below. */
949 for (j = i; j < pvec_pages; j++) {
950 dout(" pvec leftover page %p\n",
951 pvec.pages[j]);
952 pvec.pages[j-i+first] = pvec.pages[j];
954 pvec.nr -= i-first;
957 /* Format the osd request message and submit the write */
958 offset = page_offset(pages[0]);
959 len = (u64)locked_pages << PAGE_CACHE_SHIFT;
960 if (snap_size == -1) {
961 len = min(len, (u64)i_size_read(inode) - offset);
962 /* writepages_finish() clears writeback pages
963 * according to the data length, so make sure
964 * data length covers all locked pages */
965 len = max(len, 1 +
966 ((u64)(locked_pages - 1) << PAGE_CACHE_SHIFT));
967 } else {
968 len = min(len, snap_size - offset);
970 dout("writepages got %d pages at %llu~%llu\n",
971 locked_pages, offset, len);
973 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0,
974 !!pool, false);
976 pages = NULL; /* request message now owns the pages array */
977 pool = NULL;
979 /* Update the write op length in case we changed it */
981 osd_req_op_extent_update(req, 0, len);
983 vino = ceph_vino(inode);
984 ceph_osdc_build_request(req, offset, snapc, vino.snap,
985 &inode->i_mtime);
987 rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
988 BUG_ON(rc);
989 req = NULL;
991 /* continue? */
992 index = next;
993 wbc->nr_to_write -= locked_pages;
994 if (wbc->nr_to_write <= 0)
995 done = 1;
997 release_pvec_pages:
998 dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
999 pvec.nr ? pvec.pages[0] : NULL);
1000 pagevec_release(&pvec);
1002 if (locked_pages && !done)
1003 goto retry;
1006 if (should_loop && !done) {
1007 /* more to do; loop back to beginning of file */
1008 dout("writepages looping back to beginning of file\n");
1009 should_loop = 0;
1010 index = 0;
1011 goto retry;
1014 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1015 mapping->writeback_index = index;
1017 out:
1018 if (req)
1019 ceph_osdc_put_request(req);
1020 ceph_put_snap_context(snapc);
1021 dout("writepages done, rc = %d\n", rc);
1022 return rc;
1028 * See if a given @snapc is either writeable, or already written.
1030 static int context_is_writeable_or_written(struct inode *inode,
1031 struct ceph_snap_context *snapc)
1033 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL);
1034 int ret = !oldest || snapc->seq <= oldest->seq;
1036 ceph_put_snap_context(oldest);
1037 return ret;
1041 * We are only allowed to write into/dirty the page if the page is
1042 * clean, or already dirty within the same snap context.
1044 * called with page locked.
1045 * return success with page locked,
1046 * or any failure (incl -EAGAIN) with page unlocked.
1048 static int ceph_update_writeable_page(struct file *file,
1049 loff_t pos, unsigned len,
1050 struct page *page)
1052 struct inode *inode = file_inode(file);
1053 struct ceph_inode_info *ci = ceph_inode(inode);
1054 loff_t page_off = pos & PAGE_CACHE_MASK;
1055 int pos_in_page = pos & ~PAGE_CACHE_MASK;
1056 int end_in_page = pos_in_page + len;
1057 loff_t i_size;
1058 int r;
1059 struct ceph_snap_context *snapc, *oldest;
1061 retry_locked:
1062 /* writepages currently holds page lock, but if we change that later, */
1063 wait_on_page_writeback(page);
1065 snapc = page_snap_context(page);
1066 if (snapc && snapc != ci->i_head_snapc) {
1068 * this page is already dirty in another (older) snap
1069 * context! is it writeable now?
1071 oldest = get_oldest_context(inode, NULL);
1073 if (snapc->seq > oldest->seq) {
1074 ceph_put_snap_context(oldest);
1075 dout(" page %p snapc %p not current or oldest\n",
1076 page, snapc);
1078 * queue for writeback, and wait for snapc to
1079 * be writeable or written
1081 snapc = ceph_get_snap_context(snapc);
1082 unlock_page(page);
1083 ceph_queue_writeback(inode);
1084 r = wait_event_interruptible(ci->i_cap_wq,
1085 context_is_writeable_or_written(inode, snapc));
1086 ceph_put_snap_context(snapc);
1087 if (r == -ERESTARTSYS)
1088 return r;
1089 return -EAGAIN;
1091 ceph_put_snap_context(oldest);
1093 /* yay, writeable, do it now (without dropping page lock) */
1094 dout(" page %p snapc %p not current, but oldest\n",
1095 page, snapc);
1096 if (!clear_page_dirty_for_io(page))
1097 goto retry_locked;
1098 r = writepage_nounlock(page, NULL);
1099 if (r < 0)
1100 goto fail_unlock;
1101 goto retry_locked;
1104 if (PageUptodate(page)) {
1105 dout(" page %p already uptodate\n", page);
1106 return 0;
1109 /* full page? */
1110 if (pos_in_page == 0 && len == PAGE_CACHE_SIZE)
1111 return 0;
1113 /* past end of file? */
1114 i_size = inode->i_size; /* caller holds i_mutex */
1116 if (page_off >= i_size ||
1117 (pos_in_page == 0 && (pos+len) >= i_size &&
1118 end_in_page - pos_in_page != PAGE_CACHE_SIZE)) {
1119 dout(" zeroing %p 0 - %d and %d - %d\n",
1120 page, pos_in_page, end_in_page, (int)PAGE_CACHE_SIZE);
1121 zero_user_segments(page,
1122 0, pos_in_page,
1123 end_in_page, PAGE_CACHE_SIZE);
1124 return 0;
1127 /* we need to read it. */
1128 r = ceph_do_readpage(file, page);
1129 if (r < 0) {
1130 if (r == -EINPROGRESS)
1131 return -EAGAIN;
1132 goto fail_unlock;
1134 goto retry_locked;
1135 fail_unlock:
1136 unlock_page(page);
1137 return r;
1141 * We are only allowed to write into/dirty the page if the page is
1142 * clean, or already dirty within the same snap context.
1144 static int ceph_write_begin(struct file *file, struct address_space *mapping,
1145 loff_t pos, unsigned len, unsigned flags,
1146 struct page **pagep, void **fsdata)
1148 struct inode *inode = file_inode(file);
1149 struct page *page;
1150 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1151 int r;
1153 do {
1154 /* get a page */
1155 page = grab_cache_page_write_begin(mapping, index, 0);
1156 if (!page)
1157 return -ENOMEM;
1158 *pagep = page;
1160 dout("write_begin file %p inode %p page %p %d~%d\n", file,
1161 inode, page, (int)pos, (int)len);
1163 r = ceph_update_writeable_page(file, pos, len, page);
1164 if (r < 0)
1165 page_cache_release(page);
1166 else
1167 *pagep = page;
1168 } while (r == -EAGAIN);
1170 return r;
1174 * we don't do anything in here that simple_write_end doesn't do
1175 * except adjust dirty page accounting
1177 static int ceph_write_end(struct file *file, struct address_space *mapping,
1178 loff_t pos, unsigned len, unsigned copied,
1179 struct page *page, void *fsdata)
1181 struct inode *inode = file_inode(file);
1182 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
1183 int check_cap = 0;
1185 dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
1186 inode, page, (int)pos, (int)copied, (int)len);
1188 /* zero the stale part of the page if we did a short copy */
1189 if (copied < len)
1190 zero_user_segment(page, from+copied, len);
1192 /* did file size increase? */
1193 /* (no need for i_size_read(); we caller holds i_mutex */
1194 if (pos+copied > inode->i_size)
1195 check_cap = ceph_inode_set_size(inode, pos+copied);
1197 if (!PageUptodate(page))
1198 SetPageUptodate(page);
1200 set_page_dirty(page);
1202 unlock_page(page);
1203 page_cache_release(page);
1205 if (check_cap)
1206 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
1208 return copied;
1212 * we set .direct_IO to indicate direct io is supported, but since we
1213 * intercept O_DIRECT reads and writes early, this function should
1214 * never get called.
1216 static ssize_t ceph_direct_io(struct kiocb *iocb, struct iov_iter *iter,
1217 loff_t pos)
1219 WARN_ON(1);
1220 return -EINVAL;
1223 const struct address_space_operations ceph_aops = {
1224 .readpage = ceph_readpage,
1225 .readpages = ceph_readpages,
1226 .writepage = ceph_writepage,
1227 .writepages = ceph_writepages_start,
1228 .write_begin = ceph_write_begin,
1229 .write_end = ceph_write_end,
1230 .set_page_dirty = ceph_set_page_dirty,
1231 .invalidatepage = ceph_invalidatepage,
1232 .releasepage = ceph_releasepage,
1233 .direct_IO = ceph_direct_io,
1238 * vm ops
1240 static int ceph_filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1242 struct inode *inode = file_inode(vma->vm_file);
1243 struct ceph_inode_info *ci = ceph_inode(inode);
1244 struct ceph_file_info *fi = vma->vm_file->private_data;
1245 struct page *pinned_page = NULL;
1246 loff_t off = vmf->pgoff << PAGE_CACHE_SHIFT;
1247 int want, got, ret;
1249 dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n",
1250 inode, ceph_vinop(inode), off, (size_t)PAGE_CACHE_SIZE);
1251 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1252 want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1253 else
1254 want = CEPH_CAP_FILE_CACHE;
1255 while (1) {
1256 got = 0;
1257 ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want,
1258 -1, &got, &pinned_page);
1259 if (ret == 0)
1260 break;
1261 if (ret != -ERESTARTSYS) {
1262 WARN_ON(1);
1263 return VM_FAULT_SIGBUS;
1266 dout("filemap_fault %p %llu~%zd got cap refs on %s\n",
1267 inode, off, (size_t)PAGE_CACHE_SIZE, ceph_cap_string(got));
1269 if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
1270 ci->i_inline_version == CEPH_INLINE_NONE)
1271 ret = filemap_fault(vma, vmf);
1272 else
1273 ret = -EAGAIN;
1275 dout("filemap_fault %p %llu~%zd dropping cap refs on %s ret %d\n",
1276 inode, off, (size_t)PAGE_CACHE_SIZE, ceph_cap_string(got), ret);
1277 if (pinned_page)
1278 page_cache_release(pinned_page);
1279 ceph_put_cap_refs(ci, got);
1281 if (ret != -EAGAIN)
1282 return ret;
1284 /* read inline data */
1285 if (off >= PAGE_CACHE_SIZE) {
1286 /* does not support inline data > PAGE_SIZE */
1287 ret = VM_FAULT_SIGBUS;
1288 } else {
1289 int ret1;
1290 struct address_space *mapping = inode->i_mapping;
1291 struct page *page = find_or_create_page(mapping, 0,
1292 mapping_gfp_constraint(mapping,
1293 ~__GFP_FS));
1294 if (!page) {
1295 ret = VM_FAULT_OOM;
1296 goto out;
1298 ret1 = __ceph_do_getattr(inode, page,
1299 CEPH_STAT_CAP_INLINE_DATA, true);
1300 if (ret1 < 0 || off >= i_size_read(inode)) {
1301 unlock_page(page);
1302 page_cache_release(page);
1303 ret = VM_FAULT_SIGBUS;
1304 goto out;
1306 if (ret1 < PAGE_CACHE_SIZE)
1307 zero_user_segment(page, ret1, PAGE_CACHE_SIZE);
1308 else
1309 flush_dcache_page(page);
1310 SetPageUptodate(page);
1311 vmf->page = page;
1312 ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
1314 out:
1315 dout("filemap_fault %p %llu~%zd read inline data ret %d\n",
1316 inode, off, (size_t)PAGE_CACHE_SIZE, ret);
1317 return ret;
1321 * Reuse write_begin here for simplicity.
1323 static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1325 struct inode *inode = file_inode(vma->vm_file);
1326 struct ceph_inode_info *ci = ceph_inode(inode);
1327 struct ceph_file_info *fi = vma->vm_file->private_data;
1328 struct ceph_cap_flush *prealloc_cf;
1329 struct page *page = vmf->page;
1330 loff_t off = page_offset(page);
1331 loff_t size = i_size_read(inode);
1332 size_t len;
1333 int want, got, ret;
1335 prealloc_cf = ceph_alloc_cap_flush();
1336 if (!prealloc_cf)
1337 return VM_FAULT_SIGBUS;
1339 if (ci->i_inline_version != CEPH_INLINE_NONE) {
1340 struct page *locked_page = NULL;
1341 if (off == 0) {
1342 lock_page(page);
1343 locked_page = page;
1345 ret = ceph_uninline_data(vma->vm_file, locked_page);
1346 if (locked_page)
1347 unlock_page(locked_page);
1348 if (ret < 0) {
1349 ret = VM_FAULT_SIGBUS;
1350 goto out_free;
1354 if (off + PAGE_CACHE_SIZE <= size)
1355 len = PAGE_CACHE_SIZE;
1356 else
1357 len = size & ~PAGE_CACHE_MASK;
1359 dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
1360 inode, ceph_vinop(inode), off, len, size);
1361 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1362 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1363 else
1364 want = CEPH_CAP_FILE_BUFFER;
1365 while (1) {
1366 got = 0;
1367 ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, off + len,
1368 &got, NULL);
1369 if (ret == 0)
1370 break;
1371 if (ret != -ERESTARTSYS) {
1372 WARN_ON(1);
1373 ret = VM_FAULT_SIGBUS;
1374 goto out_free;
1377 dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
1378 inode, off, len, ceph_cap_string(got));
1380 /* Update time before taking page lock */
1381 file_update_time(vma->vm_file);
1383 lock_page(page);
1385 ret = VM_FAULT_NOPAGE;
1386 if ((off > size) ||
1387 (page->mapping != inode->i_mapping))
1388 goto out;
1390 ret = ceph_update_writeable_page(vma->vm_file, off, len, page);
1391 if (ret == 0) {
1392 /* success. we'll keep the page locked. */
1393 set_page_dirty(page);
1394 ret = VM_FAULT_LOCKED;
1395 } else {
1396 if (ret == -ENOMEM)
1397 ret = VM_FAULT_OOM;
1398 else
1399 ret = VM_FAULT_SIGBUS;
1401 out:
1402 if (ret != VM_FAULT_LOCKED)
1403 unlock_page(page);
1404 if (ret == VM_FAULT_LOCKED ||
1405 ci->i_inline_version != CEPH_INLINE_NONE) {
1406 int dirty;
1407 spin_lock(&ci->i_ceph_lock);
1408 ci->i_inline_version = CEPH_INLINE_NONE;
1409 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1410 &prealloc_cf);
1411 spin_unlock(&ci->i_ceph_lock);
1412 if (dirty)
1413 __mark_inode_dirty(inode, dirty);
1416 dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %d\n",
1417 inode, off, len, ceph_cap_string(got), ret);
1418 ceph_put_cap_refs(ci, got);
1419 out_free:
1420 ceph_free_cap_flush(prealloc_cf);
1422 return ret;
1425 void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
1426 char *data, size_t len)
1428 struct address_space *mapping = inode->i_mapping;
1429 struct page *page;
1431 if (locked_page) {
1432 page = locked_page;
1433 } else {
1434 if (i_size_read(inode) == 0)
1435 return;
1436 page = find_or_create_page(mapping, 0,
1437 mapping_gfp_constraint(mapping,
1438 ~__GFP_FS));
1439 if (!page)
1440 return;
1441 if (PageUptodate(page)) {
1442 unlock_page(page);
1443 page_cache_release(page);
1444 return;
1448 dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
1449 inode, ceph_vinop(inode), len, locked_page);
1451 if (len > 0) {
1452 void *kaddr = kmap_atomic(page);
1453 memcpy(kaddr, data, len);
1454 kunmap_atomic(kaddr);
1457 if (page != locked_page) {
1458 if (len < PAGE_CACHE_SIZE)
1459 zero_user_segment(page, len, PAGE_CACHE_SIZE);
1460 else
1461 flush_dcache_page(page);
1463 SetPageUptodate(page);
1464 unlock_page(page);
1465 page_cache_release(page);
1469 int ceph_uninline_data(struct file *filp, struct page *locked_page)
1471 struct inode *inode = file_inode(filp);
1472 struct ceph_inode_info *ci = ceph_inode(inode);
1473 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1474 struct ceph_osd_request *req;
1475 struct page *page = NULL;
1476 u64 len, inline_version;
1477 int err = 0;
1478 bool from_pagecache = false;
1480 spin_lock(&ci->i_ceph_lock);
1481 inline_version = ci->i_inline_version;
1482 spin_unlock(&ci->i_ceph_lock);
1484 dout("uninline_data %p %llx.%llx inline_version %llu\n",
1485 inode, ceph_vinop(inode), inline_version);
1487 if (inline_version == 1 || /* initial version, no data */
1488 inline_version == CEPH_INLINE_NONE)
1489 goto out;
1491 if (locked_page) {
1492 page = locked_page;
1493 WARN_ON(!PageUptodate(page));
1494 } else if (ceph_caps_issued(ci) &
1495 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) {
1496 page = find_get_page(inode->i_mapping, 0);
1497 if (page) {
1498 if (PageUptodate(page)) {
1499 from_pagecache = true;
1500 lock_page(page);
1501 } else {
1502 page_cache_release(page);
1503 page = NULL;
1508 if (page) {
1509 len = i_size_read(inode);
1510 if (len > PAGE_CACHE_SIZE)
1511 len = PAGE_CACHE_SIZE;
1512 } else {
1513 page = __page_cache_alloc(GFP_NOFS);
1514 if (!page) {
1515 err = -ENOMEM;
1516 goto out;
1518 err = __ceph_do_getattr(inode, page,
1519 CEPH_STAT_CAP_INLINE_DATA, true);
1520 if (err < 0) {
1521 /* no inline data */
1522 if (err == -ENODATA)
1523 err = 0;
1524 goto out;
1526 len = err;
1529 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1530 ceph_vino(inode), 0, &len, 0, 1,
1531 CEPH_OSD_OP_CREATE,
1532 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
1533 ceph_empty_snapc, 0, 0, false);
1534 if (IS_ERR(req)) {
1535 err = PTR_ERR(req);
1536 goto out;
1539 ceph_osdc_build_request(req, 0, NULL, CEPH_NOSNAP, &inode->i_mtime);
1540 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1541 if (!err)
1542 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1543 ceph_osdc_put_request(req);
1544 if (err < 0)
1545 goto out;
1547 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1548 ceph_vino(inode), 0, &len, 1, 3,
1549 CEPH_OSD_OP_WRITE,
1550 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
1551 ceph_empty_snapc,
1552 ci->i_truncate_seq, ci->i_truncate_size,
1553 false);
1554 if (IS_ERR(req)) {
1555 err = PTR_ERR(req);
1556 goto out;
1559 osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false);
1562 __le64 xattr_buf = cpu_to_le64(inline_version);
1563 err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
1564 "inline_version", &xattr_buf,
1565 sizeof(xattr_buf),
1566 CEPH_OSD_CMPXATTR_OP_GT,
1567 CEPH_OSD_CMPXATTR_MODE_U64);
1568 if (err)
1569 goto out_put;
1573 char xattr_buf[32];
1574 int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
1575 "%llu", inline_version);
1576 err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
1577 "inline_version",
1578 xattr_buf, xattr_len, 0, 0);
1579 if (err)
1580 goto out_put;
1583 ceph_osdc_build_request(req, 0, NULL, CEPH_NOSNAP, &inode->i_mtime);
1584 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1585 if (!err)
1586 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1587 out_put:
1588 ceph_osdc_put_request(req);
1589 if (err == -ECANCELED)
1590 err = 0;
1591 out:
1592 if (page && page != locked_page) {
1593 if (from_pagecache) {
1594 unlock_page(page);
1595 page_cache_release(page);
1596 } else
1597 __free_pages(page, 0);
1600 dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
1601 inode, ceph_vinop(inode), inline_version, err);
1602 return err;
1605 static const struct vm_operations_struct ceph_vmops = {
1606 .fault = ceph_filemap_fault,
1607 .page_mkwrite = ceph_page_mkwrite,
1610 int ceph_mmap(struct file *file, struct vm_area_struct *vma)
1612 struct address_space *mapping = file->f_mapping;
1614 if (!mapping->a_ops->readpage)
1615 return -ENOEXEC;
1616 file_accessed(file);
1617 vma->vm_ops = &ceph_vmops;
1618 return 0;
1621 enum {
1622 POOL_READ = 1,
1623 POOL_WRITE = 2,
1626 static int __ceph_pool_perm_get(struct ceph_inode_info *ci, u32 pool)
1628 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1629 struct ceph_mds_client *mdsc = fsc->mdsc;
1630 struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
1631 struct rb_node **p, *parent;
1632 struct ceph_pool_perm *perm;
1633 struct page **pages;
1634 int err = 0, err2 = 0, have = 0;
1636 down_read(&mdsc->pool_perm_rwsem);
1637 p = &mdsc->pool_perm_tree.rb_node;
1638 while (*p) {
1639 perm = rb_entry(*p, struct ceph_pool_perm, node);
1640 if (pool < perm->pool)
1641 p = &(*p)->rb_left;
1642 else if (pool > perm->pool)
1643 p = &(*p)->rb_right;
1644 else {
1645 have = perm->perm;
1646 break;
1649 up_read(&mdsc->pool_perm_rwsem);
1650 if (*p)
1651 goto out;
1653 dout("__ceph_pool_perm_get pool %u no perm cached\n", pool);
1655 down_write(&mdsc->pool_perm_rwsem);
1656 parent = NULL;
1657 while (*p) {
1658 parent = *p;
1659 perm = rb_entry(parent, struct ceph_pool_perm, node);
1660 if (pool < perm->pool)
1661 p = &(*p)->rb_left;
1662 else if (pool > perm->pool)
1663 p = &(*p)->rb_right;
1664 else {
1665 have = perm->perm;
1666 break;
1669 if (*p) {
1670 up_write(&mdsc->pool_perm_rwsem);
1671 goto out;
1674 rd_req = ceph_osdc_alloc_request(&fsc->client->osdc,
1675 ceph_empty_snapc,
1676 1, false, GFP_NOFS);
1677 if (!rd_req) {
1678 err = -ENOMEM;
1679 goto out_unlock;
1682 rd_req->r_flags = CEPH_OSD_FLAG_READ;
1683 osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
1684 rd_req->r_base_oloc.pool = pool;
1685 snprintf(rd_req->r_base_oid.name, sizeof(rd_req->r_base_oid.name),
1686 "%llx.00000000", ci->i_vino.ino);
1687 rd_req->r_base_oid.name_len = strlen(rd_req->r_base_oid.name);
1689 wr_req = ceph_osdc_alloc_request(&fsc->client->osdc,
1690 ceph_empty_snapc,
1691 1, false, GFP_NOFS);
1692 if (!wr_req) {
1693 err = -ENOMEM;
1694 goto out_unlock;
1697 wr_req->r_flags = CEPH_OSD_FLAG_WRITE |
1698 CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK;
1699 osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
1700 wr_req->r_base_oloc.pool = pool;
1701 wr_req->r_base_oid = rd_req->r_base_oid;
1703 /* one page should be large enough for STAT data */
1704 pages = ceph_alloc_page_vector(1, GFP_KERNEL);
1705 if (IS_ERR(pages)) {
1706 err = PTR_ERR(pages);
1707 goto out_unlock;
1710 osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
1711 0, false, true);
1712 ceph_osdc_build_request(rd_req, 0, NULL, CEPH_NOSNAP,
1713 &ci->vfs_inode.i_mtime);
1714 err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false);
1716 ceph_osdc_build_request(wr_req, 0, NULL, CEPH_NOSNAP,
1717 &ci->vfs_inode.i_mtime);
1718 err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false);
1720 if (!err)
1721 err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
1722 if (!err2)
1723 err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
1725 if (err >= 0 || err == -ENOENT)
1726 have |= POOL_READ;
1727 else if (err != -EPERM)
1728 goto out_unlock;
1730 if (err2 == 0 || err2 == -EEXIST)
1731 have |= POOL_WRITE;
1732 else if (err2 != -EPERM) {
1733 err = err2;
1734 goto out_unlock;
1737 perm = kmalloc(sizeof(*perm), GFP_NOFS);
1738 if (!perm) {
1739 err = -ENOMEM;
1740 goto out_unlock;
1743 perm->pool = pool;
1744 perm->perm = have;
1745 rb_link_node(&perm->node, parent, p);
1746 rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
1747 err = 0;
1748 out_unlock:
1749 up_write(&mdsc->pool_perm_rwsem);
1751 if (rd_req)
1752 ceph_osdc_put_request(rd_req);
1753 if (wr_req)
1754 ceph_osdc_put_request(wr_req);
1755 out:
1756 if (!err)
1757 err = have;
1758 dout("__ceph_pool_perm_get pool %u result = %d\n", pool, err);
1759 return err;
1762 int ceph_pool_perm_check(struct ceph_inode_info *ci, int need)
1764 u32 pool;
1765 int ret, flags;
1767 if (ceph_test_mount_opt(ceph_inode_to_client(&ci->vfs_inode),
1768 NOPOOLPERM))
1769 return 0;
1771 spin_lock(&ci->i_ceph_lock);
1772 flags = ci->i_ceph_flags;
1773 pool = ceph_file_layout_pg_pool(ci->i_layout);
1774 spin_unlock(&ci->i_ceph_lock);
1775 check:
1776 if (flags & CEPH_I_POOL_PERM) {
1777 if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
1778 dout("ceph_pool_perm_check pool %u no read perm\n",
1779 pool);
1780 return -EPERM;
1782 if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
1783 dout("ceph_pool_perm_check pool %u no write perm\n",
1784 pool);
1785 return -EPERM;
1787 return 0;
1790 ret = __ceph_pool_perm_get(ci, pool);
1791 if (ret < 0)
1792 return ret;
1794 flags = CEPH_I_POOL_PERM;
1795 if (ret & POOL_READ)
1796 flags |= CEPH_I_POOL_RD;
1797 if (ret & POOL_WRITE)
1798 flags |= CEPH_I_POOL_WR;
1800 spin_lock(&ci->i_ceph_lock);
1801 if (pool == ceph_file_layout_pg_pool(ci->i_layout)) {
1802 ci->i_ceph_flags = flags;
1803 } else {
1804 pool = ceph_file_layout_pg_pool(ci->i_layout);
1805 flags = ci->i_ceph_flags;
1807 spin_unlock(&ci->i_ceph_lock);
1808 goto check;
1811 void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
1813 struct ceph_pool_perm *perm;
1814 struct rb_node *n;
1816 while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
1817 n = rb_first(&mdsc->pool_perm_tree);
1818 perm = rb_entry(n, struct ceph_pool_perm, node);
1819 rb_erase(n, &mdsc->pool_perm_tree);
1820 kfree(perm);