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>
13 #include "mds_client.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
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;
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;
73 struct ceph_inode_info *ci;
74 struct ceph_snap_context *snapc;
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));
87 inode = mapping->host;
88 ci = ceph_inode(inode);
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,
98 snapc = ceph_get_snap_context(capsnap->context);
99 capsnap->dirty_pages++;
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)
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);
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
136 static void ceph_invalidatepage(struct page *page, unsigned int offset,
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_SIZE) {
147 dout("%p invalidatepage %p idx %lu partial dirty page %u~%u\n",
148 inode, page, page->index, offset, length);
152 ceph_invalidate_fscache_page(inode, page);
154 if (!PagePrivate(page))
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);
173 ClearPagePrivate(page);
176 static int ceph_releasepage(struct page *page, gfp_t g)
178 dout("%p releasepage %p idx %lu (%sdirty)\n", page->mapping->host,
179 page, page->index, PageDirty(page) ? "" : "not ");
181 /* Can we release the page from the cache? */
182 if (!ceph_release_fscache_page(page, g))
185 return !PagePrivate(page);
189 * read a single page, without unlocking it.
191 static int readpage_nounlock(struct file *filp, struct page *page)
193 struct inode *inode = file_inode(filp);
194 struct ceph_inode_info *ci = ceph_inode(inode);
195 struct ceph_osd_client *osdc =
196 &ceph_inode_to_client(inode)->client->osdc;
198 u64 off = page_offset(page);
201 if (off >= i_size_read(inode)) {
202 zero_user_segment(page, 0, PAGE_SIZE);
203 SetPageUptodate(page);
207 if (ci->i_inline_version != CEPH_INLINE_NONE) {
209 * Uptodate inline data should have been added
210 * into page cache while getting Fcr caps.
214 zero_user_segment(page, 0, PAGE_SIZE);
215 SetPageUptodate(page);
219 err = ceph_readpage_from_fscache(inode, page);
223 dout("readpage inode %p file %p page %p index %lu\n",
224 inode, filp, page, page->index);
225 err = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout,
227 ci->i_truncate_seq, ci->i_truncate_size,
233 ceph_fscache_readpage_cancel(inode, page);
237 /* zero fill remainder of page */
238 zero_user_segment(page, err, PAGE_SIZE);
240 flush_dcache_page(page);
242 SetPageUptodate(page);
243 ceph_readpage_to_fscache(inode, page);
246 return err < 0 ? err : 0;
249 static int ceph_readpage(struct file *filp, struct page *page)
251 int r = readpage_nounlock(filp, page);
257 * Finish an async read(ahead) op.
259 static void finish_read(struct ceph_osd_request *req)
261 struct inode *inode = req->r_inode;
262 struct ceph_osd_data *osd_data;
263 int rc = req->r_result <= 0 ? req->r_result : 0;
264 int bytes = req->r_result >= 0 ? req->r_result : 0;
268 dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes);
270 /* unlock all pages, zeroing any data we didn't read */
271 osd_data = osd_req_op_extent_osd_data(req, 0);
272 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
273 num_pages = calc_pages_for((u64)osd_data->alignment,
274 (u64)osd_data->length);
275 for (i = 0; i < num_pages; i++) {
276 struct page *page = osd_data->pages[i];
278 if (rc < 0 && rc != -ENOENT) {
279 ceph_fscache_readpage_cancel(inode, page);
282 if (bytes < (int)PAGE_SIZE) {
283 /* zero (remainder of) page */
284 int s = bytes < 0 ? 0 : bytes;
285 zero_user_segment(page, s, PAGE_SIZE);
287 dout("finish_read %p uptodate %p idx %lu\n", inode, page,
289 flush_dcache_page(page);
290 SetPageUptodate(page);
291 ceph_readpage_to_fscache(inode, page);
297 kfree(osd_data->pages);
301 * start an async read(ahead) operation. return nr_pages we submitted
302 * a read for on success, or negative error code.
304 static int start_read(struct inode *inode, struct list_head *page_list, int max)
306 struct ceph_osd_client *osdc =
307 &ceph_inode_to_client(inode)->client->osdc;
308 struct ceph_inode_info *ci = ceph_inode(inode);
309 struct page *page = list_entry(page_list->prev, struct page, lru);
310 struct ceph_vino vino;
311 struct ceph_osd_request *req;
321 if (!current->journal_info) {
322 /* caller of readpages does not hold buffer and read caps
323 * (fadvise, madvise and readahead cases) */
324 int want = CEPH_CAP_FILE_CACHE;
325 ret = ceph_try_get_caps(ci, CEPH_CAP_FILE_RD, want, &got);
327 dout("start_read %p, error getting cap\n", inode);
328 } else if (!(got & want)) {
329 dout("start_read %p, no cache cap\n", inode);
334 ceph_put_cap_refs(ci, got);
335 while (!list_empty(page_list)) {
336 page = list_entry(page_list->prev,
338 list_del(&page->lru);
345 off = (u64) page_offset(page);
348 next_index = page->index;
349 list_for_each_entry_reverse(page, page_list, lru) {
350 if (page->index != next_index)
354 if (max && nr_pages == max)
357 len = nr_pages << PAGE_SHIFT;
358 dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages,
360 vino = ceph_vino(inode);
361 req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len,
362 0, 1, CEPH_OSD_OP_READ,
363 CEPH_OSD_FLAG_READ, NULL,
364 ci->i_truncate_seq, ci->i_truncate_size,
371 /* build page vector */
372 nr_pages = calc_pages_for(0, len);
373 pages = kmalloc(sizeof(*pages) * nr_pages, GFP_KERNEL);
378 for (i = 0; i < nr_pages; ++i) {
379 page = list_entry(page_list->prev, struct page, lru);
380 BUG_ON(PageLocked(page));
381 list_del(&page->lru);
383 dout("start_read %p adding %p idx %lu\n", inode, page,
385 if (add_to_page_cache_lru(page, &inode->i_data, page->index,
387 ceph_fscache_uncache_page(inode, page);
389 dout("start_read %p add_to_page_cache failed %p\n",
393 len = nr_pages << PAGE_SHIFT;
400 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false);
401 req->r_callback = finish_read;
402 req->r_inode = inode;
404 dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len);
405 ret = ceph_osdc_start_request(osdc, req, false);
408 ceph_osdc_put_request(req);
410 /* After adding locked pages to page cache, the inode holds cache cap.
411 * So we can drop our cap refs. */
413 ceph_put_cap_refs(ci, got);
418 for (i = 0; i < nr_pages; ++i) {
419 ceph_fscache_readpage_cancel(inode, pages[i]);
420 unlock_page(pages[i]);
422 ceph_put_page_vector(pages, nr_pages, false);
424 ceph_osdc_put_request(req);
427 ceph_put_cap_refs(ci, got);
433 * Read multiple pages. Leave pages we don't read + unlock in page_list;
434 * the caller (VM) cleans them up.
436 static int ceph_readpages(struct file *file, struct address_space *mapping,
437 struct list_head *page_list, unsigned nr_pages)
439 struct inode *inode = file_inode(file);
440 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
444 if (ceph_inode(inode)->i_inline_version != CEPH_INLINE_NONE)
447 rc = ceph_readpages_from_fscache(mapping->host, mapping, page_list,
453 if (fsc->mount_options->rsize >= PAGE_SIZE)
454 max = (fsc->mount_options->rsize + PAGE_SIZE - 1)
457 dout("readpages %p file %p nr_pages %d max %d\n", inode,
460 while (!list_empty(page_list)) {
461 rc = start_read(inode, page_list, max);
466 ceph_fscache_readpages_cancel(inode, page_list);
468 dout("readpages %p file %p ret %d\n", inode, file, rc);
473 * Get ref for the oldest snapc for an inode with dirty data... that is, the
474 * only snap context we are allowed to write back.
476 static struct ceph_snap_context *get_oldest_context(struct inode *inode,
481 struct ceph_inode_info *ci = ceph_inode(inode);
482 struct ceph_snap_context *snapc = NULL;
483 struct ceph_cap_snap *capsnap = NULL;
485 spin_lock(&ci->i_ceph_lock);
486 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
487 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
488 capsnap->context, capsnap->dirty_pages);
489 if (capsnap->dirty_pages) {
490 snapc = ceph_get_snap_context(capsnap->context);
492 *snap_size = capsnap->size;
494 *truncate_size = capsnap->truncate_size;
496 *truncate_seq = capsnap->truncate_seq;
500 if (!snapc && ci->i_wrbuffer_ref_head) {
501 snapc = ceph_get_snap_context(ci->i_head_snapc);
502 dout(" head snapc %p has %d dirty pages\n",
503 snapc, ci->i_wrbuffer_ref_head);
505 *truncate_size = capsnap->truncate_size;
507 *truncate_seq = capsnap->truncate_seq;
509 spin_unlock(&ci->i_ceph_lock);
514 * Write a single page, but leave the page locked.
516 * If we get a write error, set the page error bit, but still adjust the
517 * dirty page accounting (i.e., page is no longer dirty).
519 static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
522 struct ceph_inode_info *ci;
523 struct ceph_fs_client *fsc;
524 struct ceph_osd_client *osdc;
525 struct ceph_snap_context *snapc, *oldest;
526 loff_t page_off = page_offset(page);
527 loff_t snap_size = -1;
531 int err = 0, len = PAGE_SIZE;
533 dout("writepage %p idx %lu\n", page, page->index);
535 if (!page->mapping || !page->mapping->host) {
536 dout("writepage %p - no mapping\n", page);
539 inode = page->mapping->host;
540 ci = ceph_inode(inode);
541 fsc = ceph_inode_to_client(inode);
542 osdc = &fsc->client->osdc;
544 /* verify this is a writeable snap context */
545 snapc = page_snap_context(page);
547 dout("writepage %p page %p not dirty?\n", inode, page);
550 oldest = get_oldest_context(inode, &snap_size,
551 &truncate_size, &truncate_seq);
552 if (snapc->seq > oldest->seq) {
553 dout("writepage %p page %p snapc %p not writeable - noop\n",
555 /* we should only noop if called by kswapd */
556 WARN_ON((current->flags & PF_MEMALLOC) == 0);
557 ceph_put_snap_context(oldest);
560 ceph_put_snap_context(oldest);
563 snap_size = i_size_read(inode);
565 /* is this a partial page at end of file? */
566 if (page_off >= snap_size) {
567 dout("%p page eof %llu\n", page, snap_size);
570 if (snap_size < page_off + len)
571 len = snap_size - page_off;
573 dout("writepage %p page %p index %lu on %llu~%u snapc %p\n",
574 inode, page, page->index, page_off, len, snapc);
576 writeback_stat = atomic_long_inc_return(&fsc->writeback_count);
578 CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
579 set_bdi_congested(&fsc->backing_dev_info, BLK_RW_ASYNC);
581 set_page_writeback(page);
582 err = ceph_osdc_writepages(osdc, ceph_vino(inode),
583 &ci->i_layout, snapc,
585 truncate_seq, truncate_size,
586 &inode->i_mtime, &page, 1);
588 struct writeback_control tmp_wbc;
591 if (err == -ERESTARTSYS) {
592 /* killed by SIGKILL */
593 dout("writepage interrupted page %p\n", page);
594 redirty_page_for_writepage(wbc, page);
595 end_page_writeback(page);
598 dout("writepage setting page/mapping error %d %p\n",
601 mapping_set_error(&inode->i_data, err);
602 wbc->pages_skipped++;
604 dout("writepage cleaned page %p\n", page);
605 err = 0; /* vfs expects us to return 0 */
608 ClearPagePrivate(page);
609 end_page_writeback(page);
610 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
611 ceph_put_snap_context(snapc); /* page's reference */
616 static int ceph_writepage(struct page *page, struct writeback_control *wbc)
619 struct inode *inode = page->mapping->host;
622 err = writepage_nounlock(page, wbc);
623 if (err == -ERESTARTSYS) {
624 /* direct memory reclaimer was killed by SIGKILL. return 0
625 * to prevent caller from setting mapping/page error */
634 * lame release_pages helper. release_pages() isn't exported to
637 static void ceph_release_pages(struct page **pages, int num)
642 pagevec_init(&pvec, 0);
643 for (i = 0; i < num; i++) {
644 if (pagevec_add(&pvec, pages[i]) == 0)
645 pagevec_release(&pvec);
647 pagevec_release(&pvec);
651 * async writeback completion handler.
653 * If we get an error, set the mapping error bit, but not the individual
656 static void writepages_finish(struct ceph_osd_request *req)
658 struct inode *inode = req->r_inode;
659 struct ceph_inode_info *ci = ceph_inode(inode);
660 struct ceph_osd_data *osd_data;
662 int num_pages, total_pages = 0;
664 int rc = req->r_result;
665 struct ceph_snap_context *snapc = req->r_snapc;
666 struct address_space *mapping = inode->i_mapping;
667 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
670 dout("writepages_finish %p rc %d\n", inode, rc);
672 mapping_set_error(mapping, rc);
675 * We lost the cache cap, need to truncate the page before
676 * it is unlocked, otherwise we'd truncate it later in the
677 * page truncation thread, possibly losing some data that
680 remove_page = !(ceph_caps_issued(ci) &
681 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
683 /* clean all pages */
684 for (i = 0; i < req->r_num_ops; i++) {
685 if (req->r_ops[i].op != CEPH_OSD_OP_WRITE)
688 osd_data = osd_req_op_extent_osd_data(req, i);
689 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
690 num_pages = calc_pages_for((u64)osd_data->alignment,
691 (u64)osd_data->length);
692 total_pages += num_pages;
693 for (j = 0; j < num_pages; j++) {
694 page = osd_data->pages[j];
696 WARN_ON(!PageUptodate(page));
698 if (atomic_long_dec_return(&fsc->writeback_count) <
699 CONGESTION_OFF_THRESH(
700 fsc->mount_options->congestion_kb))
701 clear_bdi_congested(&fsc->backing_dev_info,
707 ceph_put_snap_context(page_snap_context(page));
709 ClearPagePrivate(page);
710 dout("unlocking %p\n", page);
711 end_page_writeback(page);
714 generic_error_remove_page(inode->i_mapping,
719 dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n",
720 inode, osd_data->length, rc >= 0 ? num_pages : 0);
722 ceph_release_pages(osd_data->pages, num_pages);
725 ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
727 osd_data = osd_req_op_extent_osd_data(req, 0);
728 if (osd_data->pages_from_pool)
729 mempool_free(osd_data->pages,
730 ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool);
732 kfree(osd_data->pages);
733 ceph_osdc_put_request(req);
737 * initiate async writeback
739 static int ceph_writepages_start(struct address_space *mapping,
740 struct writeback_control *wbc)
742 struct inode *inode = mapping->host;
743 struct ceph_inode_info *ci = ceph_inode(inode);
744 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
745 struct ceph_vino vino = ceph_vino(inode);
746 pgoff_t index, start, end;
749 pgoff_t max_pages = 0, max_pages_ever = 0;
750 struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
754 unsigned wsize = 1 << inode->i_blkbits;
755 struct ceph_osd_request *req = NULL;
757 loff_t snap_size, i_size;
762 * Include a 'sync' in the OSD request if this is a data
763 * integrity write (e.g., O_SYNC write or fsync()), or if our
764 * cap is being revoked.
766 if ((wbc->sync_mode == WB_SYNC_ALL) ||
767 ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER))
769 dout("writepages_start %p dosync=%d (mode=%s)\n",
771 wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
772 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
774 if (ACCESS_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
775 if (ci->i_wrbuffer_ref > 0) {
777 "writepage_start %p %lld forced umount\n",
778 inode, ceph_ino(inode));
780 mapping_set_error(mapping, -EIO);
781 return -EIO; /* we're in a forced umount, don't write! */
783 if (fsc->mount_options->wsize && fsc->mount_options->wsize < wsize)
784 wsize = fsc->mount_options->wsize;
785 if (wsize < PAGE_SIZE)
787 max_pages_ever = wsize >> PAGE_SHIFT;
789 pagevec_init(&pvec, 0);
791 /* where to start/end? */
792 if (wbc->range_cyclic) {
793 start = mapping->writeback_index; /* Start from prev offset */
795 dout(" cyclic, start at %lu\n", start);
797 start = wbc->range_start >> PAGE_SHIFT;
798 end = wbc->range_end >> PAGE_SHIFT;
799 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
802 dout(" not cyclic, %lu to %lu\n", start, end);
807 /* find oldest snap context with dirty data */
808 ceph_put_snap_context(snapc);
810 snapc = get_oldest_context(inode, &snap_size,
811 &truncate_size, &truncate_seq);
813 /* hmm, why does writepages get called when there
815 dout(" no snap context with dirty data?\n");
818 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
819 snapc, snapc->seq, snapc->num_snaps);
821 i_size = i_size_read(inode);
823 if (last_snapc && snapc != last_snapc) {
824 /* if we switched to a newer snapc, restart our scan at the
825 * start of the original file range. */
826 dout(" snapc differs from last pass, restarting at %lu\n",
832 while (!done && index <= end) {
835 pgoff_t strip_unit_end = 0;
836 int num_ops = 0, op_idx;
837 int pvec_pages, locked_pages = 0;
838 struct page **pages = NULL, **data_pages;
839 mempool_t *pool = NULL; /* Becomes non-null if mempool used */
842 u64 offset = 0, len = 0;
844 max_pages = max_pages_ever;
848 want = min(end - index,
849 min((pgoff_t)PAGEVEC_SIZE,
850 max_pages - (pgoff_t)locked_pages) - 1)
852 pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index,
855 dout("pagevec_lookup_tag got %d\n", pvec_pages);
856 if (!pvec_pages && !locked_pages)
858 for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
859 page = pvec.pages[i];
860 dout("? %p idx %lu\n", page, page->index);
861 if (locked_pages == 0)
862 lock_page(page); /* first page */
863 else if (!trylock_page(page))
866 /* only dirty pages, or our accounting breaks */
867 if (unlikely(!PageDirty(page)) ||
868 unlikely(page->mapping != mapping)) {
869 dout("!dirty or !mapping %p\n", page);
873 if (!wbc->range_cyclic && page->index > end) {
874 dout("end of range %p\n", page);
879 if (strip_unit_end && (page->index > strip_unit_end)) {
880 dout("end of strip unit %p\n", page);
884 if (wbc->sync_mode != WB_SYNC_NONE) {
885 dout("waiting on writeback %p\n", page);
886 wait_on_page_writeback(page);
888 if (page_offset(page) >=
889 (snap_size == -1 ? i_size : snap_size)) {
890 dout("%p page eof %llu\n", page,
891 (snap_size == -1 ? i_size : snap_size));
896 if (PageWriteback(page)) {
897 dout("%p under writeback\n", page);
902 /* only if matching snap context */
903 pgsnapc = page_snap_context(page);
904 if (pgsnapc->seq > snapc->seq) {
905 dout("page snapc %p %lld > oldest %p %lld\n",
906 pgsnapc, pgsnapc->seq, snapc, snapc->seq);
909 continue; /* keep looking for snap */
913 if (!clear_page_dirty_for_io(page)) {
914 dout("%p !clear_page_dirty_for_io\n", page);
920 * We have something to write. If this is
921 * the first locked page this time through,
922 * calculate max possinle write size and
923 * allocate a page array
925 if (locked_pages == 0) {
929 /* prepare async write request */
930 offset = (u64)page_offset(page);
933 rc = ceph_calc_file_object_mapping(&ci->i_layout,
942 num_ops = 1 + do_sync;
943 strip_unit_end = page->index +
944 ((len - 1) >> PAGE_SHIFT);
947 max_pages = calc_pages_for(0, (u64)len);
948 pages = kmalloc(max_pages * sizeof (*pages),
951 pool = fsc->wb_pagevec_pool;
952 pages = mempool_alloc(pool, GFP_NOFS);
957 } else if (page->index !=
958 (offset + len) >> PAGE_SHIFT) {
959 if (num_ops >= (pool ? CEPH_OSD_SLAB_OPS :
961 redirty_page_for_writepage(wbc, page);
967 offset = (u64)page_offset(page);
971 /* note position of first page in pvec */
974 dout("%p will write page %p idx %lu\n",
975 inode, page, page->index);
977 if (atomic_long_inc_return(&fsc->writeback_count) >
978 CONGESTION_ON_THRESH(
979 fsc->mount_options->congestion_kb)) {
980 set_bdi_congested(&fsc->backing_dev_info,
984 pages[locked_pages] = page;
989 /* did we get anything? */
991 goto release_pvec_pages;
994 BUG_ON(!locked_pages || first < 0);
996 if (pvec_pages && i == pvec_pages &&
997 locked_pages < max_pages) {
998 dout("reached end pvec, trying for more\n");
999 pagevec_reinit(&pvec);
1000 goto get_more_pages;
1003 /* shift unused pages over in the pvec... we
1004 * will need to release them below. */
1005 for (j = i; j < pvec_pages; j++) {
1006 dout(" pvec leftover page %p\n", pvec.pages[j]);
1007 pvec.pages[j-i+first] = pvec.pages[j];
1013 offset = page_offset(pages[0]);
1016 req = ceph_osdc_new_request(&fsc->client->osdc,
1017 &ci->i_layout, vino,
1018 offset, &len, 0, num_ops,
1020 CEPH_OSD_FLAG_WRITE |
1021 CEPH_OSD_FLAG_ONDISK,
1022 snapc, truncate_seq,
1023 truncate_size, false);
1025 req = ceph_osdc_new_request(&fsc->client->osdc,
1026 &ci->i_layout, vino,
1031 CEPH_OSD_FLAG_WRITE |
1032 CEPH_OSD_FLAG_ONDISK,
1033 snapc, truncate_seq,
1034 truncate_size, true);
1035 BUG_ON(IS_ERR(req));
1037 BUG_ON(len < page_offset(pages[locked_pages - 1]) +
1038 PAGE_SIZE - offset);
1040 req->r_callback = writepages_finish;
1041 req->r_inode = inode;
1043 /* Format the osd request message and submit the write */
1047 for (i = 0; i < locked_pages; i++) {
1048 u64 cur_offset = page_offset(pages[i]);
1049 if (offset + len != cur_offset) {
1050 if (op_idx + do_sync + 1 == req->r_num_ops)
1052 osd_req_op_extent_dup_last(req, op_idx,
1053 cur_offset - offset);
1054 dout("writepages got pages at %llu~%llu\n",
1056 osd_req_op_extent_osd_data_pages(req, op_idx,
1059 osd_req_op_extent_update(req, op_idx, len);
1062 offset = cur_offset;
1063 data_pages = pages + i;
1067 set_page_writeback(pages[i]);
1071 if (snap_size != -1) {
1072 len = min(len, snap_size - offset);
1073 } else if (i == locked_pages) {
1074 /* writepages_finish() clears writeback pages
1075 * according to the data length, so make sure
1076 * data length covers all locked pages */
1077 u64 min_len = len + 1 - PAGE_SIZE;
1078 len = min(len, (u64)i_size_read(inode) - offset);
1079 len = max(len, min_len);
1081 dout("writepages got pages at %llu~%llu\n", offset, len);
1083 osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len,
1085 osd_req_op_extent_update(req, op_idx, len);
1089 osd_req_op_init(req, op_idx, CEPH_OSD_OP_STARTSYNC, 0);
1091 BUG_ON(op_idx + 1 != req->r_num_ops);
1094 if (i < locked_pages) {
1095 BUG_ON(num_ops <= req->r_num_ops);
1096 num_ops -= req->r_num_ops;
1100 /* allocate new pages array for next request */
1102 pages = kmalloc(locked_pages * sizeof (*pages),
1105 pool = fsc->wb_pagevec_pool;
1106 pages = mempool_alloc(pool, GFP_NOFS);
1109 memcpy(pages, data_pages + i,
1110 locked_pages * sizeof(*pages));
1111 memset(data_pages + i, 0,
1112 locked_pages * sizeof(*pages));
1114 BUG_ON(num_ops != req->r_num_ops);
1115 index = pages[i - 1]->index + 1;
1116 /* request message now owns the pages array */
1120 req->r_mtime = inode->i_mtime;
1121 rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
1125 wbc->nr_to_write -= i;
1129 if (wbc->nr_to_write <= 0)
1133 dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
1134 pvec.nr ? pvec.pages[0] : NULL);
1135 pagevec_release(&pvec);
1137 if (locked_pages && !done)
1141 if (should_loop && !done) {
1142 /* more to do; loop back to beginning of file */
1143 dout("writepages looping back to beginning of file\n");
1149 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1150 mapping->writeback_index = index;
1153 ceph_osdc_put_request(req);
1154 ceph_put_snap_context(snapc);
1155 dout("writepages done, rc = %d\n", rc);
1162 * See if a given @snapc is either writeable, or already written.
1164 static int context_is_writeable_or_written(struct inode *inode,
1165 struct ceph_snap_context *snapc)
1167 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL,
1169 int ret = !oldest || snapc->seq <= oldest->seq;
1171 ceph_put_snap_context(oldest);
1176 * We are only allowed to write into/dirty the page if the page is
1177 * clean, or already dirty within the same snap context.
1179 * called with page locked.
1180 * return success with page locked,
1181 * or any failure (incl -EAGAIN) with page unlocked.
1183 static int ceph_update_writeable_page(struct file *file,
1184 loff_t pos, unsigned len,
1187 struct inode *inode = file_inode(file);
1188 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1189 struct ceph_inode_info *ci = ceph_inode(inode);
1190 loff_t page_off = pos & PAGE_MASK;
1191 int pos_in_page = pos & ~PAGE_MASK;
1192 int end_in_page = pos_in_page + len;
1195 struct ceph_snap_context *snapc, *oldest;
1197 if (ACCESS_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
1198 dout(" page %p forced umount\n", page);
1204 /* writepages currently holds page lock, but if we change that later, */
1205 wait_on_page_writeback(page);
1207 snapc = page_snap_context(page);
1208 if (snapc && snapc != ci->i_head_snapc) {
1210 * this page is already dirty in another (older) snap
1211 * context! is it writeable now?
1213 oldest = get_oldest_context(inode, NULL, NULL, NULL);
1215 if (snapc->seq > oldest->seq) {
1216 ceph_put_snap_context(oldest);
1217 dout(" page %p snapc %p not current or oldest\n",
1220 * queue for writeback, and wait for snapc to
1221 * be writeable or written
1223 snapc = ceph_get_snap_context(snapc);
1225 ceph_queue_writeback(inode);
1226 r = wait_event_killable(ci->i_cap_wq,
1227 context_is_writeable_or_written(inode, snapc));
1228 ceph_put_snap_context(snapc);
1229 if (r == -ERESTARTSYS)
1233 ceph_put_snap_context(oldest);
1235 /* yay, writeable, do it now (without dropping page lock) */
1236 dout(" page %p snapc %p not current, but oldest\n",
1238 if (!clear_page_dirty_for_io(page))
1240 r = writepage_nounlock(page, NULL);
1246 if (PageUptodate(page)) {
1247 dout(" page %p already uptodate\n", page);
1252 if (pos_in_page == 0 && len == PAGE_SIZE)
1255 /* past end of file? */
1256 i_size = i_size_read(inode);
1258 if (page_off >= i_size ||
1259 (pos_in_page == 0 && (pos+len) >= i_size &&
1260 end_in_page - pos_in_page != PAGE_SIZE)) {
1261 dout(" zeroing %p 0 - %d and %d - %d\n",
1262 page, pos_in_page, end_in_page, (int)PAGE_SIZE);
1263 zero_user_segments(page,
1265 end_in_page, PAGE_SIZE);
1269 /* we need to read it. */
1270 r = readpage_nounlock(file, page);
1280 * We are only allowed to write into/dirty the page if the page is
1281 * clean, or already dirty within the same snap context.
1283 static int ceph_write_begin(struct file *file, struct address_space *mapping,
1284 loff_t pos, unsigned len, unsigned flags,
1285 struct page **pagep, void **fsdata)
1287 struct inode *inode = file_inode(file);
1289 pgoff_t index = pos >> PAGE_SHIFT;
1294 page = grab_cache_page_write_begin(mapping, index, 0);
1298 dout("write_begin file %p inode %p page %p %d~%d\n", file,
1299 inode, page, (int)pos, (int)len);
1301 r = ceph_update_writeable_page(file, pos, len, page);
1306 } while (r == -EAGAIN);
1312 * we don't do anything in here that simple_write_end doesn't do
1313 * except adjust dirty page accounting
1315 static int ceph_write_end(struct file *file, struct address_space *mapping,
1316 loff_t pos, unsigned len, unsigned copied,
1317 struct page *page, void *fsdata)
1319 struct inode *inode = file_inode(file);
1322 dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
1323 inode, page, (int)pos, (int)copied, (int)len);
1325 /* zero the stale part of the page if we did a short copy */
1326 if (!PageUptodate(page)) {
1331 SetPageUptodate(page);
1334 /* did file size increase? */
1335 if (pos+copied > i_size_read(inode))
1336 check_cap = ceph_inode_set_size(inode, pos+copied);
1338 set_page_dirty(page);
1345 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
1351 * we set .direct_IO to indicate direct io is supported, but since we
1352 * intercept O_DIRECT reads and writes early, this function should
1355 static ssize_t ceph_direct_io(struct kiocb *iocb, struct iov_iter *iter)
1361 const struct address_space_operations ceph_aops = {
1362 .readpage = ceph_readpage,
1363 .readpages = ceph_readpages,
1364 .writepage = ceph_writepage,
1365 .writepages = ceph_writepages_start,
1366 .write_begin = ceph_write_begin,
1367 .write_end = ceph_write_end,
1368 .set_page_dirty = ceph_set_page_dirty,
1369 .invalidatepage = ceph_invalidatepage,
1370 .releasepage = ceph_releasepage,
1371 .direct_IO = ceph_direct_io,
1374 static void ceph_block_sigs(sigset_t *oldset)
1377 siginitsetinv(&mask, sigmask(SIGKILL));
1378 sigprocmask(SIG_BLOCK, &mask, oldset);
1381 static void ceph_restore_sigs(sigset_t *oldset)
1383 sigprocmask(SIG_SETMASK, oldset, NULL);
1389 static int ceph_filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1391 struct inode *inode = file_inode(vma->vm_file);
1392 struct ceph_inode_info *ci = ceph_inode(inode);
1393 struct ceph_file_info *fi = vma->vm_file->private_data;
1394 struct page *pinned_page = NULL;
1395 loff_t off = vmf->pgoff << PAGE_SHIFT;
1399 ceph_block_sigs(&oldset);
1401 dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n",
1402 inode, ceph_vinop(inode), off, (size_t)PAGE_SIZE);
1403 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1404 want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1406 want = CEPH_CAP_FILE_CACHE;
1409 ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, -1, &got, &pinned_page);
1413 dout("filemap_fault %p %llu~%zd got cap refs on %s\n",
1414 inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got));
1416 if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
1417 ci->i_inline_version == CEPH_INLINE_NONE) {
1418 current->journal_info = vma->vm_file;
1419 ret = filemap_fault(vma, vmf);
1420 current->journal_info = NULL;
1424 dout("filemap_fault %p %llu~%zd dropping cap refs on %s ret %d\n",
1425 inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got), ret);
1427 put_page(pinned_page);
1428 ceph_put_cap_refs(ci, got);
1433 /* read inline data */
1434 if (off >= PAGE_SIZE) {
1435 /* does not support inline data > PAGE_SIZE */
1436 ret = VM_FAULT_SIGBUS;
1439 struct address_space *mapping = inode->i_mapping;
1440 struct page *page = find_or_create_page(mapping, 0,
1441 mapping_gfp_constraint(mapping,
1447 ret1 = __ceph_do_getattr(inode, page,
1448 CEPH_STAT_CAP_INLINE_DATA, true);
1449 if (ret1 < 0 || off >= i_size_read(inode)) {
1455 ret = VM_FAULT_SIGBUS;
1458 if (ret1 < PAGE_SIZE)
1459 zero_user_segment(page, ret1, PAGE_SIZE);
1461 flush_dcache_page(page);
1462 SetPageUptodate(page);
1464 ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
1466 dout("filemap_fault %p %llu~%zd read inline data ret %d\n",
1467 inode, off, (size_t)PAGE_SIZE, ret);
1470 ceph_restore_sigs(&oldset);
1472 ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
1478 * Reuse write_begin here for simplicity.
1480 static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1482 struct inode *inode = file_inode(vma->vm_file);
1483 struct ceph_inode_info *ci = ceph_inode(inode);
1484 struct ceph_file_info *fi = vma->vm_file->private_data;
1485 struct ceph_cap_flush *prealloc_cf;
1486 struct page *page = vmf->page;
1487 loff_t off = page_offset(page);
1488 loff_t size = i_size_read(inode);
1493 prealloc_cf = ceph_alloc_cap_flush();
1495 return VM_FAULT_OOM;
1497 ceph_block_sigs(&oldset);
1499 if (ci->i_inline_version != CEPH_INLINE_NONE) {
1500 struct page *locked_page = NULL;
1505 ret = ceph_uninline_data(vma->vm_file, locked_page);
1507 unlock_page(locked_page);
1512 if (off + PAGE_SIZE <= size)
1515 len = size & ~PAGE_MASK;
1517 dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
1518 inode, ceph_vinop(inode), off, len, size);
1519 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1520 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1522 want = CEPH_CAP_FILE_BUFFER;
1525 ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, off + len,
1530 dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
1531 inode, off, len, ceph_cap_string(got));
1533 /* Update time before taking page lock */
1534 file_update_time(vma->vm_file);
1539 if ((off > size) || (page->mapping != inode->i_mapping)) {
1541 ret = VM_FAULT_NOPAGE;
1545 ret = ceph_update_writeable_page(vma->vm_file, off, len, page);
1547 /* success. we'll keep the page locked. */
1548 set_page_dirty(page);
1549 ret = VM_FAULT_LOCKED;
1551 } while (ret == -EAGAIN);
1553 if (ret == VM_FAULT_LOCKED ||
1554 ci->i_inline_version != CEPH_INLINE_NONE) {
1556 spin_lock(&ci->i_ceph_lock);
1557 ci->i_inline_version = CEPH_INLINE_NONE;
1558 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1560 spin_unlock(&ci->i_ceph_lock);
1562 __mark_inode_dirty(inode, dirty);
1565 dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %d\n",
1566 inode, off, len, ceph_cap_string(got), ret);
1567 ceph_put_cap_refs(ci, got);
1569 ceph_restore_sigs(&oldset);
1570 ceph_free_cap_flush(prealloc_cf);
1572 ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
1576 void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
1577 char *data, size_t len)
1579 struct address_space *mapping = inode->i_mapping;
1585 if (i_size_read(inode) == 0)
1587 page = find_or_create_page(mapping, 0,
1588 mapping_gfp_constraint(mapping,
1592 if (PageUptodate(page)) {
1599 dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
1600 inode, ceph_vinop(inode), len, locked_page);
1603 void *kaddr = kmap_atomic(page);
1604 memcpy(kaddr, data, len);
1605 kunmap_atomic(kaddr);
1608 if (page != locked_page) {
1609 if (len < PAGE_SIZE)
1610 zero_user_segment(page, len, PAGE_SIZE);
1612 flush_dcache_page(page);
1614 SetPageUptodate(page);
1620 int ceph_uninline_data(struct file *filp, struct page *locked_page)
1622 struct inode *inode = file_inode(filp);
1623 struct ceph_inode_info *ci = ceph_inode(inode);
1624 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1625 struct ceph_osd_request *req;
1626 struct page *page = NULL;
1627 u64 len, inline_version;
1629 bool from_pagecache = false;
1631 spin_lock(&ci->i_ceph_lock);
1632 inline_version = ci->i_inline_version;
1633 spin_unlock(&ci->i_ceph_lock);
1635 dout("uninline_data %p %llx.%llx inline_version %llu\n",
1636 inode, ceph_vinop(inode), inline_version);
1638 if (inline_version == 1 || /* initial version, no data */
1639 inline_version == CEPH_INLINE_NONE)
1644 WARN_ON(!PageUptodate(page));
1645 } else if (ceph_caps_issued(ci) &
1646 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) {
1647 page = find_get_page(inode->i_mapping, 0);
1649 if (PageUptodate(page)) {
1650 from_pagecache = true;
1660 len = i_size_read(inode);
1661 if (len > PAGE_SIZE)
1664 page = __page_cache_alloc(GFP_NOFS);
1669 err = __ceph_do_getattr(inode, page,
1670 CEPH_STAT_CAP_INLINE_DATA, true);
1672 /* no inline data */
1673 if (err == -ENODATA)
1680 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1681 ceph_vino(inode), 0, &len, 0, 1,
1683 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
1690 req->r_mtime = inode->i_mtime;
1691 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1693 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1694 ceph_osdc_put_request(req);
1698 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1699 ceph_vino(inode), 0, &len, 1, 3,
1701 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
1702 NULL, ci->i_truncate_seq,
1703 ci->i_truncate_size, false);
1709 osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false);
1712 __le64 xattr_buf = cpu_to_le64(inline_version);
1713 err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
1714 "inline_version", &xattr_buf,
1716 CEPH_OSD_CMPXATTR_OP_GT,
1717 CEPH_OSD_CMPXATTR_MODE_U64);
1724 int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
1725 "%llu", inline_version);
1726 err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
1728 xattr_buf, xattr_len, 0, 0);
1733 req->r_mtime = inode->i_mtime;
1734 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1736 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1738 ceph_osdc_put_request(req);
1739 if (err == -ECANCELED)
1742 if (page && page != locked_page) {
1743 if (from_pagecache) {
1747 __free_pages(page, 0);
1750 dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
1751 inode, ceph_vinop(inode), inline_version, err);
1755 static const struct vm_operations_struct ceph_vmops = {
1756 .fault = ceph_filemap_fault,
1757 .page_mkwrite = ceph_page_mkwrite,
1760 int ceph_mmap(struct file *file, struct vm_area_struct *vma)
1762 struct address_space *mapping = file->f_mapping;
1764 if (!mapping->a_ops->readpage)
1766 file_accessed(file);
1767 vma->vm_ops = &ceph_vmops;
1776 static int __ceph_pool_perm_get(struct ceph_inode_info *ci,
1777 s64 pool, struct ceph_string *pool_ns)
1779 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1780 struct ceph_mds_client *mdsc = fsc->mdsc;
1781 struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
1782 struct rb_node **p, *parent;
1783 struct ceph_pool_perm *perm;
1784 struct page **pages;
1786 int err = 0, err2 = 0, have = 0;
1788 down_read(&mdsc->pool_perm_rwsem);
1789 p = &mdsc->pool_perm_tree.rb_node;
1791 perm = rb_entry(*p, struct ceph_pool_perm, node);
1792 if (pool < perm->pool)
1794 else if (pool > perm->pool)
1795 p = &(*p)->rb_right;
1797 int ret = ceph_compare_string(pool_ns,
1803 p = &(*p)->rb_right;
1810 up_read(&mdsc->pool_perm_rwsem);
1815 dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n",
1816 pool, (int)pool_ns->len, pool_ns->str);
1818 dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool);
1820 down_write(&mdsc->pool_perm_rwsem);
1821 p = &mdsc->pool_perm_tree.rb_node;
1825 perm = rb_entry(parent, struct ceph_pool_perm, node);
1826 if (pool < perm->pool)
1828 else if (pool > perm->pool)
1829 p = &(*p)->rb_right;
1831 int ret = ceph_compare_string(pool_ns,
1837 p = &(*p)->rb_right;
1845 up_write(&mdsc->pool_perm_rwsem);
1849 rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
1850 1, false, GFP_NOFS);
1856 rd_req->r_flags = CEPH_OSD_FLAG_READ;
1857 osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
1858 rd_req->r_base_oloc.pool = pool;
1860 rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns);
1861 ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino);
1863 err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS);
1867 wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
1868 1, false, GFP_NOFS);
1874 wr_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ACK;
1875 osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
1876 ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc);
1877 ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid);
1879 err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS);
1883 /* one page should be large enough for STAT data */
1884 pages = ceph_alloc_page_vector(1, GFP_KERNEL);
1885 if (IS_ERR(pages)) {
1886 err = PTR_ERR(pages);
1890 osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
1892 err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false);
1894 wr_req->r_mtime = ci->vfs_inode.i_mtime;
1895 err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false);
1898 err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
1900 err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
1902 if (err >= 0 || err == -ENOENT)
1904 else if (err != -EPERM)
1907 if (err2 == 0 || err2 == -EEXIST)
1909 else if (err2 != -EPERM) {
1914 pool_ns_len = pool_ns ? pool_ns->len : 0;
1915 perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS);
1923 perm->pool_ns_len = pool_ns_len;
1924 if (pool_ns_len > 0)
1925 memcpy(perm->pool_ns, pool_ns->str, pool_ns_len);
1926 perm->pool_ns[pool_ns_len] = 0;
1928 rb_link_node(&perm->node, parent, p);
1929 rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
1932 up_write(&mdsc->pool_perm_rwsem);
1934 ceph_osdc_put_request(rd_req);
1935 ceph_osdc_put_request(wr_req);
1940 dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n",
1941 pool, (int)pool_ns->len, pool_ns->str, err);
1943 dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err);
1947 int ceph_pool_perm_check(struct ceph_inode_info *ci, int need)
1950 struct ceph_string *pool_ns;
1953 if (ci->i_vino.snap != CEPH_NOSNAP) {
1955 * Pool permission check needs to write to the first object.
1956 * But for snapshot, head of the first object may have alread
1957 * been deleted. Skip check to avoid creating orphan object.
1962 if (ceph_test_mount_opt(ceph_inode_to_client(&ci->vfs_inode),
1966 spin_lock(&ci->i_ceph_lock);
1967 flags = ci->i_ceph_flags;
1968 pool = ci->i_layout.pool_id;
1969 spin_unlock(&ci->i_ceph_lock);
1971 if (flags & CEPH_I_POOL_PERM) {
1972 if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
1973 dout("ceph_pool_perm_check pool %lld no read perm\n",
1977 if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
1978 dout("ceph_pool_perm_check pool %lld no write perm\n",
1985 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
1986 ret = __ceph_pool_perm_get(ci, pool, pool_ns);
1987 ceph_put_string(pool_ns);
1991 flags = CEPH_I_POOL_PERM;
1992 if (ret & POOL_READ)
1993 flags |= CEPH_I_POOL_RD;
1994 if (ret & POOL_WRITE)
1995 flags |= CEPH_I_POOL_WR;
1997 spin_lock(&ci->i_ceph_lock);
1998 if (pool == ci->i_layout.pool_id &&
1999 pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
2000 ci->i_ceph_flags |= flags;
2002 pool = ci->i_layout.pool_id;
2003 flags = ci->i_ceph_flags;
2005 spin_unlock(&ci->i_ceph_lock);
2009 void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
2011 struct ceph_pool_perm *perm;
2014 while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
2015 n = rb_first(&mdsc->pool_perm_tree);
2016 perm = rb_entry(n, struct ceph_pool_perm, node);
2017 rb_erase(n, &mdsc->pool_perm_tree);