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\n", page->mapping->host,
180 WARN_ON(PageDirty(page));
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 readpage_nounlock(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 = readpage_nounlock(filp, page);
258 * Finish an async read(ahead) op.
260 static void finish_read(struct ceph_osd_request *req)
262 struct inode *inode = req->r_inode;
263 struct ceph_osd_data *osd_data;
264 int rc = req->r_result <= 0 ? req->r_result : 0;
265 int bytes = req->r_result >= 0 ? req->r_result : 0;
269 dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes);
271 /* unlock all pages, zeroing any data we didn't read */
272 osd_data = osd_req_op_extent_osd_data(req, 0);
273 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
274 num_pages = calc_pages_for((u64)osd_data->alignment,
275 (u64)osd_data->length);
276 for (i = 0; i < num_pages; i++) {
277 struct page *page = osd_data->pages[i];
279 if (rc < 0 && rc != -ENOENT) {
280 ceph_fscache_readpage_cancel(inode, page);
283 if (bytes < (int)PAGE_SIZE) {
284 /* zero (remainder of) page */
285 int s = bytes < 0 ? 0 : bytes;
286 zero_user_segment(page, s, PAGE_SIZE);
288 dout("finish_read %p uptodate %p idx %lu\n", inode, page,
290 flush_dcache_page(page);
291 SetPageUptodate(page);
292 ceph_readpage_to_fscache(inode, page);
298 kfree(osd_data->pages);
301 static void ceph_unlock_page_vector(struct page **pages, int num_pages)
305 for (i = 0; i < num_pages; i++)
306 unlock_page(pages[i]);
310 * start an async read(ahead) operation. return nr_pages we submitted
311 * a read for on success, or negative error code.
313 static int start_read(struct inode *inode, struct list_head *page_list, int max)
315 struct ceph_osd_client *osdc =
316 &ceph_inode_to_client(inode)->client->osdc;
317 struct ceph_inode_info *ci = ceph_inode(inode);
318 struct page *page = list_entry(page_list->prev, struct page, lru);
319 struct ceph_vino vino;
320 struct ceph_osd_request *req;
329 off = (u64) page_offset(page);
332 next_index = page->index;
333 list_for_each_entry_reverse(page, page_list, lru) {
334 if (page->index != next_index)
338 if (max && nr_pages == max)
341 len = nr_pages << PAGE_SHIFT;
342 dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages,
344 vino = ceph_vino(inode);
345 req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len,
346 0, 1, CEPH_OSD_OP_READ,
347 CEPH_OSD_FLAG_READ, NULL,
348 ci->i_truncate_seq, ci->i_truncate_size,
353 /* build page vector */
354 nr_pages = calc_pages_for(0, len);
355 pages = kmalloc(sizeof(*pages) * nr_pages, GFP_KERNEL);
359 for (i = 0; i < nr_pages; ++i) {
360 page = list_entry(page_list->prev, struct page, lru);
361 BUG_ON(PageLocked(page));
362 list_del(&page->lru);
364 dout("start_read %p adding %p idx %lu\n", inode, page,
366 if (add_to_page_cache_lru(page, &inode->i_data, page->index,
368 ceph_fscache_uncache_page(inode, page);
370 dout("start_read %p add_to_page_cache failed %p\n",
377 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false);
378 req->r_callback = finish_read;
379 req->r_inode = inode;
381 dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len);
382 ret = ceph_osdc_start_request(osdc, req, false);
385 ceph_osdc_put_request(req);
389 ceph_unlock_page_vector(pages, nr_pages);
390 ceph_release_page_vector(pages, nr_pages);
392 ceph_osdc_put_request(req);
398 * Read multiple pages. Leave pages we don't read + unlock in page_list;
399 * the caller (VM) cleans them up.
401 static int ceph_readpages(struct file *file, struct address_space *mapping,
402 struct list_head *page_list, unsigned nr_pages)
404 struct inode *inode = file_inode(file);
405 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
409 if (ceph_inode(inode)->i_inline_version != CEPH_INLINE_NONE)
412 rc = ceph_readpages_from_fscache(mapping->host, mapping, page_list,
418 if (fsc->mount_options->rsize >= PAGE_SIZE)
419 max = (fsc->mount_options->rsize + PAGE_SIZE - 1)
422 dout("readpages %p file %p nr_pages %d max %d\n", inode,
425 while (!list_empty(page_list)) {
426 rc = start_read(inode, page_list, max);
432 ceph_fscache_readpages_cancel(inode, page_list);
434 dout("readpages %p file %p ret %d\n", inode, file, rc);
439 * Get ref for the oldest snapc for an inode with dirty data... that is, the
440 * only snap context we are allowed to write back.
442 static struct ceph_snap_context *get_oldest_context(struct inode *inode,
445 struct ceph_inode_info *ci = ceph_inode(inode);
446 struct ceph_snap_context *snapc = NULL;
447 struct ceph_cap_snap *capsnap = NULL;
449 spin_lock(&ci->i_ceph_lock);
450 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
451 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
452 capsnap->context, capsnap->dirty_pages);
453 if (capsnap->dirty_pages) {
454 snapc = ceph_get_snap_context(capsnap->context);
456 *snap_size = capsnap->size;
460 if (!snapc && ci->i_wrbuffer_ref_head) {
461 snapc = ceph_get_snap_context(ci->i_head_snapc);
462 dout(" head snapc %p has %d dirty pages\n",
463 snapc, ci->i_wrbuffer_ref_head);
465 spin_unlock(&ci->i_ceph_lock);
470 * Write a single page, but leave the page locked.
472 * If we get a write error, set the page error bit, but still adjust the
473 * dirty page accounting (i.e., page is no longer dirty).
475 static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
478 struct ceph_inode_info *ci;
479 struct ceph_fs_client *fsc;
480 struct ceph_osd_client *osdc;
481 struct ceph_snap_context *snapc, *oldest;
482 loff_t page_off = page_offset(page);
483 loff_t snap_size = -1;
487 int err = 0, len = PAGE_SIZE;
489 dout("writepage %p idx %lu\n", page, page->index);
491 if (!page->mapping || !page->mapping->host) {
492 dout("writepage %p - no mapping\n", page);
495 inode = page->mapping->host;
496 ci = ceph_inode(inode);
497 fsc = ceph_inode_to_client(inode);
498 osdc = &fsc->client->osdc;
500 /* verify this is a writeable snap context */
501 snapc = page_snap_context(page);
503 dout("writepage %p page %p not dirty?\n", inode, page);
506 oldest = get_oldest_context(inode, &snap_size);
507 if (snapc->seq > oldest->seq) {
508 dout("writepage %p page %p snapc %p not writeable - noop\n",
510 /* we should only noop if called by kswapd */
511 WARN_ON((current->flags & PF_MEMALLOC) == 0);
512 ceph_put_snap_context(oldest);
515 ceph_put_snap_context(oldest);
517 spin_lock(&ci->i_ceph_lock);
518 truncate_seq = ci->i_truncate_seq;
519 truncate_size = ci->i_truncate_size;
521 snap_size = i_size_read(inode);
522 spin_unlock(&ci->i_ceph_lock);
524 /* is this a partial page at end of file? */
525 if (page_off >= snap_size) {
526 dout("%p page eof %llu\n", page, snap_size);
529 if (snap_size < page_off + len)
530 len = snap_size - page_off;
532 dout("writepage %p page %p index %lu on %llu~%u snapc %p\n",
533 inode, page, page->index, page_off, len, snapc);
535 writeback_stat = atomic_long_inc_return(&fsc->writeback_count);
537 CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
538 set_bdi_congested(&fsc->backing_dev_info, BLK_RW_ASYNC);
540 set_page_writeback(page);
541 err = ceph_osdc_writepages(osdc, ceph_vino(inode),
542 &ci->i_layout, snapc,
544 truncate_seq, truncate_size,
545 &inode->i_mtime, &page, 1);
547 struct writeback_control tmp_wbc;
550 if (err == -ERESTARTSYS) {
551 /* killed by SIGKILL */
552 dout("writepage interrupted page %p\n", page);
553 redirty_page_for_writepage(wbc, page);
554 end_page_writeback(page);
557 dout("writepage setting page/mapping error %d %p\n",
560 mapping_set_error(&inode->i_data, err);
561 wbc->pages_skipped++;
563 dout("writepage cleaned page %p\n", page);
564 err = 0; /* vfs expects us to return 0 */
567 ClearPagePrivate(page);
568 end_page_writeback(page);
569 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
570 ceph_put_snap_context(snapc); /* page's reference */
575 static int ceph_writepage(struct page *page, struct writeback_control *wbc)
578 struct inode *inode = page->mapping->host;
581 err = writepage_nounlock(page, wbc);
582 if (err == -ERESTARTSYS) {
583 /* direct memory reclaimer was killed by SIGKILL. return 0
584 * to prevent caller from setting mapping/page error */
593 * lame release_pages helper. release_pages() isn't exported to
596 static void ceph_release_pages(struct page **pages, int num)
601 pagevec_init(&pvec, 0);
602 for (i = 0; i < num; i++) {
603 if (pagevec_add(&pvec, pages[i]) == 0)
604 pagevec_release(&pvec);
606 pagevec_release(&pvec);
610 * async writeback completion handler.
612 * If we get an error, set the mapping error bit, but not the individual
615 static void writepages_finish(struct ceph_osd_request *req)
617 struct inode *inode = req->r_inode;
618 struct ceph_inode_info *ci = ceph_inode(inode);
619 struct ceph_osd_data *osd_data;
621 int num_pages, total_pages = 0;
623 int rc = req->r_result;
624 struct ceph_snap_context *snapc = req->r_snapc;
625 struct address_space *mapping = inode->i_mapping;
626 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
629 dout("writepages_finish %p rc %d\n", inode, rc);
631 mapping_set_error(mapping, rc);
634 * We lost the cache cap, need to truncate the page before
635 * it is unlocked, otherwise we'd truncate it later in the
636 * page truncation thread, possibly losing some data that
639 remove_page = !(ceph_caps_issued(ci) &
640 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
642 /* clean all pages */
643 for (i = 0; i < req->r_num_ops; i++) {
644 if (req->r_ops[i].op != CEPH_OSD_OP_WRITE)
647 osd_data = osd_req_op_extent_osd_data(req, i);
648 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
649 num_pages = calc_pages_for((u64)osd_data->alignment,
650 (u64)osd_data->length);
651 total_pages += num_pages;
652 for (j = 0; j < num_pages; j++) {
653 page = osd_data->pages[j];
655 WARN_ON(!PageUptodate(page));
657 if (atomic_long_dec_return(&fsc->writeback_count) <
658 CONGESTION_OFF_THRESH(
659 fsc->mount_options->congestion_kb))
660 clear_bdi_congested(&fsc->backing_dev_info,
666 ceph_put_snap_context(page_snap_context(page));
668 ClearPagePrivate(page);
669 dout("unlocking %p\n", page);
670 end_page_writeback(page);
673 generic_error_remove_page(inode->i_mapping,
678 dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n",
679 inode, osd_data->length, rc >= 0 ? num_pages : 0);
681 ceph_release_pages(osd_data->pages, num_pages);
684 ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
686 osd_data = osd_req_op_extent_osd_data(req, 0);
687 if (osd_data->pages_from_pool)
688 mempool_free(osd_data->pages,
689 ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool);
691 kfree(osd_data->pages);
692 ceph_osdc_put_request(req);
696 * initiate async writeback
698 static int ceph_writepages_start(struct address_space *mapping,
699 struct writeback_control *wbc)
701 struct inode *inode = mapping->host;
702 struct ceph_inode_info *ci = ceph_inode(inode);
703 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
704 struct ceph_vino vino = ceph_vino(inode);
705 pgoff_t index, start, end;
708 pgoff_t max_pages = 0, max_pages_ever = 0;
709 struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
713 unsigned wsize = 1 << inode->i_blkbits;
714 struct ceph_osd_request *req = NULL;
716 loff_t snap_size, i_size;
721 * Include a 'sync' in the OSD request if this is a data
722 * integrity write (e.g., O_SYNC write or fsync()), or if our
723 * cap is being revoked.
725 if ((wbc->sync_mode == WB_SYNC_ALL) ||
726 ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER))
728 dout("writepages_start %p dosync=%d (mode=%s)\n",
730 wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
731 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
733 if (ACCESS_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
734 if (ci->i_wrbuffer_ref > 0) {
736 "writepage_start %p %lld forced umount\n",
737 inode, ceph_ino(inode));
739 mapping_set_error(mapping, -EIO);
740 return -EIO; /* we're in a forced umount, don't write! */
742 if (fsc->mount_options->wsize && fsc->mount_options->wsize < wsize)
743 wsize = fsc->mount_options->wsize;
744 if (wsize < PAGE_SIZE)
746 max_pages_ever = wsize >> PAGE_SHIFT;
748 pagevec_init(&pvec, 0);
750 /* where to start/end? */
751 if (wbc->range_cyclic) {
752 start = mapping->writeback_index; /* Start from prev offset */
754 dout(" cyclic, start at %lu\n", start);
756 start = wbc->range_start >> PAGE_SHIFT;
757 end = wbc->range_end >> PAGE_SHIFT;
758 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
761 dout(" not cyclic, %lu to %lu\n", start, end);
766 /* find oldest snap context with dirty data */
767 ceph_put_snap_context(snapc);
769 snapc = get_oldest_context(inode, &snap_size);
771 /* hmm, why does writepages get called when there
773 dout(" no snap context with dirty data?\n");
776 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
777 snapc, snapc->seq, snapc->num_snaps);
779 spin_lock(&ci->i_ceph_lock);
780 truncate_seq = ci->i_truncate_seq;
781 truncate_size = ci->i_truncate_size;
782 i_size = i_size_read(inode);
783 spin_unlock(&ci->i_ceph_lock);
785 if (last_snapc && snapc != last_snapc) {
786 /* if we switched to a newer snapc, restart our scan at the
787 * start of the original file range. */
788 dout(" snapc differs from last pass, restarting at %lu\n",
794 while (!done && index <= end) {
797 pgoff_t strip_unit_end = 0;
798 int num_ops = 0, op_idx;
799 int pvec_pages, locked_pages = 0;
800 struct page **pages = NULL, **data_pages;
801 mempool_t *pool = NULL; /* Becomes non-null if mempool used */
804 u64 offset = 0, len = 0;
806 max_pages = max_pages_ever;
810 want = min(end - index,
811 min((pgoff_t)PAGEVEC_SIZE,
812 max_pages - (pgoff_t)locked_pages) - 1)
814 pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index,
817 dout("pagevec_lookup_tag got %d\n", pvec_pages);
818 if (!pvec_pages && !locked_pages)
820 for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
821 page = pvec.pages[i];
822 dout("? %p idx %lu\n", page, page->index);
823 if (locked_pages == 0)
824 lock_page(page); /* first page */
825 else if (!trylock_page(page))
828 /* only dirty pages, or our accounting breaks */
829 if (unlikely(!PageDirty(page)) ||
830 unlikely(page->mapping != mapping)) {
831 dout("!dirty or !mapping %p\n", page);
835 if (!wbc->range_cyclic && page->index > end) {
836 dout("end of range %p\n", page);
841 if (strip_unit_end && (page->index > strip_unit_end)) {
842 dout("end of strip unit %p\n", page);
846 if (wbc->sync_mode != WB_SYNC_NONE) {
847 dout("waiting on writeback %p\n", page);
848 wait_on_page_writeback(page);
850 if (page_offset(page) >=
851 (snap_size == -1 ? i_size : snap_size)) {
852 dout("%p page eof %llu\n", page,
853 (snap_size == -1 ? i_size : snap_size));
858 if (PageWriteback(page)) {
859 dout("%p under writeback\n", page);
864 /* only if matching snap context */
865 pgsnapc = page_snap_context(page);
866 if (pgsnapc->seq > snapc->seq) {
867 dout("page snapc %p %lld > oldest %p %lld\n",
868 pgsnapc, pgsnapc->seq, snapc, snapc->seq);
871 continue; /* keep looking for snap */
875 if (!clear_page_dirty_for_io(page)) {
876 dout("%p !clear_page_dirty_for_io\n", page);
882 * We have something to write. If this is
883 * the first locked page this time through,
884 * calculate max possinle write size and
885 * allocate a page array
887 if (locked_pages == 0) {
891 /* prepare async write request */
892 offset = (u64)page_offset(page);
895 rc = ceph_calc_file_object_mapping(&ci->i_layout,
904 num_ops = 1 + do_sync;
905 strip_unit_end = page->index +
906 ((len - 1) >> PAGE_SHIFT);
909 max_pages = calc_pages_for(0, (u64)len);
910 pages = kmalloc(max_pages * sizeof (*pages),
913 pool = fsc->wb_pagevec_pool;
914 pages = mempool_alloc(pool, GFP_NOFS);
919 } else if (page->index !=
920 (offset + len) >> PAGE_SHIFT) {
921 if (num_ops >= (pool ? CEPH_OSD_SLAB_OPS :
923 redirty_page_for_writepage(wbc, page);
929 offset = (u64)page_offset(page);
933 /* note position of first page in pvec */
936 dout("%p will write page %p idx %lu\n",
937 inode, page, page->index);
939 if (atomic_long_inc_return(&fsc->writeback_count) >
940 CONGESTION_ON_THRESH(
941 fsc->mount_options->congestion_kb)) {
942 set_bdi_congested(&fsc->backing_dev_info,
946 pages[locked_pages] = page;
951 /* did we get anything? */
953 goto release_pvec_pages;
956 BUG_ON(!locked_pages || first < 0);
958 if (pvec_pages && i == pvec_pages &&
959 locked_pages < max_pages) {
960 dout("reached end pvec, trying for more\n");
961 pagevec_reinit(&pvec);
965 /* shift unused pages over in the pvec... we
966 * will need to release them below. */
967 for (j = i; j < pvec_pages; j++) {
968 dout(" pvec leftover page %p\n", pvec.pages[j]);
969 pvec.pages[j-i+first] = pvec.pages[j];
975 offset = page_offset(pages[0]);
978 req = ceph_osdc_new_request(&fsc->client->osdc,
980 offset, &len, 0, num_ops,
982 CEPH_OSD_FLAG_WRITE |
983 CEPH_OSD_FLAG_ONDISK,
985 truncate_size, false);
987 req = ceph_osdc_new_request(&fsc->client->osdc,
993 CEPH_OSD_FLAG_WRITE |
994 CEPH_OSD_FLAG_ONDISK,
996 truncate_size, true);
999 BUG_ON(len < page_offset(pages[locked_pages - 1]) +
1000 PAGE_SIZE - offset);
1002 req->r_callback = writepages_finish;
1003 req->r_inode = inode;
1005 /* Format the osd request message and submit the write */
1009 for (i = 0; i < locked_pages; i++) {
1010 u64 cur_offset = page_offset(pages[i]);
1011 if (offset + len != cur_offset) {
1012 if (op_idx + do_sync + 1 == req->r_num_ops)
1014 osd_req_op_extent_dup_last(req, op_idx,
1015 cur_offset - offset);
1016 dout("writepages got pages at %llu~%llu\n",
1018 osd_req_op_extent_osd_data_pages(req, op_idx,
1021 osd_req_op_extent_update(req, op_idx, len);
1024 offset = cur_offset;
1025 data_pages = pages + i;
1029 set_page_writeback(pages[i]);
1033 if (snap_size != -1) {
1034 len = min(len, snap_size - offset);
1035 } else if (i == locked_pages) {
1036 /* writepages_finish() clears writeback pages
1037 * according to the data length, so make sure
1038 * data length covers all locked pages */
1039 u64 min_len = len + 1 - PAGE_SIZE;
1040 len = min(len, (u64)i_size_read(inode) - offset);
1041 len = max(len, min_len);
1043 dout("writepages got pages at %llu~%llu\n", offset, len);
1045 osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len,
1047 osd_req_op_extent_update(req, op_idx, len);
1051 osd_req_op_init(req, op_idx, CEPH_OSD_OP_STARTSYNC, 0);
1053 BUG_ON(op_idx + 1 != req->r_num_ops);
1056 if (i < locked_pages) {
1057 BUG_ON(num_ops <= req->r_num_ops);
1058 num_ops -= req->r_num_ops;
1062 /* allocate new pages array for next request */
1064 pages = kmalloc(locked_pages * sizeof (*pages),
1067 pool = fsc->wb_pagevec_pool;
1068 pages = mempool_alloc(pool, GFP_NOFS);
1071 memcpy(pages, data_pages + i,
1072 locked_pages * sizeof(*pages));
1073 memset(data_pages + i, 0,
1074 locked_pages * sizeof(*pages));
1076 BUG_ON(num_ops != req->r_num_ops);
1077 index = pages[i - 1]->index + 1;
1078 /* request message now owns the pages array */
1082 req->r_mtime = inode->i_mtime;
1083 rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
1087 wbc->nr_to_write -= i;
1091 if (wbc->nr_to_write <= 0)
1095 dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
1096 pvec.nr ? pvec.pages[0] : NULL);
1097 pagevec_release(&pvec);
1099 if (locked_pages && !done)
1103 if (should_loop && !done) {
1104 /* more to do; loop back to beginning of file */
1105 dout("writepages looping back to beginning of file\n");
1111 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1112 mapping->writeback_index = index;
1115 ceph_osdc_put_request(req);
1116 ceph_put_snap_context(snapc);
1117 dout("writepages done, rc = %d\n", rc);
1124 * See if a given @snapc is either writeable, or already written.
1126 static int context_is_writeable_or_written(struct inode *inode,
1127 struct ceph_snap_context *snapc)
1129 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL);
1130 int ret = !oldest || snapc->seq <= oldest->seq;
1132 ceph_put_snap_context(oldest);
1137 * We are only allowed to write into/dirty the page if the page is
1138 * clean, or already dirty within the same snap context.
1140 * called with page locked.
1141 * return success with page locked,
1142 * or any failure (incl -EAGAIN) with page unlocked.
1144 static int ceph_update_writeable_page(struct file *file,
1145 loff_t pos, unsigned len,
1148 struct inode *inode = file_inode(file);
1149 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1150 struct ceph_inode_info *ci = ceph_inode(inode);
1151 loff_t page_off = pos & PAGE_MASK;
1152 int pos_in_page = pos & ~PAGE_MASK;
1153 int end_in_page = pos_in_page + len;
1156 struct ceph_snap_context *snapc, *oldest;
1158 if (ACCESS_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
1159 dout(" page %p forced umount\n", page);
1165 /* writepages currently holds page lock, but if we change that later, */
1166 wait_on_page_writeback(page);
1168 snapc = page_snap_context(page);
1169 if (snapc && snapc != ci->i_head_snapc) {
1171 * this page is already dirty in another (older) snap
1172 * context! is it writeable now?
1174 oldest = get_oldest_context(inode, NULL);
1176 if (snapc->seq > oldest->seq) {
1177 ceph_put_snap_context(oldest);
1178 dout(" page %p snapc %p not current or oldest\n",
1181 * queue for writeback, and wait for snapc to
1182 * be writeable or written
1184 snapc = ceph_get_snap_context(snapc);
1186 ceph_queue_writeback(inode);
1187 r = wait_event_killable(ci->i_cap_wq,
1188 context_is_writeable_or_written(inode, snapc));
1189 ceph_put_snap_context(snapc);
1190 if (r == -ERESTARTSYS)
1194 ceph_put_snap_context(oldest);
1196 /* yay, writeable, do it now (without dropping page lock) */
1197 dout(" page %p snapc %p not current, but oldest\n",
1199 if (!clear_page_dirty_for_io(page))
1201 r = writepage_nounlock(page, NULL);
1207 if (PageUptodate(page)) {
1208 dout(" page %p already uptodate\n", page);
1213 if (pos_in_page == 0 && len == PAGE_SIZE)
1216 /* past end of file? */
1217 i_size = i_size_read(inode);
1219 if (page_off >= i_size ||
1220 (pos_in_page == 0 && (pos+len) >= i_size &&
1221 end_in_page - pos_in_page != PAGE_SIZE)) {
1222 dout(" zeroing %p 0 - %d and %d - %d\n",
1223 page, pos_in_page, end_in_page, (int)PAGE_SIZE);
1224 zero_user_segments(page,
1226 end_in_page, PAGE_SIZE);
1230 /* we need to read it. */
1231 r = readpage_nounlock(file, page);
1241 * We are only allowed to write into/dirty the page if the page is
1242 * clean, or already dirty within the same snap context.
1244 static int ceph_write_begin(struct file *file, struct address_space *mapping,
1245 loff_t pos, unsigned len, unsigned flags,
1246 struct page **pagep, void **fsdata)
1248 struct inode *inode = file_inode(file);
1250 pgoff_t index = pos >> PAGE_SHIFT;
1255 page = grab_cache_page_write_begin(mapping, index, 0);
1259 dout("write_begin file %p inode %p page %p %d~%d\n", file,
1260 inode, page, (int)pos, (int)len);
1262 r = ceph_update_writeable_page(file, pos, len, page);
1267 } while (r == -EAGAIN);
1273 * we don't do anything in here that simple_write_end doesn't do
1274 * except adjust dirty page accounting
1276 static int ceph_write_end(struct file *file, struct address_space *mapping,
1277 loff_t pos, unsigned len, unsigned copied,
1278 struct page *page, void *fsdata)
1280 struct inode *inode = file_inode(file);
1281 unsigned from = pos & (PAGE_SIZE - 1);
1284 dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
1285 inode, page, (int)pos, (int)copied, (int)len);
1287 /* zero the stale part of the page if we did a short copy */
1289 zero_user_segment(page, from+copied, len);
1291 /* did file size increase? */
1292 if (pos+copied > i_size_read(inode))
1293 check_cap = ceph_inode_set_size(inode, pos+copied);
1295 if (!PageUptodate(page))
1296 SetPageUptodate(page);
1298 set_page_dirty(page);
1304 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
1310 * we set .direct_IO to indicate direct io is supported, but since we
1311 * intercept O_DIRECT reads and writes early, this function should
1314 static ssize_t ceph_direct_io(struct kiocb *iocb, struct iov_iter *iter)
1320 const struct address_space_operations ceph_aops = {
1321 .readpage = ceph_readpage,
1322 .readpages = ceph_readpages,
1323 .writepage = ceph_writepage,
1324 .writepages = ceph_writepages_start,
1325 .write_begin = ceph_write_begin,
1326 .write_end = ceph_write_end,
1327 .set_page_dirty = ceph_set_page_dirty,
1328 .invalidatepage = ceph_invalidatepage,
1329 .releasepage = ceph_releasepage,
1330 .direct_IO = ceph_direct_io,
1333 static void ceph_block_sigs(sigset_t *oldset)
1336 siginitsetinv(&mask, sigmask(SIGKILL));
1337 sigprocmask(SIG_BLOCK, &mask, oldset);
1340 static void ceph_restore_sigs(sigset_t *oldset)
1342 sigprocmask(SIG_SETMASK, oldset, NULL);
1348 static int ceph_filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1350 struct inode *inode = file_inode(vma->vm_file);
1351 struct ceph_inode_info *ci = ceph_inode(inode);
1352 struct ceph_file_info *fi = vma->vm_file->private_data;
1353 struct page *pinned_page = NULL;
1354 loff_t off = vmf->pgoff << PAGE_SHIFT;
1358 ceph_block_sigs(&oldset);
1360 dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n",
1361 inode, ceph_vinop(inode), off, (size_t)PAGE_SIZE);
1362 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1363 want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1365 want = CEPH_CAP_FILE_CACHE;
1368 ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, -1, &got, &pinned_page);
1372 dout("filemap_fault %p %llu~%zd got cap refs on %s\n",
1373 inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got));
1375 if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
1376 ci->i_inline_version == CEPH_INLINE_NONE)
1377 ret = filemap_fault(vma, vmf);
1381 dout("filemap_fault %p %llu~%zd dropping cap refs on %s ret %d\n",
1382 inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got), ret);
1384 put_page(pinned_page);
1385 ceph_put_cap_refs(ci, got);
1390 /* read inline data */
1391 if (off >= PAGE_SIZE) {
1392 /* does not support inline data > PAGE_SIZE */
1393 ret = VM_FAULT_SIGBUS;
1396 struct address_space *mapping = inode->i_mapping;
1397 struct page *page = find_or_create_page(mapping, 0,
1398 mapping_gfp_constraint(mapping,
1404 ret1 = __ceph_do_getattr(inode, page,
1405 CEPH_STAT_CAP_INLINE_DATA, true);
1406 if (ret1 < 0 || off >= i_size_read(inode)) {
1412 ret = VM_FAULT_SIGBUS;
1415 if (ret1 < PAGE_SIZE)
1416 zero_user_segment(page, ret1, PAGE_SIZE);
1418 flush_dcache_page(page);
1419 SetPageUptodate(page);
1421 ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
1423 dout("filemap_fault %p %llu~%zd read inline data ret %d\n",
1424 inode, off, (size_t)PAGE_SIZE, ret);
1427 ceph_restore_sigs(&oldset);
1429 ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
1435 * Reuse write_begin here for simplicity.
1437 static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1439 struct inode *inode = file_inode(vma->vm_file);
1440 struct ceph_inode_info *ci = ceph_inode(inode);
1441 struct ceph_file_info *fi = vma->vm_file->private_data;
1442 struct ceph_cap_flush *prealloc_cf;
1443 struct page *page = vmf->page;
1444 loff_t off = page_offset(page);
1445 loff_t size = i_size_read(inode);
1450 prealloc_cf = ceph_alloc_cap_flush();
1452 return VM_FAULT_OOM;
1454 ceph_block_sigs(&oldset);
1456 if (ci->i_inline_version != CEPH_INLINE_NONE) {
1457 struct page *locked_page = NULL;
1462 ret = ceph_uninline_data(vma->vm_file, locked_page);
1464 unlock_page(locked_page);
1469 if (off + PAGE_SIZE <= size)
1472 len = size & ~PAGE_MASK;
1474 dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
1475 inode, ceph_vinop(inode), off, len, size);
1476 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1477 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1479 want = CEPH_CAP_FILE_BUFFER;
1482 ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, off + len,
1487 dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
1488 inode, off, len, ceph_cap_string(got));
1490 /* Update time before taking page lock */
1491 file_update_time(vma->vm_file);
1496 if ((off > size) || (page->mapping != inode->i_mapping)) {
1498 ret = VM_FAULT_NOPAGE;
1502 ret = ceph_update_writeable_page(vma->vm_file, off, len, page);
1504 /* success. we'll keep the page locked. */
1505 set_page_dirty(page);
1506 ret = VM_FAULT_LOCKED;
1508 } while (ret == -EAGAIN);
1510 if (ret == VM_FAULT_LOCKED ||
1511 ci->i_inline_version != CEPH_INLINE_NONE) {
1513 spin_lock(&ci->i_ceph_lock);
1514 ci->i_inline_version = CEPH_INLINE_NONE;
1515 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1517 spin_unlock(&ci->i_ceph_lock);
1519 __mark_inode_dirty(inode, dirty);
1522 dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %d\n",
1523 inode, off, len, ceph_cap_string(got), ret);
1524 ceph_put_cap_refs(ci, got);
1526 ceph_restore_sigs(&oldset);
1527 ceph_free_cap_flush(prealloc_cf);
1529 ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
1533 void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
1534 char *data, size_t len)
1536 struct address_space *mapping = inode->i_mapping;
1542 if (i_size_read(inode) == 0)
1544 page = find_or_create_page(mapping, 0,
1545 mapping_gfp_constraint(mapping,
1549 if (PageUptodate(page)) {
1556 dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
1557 inode, ceph_vinop(inode), len, locked_page);
1560 void *kaddr = kmap_atomic(page);
1561 memcpy(kaddr, data, len);
1562 kunmap_atomic(kaddr);
1565 if (page != locked_page) {
1566 if (len < PAGE_SIZE)
1567 zero_user_segment(page, len, PAGE_SIZE);
1569 flush_dcache_page(page);
1571 SetPageUptodate(page);
1577 int ceph_uninline_data(struct file *filp, struct page *locked_page)
1579 struct inode *inode = file_inode(filp);
1580 struct ceph_inode_info *ci = ceph_inode(inode);
1581 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1582 struct ceph_osd_request *req;
1583 struct page *page = NULL;
1584 u64 len, inline_version;
1586 bool from_pagecache = false;
1588 spin_lock(&ci->i_ceph_lock);
1589 inline_version = ci->i_inline_version;
1590 spin_unlock(&ci->i_ceph_lock);
1592 dout("uninline_data %p %llx.%llx inline_version %llu\n",
1593 inode, ceph_vinop(inode), inline_version);
1595 if (inline_version == 1 || /* initial version, no data */
1596 inline_version == CEPH_INLINE_NONE)
1601 WARN_ON(!PageUptodate(page));
1602 } else if (ceph_caps_issued(ci) &
1603 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) {
1604 page = find_get_page(inode->i_mapping, 0);
1606 if (PageUptodate(page)) {
1607 from_pagecache = true;
1617 len = i_size_read(inode);
1618 if (len > PAGE_SIZE)
1621 page = __page_cache_alloc(GFP_NOFS);
1626 err = __ceph_do_getattr(inode, page,
1627 CEPH_STAT_CAP_INLINE_DATA, true);
1629 /* no inline data */
1630 if (err == -ENODATA)
1637 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1638 ceph_vino(inode), 0, &len, 0, 1,
1640 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
1647 req->r_mtime = inode->i_mtime;
1648 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1650 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1651 ceph_osdc_put_request(req);
1655 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1656 ceph_vino(inode), 0, &len, 1, 3,
1658 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
1659 NULL, ci->i_truncate_seq,
1660 ci->i_truncate_size, false);
1666 osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false);
1669 __le64 xattr_buf = cpu_to_le64(inline_version);
1670 err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
1671 "inline_version", &xattr_buf,
1673 CEPH_OSD_CMPXATTR_OP_GT,
1674 CEPH_OSD_CMPXATTR_MODE_U64);
1681 int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
1682 "%llu", inline_version);
1683 err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
1685 xattr_buf, xattr_len, 0, 0);
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);
1695 ceph_osdc_put_request(req);
1696 if (err == -ECANCELED)
1699 if (page && page != locked_page) {
1700 if (from_pagecache) {
1704 __free_pages(page, 0);
1707 dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
1708 inode, ceph_vinop(inode), inline_version, err);
1712 static const struct vm_operations_struct ceph_vmops = {
1713 .fault = ceph_filemap_fault,
1714 .page_mkwrite = ceph_page_mkwrite,
1717 int ceph_mmap(struct file *file, struct vm_area_struct *vma)
1719 struct address_space *mapping = file->f_mapping;
1721 if (!mapping->a_ops->readpage)
1723 file_accessed(file);
1724 vma->vm_ops = &ceph_vmops;
1733 static int __ceph_pool_perm_get(struct ceph_inode_info *ci,
1734 s64 pool, struct ceph_string *pool_ns)
1736 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1737 struct ceph_mds_client *mdsc = fsc->mdsc;
1738 struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
1739 struct rb_node **p, *parent;
1740 struct ceph_pool_perm *perm;
1741 struct page **pages;
1743 int err = 0, err2 = 0, have = 0;
1745 down_read(&mdsc->pool_perm_rwsem);
1746 p = &mdsc->pool_perm_tree.rb_node;
1748 perm = rb_entry(*p, struct ceph_pool_perm, node);
1749 if (pool < perm->pool)
1751 else if (pool > perm->pool)
1752 p = &(*p)->rb_right;
1754 int ret = ceph_compare_string(pool_ns,
1760 p = &(*p)->rb_right;
1767 up_read(&mdsc->pool_perm_rwsem);
1772 dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n",
1773 pool, (int)pool_ns->len, pool_ns->str);
1775 dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool);
1777 down_write(&mdsc->pool_perm_rwsem);
1778 p = &mdsc->pool_perm_tree.rb_node;
1782 perm = rb_entry(parent, struct ceph_pool_perm, node);
1783 if (pool < perm->pool)
1785 else if (pool > perm->pool)
1786 p = &(*p)->rb_right;
1788 int ret = ceph_compare_string(pool_ns,
1794 p = &(*p)->rb_right;
1802 up_write(&mdsc->pool_perm_rwsem);
1806 rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
1807 1, false, GFP_NOFS);
1813 rd_req->r_flags = CEPH_OSD_FLAG_READ;
1814 osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
1815 rd_req->r_base_oloc.pool = pool;
1817 rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns);
1818 ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino);
1820 err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS);
1824 wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
1825 1, false, GFP_NOFS);
1831 wr_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ACK;
1832 osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
1833 ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc);
1834 ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid);
1836 err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS);
1840 /* one page should be large enough for STAT data */
1841 pages = ceph_alloc_page_vector(1, GFP_KERNEL);
1842 if (IS_ERR(pages)) {
1843 err = PTR_ERR(pages);
1847 osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
1849 err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false);
1851 wr_req->r_mtime = ci->vfs_inode.i_mtime;
1852 err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false);
1855 err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
1857 err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
1859 if (err >= 0 || err == -ENOENT)
1861 else if (err != -EPERM)
1864 if (err2 == 0 || err2 == -EEXIST)
1866 else if (err2 != -EPERM) {
1871 pool_ns_len = pool_ns ? pool_ns->len : 0;
1872 perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS);
1880 perm->pool_ns_len = pool_ns_len;
1881 if (pool_ns_len > 0)
1882 memcpy(perm->pool_ns, pool_ns->str, pool_ns_len);
1883 perm->pool_ns[pool_ns_len] = 0;
1885 rb_link_node(&perm->node, parent, p);
1886 rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
1889 up_write(&mdsc->pool_perm_rwsem);
1891 ceph_osdc_put_request(rd_req);
1892 ceph_osdc_put_request(wr_req);
1897 dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n",
1898 pool, (int)pool_ns->len, pool_ns->str, err);
1900 dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err);
1904 int ceph_pool_perm_check(struct ceph_inode_info *ci, int need)
1907 struct ceph_string *pool_ns;
1910 if (ceph_test_mount_opt(ceph_inode_to_client(&ci->vfs_inode),
1914 spin_lock(&ci->i_ceph_lock);
1915 flags = ci->i_ceph_flags;
1916 pool = ci->i_layout.pool_id;
1917 spin_unlock(&ci->i_ceph_lock);
1919 if (flags & CEPH_I_POOL_PERM) {
1920 if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
1921 dout("ceph_pool_perm_check pool %lld no read perm\n",
1925 if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
1926 dout("ceph_pool_perm_check pool %lld no write perm\n",
1933 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
1934 ret = __ceph_pool_perm_get(ci, pool, pool_ns);
1935 ceph_put_string(pool_ns);
1939 flags = CEPH_I_POOL_PERM;
1940 if (ret & POOL_READ)
1941 flags |= CEPH_I_POOL_RD;
1942 if (ret & POOL_WRITE)
1943 flags |= CEPH_I_POOL_WR;
1945 spin_lock(&ci->i_ceph_lock);
1946 if (pool == ci->i_layout.pool_id &&
1947 pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
1948 ci->i_ceph_flags |= flags;
1950 pool = ci->i_layout.pool_id;
1951 flags = ci->i_ceph_flags;
1953 spin_unlock(&ci->i_ceph_lock);
1957 void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
1959 struct ceph_pool_perm *perm;
1962 while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
1963 n = rb_first(&mdsc->pool_perm_tree);
1964 perm = rb_entry(n, struct ceph_pool_perm, node);
1965 rb_erase(n, &mdsc->pool_perm_tree);