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 && fsc->mount_options->wsize < wsize)
783 wsize = fsc->mount_options->wsize;
784 if (wsize < PAGE_SIZE)
786 max_pages_ever = wsize >> PAGE_SHIFT;
788 pagevec_init(&pvec, 0);
790 /* where to start/end? */
791 if (wbc->range_cyclic) {
792 start = mapping->writeback_index; /* Start from prev offset */
794 dout(" cyclic, start at %lu\n", start);
796 start = wbc->range_start >> PAGE_SHIFT;
797 end = wbc->range_end >> PAGE_SHIFT;
798 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
801 dout(" not cyclic, %lu to %lu\n", start, end);
806 /* find oldest snap context with dirty data */
807 ceph_put_snap_context(snapc);
809 snapc = get_oldest_context(inode, &snap_size,
810 &truncate_size, &truncate_seq);
812 /* hmm, why does writepages get called when there
814 dout(" no snap context with dirty data?\n");
817 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
818 snapc, snapc->seq, snapc->num_snaps);
820 i_size = i_size_read(inode);
822 if (last_snapc && snapc != last_snapc) {
823 /* if we switched to a newer snapc, restart our scan at the
824 * start of the original file range. */
825 dout(" snapc differs from last pass, restarting at %lu\n",
831 while (!done && index <= end) {
834 pgoff_t strip_unit_end = 0;
835 int num_ops = 0, op_idx;
836 int pvec_pages, locked_pages = 0;
837 struct page **pages = NULL, **data_pages;
838 mempool_t *pool = NULL; /* Becomes non-null if mempool used */
841 u64 offset = 0, len = 0;
843 max_pages = max_pages_ever;
847 want = min(end - index,
848 min((pgoff_t)PAGEVEC_SIZE,
849 max_pages - (pgoff_t)locked_pages) - 1)
851 pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index,
854 dout("pagevec_lookup_tag got %d\n", pvec_pages);
855 if (!pvec_pages && !locked_pages)
857 for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
858 page = pvec.pages[i];
859 dout("? %p idx %lu\n", page, page->index);
860 if (locked_pages == 0)
861 lock_page(page); /* first page */
862 else if (!trylock_page(page))
865 /* only dirty pages, or our accounting breaks */
866 if (unlikely(!PageDirty(page)) ||
867 unlikely(page->mapping != mapping)) {
868 dout("!dirty or !mapping %p\n", page);
872 if (!wbc->range_cyclic && page->index > end) {
873 dout("end of range %p\n", page);
878 if (strip_unit_end && (page->index > strip_unit_end)) {
879 dout("end of strip unit %p\n", page);
883 if (wbc->sync_mode != WB_SYNC_NONE) {
884 dout("waiting on writeback %p\n", page);
885 wait_on_page_writeback(page);
887 if (page_offset(page) >=
888 (snap_size == -1 ? i_size : snap_size)) {
889 dout("%p page eof %llu\n", page,
890 (snap_size == -1 ? i_size : snap_size));
895 if (PageWriteback(page)) {
896 dout("%p under writeback\n", page);
901 /* only if matching snap context */
902 pgsnapc = page_snap_context(page);
903 if (pgsnapc->seq > snapc->seq) {
904 dout("page snapc %p %lld > oldest %p %lld\n",
905 pgsnapc, pgsnapc->seq, snapc, snapc->seq);
908 continue; /* keep looking for snap */
912 if (!clear_page_dirty_for_io(page)) {
913 dout("%p !clear_page_dirty_for_io\n", page);
919 * We have something to write. If this is
920 * the first locked page this time through,
921 * calculate max possinle write size and
922 * allocate a page array
924 if (locked_pages == 0) {
928 /* prepare async write request */
929 offset = (u64)page_offset(page);
932 rc = ceph_calc_file_object_mapping(&ci->i_layout,
941 num_ops = 1 + do_sync;
942 strip_unit_end = page->index +
943 ((len - 1) >> PAGE_SHIFT);
946 max_pages = calc_pages_for(0, (u64)len);
947 pages = kmalloc(max_pages * sizeof (*pages),
950 pool = fsc->wb_pagevec_pool;
951 pages = mempool_alloc(pool, GFP_NOFS);
956 } else if (page->index !=
957 (offset + len) >> PAGE_SHIFT) {
958 if (num_ops >= (pool ? CEPH_OSD_SLAB_OPS :
960 redirty_page_for_writepage(wbc, page);
966 offset = (u64)page_offset(page);
970 /* note position of first page in pvec */
973 dout("%p will write page %p idx %lu\n",
974 inode, page, page->index);
976 if (atomic_long_inc_return(&fsc->writeback_count) >
977 CONGESTION_ON_THRESH(
978 fsc->mount_options->congestion_kb)) {
979 set_bdi_congested(inode_to_bdi(inode),
983 pages[locked_pages] = page;
988 /* did we get anything? */
990 goto release_pvec_pages;
993 BUG_ON(!locked_pages || first < 0);
995 if (pvec_pages && i == pvec_pages &&
996 locked_pages < max_pages) {
997 dout("reached end pvec, trying for more\n");
998 pagevec_reinit(&pvec);
1002 /* shift unused pages over in the pvec... we
1003 * will need to release them below. */
1004 for (j = i; j < pvec_pages; j++) {
1005 dout(" pvec leftover page %p\n", pvec.pages[j]);
1006 pvec.pages[j-i+first] = pvec.pages[j];
1012 offset = page_offset(pages[0]);
1015 req = ceph_osdc_new_request(&fsc->client->osdc,
1016 &ci->i_layout, vino,
1017 offset, &len, 0, num_ops,
1019 CEPH_OSD_FLAG_WRITE,
1020 snapc, truncate_seq,
1021 truncate_size, false);
1023 req = ceph_osdc_new_request(&fsc->client->osdc,
1024 &ci->i_layout, vino,
1029 CEPH_OSD_FLAG_WRITE,
1030 snapc, truncate_seq,
1031 truncate_size, true);
1032 BUG_ON(IS_ERR(req));
1034 BUG_ON(len < page_offset(pages[locked_pages - 1]) +
1035 PAGE_SIZE - offset);
1037 req->r_callback = writepages_finish;
1038 req->r_inode = inode;
1040 /* Format the osd request message and submit the write */
1044 for (i = 0; i < locked_pages; i++) {
1045 u64 cur_offset = page_offset(pages[i]);
1046 if (offset + len != cur_offset) {
1047 if (op_idx + do_sync + 1 == req->r_num_ops)
1049 osd_req_op_extent_dup_last(req, op_idx,
1050 cur_offset - offset);
1051 dout("writepages got pages at %llu~%llu\n",
1053 osd_req_op_extent_osd_data_pages(req, op_idx,
1056 osd_req_op_extent_update(req, op_idx, len);
1059 offset = cur_offset;
1060 data_pages = pages + i;
1064 set_page_writeback(pages[i]);
1068 if (snap_size != -1) {
1069 len = min(len, snap_size - offset);
1070 } else if (i == locked_pages) {
1071 /* writepages_finish() clears writeback pages
1072 * according to the data length, so make sure
1073 * data length covers all locked pages */
1074 u64 min_len = len + 1 - PAGE_SIZE;
1075 len = min(len, (u64)i_size_read(inode) - offset);
1076 len = max(len, min_len);
1078 dout("writepages got pages at %llu~%llu\n", offset, len);
1080 osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len,
1082 osd_req_op_extent_update(req, op_idx, len);
1086 osd_req_op_init(req, op_idx, CEPH_OSD_OP_STARTSYNC, 0);
1088 BUG_ON(op_idx + 1 != req->r_num_ops);
1091 if (i < locked_pages) {
1092 BUG_ON(num_ops <= req->r_num_ops);
1093 num_ops -= req->r_num_ops;
1097 /* allocate new pages array for next request */
1099 pages = kmalloc(locked_pages * sizeof (*pages),
1102 pool = fsc->wb_pagevec_pool;
1103 pages = mempool_alloc(pool, GFP_NOFS);
1106 memcpy(pages, data_pages + i,
1107 locked_pages * sizeof(*pages));
1108 memset(data_pages + i, 0,
1109 locked_pages * sizeof(*pages));
1111 BUG_ON(num_ops != req->r_num_ops);
1112 index = pages[i - 1]->index + 1;
1113 /* request message now owns the pages array */
1117 req->r_mtime = inode->i_mtime;
1118 rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
1122 wbc->nr_to_write -= i;
1126 if (wbc->nr_to_write <= 0)
1130 dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
1131 pvec.nr ? pvec.pages[0] : NULL);
1132 pagevec_release(&pvec);
1134 if (locked_pages && !done)
1138 if (should_loop && !done) {
1139 /* more to do; loop back to beginning of file */
1140 dout("writepages looping back to beginning of file\n");
1146 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1147 mapping->writeback_index = index;
1150 ceph_osdc_put_request(req);
1151 ceph_put_snap_context(snapc);
1152 dout("writepages done, rc = %d\n", rc);
1159 * See if a given @snapc is either writeable, or already written.
1161 static int context_is_writeable_or_written(struct inode *inode,
1162 struct ceph_snap_context *snapc)
1164 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL,
1166 int ret = !oldest || snapc->seq <= oldest->seq;
1168 ceph_put_snap_context(oldest);
1173 * We are only allowed to write into/dirty the page if the page is
1174 * clean, or already dirty within the same snap context.
1176 * called with page locked.
1177 * return success with page locked,
1178 * or any failure (incl -EAGAIN) with page unlocked.
1180 static int ceph_update_writeable_page(struct file *file,
1181 loff_t pos, unsigned len,
1184 struct inode *inode = file_inode(file);
1185 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1186 struct ceph_inode_info *ci = ceph_inode(inode);
1187 loff_t page_off = pos & PAGE_MASK;
1188 int pos_in_page = pos & ~PAGE_MASK;
1189 int end_in_page = pos_in_page + len;
1192 struct ceph_snap_context *snapc, *oldest;
1194 if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
1195 dout(" page %p forced umount\n", page);
1201 /* writepages currently holds page lock, but if we change that later, */
1202 wait_on_page_writeback(page);
1204 snapc = page_snap_context(page);
1205 if (snapc && snapc != ci->i_head_snapc) {
1207 * this page is already dirty in another (older) snap
1208 * context! is it writeable now?
1210 oldest = get_oldest_context(inode, NULL, NULL, NULL);
1212 if (snapc->seq > oldest->seq) {
1213 ceph_put_snap_context(oldest);
1214 dout(" page %p snapc %p not current or oldest\n",
1217 * queue for writeback, and wait for snapc to
1218 * be writeable or written
1220 snapc = ceph_get_snap_context(snapc);
1222 ceph_queue_writeback(inode);
1223 r = wait_event_killable(ci->i_cap_wq,
1224 context_is_writeable_or_written(inode, snapc));
1225 ceph_put_snap_context(snapc);
1226 if (r == -ERESTARTSYS)
1230 ceph_put_snap_context(oldest);
1232 /* yay, writeable, do it now (without dropping page lock) */
1233 dout(" page %p snapc %p not current, but oldest\n",
1235 if (!clear_page_dirty_for_io(page))
1237 r = writepage_nounlock(page, NULL);
1243 if (PageUptodate(page)) {
1244 dout(" page %p already uptodate\n", page);
1249 if (pos_in_page == 0 && len == PAGE_SIZE)
1252 /* past end of file? */
1253 i_size = i_size_read(inode);
1255 if (page_off >= i_size ||
1256 (pos_in_page == 0 && (pos+len) >= i_size &&
1257 end_in_page - pos_in_page != PAGE_SIZE)) {
1258 dout(" zeroing %p 0 - %d and %d - %d\n",
1259 page, pos_in_page, end_in_page, (int)PAGE_SIZE);
1260 zero_user_segments(page,
1262 end_in_page, PAGE_SIZE);
1266 /* we need to read it. */
1267 r = ceph_do_readpage(file, page);
1269 if (r == -EINPROGRESS)
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);
1320 bool check_cap = false;
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_fault *vmf)
1391 struct vm_area_struct *vma = vmf->vma;
1392 struct inode *inode = file_inode(vma->vm_file);
1393 struct ceph_inode_info *ci = ceph_inode(inode);
1394 struct ceph_file_info *fi = vma->vm_file->private_data;
1395 struct page *pinned_page = NULL;
1396 loff_t off = vmf->pgoff << PAGE_SHIFT;
1400 ceph_block_sigs(&oldset);
1402 dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n",
1403 inode, ceph_vinop(inode), off, (size_t)PAGE_SIZE);
1404 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1405 want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1407 want = CEPH_CAP_FILE_CACHE;
1410 ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, -1, &got, &pinned_page);
1414 dout("filemap_fault %p %llu~%zd got cap refs on %s\n",
1415 inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got));
1417 if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
1418 ci->i_inline_version == CEPH_INLINE_NONE) {
1419 current->journal_info = vma->vm_file;
1420 ret = filemap_fault(vmf);
1421 current->journal_info = NULL;
1425 dout("filemap_fault %p %llu~%zd dropping cap refs on %s ret %d\n",
1426 inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got), ret);
1428 put_page(pinned_page);
1429 ceph_put_cap_refs(ci, got);
1434 /* read inline data */
1435 if (off >= PAGE_SIZE) {
1436 /* does not support inline data > PAGE_SIZE */
1437 ret = VM_FAULT_SIGBUS;
1440 struct address_space *mapping = inode->i_mapping;
1441 struct page *page = find_or_create_page(mapping, 0,
1442 mapping_gfp_constraint(mapping,
1448 ret1 = __ceph_do_getattr(inode, page,
1449 CEPH_STAT_CAP_INLINE_DATA, true);
1450 if (ret1 < 0 || off >= i_size_read(inode)) {
1456 ret = VM_FAULT_SIGBUS;
1459 if (ret1 < PAGE_SIZE)
1460 zero_user_segment(page, ret1, PAGE_SIZE);
1462 flush_dcache_page(page);
1463 SetPageUptodate(page);
1465 ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
1467 dout("filemap_fault %p %llu~%zd read inline data ret %d\n",
1468 inode, off, (size_t)PAGE_SIZE, ret);
1471 ceph_restore_sigs(&oldset);
1473 ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
1479 * Reuse write_begin here for simplicity.
1481 static int ceph_page_mkwrite(struct vm_fault *vmf)
1483 struct vm_area_struct *vma = vmf->vma;
1484 struct inode *inode = file_inode(vma->vm_file);
1485 struct ceph_inode_info *ci = ceph_inode(inode);
1486 struct ceph_file_info *fi = vma->vm_file->private_data;
1487 struct ceph_cap_flush *prealloc_cf;
1488 struct page *page = vmf->page;
1489 loff_t off = page_offset(page);
1490 loff_t size = i_size_read(inode);
1495 prealloc_cf = ceph_alloc_cap_flush();
1497 return VM_FAULT_OOM;
1499 ceph_block_sigs(&oldset);
1501 if (ci->i_inline_version != CEPH_INLINE_NONE) {
1502 struct page *locked_page = NULL;
1507 ret = ceph_uninline_data(vma->vm_file, locked_page);
1509 unlock_page(locked_page);
1514 if (off + PAGE_SIZE <= size)
1517 len = size & ~PAGE_MASK;
1519 dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
1520 inode, ceph_vinop(inode), off, len, size);
1521 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1522 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1524 want = CEPH_CAP_FILE_BUFFER;
1527 ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, off + len,
1532 dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
1533 inode, off, len, ceph_cap_string(got));
1535 /* Update time before taking page lock */
1536 file_update_time(vma->vm_file);
1541 if ((off > size) || (page->mapping != inode->i_mapping)) {
1543 ret = VM_FAULT_NOPAGE;
1547 ret = ceph_update_writeable_page(vma->vm_file, off, len, page);
1549 /* success. we'll keep the page locked. */
1550 set_page_dirty(page);
1551 ret = VM_FAULT_LOCKED;
1553 } while (ret == -EAGAIN);
1555 if (ret == VM_FAULT_LOCKED ||
1556 ci->i_inline_version != CEPH_INLINE_NONE) {
1558 spin_lock(&ci->i_ceph_lock);
1559 ci->i_inline_version = CEPH_INLINE_NONE;
1560 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1562 spin_unlock(&ci->i_ceph_lock);
1564 __mark_inode_dirty(inode, dirty);
1567 dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %d\n",
1568 inode, off, len, ceph_cap_string(got), ret);
1569 ceph_put_cap_refs(ci, got);
1571 ceph_restore_sigs(&oldset);
1572 ceph_free_cap_flush(prealloc_cf);
1574 ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
1578 void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
1579 char *data, size_t len)
1581 struct address_space *mapping = inode->i_mapping;
1587 if (i_size_read(inode) == 0)
1589 page = find_or_create_page(mapping, 0,
1590 mapping_gfp_constraint(mapping,
1594 if (PageUptodate(page)) {
1601 dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
1602 inode, ceph_vinop(inode), len, locked_page);
1605 void *kaddr = kmap_atomic(page);
1606 memcpy(kaddr, data, len);
1607 kunmap_atomic(kaddr);
1610 if (page != locked_page) {
1611 if (len < PAGE_SIZE)
1612 zero_user_segment(page, len, PAGE_SIZE);
1614 flush_dcache_page(page);
1616 SetPageUptodate(page);
1622 int ceph_uninline_data(struct file *filp, struct page *locked_page)
1624 struct inode *inode = file_inode(filp);
1625 struct ceph_inode_info *ci = ceph_inode(inode);
1626 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1627 struct ceph_osd_request *req;
1628 struct page *page = NULL;
1629 u64 len, inline_version;
1631 bool from_pagecache = false;
1633 spin_lock(&ci->i_ceph_lock);
1634 inline_version = ci->i_inline_version;
1635 spin_unlock(&ci->i_ceph_lock);
1637 dout("uninline_data %p %llx.%llx inline_version %llu\n",
1638 inode, ceph_vinop(inode), inline_version);
1640 if (inline_version == 1 || /* initial version, no data */
1641 inline_version == CEPH_INLINE_NONE)
1646 WARN_ON(!PageUptodate(page));
1647 } else if (ceph_caps_issued(ci) &
1648 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) {
1649 page = find_get_page(inode->i_mapping, 0);
1651 if (PageUptodate(page)) {
1652 from_pagecache = true;
1662 len = i_size_read(inode);
1663 if (len > PAGE_SIZE)
1666 page = __page_cache_alloc(GFP_NOFS);
1671 err = __ceph_do_getattr(inode, page,
1672 CEPH_STAT_CAP_INLINE_DATA, true);
1674 /* no inline data */
1675 if (err == -ENODATA)
1682 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1683 ceph_vino(inode), 0, &len, 0, 1,
1684 CEPH_OSD_OP_CREATE, CEPH_OSD_FLAG_WRITE,
1691 req->r_mtime = inode->i_mtime;
1692 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1694 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1695 ceph_osdc_put_request(req);
1699 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1700 ceph_vino(inode), 0, &len, 1, 3,
1701 CEPH_OSD_OP_WRITE, 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;
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 wr_req->r_abort_on_full = true;
1896 err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false);
1899 err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
1901 err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
1903 if (err >= 0 || err == -ENOENT)
1905 else if (err != -EPERM)
1908 if (err2 == 0 || err2 == -EEXIST)
1910 else if (err2 != -EPERM) {
1915 pool_ns_len = pool_ns ? pool_ns->len : 0;
1916 perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS);
1924 perm->pool_ns_len = pool_ns_len;
1925 if (pool_ns_len > 0)
1926 memcpy(perm->pool_ns, pool_ns->str, pool_ns_len);
1927 perm->pool_ns[pool_ns_len] = 0;
1929 rb_link_node(&perm->node, parent, p);
1930 rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
1933 up_write(&mdsc->pool_perm_rwsem);
1935 ceph_osdc_put_request(rd_req);
1936 ceph_osdc_put_request(wr_req);
1941 dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n",
1942 pool, (int)pool_ns->len, pool_ns->str, err);
1944 dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err);
1948 int ceph_pool_perm_check(struct ceph_inode_info *ci, int need)
1951 struct ceph_string *pool_ns;
1954 if (ci->i_vino.snap != CEPH_NOSNAP) {
1956 * Pool permission check needs to write to the first object.
1957 * But for snapshot, head of the first object may have alread
1958 * been deleted. Skip check to avoid creating orphan object.
1963 if (ceph_test_mount_opt(ceph_inode_to_client(&ci->vfs_inode),
1967 spin_lock(&ci->i_ceph_lock);
1968 flags = ci->i_ceph_flags;
1969 pool = ci->i_layout.pool_id;
1970 spin_unlock(&ci->i_ceph_lock);
1972 if (flags & CEPH_I_POOL_PERM) {
1973 if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
1974 dout("ceph_pool_perm_check pool %lld no read perm\n",
1978 if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
1979 dout("ceph_pool_perm_check pool %lld no write perm\n",
1986 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
1987 ret = __ceph_pool_perm_get(ci, pool, pool_ns);
1988 ceph_put_string(pool_ns);
1992 flags = CEPH_I_POOL_PERM;
1993 if (ret & POOL_READ)
1994 flags |= CEPH_I_POOL_RD;
1995 if (ret & POOL_WRITE)
1996 flags |= CEPH_I_POOL_WR;
1998 spin_lock(&ci->i_ceph_lock);
1999 if (pool == ci->i_layout.pool_id &&
2000 pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
2001 ci->i_ceph_flags |= flags;
2003 pool = ci->i_layout.pool_id;
2004 flags = ci->i_ceph_flags;
2006 spin_unlock(&ci->i_ceph_lock);
2010 void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
2012 struct ceph_pool_perm *perm;
2015 while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
2016 n = rb_first(&mdsc->pool_perm_tree);
2017 perm = rb_entry(n, struct ceph_pool_perm, node);
2018 rb_erase(n, &mdsc->pool_perm_tree);