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 max = fsc->mount_options->rsize >> PAGE_SHIFT;
459 dout("readpages %p file %p nr_pages %d max %d\n",
460 inode, file, nr_pages, max);
461 while (!list_empty(page_list)) {
462 rc = start_read(inode, page_list, max);
467 ceph_fscache_readpages_cancel(inode, page_list);
469 dout("readpages %p file %p ret %d\n", inode, file, rc);
474 * Get ref for the oldest snapc for an inode with dirty data... that is, the
475 * only snap context we are allowed to write back.
477 static struct ceph_snap_context *get_oldest_context(struct inode *inode,
482 struct ceph_inode_info *ci = ceph_inode(inode);
483 struct ceph_snap_context *snapc = NULL;
484 struct ceph_cap_snap *capsnap = NULL;
486 spin_lock(&ci->i_ceph_lock);
487 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
488 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
489 capsnap->context, capsnap->dirty_pages);
490 if (capsnap->dirty_pages) {
491 snapc = ceph_get_snap_context(capsnap->context);
493 *snap_size = capsnap->size;
495 *truncate_size = capsnap->truncate_size;
497 *truncate_seq = capsnap->truncate_seq;
501 if (!snapc && ci->i_wrbuffer_ref_head) {
502 snapc = ceph_get_snap_context(ci->i_head_snapc);
503 dout(" head snapc %p has %d dirty pages\n",
504 snapc, ci->i_wrbuffer_ref_head);
506 *truncate_size = ci->i_truncate_size;
508 *truncate_seq = ci->i_truncate_seq;
510 spin_unlock(&ci->i_ceph_lock);
515 * Write a single page, but leave the page locked.
517 * If we get a write error, set the page error bit, but still adjust the
518 * dirty page accounting (i.e., page is no longer dirty).
520 static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
523 struct ceph_inode_info *ci;
524 struct ceph_fs_client *fsc;
525 struct ceph_osd_client *osdc;
526 struct ceph_snap_context *snapc, *oldest;
527 loff_t page_off = page_offset(page);
528 loff_t snap_size = -1;
532 int err, len = PAGE_SIZE;
534 dout("writepage %p idx %lu\n", page, page->index);
536 inode = page->mapping->host;
537 ci = ceph_inode(inode);
538 fsc = ceph_inode_to_client(inode);
539 osdc = &fsc->client->osdc;
541 /* verify this is a writeable snap context */
542 snapc = page_snap_context(page);
544 dout("writepage %p page %p not dirty?\n", inode, page);
547 oldest = get_oldest_context(inode, &snap_size,
548 &truncate_size, &truncate_seq);
549 if (snapc->seq > oldest->seq) {
550 dout("writepage %p page %p snapc %p not writeable - noop\n",
552 /* we should only noop if called by kswapd */
553 WARN_ON(!(current->flags & PF_MEMALLOC));
554 ceph_put_snap_context(oldest);
555 redirty_page_for_writepage(wbc, page);
558 ceph_put_snap_context(oldest);
561 snap_size = i_size_read(inode);
563 /* is this a partial page at end of file? */
564 if (page_off >= snap_size) {
565 dout("%p page eof %llu\n", page, snap_size);
569 if (snap_size < page_off + len)
570 len = snap_size - page_off;
572 dout("writepage %p page %p index %lu on %llu~%u snapc %p\n",
573 inode, page, page->index, page_off, len, snapc);
575 writeback_stat = atomic_long_inc_return(&fsc->writeback_count);
577 CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
578 set_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
580 set_page_writeback(page);
581 err = ceph_osdc_writepages(osdc, ceph_vino(inode),
582 &ci->i_layout, snapc,
584 truncate_seq, truncate_size,
585 &inode->i_mtime, &page, 1);
587 struct writeback_control tmp_wbc;
590 if (err == -ERESTARTSYS) {
591 /* killed by SIGKILL */
592 dout("writepage interrupted page %p\n", page);
593 redirty_page_for_writepage(wbc, page);
594 end_page_writeback(page);
597 dout("writepage setting page/mapping error %d %p\n",
600 mapping_set_error(&inode->i_data, err);
601 wbc->pages_skipped++;
603 dout("writepage cleaned page %p\n", page);
604 err = 0; /* vfs expects us to return 0 */
607 ClearPagePrivate(page);
608 end_page_writeback(page);
609 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
610 ceph_put_snap_context(snapc); /* page's reference */
614 static int ceph_writepage(struct page *page, struct writeback_control *wbc)
617 struct inode *inode = page->mapping->host;
620 err = writepage_nounlock(page, wbc);
621 if (err == -ERESTARTSYS) {
622 /* direct memory reclaimer was killed by SIGKILL. return 0
623 * to prevent caller from setting mapping/page error */
632 * lame release_pages helper. release_pages() isn't exported to
635 static void ceph_release_pages(struct page **pages, int num)
640 pagevec_init(&pvec, 0);
641 for (i = 0; i < num; i++) {
642 if (pagevec_add(&pvec, pages[i]) == 0)
643 pagevec_release(&pvec);
645 pagevec_release(&pvec);
649 * async writeback completion handler.
651 * If we get an error, set the mapping error bit, but not the individual
654 static void writepages_finish(struct ceph_osd_request *req)
656 struct inode *inode = req->r_inode;
657 struct ceph_inode_info *ci = ceph_inode(inode);
658 struct ceph_osd_data *osd_data;
660 int num_pages, total_pages = 0;
662 int rc = req->r_result;
663 struct ceph_snap_context *snapc = req->r_snapc;
664 struct address_space *mapping = inode->i_mapping;
665 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
668 dout("writepages_finish %p rc %d\n", inode, rc);
670 mapping_set_error(mapping, rc);
671 ceph_set_error_write(ci);
673 ceph_clear_error_write(ci);
677 * We lost the cache cap, need to truncate the page before
678 * it is unlocked, otherwise we'd truncate it later in the
679 * page truncation thread, possibly losing some data that
682 remove_page = !(ceph_caps_issued(ci) &
683 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
685 /* clean all pages */
686 for (i = 0; i < req->r_num_ops; i++) {
687 if (req->r_ops[i].op != CEPH_OSD_OP_WRITE)
690 osd_data = osd_req_op_extent_osd_data(req, i);
691 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
692 num_pages = calc_pages_for((u64)osd_data->alignment,
693 (u64)osd_data->length);
694 total_pages += num_pages;
695 for (j = 0; j < num_pages; j++) {
696 page = osd_data->pages[j];
698 WARN_ON(!PageUptodate(page));
700 if (atomic_long_dec_return(&fsc->writeback_count) <
701 CONGESTION_OFF_THRESH(
702 fsc->mount_options->congestion_kb))
703 clear_bdi_congested(inode_to_bdi(inode),
706 ceph_put_snap_context(page_snap_context(page));
708 ClearPagePrivate(page);
709 dout("unlocking %p\n", page);
710 end_page_writeback(page);
713 generic_error_remove_page(inode->i_mapping,
718 dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n",
719 inode, osd_data->length, rc >= 0 ? num_pages : 0);
721 ceph_release_pages(osd_data->pages, num_pages);
724 ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
726 osd_data = osd_req_op_extent_osd_data(req, 0);
727 if (osd_data->pages_from_pool)
728 mempool_free(osd_data->pages,
729 ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool);
731 kfree(osd_data->pages);
732 ceph_osdc_put_request(req);
736 * initiate async writeback
738 static int ceph_writepages_start(struct address_space *mapping,
739 struct writeback_control *wbc)
741 struct inode *inode = mapping->host;
742 struct ceph_inode_info *ci = ceph_inode(inode);
743 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
744 struct ceph_vino vino = ceph_vino(inode);
745 pgoff_t index, start, end;
748 pgoff_t max_pages = 0, max_pages_ever = 0;
749 struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
753 unsigned int wsize = i_blocksize(inode);
754 struct ceph_osd_request *req = NULL;
756 loff_t snap_size, i_size;
761 * Include a 'sync' in the OSD request if this is a data
762 * integrity write (e.g., O_SYNC write or fsync()), or if our
763 * cap is being revoked.
765 if ((wbc->sync_mode == WB_SYNC_ALL) ||
766 ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER))
768 dout("writepages_start %p dosync=%d (mode=%s)\n",
770 wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
771 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
773 if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
774 if (ci->i_wrbuffer_ref > 0) {
776 "writepage_start %p %lld forced umount\n",
777 inode, ceph_ino(inode));
779 mapping_set_error(mapping, -EIO);
780 return -EIO; /* we're in a forced umount, don't write! */
782 if (fsc->mount_options->wsize < wsize)
783 wsize = fsc->mount_options->wsize;
784 max_pages_ever = wsize >> PAGE_SHIFT;
786 pagevec_init(&pvec, 0);
788 /* where to start/end? */
789 if (wbc->range_cyclic) {
790 start = mapping->writeback_index; /* Start from prev offset */
792 dout(" cyclic, start at %lu\n", start);
794 start = wbc->range_start >> PAGE_SHIFT;
795 end = wbc->range_end >> PAGE_SHIFT;
796 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
799 dout(" not cyclic, %lu to %lu\n", start, end);
804 /* find oldest snap context with dirty data */
805 ceph_put_snap_context(snapc);
807 snapc = get_oldest_context(inode, &snap_size,
808 &truncate_size, &truncate_seq);
810 /* hmm, why does writepages get called when there
812 dout(" no snap context with dirty data?\n");
815 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
816 snapc, snapc->seq, snapc->num_snaps);
818 i_size = i_size_read(inode);
820 if (last_snapc && snapc != last_snapc) {
821 /* if we switched to a newer snapc, restart our scan at the
822 * start of the original file range. */
823 dout(" snapc differs from last pass, restarting at %lu\n",
829 while (!done && index <= end) {
832 pgoff_t strip_unit_end = 0;
833 int num_ops = 0, op_idx;
834 int pvec_pages, locked_pages = 0;
835 struct page **pages = NULL, **data_pages;
836 mempool_t *pool = NULL; /* Becomes non-null if mempool used */
839 u64 offset = 0, len = 0;
841 max_pages = max_pages_ever;
845 want = min(end - index,
846 min((pgoff_t)PAGEVEC_SIZE,
847 max_pages - (pgoff_t)locked_pages) - 1)
849 pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index,
852 dout("pagevec_lookup_tag got %d\n", pvec_pages);
853 if (!pvec_pages && !locked_pages)
855 for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
856 page = pvec.pages[i];
857 dout("? %p idx %lu\n", page, page->index);
858 if (locked_pages == 0)
859 lock_page(page); /* first page */
860 else if (!trylock_page(page))
863 /* only dirty pages, or our accounting breaks */
864 if (unlikely(!PageDirty(page)) ||
865 unlikely(page->mapping != mapping)) {
866 dout("!dirty or !mapping %p\n", page);
870 if (!wbc->range_cyclic && page->index > end) {
871 dout("end of range %p\n", page);
876 if (strip_unit_end && (page->index > strip_unit_end)) {
877 dout("end of strip unit %p\n", page);
881 if (wbc->sync_mode != WB_SYNC_NONE) {
882 dout("waiting on writeback %p\n", page);
883 wait_on_page_writeback(page);
885 if (page_offset(page) >=
886 (snap_size == -1 ? i_size : snap_size)) {
887 dout("%p page eof %llu\n", page,
888 (snap_size == -1 ? i_size : snap_size));
893 if (PageWriteback(page)) {
894 dout("%p under writeback\n", page);
899 /* only if matching snap context */
900 pgsnapc = page_snap_context(page);
901 if (pgsnapc->seq > snapc->seq) {
902 dout("page snapc %p %lld > oldest %p %lld\n",
903 pgsnapc, pgsnapc->seq, snapc, snapc->seq);
906 continue; /* keep looking for snap */
910 if (!clear_page_dirty_for_io(page)) {
911 dout("%p !clear_page_dirty_for_io\n", page);
917 * We have something to write. If this is
918 * the first locked page this time through,
919 * calculate max possinle write size and
920 * allocate a page array
922 if (locked_pages == 0) {
926 /* prepare async write request */
927 offset = (u64)page_offset(page);
930 rc = ceph_calc_file_object_mapping(&ci->i_layout,
939 num_ops = 1 + do_sync;
940 strip_unit_end = page->index +
941 ((len - 1) >> PAGE_SHIFT);
944 max_pages = calc_pages_for(0, (u64)len);
945 pages = kmalloc(max_pages * sizeof (*pages),
948 pool = fsc->wb_pagevec_pool;
949 pages = mempool_alloc(pool, GFP_NOFS);
954 } else if (page->index !=
955 (offset + len) >> PAGE_SHIFT) {
956 if (num_ops >= (pool ? CEPH_OSD_SLAB_OPS :
958 redirty_page_for_writepage(wbc, page);
964 offset = (u64)page_offset(page);
968 /* note position of first page in pvec */
971 dout("%p will write page %p idx %lu\n",
972 inode, page, page->index);
974 if (atomic_long_inc_return(&fsc->writeback_count) >
975 CONGESTION_ON_THRESH(
976 fsc->mount_options->congestion_kb)) {
977 set_bdi_congested(inode_to_bdi(inode),
981 pages[locked_pages] = page;
986 /* did we get anything? */
988 goto release_pvec_pages;
991 BUG_ON(!locked_pages || first < 0);
993 if (pvec_pages && i == pvec_pages &&
994 locked_pages < max_pages) {
995 dout("reached end pvec, trying for more\n");
996 pagevec_reinit(&pvec);
1000 /* shift unused pages over in the pvec... we
1001 * will need to release them below. */
1002 for (j = i; j < pvec_pages; j++) {
1003 dout(" pvec leftover page %p\n", pvec.pages[j]);
1004 pvec.pages[j-i+first] = pvec.pages[j];
1010 offset = page_offset(pages[0]);
1013 req = ceph_osdc_new_request(&fsc->client->osdc,
1014 &ci->i_layout, vino,
1015 offset, &len, 0, num_ops,
1017 CEPH_OSD_FLAG_WRITE,
1018 snapc, truncate_seq,
1019 truncate_size, false);
1021 req = ceph_osdc_new_request(&fsc->client->osdc,
1022 &ci->i_layout, vino,
1027 CEPH_OSD_FLAG_WRITE,
1028 snapc, truncate_seq,
1029 truncate_size, true);
1030 BUG_ON(IS_ERR(req));
1032 BUG_ON(len < page_offset(pages[locked_pages - 1]) +
1033 PAGE_SIZE - offset);
1035 req->r_callback = writepages_finish;
1036 req->r_inode = inode;
1038 /* Format the osd request message and submit the write */
1042 for (i = 0; i < locked_pages; i++) {
1043 u64 cur_offset = page_offset(pages[i]);
1044 if (offset + len != cur_offset) {
1045 if (op_idx + do_sync + 1 == req->r_num_ops)
1047 osd_req_op_extent_dup_last(req, op_idx,
1048 cur_offset - offset);
1049 dout("writepages got pages at %llu~%llu\n",
1051 osd_req_op_extent_osd_data_pages(req, op_idx,
1054 osd_req_op_extent_update(req, op_idx, len);
1057 offset = cur_offset;
1058 data_pages = pages + i;
1062 set_page_writeback(pages[i]);
1066 if (snap_size != -1) {
1067 len = min(len, snap_size - offset);
1068 } else if (i == locked_pages) {
1069 /* writepages_finish() clears writeback pages
1070 * according to the data length, so make sure
1071 * data length covers all locked pages */
1072 u64 min_len = len + 1 - PAGE_SIZE;
1073 len = min(len, (u64)i_size_read(inode) - offset);
1074 len = max(len, min_len);
1076 dout("writepages got pages at %llu~%llu\n", offset, len);
1078 osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len,
1080 osd_req_op_extent_update(req, op_idx, len);
1084 osd_req_op_init(req, op_idx, CEPH_OSD_OP_STARTSYNC, 0);
1086 BUG_ON(op_idx + 1 != req->r_num_ops);
1089 if (i < locked_pages) {
1090 BUG_ON(num_ops <= req->r_num_ops);
1091 num_ops -= req->r_num_ops;
1095 /* allocate new pages array for next request */
1097 pages = kmalloc(locked_pages * sizeof (*pages),
1100 pool = fsc->wb_pagevec_pool;
1101 pages = mempool_alloc(pool, GFP_NOFS);
1104 memcpy(pages, data_pages + i,
1105 locked_pages * sizeof(*pages));
1106 memset(data_pages + i, 0,
1107 locked_pages * sizeof(*pages));
1109 BUG_ON(num_ops != req->r_num_ops);
1110 index = pages[i - 1]->index + 1;
1111 /* request message now owns the pages array */
1115 req->r_mtime = inode->i_mtime;
1116 rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
1120 wbc->nr_to_write -= i;
1124 if (wbc->nr_to_write <= 0)
1128 dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
1129 pvec.nr ? pvec.pages[0] : NULL);
1130 pagevec_release(&pvec);
1132 if (locked_pages && !done)
1136 if (should_loop && !done) {
1137 /* more to do; loop back to beginning of file */
1138 dout("writepages looping back to beginning of file\n");
1144 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1145 mapping->writeback_index = index;
1148 ceph_osdc_put_request(req);
1149 ceph_put_snap_context(snapc);
1150 dout("writepages done, rc = %d\n", rc);
1157 * See if a given @snapc is either writeable, or already written.
1159 static int context_is_writeable_or_written(struct inode *inode,
1160 struct ceph_snap_context *snapc)
1162 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL,
1164 int ret = !oldest || snapc->seq <= oldest->seq;
1166 ceph_put_snap_context(oldest);
1171 * We are only allowed to write into/dirty the page if the page is
1172 * clean, or already dirty within the same snap context.
1174 * called with page locked.
1175 * return success with page locked,
1176 * or any failure (incl -EAGAIN) with page unlocked.
1178 static int ceph_update_writeable_page(struct file *file,
1179 loff_t pos, unsigned len,
1182 struct inode *inode = file_inode(file);
1183 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1184 struct ceph_inode_info *ci = ceph_inode(inode);
1185 loff_t page_off = pos & PAGE_MASK;
1186 int pos_in_page = pos & ~PAGE_MASK;
1187 int end_in_page = pos_in_page + len;
1190 struct ceph_snap_context *snapc, *oldest;
1192 if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
1193 dout(" page %p forced umount\n", page);
1199 /* writepages currently holds page lock, but if we change that later, */
1200 wait_on_page_writeback(page);
1202 snapc = page_snap_context(page);
1203 if (snapc && snapc != ci->i_head_snapc) {
1205 * this page is already dirty in another (older) snap
1206 * context! is it writeable now?
1208 oldest = get_oldest_context(inode, NULL, NULL, NULL);
1210 if (snapc->seq > oldest->seq) {
1211 ceph_put_snap_context(oldest);
1212 dout(" page %p snapc %p not current or oldest\n",
1215 * queue for writeback, and wait for snapc to
1216 * be writeable or written
1218 snapc = ceph_get_snap_context(snapc);
1220 ceph_queue_writeback(inode);
1221 r = wait_event_killable(ci->i_cap_wq,
1222 context_is_writeable_or_written(inode, snapc));
1223 ceph_put_snap_context(snapc);
1224 if (r == -ERESTARTSYS)
1228 ceph_put_snap_context(oldest);
1230 /* yay, writeable, do it now (without dropping page lock) */
1231 dout(" page %p snapc %p not current, but oldest\n",
1233 if (!clear_page_dirty_for_io(page))
1235 r = writepage_nounlock(page, NULL);
1241 if (PageUptodate(page)) {
1242 dout(" page %p already uptodate\n", page);
1247 if (pos_in_page == 0 && len == PAGE_SIZE)
1250 /* past end of file? */
1251 i_size = i_size_read(inode);
1253 if (page_off >= i_size ||
1254 (pos_in_page == 0 && (pos+len) >= i_size &&
1255 end_in_page - pos_in_page != PAGE_SIZE)) {
1256 dout(" zeroing %p 0 - %d and %d - %d\n",
1257 page, pos_in_page, end_in_page, (int)PAGE_SIZE);
1258 zero_user_segments(page,
1260 end_in_page, PAGE_SIZE);
1264 /* we need to read it. */
1265 r = ceph_do_readpage(file, page);
1267 if (r == -EINPROGRESS)
1278 * We are only allowed to write into/dirty the page if the page is
1279 * clean, or already dirty within the same snap context.
1281 static int ceph_write_begin(struct file *file, struct address_space *mapping,
1282 loff_t pos, unsigned len, unsigned flags,
1283 struct page **pagep, void **fsdata)
1285 struct inode *inode = file_inode(file);
1287 pgoff_t index = pos >> PAGE_SHIFT;
1292 page = grab_cache_page_write_begin(mapping, index, 0);
1296 dout("write_begin file %p inode %p page %p %d~%d\n", file,
1297 inode, page, (int)pos, (int)len);
1299 r = ceph_update_writeable_page(file, pos, len, page);
1304 } while (r == -EAGAIN);
1310 * we don't do anything in here that simple_write_end doesn't do
1311 * except adjust dirty page accounting
1313 static int ceph_write_end(struct file *file, struct address_space *mapping,
1314 loff_t pos, unsigned len, unsigned copied,
1315 struct page *page, void *fsdata)
1317 struct inode *inode = file_inode(file);
1318 bool check_cap = false;
1320 dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
1321 inode, page, (int)pos, (int)copied, (int)len);
1323 /* zero the stale part of the page if we did a short copy */
1324 if (!PageUptodate(page)) {
1329 SetPageUptodate(page);
1332 /* did file size increase? */
1333 if (pos+copied > i_size_read(inode))
1334 check_cap = ceph_inode_set_size(inode, pos+copied);
1336 set_page_dirty(page);
1343 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
1349 * we set .direct_IO to indicate direct io is supported, but since we
1350 * intercept O_DIRECT reads and writes early, this function should
1353 static ssize_t ceph_direct_io(struct kiocb *iocb, struct iov_iter *iter)
1359 const struct address_space_operations ceph_aops = {
1360 .readpage = ceph_readpage,
1361 .readpages = ceph_readpages,
1362 .writepage = ceph_writepage,
1363 .writepages = ceph_writepages_start,
1364 .write_begin = ceph_write_begin,
1365 .write_end = ceph_write_end,
1366 .set_page_dirty = ceph_set_page_dirty,
1367 .invalidatepage = ceph_invalidatepage,
1368 .releasepage = ceph_releasepage,
1369 .direct_IO = ceph_direct_io,
1372 static void ceph_block_sigs(sigset_t *oldset)
1375 siginitsetinv(&mask, sigmask(SIGKILL));
1376 sigprocmask(SIG_BLOCK, &mask, oldset);
1379 static void ceph_restore_sigs(sigset_t *oldset)
1381 sigprocmask(SIG_SETMASK, oldset, NULL);
1387 static int ceph_filemap_fault(struct vm_fault *vmf)
1389 struct vm_area_struct *vma = vmf->vma;
1390 struct inode *inode = file_inode(vma->vm_file);
1391 struct ceph_inode_info *ci = ceph_inode(inode);
1392 struct ceph_file_info *fi = vma->vm_file->private_data;
1393 struct page *pinned_page = NULL;
1394 loff_t off = vmf->pgoff << PAGE_SHIFT;
1398 ceph_block_sigs(&oldset);
1400 dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n",
1401 inode, ceph_vinop(inode), off, (size_t)PAGE_SIZE);
1402 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1403 want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1405 want = CEPH_CAP_FILE_CACHE;
1408 ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, -1, &got, &pinned_page);
1412 dout("filemap_fault %p %llu~%zd got cap refs on %s\n",
1413 inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got));
1415 if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
1416 ci->i_inline_version == CEPH_INLINE_NONE) {
1417 current->journal_info = vma->vm_file;
1418 ret = filemap_fault(vmf);
1419 current->journal_info = NULL;
1423 dout("filemap_fault %p %llu~%zd dropping cap refs on %s ret %d\n",
1424 inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got), ret);
1426 put_page(pinned_page);
1427 ceph_put_cap_refs(ci, got);
1432 /* read inline data */
1433 if (off >= PAGE_SIZE) {
1434 /* does not support inline data > PAGE_SIZE */
1435 ret = VM_FAULT_SIGBUS;
1438 struct address_space *mapping = inode->i_mapping;
1439 struct page *page = find_or_create_page(mapping, 0,
1440 mapping_gfp_constraint(mapping,
1446 ret1 = __ceph_do_getattr(inode, page,
1447 CEPH_STAT_CAP_INLINE_DATA, true);
1448 if (ret1 < 0 || off >= i_size_read(inode)) {
1454 ret = VM_FAULT_SIGBUS;
1457 if (ret1 < PAGE_SIZE)
1458 zero_user_segment(page, ret1, PAGE_SIZE);
1460 flush_dcache_page(page);
1461 SetPageUptodate(page);
1463 ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
1465 dout("filemap_fault %p %llu~%zd read inline data ret %d\n",
1466 inode, off, (size_t)PAGE_SIZE, ret);
1469 ceph_restore_sigs(&oldset);
1471 ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
1477 * Reuse write_begin here for simplicity.
1479 static int ceph_page_mkwrite(struct vm_fault *vmf)
1481 struct vm_area_struct *vma = vmf->vma;
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,
1682 CEPH_OSD_OP_CREATE, CEPH_OSD_FLAG_WRITE,
1689 req->r_mtime = inode->i_mtime;
1690 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1692 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1693 ceph_osdc_put_request(req);
1697 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1698 ceph_vino(inode), 0, &len, 1, 3,
1699 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
1700 NULL, ci->i_truncate_seq,
1701 ci->i_truncate_size, false);
1707 osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false);
1710 __le64 xattr_buf = cpu_to_le64(inline_version);
1711 err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
1712 "inline_version", &xattr_buf,
1714 CEPH_OSD_CMPXATTR_OP_GT,
1715 CEPH_OSD_CMPXATTR_MODE_U64);
1722 int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
1723 "%llu", inline_version);
1724 err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
1726 xattr_buf, xattr_len, 0, 0);
1731 req->r_mtime = inode->i_mtime;
1732 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1734 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1736 ceph_osdc_put_request(req);
1737 if (err == -ECANCELED)
1740 if (page && page != locked_page) {
1741 if (from_pagecache) {
1745 __free_pages(page, 0);
1748 dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
1749 inode, ceph_vinop(inode), inline_version, err);
1753 static const struct vm_operations_struct ceph_vmops = {
1754 .fault = ceph_filemap_fault,
1755 .page_mkwrite = ceph_page_mkwrite,
1758 int ceph_mmap(struct file *file, struct vm_area_struct *vma)
1760 struct address_space *mapping = file->f_mapping;
1762 if (!mapping->a_ops->readpage)
1764 file_accessed(file);
1765 vma->vm_ops = &ceph_vmops;
1774 static int __ceph_pool_perm_get(struct ceph_inode_info *ci,
1775 s64 pool, struct ceph_string *pool_ns)
1777 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1778 struct ceph_mds_client *mdsc = fsc->mdsc;
1779 struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
1780 struct rb_node **p, *parent;
1781 struct ceph_pool_perm *perm;
1782 struct page **pages;
1784 int err = 0, err2 = 0, have = 0;
1786 down_read(&mdsc->pool_perm_rwsem);
1787 p = &mdsc->pool_perm_tree.rb_node;
1789 perm = rb_entry(*p, struct ceph_pool_perm, node);
1790 if (pool < perm->pool)
1792 else if (pool > perm->pool)
1793 p = &(*p)->rb_right;
1795 int ret = ceph_compare_string(pool_ns,
1801 p = &(*p)->rb_right;
1808 up_read(&mdsc->pool_perm_rwsem);
1813 dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n",
1814 pool, (int)pool_ns->len, pool_ns->str);
1816 dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool);
1818 down_write(&mdsc->pool_perm_rwsem);
1819 p = &mdsc->pool_perm_tree.rb_node;
1823 perm = rb_entry(parent, struct ceph_pool_perm, node);
1824 if (pool < perm->pool)
1826 else if (pool > perm->pool)
1827 p = &(*p)->rb_right;
1829 int ret = ceph_compare_string(pool_ns,
1835 p = &(*p)->rb_right;
1843 up_write(&mdsc->pool_perm_rwsem);
1847 rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
1848 1, false, GFP_NOFS);
1854 rd_req->r_flags = CEPH_OSD_FLAG_READ;
1855 osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
1856 rd_req->r_base_oloc.pool = pool;
1858 rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns);
1859 ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino);
1861 err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS);
1865 wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
1866 1, false, GFP_NOFS);
1872 wr_req->r_flags = CEPH_OSD_FLAG_WRITE;
1873 osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
1874 ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc);
1875 ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid);
1877 err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS);
1881 /* one page should be large enough for STAT data */
1882 pages = ceph_alloc_page_vector(1, GFP_KERNEL);
1883 if (IS_ERR(pages)) {
1884 err = PTR_ERR(pages);
1888 osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
1890 err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false);
1892 wr_req->r_mtime = ci->vfs_inode.i_mtime;
1893 wr_req->r_abort_on_full = true;
1894 err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false);
1897 err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
1899 err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
1901 if (err >= 0 || err == -ENOENT)
1903 else if (err != -EPERM)
1906 if (err2 == 0 || err2 == -EEXIST)
1908 else if (err2 != -EPERM) {
1913 pool_ns_len = pool_ns ? pool_ns->len : 0;
1914 perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS);
1922 perm->pool_ns_len = pool_ns_len;
1923 if (pool_ns_len > 0)
1924 memcpy(perm->pool_ns, pool_ns->str, pool_ns_len);
1925 perm->pool_ns[pool_ns_len] = 0;
1927 rb_link_node(&perm->node, parent, p);
1928 rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
1931 up_write(&mdsc->pool_perm_rwsem);
1933 ceph_osdc_put_request(rd_req);
1934 ceph_osdc_put_request(wr_req);
1939 dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n",
1940 pool, (int)pool_ns->len, pool_ns->str, err);
1942 dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err);
1946 int ceph_pool_perm_check(struct ceph_inode_info *ci, int need)
1949 struct ceph_string *pool_ns;
1952 if (ci->i_vino.snap != CEPH_NOSNAP) {
1954 * Pool permission check needs to write to the first object.
1955 * But for snapshot, head of the first object may have alread
1956 * been deleted. Skip check to avoid creating orphan object.
1961 if (ceph_test_mount_opt(ceph_inode_to_client(&ci->vfs_inode),
1965 spin_lock(&ci->i_ceph_lock);
1966 flags = ci->i_ceph_flags;
1967 pool = ci->i_layout.pool_id;
1968 spin_unlock(&ci->i_ceph_lock);
1970 if (flags & CEPH_I_POOL_PERM) {
1971 if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
1972 dout("ceph_pool_perm_check pool %lld no read perm\n",
1976 if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
1977 dout("ceph_pool_perm_check pool %lld no write perm\n",
1984 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
1985 ret = __ceph_pool_perm_get(ci, pool, pool_ns);
1986 ceph_put_string(pool_ns);
1990 flags = CEPH_I_POOL_PERM;
1991 if (ret & POOL_READ)
1992 flags |= CEPH_I_POOL_RD;
1993 if (ret & POOL_WRITE)
1994 flags |= CEPH_I_POOL_WR;
1996 spin_lock(&ci->i_ceph_lock);
1997 if (pool == ci->i_layout.pool_id &&
1998 pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
1999 ci->i_ceph_flags |= flags;
2001 pool = ci->i_layout.pool_id;
2002 flags = ci->i_ceph_flags;
2004 spin_unlock(&ci->i_ceph_lock);
2008 void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
2010 struct ceph_pool_perm *perm;
2013 while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
2014 n = rb_first(&mdsc->pool_perm_tree);
2015 perm = rb_entry(n, struct ceph_pool_perm, node);
2016 rb_erase(n, &mdsc->pool_perm_tree);