1 #include <linux/ceph/ceph_debug.h>
3 #include <linux/backing-dev.h>
6 #include <linux/pagemap.h>
7 #include <linux/writeback.h> /* generic_writepages */
8 #include <linux/slab.h>
9 #include <linux/pagevec.h>
10 #include <linux/task_io_accounting_ops.h>
13 #include "mds_client.h"
15 #include <linux/ceph/osd_client.h>
18 * Ceph address space ops.
20 * There are a few funny things going on here.
22 * The page->private field is used to reference a struct
23 * ceph_snap_context for _every_ dirty page. This indicates which
24 * snapshot the page was logically dirtied in, and thus which snap
25 * context needs to be associated with the osd write during writeback.
27 * Similarly, struct ceph_inode_info maintains a set of counters to
28 * count dirty pages on the inode. In the absence of snapshots,
29 * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count.
31 * When a snapshot is taken (that is, when the client receives
32 * notification that a snapshot was taken), each inode with caps and
33 * with dirty pages (dirty pages implies there is a cap) gets a new
34 * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending
35 * order, new snaps go to the tail). The i_wrbuffer_ref_head count is
36 * moved to capsnap->dirty. (Unless a sync write is currently in
37 * progress. In that case, the capsnap is said to be "pending", new
38 * writes cannot start, and the capsnap isn't "finalized" until the
39 * write completes (or fails) and a final size/mtime for the inode for
40 * that snap can be settled upon.) i_wrbuffer_ref_head is reset to 0.
42 * On writeback, we must submit writes to the osd IN SNAP ORDER. So,
43 * we look for the first capsnap in i_cap_snaps and write out pages in
44 * that snap context _only_. Then we move on to the next capsnap,
45 * eventually reaching the "live" or "head" context (i.e., pages that
46 * are not yet snapped) and are writing the most recently dirtied
49 * Invalidate and so forth must take care to ensure the dirty page
50 * accounting is preserved.
53 #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10))
54 #define CONGESTION_OFF_THRESH(congestion_kb) \
55 (CONGESTION_ON_THRESH(congestion_kb) - \
56 (CONGESTION_ON_THRESH(congestion_kb) >> 2))
58 static inline struct ceph_snap_context
*page_snap_context(struct page
*page
)
60 if (PagePrivate(page
))
61 return (void *)page
->private;
66 * Dirty a page. Optimistically adjust accounting, on the assumption
67 * that we won't race with invalidate. If we do, readjust.
69 static int ceph_set_page_dirty(struct page
*page
)
71 struct address_space
*mapping
= page
->mapping
;
73 struct ceph_inode_info
*ci
;
74 struct ceph_snap_context
*snapc
;
77 if (unlikely(!mapping
))
78 return !TestSetPageDirty(page
);
80 if (PageDirty(page
)) {
81 dout("%p set_page_dirty %p idx %lu -- already dirty\n",
82 mapping
->host
, page
, page
->index
);
83 BUG_ON(!PagePrivate(page
));
87 inode
= mapping
->host
;
88 ci
= ceph_inode(inode
);
91 spin_lock(&ci
->i_ceph_lock
);
92 BUG_ON(ci
->i_wr_ref
== 0); // caller should hold Fw reference
93 if (__ceph_have_pending_cap_snap(ci
)) {
94 struct ceph_cap_snap
*capsnap
=
95 list_last_entry(&ci
->i_cap_snaps
,
98 snapc
= ceph_get_snap_context(capsnap
->context
);
99 capsnap
->dirty_pages
++;
101 BUG_ON(!ci
->i_head_snapc
);
102 snapc
= ceph_get_snap_context(ci
->i_head_snapc
);
103 ++ci
->i_wrbuffer_ref_head
;
105 if (ci
->i_wrbuffer_ref
== 0)
107 ++ci
->i_wrbuffer_ref
;
108 dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d "
109 "snapc %p seq %lld (%d snaps)\n",
110 mapping
->host
, page
, page
->index
,
111 ci
->i_wrbuffer_ref
-1, ci
->i_wrbuffer_ref_head
-1,
112 ci
->i_wrbuffer_ref
, ci
->i_wrbuffer_ref_head
,
113 snapc
, snapc
->seq
, snapc
->num_snaps
);
114 spin_unlock(&ci
->i_ceph_lock
);
117 * Reference snap context in page->private. Also set
118 * PagePrivate so that we get invalidatepage callback.
120 BUG_ON(PagePrivate(page
));
121 page
->private = (unsigned long)snapc
;
122 SetPagePrivate(page
);
124 ret
= __set_page_dirty_nobuffers(page
);
125 WARN_ON(!PageLocked(page
));
126 WARN_ON(!page
->mapping
);
132 * If we are truncating the full page (i.e. offset == 0), adjust the
133 * dirty page counters appropriately. Only called if there is private
136 static void ceph_invalidatepage(struct page
*page
, unsigned int offset
,
140 struct ceph_inode_info
*ci
;
141 struct ceph_snap_context
*snapc
= page_snap_context(page
);
143 inode
= page
->mapping
->host
;
144 ci
= ceph_inode(inode
);
146 if (offset
!= 0 || length
!= PAGE_SIZE
) {
147 dout("%p invalidatepage %p idx %lu partial dirty page %u~%u\n",
148 inode
, page
, page
->index
, offset
, length
);
152 ceph_invalidate_fscache_page(inode
, page
);
154 if (!PagePrivate(page
))
158 * We can get non-dirty pages here due to races between
159 * set_page_dirty and truncate_complete_page; just spit out a
160 * warning, in case we end up with accounting problems later.
162 if (!PageDirty(page
))
163 pr_err("%p invalidatepage %p page not dirty\n", inode
, page
);
165 ClearPageChecked(page
);
167 dout("%p invalidatepage %p idx %lu full dirty page\n",
168 inode
, page
, page
->index
);
170 ceph_put_wrbuffer_cap_refs(ci
, 1, snapc
);
171 ceph_put_snap_context(snapc
);
173 ClearPagePrivate(page
);
176 static int ceph_releasepage(struct page
*page
, gfp_t g
)
178 dout("%p releasepage %p idx %lu (%sdirty)\n", page
->mapping
->host
,
179 page
, page
->index
, PageDirty(page
) ? "" : "not ");
181 /* Can we release the page from the cache? */
182 if (!ceph_release_fscache_page(page
, g
))
185 return !PagePrivate(page
);
189 * read a single page, without unlocking it.
191 static int readpage_nounlock(struct file
*filp
, struct page
*page
)
193 struct inode
*inode
= file_inode(filp
);
194 struct ceph_inode_info
*ci
= ceph_inode(inode
);
195 struct ceph_osd_client
*osdc
=
196 &ceph_inode_to_client(inode
)->client
->osdc
;
198 u64 off
= page_offset(page
);
201 if (off
>= i_size_read(inode
)) {
202 zero_user_segment(page
, 0, PAGE_SIZE
);
203 SetPageUptodate(page
);
207 if (ci
->i_inline_version
!= CEPH_INLINE_NONE
) {
209 * Uptodate inline data should have been added
210 * into page cache while getting Fcr caps.
214 zero_user_segment(page
, 0, PAGE_SIZE
);
215 SetPageUptodate(page
);
219 err
= ceph_readpage_from_fscache(inode
, page
);
223 dout("readpage inode %p file %p page %p index %lu\n",
224 inode
, filp
, page
, page
->index
);
225 err
= ceph_osdc_readpages(osdc
, ceph_vino(inode
), &ci
->i_layout
,
227 ci
->i_truncate_seq
, ci
->i_truncate_size
,
233 ceph_fscache_readpage_cancel(inode
, page
);
237 /* zero fill remainder of page */
238 zero_user_segment(page
, err
, PAGE_SIZE
);
240 flush_dcache_page(page
);
242 SetPageUptodate(page
);
243 ceph_readpage_to_fscache(inode
, page
);
246 return err
< 0 ? err
: 0;
249 static int ceph_readpage(struct file
*filp
, struct page
*page
)
251 int r
= readpage_nounlock(filp
, page
);
257 * Finish an async read(ahead) op.
259 static void finish_read(struct ceph_osd_request
*req
)
261 struct inode
*inode
= req
->r_inode
;
262 struct ceph_osd_data
*osd_data
;
263 int rc
= req
->r_result
<= 0 ? req
->r_result
: 0;
264 int bytes
= req
->r_result
>= 0 ? req
->r_result
: 0;
268 dout("finish_read %p req %p rc %d bytes %d\n", inode
, req
, rc
, bytes
);
270 /* unlock all pages, zeroing any data we didn't read */
271 osd_data
= osd_req_op_extent_osd_data(req
, 0);
272 BUG_ON(osd_data
->type
!= CEPH_OSD_DATA_TYPE_PAGES
);
273 num_pages
= calc_pages_for((u64
)osd_data
->alignment
,
274 (u64
)osd_data
->length
);
275 for (i
= 0; i
< num_pages
; i
++) {
276 struct page
*page
= osd_data
->pages
[i
];
278 if (rc
< 0 && rc
!= -ENOENT
) {
279 ceph_fscache_readpage_cancel(inode
, page
);
282 if (bytes
< (int)PAGE_SIZE
) {
283 /* zero (remainder of) page */
284 int s
= bytes
< 0 ? 0 : bytes
;
285 zero_user_segment(page
, s
, PAGE_SIZE
);
287 dout("finish_read %p uptodate %p idx %lu\n", inode
, page
,
289 flush_dcache_page(page
);
290 SetPageUptodate(page
);
291 ceph_readpage_to_fscache(inode
, page
);
297 kfree(osd_data
->pages
);
301 * start an async read(ahead) operation. return nr_pages we submitted
302 * a read for on success, or negative error code.
304 static int start_read(struct inode
*inode
, struct list_head
*page_list
, int max
)
306 struct ceph_osd_client
*osdc
=
307 &ceph_inode_to_client(inode
)->client
->osdc
;
308 struct ceph_inode_info
*ci
= ceph_inode(inode
);
309 struct page
*page
= list_entry(page_list
->prev
, struct page
, lru
);
310 struct ceph_vino vino
;
311 struct ceph_osd_request
*req
;
320 off
= (u64
) page_offset(page
);
323 next_index
= page
->index
;
324 list_for_each_entry_reverse(page
, page_list
, lru
) {
325 if (page
->index
!= next_index
)
329 if (max
&& nr_pages
== max
)
332 len
= nr_pages
<< PAGE_SHIFT
;
333 dout("start_read %p nr_pages %d is %lld~%lld\n", inode
, nr_pages
,
335 vino
= ceph_vino(inode
);
336 req
= ceph_osdc_new_request(osdc
, &ci
->i_layout
, vino
, off
, &len
,
337 0, 1, CEPH_OSD_OP_READ
,
338 CEPH_OSD_FLAG_READ
, NULL
,
339 ci
->i_truncate_seq
, ci
->i_truncate_size
,
344 /* build page vector */
345 nr_pages
= calc_pages_for(0, len
);
346 pages
= kmalloc(sizeof(*pages
) * nr_pages
, GFP_KERNEL
);
350 for (i
= 0; i
< nr_pages
; ++i
) {
351 page
= list_entry(page_list
->prev
, struct page
, lru
);
352 BUG_ON(PageLocked(page
));
353 list_del(&page
->lru
);
355 dout("start_read %p adding %p idx %lu\n", inode
, page
,
357 if (add_to_page_cache_lru(page
, &inode
->i_data
, page
->index
,
359 ceph_fscache_uncache_page(inode
, page
);
361 dout("start_read %p add_to_page_cache failed %p\n",
365 len
= nr_pages
<< PAGE_SHIFT
;
372 osd_req_op_extent_osd_data_pages(req
, 0, pages
, len
, 0, false, false);
373 req
->r_callback
= finish_read
;
374 req
->r_inode
= inode
;
376 dout("start_read %p starting %p %lld~%lld\n", inode
, req
, off
, len
);
377 ret
= ceph_osdc_start_request(osdc
, req
, false);
380 ceph_osdc_put_request(req
);
384 for (i
= 0; i
< nr_pages
; ++i
) {
385 ceph_fscache_readpage_cancel(inode
, pages
[i
]);
386 unlock_page(pages
[i
]);
388 ceph_put_page_vector(pages
, nr_pages
, false);
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 set_page_writeback(page
);
539 err
= ceph_osdc_writepages(osdc
, ceph_vino(inode
),
540 &ci
->i_layout
, snapc
,
542 truncate_seq
, truncate_size
,
543 &inode
->i_mtime
, &page
, 1);
545 struct writeback_control tmp_wbc
;
548 if (err
== -ERESTARTSYS
) {
549 /* killed by SIGKILL */
550 dout("writepage interrupted page %p\n", page
);
551 redirty_page_for_writepage(wbc
, page
);
552 end_page_writeback(page
);
555 dout("writepage setting page/mapping error %d %p\n",
558 mapping_set_error(&inode
->i_data
, err
);
559 wbc
->pages_skipped
++;
561 dout("writepage cleaned page %p\n", page
);
562 err
= 0; /* vfs expects us to return 0 */
565 ClearPagePrivate(page
);
566 end_page_writeback(page
);
567 ceph_put_wrbuffer_cap_refs(ci
, 1, snapc
);
568 ceph_put_snap_context(snapc
); /* page's reference */
573 static int ceph_writepage(struct page
*page
, struct writeback_control
*wbc
)
576 struct inode
*inode
= page
->mapping
->host
;
579 err
= writepage_nounlock(page
, wbc
);
580 if (err
== -ERESTARTSYS
) {
581 /* direct memory reclaimer was killed by SIGKILL. return 0
582 * to prevent caller from setting mapping/page error */
591 * lame release_pages helper. release_pages() isn't exported to
594 static void ceph_release_pages(struct page
**pages
, int num
)
599 pagevec_init(&pvec
, 0);
600 for (i
= 0; i
< num
; i
++) {
601 if (pagevec_add(&pvec
, pages
[i
]) == 0)
602 pagevec_release(&pvec
);
604 pagevec_release(&pvec
);
608 * async writeback completion handler.
610 * If we get an error, set the mapping error bit, but not the individual
613 static void writepages_finish(struct ceph_osd_request
*req
)
615 struct inode
*inode
= req
->r_inode
;
616 struct ceph_inode_info
*ci
= ceph_inode(inode
);
617 struct ceph_osd_data
*osd_data
;
619 int num_pages
, total_pages
= 0;
621 int rc
= req
->r_result
;
622 struct ceph_snap_context
*snapc
= req
->r_snapc
;
623 struct address_space
*mapping
= inode
->i_mapping
;
624 struct ceph_fs_client
*fsc
= ceph_inode_to_client(inode
);
627 dout("writepages_finish %p rc %d\n", inode
, rc
);
629 mapping_set_error(mapping
, rc
);
632 * We lost the cache cap, need to truncate the page before
633 * it is unlocked, otherwise we'd truncate it later in the
634 * page truncation thread, possibly losing some data that
637 remove_page
= !(ceph_caps_issued(ci
) &
638 (CEPH_CAP_FILE_CACHE
|CEPH_CAP_FILE_LAZYIO
));
640 /* clean all pages */
641 for (i
= 0; i
< req
->r_num_ops
; i
++) {
642 if (req
->r_ops
[i
].op
!= CEPH_OSD_OP_WRITE
)
645 osd_data
= osd_req_op_extent_osd_data(req
, i
);
646 BUG_ON(osd_data
->type
!= CEPH_OSD_DATA_TYPE_PAGES
);
647 num_pages
= calc_pages_for((u64
)osd_data
->alignment
,
648 (u64
)osd_data
->length
);
649 total_pages
+= num_pages
;
650 for (j
= 0; j
< num_pages
; j
++) {
651 page
= osd_data
->pages
[j
];
653 WARN_ON(!PageUptodate(page
));
655 if (atomic_long_dec_return(&fsc
->writeback_count
) <
656 CONGESTION_OFF_THRESH(
657 fsc
->mount_options
->congestion_kb
))
658 clear_bdi_congested(&fsc
->backing_dev_info
,
664 ceph_put_snap_context(page_snap_context(page
));
666 ClearPagePrivate(page
);
667 dout("unlocking %p\n", page
);
668 end_page_writeback(page
);
671 generic_error_remove_page(inode
->i_mapping
,
676 dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n",
677 inode
, osd_data
->length
, rc
>= 0 ? num_pages
: 0);
679 ceph_release_pages(osd_data
->pages
, num_pages
);
682 ceph_put_wrbuffer_cap_refs(ci
, total_pages
, snapc
);
684 osd_data
= osd_req_op_extent_osd_data(req
, 0);
685 if (osd_data
->pages_from_pool
)
686 mempool_free(osd_data
->pages
,
687 ceph_sb_to_client(inode
->i_sb
)->wb_pagevec_pool
);
689 kfree(osd_data
->pages
);
690 ceph_osdc_put_request(req
);
694 * initiate async writeback
696 static int ceph_writepages_start(struct address_space
*mapping
,
697 struct writeback_control
*wbc
)
699 struct inode
*inode
= mapping
->host
;
700 struct ceph_inode_info
*ci
= ceph_inode(inode
);
701 struct ceph_fs_client
*fsc
= ceph_inode_to_client(inode
);
702 struct ceph_vino vino
= ceph_vino(inode
);
703 pgoff_t index
, start
, end
;
706 pgoff_t max_pages
= 0, max_pages_ever
= 0;
707 struct ceph_snap_context
*snapc
= NULL
, *last_snapc
= NULL
, *pgsnapc
;
711 unsigned wsize
= 1 << inode
->i_blkbits
;
712 struct ceph_osd_request
*req
= NULL
;
714 loff_t snap_size
, i_size
;
719 * Include a 'sync' in the OSD request if this is a data
720 * integrity write (e.g., O_SYNC write or fsync()), or if our
721 * cap is being revoked.
723 if ((wbc
->sync_mode
== WB_SYNC_ALL
) ||
724 ceph_caps_revoking(ci
, CEPH_CAP_FILE_BUFFER
))
726 dout("writepages_start %p dosync=%d (mode=%s)\n",
728 wbc
->sync_mode
== WB_SYNC_NONE
? "NONE" :
729 (wbc
->sync_mode
== WB_SYNC_ALL
? "ALL" : "HOLD"));
731 if (ACCESS_ONCE(fsc
->mount_state
) == CEPH_MOUNT_SHUTDOWN
) {
732 if (ci
->i_wrbuffer_ref
> 0) {
734 "writepage_start %p %lld forced umount\n",
735 inode
, ceph_ino(inode
));
737 mapping_set_error(mapping
, -EIO
);
738 return -EIO
; /* we're in a forced umount, don't write! */
740 if (fsc
->mount_options
->wsize
&& fsc
->mount_options
->wsize
< wsize
)
741 wsize
= fsc
->mount_options
->wsize
;
742 if (wsize
< PAGE_SIZE
)
744 max_pages_ever
= wsize
>> PAGE_SHIFT
;
746 pagevec_init(&pvec
, 0);
748 /* where to start/end? */
749 if (wbc
->range_cyclic
) {
750 start
= mapping
->writeback_index
; /* Start from prev offset */
752 dout(" cyclic, start at %lu\n", start
);
754 start
= wbc
->range_start
>> PAGE_SHIFT
;
755 end
= wbc
->range_end
>> PAGE_SHIFT
;
756 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
759 dout(" not cyclic, %lu to %lu\n", start
, end
);
764 /* find oldest snap context with dirty data */
765 ceph_put_snap_context(snapc
);
767 snapc
= get_oldest_context(inode
, &snap_size
);
769 /* hmm, why does writepages get called when there
771 dout(" no snap context with dirty data?\n");
774 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
775 snapc
, snapc
->seq
, snapc
->num_snaps
);
777 spin_lock(&ci
->i_ceph_lock
);
778 truncate_seq
= ci
->i_truncate_seq
;
779 truncate_size
= ci
->i_truncate_size
;
780 i_size
= i_size_read(inode
);
781 spin_unlock(&ci
->i_ceph_lock
);
783 if (last_snapc
&& snapc
!= last_snapc
) {
784 /* if we switched to a newer snapc, restart our scan at the
785 * start of the original file range. */
786 dout(" snapc differs from last pass, restarting at %lu\n",
792 while (!done
&& index
<= end
) {
795 pgoff_t strip_unit_end
= 0;
796 int num_ops
= 0, op_idx
;
797 int pvec_pages
, locked_pages
= 0;
798 struct page
**pages
= NULL
, **data_pages
;
799 mempool_t
*pool
= NULL
; /* Becomes non-null if mempool used */
802 u64 offset
= 0, len
= 0;
804 max_pages
= max_pages_ever
;
808 want
= min(end
- index
,
809 min((pgoff_t
)PAGEVEC_SIZE
,
810 max_pages
- (pgoff_t
)locked_pages
) - 1)
812 pvec_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
,
815 dout("pagevec_lookup_tag got %d\n", pvec_pages
);
816 if (!pvec_pages
&& !locked_pages
)
818 for (i
= 0; i
< pvec_pages
&& locked_pages
< max_pages
; i
++) {
819 page
= pvec
.pages
[i
];
820 dout("? %p idx %lu\n", page
, page
->index
);
821 if (locked_pages
== 0)
822 lock_page(page
); /* first page */
823 else if (!trylock_page(page
))
826 /* only dirty pages, or our accounting breaks */
827 if (unlikely(!PageDirty(page
)) ||
828 unlikely(page
->mapping
!= mapping
)) {
829 dout("!dirty or !mapping %p\n", page
);
833 if (!wbc
->range_cyclic
&& page
->index
> end
) {
834 dout("end of range %p\n", page
);
839 if (strip_unit_end
&& (page
->index
> strip_unit_end
)) {
840 dout("end of strip unit %p\n", page
);
844 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
845 dout("waiting on writeback %p\n", page
);
846 wait_on_page_writeback(page
);
848 if (page_offset(page
) >=
849 (snap_size
== -1 ? i_size
: snap_size
)) {
850 dout("%p page eof %llu\n", page
,
851 (snap_size
== -1 ? i_size
: snap_size
));
856 if (PageWriteback(page
)) {
857 dout("%p under writeback\n", page
);
862 /* only if matching snap context */
863 pgsnapc
= page_snap_context(page
);
864 if (pgsnapc
->seq
> snapc
->seq
) {
865 dout("page snapc %p %lld > oldest %p %lld\n",
866 pgsnapc
, pgsnapc
->seq
, snapc
, snapc
->seq
);
869 continue; /* keep looking for snap */
873 if (!clear_page_dirty_for_io(page
)) {
874 dout("%p !clear_page_dirty_for_io\n", page
);
880 * We have something to write. If this is
881 * the first locked page this time through,
882 * calculate max possinle write size and
883 * allocate a page array
885 if (locked_pages
== 0) {
889 /* prepare async write request */
890 offset
= (u64
)page_offset(page
);
893 rc
= ceph_calc_file_object_mapping(&ci
->i_layout
,
902 num_ops
= 1 + do_sync
;
903 strip_unit_end
= page
->index
+
904 ((len
- 1) >> PAGE_SHIFT
);
907 max_pages
= calc_pages_for(0, (u64
)len
);
908 pages
= kmalloc(max_pages
* sizeof (*pages
),
911 pool
= fsc
->wb_pagevec_pool
;
912 pages
= mempool_alloc(pool
, GFP_NOFS
);
917 } else if (page
->index
!=
918 (offset
+ len
) >> PAGE_SHIFT
) {
919 if (num_ops
>= (pool
? CEPH_OSD_SLAB_OPS
:
921 redirty_page_for_writepage(wbc
, page
);
927 offset
= (u64
)page_offset(page
);
931 /* note position of first page in pvec */
934 dout("%p will write page %p idx %lu\n",
935 inode
, page
, page
->index
);
937 if (atomic_long_inc_return(&fsc
->writeback_count
) >
938 CONGESTION_ON_THRESH(
939 fsc
->mount_options
->congestion_kb
)) {
940 set_bdi_congested(&fsc
->backing_dev_info
,
944 pages
[locked_pages
] = page
;
949 /* did we get anything? */
951 goto release_pvec_pages
;
954 BUG_ON(!locked_pages
|| first
< 0);
956 if (pvec_pages
&& i
== pvec_pages
&&
957 locked_pages
< max_pages
) {
958 dout("reached end pvec, trying for more\n");
959 pagevec_reinit(&pvec
);
963 /* shift unused pages over in the pvec... we
964 * will need to release them below. */
965 for (j
= i
; j
< pvec_pages
; j
++) {
966 dout(" pvec leftover page %p\n", pvec
.pages
[j
]);
967 pvec
.pages
[j
-i
+first
] = pvec
.pages
[j
];
973 offset
= page_offset(pages
[0]);
976 req
= ceph_osdc_new_request(&fsc
->client
->osdc
,
978 offset
, &len
, 0, num_ops
,
980 CEPH_OSD_FLAG_WRITE
|
981 CEPH_OSD_FLAG_ONDISK
,
983 truncate_size
, false);
985 req
= ceph_osdc_new_request(&fsc
->client
->osdc
,
991 CEPH_OSD_FLAG_WRITE
|
992 CEPH_OSD_FLAG_ONDISK
,
994 truncate_size
, true);
997 BUG_ON(len
< page_offset(pages
[locked_pages
- 1]) +
1000 req
->r_callback
= writepages_finish
;
1001 req
->r_inode
= inode
;
1003 /* Format the osd request message and submit the write */
1007 for (i
= 0; i
< locked_pages
; i
++) {
1008 u64 cur_offset
= page_offset(pages
[i
]);
1009 if (offset
+ len
!= cur_offset
) {
1010 if (op_idx
+ do_sync
+ 1 == req
->r_num_ops
)
1012 osd_req_op_extent_dup_last(req
, op_idx
,
1013 cur_offset
- offset
);
1014 dout("writepages got pages at %llu~%llu\n",
1016 osd_req_op_extent_osd_data_pages(req
, op_idx
,
1019 osd_req_op_extent_update(req
, op_idx
, len
);
1022 offset
= cur_offset
;
1023 data_pages
= pages
+ i
;
1027 set_page_writeback(pages
[i
]);
1031 if (snap_size
!= -1) {
1032 len
= min(len
, snap_size
- offset
);
1033 } else if (i
== locked_pages
) {
1034 /* writepages_finish() clears writeback pages
1035 * according to the data length, so make sure
1036 * data length covers all locked pages */
1037 u64 min_len
= len
+ 1 - PAGE_SIZE
;
1038 len
= min(len
, (u64
)i_size_read(inode
) - offset
);
1039 len
= max(len
, min_len
);
1041 dout("writepages got pages at %llu~%llu\n", offset
, len
);
1043 osd_req_op_extent_osd_data_pages(req
, op_idx
, data_pages
, len
,
1045 osd_req_op_extent_update(req
, op_idx
, len
);
1049 osd_req_op_init(req
, op_idx
, CEPH_OSD_OP_STARTSYNC
, 0);
1051 BUG_ON(op_idx
+ 1 != req
->r_num_ops
);
1054 if (i
< locked_pages
) {
1055 BUG_ON(num_ops
<= req
->r_num_ops
);
1056 num_ops
-= req
->r_num_ops
;
1060 /* allocate new pages array for next request */
1062 pages
= kmalloc(locked_pages
* sizeof (*pages
),
1065 pool
= fsc
->wb_pagevec_pool
;
1066 pages
= mempool_alloc(pool
, GFP_NOFS
);
1069 memcpy(pages
, data_pages
+ i
,
1070 locked_pages
* sizeof(*pages
));
1071 memset(data_pages
+ i
, 0,
1072 locked_pages
* sizeof(*pages
));
1074 BUG_ON(num_ops
!= req
->r_num_ops
);
1075 index
= pages
[i
- 1]->index
+ 1;
1076 /* request message now owns the pages array */
1080 req
->r_mtime
= inode
->i_mtime
;
1081 rc
= ceph_osdc_start_request(&fsc
->client
->osdc
, req
, true);
1085 wbc
->nr_to_write
-= i
;
1089 if (wbc
->nr_to_write
<= 0)
1093 dout("pagevec_release on %d pages (%p)\n", (int)pvec
.nr
,
1094 pvec
.nr
? pvec
.pages
[0] : NULL
);
1095 pagevec_release(&pvec
);
1097 if (locked_pages
&& !done
)
1101 if (should_loop
&& !done
) {
1102 /* more to do; loop back to beginning of file */
1103 dout("writepages looping back to beginning of file\n");
1109 if (wbc
->range_cyclic
|| (range_whole
&& wbc
->nr_to_write
> 0))
1110 mapping
->writeback_index
= index
;
1113 ceph_osdc_put_request(req
);
1114 ceph_put_snap_context(snapc
);
1115 dout("writepages done, rc = %d\n", rc
);
1122 * See if a given @snapc is either writeable, or already written.
1124 static int context_is_writeable_or_written(struct inode
*inode
,
1125 struct ceph_snap_context
*snapc
)
1127 struct ceph_snap_context
*oldest
= get_oldest_context(inode
, NULL
);
1128 int ret
= !oldest
|| snapc
->seq
<= oldest
->seq
;
1130 ceph_put_snap_context(oldest
);
1135 * We are only allowed to write into/dirty the page if the page is
1136 * clean, or already dirty within the same snap context.
1138 * called with page locked.
1139 * return success with page locked,
1140 * or any failure (incl -EAGAIN) with page unlocked.
1142 static int ceph_update_writeable_page(struct file
*file
,
1143 loff_t pos
, unsigned len
,
1146 struct inode
*inode
= file_inode(file
);
1147 struct ceph_fs_client
*fsc
= ceph_inode_to_client(inode
);
1148 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1149 loff_t page_off
= pos
& PAGE_MASK
;
1150 int pos_in_page
= pos
& ~PAGE_MASK
;
1151 int end_in_page
= pos_in_page
+ len
;
1154 struct ceph_snap_context
*snapc
, *oldest
;
1156 if (ACCESS_ONCE(fsc
->mount_state
) == CEPH_MOUNT_SHUTDOWN
) {
1157 dout(" page %p forced umount\n", page
);
1163 /* writepages currently holds page lock, but if we change that later, */
1164 wait_on_page_writeback(page
);
1166 snapc
= page_snap_context(page
);
1167 if (snapc
&& snapc
!= ci
->i_head_snapc
) {
1169 * this page is already dirty in another (older) snap
1170 * context! is it writeable now?
1172 oldest
= get_oldest_context(inode
, NULL
);
1174 if (snapc
->seq
> oldest
->seq
) {
1175 ceph_put_snap_context(oldest
);
1176 dout(" page %p snapc %p not current or oldest\n",
1179 * queue for writeback, and wait for snapc to
1180 * be writeable or written
1182 snapc
= ceph_get_snap_context(snapc
);
1184 ceph_queue_writeback(inode
);
1185 r
= wait_event_killable(ci
->i_cap_wq
,
1186 context_is_writeable_or_written(inode
, snapc
));
1187 ceph_put_snap_context(snapc
);
1188 if (r
== -ERESTARTSYS
)
1192 ceph_put_snap_context(oldest
);
1194 /* yay, writeable, do it now (without dropping page lock) */
1195 dout(" page %p snapc %p not current, but oldest\n",
1197 if (!clear_page_dirty_for_io(page
))
1199 r
= writepage_nounlock(page
, NULL
);
1205 if (PageUptodate(page
)) {
1206 dout(" page %p already uptodate\n", page
);
1211 if (pos_in_page
== 0 && len
== PAGE_SIZE
)
1214 /* past end of file? */
1215 i_size
= i_size_read(inode
);
1217 if (page_off
>= i_size
||
1218 (pos_in_page
== 0 && (pos
+len
) >= i_size
&&
1219 end_in_page
- pos_in_page
!= PAGE_SIZE
)) {
1220 dout(" zeroing %p 0 - %d and %d - %d\n",
1221 page
, pos_in_page
, end_in_page
, (int)PAGE_SIZE
);
1222 zero_user_segments(page
,
1224 end_in_page
, PAGE_SIZE
);
1228 /* we need to read it. */
1229 r
= readpage_nounlock(file
, page
);
1239 * We are only allowed to write into/dirty the page if the page is
1240 * clean, or already dirty within the same snap context.
1242 static int ceph_write_begin(struct file
*file
, struct address_space
*mapping
,
1243 loff_t pos
, unsigned len
, unsigned flags
,
1244 struct page
**pagep
, void **fsdata
)
1246 struct inode
*inode
= file_inode(file
);
1248 pgoff_t index
= pos
>> PAGE_SHIFT
;
1253 page
= grab_cache_page_write_begin(mapping
, index
, 0);
1257 dout("write_begin file %p inode %p page %p %d~%d\n", file
,
1258 inode
, page
, (int)pos
, (int)len
);
1260 r
= ceph_update_writeable_page(file
, pos
, len
, page
);
1265 } while (r
== -EAGAIN
);
1271 * we don't do anything in here that simple_write_end doesn't do
1272 * except adjust dirty page accounting
1274 static int ceph_write_end(struct file
*file
, struct address_space
*mapping
,
1275 loff_t pos
, unsigned len
, unsigned copied
,
1276 struct page
*page
, void *fsdata
)
1278 struct inode
*inode
= file_inode(file
);
1279 unsigned from
= pos
& (PAGE_SIZE
- 1);
1282 dout("write_end file %p inode %p page %p %d~%d (%d)\n", file
,
1283 inode
, page
, (int)pos
, (int)copied
, (int)len
);
1285 /* zero the stale part of the page if we did a short copy */
1287 zero_user_segment(page
, from
+copied
, len
);
1289 /* did file size increase? */
1290 if (pos
+copied
> i_size_read(inode
))
1291 check_cap
= ceph_inode_set_size(inode
, pos
+copied
);
1293 if (!PageUptodate(page
))
1294 SetPageUptodate(page
);
1296 set_page_dirty(page
);
1302 ceph_check_caps(ceph_inode(inode
), CHECK_CAPS_AUTHONLY
, NULL
);
1308 * we set .direct_IO to indicate direct io is supported, but since we
1309 * intercept O_DIRECT reads and writes early, this function should
1312 static ssize_t
ceph_direct_io(struct kiocb
*iocb
, struct iov_iter
*iter
)
1318 const struct address_space_operations ceph_aops
= {
1319 .readpage
= ceph_readpage
,
1320 .readpages
= ceph_readpages
,
1321 .writepage
= ceph_writepage
,
1322 .writepages
= ceph_writepages_start
,
1323 .write_begin
= ceph_write_begin
,
1324 .write_end
= ceph_write_end
,
1325 .set_page_dirty
= ceph_set_page_dirty
,
1326 .invalidatepage
= ceph_invalidatepage
,
1327 .releasepage
= ceph_releasepage
,
1328 .direct_IO
= ceph_direct_io
,
1331 static void ceph_block_sigs(sigset_t
*oldset
)
1334 siginitsetinv(&mask
, sigmask(SIGKILL
));
1335 sigprocmask(SIG_BLOCK
, &mask
, oldset
);
1338 static void ceph_restore_sigs(sigset_t
*oldset
)
1340 sigprocmask(SIG_SETMASK
, oldset
, NULL
);
1346 static int ceph_filemap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1348 struct inode
*inode
= file_inode(vma
->vm_file
);
1349 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1350 struct ceph_file_info
*fi
= vma
->vm_file
->private_data
;
1351 struct page
*pinned_page
= NULL
;
1352 loff_t off
= vmf
->pgoff
<< PAGE_SHIFT
;
1356 ceph_block_sigs(&oldset
);
1358 dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n",
1359 inode
, ceph_vinop(inode
), off
, (size_t)PAGE_SIZE
);
1360 if (fi
->fmode
& CEPH_FILE_MODE_LAZY
)
1361 want
= CEPH_CAP_FILE_CACHE
| CEPH_CAP_FILE_LAZYIO
;
1363 want
= CEPH_CAP_FILE_CACHE
;
1366 ret
= ceph_get_caps(ci
, CEPH_CAP_FILE_RD
, want
, -1, &got
, &pinned_page
);
1370 dout("filemap_fault %p %llu~%zd got cap refs on %s\n",
1371 inode
, off
, (size_t)PAGE_SIZE
, ceph_cap_string(got
));
1373 if ((got
& (CEPH_CAP_FILE_CACHE
| CEPH_CAP_FILE_LAZYIO
)) ||
1374 ci
->i_inline_version
== CEPH_INLINE_NONE
)
1375 ret
= filemap_fault(vma
, vmf
);
1379 dout("filemap_fault %p %llu~%zd dropping cap refs on %s ret %d\n",
1380 inode
, off
, (size_t)PAGE_SIZE
, ceph_cap_string(got
), ret
);
1382 put_page(pinned_page
);
1383 ceph_put_cap_refs(ci
, got
);
1388 /* read inline data */
1389 if (off
>= PAGE_SIZE
) {
1390 /* does not support inline data > PAGE_SIZE */
1391 ret
= VM_FAULT_SIGBUS
;
1394 struct address_space
*mapping
= inode
->i_mapping
;
1395 struct page
*page
= find_or_create_page(mapping
, 0,
1396 mapping_gfp_constraint(mapping
,
1402 ret1
= __ceph_do_getattr(inode
, page
,
1403 CEPH_STAT_CAP_INLINE_DATA
, true);
1404 if (ret1
< 0 || off
>= i_size_read(inode
)) {
1410 ret
= VM_FAULT_SIGBUS
;
1413 if (ret1
< PAGE_SIZE
)
1414 zero_user_segment(page
, ret1
, PAGE_SIZE
);
1416 flush_dcache_page(page
);
1417 SetPageUptodate(page
);
1419 ret
= VM_FAULT_MAJOR
| VM_FAULT_LOCKED
;
1421 dout("filemap_fault %p %llu~%zd read inline data ret %d\n",
1422 inode
, off
, (size_t)PAGE_SIZE
, ret
);
1425 ceph_restore_sigs(&oldset
);
1427 ret
= (ret
== -ENOMEM
) ? VM_FAULT_OOM
: VM_FAULT_SIGBUS
;
1433 * Reuse write_begin here for simplicity.
1435 static int ceph_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1437 struct inode
*inode
= file_inode(vma
->vm_file
);
1438 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1439 struct ceph_file_info
*fi
= vma
->vm_file
->private_data
;
1440 struct ceph_cap_flush
*prealloc_cf
;
1441 struct page
*page
= vmf
->page
;
1442 loff_t off
= page_offset(page
);
1443 loff_t size
= i_size_read(inode
);
1448 prealloc_cf
= ceph_alloc_cap_flush();
1450 return VM_FAULT_OOM
;
1452 ceph_block_sigs(&oldset
);
1454 if (ci
->i_inline_version
!= CEPH_INLINE_NONE
) {
1455 struct page
*locked_page
= NULL
;
1460 ret
= ceph_uninline_data(vma
->vm_file
, locked_page
);
1462 unlock_page(locked_page
);
1467 if (off
+ PAGE_SIZE
<= size
)
1470 len
= size
& ~PAGE_MASK
;
1472 dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
1473 inode
, ceph_vinop(inode
), off
, len
, size
);
1474 if (fi
->fmode
& CEPH_FILE_MODE_LAZY
)
1475 want
= CEPH_CAP_FILE_BUFFER
| CEPH_CAP_FILE_LAZYIO
;
1477 want
= CEPH_CAP_FILE_BUFFER
;
1480 ret
= ceph_get_caps(ci
, CEPH_CAP_FILE_WR
, want
, off
+ len
,
1485 dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
1486 inode
, off
, len
, ceph_cap_string(got
));
1488 /* Update time before taking page lock */
1489 file_update_time(vma
->vm_file
);
1494 if ((off
> size
) || (page
->mapping
!= inode
->i_mapping
)) {
1496 ret
= VM_FAULT_NOPAGE
;
1500 ret
= ceph_update_writeable_page(vma
->vm_file
, off
, len
, page
);
1502 /* success. we'll keep the page locked. */
1503 set_page_dirty(page
);
1504 ret
= VM_FAULT_LOCKED
;
1506 } while (ret
== -EAGAIN
);
1508 if (ret
== VM_FAULT_LOCKED
||
1509 ci
->i_inline_version
!= CEPH_INLINE_NONE
) {
1511 spin_lock(&ci
->i_ceph_lock
);
1512 ci
->i_inline_version
= CEPH_INLINE_NONE
;
1513 dirty
= __ceph_mark_dirty_caps(ci
, CEPH_CAP_FILE_WR
,
1515 spin_unlock(&ci
->i_ceph_lock
);
1517 __mark_inode_dirty(inode
, dirty
);
1520 dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %d\n",
1521 inode
, off
, len
, ceph_cap_string(got
), ret
);
1522 ceph_put_cap_refs(ci
, got
);
1524 ceph_restore_sigs(&oldset
);
1525 ceph_free_cap_flush(prealloc_cf
);
1527 ret
= (ret
== -ENOMEM
) ? VM_FAULT_OOM
: VM_FAULT_SIGBUS
;
1531 void ceph_fill_inline_data(struct inode
*inode
, struct page
*locked_page
,
1532 char *data
, size_t len
)
1534 struct address_space
*mapping
= inode
->i_mapping
;
1540 if (i_size_read(inode
) == 0)
1542 page
= find_or_create_page(mapping
, 0,
1543 mapping_gfp_constraint(mapping
,
1547 if (PageUptodate(page
)) {
1554 dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
1555 inode
, ceph_vinop(inode
), len
, locked_page
);
1558 void *kaddr
= kmap_atomic(page
);
1559 memcpy(kaddr
, data
, len
);
1560 kunmap_atomic(kaddr
);
1563 if (page
!= locked_page
) {
1564 if (len
< PAGE_SIZE
)
1565 zero_user_segment(page
, len
, PAGE_SIZE
);
1567 flush_dcache_page(page
);
1569 SetPageUptodate(page
);
1575 int ceph_uninline_data(struct file
*filp
, struct page
*locked_page
)
1577 struct inode
*inode
= file_inode(filp
);
1578 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1579 struct ceph_fs_client
*fsc
= ceph_inode_to_client(inode
);
1580 struct ceph_osd_request
*req
;
1581 struct page
*page
= NULL
;
1582 u64 len
, inline_version
;
1584 bool from_pagecache
= false;
1586 spin_lock(&ci
->i_ceph_lock
);
1587 inline_version
= ci
->i_inline_version
;
1588 spin_unlock(&ci
->i_ceph_lock
);
1590 dout("uninline_data %p %llx.%llx inline_version %llu\n",
1591 inode
, ceph_vinop(inode
), inline_version
);
1593 if (inline_version
== 1 || /* initial version, no data */
1594 inline_version
== CEPH_INLINE_NONE
)
1599 WARN_ON(!PageUptodate(page
));
1600 } else if (ceph_caps_issued(ci
) &
1601 (CEPH_CAP_FILE_CACHE
|CEPH_CAP_FILE_LAZYIO
)) {
1602 page
= find_get_page(inode
->i_mapping
, 0);
1604 if (PageUptodate(page
)) {
1605 from_pagecache
= true;
1615 len
= i_size_read(inode
);
1616 if (len
> PAGE_SIZE
)
1619 page
= __page_cache_alloc(GFP_NOFS
);
1624 err
= __ceph_do_getattr(inode
, page
,
1625 CEPH_STAT_CAP_INLINE_DATA
, true);
1627 /* no inline data */
1628 if (err
== -ENODATA
)
1635 req
= ceph_osdc_new_request(&fsc
->client
->osdc
, &ci
->i_layout
,
1636 ceph_vino(inode
), 0, &len
, 0, 1,
1638 CEPH_OSD_FLAG_ONDISK
| CEPH_OSD_FLAG_WRITE
,
1645 req
->r_mtime
= inode
->i_mtime
;
1646 err
= ceph_osdc_start_request(&fsc
->client
->osdc
, req
, false);
1648 err
= ceph_osdc_wait_request(&fsc
->client
->osdc
, req
);
1649 ceph_osdc_put_request(req
);
1653 req
= ceph_osdc_new_request(&fsc
->client
->osdc
, &ci
->i_layout
,
1654 ceph_vino(inode
), 0, &len
, 1, 3,
1656 CEPH_OSD_FLAG_ONDISK
| CEPH_OSD_FLAG_WRITE
,
1657 NULL
, ci
->i_truncate_seq
,
1658 ci
->i_truncate_size
, false);
1664 osd_req_op_extent_osd_data_pages(req
, 1, &page
, len
, 0, false, false);
1667 __le64 xattr_buf
= cpu_to_le64(inline_version
);
1668 err
= osd_req_op_xattr_init(req
, 0, CEPH_OSD_OP_CMPXATTR
,
1669 "inline_version", &xattr_buf
,
1671 CEPH_OSD_CMPXATTR_OP_GT
,
1672 CEPH_OSD_CMPXATTR_MODE_U64
);
1679 int xattr_len
= snprintf(xattr_buf
, sizeof(xattr_buf
),
1680 "%llu", inline_version
);
1681 err
= osd_req_op_xattr_init(req
, 2, CEPH_OSD_OP_SETXATTR
,
1683 xattr_buf
, xattr_len
, 0, 0);
1688 req
->r_mtime
= inode
->i_mtime
;
1689 err
= ceph_osdc_start_request(&fsc
->client
->osdc
, req
, false);
1691 err
= ceph_osdc_wait_request(&fsc
->client
->osdc
, req
);
1693 ceph_osdc_put_request(req
);
1694 if (err
== -ECANCELED
)
1697 if (page
&& page
!= locked_page
) {
1698 if (from_pagecache
) {
1702 __free_pages(page
, 0);
1705 dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
1706 inode
, ceph_vinop(inode
), inline_version
, err
);
1710 static const struct vm_operations_struct ceph_vmops
= {
1711 .fault
= ceph_filemap_fault
,
1712 .page_mkwrite
= ceph_page_mkwrite
,
1715 int ceph_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1717 struct address_space
*mapping
= file
->f_mapping
;
1719 if (!mapping
->a_ops
->readpage
)
1721 file_accessed(file
);
1722 vma
->vm_ops
= &ceph_vmops
;
1731 static int __ceph_pool_perm_get(struct ceph_inode_info
*ci
,
1732 s64 pool
, struct ceph_string
*pool_ns
)
1734 struct ceph_fs_client
*fsc
= ceph_inode_to_client(&ci
->vfs_inode
);
1735 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
1736 struct ceph_osd_request
*rd_req
= NULL
, *wr_req
= NULL
;
1737 struct rb_node
**p
, *parent
;
1738 struct ceph_pool_perm
*perm
;
1739 struct page
**pages
;
1741 int err
= 0, err2
= 0, have
= 0;
1743 down_read(&mdsc
->pool_perm_rwsem
);
1744 p
= &mdsc
->pool_perm_tree
.rb_node
;
1746 perm
= rb_entry(*p
, struct ceph_pool_perm
, node
);
1747 if (pool
< perm
->pool
)
1749 else if (pool
> perm
->pool
)
1750 p
= &(*p
)->rb_right
;
1752 int ret
= ceph_compare_string(pool_ns
,
1758 p
= &(*p
)->rb_right
;
1765 up_read(&mdsc
->pool_perm_rwsem
);
1770 dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n",
1771 pool
, (int)pool_ns
->len
, pool_ns
->str
);
1773 dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool
);
1775 down_write(&mdsc
->pool_perm_rwsem
);
1776 p
= &mdsc
->pool_perm_tree
.rb_node
;
1780 perm
= rb_entry(parent
, struct ceph_pool_perm
, node
);
1781 if (pool
< perm
->pool
)
1783 else if (pool
> perm
->pool
)
1784 p
= &(*p
)->rb_right
;
1786 int ret
= ceph_compare_string(pool_ns
,
1792 p
= &(*p
)->rb_right
;
1800 up_write(&mdsc
->pool_perm_rwsem
);
1804 rd_req
= ceph_osdc_alloc_request(&fsc
->client
->osdc
, NULL
,
1805 1, false, GFP_NOFS
);
1811 rd_req
->r_flags
= CEPH_OSD_FLAG_READ
;
1812 osd_req_op_init(rd_req
, 0, CEPH_OSD_OP_STAT
, 0);
1813 rd_req
->r_base_oloc
.pool
= pool
;
1815 rd_req
->r_base_oloc
.pool_ns
= ceph_get_string(pool_ns
);
1816 ceph_oid_printf(&rd_req
->r_base_oid
, "%llx.00000000", ci
->i_vino
.ino
);
1818 err
= ceph_osdc_alloc_messages(rd_req
, GFP_NOFS
);
1822 wr_req
= ceph_osdc_alloc_request(&fsc
->client
->osdc
, NULL
,
1823 1, false, GFP_NOFS
);
1829 wr_req
->r_flags
= CEPH_OSD_FLAG_WRITE
| CEPH_OSD_FLAG_ACK
;
1830 osd_req_op_init(wr_req
, 0, CEPH_OSD_OP_CREATE
, CEPH_OSD_OP_FLAG_EXCL
);
1831 ceph_oloc_copy(&wr_req
->r_base_oloc
, &rd_req
->r_base_oloc
);
1832 ceph_oid_copy(&wr_req
->r_base_oid
, &rd_req
->r_base_oid
);
1834 err
= ceph_osdc_alloc_messages(wr_req
, GFP_NOFS
);
1838 /* one page should be large enough for STAT data */
1839 pages
= ceph_alloc_page_vector(1, GFP_KERNEL
);
1840 if (IS_ERR(pages
)) {
1841 err
= PTR_ERR(pages
);
1845 osd_req_op_raw_data_in_pages(rd_req
, 0, pages
, PAGE_SIZE
,
1847 err
= ceph_osdc_start_request(&fsc
->client
->osdc
, rd_req
, false);
1849 wr_req
->r_mtime
= ci
->vfs_inode
.i_mtime
;
1850 err2
= ceph_osdc_start_request(&fsc
->client
->osdc
, wr_req
, false);
1853 err
= ceph_osdc_wait_request(&fsc
->client
->osdc
, rd_req
);
1855 err2
= ceph_osdc_wait_request(&fsc
->client
->osdc
, wr_req
);
1857 if (err
>= 0 || err
== -ENOENT
)
1859 else if (err
!= -EPERM
)
1862 if (err2
== 0 || err2
== -EEXIST
)
1864 else if (err2
!= -EPERM
) {
1869 pool_ns_len
= pool_ns
? pool_ns
->len
: 0;
1870 perm
= kmalloc(sizeof(*perm
) + pool_ns_len
+ 1, GFP_NOFS
);
1878 perm
->pool_ns_len
= pool_ns_len
;
1879 if (pool_ns_len
> 0)
1880 memcpy(perm
->pool_ns
, pool_ns
->str
, pool_ns_len
);
1881 perm
->pool_ns
[pool_ns_len
] = 0;
1883 rb_link_node(&perm
->node
, parent
, p
);
1884 rb_insert_color(&perm
->node
, &mdsc
->pool_perm_tree
);
1887 up_write(&mdsc
->pool_perm_rwsem
);
1889 ceph_osdc_put_request(rd_req
);
1890 ceph_osdc_put_request(wr_req
);
1895 dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n",
1896 pool
, (int)pool_ns
->len
, pool_ns
->str
, err
);
1898 dout("__ceph_pool_perm_get pool %lld result = %d\n", pool
, err
);
1902 int ceph_pool_perm_check(struct ceph_inode_info
*ci
, int need
)
1905 struct ceph_string
*pool_ns
;
1908 if (ceph_test_mount_opt(ceph_inode_to_client(&ci
->vfs_inode
),
1912 spin_lock(&ci
->i_ceph_lock
);
1913 flags
= ci
->i_ceph_flags
;
1914 pool
= ci
->i_layout
.pool_id
;
1915 spin_unlock(&ci
->i_ceph_lock
);
1917 if (flags
& CEPH_I_POOL_PERM
) {
1918 if ((need
& CEPH_CAP_FILE_RD
) && !(flags
& CEPH_I_POOL_RD
)) {
1919 dout("ceph_pool_perm_check pool %lld no read perm\n",
1923 if ((need
& CEPH_CAP_FILE_WR
) && !(flags
& CEPH_I_POOL_WR
)) {
1924 dout("ceph_pool_perm_check pool %lld no write perm\n",
1931 pool_ns
= ceph_try_get_string(ci
->i_layout
.pool_ns
);
1932 ret
= __ceph_pool_perm_get(ci
, pool
, pool_ns
);
1933 ceph_put_string(pool_ns
);
1937 flags
= CEPH_I_POOL_PERM
;
1938 if (ret
& POOL_READ
)
1939 flags
|= CEPH_I_POOL_RD
;
1940 if (ret
& POOL_WRITE
)
1941 flags
|= CEPH_I_POOL_WR
;
1943 spin_lock(&ci
->i_ceph_lock
);
1944 if (pool
== ci
->i_layout
.pool_id
&&
1945 pool_ns
== rcu_dereference_raw(ci
->i_layout
.pool_ns
)) {
1946 ci
->i_ceph_flags
|= flags
;
1948 pool
= ci
->i_layout
.pool_id
;
1949 flags
= ci
->i_ceph_flags
;
1951 spin_unlock(&ci
->i_ceph_lock
);
1955 void ceph_pool_perm_destroy(struct ceph_mds_client
*mdsc
)
1957 struct ceph_pool_perm
*perm
;
1960 while (!RB_EMPTY_ROOT(&mdsc
->pool_perm_tree
)) {
1961 n
= rb_first(&mdsc
->pool_perm_tree
);
1962 perm
= rb_entry(n
, struct ceph_pool_perm
, node
);
1963 rb_erase(n
, &mdsc
->pool_perm_tree
);