1 #include <linux/ceph/ceph_debug.h>
3 #include <linux/backing-dev.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>
11 #include <linux/signal.h>
14 #include "mds_client.h"
16 #include <linux/ceph/osd_client.h>
19 * Ceph address space ops.
21 * There are a few funny things going on here.
23 * The page->private field is used to reference a struct
24 * ceph_snap_context for _every_ dirty page. This indicates which
25 * snapshot the page was logically dirtied in, and thus which snap
26 * context needs to be associated with the osd write during writeback.
28 * Similarly, struct ceph_inode_info maintains a set of counters to
29 * count dirty pages on the inode. In the absence of snapshots,
30 * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count.
32 * When a snapshot is taken (that is, when the client receives
33 * notification that a snapshot was taken), each inode with caps and
34 * with dirty pages (dirty pages implies there is a cap) gets a new
35 * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending
36 * order, new snaps go to the tail). The i_wrbuffer_ref_head count is
37 * moved to capsnap->dirty. (Unless a sync write is currently in
38 * progress. In that case, the capsnap is said to be "pending", new
39 * writes cannot start, and the capsnap isn't "finalized" until the
40 * write completes (or fails) and a final size/mtime for the inode for
41 * that snap can be settled upon.) i_wrbuffer_ref_head is reset to 0.
43 * On writeback, we must submit writes to the osd IN SNAP ORDER. So,
44 * we look for the first capsnap in i_cap_snaps and write out pages in
45 * that snap context _only_. Then we move on to the next capsnap,
46 * eventually reaching the "live" or "head" context (i.e., pages that
47 * are not yet snapped) and are writing the most recently dirtied
50 * Invalidate and so forth must take care to ensure the dirty page
51 * accounting is preserved.
54 #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10))
55 #define CONGESTION_OFF_THRESH(congestion_kb) \
56 (CONGESTION_ON_THRESH(congestion_kb) - \
57 (CONGESTION_ON_THRESH(congestion_kb) >> 2))
59 static inline struct ceph_snap_context *page_snap_context(struct page *page)
61 if (PagePrivate(page))
62 return (void *)page->private;
67 * Dirty a page. Optimistically adjust accounting, on the assumption
68 * that we won't race with invalidate. If we do, readjust.
70 static int ceph_set_page_dirty(struct page *page)
72 struct address_space *mapping = page->mapping;
74 struct ceph_inode_info *ci;
75 struct ceph_snap_context *snapc;
78 if (unlikely(!mapping))
79 return !TestSetPageDirty(page);
81 if (PageDirty(page)) {
82 dout("%p set_page_dirty %p idx %lu -- already dirty\n",
83 mapping->host, page, page->index);
84 BUG_ON(!PagePrivate(page));
88 inode = mapping->host;
89 ci = ceph_inode(inode);
92 spin_lock(&ci->i_ceph_lock);
93 BUG_ON(ci->i_wr_ref == 0); // caller should hold Fw reference
94 if (__ceph_have_pending_cap_snap(ci)) {
95 struct ceph_cap_snap *capsnap =
96 list_last_entry(&ci->i_cap_snaps,
99 snapc = ceph_get_snap_context(capsnap->context);
100 capsnap->dirty_pages++;
102 BUG_ON(!ci->i_head_snapc);
103 snapc = ceph_get_snap_context(ci->i_head_snapc);
104 ++ci->i_wrbuffer_ref_head;
106 if (ci->i_wrbuffer_ref == 0)
108 ++ci->i_wrbuffer_ref;
109 dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d "
110 "snapc %p seq %lld (%d snaps)\n",
111 mapping->host, page, page->index,
112 ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1,
113 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
114 snapc, snapc->seq, snapc->num_snaps);
115 spin_unlock(&ci->i_ceph_lock);
118 * Reference snap context in page->private. Also set
119 * PagePrivate so that we get invalidatepage callback.
121 BUG_ON(PagePrivate(page));
122 page->private = (unsigned long)snapc;
123 SetPagePrivate(page);
125 ret = __set_page_dirty_nobuffers(page);
126 WARN_ON(!PageLocked(page));
127 WARN_ON(!page->mapping);
133 * If we are truncating the full page (i.e. offset == 0), adjust the
134 * dirty page counters appropriately. Only called if there is private
137 static void ceph_invalidatepage(struct page *page, unsigned int offset,
141 struct ceph_inode_info *ci;
142 struct ceph_snap_context *snapc = page_snap_context(page);
144 inode = page->mapping->host;
145 ci = ceph_inode(inode);
147 if (offset != 0 || length != PAGE_SIZE) {
148 dout("%p invalidatepage %p idx %lu partial dirty page %u~%u\n",
149 inode, page, page->index, offset, length);
153 ceph_invalidate_fscache_page(inode, page);
155 if (!PagePrivate(page))
159 * We can get non-dirty pages here due to races between
160 * set_page_dirty and truncate_complete_page; just spit out a
161 * warning, in case we end up with accounting problems later.
163 if (!PageDirty(page))
164 pr_err("%p invalidatepage %p page not dirty\n", inode, page);
166 ClearPageChecked(page);
168 dout("%p invalidatepage %p idx %lu full dirty page\n",
169 inode, page, page->index);
171 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
172 ceph_put_snap_context(snapc);
174 ClearPagePrivate(page);
177 static int ceph_releasepage(struct page *page, gfp_t g)
179 dout("%p releasepage %p idx %lu (%sdirty)\n", page->mapping->host,
180 page, page->index, PageDirty(page) ? "" : "not ");
182 /* Can we release the page from the cache? */
183 if (!ceph_release_fscache_page(page, g))
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;
199 u64 off = page_offset(page);
202 if (off >= i_size_read(inode)) {
203 zero_user_segment(page, 0, PAGE_SIZE);
204 SetPageUptodate(page);
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.
215 zero_user_segment(page, 0, PAGE_SIZE);
216 SetPageUptodate(page);
220 err = ceph_readpage_from_fscache(inode, page);
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,
228 ci->i_truncate_seq, ci->i_truncate_size,
234 ceph_fscache_readpage_cancel(inode, page);
238 /* zero fill remainder of page */
239 zero_user_segment(page, err, PAGE_SIZE);
241 flush_dcache_page(page);
243 SetPageUptodate(page);
244 ceph_readpage_to_fscache(inode, page);
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)
261 * Finish an async read(ahead) op.
263 static void finish_read(struct ceph_osd_request *req)
265 struct inode *inode = req->r_inode;
266 struct ceph_osd_data *osd_data;
267 int rc = req->r_result <= 0 ? req->r_result : 0;
268 int bytes = req->r_result >= 0 ? req->r_result : 0;
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 ceph_fscache_readpage_cancel(inode, page);
286 if (bytes < (int)PAGE_SIZE) {
287 /* zero (remainder of) page */
288 int s = bytes < 0 ? 0 : bytes;
289 zero_user_segment(page, s, PAGE_SIZE);
291 dout("finish_read %p uptodate %p idx %lu\n", inode, page,
293 flush_dcache_page(page);
294 SetPageUptodate(page);
295 ceph_readpage_to_fscache(inode, page);
301 kfree(osd_data->pages);
305 * start an async read(ahead) operation. return nr_pages we submitted
306 * a read for on success, or negative error code.
308 static int start_read(struct inode *inode, struct list_head *page_list, int max)
310 struct ceph_osd_client *osdc =
311 &ceph_inode_to_client(inode)->client->osdc;
312 struct ceph_inode_info *ci = ceph_inode(inode);
313 struct page *page = list_entry(page_list->prev, struct page, lru);
314 struct ceph_vino vino;
315 struct ceph_osd_request *req;
325 if (!current->journal_info) {
326 /* caller of readpages does not hold buffer and read caps
327 * (fadvise, madvise and readahead cases) */
328 int want = CEPH_CAP_FILE_CACHE;
329 ret = ceph_try_get_caps(ci, CEPH_CAP_FILE_RD, want, &got);
331 dout("start_read %p, error getting cap\n", inode);
332 } else if (!(got & want)) {
333 dout("start_read %p, no cache cap\n", inode);
338 ceph_put_cap_refs(ci, got);
339 while (!list_empty(page_list)) {
340 page = list_entry(page_list->prev,
342 list_del(&page->lru);
349 off = (u64) page_offset(page);
352 next_index = page->index;
353 list_for_each_entry_reverse(page, page_list, lru) {
354 if (page->index != next_index)
358 if (max && nr_pages == max)
361 len = nr_pages << PAGE_SHIFT;
362 dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages,
364 vino = ceph_vino(inode);
365 req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len,
366 0, 1, CEPH_OSD_OP_READ,
367 CEPH_OSD_FLAG_READ, NULL,
368 ci->i_truncate_seq, ci->i_truncate_size,
375 /* build page vector */
376 nr_pages = calc_pages_for(0, len);
377 pages = kmalloc(sizeof(*pages) * nr_pages, GFP_KERNEL);
382 for (i = 0; i < nr_pages; ++i) {
383 page = list_entry(page_list->prev, struct page, lru);
384 BUG_ON(PageLocked(page));
385 list_del(&page->lru);
387 dout("start_read %p adding %p idx %lu\n", inode, page,
389 if (add_to_page_cache_lru(page, &inode->i_data, page->index,
391 ceph_fscache_uncache_page(inode, page);
393 dout("start_read %p add_to_page_cache failed %p\n",
397 len = nr_pages << PAGE_SHIFT;
398 osd_req_op_extent_update(req, 0, len);
405 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false);
406 req->r_callback = finish_read;
407 req->r_inode = inode;
409 dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len);
410 ret = ceph_osdc_start_request(osdc, req, false);
413 ceph_osdc_put_request(req);
415 /* After adding locked pages to page cache, the inode holds cache cap.
416 * So we can drop our cap refs. */
418 ceph_put_cap_refs(ci, got);
423 for (i = 0; i < nr_pages; ++i) {
424 ceph_fscache_readpage_cancel(inode, pages[i]);
425 unlock_page(pages[i]);
427 ceph_put_page_vector(pages, nr_pages, false);
429 ceph_osdc_put_request(req);
432 ceph_put_cap_refs(ci, got);
438 * Read multiple pages. Leave pages we don't read + unlock in page_list;
439 * the caller (VM) cleans them up.
441 static int ceph_readpages(struct file *file, struct address_space *mapping,
442 struct list_head *page_list, unsigned nr_pages)
444 struct inode *inode = file_inode(file);
445 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
449 if (ceph_inode(inode)->i_inline_version != CEPH_INLINE_NONE)
452 rc = ceph_readpages_from_fscache(mapping->host, mapping, page_list,
458 if (fsc->mount_options->rsize >= PAGE_SIZE)
459 max = (fsc->mount_options->rsize + PAGE_SIZE - 1)
462 dout("readpages %p file %p nr_pages %d max %d\n", inode,
465 while (!list_empty(page_list)) {
466 rc = start_read(inode, page_list, max);
471 ceph_fscache_readpages_cancel(inode, page_list);
473 dout("readpages %p file %p ret %d\n", inode, file, rc);
478 * Get ref for the oldest snapc for an inode with dirty data... that is, the
479 * only snap context we are allowed to write back.
481 static struct ceph_snap_context *get_oldest_context(struct inode *inode,
486 struct ceph_inode_info *ci = ceph_inode(inode);
487 struct ceph_snap_context *snapc = NULL;
488 struct ceph_cap_snap *capsnap = NULL;
490 spin_lock(&ci->i_ceph_lock);
491 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
492 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
493 capsnap->context, capsnap->dirty_pages);
494 if (capsnap->dirty_pages) {
495 snapc = ceph_get_snap_context(capsnap->context);
497 *snap_size = capsnap->size;
499 *truncate_size = capsnap->truncate_size;
501 *truncate_seq = capsnap->truncate_seq;
505 if (!snapc && ci->i_wrbuffer_ref_head) {
506 snapc = ceph_get_snap_context(ci->i_head_snapc);
507 dout(" head snapc %p has %d dirty pages\n",
508 snapc, ci->i_wrbuffer_ref_head);
510 *truncate_size = ci->i_truncate_size;
512 *truncate_seq = ci->i_truncate_seq;
514 spin_unlock(&ci->i_ceph_lock);
519 * Write a single page, but leave the page locked.
521 * If we get a write error, set the page error bit, but still adjust the
522 * dirty page accounting (i.e., page is no longer dirty).
524 static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
527 struct ceph_inode_info *ci;
528 struct ceph_fs_client *fsc;
529 struct ceph_osd_client *osdc;
530 struct ceph_snap_context *snapc, *oldest;
531 loff_t page_off = page_offset(page);
532 loff_t snap_size = -1;
536 int err, len = PAGE_SIZE;
538 dout("writepage %p idx %lu\n", page, page->index);
540 inode = page->mapping->host;
541 ci = ceph_inode(inode);
542 fsc = ceph_inode_to_client(inode);
543 osdc = &fsc->client->osdc;
545 /* verify this is a writeable snap context */
546 snapc = page_snap_context(page);
548 dout("writepage %p page %p not dirty?\n", inode, page);
551 oldest = get_oldest_context(inode, &snap_size,
552 &truncate_size, &truncate_seq);
553 if (snapc->seq > oldest->seq) {
554 dout("writepage %p page %p snapc %p not writeable - noop\n",
556 /* we should only noop if called by kswapd */
557 WARN_ON(!(current->flags & PF_MEMALLOC));
558 ceph_put_snap_context(oldest);
559 redirty_page_for_writepage(wbc, page);
562 ceph_put_snap_context(oldest);
565 snap_size = i_size_read(inode);
567 /* is this a partial page at end of file? */
568 if (page_off >= snap_size) {
569 dout("%p page eof %llu\n", page, snap_size);
573 if (snap_size < page_off + len)
574 len = snap_size - page_off;
576 dout("writepage %p page %p index %lu on %llu~%u snapc %p\n",
577 inode, page, page->index, page_off, len, snapc);
579 writeback_stat = atomic_long_inc_return(&fsc->writeback_count);
581 CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
582 set_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
584 set_page_writeback(page);
585 err = ceph_osdc_writepages(osdc, ceph_vino(inode),
586 &ci->i_layout, snapc,
588 truncate_seq, truncate_size,
589 &inode->i_mtime, &page, 1);
591 struct writeback_control tmp_wbc;
594 if (err == -ERESTARTSYS) {
595 /* killed by SIGKILL */
596 dout("writepage interrupted page %p\n", page);
597 redirty_page_for_writepage(wbc, page);
598 end_page_writeback(page);
601 dout("writepage setting page/mapping error %d %p\n",
604 mapping_set_error(&inode->i_data, err);
605 wbc->pages_skipped++;
607 dout("writepage cleaned page %p\n", page);
608 err = 0; /* vfs expects us to return 0 */
611 ClearPagePrivate(page);
612 end_page_writeback(page);
613 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
614 ceph_put_snap_context(snapc); /* page's reference */
618 static int ceph_writepage(struct page *page, struct writeback_control *wbc)
621 struct inode *inode = page->mapping->host;
624 err = writepage_nounlock(page, wbc);
625 if (err == -ERESTARTSYS) {
626 /* direct memory reclaimer was killed by SIGKILL. return 0
627 * to prevent caller from setting mapping/page error */
636 * lame release_pages helper. release_pages() isn't exported to
639 static void ceph_release_pages(struct page **pages, int num)
644 pagevec_init(&pvec, 0);
645 for (i = 0; i < num; i++) {
646 if (pagevec_add(&pvec, pages[i]) == 0)
647 pagevec_release(&pvec);
649 pagevec_release(&pvec);
653 * async writeback completion handler.
655 * If we get an error, set the mapping error bit, but not the individual
658 static void writepages_finish(struct ceph_osd_request *req)
660 struct inode *inode = req->r_inode;
661 struct ceph_inode_info *ci = ceph_inode(inode);
662 struct ceph_osd_data *osd_data;
664 int num_pages, total_pages = 0;
666 int rc = req->r_result;
667 struct ceph_snap_context *snapc = req->r_snapc;
668 struct address_space *mapping = inode->i_mapping;
669 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
672 dout("writepages_finish %p rc %d\n", inode, rc);
674 mapping_set_error(mapping, rc);
675 ceph_set_error_write(ci);
677 ceph_clear_error_write(ci);
681 * We lost the cache cap, need to truncate the page before
682 * it is unlocked, otherwise we'd truncate it later in the
683 * page truncation thread, possibly losing some data that
686 remove_page = !(ceph_caps_issued(ci) &
687 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
689 /* clean all pages */
690 for (i = 0; i < req->r_num_ops; i++) {
691 if (req->r_ops[i].op != CEPH_OSD_OP_WRITE)
694 osd_data = osd_req_op_extent_osd_data(req, i);
695 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
696 num_pages = calc_pages_for((u64)osd_data->alignment,
697 (u64)osd_data->length);
698 total_pages += num_pages;
699 for (j = 0; j < num_pages; j++) {
700 page = osd_data->pages[j];
702 WARN_ON(!PageUptodate(page));
704 if (atomic_long_dec_return(&fsc->writeback_count) <
705 CONGESTION_OFF_THRESH(
706 fsc->mount_options->congestion_kb))
707 clear_bdi_congested(inode_to_bdi(inode),
710 ceph_put_snap_context(page_snap_context(page));
712 ClearPagePrivate(page);
713 dout("unlocking %p\n", page);
714 end_page_writeback(page);
717 generic_error_remove_page(inode->i_mapping,
722 dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n",
723 inode, osd_data->length, rc >= 0 ? num_pages : 0);
725 ceph_release_pages(osd_data->pages, num_pages);
728 ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
730 osd_data = osd_req_op_extent_osd_data(req, 0);
731 if (osd_data->pages_from_pool)
732 mempool_free(osd_data->pages,
733 ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool);
735 kfree(osd_data->pages);
736 ceph_osdc_put_request(req);
740 * initiate async writeback
742 static int ceph_writepages_start(struct address_space *mapping,
743 struct writeback_control *wbc)
745 struct inode *inode = mapping->host;
746 struct ceph_inode_info *ci = ceph_inode(inode);
747 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
748 struct ceph_vino vino = ceph_vino(inode);
749 pgoff_t index, start, end;
752 pgoff_t max_pages = 0, max_pages_ever = 0;
753 struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
757 unsigned int wsize = i_blocksize(inode);
758 struct ceph_osd_request *req = NULL;
760 loff_t snap_size, i_size;
765 * Include a 'sync' in the OSD request if this is a data
766 * integrity write (e.g., O_SYNC write or fsync()), or if our
767 * cap is being revoked.
769 if ((wbc->sync_mode == WB_SYNC_ALL) ||
770 ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER))
772 dout("writepages_start %p dosync=%d (mode=%s)\n",
774 wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
775 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
777 if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
778 if (ci->i_wrbuffer_ref > 0) {
780 "writepage_start %p %lld forced umount\n",
781 inode, ceph_ino(inode));
783 mapping_set_error(mapping, -EIO);
784 return -EIO; /* we're in a forced umount, don't write! */
786 if (fsc->mount_options->wsize && fsc->mount_options->wsize < wsize)
787 wsize = fsc->mount_options->wsize;
788 if (wsize < PAGE_SIZE)
790 max_pages_ever = wsize >> PAGE_SHIFT;
792 pagevec_init(&pvec, 0);
794 /* where to start/end? */
795 if (wbc->range_cyclic) {
796 start = mapping->writeback_index; /* Start from prev offset */
798 dout(" cyclic, start at %lu\n", start);
800 start = wbc->range_start >> PAGE_SHIFT;
801 end = wbc->range_end >> PAGE_SHIFT;
802 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
805 dout(" not cyclic, %lu to %lu\n", start, end);
810 /* find oldest snap context with dirty data */
811 ceph_put_snap_context(snapc);
813 snapc = get_oldest_context(inode, &snap_size,
814 &truncate_size, &truncate_seq);
816 /* hmm, why does writepages get called when there
818 dout(" no snap context with dirty data?\n");
821 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
822 snapc, snapc->seq, snapc->num_snaps);
824 i_size = i_size_read(inode);
826 if (last_snapc && snapc != last_snapc) {
827 /* if we switched to a newer snapc, restart our scan at the
828 * start of the original file range. */
829 dout(" snapc differs from last pass, restarting at %lu\n",
835 while (!done && index <= end) {
838 pgoff_t strip_unit_end = 0;
839 int num_ops = 0, op_idx;
840 int pvec_pages, locked_pages = 0;
841 struct page **pages = NULL, **data_pages;
842 mempool_t *pool = NULL; /* Becomes non-null if mempool used */
845 u64 offset = 0, len = 0;
847 max_pages = max_pages_ever;
851 want = min(end - index,
852 min((pgoff_t)PAGEVEC_SIZE,
853 max_pages - (pgoff_t)locked_pages) - 1)
855 pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index,
858 dout("pagevec_lookup_tag got %d\n", pvec_pages);
859 if (!pvec_pages && !locked_pages)
861 for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
862 page = pvec.pages[i];
863 dout("? %p idx %lu\n", page, page->index);
864 if (locked_pages == 0)
865 lock_page(page); /* first page */
866 else if (!trylock_page(page))
869 /* only dirty pages, or our accounting breaks */
870 if (unlikely(!PageDirty(page)) ||
871 unlikely(page->mapping != mapping)) {
872 dout("!dirty or !mapping %p\n", page);
876 if (!wbc->range_cyclic && page->index > end) {
877 dout("end of range %p\n", page);
882 if (strip_unit_end && (page->index > strip_unit_end)) {
883 dout("end of strip unit %p\n", page);
887 if (wbc->sync_mode != WB_SYNC_NONE) {
888 dout("waiting on writeback %p\n", page);
889 wait_on_page_writeback(page);
891 if (page_offset(page) >=
892 (snap_size == -1 ? i_size : snap_size)) {
893 dout("%p page eof %llu\n", page,
894 (snap_size == -1 ? i_size : snap_size));
899 if (PageWriteback(page)) {
900 dout("%p under writeback\n", page);
905 /* only if matching snap context */
906 pgsnapc = page_snap_context(page);
907 if (pgsnapc->seq > snapc->seq) {
908 dout("page snapc %p %lld > oldest %p %lld\n",
909 pgsnapc, pgsnapc->seq, snapc, snapc->seq);
912 continue; /* keep looking for snap */
916 if (!clear_page_dirty_for_io(page)) {
917 dout("%p !clear_page_dirty_for_io\n", page);
923 * We have something to write. If this is
924 * the first locked page this time through,
925 * calculate max possinle write size and
926 * allocate a page array
928 if (locked_pages == 0) {
932 /* prepare async write request */
933 offset = (u64)page_offset(page);
936 rc = ceph_calc_file_object_mapping(&ci->i_layout,
945 num_ops = 1 + do_sync;
946 strip_unit_end = page->index +
947 ((len - 1) >> PAGE_SHIFT);
950 max_pages = calc_pages_for(0, (u64)len);
951 pages = kmalloc(max_pages * sizeof (*pages),
954 pool = fsc->wb_pagevec_pool;
955 pages = mempool_alloc(pool, GFP_NOFS);
960 } else if (page->index !=
961 (offset + len) >> PAGE_SHIFT) {
962 if (num_ops >= (pool ? CEPH_OSD_SLAB_OPS :
964 redirty_page_for_writepage(wbc, page);
970 offset = (u64)page_offset(page);
974 /* note position of first page in pvec */
977 dout("%p will write page %p idx %lu\n",
978 inode, page, page->index);
980 if (atomic_long_inc_return(&fsc->writeback_count) >
981 CONGESTION_ON_THRESH(
982 fsc->mount_options->congestion_kb)) {
983 set_bdi_congested(inode_to_bdi(inode),
987 pages[locked_pages] = page;
992 /* did we get anything? */
994 goto release_pvec_pages;
997 BUG_ON(!locked_pages || first < 0);
999 if (pvec_pages && i == pvec_pages &&
1000 locked_pages < max_pages) {
1001 dout("reached end pvec, trying for more\n");
1002 pagevec_reinit(&pvec);
1003 goto get_more_pages;
1006 /* shift unused pages over in the pvec... we
1007 * will need to release them below. */
1008 for (j = i; j < pvec_pages; j++) {
1009 dout(" pvec leftover page %p\n", pvec.pages[j]);
1010 pvec.pages[j-i+first] = pvec.pages[j];
1016 offset = page_offset(pages[0]);
1019 req = ceph_osdc_new_request(&fsc->client->osdc,
1020 &ci->i_layout, vino,
1021 offset, &len, 0, num_ops,
1023 CEPH_OSD_FLAG_WRITE,
1024 snapc, truncate_seq,
1025 truncate_size, false);
1027 req = ceph_osdc_new_request(&fsc->client->osdc,
1028 &ci->i_layout, vino,
1033 CEPH_OSD_FLAG_WRITE,
1034 snapc, truncate_seq,
1035 truncate_size, true);
1036 BUG_ON(IS_ERR(req));
1038 BUG_ON(len < page_offset(pages[locked_pages - 1]) +
1039 PAGE_SIZE - offset);
1041 req->r_callback = writepages_finish;
1042 req->r_inode = inode;
1044 /* Format the osd request message and submit the write */
1048 for (i = 0; i < locked_pages; i++) {
1049 u64 cur_offset = page_offset(pages[i]);
1050 if (offset + len != cur_offset) {
1051 if (op_idx + do_sync + 1 == req->r_num_ops)
1053 osd_req_op_extent_dup_last(req, op_idx,
1054 cur_offset - offset);
1055 dout("writepages got pages at %llu~%llu\n",
1057 osd_req_op_extent_osd_data_pages(req, op_idx,
1060 osd_req_op_extent_update(req, op_idx, len);
1063 offset = cur_offset;
1064 data_pages = pages + i;
1068 set_page_writeback(pages[i]);
1072 if (snap_size != -1) {
1073 len = min(len, snap_size - offset);
1074 } else if (i == locked_pages) {
1075 /* writepages_finish() clears writeback pages
1076 * according to the data length, so make sure
1077 * data length covers all locked pages */
1078 u64 min_len = len + 1 - PAGE_SIZE;
1079 len = min(len, (u64)i_size_read(inode) - offset);
1080 len = max(len, min_len);
1082 dout("writepages got pages at %llu~%llu\n", offset, len);
1084 osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len,
1086 osd_req_op_extent_update(req, op_idx, len);
1090 osd_req_op_init(req, op_idx, CEPH_OSD_OP_STARTSYNC, 0);
1092 BUG_ON(op_idx + 1 != req->r_num_ops);
1095 if (i < locked_pages) {
1096 BUG_ON(num_ops <= req->r_num_ops);
1097 num_ops -= req->r_num_ops;
1101 /* allocate new pages array for next request */
1103 pages = kmalloc(locked_pages * sizeof (*pages),
1106 pool = fsc->wb_pagevec_pool;
1107 pages = mempool_alloc(pool, GFP_NOFS);
1110 memcpy(pages, data_pages + i,
1111 locked_pages * sizeof(*pages));
1112 memset(data_pages + i, 0,
1113 locked_pages * sizeof(*pages));
1115 BUG_ON(num_ops != req->r_num_ops);
1116 index = pages[i - 1]->index + 1;
1117 /* request message now owns the pages array */
1121 req->r_mtime = inode->i_mtime;
1122 rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
1126 wbc->nr_to_write -= i;
1130 if (wbc->nr_to_write <= 0)
1134 dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
1135 pvec.nr ? pvec.pages[0] : NULL);
1136 pagevec_release(&pvec);
1138 if (locked_pages && !done)
1142 if (should_loop && !done) {
1143 /* more to do; loop back to beginning of file */
1144 dout("writepages looping back to beginning of file\n");
1150 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1151 mapping->writeback_index = index;
1154 ceph_osdc_put_request(req);
1155 ceph_put_snap_context(snapc);
1156 dout("writepages done, rc = %d\n", rc);
1163 * See if a given @snapc is either writeable, or already written.
1165 static int context_is_writeable_or_written(struct inode *inode,
1166 struct ceph_snap_context *snapc)
1168 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL,
1170 int ret = !oldest || snapc->seq <= oldest->seq;
1172 ceph_put_snap_context(oldest);
1177 * We are only allowed to write into/dirty the page if the page is
1178 * clean, or already dirty within the same snap context.
1180 * called with page locked.
1181 * return success with page locked,
1182 * or any failure (incl -EAGAIN) with page unlocked.
1184 static int ceph_update_writeable_page(struct file *file,
1185 loff_t pos, unsigned len,
1188 struct inode *inode = file_inode(file);
1189 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1190 struct ceph_inode_info *ci = ceph_inode(inode);
1191 loff_t page_off = pos & PAGE_MASK;
1192 int pos_in_page = pos & ~PAGE_MASK;
1193 int end_in_page = pos_in_page + len;
1196 struct ceph_snap_context *snapc, *oldest;
1198 if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
1199 dout(" page %p forced umount\n", page);
1205 /* writepages currently holds page lock, but if we change that later, */
1206 wait_on_page_writeback(page);
1208 snapc = page_snap_context(page);
1209 if (snapc && snapc != ci->i_head_snapc) {
1211 * this page is already dirty in another (older) snap
1212 * context! is it writeable now?
1214 oldest = get_oldest_context(inode, NULL, NULL, NULL);
1216 if (snapc->seq > oldest->seq) {
1217 ceph_put_snap_context(oldest);
1218 dout(" page %p snapc %p not current or oldest\n",
1221 * queue for writeback, and wait for snapc to
1222 * be writeable or written
1224 snapc = ceph_get_snap_context(snapc);
1226 ceph_queue_writeback(inode);
1227 r = wait_event_killable(ci->i_cap_wq,
1228 context_is_writeable_or_written(inode, snapc));
1229 ceph_put_snap_context(snapc);
1230 if (r == -ERESTARTSYS)
1234 ceph_put_snap_context(oldest);
1236 /* yay, writeable, do it now (without dropping page lock) */
1237 dout(" page %p snapc %p not current, but oldest\n",
1239 if (!clear_page_dirty_for_io(page))
1241 r = writepage_nounlock(page, NULL);
1247 if (PageUptodate(page)) {
1248 dout(" page %p already uptodate\n", page);
1253 if (pos_in_page == 0 && len == PAGE_SIZE)
1256 /* past end of file? */
1257 i_size = i_size_read(inode);
1259 if (page_off >= i_size ||
1260 (pos_in_page == 0 && (pos+len) >= i_size &&
1261 end_in_page - pos_in_page != PAGE_SIZE)) {
1262 dout(" zeroing %p 0 - %d and %d - %d\n",
1263 page, pos_in_page, end_in_page, (int)PAGE_SIZE);
1264 zero_user_segments(page,
1266 end_in_page, PAGE_SIZE);
1270 /* we need to read it. */
1271 r = ceph_do_readpage(file, page);
1273 if (r == -EINPROGRESS)
1284 * We are only allowed to write into/dirty the page if the page is
1285 * clean, or already dirty within the same snap context.
1287 static int ceph_write_begin(struct file *file, struct address_space *mapping,
1288 loff_t pos, unsigned len, unsigned flags,
1289 struct page **pagep, void **fsdata)
1291 struct inode *inode = file_inode(file);
1293 pgoff_t index = pos >> PAGE_SHIFT;
1298 page = grab_cache_page_write_begin(mapping, index, 0);
1302 dout("write_begin file %p inode %p page %p %d~%d\n", file,
1303 inode, page, (int)pos, (int)len);
1305 r = ceph_update_writeable_page(file, pos, len, page);
1310 } while (r == -EAGAIN);
1316 * we don't do anything in here that simple_write_end doesn't do
1317 * except adjust dirty page accounting
1319 static int ceph_write_end(struct file *file, struct address_space *mapping,
1320 loff_t pos, unsigned len, unsigned copied,
1321 struct page *page, void *fsdata)
1323 struct inode *inode = file_inode(file);
1324 bool check_cap = false;
1326 dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
1327 inode, page, (int)pos, (int)copied, (int)len);
1329 /* zero the stale part of the page if we did a short copy */
1330 if (!PageUptodate(page)) {
1335 SetPageUptodate(page);
1338 /* did file size increase? */
1339 if (pos+copied > i_size_read(inode))
1340 check_cap = ceph_inode_set_size(inode, pos+copied);
1342 set_page_dirty(page);
1349 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
1355 * we set .direct_IO to indicate direct io is supported, but since we
1356 * intercept O_DIRECT reads and writes early, this function should
1359 static ssize_t ceph_direct_io(struct kiocb *iocb, struct iov_iter *iter)
1365 const struct address_space_operations ceph_aops = {
1366 .readpage = ceph_readpage,
1367 .readpages = ceph_readpages,
1368 .writepage = ceph_writepage,
1369 .writepages = ceph_writepages_start,
1370 .write_begin = ceph_write_begin,
1371 .write_end = ceph_write_end,
1372 .set_page_dirty = ceph_set_page_dirty,
1373 .invalidatepage = ceph_invalidatepage,
1374 .releasepage = ceph_releasepage,
1375 .direct_IO = ceph_direct_io,
1378 static void ceph_block_sigs(sigset_t *oldset)
1381 siginitsetinv(&mask, sigmask(SIGKILL));
1382 sigprocmask(SIG_BLOCK, &mask, oldset);
1385 static void ceph_restore_sigs(sigset_t *oldset)
1387 sigprocmask(SIG_SETMASK, oldset, NULL);
1393 static int ceph_filemap_fault(struct vm_fault *vmf)
1395 struct vm_area_struct *vma = vmf->vma;
1396 struct inode *inode = file_inode(vma->vm_file);
1397 struct ceph_inode_info *ci = ceph_inode(inode);
1398 struct ceph_file_info *fi = vma->vm_file->private_data;
1399 struct page *pinned_page = NULL;
1400 loff_t off = vmf->pgoff << PAGE_SHIFT;
1404 ceph_block_sigs(&oldset);
1406 dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n",
1407 inode, ceph_vinop(inode), off, (size_t)PAGE_SIZE);
1408 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1409 want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1411 want = CEPH_CAP_FILE_CACHE;
1414 ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, -1, &got, &pinned_page);
1418 dout("filemap_fault %p %llu~%zd got cap refs on %s\n",
1419 inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got));
1421 if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
1422 ci->i_inline_version == CEPH_INLINE_NONE) {
1423 current->journal_info = vma->vm_file;
1424 ret = filemap_fault(vmf);
1425 current->journal_info = NULL;
1429 dout("filemap_fault %p %llu~%zd dropping cap refs on %s ret %d\n",
1430 inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got), ret);
1432 put_page(pinned_page);
1433 ceph_put_cap_refs(ci, got);
1438 /* read inline data */
1439 if (off >= PAGE_SIZE) {
1440 /* does not support inline data > PAGE_SIZE */
1441 ret = VM_FAULT_SIGBUS;
1444 struct address_space *mapping = inode->i_mapping;
1445 struct page *page = find_or_create_page(mapping, 0,
1446 mapping_gfp_constraint(mapping,
1452 ret1 = __ceph_do_getattr(inode, page,
1453 CEPH_STAT_CAP_INLINE_DATA, true);
1454 if (ret1 < 0 || off >= i_size_read(inode)) {
1460 ret = VM_FAULT_SIGBUS;
1463 if (ret1 < PAGE_SIZE)
1464 zero_user_segment(page, ret1, PAGE_SIZE);
1466 flush_dcache_page(page);
1467 SetPageUptodate(page);
1469 ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
1471 dout("filemap_fault %p %llu~%zd read inline data ret %d\n",
1472 inode, off, (size_t)PAGE_SIZE, ret);
1475 ceph_restore_sigs(&oldset);
1477 ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
1483 * Reuse write_begin here for simplicity.
1485 static int ceph_page_mkwrite(struct vm_fault *vmf)
1487 struct vm_area_struct *vma = vmf->vma;
1488 struct inode *inode = file_inode(vma->vm_file);
1489 struct ceph_inode_info *ci = ceph_inode(inode);
1490 struct ceph_file_info *fi = vma->vm_file->private_data;
1491 struct ceph_cap_flush *prealloc_cf;
1492 struct page *page = vmf->page;
1493 loff_t off = page_offset(page);
1494 loff_t size = i_size_read(inode);
1499 prealloc_cf = ceph_alloc_cap_flush();
1501 return VM_FAULT_OOM;
1503 ceph_block_sigs(&oldset);
1505 if (ci->i_inline_version != CEPH_INLINE_NONE) {
1506 struct page *locked_page = NULL;
1511 ret = ceph_uninline_data(vma->vm_file, locked_page);
1513 unlock_page(locked_page);
1518 if (off + PAGE_SIZE <= size)
1521 len = size & ~PAGE_MASK;
1523 dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
1524 inode, ceph_vinop(inode), off, len, size);
1525 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1526 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1528 want = CEPH_CAP_FILE_BUFFER;
1531 ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, off + len,
1536 dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
1537 inode, off, len, ceph_cap_string(got));
1539 /* Update time before taking page lock */
1540 file_update_time(vma->vm_file);
1545 if ((off > size) || (page->mapping != inode->i_mapping)) {
1547 ret = VM_FAULT_NOPAGE;
1551 ret = ceph_update_writeable_page(vma->vm_file, off, len, page);
1553 /* success. we'll keep the page locked. */
1554 set_page_dirty(page);
1555 ret = VM_FAULT_LOCKED;
1557 } while (ret == -EAGAIN);
1559 if (ret == VM_FAULT_LOCKED ||
1560 ci->i_inline_version != CEPH_INLINE_NONE) {
1562 spin_lock(&ci->i_ceph_lock);
1563 ci->i_inline_version = CEPH_INLINE_NONE;
1564 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1566 spin_unlock(&ci->i_ceph_lock);
1568 __mark_inode_dirty(inode, dirty);
1571 dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %d\n",
1572 inode, off, len, ceph_cap_string(got), ret);
1573 ceph_put_cap_refs(ci, got);
1575 ceph_restore_sigs(&oldset);
1576 ceph_free_cap_flush(prealloc_cf);
1578 ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
1582 void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
1583 char *data, size_t len)
1585 struct address_space *mapping = inode->i_mapping;
1591 if (i_size_read(inode) == 0)
1593 page = find_or_create_page(mapping, 0,
1594 mapping_gfp_constraint(mapping,
1598 if (PageUptodate(page)) {
1605 dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
1606 inode, ceph_vinop(inode), len, locked_page);
1609 void *kaddr = kmap_atomic(page);
1610 memcpy(kaddr, data, len);
1611 kunmap_atomic(kaddr);
1614 if (page != locked_page) {
1615 if (len < PAGE_SIZE)
1616 zero_user_segment(page, len, PAGE_SIZE);
1618 flush_dcache_page(page);
1620 SetPageUptodate(page);
1626 int ceph_uninline_data(struct file *filp, struct page *locked_page)
1628 struct inode *inode = file_inode(filp);
1629 struct ceph_inode_info *ci = ceph_inode(inode);
1630 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1631 struct ceph_osd_request *req;
1632 struct page *page = NULL;
1633 u64 len, inline_version;
1635 bool from_pagecache = false;
1637 spin_lock(&ci->i_ceph_lock);
1638 inline_version = ci->i_inline_version;
1639 spin_unlock(&ci->i_ceph_lock);
1641 dout("uninline_data %p %llx.%llx inline_version %llu\n",
1642 inode, ceph_vinop(inode), inline_version);
1644 if (inline_version == 1 || /* initial version, no data */
1645 inline_version == CEPH_INLINE_NONE)
1650 WARN_ON(!PageUptodate(page));
1651 } else if (ceph_caps_issued(ci) &
1652 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) {
1653 page = find_get_page(inode->i_mapping, 0);
1655 if (PageUptodate(page)) {
1656 from_pagecache = true;
1666 len = i_size_read(inode);
1667 if (len > PAGE_SIZE)
1670 page = __page_cache_alloc(GFP_NOFS);
1675 err = __ceph_do_getattr(inode, page,
1676 CEPH_STAT_CAP_INLINE_DATA, true);
1678 /* no inline data */
1679 if (err == -ENODATA)
1686 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1687 ceph_vino(inode), 0, &len, 0, 1,
1688 CEPH_OSD_OP_CREATE, CEPH_OSD_FLAG_WRITE,
1695 req->r_mtime = inode->i_mtime;
1696 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1698 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1699 ceph_osdc_put_request(req);
1703 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1704 ceph_vino(inode), 0, &len, 1, 3,
1705 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
1706 NULL, ci->i_truncate_seq,
1707 ci->i_truncate_size, false);
1713 osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false);
1716 __le64 xattr_buf = cpu_to_le64(inline_version);
1717 err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
1718 "inline_version", &xattr_buf,
1720 CEPH_OSD_CMPXATTR_OP_GT,
1721 CEPH_OSD_CMPXATTR_MODE_U64);
1728 int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
1729 "%llu", inline_version);
1730 err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
1732 xattr_buf, xattr_len, 0, 0);
1737 req->r_mtime = inode->i_mtime;
1738 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1740 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1742 ceph_osdc_put_request(req);
1743 if (err == -ECANCELED)
1746 if (page && page != locked_page) {
1747 if (from_pagecache) {
1751 __free_pages(page, 0);
1754 dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
1755 inode, ceph_vinop(inode), inline_version, err);
1759 static const struct vm_operations_struct ceph_vmops = {
1760 .fault = ceph_filemap_fault,
1761 .page_mkwrite = ceph_page_mkwrite,
1764 int ceph_mmap(struct file *file, struct vm_area_struct *vma)
1766 struct address_space *mapping = file->f_mapping;
1768 if (!mapping->a_ops->readpage)
1770 file_accessed(file);
1771 vma->vm_ops = &ceph_vmops;
1780 static int __ceph_pool_perm_get(struct ceph_inode_info *ci,
1781 s64 pool, struct ceph_string *pool_ns)
1783 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1784 struct ceph_mds_client *mdsc = fsc->mdsc;
1785 struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
1786 struct rb_node **p, *parent;
1787 struct ceph_pool_perm *perm;
1788 struct page **pages;
1790 int err = 0, err2 = 0, have = 0;
1792 down_read(&mdsc->pool_perm_rwsem);
1793 p = &mdsc->pool_perm_tree.rb_node;
1795 perm = rb_entry(*p, struct ceph_pool_perm, node);
1796 if (pool < perm->pool)
1798 else if (pool > perm->pool)
1799 p = &(*p)->rb_right;
1801 int ret = ceph_compare_string(pool_ns,
1807 p = &(*p)->rb_right;
1814 up_read(&mdsc->pool_perm_rwsem);
1819 dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n",
1820 pool, (int)pool_ns->len, pool_ns->str);
1822 dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool);
1824 down_write(&mdsc->pool_perm_rwsem);
1825 p = &mdsc->pool_perm_tree.rb_node;
1829 perm = rb_entry(parent, struct ceph_pool_perm, node);
1830 if (pool < perm->pool)
1832 else if (pool > perm->pool)
1833 p = &(*p)->rb_right;
1835 int ret = ceph_compare_string(pool_ns,
1841 p = &(*p)->rb_right;
1849 up_write(&mdsc->pool_perm_rwsem);
1853 rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
1854 1, false, GFP_NOFS);
1860 rd_req->r_flags = CEPH_OSD_FLAG_READ;
1861 osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
1862 rd_req->r_base_oloc.pool = pool;
1864 rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns);
1865 ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino);
1867 err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS);
1871 wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
1872 1, false, GFP_NOFS);
1878 wr_req->r_flags = CEPH_OSD_FLAG_WRITE;
1879 osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
1880 ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc);
1881 ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid);
1883 err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS);
1887 /* one page should be large enough for STAT data */
1888 pages = ceph_alloc_page_vector(1, GFP_KERNEL);
1889 if (IS_ERR(pages)) {
1890 err = PTR_ERR(pages);
1894 osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
1896 err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false);
1898 wr_req->r_mtime = ci->vfs_inode.i_mtime;
1899 wr_req->r_abort_on_full = true;
1900 err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false);
1903 err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
1905 err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
1907 if (err >= 0 || err == -ENOENT)
1909 else if (err != -EPERM)
1912 if (err2 == 0 || err2 == -EEXIST)
1914 else if (err2 != -EPERM) {
1919 pool_ns_len = pool_ns ? pool_ns->len : 0;
1920 perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS);
1928 perm->pool_ns_len = pool_ns_len;
1929 if (pool_ns_len > 0)
1930 memcpy(perm->pool_ns, pool_ns->str, pool_ns_len);
1931 perm->pool_ns[pool_ns_len] = 0;
1933 rb_link_node(&perm->node, parent, p);
1934 rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
1937 up_write(&mdsc->pool_perm_rwsem);
1939 ceph_osdc_put_request(rd_req);
1940 ceph_osdc_put_request(wr_req);
1945 dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n",
1946 pool, (int)pool_ns->len, pool_ns->str, err);
1948 dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err);
1952 int ceph_pool_perm_check(struct ceph_inode_info *ci, int need)
1955 struct ceph_string *pool_ns;
1958 if (ci->i_vino.snap != CEPH_NOSNAP) {
1960 * Pool permission check needs to write to the first object.
1961 * But for snapshot, head of the first object may have alread
1962 * been deleted. Skip check to avoid creating orphan object.
1967 if (ceph_test_mount_opt(ceph_inode_to_client(&ci->vfs_inode),
1971 spin_lock(&ci->i_ceph_lock);
1972 flags = ci->i_ceph_flags;
1973 pool = ci->i_layout.pool_id;
1974 spin_unlock(&ci->i_ceph_lock);
1976 if (flags & CEPH_I_POOL_PERM) {
1977 if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
1978 dout("ceph_pool_perm_check pool %lld no read perm\n",
1982 if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
1983 dout("ceph_pool_perm_check pool %lld no write perm\n",
1990 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
1991 ret = __ceph_pool_perm_get(ci, pool, pool_ns);
1992 ceph_put_string(pool_ns);
1996 flags = CEPH_I_POOL_PERM;
1997 if (ret & POOL_READ)
1998 flags |= CEPH_I_POOL_RD;
1999 if (ret & POOL_WRITE)
2000 flags |= CEPH_I_POOL_WR;
2002 spin_lock(&ci->i_ceph_lock);
2003 if (pool == ci->i_layout.pool_id &&
2004 pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
2005 ci->i_ceph_flags |= flags;
2007 pool = ci->i_layout.pool_id;
2008 flags = ci->i_ceph_flags;
2010 spin_unlock(&ci->i_ceph_lock);
2014 void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
2016 struct ceph_pool_perm *perm;
2019 while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
2020 n = rb_first(&mdsc->pool_perm_tree);
2021 perm = rb_entry(n, struct ceph_pool_perm, node);
2022 rb_erase(n, &mdsc->pool_perm_tree);