blob: ef3ebd780aff8bf8c9203ae366f3d4b688cd7af6 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weil1d3576f2009-10-06 11:31:09 -07002
3#include <linux/backing-dev.h>
4#include <linux/fs.h>
5#include <linux/mm.h>
6#include <linux/pagemap.h>
7#include <linux/writeback.h> /* generic_writepages */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Sage Weil1d3576f2009-10-06 11:31:09 -07009#include <linux/pagevec.h>
10#include <linux/task_io_accounting_ops.h>
11
12#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070013#include "mds_client.h"
Milosz Tanski99ccbd22013-08-21 17:29:54 -040014#include "cache.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070015#include <linux/ceph/osd_client.h>
Sage Weil1d3576f2009-10-06 11:31:09 -070016
17/*
18 * Ceph address space ops.
19 *
20 * There are a few funny things going on here.
21 *
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.
26 *
27 * Similarly, struct ceph_inode_info maintains a set of counters to
Lucas De Marchi25985ed2011-03-30 22:57:33 -030028 * count dirty pages on the inode. In the absence of snapshots,
Sage Weil1d3576f2009-10-06 11:31:09 -070029 * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count.
30 *
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.
41 *
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
47 * pages.
48 *
49 * Invalidate and so forth must take care to ensure the dirty page
50 * accounting is preserved.
51 */
52
Yehuda Sadeh2baba252009-12-18 13:51:57 -080053#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))
57
Yan, Zheng61600ef2012-05-28 14:44:30 +080058static inline struct ceph_snap_context *page_snap_context(struct page *page)
59{
60 if (PagePrivate(page))
61 return (void *)page->private;
62 return NULL;
63}
Sage Weil1d3576f2009-10-06 11:31:09 -070064
65/*
66 * Dirty a page. Optimistically adjust accounting, on the assumption
67 * that we won't race with invalidate. If we do, readjust.
68 */
69static int ceph_set_page_dirty(struct page *page)
70{
71 struct address_space *mapping = page->mapping;
72 struct inode *inode;
73 struct ceph_inode_info *ci;
Sage Weil1d3576f2009-10-06 11:31:09 -070074 struct ceph_snap_context *snapc;
Sha Zhengju7d6e1f52013-08-21 16:27:34 +080075 int ret;
Sage Weil1d3576f2009-10-06 11:31:09 -070076
77 if (unlikely(!mapping))
78 return !TestSetPageDirty(page);
79
Sha Zhengju7d6e1f52013-08-21 16:27:34 +080080 if (PageDirty(page)) {
Sage Weil1d3576f2009-10-06 11:31:09 -070081 dout("%p set_page_dirty %p idx %lu -- already dirty\n",
82 mapping->host, page, page->index);
Sha Zhengju7d6e1f52013-08-21 16:27:34 +080083 BUG_ON(!PagePrivate(page));
Sage Weil1d3576f2009-10-06 11:31:09 -070084 return 0;
85 }
86
87 inode = mapping->host;
88 ci = ceph_inode(inode);
89
Sage Weil1d3576f2009-10-06 11:31:09 -070090 /* dirty the head */
Sage Weilbe655592011-11-30 09:47:09 -080091 spin_lock(&ci->i_ceph_lock);
Yan, Zheng5dda377c2015-04-30 14:40:54 +080092 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,
96 struct ceph_cap_snap,
97 ci_item);
98 snapc = ceph_get_snap_context(capsnap->context);
99 capsnap->dirty_pages++;
100 } else {
101 BUG_ON(!ci->i_head_snapc);
102 snapc = ceph_get_snap_context(ci->i_head_snapc);
103 ++ci->i_wrbuffer_ref_head;
104 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700105 if (ci->i_wrbuffer_ref == 0)
Dave Chinner0444d762011-03-29 18:08:50 +1100106 ihold(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700107 ++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);
Sage Weilbe655592011-11-30 09:47:09 -0800114 spin_unlock(&ci->i_ceph_lock);
Sage Weil1d3576f2009-10-06 11:31:09 -0700115
Sha Zhengju7d6e1f52013-08-21 16:27:34 +0800116 /*
117 * Reference snap context in page->private. Also set
118 * PagePrivate so that we get invalidatepage callback.
119 */
120 BUG_ON(PagePrivate(page));
121 page->private = (unsigned long)snapc;
122 SetPagePrivate(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700123
Sha Zhengju7d6e1f52013-08-21 16:27:34 +0800124 ret = __set_page_dirty_nobuffers(page);
125 WARN_ON(!PageLocked(page));
126 WARN_ON(!page->mapping);
Sage Weil1d3576f2009-10-06 11:31:09 -0700127
Sha Zhengju7d6e1f52013-08-21 16:27:34 +0800128 return ret;
Sage Weil1d3576f2009-10-06 11:31:09 -0700129}
130
131/*
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
134 * data on the page.
135 */
Lukas Czernerd47992f2013-05-21 23:17:23 -0400136static void ceph_invalidatepage(struct page *page, unsigned int offset,
137 unsigned int length)
Sage Weil1d3576f2009-10-06 11:31:09 -0700138{
Alexander Beregalov4ce1e9a2010-02-22 17:17:44 +0300139 struct inode *inode;
Sage Weil1d3576f2009-10-06 11:31:09 -0700140 struct ceph_inode_info *ci;
Yan, Zheng61600ef2012-05-28 14:44:30 +0800141 struct ceph_snap_context *snapc = page_snap_context(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700142
Alexander Beregalov4ce1e9a2010-02-22 17:17:44 +0300143 inode = page->mapping->host;
Milosz Tanskib150f5c12013-08-09 12:59:55 -0400144 ci = ceph_inode(inode);
145
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300146 if (offset != 0 || length != PAGE_SIZE) {
Milosz Tanskib150f5c12013-08-09 12:59:55 -0400147 dout("%p invalidatepage %p idx %lu partial dirty page %u~%u\n",
148 inode, page, page->index, offset, length);
149 return;
150 }
Alexander Beregalov4ce1e9a2010-02-22 17:17:44 +0300151
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400152 ceph_invalidate_fscache_page(inode, page);
153
154 if (!PagePrivate(page))
155 return;
156
Sage Weil1d3576f2009-10-06 11:31:09 -0700157 /*
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.
161 */
162 if (!PageDirty(page))
163 pr_err("%p invalidatepage %p page not dirty\n", inode, page);
164
Milosz Tanskib150f5c12013-08-09 12:59:55 -0400165 ClearPageChecked(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700166
Milosz Tanskib150f5c12013-08-09 12:59:55 -0400167 dout("%p invalidatepage %p idx %lu full dirty page\n",
168 inode, page, page->index);
169
170 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
171 ceph_put_snap_context(snapc);
172 page->private = 0;
173 ClearPagePrivate(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700174}
175
Sage Weil1d3576f2009-10-06 11:31:09 -0700176static int ceph_releasepage(struct page *page, gfp_t g)
177{
NeilBrowne55f1a12016-08-31 12:59:29 +1000178 dout("%p releasepage %p idx %lu (%sdirty)\n", page->mapping->host,
179 page, page->index, PageDirty(page) ? "" : "not ");
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400180
181 /* Can we release the page from the cache? */
182 if (!ceph_release_fscache_page(page, g))
183 return 0;
184
185 return !PagePrivate(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700186}
187
188/*
189 * read a single page, without unlocking it.
190 */
191static int readpage_nounlock(struct file *filp, struct page *page)
192{
Al Viro496ad9a2013-01-23 17:07:38 -0500193 struct inode *inode = file_inode(filp);
Sage Weil1d3576f2009-10-06 11:31:09 -0700194 struct ceph_inode_info *ci = ceph_inode(inode);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400195 struct ceph_osd_client *osdc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700196 &ceph_inode_to_client(inode)->client->osdc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700197 int err = 0;
Yan, Zheng83701242014-11-14 22:36:18 +0800198 u64 off = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300199 u64 len = PAGE_SIZE;
Sage Weil1d3576f2009-10-06 11:31:09 -0700200
Yan, Zheng83701242014-11-14 22:36:18 +0800201 if (off >= i_size_read(inode)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300202 zero_user_segment(page, 0, PAGE_SIZE);
Yan, Zheng83701242014-11-14 22:36:18 +0800203 SetPageUptodate(page);
204 return 0;
205 }
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400206
Yan, Zhengfcc02d22015-01-10 11:43:12 +0800207 if (ci->i_inline_version != CEPH_INLINE_NONE) {
208 /*
209 * Uptodate inline data should have been added
210 * into page cache while getting Fcr caps.
211 */
212 if (off == 0)
213 return -EINVAL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300214 zero_user_segment(page, 0, PAGE_SIZE);
Yan, Zhengfcc02d22015-01-10 11:43:12 +0800215 SetPageUptodate(page);
216 return 0;
217 }
Yan, Zheng83701242014-11-14 22:36:18 +0800218
219 err = ceph_readpage_from_fscache(inode, page);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400220 if (err == 0)
221 goto out;
222
Sage Weil1d3576f2009-10-06 11:31:09 -0700223 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,
Yan, Zheng83701242014-11-14 22:36:18 +0800226 off, &len,
Sage Weil1d3576f2009-10-06 11:31:09 -0700227 ci->i_truncate_seq, ci->i_truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -0800228 &page, 1, 0);
Sage Weil1d3576f2009-10-06 11:31:09 -0700229 if (err == -ENOENT)
230 err = 0;
231 if (err < 0) {
232 SetPageError(page);
Li Wang18302802013-12-19 06:03:49 -0800233 ceph_fscache_readpage_cancel(inode, page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700234 goto out;
Sage Weil1d3576f2009-10-06 11:31:09 -0700235 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300236 if (err < PAGE_SIZE)
Zhang Zhen23cd5732014-05-28 15:09:55 +0800237 /* zero fill remainder of page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300238 zero_user_segment(page, err, PAGE_SIZE);
Zhang Zhen23cd5732014-05-28 15:09:55 +0800239 else
240 flush_dcache_page(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700241
Zhang Zhen23cd5732014-05-28 15:09:55 +0800242 SetPageUptodate(page);
243 ceph_readpage_to_fscache(inode, page);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400244
Sage Weil1d3576f2009-10-06 11:31:09 -0700245out:
246 return err < 0 ? err : 0;
247}
248
249static int ceph_readpage(struct file *filp, struct page *page)
250{
251 int r = readpage_nounlock(filp, page);
252 unlock_page(page);
253 return r;
254}
255
256/*
Sage Weil7c272192011-08-03 09:58:09 -0700257 * Finish an async read(ahead) op.
Sage Weil1d3576f2009-10-06 11:31:09 -0700258 */
Ilya Dryomov85e084f2016-04-28 16:07:24 +0200259static void finish_read(struct ceph_osd_request *req)
Sage Weil1d3576f2009-10-06 11:31:09 -0700260{
Sage Weil7c272192011-08-03 09:58:09 -0700261 struct inode *inode = req->r_inode;
Alex Elder87060c12013-04-03 01:28:58 -0500262 struct ceph_osd_data *osd_data;
Ilya Dryomov85e084f2016-04-28 16:07:24 +0200263 int rc = req->r_result <= 0 ? req->r_result : 0;
264 int bytes = req->r_result >= 0 ? req->r_result : 0;
Alex Eldere0c59482013-03-07 15:38:25 -0600265 int num_pages;
Sage Weil7c272192011-08-03 09:58:09 -0700266 int i;
267
Sage Weil7c272192011-08-03 09:58:09 -0700268 dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes);
269
270 /* unlock all pages, zeroing any data we didn't read */
Alex Elder406e2c92013-04-15 14:50:36 -0500271 osd_data = osd_req_op_extent_osd_data(req, 0);
Alex Elder87060c12013-04-03 01:28:58 -0500272 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);
Alex Eldere0c59482013-03-07 15:38:25 -0600275 for (i = 0; i < num_pages; i++) {
Alex Elder87060c12013-04-03 01:28:58 -0500276 struct page *page = osd_data->pages[i];
Sage Weil7c272192011-08-03 09:58:09 -0700277
Yan, Zheng368e3582016-05-17 11:58:02 +0800278 if (rc < 0 && rc != -ENOENT) {
279 ceph_fscache_readpage_cancel(inode, page);
Li Wangf36132a2013-11-27 22:28:13 +0800280 goto unlock;
Yan, Zheng368e3582016-05-17 11:58:02 +0800281 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300282 if (bytes < (int)PAGE_SIZE) {
Sage Weil7c272192011-08-03 09:58:09 -0700283 /* zero (remainder of) page */
284 int s = bytes < 0 ? 0 : bytes;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300285 zero_user_segment(page, s, PAGE_SIZE);
Sage Weil7c272192011-08-03 09:58:09 -0700286 }
287 dout("finish_read %p uptodate %p idx %lu\n", inode, page,
288 page->index);
289 flush_dcache_page(page);
290 SetPageUptodate(page);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400291 ceph_readpage_to_fscache(inode, page);
Li Wangf36132a2013-11-27 22:28:13 +0800292unlock:
Sage Weil7c272192011-08-03 09:58:09 -0700293 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300294 put_page(page);
295 bytes -= PAGE_SIZE;
Sage Weil7c272192011-08-03 09:58:09 -0700296 }
Alex Elder87060c12013-04-03 01:28:58 -0500297 kfree(osd_data->pages);
Sage Weil7c272192011-08-03 09:58:09 -0700298}
299
300/*
301 * start an async read(ahead) operation. return nr_pages we submitted
302 * a read for on success, or negative error code.
303 */
Sage Weil0d66a482011-08-04 08:21:30 -0700304static int start_read(struct inode *inode, struct list_head *page_list, int max)
Sage Weil7c272192011-08-03 09:58:09 -0700305{
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);
Alex Elderacead002013-03-14 14:09:05 -0500310 struct ceph_vino vino;
Sage Weil7c272192011-08-03 09:58:09 -0700311 struct ceph_osd_request *req;
312 u64 off;
313 u64 len;
314 int i;
Sage Weil1d3576f2009-10-06 11:31:09 -0700315 struct page **pages;
Sage Weil7c272192011-08-03 09:58:09 -0700316 pgoff_t next_index;
317 int nr_pages = 0;
318 int ret;
319
Alex Elder6285bc22012-10-02 10:25:51 -0500320 off = (u64) page_offset(page);
Sage Weil7c272192011-08-03 09:58:09 -0700321
322 /* count pages */
323 next_index = page->index;
324 list_for_each_entry_reverse(page, page_list, lru) {
325 if (page->index != next_index)
326 break;
327 nr_pages++;
328 next_index++;
Sage Weil0d66a482011-08-04 08:21:30 -0700329 if (max && nr_pages == max)
330 break;
Sage Weil7c272192011-08-03 09:58:09 -0700331 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300332 len = nr_pages << PAGE_SHIFT;
Sage Weil7c272192011-08-03 09:58:09 -0700333 dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages,
334 off, len);
Alex Elderacead002013-03-14 14:09:05 -0500335 vino = ceph_vino(inode);
336 req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len,
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800337 0, 1, CEPH_OSD_OP_READ,
Alex Elderacead002013-03-14 14:09:05 -0500338 CEPH_OSD_FLAG_READ, NULL,
Sage Weil7c272192011-08-03 09:58:09 -0700339 ci->i_truncate_seq, ci->i_truncate_size,
Alex Elderacead002013-03-14 14:09:05 -0500340 false);
Sage Weil68162822012-09-24 21:01:02 -0700341 if (IS_ERR(req))
342 return PTR_ERR(req);
Sage Weil1d3576f2009-10-06 11:31:09 -0700343
344 /* build page vector */
Alex Eldercf7b7e12013-03-01 18:00:15 -0600345 nr_pages = calc_pages_for(0, len);
Yan, Zheng687265e2015-06-13 17:27:05 +0800346 pages = kmalloc(sizeof(*pages) * nr_pages, GFP_KERNEL);
Sage Weil7c272192011-08-03 09:58:09 -0700347 ret = -ENOMEM;
Sage Weil1d3576f2009-10-06 11:31:09 -0700348 if (!pages)
Sage Weil7c272192011-08-03 09:58:09 -0700349 goto out;
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);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400354
Sage Weil7c272192011-08-03 09:58:09 -0700355 dout("start_read %p adding %p idx %lu\n", inode, page,
356 page->index);
357 if (add_to_page_cache_lru(page, &inode->i_data, page->index,
Yan, Zheng687265e2015-06-13 17:27:05 +0800358 GFP_KERNEL)) {
Milosz Tanskid4d3aa32013-09-03 19:11:17 -0400359 ceph_fscache_uncache_page(inode, page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300360 put_page(page);
Sage Weil7c272192011-08-03 09:58:09 -0700361 dout("start_read %p add_to_page_cache failed %p\n",
362 inode, page);
363 nr_pages = i;
Yan, Zheng1afe4782016-08-24 11:33:46 +0800364 if (nr_pages > 0) {
365 len = nr_pages << PAGE_SHIFT;
366 break;
367 }
Sage Weil7c272192011-08-03 09:58:09 -0700368 goto out_pages;
Sage Weil1d3576f2009-10-06 11:31:09 -0700369 }
Sage Weil7c272192011-08-03 09:58:09 -0700370 pages[i] = page;
Sage Weil1d3576f2009-10-06 11:31:09 -0700371 }
Alex Elder406e2c92013-04-15 14:50:36 -0500372 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false);
Sage Weil7c272192011-08-03 09:58:09 -0700373 req->r_callback = finish_read;
374 req->r_inode = inode;
375
376 dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len);
377 ret = ceph_osdc_start_request(osdc, req, false);
378 if (ret < 0)
379 goto out_pages;
380 ceph_osdc_put_request(req);
381 return nr_pages;
382
383out_pages:
Yan, Zheng1afe4782016-08-24 11:33:46 +0800384 for (i = 0; i < nr_pages; ++i) {
385 ceph_fscache_readpage_cancel(inode, pages[i]);
386 unlock_page(pages[i]);
387 }
388 ceph_put_page_vector(pages, nr_pages, false);
Sage Weil7c272192011-08-03 09:58:09 -0700389out:
390 ceph_osdc_put_request(req);
391 return ret;
Sage Weil1d3576f2009-10-06 11:31:09 -0700392}
393
Sage Weil7c272192011-08-03 09:58:09 -0700394
Sage Weil1d3576f2009-10-06 11:31:09 -0700395/*
396 * Read multiple pages. Leave pages we don't read + unlock in page_list;
397 * the caller (VM) cleans them up.
398 */
399static int ceph_readpages(struct file *file, struct address_space *mapping,
400 struct list_head *page_list, unsigned nr_pages)
401{
Al Viro496ad9a2013-01-23 17:07:38 -0500402 struct inode *inode = file_inode(file);
Sage Weil0d66a482011-08-04 08:21:30 -0700403 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700404 int rc = 0;
Sage Weil0d66a482011-08-04 08:21:30 -0700405 int max = 0;
Sage Weil1d3576f2009-10-06 11:31:09 -0700406
Yan, Zheng83701242014-11-14 22:36:18 +0800407 if (ceph_inode(inode)->i_inline_version != CEPH_INLINE_NONE)
408 return -EINVAL;
409
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400410 rc = ceph_readpages_from_fscache(mapping->host, mapping, page_list,
411 &nr_pages);
412
413 if (rc == 0)
414 goto out;
415
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300416 if (fsc->mount_options->rsize >= PAGE_SIZE)
417 max = (fsc->mount_options->rsize + PAGE_SIZE - 1)
Sage Weil0d66a482011-08-04 08:21:30 -0700418 >> PAGE_SHIFT;
419
Alex Elder2794a822013-02-14 12:16:43 -0600420 dout("readpages %p file %p nr_pages %d max %d\n", inode,
421 file, nr_pages,
Sage Weil0d66a482011-08-04 08:21:30 -0700422 max);
Sage Weil7c272192011-08-03 09:58:09 -0700423 while (!list_empty(page_list)) {
Sage Weil0d66a482011-08-04 08:21:30 -0700424 rc = start_read(inode, page_list, max);
Sage Weil7c272192011-08-03 09:58:09 -0700425 if (rc < 0)
426 goto out;
427 BUG_ON(rc == 0);
Sage Weil1d3576f2009-10-06 11:31:09 -0700428 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700429out:
Milosz Tanski76be7782013-08-21 17:30:27 -0400430 ceph_fscache_readpages_cancel(inode, page_list);
431
Sage Weil7c272192011-08-03 09:58:09 -0700432 dout("readpages %p file %p ret %d\n", inode, file, rc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700433 return rc;
434}
435
436/*
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.
Sage Weil1d3576f2009-10-06 11:31:09 -0700439 */
Sage Weil6298a332010-03-31 22:01:38 -0700440static struct ceph_snap_context *get_oldest_context(struct inode *inode,
Yan, Zhenge1966b42015-06-18 03:10:58 +0800441 loff_t *snap_size)
Sage Weil1d3576f2009-10-06 11:31:09 -0700442{
443 struct ceph_inode_info *ci = ceph_inode(inode);
444 struct ceph_snap_context *snapc = NULL;
445 struct ceph_cap_snap *capsnap = NULL;
446
Sage Weilbe655592011-11-30 09:47:09 -0800447 spin_lock(&ci->i_ceph_lock);
Sage Weil1d3576f2009-10-06 11:31:09 -0700448 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);
453 if (snap_size)
454 *snap_size = capsnap->size;
455 break;
456 }
457 }
Sage Weil7d8cb262010-08-24 08:44:16 -0700458 if (!snapc && ci->i_wrbuffer_ref_head) {
Sage Weil80e755f2010-03-31 21:52:10 -0700459 snapc = ceph_get_snap_context(ci->i_head_snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700460 dout(" head snapc %p has %d dirty pages\n",
461 snapc, ci->i_wrbuffer_ref_head);
462 }
Sage Weilbe655592011-11-30 09:47:09 -0800463 spin_unlock(&ci->i_ceph_lock);
Sage Weil1d3576f2009-10-06 11:31:09 -0700464 return snapc;
465}
466
467/*
468 * Write a single page, but leave the page locked.
469 *
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).
472 */
473static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
474{
475 struct inode *inode;
476 struct ceph_inode_info *ci;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700477 struct ceph_fs_client *fsc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700478 struct ceph_osd_client *osdc;
Sage Weil6298a332010-03-31 22:01:38 -0700479 struct ceph_snap_context *snapc, *oldest;
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800480 loff_t page_off = page_offset(page);
Yan, Zhenge1966b42015-06-18 03:10:58 +0800481 loff_t snap_size = -1;
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800482 long writeback_stat;
Yan, Zhenge1966b42015-06-18 03:10:58 +0800483 u64 truncate_size;
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800484 u32 truncate_seq;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300485 int err = 0, len = PAGE_SIZE;
Sage Weil1d3576f2009-10-06 11:31:09 -0700486
487 dout("writepage %p idx %lu\n", page, page->index);
488
489 if (!page->mapping || !page->mapping->host) {
490 dout("writepage %p - no mapping\n", page);
491 return -EFAULT;
492 }
493 inode = page->mapping->host;
494 ci = ceph_inode(inode);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700495 fsc = ceph_inode_to_client(inode);
496 osdc = &fsc->client->osdc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700497
498 /* verify this is a writeable snap context */
Yan, Zheng61600ef2012-05-28 14:44:30 +0800499 snapc = page_snap_context(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700500 if (snapc == NULL) {
501 dout("writepage %p page %p not dirty?\n", inode, page);
502 goto out;
503 }
Sage Weil6298a332010-03-31 22:01:38 -0700504 oldest = get_oldest_context(inode, &snap_size);
505 if (snapc->seq > oldest->seq) {
Sage Weil1d3576f2009-10-06 11:31:09 -0700506 dout("writepage %p page %p snapc %p not writeable - noop\n",
Yan, Zheng61600ef2012-05-28 14:44:30 +0800507 inode, page, snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700508 /* we should only noop if called by kswapd */
509 WARN_ON((current->flags & PF_MEMALLOC) == 0);
Sage Weil6298a332010-03-31 22:01:38 -0700510 ceph_put_snap_context(oldest);
Sage Weil1d3576f2009-10-06 11:31:09 -0700511 goto out;
512 }
Sage Weil6298a332010-03-31 22:01:38 -0700513 ceph_put_snap_context(oldest);
Sage Weil1d3576f2009-10-06 11:31:09 -0700514
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800515 spin_lock(&ci->i_ceph_lock);
516 truncate_seq = ci->i_truncate_seq;
517 truncate_size = ci->i_truncate_size;
Yan, Zhenge1966b42015-06-18 03:10:58 +0800518 if (snap_size == -1)
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800519 snap_size = i_size_read(inode);
520 spin_unlock(&ci->i_ceph_lock);
521
Sage Weil1d3576f2009-10-06 11:31:09 -0700522 /* is this a partial page at end of file? */
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800523 if (page_off >= snap_size) {
524 dout("%p page eof %llu\n", page, snap_size);
525 goto out;
526 }
527 if (snap_size < page_off + len)
528 len = snap_size - page_off;
Sage Weil1d3576f2009-10-06 11:31:09 -0700529
Sage Weilae00d4f2010-09-16 16:26:51 -0700530 dout("writepage %p page %p index %lu on %llu~%u snapc %p\n",
531 inode, page, page->index, page_off, len, snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700532
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700533 writeback_stat = atomic_long_inc_return(&fsc->writeback_count);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800534 if (writeback_stat >
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700535 CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
536 set_bdi_congested(&fsc->backing_dev_info, BLK_RW_ASYNC);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800537
Sage Weil1d3576f2009-10-06 11:31:09 -0700538 set_page_writeback(page);
539 err = ceph_osdc_writepages(osdc, ceph_vino(inode),
540 &ci->i_layout, snapc,
541 page_off, len,
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800542 truncate_seq, truncate_size,
Alex Elder24808822013-02-15 11:42:29 -0600543 &inode->i_mtime, &page, 1);
Sage Weil1d3576f2009-10-06 11:31:09 -0700544 if (err < 0) {
Yan, Zhengad15ec02016-05-13 17:29:51 +0800545 struct writeback_control tmp_wbc;
546 if (!wbc)
547 wbc = &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);
553 goto out;
554 }
555 dout("writepage setting page/mapping error %d %p\n",
556 err, page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700557 SetPageError(page);
558 mapping_set_error(&inode->i_data, err);
Yan, Zhengad15ec02016-05-13 17:29:51 +0800559 wbc->pages_skipped++;
Sage Weil1d3576f2009-10-06 11:31:09 -0700560 } else {
561 dout("writepage cleaned page %p\n", page);
562 err = 0; /* vfs expects us to return 0 */
563 }
564 page->private = 0;
565 ClearPagePrivate(page);
566 end_page_writeback(page);
567 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
Sage Weil6298a332010-03-31 22:01:38 -0700568 ceph_put_snap_context(snapc); /* page's reference */
Sage Weil1d3576f2009-10-06 11:31:09 -0700569out:
570 return err;
571}
572
573static int ceph_writepage(struct page *page, struct writeback_control *wbc)
574{
Yehuda Sadehdbd646a2009-12-16 14:51:06 -0800575 int err;
576 struct inode *inode = page->mapping->host;
577 BUG_ON(!inode);
Sage Weil70b666c2011-05-27 09:24:26 -0700578 ihold(inode);
Yehuda Sadehdbd646a2009-12-16 14:51:06 -0800579 err = writepage_nounlock(page, wbc);
Yan, Zhengad15ec02016-05-13 17:29:51 +0800580 if (err == -ERESTARTSYS) {
581 /* direct memory reclaimer was killed by SIGKILL. return 0
582 * to prevent caller from setting mapping/page error */
583 err = 0;
584 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700585 unlock_page(page);
Yehuda Sadehdbd646a2009-12-16 14:51:06 -0800586 iput(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700587 return err;
588}
589
Sage Weil1d3576f2009-10-06 11:31:09 -0700590/*
591 * lame release_pages helper. release_pages() isn't exported to
592 * modules.
593 */
594static void ceph_release_pages(struct page **pages, int num)
595{
596 struct pagevec pvec;
597 int i;
598
599 pagevec_init(&pvec, 0);
600 for (i = 0; i < num; i++) {
601 if (pagevec_add(&pvec, pages[i]) == 0)
602 pagevec_release(&pvec);
603 }
604 pagevec_release(&pvec);
605}
606
Sage Weil1d3576f2009-10-06 11:31:09 -0700607/*
608 * async writeback completion handler.
609 *
610 * If we get an error, set the mapping error bit, but not the individual
611 * page error bits.
612 */
Ilya Dryomov85e084f2016-04-28 16:07:24 +0200613static void writepages_finish(struct ceph_osd_request *req)
Sage Weil1d3576f2009-10-06 11:31:09 -0700614{
615 struct inode *inode = req->r_inode;
Sage Weil1d3576f2009-10-06 11:31:09 -0700616 struct ceph_inode_info *ci = ceph_inode(inode);
Alex Elder87060c12013-04-03 01:28:58 -0500617 struct ceph_osd_data *osd_data;
Sage Weil1d3576f2009-10-06 11:31:09 -0700618 struct page *page;
Yan, Zheng5b646402016-01-07 16:00:17 +0800619 int num_pages, total_pages = 0;
620 int i, j;
621 int rc = req->r_result;
Sage Weil1d3576f2009-10-06 11:31:09 -0700622 struct ceph_snap_context *snapc = req->r_snapc;
623 struct address_space *mapping = inode->i_mapping;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700624 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
Yan, Zheng5b646402016-01-07 16:00:17 +0800625 bool remove_page;
Sage Weil1d3576f2009-10-06 11:31:09 -0700626
Yan, Zheng5b646402016-01-07 16:00:17 +0800627 dout("writepages_finish %p rc %d\n", inode, rc);
628 if (rc < 0)
Sage Weil1d3576f2009-10-06 11:31:09 -0700629 mapping_set_error(mapping, rc);
Yan, Zheng5b646402016-01-07 16:00:17 +0800630
631 /*
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
635 * raced its way in
636 */
637 remove_page = !(ceph_caps_issued(ci) &
638 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
Sage Weil1d3576f2009-10-06 11:31:09 -0700639
640 /* clean all pages */
Yan, Zheng5b646402016-01-07 16:00:17 +0800641 for (i = 0; i < req->r_num_ops; i++) {
642 if (req->r_ops[i].op != CEPH_OSD_OP_WRITE)
643 break;
Sage Weil1d3576f2009-10-06 11:31:09 -0700644
Yan, Zheng5b646402016-01-07 16:00:17 +0800645 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];
652 BUG_ON(!page);
653 WARN_ON(!PageUptodate(page));
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800654
Yan, Zheng5b646402016-01-07 16:00:17 +0800655 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,
659 BLK_RW_ASYNC);
Yehuda Sadehe63dc5c2010-02-19 00:07:01 +0000660
Yan, Zhengb109eec62016-05-13 17:54:17 +0800661 if (rc < 0)
662 SetPageError(page);
663
Yan, Zheng5b646402016-01-07 16:00:17 +0800664 ceph_put_snap_context(page_snap_context(page));
665 page->private = 0;
666 ClearPagePrivate(page);
667 dout("unlocking %p\n", page);
668 end_page_writeback(page);
Yehuda Sadehe63dc5c2010-02-19 00:07:01 +0000669
Yan, Zheng5b646402016-01-07 16:00:17 +0800670 if (remove_page)
671 generic_error_remove_page(inode->i_mapping,
672 page);
673
674 unlock_page(page);
675 }
676 dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n",
677 inode, osd_data->length, rc >= 0 ? num_pages : 0);
678
679 ceph_release_pages(osd_data->pages, num_pages);
Sage Weil1d3576f2009-10-06 11:31:09 -0700680 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700681
Yan, Zheng5b646402016-01-07 16:00:17 +0800682 ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
683
684 osd_data = osd_req_op_extent_osd_data(req, 0);
Alex Elder87060c12013-04-03 01:28:58 -0500685 if (osd_data->pages_from_pool)
686 mempool_free(osd_data->pages,
Cheng Renquan640ef792010-03-26 17:40:33 +0800687 ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool);
Sage Weil1d3576f2009-10-06 11:31:09 -0700688 else
Alex Elder87060c12013-04-03 01:28:58 -0500689 kfree(osd_data->pages);
Sage Weil1d3576f2009-10-06 11:31:09 -0700690 ceph_osdc_put_request(req);
691}
692
Sage Weil1d3576f2009-10-06 11:31:09 -0700693/*
694 * initiate async writeback
695 */
696static int ceph_writepages_start(struct address_space *mapping,
697 struct writeback_control *wbc)
698{
699 struct inode *inode = mapping->host;
Sage Weil1d3576f2009-10-06 11:31:09 -0700700 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800701 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
702 struct ceph_vino vino = ceph_vino(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700703 pgoff_t index, start, end;
704 int range_whole = 0;
705 int should_loop = 1;
706 pgoff_t max_pages = 0, max_pages_ever = 0;
Sage Weil80e755f2010-03-31 21:52:10 -0700707 struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700708 struct pagevec pvec;
709 int done = 0;
710 int rc = 0;
711 unsigned wsize = 1 << inode->i_blkbits;
712 struct ceph_osd_request *req = NULL;
Dan Carpenter021b77b2014-11-28 11:33:34 +0300713 int do_sync = 0;
Yan, Zhenge1966b42015-06-18 03:10:58 +0800714 loff_t snap_size, i_size;
715 u64 truncate_size;
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800716 u32 truncate_seq;
Sage Weil1d3576f2009-10-06 11:31:09 -0700717
718 /*
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.
722 */
majianpengc62988e2013-06-19 15:12:06 +0800723 if ((wbc->sync_mode == WB_SYNC_ALL) ||
724 ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER))
Sage Weil1d3576f2009-10-06 11:31:09 -0700725 do_sync = 1;
726 dout("writepages_start %p dosync=%d (mode=%s)\n",
727 inode, do_sync,
728 wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
729 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
730
Yan, Zheng48fec5d2015-07-01 16:27:46 +0800731 if (ACCESS_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
Yan, Zheng6c93df52016-04-15 13:56:12 +0800732 if (ci->i_wrbuffer_ref > 0) {
733 pr_warn_ratelimited(
734 "writepage_start %p %lld forced umount\n",
735 inode, ceph_ino(inode));
736 }
Yan, Zhenga341d4d2015-07-01 17:03:23 +0800737 mapping_set_error(mapping, -EIO);
Sage Weil1d3576f2009-10-06 11:31:09 -0700738 return -EIO; /* we're in a forced umount, don't write! */
739 }
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700740 if (fsc->mount_options->wsize && fsc->mount_options->wsize < wsize)
741 wsize = fsc->mount_options->wsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300742 if (wsize < PAGE_SIZE)
743 wsize = PAGE_SIZE;
744 max_pages_ever = wsize >> PAGE_SHIFT;
Sage Weil1d3576f2009-10-06 11:31:09 -0700745
746 pagevec_init(&pvec, 0);
747
Sage Weil1d3576f2009-10-06 11:31:09 -0700748 /* where to start/end? */
749 if (wbc->range_cyclic) {
750 start = mapping->writeback_index; /* Start from prev offset */
751 end = -1;
752 dout(" cyclic, start at %lu\n", start);
753 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300754 start = wbc->range_start >> PAGE_SHIFT;
755 end = wbc->range_end >> PAGE_SHIFT;
Sage Weil1d3576f2009-10-06 11:31:09 -0700756 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
757 range_whole = 1;
758 should_loop = 0;
759 dout(" not cyclic, %lu to %lu\n", start, end);
760 }
761 index = start;
762
763retry:
764 /* find oldest snap context with dirty data */
765 ceph_put_snap_context(snapc);
Yan, Zhenge1966b42015-06-18 03:10:58 +0800766 snap_size = -1;
Sage Weil1d3576f2009-10-06 11:31:09 -0700767 snapc = get_oldest_context(inode, &snap_size);
768 if (!snapc) {
769 /* hmm, why does writepages get called when there
770 is no dirty data? */
771 dout(" no snap context with dirty data?\n");
772 goto out;
773 }
774 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
775 snapc, snapc->seq, snapc->num_snaps);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800776
777 spin_lock(&ci->i_ceph_lock);
778 truncate_seq = ci->i_truncate_seq;
779 truncate_size = ci->i_truncate_size;
Yan, Zhenge1966b42015-06-18 03:10:58 +0800780 i_size = i_size_read(inode);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800781 spin_unlock(&ci->i_ceph_lock);
782
Sage Weil1d3576f2009-10-06 11:31:09 -0700783 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",
787 index);
788 index = start;
789 }
790 last_snapc = snapc;
791
792 while (!done && index <= end) {
793 unsigned i;
794 int first;
Yan, Zheng5b646402016-01-07 16:00:17 +0800795 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;
Alex Eldere5975c72013-03-14 14:09:05 -0500799 mempool_t *pool = NULL; /* Becomes non-null if mempool used */
Sage Weil1d3576f2009-10-06 11:31:09 -0700800 struct page *page;
801 int want;
Yan, Zheng5b646402016-01-07 16:00:17 +0800802 u64 offset = 0, len = 0;
Sage Weil1d3576f2009-10-06 11:31:09 -0700803
Sage Weil1d3576f2009-10-06 11:31:09 -0700804 max_pages = max_pages_ever;
805
806get_more_pages:
807 first = -1;
808 want = min(end - index,
809 min((pgoff_t)PAGEVEC_SIZE,
810 max_pages - (pgoff_t)locked_pages) - 1)
811 + 1;
812 pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index,
813 PAGECACHE_TAG_DIRTY,
814 want);
815 dout("pagevec_lookup_tag got %d\n", pvec_pages);
816 if (!pvec_pages && !locked_pages)
817 break;
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))
824 break;
825
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);
830 unlock_page(page);
831 break;
832 }
833 if (!wbc->range_cyclic && page->index > end) {
834 dout("end of range %p\n", page);
835 done = 1;
836 unlock_page(page);
837 break;
838 }
Yan, Zheng5b646402016-01-07 16:00:17 +0800839 if (strip_unit_end && (page->index > strip_unit_end)) {
840 dout("end of strip unit %p\n", page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700841 unlock_page(page);
842 break;
843 }
844 if (wbc->sync_mode != WB_SYNC_NONE) {
845 dout("waiting on writeback %p\n", page);
846 wait_on_page_writeback(page);
847 }
Yan, Zhenge1966b42015-06-18 03:10:58 +0800848 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));
Sage Weil1d3576f2009-10-06 11:31:09 -0700852 done = 1;
853 unlock_page(page);
854 break;
855 }
856 if (PageWriteback(page)) {
857 dout("%p under writeback\n", page);
858 unlock_page(page);
859 break;
860 }
861
862 /* only if matching snap context */
Yan, Zheng61600ef2012-05-28 14:44:30 +0800863 pgsnapc = page_snap_context(page);
Sage Weil80e755f2010-03-31 21:52:10 -0700864 if (pgsnapc->seq > snapc->seq) {
865 dout("page snapc %p %lld > oldest %p %lld\n",
866 pgsnapc, pgsnapc->seq, snapc, snapc->seq);
Sage Weil1d3576f2009-10-06 11:31:09 -0700867 unlock_page(page);
868 if (!locked_pages)
869 continue; /* keep looking for snap */
870 break;
871 }
872
873 if (!clear_page_dirty_for_io(page)) {
874 dout("%p !clear_page_dirty_for_io\n", page);
875 unlock_page(page);
876 break;
877 }
878
Alex Eldere5975c72013-03-14 14:09:05 -0500879 /*
880 * We have something to write. If this is
881 * the first locked page this time through,
Yan, Zheng5b646402016-01-07 16:00:17 +0800882 * calculate max possinle write size and
883 * allocate a page array
Alex Eldere5975c72013-03-14 14:09:05 -0500884 */
Sage Weil1d3576f2009-10-06 11:31:09 -0700885 if (locked_pages == 0) {
Yan, Zheng5b646402016-01-07 16:00:17 +0800886 u64 objnum;
887 u64 objoff;
888
Sage Weil1d3576f2009-10-06 11:31:09 -0700889 /* prepare async write request */
Alex Eldere5975c72013-03-14 14:09:05 -0500890 offset = (u64)page_offset(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700891 len = wsize;
Yan, Zheng5b646402016-01-07 16:00:17 +0800892
893 rc = ceph_calc_file_object_mapping(&ci->i_layout,
894 offset, len,
895 &objnum, &objoff,
896 &len);
897 if (rc < 0) {
Henry C Chang8c718972011-05-03 09:45:16 +0000898 unlock_page(page);
899 break;
900 }
901
Yan, Zheng5b646402016-01-07 16:00:17 +0800902 num_ops = 1 + do_sync;
903 strip_unit_end = page->index +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300904 ((len - 1) >> PAGE_SHIFT);
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800905
Yan, Zheng5b646402016-01-07 16:00:17 +0800906 BUG_ON(pages);
Alex Elder88486952013-03-14 14:09:05 -0500907 max_pages = calc_pages_for(0, (u64)len);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800908 pages = kmalloc(max_pages * sizeof (*pages),
909 GFP_NOFS);
Alex Elder88486952013-03-14 14:09:05 -0500910 if (!pages) {
911 pool = fsc->wb_pagevec_pool;
Alex Elder88486952013-03-14 14:09:05 -0500912 pages = mempool_alloc(pool, GFP_NOFS);
Alex Eldere5975c72013-03-14 14:09:05 -0500913 BUG_ON(!pages);
Alex Elder88486952013-03-14 14:09:05 -0500914 }
Yan, Zheng5b646402016-01-07 16:00:17 +0800915
916 len = 0;
917 } else if (page->index !=
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300918 (offset + len) >> PAGE_SHIFT) {
Yan, Zheng5b646402016-01-07 16:00:17 +0800919 if (num_ops >= (pool ? CEPH_OSD_SLAB_OPS :
920 CEPH_OSD_MAX_OPS)) {
921 redirty_page_for_writepage(wbc, page);
922 unlock_page(page);
923 break;
924 }
925
926 num_ops++;
927 offset = (u64)page_offset(page);
928 len = 0;
Sage Weil1d3576f2009-10-06 11:31:09 -0700929 }
930
931 /* note position of first page in pvec */
932 if (first < 0)
933 first = i;
934 dout("%p will write page %p idx %lu\n",
935 inode, page, page->index);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800936
Yan, Zheng5b646402016-01-07 16:00:17 +0800937 if (atomic_long_inc_return(&fsc->writeback_count) >
938 CONGESTION_ON_THRESH(
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700939 fsc->mount_options->congestion_kb)) {
940 set_bdi_congested(&fsc->backing_dev_info,
Sage Weil213c99e2010-08-03 10:25:11 -0700941 BLK_RW_ASYNC);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800942 }
943
Alex Eldere5975c72013-03-14 14:09:05 -0500944 pages[locked_pages] = page;
Sage Weil1d3576f2009-10-06 11:31:09 -0700945 locked_pages++;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300946 len += PAGE_SIZE;
Sage Weil1d3576f2009-10-06 11:31:09 -0700947 }
948
949 /* did we get anything? */
950 if (!locked_pages)
951 goto release_pvec_pages;
952 if (i) {
953 int j;
954 BUG_ON(!locked_pages || first < 0);
955
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);
960 goto get_more_pages;
961 }
962
963 /* shift unused pages over in the pvec... we
964 * will need to release them below. */
965 for (j = i; j < pvec_pages; j++) {
Yan, Zheng5b646402016-01-07 16:00:17 +0800966 dout(" pvec leftover page %p\n", pvec.pages[j]);
Sage Weil1d3576f2009-10-06 11:31:09 -0700967 pvec.pages[j-i+first] = pvec.pages[j];
968 }
969 pvec.nr -= i-first;
970 }
971
Yan, Zheng5b646402016-01-07 16:00:17 +0800972new_request:
Alex Eldere5975c72013-03-14 14:09:05 -0500973 offset = page_offset(pages[0]);
Yan, Zheng5b646402016-01-07 16:00:17 +0800974 len = wsize;
975
976 req = ceph_osdc_new_request(&fsc->client->osdc,
977 &ci->i_layout, vino,
978 offset, &len, 0, num_ops,
979 CEPH_OSD_OP_WRITE,
980 CEPH_OSD_FLAG_WRITE |
981 CEPH_OSD_FLAG_ONDISK,
982 snapc, truncate_seq,
983 truncate_size, false);
984 if (IS_ERR(req)) {
985 req = ceph_osdc_new_request(&fsc->client->osdc,
986 &ci->i_layout, vino,
987 offset, &len, 0,
988 min(num_ops,
989 CEPH_OSD_SLAB_OPS),
990 CEPH_OSD_OP_WRITE,
991 CEPH_OSD_FLAG_WRITE |
992 CEPH_OSD_FLAG_ONDISK,
993 snapc, truncate_seq,
994 truncate_size, true);
995 BUG_ON(IS_ERR(req));
Yan, Zhenge1966b42015-06-18 03:10:58 +0800996 }
Yan, Zheng5b646402016-01-07 16:00:17 +0800997 BUG_ON(len < page_offset(pages[locked_pages - 1]) +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300998 PAGE_SIZE - offset);
Sage Weil1d3576f2009-10-06 11:31:09 -0700999
Yan, Zheng5b646402016-01-07 16:00:17 +08001000 req->r_callback = writepages_finish;
1001 req->r_inode = inode;
1002
1003 /* Format the osd request message and submit the write */
1004 len = 0;
1005 data_pages = pages;
1006 op_idx = 0;
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)
1011 break;
1012 osd_req_op_extent_dup_last(req, op_idx,
1013 cur_offset - offset);
1014 dout("writepages got pages at %llu~%llu\n",
1015 offset, len);
1016 osd_req_op_extent_osd_data_pages(req, op_idx,
1017 data_pages, len, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05001018 !!pool, false);
Yan, Zheng5b646402016-01-07 16:00:17 +08001019 osd_req_op_extent_update(req, op_idx, len);
Alex Eldere5975c72013-03-14 14:09:05 -05001020
Yan, Zheng5b646402016-01-07 16:00:17 +08001021 len = 0;
1022 offset = cur_offset;
1023 data_pages = pages + i;
1024 op_idx++;
1025 }
1026
1027 set_page_writeback(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001028 len += PAGE_SIZE;
Yan, Zheng5b646402016-01-07 16:00:17 +08001029 }
1030
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 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001037 u64 min_len = len + 1 - PAGE_SIZE;
Yan, Zheng5b646402016-01-07 16:00:17 +08001038 len = min(len, (u64)i_size_read(inode) - offset);
1039 len = max(len, min_len);
1040 }
1041 dout("writepages got pages at %llu~%llu\n", offset, len);
1042
1043 osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len,
1044 0, !!pool, false);
1045 osd_req_op_extent_update(req, op_idx, len);
1046
1047 if (do_sync) {
1048 op_idx++;
1049 osd_req_op_init(req, op_idx, CEPH_OSD_OP_STARTSYNC, 0);
1050 }
1051 BUG_ON(op_idx + 1 != req->r_num_ops);
1052
Alex Eldere5975c72013-03-14 14:09:05 -05001053 pool = NULL;
Yan, Zheng5b646402016-01-07 16:00:17 +08001054 if (i < locked_pages) {
1055 BUG_ON(num_ops <= req->r_num_ops);
1056 num_ops -= req->r_num_ops;
1057 num_ops += do_sync;
1058 locked_pages -= i;
Alex Eldere5975c72013-03-14 14:09:05 -05001059
Yan, Zheng5b646402016-01-07 16:00:17 +08001060 /* allocate new pages array for next request */
1061 data_pages = pages;
1062 pages = kmalloc(locked_pages * sizeof (*pages),
1063 GFP_NOFS);
1064 if (!pages) {
1065 pool = fsc->wb_pagevec_pool;
1066 pages = mempool_alloc(pool, GFP_NOFS);
1067 BUG_ON(!pages);
1068 }
1069 memcpy(pages, data_pages + i,
1070 locked_pages * sizeof(*pages));
1071 memset(data_pages + i, 0,
1072 locked_pages * sizeof(*pages));
1073 } else {
1074 BUG_ON(num_ops != req->r_num_ops);
1075 index = pages[i - 1]->index + 1;
1076 /* request message now owns the pages array */
1077 pages = NULL;
1078 }
Alex Eldere5975c72013-03-14 14:09:05 -05001079
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001080 req->r_mtime = inode->i_mtime;
Sage Weil9d6fcb02011-05-12 15:48:16 -07001081 rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
1082 BUG_ON(rc);
Sage Weil1d3576f2009-10-06 11:31:09 -07001083 req = NULL;
1084
Yan, Zheng5b646402016-01-07 16:00:17 +08001085 wbc->nr_to_write -= i;
1086 if (pages)
1087 goto new_request;
1088
Sage Weil1d3576f2009-10-06 11:31:09 -07001089 if (wbc->nr_to_write <= 0)
1090 done = 1;
1091
1092release_pvec_pages:
1093 dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
1094 pvec.nr ? pvec.pages[0] : NULL);
1095 pagevec_release(&pvec);
1096
1097 if (locked_pages && !done)
1098 goto retry;
1099 }
1100
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");
1104 should_loop = 0;
1105 index = 0;
1106 goto retry;
1107 }
1108
1109 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1110 mapping->writeback_index = index;
1111
1112out:
Ilya Dryomov3ed97d62016-04-26 15:05:29 +02001113 ceph_osdc_put_request(req);
Sage Weil1d3576f2009-10-06 11:31:09 -07001114 ceph_put_snap_context(snapc);
1115 dout("writepages done, rc = %d\n", rc);
Sage Weil1d3576f2009-10-06 11:31:09 -07001116 return rc;
1117}
1118
1119
1120
1121/*
1122 * See if a given @snapc is either writeable, or already written.
1123 */
1124static int context_is_writeable_or_written(struct inode *inode,
1125 struct ceph_snap_context *snapc)
1126{
1127 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL);
Sage Weil6298a332010-03-31 22:01:38 -07001128 int ret = !oldest || snapc->seq <= oldest->seq;
1129
1130 ceph_put_snap_context(oldest);
1131 return ret;
Sage Weil1d3576f2009-10-06 11:31:09 -07001132}
1133
1134/*
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.
Sage Weil8f883c22010-03-19 13:27:53 -07001137 *
1138 * called with page locked.
1139 * return success with page locked,
1140 * or any failure (incl -EAGAIN) with page unlocked.
Sage Weil1d3576f2009-10-06 11:31:09 -07001141 */
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001142static int ceph_update_writeable_page(struct file *file,
1143 loff_t pos, unsigned len,
1144 struct page *page)
Sage Weil1d3576f2009-10-06 11:31:09 -07001145{
Al Viro496ad9a2013-01-23 17:07:38 -05001146 struct inode *inode = file_inode(file);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001147 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -07001148 struct ceph_inode_info *ci = ceph_inode(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001149 loff_t page_off = pos & PAGE_MASK;
1150 int pos_in_page = pos & ~PAGE_MASK;
Sage Weil1d3576f2009-10-06 11:31:09 -07001151 int end_in_page = pos_in_page + len;
1152 loff_t i_size;
Sage Weil1d3576f2009-10-06 11:31:09 -07001153 int r;
Sage Weil80e755f2010-03-31 21:52:10 -07001154 struct ceph_snap_context *snapc, *oldest;
Sage Weil1d3576f2009-10-06 11:31:09 -07001155
Yan, Zheng6c93df52016-04-15 13:56:12 +08001156 if (ACCESS_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
1157 dout(" page %p forced umount\n", page);
1158 unlock_page(page);
1159 return -EIO;
1160 }
1161
Sage Weil1d3576f2009-10-06 11:31:09 -07001162retry_locked:
1163 /* writepages currently holds page lock, but if we change that later, */
1164 wait_on_page_writeback(page);
1165
Yan, Zheng61600ef2012-05-28 14:44:30 +08001166 snapc = page_snap_context(page);
Sage Weil80e755f2010-03-31 21:52:10 -07001167 if (snapc && snapc != ci->i_head_snapc) {
Sage Weil1d3576f2009-10-06 11:31:09 -07001168 /*
1169 * this page is already dirty in another (older) snap
1170 * context! is it writeable now?
1171 */
Sage Weil80e755f2010-03-31 21:52:10 -07001172 oldest = get_oldest_context(inode, NULL);
Sage Weil1d3576f2009-10-06 11:31:09 -07001173
Sage Weil80e755f2010-03-31 21:52:10 -07001174 if (snapc->seq > oldest->seq) {
Sage Weil6298a332010-03-31 22:01:38 -07001175 ceph_put_snap_context(oldest);
Sage Weil1d3576f2009-10-06 11:31:09 -07001176 dout(" page %p snapc %p not current or oldest\n",
Sage Weil6298a332010-03-31 22:01:38 -07001177 page, snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -07001178 /*
1179 * queue for writeback, and wait for snapc to
1180 * be writeable or written
1181 */
Sage Weil6298a332010-03-31 22:01:38 -07001182 snapc = ceph_get_snap_context(snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -07001183 unlock_page(page);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001184 ceph_queue_writeback(inode);
Yan, Zhenga78bbd42016-05-13 11:30:24 +08001185 r = wait_event_killable(ci->i_cap_wq,
Sage Weil1d3576f2009-10-06 11:31:09 -07001186 context_is_writeable_or_written(inode, snapc));
1187 ceph_put_snap_context(snapc);
Sage Weil8f883c22010-03-19 13:27:53 -07001188 if (r == -ERESTARTSYS)
1189 return r;
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001190 return -EAGAIN;
Sage Weil1d3576f2009-10-06 11:31:09 -07001191 }
Sage Weil6298a332010-03-31 22:01:38 -07001192 ceph_put_snap_context(oldest);
Sage Weil1d3576f2009-10-06 11:31:09 -07001193
1194 /* yay, writeable, do it now (without dropping page lock) */
1195 dout(" page %p snapc %p not current, but oldest\n",
1196 page, snapc);
1197 if (!clear_page_dirty_for_io(page))
1198 goto retry_locked;
1199 r = writepage_nounlock(page, NULL);
1200 if (r < 0)
1201 goto fail_nosnap;
1202 goto retry_locked;
1203 }
1204
1205 if (PageUptodate(page)) {
1206 dout(" page %p already uptodate\n", page);
1207 return 0;
1208 }
1209
1210 /* full page? */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001211 if (pos_in_page == 0 && len == PAGE_SIZE)
Sage Weil1d3576f2009-10-06 11:31:09 -07001212 return 0;
1213
1214 /* past end of file? */
Yan, Zheng99c88e62015-12-30 11:32:46 +08001215 i_size = i_size_read(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -07001216
Sage Weil1d3576f2009-10-06 11:31:09 -07001217 if (page_off >= i_size ||
1218 (pos_in_page == 0 && (pos+len) >= i_size &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001219 end_in_page - pos_in_page != PAGE_SIZE)) {
Sage Weil1d3576f2009-10-06 11:31:09 -07001220 dout(" zeroing %p 0 - %d and %d - %d\n",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001221 page, pos_in_page, end_in_page, (int)PAGE_SIZE);
Sage Weil1d3576f2009-10-06 11:31:09 -07001222 zero_user_segments(page,
1223 0, pos_in_page,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001224 end_in_page, PAGE_SIZE);
Sage Weil1d3576f2009-10-06 11:31:09 -07001225 return 0;
1226 }
1227
1228 /* we need to read it. */
Sage Weil1d3576f2009-10-06 11:31:09 -07001229 r = readpage_nounlock(file, page);
1230 if (r < 0)
1231 goto fail_nosnap;
1232 goto retry_locked;
Sage Weil1d3576f2009-10-06 11:31:09 -07001233fail_nosnap:
1234 unlock_page(page);
1235 return r;
1236}
1237
1238/*
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001239 * We are only allowed to write into/dirty the page if the page is
1240 * clean, or already dirty within the same snap context.
1241 */
1242static 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)
1245{
Al Viro496ad9a2013-01-23 17:07:38 -05001246 struct inode *inode = file_inode(file);
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001247 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001248 pgoff_t index = pos >> PAGE_SHIFT;
Sage Weil7971bd92013-05-01 21:15:58 -07001249 int r;
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001250
1251 do {
Sage Weil8f883c22010-03-19 13:27:53 -07001252 /* get a page */
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001253 page = grab_cache_page_write_begin(mapping, index, 0);
Sage Weil7971bd92013-05-01 21:15:58 -07001254 if (!page)
1255 return -ENOMEM;
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001256
1257 dout("write_begin file %p inode %p page %p %d~%d\n", file,
Sage Weil213c99e2010-08-03 10:25:11 -07001258 inode, page, (int)pos, (int)len);
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001259
1260 r = ceph_update_writeable_page(file, pos, len, page);
Taesoo Kimc1d00b22015-03-20 17:36:56 -04001261 if (r < 0)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001262 put_page(page);
Taesoo Kimc1d00b22015-03-20 17:36:56 -04001263 else
1264 *pagep = page;
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001265 } while (r == -EAGAIN);
1266
1267 return r;
1268}
1269
1270/*
Sage Weil1d3576f2009-10-06 11:31:09 -07001271 * we don't do anything in here that simple_write_end doesn't do
Yan, Zheng5dda377c2015-04-30 14:40:54 +08001272 * except adjust dirty page accounting
Sage Weil1d3576f2009-10-06 11:31:09 -07001273 */
1274static 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)
1277{
Al Viro496ad9a2013-01-23 17:07:38 -05001278 struct inode *inode = file_inode(file);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001279 unsigned from = pos & (PAGE_SIZE - 1);
Sage Weil1d3576f2009-10-06 11:31:09 -07001280 int check_cap = 0;
1281
1282 dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
1283 inode, page, (int)pos, (int)copied, (int)len);
1284
1285 /* zero the stale part of the page if we did a short copy */
1286 if (copied < len)
1287 zero_user_segment(page, from+copied, len);
1288
1289 /* did file size increase? */
Yan, Zheng99c88e62015-12-30 11:32:46 +08001290 if (pos+copied > i_size_read(inode))
Sage Weil1d3576f2009-10-06 11:31:09 -07001291 check_cap = ceph_inode_set_size(inode, pos+copied);
1292
1293 if (!PageUptodate(page))
1294 SetPageUptodate(page);
1295
1296 set_page_dirty(page);
1297
1298 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001299 put_page(page);
Sage Weil1d3576f2009-10-06 11:31:09 -07001300
1301 if (check_cap)
1302 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
1303
1304 return copied;
1305}
1306
1307/*
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
1310 * never get called.
1311 */
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001312static ssize_t ceph_direct_io(struct kiocb *iocb, struct iov_iter *iter)
Sage Weil1d3576f2009-10-06 11:31:09 -07001313{
1314 WARN_ON(1);
1315 return -EINVAL;
1316}
1317
1318const 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,
1329};
1330
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001331static void ceph_block_sigs(sigset_t *oldset)
1332{
1333 sigset_t mask;
1334 siginitsetinv(&mask, sigmask(SIGKILL));
1335 sigprocmask(SIG_BLOCK, &mask, oldset);
1336}
1337
1338static void ceph_restore_sigs(sigset_t *oldset)
1339{
1340 sigprocmask(SIG_SETMASK, oldset, NULL);
1341}
Sage Weil1d3576f2009-10-06 11:31:09 -07001342
1343/*
1344 * vm ops
1345 */
Yan, Zheng61f68812013-11-28 14:28:14 +08001346static int ceph_filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1347{
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;
Yan, Zheng3738daa2014-11-14 22:10:07 +08001351 struct page *pinned_page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001352 loff_t off = vmf->pgoff << PAGE_SHIFT;
Yan, Zheng61f68812013-11-28 14:28:14 +08001353 int want, got, ret;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001354 sigset_t oldset;
1355
1356 ceph_block_sigs(&oldset);
Yan, Zheng61f68812013-11-28 14:28:14 +08001357
1358 dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001359 inode, ceph_vinop(inode), off, (size_t)PAGE_SIZE);
Yan, Zheng61f68812013-11-28 14:28:14 +08001360 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1361 want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1362 else
1363 want = CEPH_CAP_FILE_CACHE;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001364
1365 got = 0;
1366 ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, -1, &got, &pinned_page);
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001367 if (ret < 0)
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001368 goto out_restore;
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001369
Yan, Zheng61f68812013-11-28 14:28:14 +08001370 dout("filemap_fault %p %llu~%zd got cap refs on %s\n",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001371 inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got));
Yan, Zheng61f68812013-11-28 14:28:14 +08001372
Yan, Zheng83701242014-11-14 22:36:18 +08001373 if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
1374 ci->i_inline_version == CEPH_INLINE_NONE)
1375 ret = filemap_fault(vma, vmf);
1376 else
1377 ret = -EAGAIN;
Yan, Zheng61f68812013-11-28 14:28:14 +08001378
1379 dout("filemap_fault %p %llu~%zd dropping cap refs on %s ret %d\n",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001380 inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got), ret);
Yan, Zheng3738daa2014-11-14 22:10:07 +08001381 if (pinned_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001382 put_page(pinned_page);
Yan, Zheng61f68812013-11-28 14:28:14 +08001383 ceph_put_cap_refs(ci, got);
1384
Yan, Zheng83701242014-11-14 22:36:18 +08001385 if (ret != -EAGAIN)
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001386 goto out_restore;
Yan, Zheng83701242014-11-14 22:36:18 +08001387
1388 /* read inline data */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001389 if (off >= PAGE_SIZE) {
Yan, Zheng83701242014-11-14 22:36:18 +08001390 /* does not support inline data > PAGE_SIZE */
1391 ret = VM_FAULT_SIGBUS;
1392 } else {
1393 int ret1;
1394 struct address_space *mapping = inode->i_mapping;
1395 struct page *page = find_or_create_page(mapping, 0,
Michal Hockoc62d2552015-11-06 16:28:49 -08001396 mapping_gfp_constraint(mapping,
1397 ~__GFP_FS));
Yan, Zheng83701242014-11-14 22:36:18 +08001398 if (!page) {
1399 ret = VM_FAULT_OOM;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001400 goto out_inline;
Yan, Zheng83701242014-11-14 22:36:18 +08001401 }
1402 ret1 = __ceph_do_getattr(inode, page,
1403 CEPH_STAT_CAP_INLINE_DATA, true);
1404 if (ret1 < 0 || off >= i_size_read(inode)) {
1405 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001406 put_page(page);
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001407 if (ret1 < 0)
1408 ret = ret1;
1409 else
1410 ret = VM_FAULT_SIGBUS;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001411 goto out_inline;
Yan, Zheng83701242014-11-14 22:36:18 +08001412 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001413 if (ret1 < PAGE_SIZE)
1414 zero_user_segment(page, ret1, PAGE_SIZE);
Yan, Zheng83701242014-11-14 22:36:18 +08001415 else
1416 flush_dcache_page(page);
1417 SetPageUptodate(page);
1418 vmf->page = page;
1419 ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001420out_inline:
1421 dout("filemap_fault %p %llu~%zd read inline data ret %d\n",
1422 inode, off, (size_t)PAGE_SIZE, ret);
Yan, Zheng83701242014-11-14 22:36:18 +08001423 }
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001424out_restore:
1425 ceph_restore_sigs(&oldset);
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001426 if (ret < 0)
1427 ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
1428
Yan, Zheng61f68812013-11-28 14:28:14 +08001429 return ret;
1430}
Sage Weil1d3576f2009-10-06 11:31:09 -07001431
1432/*
1433 * Reuse write_begin here for simplicity.
1434 */
1435static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1436{
Al Viro496ad9a2013-01-23 17:07:38 -05001437 struct inode *inode = file_inode(vma->vm_file);
Yan, Zheng61f68812013-11-28 14:28:14 +08001438 struct ceph_inode_info *ci = ceph_inode(inode);
1439 struct ceph_file_info *fi = vma->vm_file->private_data;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001440 struct ceph_cap_flush *prealloc_cf;
Yan, Zheng61f68812013-11-28 14:28:14 +08001441 struct page *page = vmf->page;
Alex Elder6285bc22012-10-02 10:25:51 -05001442 loff_t off = page_offset(page);
Yan, Zheng61f68812013-11-28 14:28:14 +08001443 loff_t size = i_size_read(inode);
1444 size_t len;
1445 int want, got, ret;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001446 sigset_t oldset;
Sage Weil1d3576f2009-10-06 11:31:09 -07001447
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001448 prealloc_cf = ceph_alloc_cap_flush();
1449 if (!prealloc_cf)
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001450 return VM_FAULT_OOM;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001451
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001452 ceph_block_sigs(&oldset);
Sage Weil1d3576f2009-10-06 11:31:09 -07001453
Yan, Zheng28127bd2014-11-14 22:38:29 +08001454 if (ci->i_inline_version != CEPH_INLINE_NONE) {
1455 struct page *locked_page = NULL;
1456 if (off == 0) {
1457 lock_page(page);
1458 locked_page = page;
1459 }
1460 ret = ceph_uninline_data(vma->vm_file, locked_page);
1461 if (locked_page)
1462 unlock_page(locked_page);
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001463 if (ret < 0)
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001464 goto out_free;
Yan, Zheng28127bd2014-11-14 22:38:29 +08001465 }
1466
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001467 if (off + PAGE_SIZE <= size)
1468 len = PAGE_SIZE;
Sage Weil1d3576f2009-10-06 11:31:09 -07001469 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001470 len = size & ~PAGE_MASK;
Sage Weil1d3576f2009-10-06 11:31:09 -07001471
Yan, Zheng61f68812013-11-28 14:28:14 +08001472 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;
1476 else
1477 want = CEPH_CAP_FILE_BUFFER;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001478
1479 got = 0;
1480 ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, off + len,
1481 &got, NULL);
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001482 if (ret < 0)
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001483 goto out_free;
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001484
Yan, Zheng61f68812013-11-28 14:28:14 +08001485 dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
1486 inode, off, len, ceph_cap_string(got));
1487
1488 /* Update time before taking page lock */
1489 file_update_time(vma->vm_file);
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001490
Yan, Zhengf0b33df2016-05-10 19:09:06 +08001491 do {
1492 lock_page(page);
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001493
Yan, Zhengf0b33df2016-05-10 19:09:06 +08001494 if ((off > size) || (page->mapping != inode->i_mapping)) {
1495 unlock_page(page);
1496 ret = VM_FAULT_NOPAGE;
1497 break;
1498 }
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001499
Yan, Zhengf0b33df2016-05-10 19:09:06 +08001500 ret = ceph_update_writeable_page(vma->vm_file, off, len, page);
1501 if (ret >= 0) {
1502 /* success. we'll keep the page locked. */
1503 set_page_dirty(page);
1504 ret = VM_FAULT_LOCKED;
1505 }
1506 } while (ret == -EAGAIN);
1507
Yan, Zheng28127bd2014-11-14 22:38:29 +08001508 if (ret == VM_FAULT_LOCKED ||
1509 ci->i_inline_version != CEPH_INLINE_NONE) {
Yan, Zheng61f68812013-11-28 14:28:14 +08001510 int dirty;
1511 spin_lock(&ci->i_ceph_lock);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001512 ci->i_inline_version = CEPH_INLINE_NONE;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001513 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1514 &prealloc_cf);
Yan, Zheng61f68812013-11-28 14:28:14 +08001515 spin_unlock(&ci->i_ceph_lock);
1516 if (dirty)
1517 __mark_inode_dirty(inode, dirty);
1518 }
1519
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);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001523out_free:
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001524 ceph_restore_sigs(&oldset);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001525 ceph_free_cap_flush(prealloc_cf);
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001526 if (ret < 0)
1527 ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
Sage Weil1d3576f2009-10-06 11:31:09 -07001528 return ret;
1529}
1530
Yan, Zheng31c542a2014-11-14 21:41:55 +08001531void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
1532 char *data, size_t len)
1533{
1534 struct address_space *mapping = inode->i_mapping;
1535 struct page *page;
1536
1537 if (locked_page) {
1538 page = locked_page;
1539 } else {
1540 if (i_size_read(inode) == 0)
1541 return;
1542 page = find_or_create_page(mapping, 0,
Michal Hockoc62d2552015-11-06 16:28:49 -08001543 mapping_gfp_constraint(mapping,
1544 ~__GFP_FS));
Yan, Zheng31c542a2014-11-14 21:41:55 +08001545 if (!page)
1546 return;
1547 if (PageUptodate(page)) {
1548 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001549 put_page(page);
Yan, Zheng31c542a2014-11-14 21:41:55 +08001550 return;
1551 }
1552 }
1553
Ilya Dryomov0668ff52014-12-19 13:10:10 +03001554 dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
Yan, Zheng31c542a2014-11-14 21:41:55 +08001555 inode, ceph_vinop(inode), len, locked_page);
1556
1557 if (len > 0) {
1558 void *kaddr = kmap_atomic(page);
1559 memcpy(kaddr, data, len);
1560 kunmap_atomic(kaddr);
1561 }
1562
1563 if (page != locked_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001564 if (len < PAGE_SIZE)
1565 zero_user_segment(page, len, PAGE_SIZE);
Yan, Zheng31c542a2014-11-14 21:41:55 +08001566 else
1567 flush_dcache_page(page);
1568
1569 SetPageUptodate(page);
1570 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001571 put_page(page);
Yan, Zheng31c542a2014-11-14 21:41:55 +08001572 }
1573}
1574
Yan, Zheng28127bd2014-11-14 22:38:29 +08001575int ceph_uninline_data(struct file *filp, struct page *locked_page)
1576{
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;
1583 int err = 0;
1584 bool from_pagecache = false;
1585
1586 spin_lock(&ci->i_ceph_lock);
1587 inline_version = ci->i_inline_version;
1588 spin_unlock(&ci->i_ceph_lock);
1589
1590 dout("uninline_data %p %llx.%llx inline_version %llu\n",
1591 inode, ceph_vinop(inode), inline_version);
1592
1593 if (inline_version == 1 || /* initial version, no data */
1594 inline_version == CEPH_INLINE_NONE)
1595 goto out;
1596
1597 if (locked_page) {
1598 page = locked_page;
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);
1603 if (page) {
1604 if (PageUptodate(page)) {
1605 from_pagecache = true;
1606 lock_page(page);
1607 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001608 put_page(page);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001609 page = NULL;
1610 }
1611 }
1612 }
1613
1614 if (page) {
1615 len = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001616 if (len > PAGE_SIZE)
1617 len = PAGE_SIZE;
Yan, Zheng28127bd2014-11-14 22:38:29 +08001618 } else {
1619 page = __page_cache_alloc(GFP_NOFS);
1620 if (!page) {
1621 err = -ENOMEM;
1622 goto out;
1623 }
1624 err = __ceph_do_getattr(inode, page,
1625 CEPH_STAT_CAP_INLINE_DATA, true);
1626 if (err < 0) {
1627 /* no inline data */
1628 if (err == -ENODATA)
1629 err = 0;
1630 goto out;
1631 }
1632 len = err;
1633 }
1634
1635 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1636 ceph_vino(inode), 0, &len, 0, 1,
1637 CEPH_OSD_OP_CREATE,
1638 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Ilya Dryomov34b759b2016-02-16 15:00:24 +01001639 NULL, 0, 0, false);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001640 if (IS_ERR(req)) {
1641 err = PTR_ERR(req);
1642 goto out;
1643 }
1644
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001645 req->r_mtime = inode->i_mtime;
Yan, Zheng28127bd2014-11-14 22:38:29 +08001646 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1647 if (!err)
1648 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1649 ceph_osdc_put_request(req);
1650 if (err < 0)
1651 goto out;
1652
1653 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1654 ceph_vino(inode), 0, &len, 1, 3,
1655 CEPH_OSD_OP_WRITE,
1656 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Ilya Dryomov34b759b2016-02-16 15:00:24 +01001657 NULL, ci->i_truncate_seq,
1658 ci->i_truncate_size, false);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001659 if (IS_ERR(req)) {
1660 err = PTR_ERR(req);
1661 goto out;
1662 }
1663
1664 osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false);
1665
Yan, Zhengec137c12015-04-13 11:25:07 +08001666 {
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,
1670 sizeof(xattr_buf),
1671 CEPH_OSD_CMPXATTR_OP_GT,
1672 CEPH_OSD_CMPXATTR_MODE_U64);
1673 if (err)
1674 goto out_put;
1675 }
Yan, Zheng28127bd2014-11-14 22:38:29 +08001676
Yan, Zhengec137c12015-04-13 11:25:07 +08001677 {
1678 char xattr_buf[32];
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,
1682 "inline_version",
1683 xattr_buf, xattr_len, 0, 0);
1684 if (err)
1685 goto out_put;
1686 }
Yan, Zheng28127bd2014-11-14 22:38:29 +08001687
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001688 req->r_mtime = inode->i_mtime;
Yan, Zheng28127bd2014-11-14 22:38:29 +08001689 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1690 if (!err)
1691 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1692out_put:
1693 ceph_osdc_put_request(req);
1694 if (err == -ECANCELED)
1695 err = 0;
1696out:
1697 if (page && page != locked_page) {
1698 if (from_pagecache) {
1699 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001700 put_page(page);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001701 } else
1702 __free_pages(page, 0);
1703 }
1704
1705 dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
1706 inode, ceph_vinop(inode), inline_version, err);
1707 return err;
1708}
1709
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07001710static const struct vm_operations_struct ceph_vmops = {
Yan, Zheng61f68812013-11-28 14:28:14 +08001711 .fault = ceph_filemap_fault,
Sage Weil1d3576f2009-10-06 11:31:09 -07001712 .page_mkwrite = ceph_page_mkwrite,
1713};
1714
1715int ceph_mmap(struct file *file, struct vm_area_struct *vma)
1716{
1717 struct address_space *mapping = file->f_mapping;
1718
1719 if (!mapping->a_ops->readpage)
1720 return -ENOEXEC;
1721 file_accessed(file);
1722 vma->vm_ops = &ceph_vmops;
Sage Weil1d3576f2009-10-06 11:31:09 -07001723 return 0;
1724}
Yan, Zheng10183a62015-04-27 15:33:28 +08001725
1726enum {
1727 POOL_READ = 1,
1728 POOL_WRITE = 2,
1729};
1730
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001731static int __ceph_pool_perm_get(struct ceph_inode_info *ci,
1732 s64 pool, struct ceph_string *pool_ns)
Yan, Zheng10183a62015-04-27 15:33:28 +08001733{
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;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001740 size_t pool_ns_len;
Yan, Zheng10183a62015-04-27 15:33:28 +08001741 int err = 0, err2 = 0, have = 0;
1742
1743 down_read(&mdsc->pool_perm_rwsem);
1744 p = &mdsc->pool_perm_tree.rb_node;
1745 while (*p) {
1746 perm = rb_entry(*p, struct ceph_pool_perm, node);
1747 if (pool < perm->pool)
1748 p = &(*p)->rb_left;
1749 else if (pool > perm->pool)
1750 p = &(*p)->rb_right;
1751 else {
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001752 int ret = ceph_compare_string(pool_ns,
1753 perm->pool_ns,
1754 perm->pool_ns_len);
1755 if (ret < 0)
1756 p = &(*p)->rb_left;
1757 else if (ret > 0)
1758 p = &(*p)->rb_right;
1759 else {
1760 have = perm->perm;
1761 break;
1762 }
Yan, Zheng10183a62015-04-27 15:33:28 +08001763 }
1764 }
1765 up_read(&mdsc->pool_perm_rwsem);
1766 if (*p)
1767 goto out;
1768
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001769 if (pool_ns)
1770 dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n",
1771 pool, (int)pool_ns->len, pool_ns->str);
1772 else
1773 dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool);
Yan, Zheng10183a62015-04-27 15:33:28 +08001774
1775 down_write(&mdsc->pool_perm_rwsem);
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001776 p = &mdsc->pool_perm_tree.rb_node;
Yan, Zheng10183a62015-04-27 15:33:28 +08001777 parent = NULL;
1778 while (*p) {
1779 parent = *p;
1780 perm = rb_entry(parent, struct ceph_pool_perm, node);
1781 if (pool < perm->pool)
1782 p = &(*p)->rb_left;
1783 else if (pool > perm->pool)
1784 p = &(*p)->rb_right;
1785 else {
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001786 int ret = ceph_compare_string(pool_ns,
1787 perm->pool_ns,
1788 perm->pool_ns_len);
1789 if (ret < 0)
1790 p = &(*p)->rb_left;
1791 else if (ret > 0)
1792 p = &(*p)->rb_right;
1793 else {
1794 have = perm->perm;
1795 break;
1796 }
Yan, Zheng10183a62015-04-27 15:33:28 +08001797 }
1798 }
1799 if (*p) {
1800 up_write(&mdsc->pool_perm_rwsem);
1801 goto out;
1802 }
1803
Ilya Dryomov34b759b2016-02-16 15:00:24 +01001804 rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
Yan, Zheng10183a62015-04-27 15:33:28 +08001805 1, false, GFP_NOFS);
1806 if (!rd_req) {
1807 err = -ENOMEM;
1808 goto out_unlock;
1809 }
1810
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;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001814 if (pool_ns)
1815 rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns);
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001816 ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino);
Yan, Zheng10183a62015-04-27 15:33:28 +08001817
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02001818 err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS);
1819 if (err)
1820 goto out_unlock;
Yan, Zheng10183a62015-04-27 15:33:28 +08001821
Ilya Dryomov34b759b2016-02-16 15:00:24 +01001822 wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
Yan, Zheng10183a62015-04-27 15:33:28 +08001823 1, false, GFP_NOFS);
1824 if (!wr_req) {
1825 err = -ENOMEM;
1826 goto out_unlock;
1827 }
1828
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001829 wr_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ACK;
Yan, Zheng10183a62015-04-27 15:33:28 +08001830 osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001831 ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc);
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001832 ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid);
Yan, Zheng10183a62015-04-27 15:33:28 +08001833
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02001834 err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS);
1835 if (err)
1836 goto out_unlock;
Yan, Zheng10183a62015-04-27 15:33:28 +08001837
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);
1842 goto out_unlock;
1843 }
1844
1845 osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
1846 0, false, true);
Yan, Zheng10183a62015-04-27 15:33:28 +08001847 err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false);
1848
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001849 wr_req->r_mtime = ci->vfs_inode.i_mtime;
Yan, Zheng10183a62015-04-27 15:33:28 +08001850 err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false);
1851
1852 if (!err)
1853 err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
1854 if (!err2)
1855 err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
1856
1857 if (err >= 0 || err == -ENOENT)
1858 have |= POOL_READ;
1859 else if (err != -EPERM)
1860 goto out_unlock;
1861
1862 if (err2 == 0 || err2 == -EEXIST)
1863 have |= POOL_WRITE;
1864 else if (err2 != -EPERM) {
1865 err = err2;
1866 goto out_unlock;
1867 }
1868
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001869 pool_ns_len = pool_ns ? pool_ns->len : 0;
1870 perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS);
Yan, Zheng10183a62015-04-27 15:33:28 +08001871 if (!perm) {
1872 err = -ENOMEM;
1873 goto out_unlock;
1874 }
1875
1876 perm->pool = pool;
1877 perm->perm = have;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001878 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;
1882
Yan, Zheng10183a62015-04-27 15:33:28 +08001883 rb_link_node(&perm->node, parent, p);
1884 rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
1885 err = 0;
1886out_unlock:
1887 up_write(&mdsc->pool_perm_rwsem);
1888
Ilya Dryomov3ed97d62016-04-26 15:05:29 +02001889 ceph_osdc_put_request(rd_req);
1890 ceph_osdc_put_request(wr_req);
Yan, Zheng10183a62015-04-27 15:33:28 +08001891out:
1892 if (!err)
1893 err = have;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001894 if (pool_ns)
1895 dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n",
1896 pool, (int)pool_ns->len, pool_ns->str, err);
1897 else
1898 dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err);
Yan, Zheng10183a62015-04-27 15:33:28 +08001899 return err;
1900}
1901
1902int ceph_pool_perm_check(struct ceph_inode_info *ci, int need)
1903{
Yan, Zheng76271512016-02-03 21:24:49 +08001904 s64 pool;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001905 struct ceph_string *pool_ns;
Yan, Zheng10183a62015-04-27 15:33:28 +08001906 int ret, flags;
1907
1908 if (ceph_test_mount_opt(ceph_inode_to_client(&ci->vfs_inode),
1909 NOPOOLPERM))
1910 return 0;
1911
1912 spin_lock(&ci->i_ceph_lock);
1913 flags = ci->i_ceph_flags;
Yan, Zheng76271512016-02-03 21:24:49 +08001914 pool = ci->i_layout.pool_id;
Yan, Zheng10183a62015-04-27 15:33:28 +08001915 spin_unlock(&ci->i_ceph_lock);
1916check:
1917 if (flags & CEPH_I_POOL_PERM) {
1918 if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
Yan, Zheng76271512016-02-03 21:24:49 +08001919 dout("ceph_pool_perm_check pool %lld no read perm\n",
Yan, Zheng10183a62015-04-27 15:33:28 +08001920 pool);
1921 return -EPERM;
1922 }
1923 if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
Yan, Zheng76271512016-02-03 21:24:49 +08001924 dout("ceph_pool_perm_check pool %lld no write perm\n",
Yan, Zheng10183a62015-04-27 15:33:28 +08001925 pool);
1926 return -EPERM;
1927 }
1928 return 0;
1929 }
1930
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001931 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);
Yan, Zheng10183a62015-04-27 15:33:28 +08001934 if (ret < 0)
1935 return ret;
1936
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;
1942
1943 spin_lock(&ci->i_ceph_lock);
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001944 if (pool == ci->i_layout.pool_id &&
1945 pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
1946 ci->i_ceph_flags |= flags;
Yan, Zheng10183a62015-04-27 15:33:28 +08001947 } else {
Yan, Zheng76271512016-02-03 21:24:49 +08001948 pool = ci->i_layout.pool_id;
Yan, Zheng10183a62015-04-27 15:33:28 +08001949 flags = ci->i_ceph_flags;
1950 }
1951 spin_unlock(&ci->i_ceph_lock);
1952 goto check;
1953}
1954
1955void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
1956{
1957 struct ceph_pool_perm *perm;
1958 struct rb_node *n;
1959
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);
1964 kfree(perm);
1965 }
1966}