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
)
281 if (bytes
< (int)PAGE_SIZE
) {
282 /* zero (remainder of) page */
283 int s
= bytes
< 0 ? 0 : bytes
;
284 zero_user_segment(page
, s
, PAGE_SIZE
);
286 dout("finish_read %p uptodate %p idx %lu\n", inode
, page
,
288 flush_dcache_page(page
);
289 SetPageUptodate(page
);
290 ceph_readpage_to_fscache(inode
, page
);
296 kfree(osd_data
->pages
);
299 static void ceph_unlock_page_vector(struct page
**pages
, int num_pages
)
303 for (i
= 0; i
< num_pages
; i
++)
304 unlock_page(pages
[i
]);
308 * start an async read(ahead) operation. return nr_pages we submitted
309 * a read for on success, or negative error code.
311 static int start_read(struct inode
*inode
, struct list_head
*page_list
, int max
)
313 struct ceph_osd_client
*osdc
=
314 &ceph_inode_to_client(inode
)->client
->osdc
;
315 struct ceph_inode_info
*ci
= ceph_inode(inode
);
316 struct page
*page
= list_entry(page_list
->prev
, struct page
, lru
);
317 struct ceph_vino vino
;
318 struct ceph_osd_request
*req
;
327 off
= (u64
) page_offset(page
);
330 next_index
= page
->index
;
331 list_for_each_entry_reverse(page
, page_list
, lru
) {
332 if (page
->index
!= next_index
)
336 if (max
&& nr_pages
== max
)
339 len
= nr_pages
<< PAGE_SHIFT
;
340 dout("start_read %p nr_pages %d is %lld~%lld\n", inode
, nr_pages
,
342 vino
= ceph_vino(inode
);
343 req
= ceph_osdc_new_request(osdc
, &ci
->i_layout
, vino
, off
, &len
,
344 0, 1, CEPH_OSD_OP_READ
,
345 CEPH_OSD_FLAG_READ
, NULL
,
346 ci
->i_truncate_seq
, ci
->i_truncate_size
,
351 /* build page vector */
352 nr_pages
= calc_pages_for(0, len
);
353 pages
= kmalloc(sizeof(*pages
) * nr_pages
, GFP_KERNEL
);
357 for (i
= 0; i
< nr_pages
; ++i
) {
358 page
= list_entry(page_list
->prev
, struct page
, lru
);
359 BUG_ON(PageLocked(page
));
360 list_del(&page
->lru
);
362 dout("start_read %p adding %p idx %lu\n", inode
, page
,
364 if (add_to_page_cache_lru(page
, &inode
->i_data
, page
->index
,
366 ceph_fscache_uncache_page(inode
, page
);
368 dout("start_read %p add_to_page_cache failed %p\n",
375 osd_req_op_extent_osd_data_pages(req
, 0, pages
, len
, 0, false, false);
376 req
->r_callback
= finish_read
;
377 req
->r_inode
= inode
;
379 dout("start_read %p starting %p %lld~%lld\n", inode
, req
, off
, len
);
380 ret
= ceph_osdc_start_request(osdc
, req
, false);
383 ceph_osdc_put_request(req
);
387 ceph_unlock_page_vector(pages
, nr_pages
);
388 ceph_release_page_vector(pages
, nr_pages
);
390 ceph_osdc_put_request(req
);
396 * Read multiple pages. Leave pages we don't read + unlock in page_list;
397 * the caller (VM) cleans them up.
399 static int ceph_readpages(struct file
*file
, struct address_space
*mapping
,
400 struct list_head
*page_list
, unsigned nr_pages
)
402 struct inode
*inode
= file_inode(file
);
403 struct ceph_fs_client
*fsc
= ceph_inode_to_client(inode
);
407 if (ceph_inode(inode
)->i_inline_version
!= CEPH_INLINE_NONE
)
410 rc
= ceph_readpages_from_fscache(mapping
->host
, mapping
, page_list
,
416 if (fsc
->mount_options
->rsize
>= PAGE_SIZE
)
417 max
= (fsc
->mount_options
->rsize
+ PAGE_SIZE
- 1)
420 dout("readpages %p file %p nr_pages %d max %d\n", inode
,
423 while (!list_empty(page_list
)) {
424 rc
= start_read(inode
, page_list
, max
);
430 ceph_fscache_readpages_cancel(inode
, page_list
);
432 dout("readpages %p file %p ret %d\n", inode
, file
, rc
);
437 * Get ref for the oldest snapc for an inode with dirty data... that is, the
438 * only snap context we are allowed to write back.
440 static struct ceph_snap_context
*get_oldest_context(struct inode
*inode
,
443 struct ceph_inode_info
*ci
= ceph_inode(inode
);
444 struct ceph_snap_context
*snapc
= NULL
;
445 struct ceph_cap_snap
*capsnap
= NULL
;
447 spin_lock(&ci
->i_ceph_lock
);
448 list_for_each_entry(capsnap
, &ci
->i_cap_snaps
, ci_item
) {
449 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap
,
450 capsnap
->context
, capsnap
->dirty_pages
);
451 if (capsnap
->dirty_pages
) {
452 snapc
= ceph_get_snap_context(capsnap
->context
);
454 *snap_size
= capsnap
->size
;
458 if (!snapc
&& ci
->i_wrbuffer_ref_head
) {
459 snapc
= ceph_get_snap_context(ci
->i_head_snapc
);
460 dout(" head snapc %p has %d dirty pages\n",
461 snapc
, ci
->i_wrbuffer_ref_head
);
463 spin_unlock(&ci
->i_ceph_lock
);
468 * Write a single page, but leave the page locked.
470 * If we get a write error, set the page error bit, but still adjust the
471 * dirty page accounting (i.e., page is no longer dirty).
473 static int writepage_nounlock(struct page
*page
, struct writeback_control
*wbc
)
476 struct ceph_inode_info
*ci
;
477 struct ceph_fs_client
*fsc
;
478 struct ceph_osd_client
*osdc
;
479 struct ceph_snap_context
*snapc
, *oldest
;
480 loff_t page_off
= page_offset(page
);
481 loff_t snap_size
= -1;
485 int err
= 0, len
= PAGE_SIZE
;
487 dout("writepage %p idx %lu\n", page
, page
->index
);
489 if (!page
->mapping
|| !page
->mapping
->host
) {
490 dout("writepage %p - no mapping\n", page
);
493 inode
= page
->mapping
->host
;
494 ci
= ceph_inode(inode
);
495 fsc
= ceph_inode_to_client(inode
);
496 osdc
= &fsc
->client
->osdc
;
498 /* verify this is a writeable snap context */
499 snapc
= page_snap_context(page
);
501 dout("writepage %p page %p not dirty?\n", inode
, page
);
504 oldest
= get_oldest_context(inode
, &snap_size
);
505 if (snapc
->seq
> oldest
->seq
) {
506 dout("writepage %p page %p snapc %p not writeable - noop\n",
508 /* we should only noop if called by kswapd */
509 WARN_ON((current
->flags
& PF_MEMALLOC
) == 0);
510 ceph_put_snap_context(oldest
);
513 ceph_put_snap_context(oldest
);
515 spin_lock(&ci
->i_ceph_lock
);
516 truncate_seq
= ci
->i_truncate_seq
;
517 truncate_size
= ci
->i_truncate_size
;
519 snap_size
= i_size_read(inode
);
520 spin_unlock(&ci
->i_ceph_lock
);
522 /* is this a partial page at end of file? */
523 if (page_off
>= snap_size
) {
524 dout("%p page eof %llu\n", page
, snap_size
);
527 if (snap_size
< page_off
+ len
)
528 len
= snap_size
- page_off
;
530 dout("writepage %p page %p index %lu on %llu~%u snapc %p\n",
531 inode
, page
, page
->index
, page_off
, len
, snapc
);
533 writeback_stat
= atomic_long_inc_return(&fsc
->writeback_count
);
535 CONGESTION_ON_THRESH(fsc
->mount_options
->congestion_kb
))
536 set_bdi_congested(&fsc
->backing_dev_info
, BLK_RW_ASYNC
);
538 ceph_readpage_to_fscache(inode
, page
);
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 dout("writepage setting page/mapping error %d %p\n", err
, page
);
549 mapping_set_error(&inode
->i_data
, err
);
551 wbc
->pages_skipped
++;
553 dout("writepage cleaned page %p\n", page
);
554 err
= 0; /* vfs expects us to return 0 */
557 ClearPagePrivate(page
);
558 end_page_writeback(page
);
559 ceph_put_wrbuffer_cap_refs(ci
, 1, snapc
);
560 ceph_put_snap_context(snapc
); /* page's reference */
565 static int ceph_writepage(struct page
*page
, struct writeback_control
*wbc
)
568 struct inode
*inode
= page
->mapping
->host
;
571 err
= writepage_nounlock(page
, wbc
);
579 * lame release_pages helper. release_pages() isn't exported to
582 static void ceph_release_pages(struct page
**pages
, int num
)
587 pagevec_init(&pvec
, 0);
588 for (i
= 0; i
< num
; i
++) {
589 if (pagevec_add(&pvec
, pages
[i
]) == 0)
590 pagevec_release(&pvec
);
592 pagevec_release(&pvec
);
596 * async writeback completion handler.
598 * If we get an error, set the mapping error bit, but not the individual
601 static void writepages_finish(struct ceph_osd_request
*req
)
603 struct inode
*inode
= req
->r_inode
;
604 struct ceph_inode_info
*ci
= ceph_inode(inode
);
605 struct ceph_osd_data
*osd_data
;
607 int num_pages
, total_pages
= 0;
609 int rc
= req
->r_result
;
610 struct ceph_snap_context
*snapc
= req
->r_snapc
;
611 struct address_space
*mapping
= inode
->i_mapping
;
612 struct ceph_fs_client
*fsc
= ceph_inode_to_client(inode
);
616 dout("writepages_finish %p rc %d\n", inode
, rc
);
618 mapping_set_error(mapping
, rc
);
621 * We lost the cache cap, need to truncate the page before
622 * it is unlocked, otherwise we'd truncate it later in the
623 * page truncation thread, possibly losing some data that
626 remove_page
= !(ceph_caps_issued(ci
) &
627 (CEPH_CAP_FILE_CACHE
|CEPH_CAP_FILE_LAZYIO
));
629 /* clean all pages */
630 for (i
= 0; i
< req
->r_num_ops
; i
++) {
631 if (req
->r_ops
[i
].op
!= CEPH_OSD_OP_WRITE
)
634 osd_data
= osd_req_op_extent_osd_data(req
, i
);
635 BUG_ON(osd_data
->type
!= CEPH_OSD_DATA_TYPE_PAGES
);
636 num_pages
= calc_pages_for((u64
)osd_data
->alignment
,
637 (u64
)osd_data
->length
);
638 total_pages
+= num_pages
;
639 for (j
= 0; j
< num_pages
; j
++) {
640 page
= osd_data
->pages
[j
];
642 WARN_ON(!PageUptodate(page
));
644 if (atomic_long_dec_return(&fsc
->writeback_count
) <
645 CONGESTION_OFF_THRESH(
646 fsc
->mount_options
->congestion_kb
))
647 clear_bdi_congested(&fsc
->backing_dev_info
,
650 ceph_put_snap_context(page_snap_context(page
));
652 ClearPagePrivate(page
);
653 dout("unlocking %p\n", page
);
654 end_page_writeback(page
);
657 generic_error_remove_page(inode
->i_mapping
,
662 dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n",
663 inode
, osd_data
->length
, rc
>= 0 ? num_pages
: 0);
665 ceph_release_pages(osd_data
->pages
, num_pages
);
668 ceph_put_wrbuffer_cap_refs(ci
, total_pages
, snapc
);
670 osd_data
= osd_req_op_extent_osd_data(req
, 0);
671 if (osd_data
->pages_from_pool
)
672 mempool_free(osd_data
->pages
,
673 ceph_sb_to_client(inode
->i_sb
)->wb_pagevec_pool
);
675 kfree(osd_data
->pages
);
676 ceph_osdc_put_request(req
);
680 * initiate async writeback
682 static int ceph_writepages_start(struct address_space
*mapping
,
683 struct writeback_control
*wbc
)
685 struct inode
*inode
= mapping
->host
;
686 struct ceph_inode_info
*ci
= ceph_inode(inode
);
687 struct ceph_fs_client
*fsc
= ceph_inode_to_client(inode
);
688 struct ceph_vino vino
= ceph_vino(inode
);
689 pgoff_t index
, start
, end
;
692 pgoff_t max_pages
= 0, max_pages_ever
= 0;
693 struct ceph_snap_context
*snapc
= NULL
, *last_snapc
= NULL
, *pgsnapc
;
697 unsigned wsize
= 1 << inode
->i_blkbits
;
698 struct ceph_osd_request
*req
= NULL
;
700 loff_t snap_size
, i_size
;
705 * Include a 'sync' in the OSD request if this is a data
706 * integrity write (e.g., O_SYNC write or fsync()), or if our
707 * cap is being revoked.
709 if ((wbc
->sync_mode
== WB_SYNC_ALL
) ||
710 ceph_caps_revoking(ci
, CEPH_CAP_FILE_BUFFER
))
712 dout("writepages_start %p dosync=%d (mode=%s)\n",
714 wbc
->sync_mode
== WB_SYNC_NONE
? "NONE" :
715 (wbc
->sync_mode
== WB_SYNC_ALL
? "ALL" : "HOLD"));
717 if (ACCESS_ONCE(fsc
->mount_state
) == CEPH_MOUNT_SHUTDOWN
) {
718 pr_warn("writepage_start %p on forced umount\n", inode
);
719 truncate_pagecache(inode
, 0);
720 mapping_set_error(mapping
, -EIO
);
721 return -EIO
; /* we're in a forced umount, don't write! */
723 if (fsc
->mount_options
->wsize
&& fsc
->mount_options
->wsize
< wsize
)
724 wsize
= fsc
->mount_options
->wsize
;
725 if (wsize
< PAGE_SIZE
)
727 max_pages_ever
= wsize
>> PAGE_SHIFT
;
729 pagevec_init(&pvec
, 0);
731 /* where to start/end? */
732 if (wbc
->range_cyclic
) {
733 start
= mapping
->writeback_index
; /* Start from prev offset */
735 dout(" cyclic, start at %lu\n", start
);
737 start
= wbc
->range_start
>> PAGE_SHIFT
;
738 end
= wbc
->range_end
>> PAGE_SHIFT
;
739 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
742 dout(" not cyclic, %lu to %lu\n", start
, end
);
747 /* find oldest snap context with dirty data */
748 ceph_put_snap_context(snapc
);
750 snapc
= get_oldest_context(inode
, &snap_size
);
752 /* hmm, why does writepages get called when there
754 dout(" no snap context with dirty data?\n");
757 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
758 snapc
, snapc
->seq
, snapc
->num_snaps
);
760 spin_lock(&ci
->i_ceph_lock
);
761 truncate_seq
= ci
->i_truncate_seq
;
762 truncate_size
= ci
->i_truncate_size
;
763 i_size
= i_size_read(inode
);
764 spin_unlock(&ci
->i_ceph_lock
);
766 if (last_snapc
&& snapc
!= last_snapc
) {
767 /* if we switched to a newer snapc, restart our scan at the
768 * start of the original file range. */
769 dout(" snapc differs from last pass, restarting at %lu\n",
775 while (!done
&& index
<= end
) {
778 pgoff_t strip_unit_end
= 0;
779 int num_ops
= 0, op_idx
;
780 int pvec_pages
, locked_pages
= 0;
781 struct page
**pages
= NULL
, **data_pages
;
782 mempool_t
*pool
= NULL
; /* Becomes non-null if mempool used */
785 u64 offset
= 0, len
= 0;
787 max_pages
= max_pages_ever
;
791 want
= min(end
- index
,
792 min((pgoff_t
)PAGEVEC_SIZE
,
793 max_pages
- (pgoff_t
)locked_pages
) - 1)
795 pvec_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
,
798 dout("pagevec_lookup_tag got %d\n", pvec_pages
);
799 if (!pvec_pages
&& !locked_pages
)
801 for (i
= 0; i
< pvec_pages
&& locked_pages
< max_pages
; i
++) {
802 page
= pvec
.pages
[i
];
803 dout("? %p idx %lu\n", page
, page
->index
);
804 if (locked_pages
== 0)
805 lock_page(page
); /* first page */
806 else if (!trylock_page(page
))
809 /* only dirty pages, or our accounting breaks */
810 if (unlikely(!PageDirty(page
)) ||
811 unlikely(page
->mapping
!= mapping
)) {
812 dout("!dirty or !mapping %p\n", page
);
816 if (!wbc
->range_cyclic
&& page
->index
> end
) {
817 dout("end of range %p\n", page
);
822 if (strip_unit_end
&& (page
->index
> strip_unit_end
)) {
823 dout("end of strip unit %p\n", page
);
827 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
828 dout("waiting on writeback %p\n", page
);
829 wait_on_page_writeback(page
);
831 if (page_offset(page
) >=
832 (snap_size
== -1 ? i_size
: snap_size
)) {
833 dout("%p page eof %llu\n", page
,
834 (snap_size
== -1 ? i_size
: snap_size
));
839 if (PageWriteback(page
)) {
840 dout("%p under writeback\n", page
);
845 /* only if matching snap context */
846 pgsnapc
= page_snap_context(page
);
847 if (pgsnapc
->seq
> snapc
->seq
) {
848 dout("page snapc %p %lld > oldest %p %lld\n",
849 pgsnapc
, pgsnapc
->seq
, snapc
, snapc
->seq
);
852 continue; /* keep looking for snap */
856 if (!clear_page_dirty_for_io(page
)) {
857 dout("%p !clear_page_dirty_for_io\n", page
);
863 * We have something to write. If this is
864 * the first locked page this time through,
865 * calculate max possinle write size and
866 * allocate a page array
868 if (locked_pages
== 0) {
872 /* prepare async write request */
873 offset
= (u64
)page_offset(page
);
876 rc
= ceph_calc_file_object_mapping(&ci
->i_layout
,
885 num_ops
= 1 + do_sync
;
886 strip_unit_end
= page
->index
+
887 ((len
- 1) >> PAGE_SHIFT
);
890 max_pages
= calc_pages_for(0, (u64
)len
);
891 pages
= kmalloc(max_pages
* sizeof (*pages
),
894 pool
= fsc
->wb_pagevec_pool
;
895 pages
= mempool_alloc(pool
, GFP_NOFS
);
900 } else if (page
->index
!=
901 (offset
+ len
) >> PAGE_SHIFT
) {
902 if (num_ops
>= (pool
? CEPH_OSD_SLAB_OPS
:
904 redirty_page_for_writepage(wbc
, page
);
910 offset
= (u64
)page_offset(page
);
914 /* note position of first page in pvec */
917 dout("%p will write page %p idx %lu\n",
918 inode
, page
, page
->index
);
920 if (atomic_long_inc_return(&fsc
->writeback_count
) >
921 CONGESTION_ON_THRESH(
922 fsc
->mount_options
->congestion_kb
)) {
923 set_bdi_congested(&fsc
->backing_dev_info
,
927 pages
[locked_pages
] = page
;
932 /* did we get anything? */
934 goto release_pvec_pages
;
937 BUG_ON(!locked_pages
|| first
< 0);
939 if (pvec_pages
&& i
== pvec_pages
&&
940 locked_pages
< max_pages
) {
941 dout("reached end pvec, trying for more\n");
942 pagevec_reinit(&pvec
);
946 /* shift unused pages over in the pvec... we
947 * will need to release them below. */
948 for (j
= i
; j
< pvec_pages
; j
++) {
949 dout(" pvec leftover page %p\n", pvec
.pages
[j
]);
950 pvec
.pages
[j
-i
+first
] = pvec
.pages
[j
];
956 offset
= page_offset(pages
[0]);
959 req
= ceph_osdc_new_request(&fsc
->client
->osdc
,
961 offset
, &len
, 0, num_ops
,
963 CEPH_OSD_FLAG_WRITE
|
964 CEPH_OSD_FLAG_ONDISK
,
966 truncate_size
, false);
968 req
= ceph_osdc_new_request(&fsc
->client
->osdc
,
974 CEPH_OSD_FLAG_WRITE
|
975 CEPH_OSD_FLAG_ONDISK
,
977 truncate_size
, true);
980 BUG_ON(len
< page_offset(pages
[locked_pages
- 1]) +
983 req
->r_callback
= writepages_finish
;
984 req
->r_inode
= inode
;
986 /* Format the osd request message and submit the write */
990 for (i
= 0; i
< locked_pages
; i
++) {
991 u64 cur_offset
= page_offset(pages
[i
]);
992 if (offset
+ len
!= cur_offset
) {
993 if (op_idx
+ do_sync
+ 1 == req
->r_num_ops
)
995 osd_req_op_extent_dup_last(req
, op_idx
,
996 cur_offset
- offset
);
997 dout("writepages got pages at %llu~%llu\n",
999 osd_req_op_extent_osd_data_pages(req
, op_idx
,
1002 osd_req_op_extent_update(req
, op_idx
, len
);
1005 offset
= cur_offset
;
1006 data_pages
= pages
+ i
;
1010 set_page_writeback(pages
[i
]);
1014 if (snap_size
!= -1) {
1015 len
= min(len
, snap_size
- offset
);
1016 } else if (i
== locked_pages
) {
1017 /* writepages_finish() clears writeback pages
1018 * according to the data length, so make sure
1019 * data length covers all locked pages */
1020 u64 min_len
= len
+ 1 - PAGE_SIZE
;
1021 len
= min(len
, (u64
)i_size_read(inode
) - offset
);
1022 len
= max(len
, min_len
);
1024 dout("writepages got pages at %llu~%llu\n", offset
, len
);
1026 osd_req_op_extent_osd_data_pages(req
, op_idx
, data_pages
, len
,
1028 osd_req_op_extent_update(req
, op_idx
, len
);
1032 osd_req_op_init(req
, op_idx
, CEPH_OSD_OP_STARTSYNC
, 0);
1034 BUG_ON(op_idx
+ 1 != req
->r_num_ops
);
1037 if (i
< locked_pages
) {
1038 BUG_ON(num_ops
<= req
->r_num_ops
);
1039 num_ops
-= req
->r_num_ops
;
1043 /* allocate new pages array for next request */
1045 pages
= kmalloc(locked_pages
* sizeof (*pages
),
1048 pool
= fsc
->wb_pagevec_pool
;
1049 pages
= mempool_alloc(pool
, GFP_NOFS
);
1052 memcpy(pages
, data_pages
+ i
,
1053 locked_pages
* sizeof(*pages
));
1054 memset(data_pages
+ i
, 0,
1055 locked_pages
* sizeof(*pages
));
1057 BUG_ON(num_ops
!= req
->r_num_ops
);
1058 index
= pages
[i
- 1]->index
+ 1;
1059 /* request message now owns the pages array */
1063 req
->r_mtime
= inode
->i_mtime
;
1064 rc
= ceph_osdc_start_request(&fsc
->client
->osdc
, req
, true);
1068 wbc
->nr_to_write
-= i
;
1072 if (wbc
->nr_to_write
<= 0)
1076 dout("pagevec_release on %d pages (%p)\n", (int)pvec
.nr
,
1077 pvec
.nr
? pvec
.pages
[0] : NULL
);
1078 pagevec_release(&pvec
);
1080 if (locked_pages
&& !done
)
1084 if (should_loop
&& !done
) {
1085 /* more to do; loop back to beginning of file */
1086 dout("writepages looping back to beginning of file\n");
1092 if (wbc
->range_cyclic
|| (range_whole
&& wbc
->nr_to_write
> 0))
1093 mapping
->writeback_index
= index
;
1096 ceph_osdc_put_request(req
);
1097 ceph_put_snap_context(snapc
);
1098 dout("writepages done, rc = %d\n", rc
);
1105 * See if a given @snapc is either writeable, or already written.
1107 static int context_is_writeable_or_written(struct inode
*inode
,
1108 struct ceph_snap_context
*snapc
)
1110 struct ceph_snap_context
*oldest
= get_oldest_context(inode
, NULL
);
1111 int ret
= !oldest
|| snapc
->seq
<= oldest
->seq
;
1113 ceph_put_snap_context(oldest
);
1118 * We are only allowed to write into/dirty the page if the page is
1119 * clean, or already dirty within the same snap context.
1121 * called with page locked.
1122 * return success with page locked,
1123 * or any failure (incl -EAGAIN) with page unlocked.
1125 static int ceph_update_writeable_page(struct file
*file
,
1126 loff_t pos
, unsigned len
,
1129 struct inode
*inode
= file_inode(file
);
1130 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1131 loff_t page_off
= pos
& PAGE_MASK
;
1132 int pos_in_page
= pos
& ~PAGE_MASK
;
1133 int end_in_page
= pos_in_page
+ len
;
1136 struct ceph_snap_context
*snapc
, *oldest
;
1139 /* writepages currently holds page lock, but if we change that later, */
1140 wait_on_page_writeback(page
);
1142 snapc
= page_snap_context(page
);
1143 if (snapc
&& snapc
!= ci
->i_head_snapc
) {
1145 * this page is already dirty in another (older) snap
1146 * context! is it writeable now?
1148 oldest
= get_oldest_context(inode
, NULL
);
1150 if (snapc
->seq
> oldest
->seq
) {
1151 ceph_put_snap_context(oldest
);
1152 dout(" page %p snapc %p not current or oldest\n",
1155 * queue for writeback, and wait for snapc to
1156 * be writeable or written
1158 snapc
= ceph_get_snap_context(snapc
);
1160 ceph_queue_writeback(inode
);
1161 r
= wait_event_interruptible(ci
->i_cap_wq
,
1162 context_is_writeable_or_written(inode
, snapc
));
1163 ceph_put_snap_context(snapc
);
1164 if (r
== -ERESTARTSYS
)
1168 ceph_put_snap_context(oldest
);
1170 /* yay, writeable, do it now (without dropping page lock) */
1171 dout(" page %p snapc %p not current, but oldest\n",
1173 if (!clear_page_dirty_for_io(page
))
1175 r
= writepage_nounlock(page
, NULL
);
1181 if (PageUptodate(page
)) {
1182 dout(" page %p already uptodate\n", page
);
1187 if (pos_in_page
== 0 && len
== PAGE_SIZE
)
1190 /* past end of file? */
1191 i_size
= i_size_read(inode
);
1193 if (page_off
>= i_size
||
1194 (pos_in_page
== 0 && (pos
+len
) >= i_size
&&
1195 end_in_page
- pos_in_page
!= PAGE_SIZE
)) {
1196 dout(" zeroing %p 0 - %d and %d - %d\n",
1197 page
, pos_in_page
, end_in_page
, (int)PAGE_SIZE
);
1198 zero_user_segments(page
,
1200 end_in_page
, PAGE_SIZE
);
1204 /* we need to read it. */
1205 r
= readpage_nounlock(file
, page
);
1215 * We are only allowed to write into/dirty the page if the page is
1216 * clean, or already dirty within the same snap context.
1218 static int ceph_write_begin(struct file
*file
, struct address_space
*mapping
,
1219 loff_t pos
, unsigned len
, unsigned flags
,
1220 struct page
**pagep
, void **fsdata
)
1222 struct inode
*inode
= file_inode(file
);
1224 pgoff_t index
= pos
>> PAGE_SHIFT
;
1229 page
= grab_cache_page_write_begin(mapping
, index
, 0);
1233 dout("write_begin file %p inode %p page %p %d~%d\n", file
,
1234 inode
, page
, (int)pos
, (int)len
);
1236 r
= ceph_update_writeable_page(file
, pos
, len
, page
);
1241 } while (r
== -EAGAIN
);
1247 * we don't do anything in here that simple_write_end doesn't do
1248 * except adjust dirty page accounting
1250 static int ceph_write_end(struct file
*file
, struct address_space
*mapping
,
1251 loff_t pos
, unsigned len
, unsigned copied
,
1252 struct page
*page
, void *fsdata
)
1254 struct inode
*inode
= file_inode(file
);
1255 unsigned from
= pos
& (PAGE_SIZE
- 1);
1258 dout("write_end file %p inode %p page %p %d~%d (%d)\n", file
,
1259 inode
, page
, (int)pos
, (int)copied
, (int)len
);
1261 /* zero the stale part of the page if we did a short copy */
1263 zero_user_segment(page
, from
+copied
, len
);
1265 /* did file size increase? */
1266 if (pos
+copied
> i_size_read(inode
))
1267 check_cap
= ceph_inode_set_size(inode
, pos
+copied
);
1269 if (!PageUptodate(page
))
1270 SetPageUptodate(page
);
1272 set_page_dirty(page
);
1278 ceph_check_caps(ceph_inode(inode
), CHECK_CAPS_AUTHONLY
, NULL
);
1284 * we set .direct_IO to indicate direct io is supported, but since we
1285 * intercept O_DIRECT reads and writes early, this function should
1288 static ssize_t
ceph_direct_io(struct kiocb
*iocb
, struct iov_iter
*iter
,
1295 const struct address_space_operations ceph_aops
= {
1296 .readpage
= ceph_readpage
,
1297 .readpages
= ceph_readpages
,
1298 .writepage
= ceph_writepage
,
1299 .writepages
= ceph_writepages_start
,
1300 .write_begin
= ceph_write_begin
,
1301 .write_end
= ceph_write_end
,
1302 .set_page_dirty
= ceph_set_page_dirty
,
1303 .invalidatepage
= ceph_invalidatepage
,
1304 .releasepage
= ceph_releasepage
,
1305 .direct_IO
= ceph_direct_io
,
1312 static int ceph_filemap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1314 struct inode
*inode
= file_inode(vma
->vm_file
);
1315 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1316 struct ceph_file_info
*fi
= vma
->vm_file
->private_data
;
1317 struct page
*pinned_page
= NULL
;
1318 loff_t off
= vmf
->pgoff
<< PAGE_SHIFT
;
1321 dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n",
1322 inode
, ceph_vinop(inode
), off
, (size_t)PAGE_SIZE
);
1323 if (fi
->fmode
& CEPH_FILE_MODE_LAZY
)
1324 want
= CEPH_CAP_FILE_CACHE
| CEPH_CAP_FILE_LAZYIO
;
1326 want
= CEPH_CAP_FILE_CACHE
;
1329 ret
= ceph_get_caps(ci
, CEPH_CAP_FILE_RD
, want
,
1330 -1, &got
, &pinned_page
);
1333 if (ret
!= -ERESTARTSYS
) {
1335 return VM_FAULT_SIGBUS
;
1338 dout("filemap_fault %p %llu~%zd got cap refs on %s\n",
1339 inode
, off
, (size_t)PAGE_SIZE
, ceph_cap_string(got
));
1341 if ((got
& (CEPH_CAP_FILE_CACHE
| CEPH_CAP_FILE_LAZYIO
)) ||
1342 ci
->i_inline_version
== CEPH_INLINE_NONE
)
1343 ret
= filemap_fault(vma
, vmf
);
1347 dout("filemap_fault %p %llu~%zd dropping cap refs on %s ret %d\n",
1348 inode
, off
, (size_t)PAGE_SIZE
, ceph_cap_string(got
), ret
);
1350 put_page(pinned_page
);
1351 ceph_put_cap_refs(ci
, got
);
1356 /* read inline data */
1357 if (off
>= PAGE_SIZE
) {
1358 /* does not support inline data > PAGE_SIZE */
1359 ret
= VM_FAULT_SIGBUS
;
1362 struct address_space
*mapping
= inode
->i_mapping
;
1363 struct page
*page
= find_or_create_page(mapping
, 0,
1364 mapping_gfp_constraint(mapping
,
1370 ret1
= __ceph_do_getattr(inode
, page
,
1371 CEPH_STAT_CAP_INLINE_DATA
, true);
1372 if (ret1
< 0 || off
>= i_size_read(inode
)) {
1375 ret
= VM_FAULT_SIGBUS
;
1378 if (ret1
< PAGE_SIZE
)
1379 zero_user_segment(page
, ret1
, PAGE_SIZE
);
1381 flush_dcache_page(page
);
1382 SetPageUptodate(page
);
1384 ret
= VM_FAULT_MAJOR
| VM_FAULT_LOCKED
;
1387 dout("filemap_fault %p %llu~%zd read inline data ret %d\n",
1388 inode
, off
, (size_t)PAGE_SIZE
, ret
);
1393 * Reuse write_begin here for simplicity.
1395 static int ceph_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1397 struct inode
*inode
= file_inode(vma
->vm_file
);
1398 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1399 struct ceph_file_info
*fi
= vma
->vm_file
->private_data
;
1400 struct ceph_cap_flush
*prealloc_cf
;
1401 struct page
*page
= vmf
->page
;
1402 loff_t off
= page_offset(page
);
1403 loff_t size
= i_size_read(inode
);
1407 prealloc_cf
= ceph_alloc_cap_flush();
1409 return VM_FAULT_SIGBUS
;
1411 if (ci
->i_inline_version
!= CEPH_INLINE_NONE
) {
1412 struct page
*locked_page
= NULL
;
1417 ret
= ceph_uninline_data(vma
->vm_file
, locked_page
);
1419 unlock_page(locked_page
);
1421 ret
= VM_FAULT_SIGBUS
;
1426 if (off
+ PAGE_SIZE
<= size
)
1429 len
= size
& ~PAGE_MASK
;
1431 dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
1432 inode
, ceph_vinop(inode
), off
, len
, size
);
1433 if (fi
->fmode
& CEPH_FILE_MODE_LAZY
)
1434 want
= CEPH_CAP_FILE_BUFFER
| CEPH_CAP_FILE_LAZYIO
;
1436 want
= CEPH_CAP_FILE_BUFFER
;
1439 ret
= ceph_get_caps(ci
, CEPH_CAP_FILE_WR
, want
, off
+ len
,
1443 if (ret
!= -ERESTARTSYS
) {
1445 ret
= VM_FAULT_SIGBUS
;
1449 dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
1450 inode
, off
, len
, ceph_cap_string(got
));
1452 /* Update time before taking page lock */
1453 file_update_time(vma
->vm_file
);
1457 ret
= VM_FAULT_NOPAGE
;
1459 (page
->mapping
!= inode
->i_mapping
)) {
1464 ret
= ceph_update_writeable_page(vma
->vm_file
, off
, len
, page
);
1466 /* success. we'll keep the page locked. */
1467 set_page_dirty(page
);
1468 ret
= VM_FAULT_LOCKED
;
1473 ret
= VM_FAULT_SIGBUS
;
1476 if (ret
== VM_FAULT_LOCKED
||
1477 ci
->i_inline_version
!= CEPH_INLINE_NONE
) {
1479 spin_lock(&ci
->i_ceph_lock
);
1480 ci
->i_inline_version
= CEPH_INLINE_NONE
;
1481 dirty
= __ceph_mark_dirty_caps(ci
, CEPH_CAP_FILE_WR
,
1483 spin_unlock(&ci
->i_ceph_lock
);
1485 __mark_inode_dirty(inode
, dirty
);
1488 dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %d\n",
1489 inode
, off
, len
, ceph_cap_string(got
), ret
);
1490 ceph_put_cap_refs(ci
, got
);
1492 ceph_free_cap_flush(prealloc_cf
);
1497 void ceph_fill_inline_data(struct inode
*inode
, struct page
*locked_page
,
1498 char *data
, size_t len
)
1500 struct address_space
*mapping
= inode
->i_mapping
;
1506 if (i_size_read(inode
) == 0)
1508 page
= find_or_create_page(mapping
, 0,
1509 mapping_gfp_constraint(mapping
,
1513 if (PageUptodate(page
)) {
1520 dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
1521 inode
, ceph_vinop(inode
), len
, locked_page
);
1524 void *kaddr
= kmap_atomic(page
);
1525 memcpy(kaddr
, data
, len
);
1526 kunmap_atomic(kaddr
);
1529 if (page
!= locked_page
) {
1530 if (len
< PAGE_SIZE
)
1531 zero_user_segment(page
, len
, PAGE_SIZE
);
1533 flush_dcache_page(page
);
1535 SetPageUptodate(page
);
1541 int ceph_uninline_data(struct file
*filp
, struct page
*locked_page
)
1543 struct inode
*inode
= file_inode(filp
);
1544 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1545 struct ceph_fs_client
*fsc
= ceph_inode_to_client(inode
);
1546 struct ceph_osd_request
*req
;
1547 struct page
*page
= NULL
;
1548 u64 len
, inline_version
;
1550 bool from_pagecache
= false;
1552 spin_lock(&ci
->i_ceph_lock
);
1553 inline_version
= ci
->i_inline_version
;
1554 spin_unlock(&ci
->i_ceph_lock
);
1556 dout("uninline_data %p %llx.%llx inline_version %llu\n",
1557 inode
, ceph_vinop(inode
), inline_version
);
1559 if (inline_version
== 1 || /* initial version, no data */
1560 inline_version
== CEPH_INLINE_NONE
)
1565 WARN_ON(!PageUptodate(page
));
1566 } else if (ceph_caps_issued(ci
) &
1567 (CEPH_CAP_FILE_CACHE
|CEPH_CAP_FILE_LAZYIO
)) {
1568 page
= find_get_page(inode
->i_mapping
, 0);
1570 if (PageUptodate(page
)) {
1571 from_pagecache
= true;
1581 len
= i_size_read(inode
);
1582 if (len
> PAGE_SIZE
)
1585 page
= __page_cache_alloc(GFP_NOFS
);
1590 err
= __ceph_do_getattr(inode
, page
,
1591 CEPH_STAT_CAP_INLINE_DATA
, true);
1593 /* no inline data */
1594 if (err
== -ENODATA
)
1601 req
= ceph_osdc_new_request(&fsc
->client
->osdc
, &ci
->i_layout
,
1602 ceph_vino(inode
), 0, &len
, 0, 1,
1604 CEPH_OSD_FLAG_ONDISK
| CEPH_OSD_FLAG_WRITE
,
1611 req
->r_mtime
= inode
->i_mtime
;
1612 err
= ceph_osdc_start_request(&fsc
->client
->osdc
, req
, false);
1614 err
= ceph_osdc_wait_request(&fsc
->client
->osdc
, req
);
1615 ceph_osdc_put_request(req
);
1619 req
= ceph_osdc_new_request(&fsc
->client
->osdc
, &ci
->i_layout
,
1620 ceph_vino(inode
), 0, &len
, 1, 3,
1622 CEPH_OSD_FLAG_ONDISK
| CEPH_OSD_FLAG_WRITE
,
1623 NULL
, ci
->i_truncate_seq
,
1624 ci
->i_truncate_size
, false);
1630 osd_req_op_extent_osd_data_pages(req
, 1, &page
, len
, 0, false, false);
1633 __le64 xattr_buf
= cpu_to_le64(inline_version
);
1634 err
= osd_req_op_xattr_init(req
, 0, CEPH_OSD_OP_CMPXATTR
,
1635 "inline_version", &xattr_buf
,
1637 CEPH_OSD_CMPXATTR_OP_GT
,
1638 CEPH_OSD_CMPXATTR_MODE_U64
);
1645 int xattr_len
= snprintf(xattr_buf
, sizeof(xattr_buf
),
1646 "%llu", inline_version
);
1647 err
= osd_req_op_xattr_init(req
, 2, CEPH_OSD_OP_SETXATTR
,
1649 xattr_buf
, xattr_len
, 0, 0);
1654 req
->r_mtime
= inode
->i_mtime
;
1655 err
= ceph_osdc_start_request(&fsc
->client
->osdc
, req
, false);
1657 err
= ceph_osdc_wait_request(&fsc
->client
->osdc
, req
);
1659 ceph_osdc_put_request(req
);
1660 if (err
== -ECANCELED
)
1663 if (page
&& page
!= locked_page
) {
1664 if (from_pagecache
) {
1668 __free_pages(page
, 0);
1671 dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
1672 inode
, ceph_vinop(inode
), inline_version
, err
);
1676 static const struct vm_operations_struct ceph_vmops
= {
1677 .fault
= ceph_filemap_fault
,
1678 .page_mkwrite
= ceph_page_mkwrite
,
1681 int ceph_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1683 struct address_space
*mapping
= file
->f_mapping
;
1685 if (!mapping
->a_ops
->readpage
)
1687 file_accessed(file
);
1688 vma
->vm_ops
= &ceph_vmops
;
1697 static int __ceph_pool_perm_get(struct ceph_inode_info
*ci
, u32 pool
)
1699 struct ceph_fs_client
*fsc
= ceph_inode_to_client(&ci
->vfs_inode
);
1700 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
1701 struct ceph_osd_request
*rd_req
= NULL
, *wr_req
= NULL
;
1702 struct rb_node
**p
, *parent
;
1703 struct ceph_pool_perm
*perm
;
1704 struct page
**pages
;
1705 int err
= 0, err2
= 0, have
= 0;
1707 down_read(&mdsc
->pool_perm_rwsem
);
1708 p
= &mdsc
->pool_perm_tree
.rb_node
;
1710 perm
= rb_entry(*p
, struct ceph_pool_perm
, node
);
1711 if (pool
< perm
->pool
)
1713 else if (pool
> perm
->pool
)
1714 p
= &(*p
)->rb_right
;
1720 up_read(&mdsc
->pool_perm_rwsem
);
1724 dout("__ceph_pool_perm_get pool %u no perm cached\n", pool
);
1726 down_write(&mdsc
->pool_perm_rwsem
);
1730 perm
= rb_entry(parent
, struct ceph_pool_perm
, node
);
1731 if (pool
< perm
->pool
)
1733 else if (pool
> perm
->pool
)
1734 p
= &(*p
)->rb_right
;
1741 up_write(&mdsc
->pool_perm_rwsem
);
1745 rd_req
= ceph_osdc_alloc_request(&fsc
->client
->osdc
, NULL
,
1746 1, false, GFP_NOFS
);
1752 rd_req
->r_flags
= CEPH_OSD_FLAG_READ
;
1753 osd_req_op_init(rd_req
, 0, CEPH_OSD_OP_STAT
, 0);
1754 rd_req
->r_base_oloc
.pool
= pool
;
1755 ceph_oid_printf(&rd_req
->r_base_oid
, "%llx.00000000", ci
->i_vino
.ino
);
1757 err
= ceph_osdc_alloc_messages(rd_req
, GFP_NOFS
);
1761 wr_req
= ceph_osdc_alloc_request(&fsc
->client
->osdc
, NULL
,
1762 1, false, GFP_NOFS
);
1768 wr_req
->r_flags
= CEPH_OSD_FLAG_WRITE
| CEPH_OSD_FLAG_ACK
;
1769 osd_req_op_init(wr_req
, 0, CEPH_OSD_OP_CREATE
, CEPH_OSD_OP_FLAG_EXCL
);
1770 ceph_oloc_copy(&wr_req
->r_base_oloc
, &rd_req
->r_base_oloc
);
1771 ceph_oid_copy(&wr_req
->r_base_oid
, &rd_req
->r_base_oid
);
1773 err
= ceph_osdc_alloc_messages(wr_req
, GFP_NOFS
);
1777 /* one page should be large enough for STAT data */
1778 pages
= ceph_alloc_page_vector(1, GFP_KERNEL
);
1779 if (IS_ERR(pages
)) {
1780 err
= PTR_ERR(pages
);
1784 osd_req_op_raw_data_in_pages(rd_req
, 0, pages
, PAGE_SIZE
,
1786 err
= ceph_osdc_start_request(&fsc
->client
->osdc
, rd_req
, false);
1788 wr_req
->r_mtime
= ci
->vfs_inode
.i_mtime
;
1789 err2
= ceph_osdc_start_request(&fsc
->client
->osdc
, wr_req
, false);
1792 err
= ceph_osdc_wait_request(&fsc
->client
->osdc
, rd_req
);
1794 err2
= ceph_osdc_wait_request(&fsc
->client
->osdc
, wr_req
);
1796 if (err
>= 0 || err
== -ENOENT
)
1798 else if (err
!= -EPERM
)
1801 if (err2
== 0 || err2
== -EEXIST
)
1803 else if (err2
!= -EPERM
) {
1808 perm
= kmalloc(sizeof(*perm
), GFP_NOFS
);
1816 rb_link_node(&perm
->node
, parent
, p
);
1817 rb_insert_color(&perm
->node
, &mdsc
->pool_perm_tree
);
1820 up_write(&mdsc
->pool_perm_rwsem
);
1822 ceph_osdc_put_request(rd_req
);
1823 ceph_osdc_put_request(wr_req
);
1827 dout("__ceph_pool_perm_get pool %u result = %d\n", pool
, err
);
1831 int ceph_pool_perm_check(struct ceph_inode_info
*ci
, int need
)
1836 /* does not support pool namespace yet */
1837 if (ci
->i_pool_ns_len
)
1840 if (ceph_test_mount_opt(ceph_inode_to_client(&ci
->vfs_inode
),
1844 spin_lock(&ci
->i_ceph_lock
);
1845 flags
= ci
->i_ceph_flags
;
1846 pool
= ceph_file_layout_pg_pool(ci
->i_layout
);
1847 spin_unlock(&ci
->i_ceph_lock
);
1849 if (flags
& CEPH_I_POOL_PERM
) {
1850 if ((need
& CEPH_CAP_FILE_RD
) && !(flags
& CEPH_I_POOL_RD
)) {
1851 dout("ceph_pool_perm_check pool %u no read perm\n",
1855 if ((need
& CEPH_CAP_FILE_WR
) && !(flags
& CEPH_I_POOL_WR
)) {
1856 dout("ceph_pool_perm_check pool %u no write perm\n",
1863 ret
= __ceph_pool_perm_get(ci
, pool
);
1867 flags
= CEPH_I_POOL_PERM
;
1868 if (ret
& POOL_READ
)
1869 flags
|= CEPH_I_POOL_RD
;
1870 if (ret
& POOL_WRITE
)
1871 flags
|= CEPH_I_POOL_WR
;
1873 spin_lock(&ci
->i_ceph_lock
);
1874 if (pool
== ceph_file_layout_pg_pool(ci
->i_layout
)) {
1875 ci
->i_ceph_flags
= flags
;
1877 pool
= ceph_file_layout_pg_pool(ci
->i_layout
);
1878 flags
= ci
->i_ceph_flags
;
1880 spin_unlock(&ci
->i_ceph_lock
);
1884 void ceph_pool_perm_destroy(struct ceph_mds_client
*mdsc
)
1886 struct ceph_pool_perm
*perm
;
1889 while (!RB_EMPTY_ROOT(&mdsc
->pool_perm_tree
)) {
1890 n
= rb_first(&mdsc
->pool_perm_tree
);
1891 perm
= rb_entry(n
, struct ceph_pool_perm
, node
);
1892 rb_erase(n
, &mdsc
->pool_perm_tree
);