blob: 18dc18f8af2cdf1e5cd0579ca91fdff72d862277 [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;
Yan, Zhenge99b0ea2016-10-25 10:51:55 +0800318 int got = 0;
319 int ret = 0;
320
321 if (!current->journal_info) {
322 /* caller of readpages does not hold buffer and read caps
323 * (fadvise, madvise and readahead cases) */
324 int want = CEPH_CAP_FILE_CACHE;
325 ret = ceph_try_get_caps(ci, CEPH_CAP_FILE_RD, want, &got);
326 if (ret < 0) {
327 dout("start_read %p, error getting cap\n", inode);
328 } else if (!(got & want)) {
329 dout("start_read %p, no cache cap\n", inode);
330 ret = 0;
331 }
332 if (ret <= 0) {
333 if (got)
334 ceph_put_cap_refs(ci, got);
335 while (!list_empty(page_list)) {
336 page = list_entry(page_list->prev,
337 struct page, lru);
338 list_del(&page->lru);
339 put_page(page);
340 }
341 return ret;
342 }
343 }
Sage Weil7c272192011-08-03 09:58:09 -0700344
Alex Elder6285bc22012-10-02 10:25:51 -0500345 off = (u64) page_offset(page);
Sage Weil7c272192011-08-03 09:58:09 -0700346
347 /* count pages */
348 next_index = page->index;
349 list_for_each_entry_reverse(page, page_list, lru) {
350 if (page->index != next_index)
351 break;
352 nr_pages++;
353 next_index++;
Sage Weil0d66a482011-08-04 08:21:30 -0700354 if (max && nr_pages == max)
355 break;
Sage Weil7c272192011-08-03 09:58:09 -0700356 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300357 len = nr_pages << PAGE_SHIFT;
Sage Weil7c272192011-08-03 09:58:09 -0700358 dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages,
359 off, len);
Alex Elderacead002013-03-14 14:09:05 -0500360 vino = ceph_vino(inode);
361 req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len,
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800362 0, 1, CEPH_OSD_OP_READ,
Alex Elderacead002013-03-14 14:09:05 -0500363 CEPH_OSD_FLAG_READ, NULL,
Sage Weil7c272192011-08-03 09:58:09 -0700364 ci->i_truncate_seq, ci->i_truncate_size,
Alex Elderacead002013-03-14 14:09:05 -0500365 false);
Yan, Zhenge99b0ea2016-10-25 10:51:55 +0800366 if (IS_ERR(req)) {
367 ret = PTR_ERR(req);
368 goto out;
369 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700370
371 /* build page vector */
Alex Eldercf7b7e12013-03-01 18:00:15 -0600372 nr_pages = calc_pages_for(0, len);
Yan, Zheng687265e2015-06-13 17:27:05 +0800373 pages = kmalloc(sizeof(*pages) * nr_pages, GFP_KERNEL);
Yan, Zhenge99b0ea2016-10-25 10:51:55 +0800374 if (!pages) {
375 ret = -ENOMEM;
376 goto out_put;
377 }
Sage Weil7c272192011-08-03 09:58:09 -0700378 for (i = 0; i < nr_pages; ++i) {
379 page = list_entry(page_list->prev, struct page, lru);
380 BUG_ON(PageLocked(page));
381 list_del(&page->lru);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400382
Sage Weil7c272192011-08-03 09:58:09 -0700383 dout("start_read %p adding %p idx %lu\n", inode, page,
384 page->index);
385 if (add_to_page_cache_lru(page, &inode->i_data, page->index,
Yan, Zheng687265e2015-06-13 17:27:05 +0800386 GFP_KERNEL)) {
Milosz Tanskid4d3aa32013-09-03 19:11:17 -0400387 ceph_fscache_uncache_page(inode, page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300388 put_page(page);
Sage Weil7c272192011-08-03 09:58:09 -0700389 dout("start_read %p add_to_page_cache failed %p\n",
390 inode, page);
391 nr_pages = i;
Yan, Zheng1afe4782016-08-24 11:33:46 +0800392 if (nr_pages > 0) {
393 len = nr_pages << PAGE_SHIFT;
Yan, Zhengdc8470f2017-01-19 11:21:29 +0800394 osd_req_op_extent_update(req, 0, len);
Yan, Zheng1afe4782016-08-24 11:33:46 +0800395 break;
396 }
Sage Weil7c272192011-08-03 09:58:09 -0700397 goto out_pages;
Sage Weil1d3576f2009-10-06 11:31:09 -0700398 }
Sage Weil7c272192011-08-03 09:58:09 -0700399 pages[i] = page;
Sage Weil1d3576f2009-10-06 11:31:09 -0700400 }
Alex Elder406e2c92013-04-15 14:50:36 -0500401 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false);
Sage Weil7c272192011-08-03 09:58:09 -0700402 req->r_callback = finish_read;
403 req->r_inode = inode;
404
405 dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len);
406 ret = ceph_osdc_start_request(osdc, req, false);
407 if (ret < 0)
408 goto out_pages;
409 ceph_osdc_put_request(req);
Yan, Zhenge99b0ea2016-10-25 10:51:55 +0800410
411 /* After adding locked pages to page cache, the inode holds cache cap.
412 * So we can drop our cap refs. */
413 if (got)
414 ceph_put_cap_refs(ci, got);
415
Sage Weil7c272192011-08-03 09:58:09 -0700416 return nr_pages;
417
418out_pages:
Yan, Zheng1afe4782016-08-24 11:33:46 +0800419 for (i = 0; i < nr_pages; ++i) {
420 ceph_fscache_readpage_cancel(inode, pages[i]);
421 unlock_page(pages[i]);
422 }
423 ceph_put_page_vector(pages, nr_pages, false);
Yan, Zhenge99b0ea2016-10-25 10:51:55 +0800424out_put:
Sage Weil7c272192011-08-03 09:58:09 -0700425 ceph_osdc_put_request(req);
Yan, Zhenge99b0ea2016-10-25 10:51:55 +0800426out:
427 if (got)
428 ceph_put_cap_refs(ci, got);
Sage Weil7c272192011-08-03 09:58:09 -0700429 return ret;
Sage Weil1d3576f2009-10-06 11:31:09 -0700430}
431
Sage Weil7c272192011-08-03 09:58:09 -0700432
Sage Weil1d3576f2009-10-06 11:31:09 -0700433/*
434 * Read multiple pages. Leave pages we don't read + unlock in page_list;
435 * the caller (VM) cleans them up.
436 */
437static int ceph_readpages(struct file *file, struct address_space *mapping,
438 struct list_head *page_list, unsigned nr_pages)
439{
Al Viro496ad9a2013-01-23 17:07:38 -0500440 struct inode *inode = file_inode(file);
Sage Weil0d66a482011-08-04 08:21:30 -0700441 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700442 int rc = 0;
Sage Weil0d66a482011-08-04 08:21:30 -0700443 int max = 0;
Sage Weil1d3576f2009-10-06 11:31:09 -0700444
Yan, Zheng83701242014-11-14 22:36:18 +0800445 if (ceph_inode(inode)->i_inline_version != CEPH_INLINE_NONE)
446 return -EINVAL;
447
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400448 rc = ceph_readpages_from_fscache(mapping->host, mapping, page_list,
449 &nr_pages);
450
451 if (rc == 0)
452 goto out;
453
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300454 if (fsc->mount_options->rsize >= PAGE_SIZE)
455 max = (fsc->mount_options->rsize + PAGE_SIZE - 1)
Sage Weil0d66a482011-08-04 08:21:30 -0700456 >> PAGE_SHIFT;
457
Alex Elder2794a822013-02-14 12:16:43 -0600458 dout("readpages %p file %p nr_pages %d max %d\n", inode,
459 file, nr_pages,
Sage Weil0d66a482011-08-04 08:21:30 -0700460 max);
Sage Weil7c272192011-08-03 09:58:09 -0700461 while (!list_empty(page_list)) {
Sage Weil0d66a482011-08-04 08:21:30 -0700462 rc = start_read(inode, page_list, max);
Sage Weil7c272192011-08-03 09:58:09 -0700463 if (rc < 0)
464 goto out;
Sage Weil1d3576f2009-10-06 11:31:09 -0700465 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700466out:
Milosz Tanski76be7782013-08-21 17:30:27 -0400467 ceph_fscache_readpages_cancel(inode, page_list);
468
Sage Weil7c272192011-08-03 09:58:09 -0700469 dout("readpages %p file %p ret %d\n", inode, file, rc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700470 return rc;
471}
472
473/*
474 * Get ref for the oldest snapc for an inode with dirty data... that is, the
475 * only snap context we are allowed to write back.
Sage Weil1d3576f2009-10-06 11:31:09 -0700476 */
Sage Weil6298a332010-03-31 22:01:38 -0700477static struct ceph_snap_context *get_oldest_context(struct inode *inode,
Yan, Zhenge1966b42015-06-18 03:10:58 +0800478 loff_t *snap_size)
Sage Weil1d3576f2009-10-06 11:31:09 -0700479{
480 struct ceph_inode_info *ci = ceph_inode(inode);
481 struct ceph_snap_context *snapc = NULL;
482 struct ceph_cap_snap *capsnap = NULL;
483
Sage Weilbe655592011-11-30 09:47:09 -0800484 spin_lock(&ci->i_ceph_lock);
Sage Weil1d3576f2009-10-06 11:31:09 -0700485 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
486 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
487 capsnap->context, capsnap->dirty_pages);
488 if (capsnap->dirty_pages) {
489 snapc = ceph_get_snap_context(capsnap->context);
490 if (snap_size)
491 *snap_size = capsnap->size;
492 break;
493 }
494 }
Sage Weil7d8cb262010-08-24 08:44:16 -0700495 if (!snapc && ci->i_wrbuffer_ref_head) {
Sage Weil80e755f2010-03-31 21:52:10 -0700496 snapc = ceph_get_snap_context(ci->i_head_snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700497 dout(" head snapc %p has %d dirty pages\n",
498 snapc, ci->i_wrbuffer_ref_head);
499 }
Sage Weilbe655592011-11-30 09:47:09 -0800500 spin_unlock(&ci->i_ceph_lock);
Sage Weil1d3576f2009-10-06 11:31:09 -0700501 return snapc;
502}
503
504/*
505 * Write a single page, but leave the page locked.
506 *
507 * If we get a write error, set the page error bit, but still adjust the
508 * dirty page accounting (i.e., page is no longer dirty).
509 */
510static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
511{
512 struct inode *inode;
513 struct ceph_inode_info *ci;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700514 struct ceph_fs_client *fsc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700515 struct ceph_osd_client *osdc;
Sage Weil6298a332010-03-31 22:01:38 -0700516 struct ceph_snap_context *snapc, *oldest;
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800517 loff_t page_off = page_offset(page);
Yan, Zhenge1966b42015-06-18 03:10:58 +0800518 loff_t snap_size = -1;
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800519 long writeback_stat;
Yan, Zhenge1966b42015-06-18 03:10:58 +0800520 u64 truncate_size;
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800521 u32 truncate_seq;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300522 int err = 0, len = PAGE_SIZE;
Sage Weil1d3576f2009-10-06 11:31:09 -0700523
524 dout("writepage %p idx %lu\n", page, page->index);
525
526 if (!page->mapping || !page->mapping->host) {
527 dout("writepage %p - no mapping\n", page);
528 return -EFAULT;
529 }
530 inode = page->mapping->host;
531 ci = ceph_inode(inode);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700532 fsc = ceph_inode_to_client(inode);
533 osdc = &fsc->client->osdc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700534
535 /* verify this is a writeable snap context */
Yan, Zheng61600ef2012-05-28 14:44:30 +0800536 snapc = page_snap_context(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700537 if (snapc == NULL) {
538 dout("writepage %p page %p not dirty?\n", inode, page);
539 goto out;
540 }
Sage Weil6298a332010-03-31 22:01:38 -0700541 oldest = get_oldest_context(inode, &snap_size);
542 if (snapc->seq > oldest->seq) {
Sage Weil1d3576f2009-10-06 11:31:09 -0700543 dout("writepage %p page %p snapc %p not writeable - noop\n",
Yan, Zheng61600ef2012-05-28 14:44:30 +0800544 inode, page, snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700545 /* we should only noop if called by kswapd */
546 WARN_ON((current->flags & PF_MEMALLOC) == 0);
Sage Weil6298a332010-03-31 22:01:38 -0700547 ceph_put_snap_context(oldest);
Sage Weil1d3576f2009-10-06 11:31:09 -0700548 goto out;
549 }
Sage Weil6298a332010-03-31 22:01:38 -0700550 ceph_put_snap_context(oldest);
Sage Weil1d3576f2009-10-06 11:31:09 -0700551
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800552 spin_lock(&ci->i_ceph_lock);
553 truncate_seq = ci->i_truncate_seq;
554 truncate_size = ci->i_truncate_size;
Yan, Zhenge1966b42015-06-18 03:10:58 +0800555 if (snap_size == -1)
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800556 snap_size = i_size_read(inode);
557 spin_unlock(&ci->i_ceph_lock);
558
Sage Weil1d3576f2009-10-06 11:31:09 -0700559 /* is this a partial page at end of file? */
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800560 if (page_off >= snap_size) {
561 dout("%p page eof %llu\n", page, snap_size);
562 goto out;
563 }
564 if (snap_size < page_off + len)
565 len = snap_size - page_off;
Sage Weil1d3576f2009-10-06 11:31:09 -0700566
Sage Weilae00d4f2010-09-16 16:26:51 -0700567 dout("writepage %p page %p index %lu on %llu~%u snapc %p\n",
568 inode, page, page->index, page_off, len, snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700569
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700570 writeback_stat = atomic_long_inc_return(&fsc->writeback_count);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800571 if (writeback_stat >
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700572 CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
573 set_bdi_congested(&fsc->backing_dev_info, BLK_RW_ASYNC);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800574
Sage Weil1d3576f2009-10-06 11:31:09 -0700575 set_page_writeback(page);
576 err = ceph_osdc_writepages(osdc, ceph_vino(inode),
577 &ci->i_layout, snapc,
578 page_off, len,
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800579 truncate_seq, truncate_size,
Alex Elder24808822013-02-15 11:42:29 -0600580 &inode->i_mtime, &page, 1);
Sage Weil1d3576f2009-10-06 11:31:09 -0700581 if (err < 0) {
Yan, Zhengad15ec02016-05-13 17:29:51 +0800582 struct writeback_control tmp_wbc;
583 if (!wbc)
584 wbc = &tmp_wbc;
585 if (err == -ERESTARTSYS) {
586 /* killed by SIGKILL */
587 dout("writepage interrupted page %p\n", page);
588 redirty_page_for_writepage(wbc, page);
589 end_page_writeback(page);
590 goto out;
591 }
592 dout("writepage setting page/mapping error %d %p\n",
593 err, page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700594 SetPageError(page);
595 mapping_set_error(&inode->i_data, err);
Yan, Zhengad15ec02016-05-13 17:29:51 +0800596 wbc->pages_skipped++;
Sage Weil1d3576f2009-10-06 11:31:09 -0700597 } else {
598 dout("writepage cleaned page %p\n", page);
599 err = 0; /* vfs expects us to return 0 */
600 }
601 page->private = 0;
602 ClearPagePrivate(page);
603 end_page_writeback(page);
604 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
Sage Weil6298a332010-03-31 22:01:38 -0700605 ceph_put_snap_context(snapc); /* page's reference */
Sage Weil1d3576f2009-10-06 11:31:09 -0700606out:
607 return err;
608}
609
610static int ceph_writepage(struct page *page, struct writeback_control *wbc)
611{
Yehuda Sadehdbd646a2009-12-16 14:51:06 -0800612 int err;
613 struct inode *inode = page->mapping->host;
614 BUG_ON(!inode);
Sage Weil70b666c2011-05-27 09:24:26 -0700615 ihold(inode);
Yehuda Sadehdbd646a2009-12-16 14:51:06 -0800616 err = writepage_nounlock(page, wbc);
Yan, Zhengad15ec02016-05-13 17:29:51 +0800617 if (err == -ERESTARTSYS) {
618 /* direct memory reclaimer was killed by SIGKILL. return 0
619 * to prevent caller from setting mapping/page error */
620 err = 0;
621 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700622 unlock_page(page);
Yehuda Sadehdbd646a2009-12-16 14:51:06 -0800623 iput(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700624 return err;
625}
626
Sage Weil1d3576f2009-10-06 11:31:09 -0700627/*
628 * lame release_pages helper. release_pages() isn't exported to
629 * modules.
630 */
631static void ceph_release_pages(struct page **pages, int num)
632{
633 struct pagevec pvec;
634 int i;
635
636 pagevec_init(&pvec, 0);
637 for (i = 0; i < num; i++) {
638 if (pagevec_add(&pvec, pages[i]) == 0)
639 pagevec_release(&pvec);
640 }
641 pagevec_release(&pvec);
642}
643
Sage Weil1d3576f2009-10-06 11:31:09 -0700644/*
645 * async writeback completion handler.
646 *
647 * If we get an error, set the mapping error bit, but not the individual
648 * page error bits.
649 */
Ilya Dryomov85e084f2016-04-28 16:07:24 +0200650static void writepages_finish(struct ceph_osd_request *req)
Sage Weil1d3576f2009-10-06 11:31:09 -0700651{
652 struct inode *inode = req->r_inode;
Sage Weil1d3576f2009-10-06 11:31:09 -0700653 struct ceph_inode_info *ci = ceph_inode(inode);
Alex Elder87060c12013-04-03 01:28:58 -0500654 struct ceph_osd_data *osd_data;
Sage Weil1d3576f2009-10-06 11:31:09 -0700655 struct page *page;
Yan, Zheng5b646402016-01-07 16:00:17 +0800656 int num_pages, total_pages = 0;
657 int i, j;
658 int rc = req->r_result;
Sage Weil1d3576f2009-10-06 11:31:09 -0700659 struct ceph_snap_context *snapc = req->r_snapc;
660 struct address_space *mapping = inode->i_mapping;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700661 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
Yan, Zheng5b646402016-01-07 16:00:17 +0800662 bool remove_page;
Sage Weil1d3576f2009-10-06 11:31:09 -0700663
Yan, Zheng5b646402016-01-07 16:00:17 +0800664 dout("writepages_finish %p rc %d\n", inode, rc);
665 if (rc < 0)
Sage Weil1d3576f2009-10-06 11:31:09 -0700666 mapping_set_error(mapping, rc);
Yan, Zheng5b646402016-01-07 16:00:17 +0800667
668 /*
669 * We lost the cache cap, need to truncate the page before
670 * it is unlocked, otherwise we'd truncate it later in the
671 * page truncation thread, possibly losing some data that
672 * raced its way in
673 */
674 remove_page = !(ceph_caps_issued(ci) &
675 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
Sage Weil1d3576f2009-10-06 11:31:09 -0700676
677 /* clean all pages */
Yan, Zheng5b646402016-01-07 16:00:17 +0800678 for (i = 0; i < req->r_num_ops; i++) {
679 if (req->r_ops[i].op != CEPH_OSD_OP_WRITE)
680 break;
Sage Weil1d3576f2009-10-06 11:31:09 -0700681
Yan, Zheng5b646402016-01-07 16:00:17 +0800682 osd_data = osd_req_op_extent_osd_data(req, i);
683 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
684 num_pages = calc_pages_for((u64)osd_data->alignment,
685 (u64)osd_data->length);
686 total_pages += num_pages;
687 for (j = 0; j < num_pages; j++) {
688 page = osd_data->pages[j];
689 BUG_ON(!page);
690 WARN_ON(!PageUptodate(page));
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800691
Yan, Zheng5b646402016-01-07 16:00:17 +0800692 if (atomic_long_dec_return(&fsc->writeback_count) <
693 CONGESTION_OFF_THRESH(
694 fsc->mount_options->congestion_kb))
695 clear_bdi_congested(&fsc->backing_dev_info,
696 BLK_RW_ASYNC);
Yehuda Sadehe63dc5c2010-02-19 00:07:01 +0000697
Yan, Zhengb109eec62016-05-13 17:54:17 +0800698 if (rc < 0)
699 SetPageError(page);
700
Yan, Zheng5b646402016-01-07 16:00:17 +0800701 ceph_put_snap_context(page_snap_context(page));
702 page->private = 0;
703 ClearPagePrivate(page);
704 dout("unlocking %p\n", page);
705 end_page_writeback(page);
Yehuda Sadehe63dc5c2010-02-19 00:07:01 +0000706
Yan, Zheng5b646402016-01-07 16:00:17 +0800707 if (remove_page)
708 generic_error_remove_page(inode->i_mapping,
709 page);
710
711 unlock_page(page);
712 }
713 dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n",
714 inode, osd_data->length, rc >= 0 ? num_pages : 0);
715
716 ceph_release_pages(osd_data->pages, num_pages);
Sage Weil1d3576f2009-10-06 11:31:09 -0700717 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700718
Yan, Zheng5b646402016-01-07 16:00:17 +0800719 ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
720
721 osd_data = osd_req_op_extent_osd_data(req, 0);
Alex Elder87060c12013-04-03 01:28:58 -0500722 if (osd_data->pages_from_pool)
723 mempool_free(osd_data->pages,
Cheng Renquan640ef792010-03-26 17:40:33 +0800724 ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool);
Sage Weil1d3576f2009-10-06 11:31:09 -0700725 else
Alex Elder87060c12013-04-03 01:28:58 -0500726 kfree(osd_data->pages);
Sage Weil1d3576f2009-10-06 11:31:09 -0700727 ceph_osdc_put_request(req);
728}
729
Sage Weil1d3576f2009-10-06 11:31:09 -0700730/*
731 * initiate async writeback
732 */
733static int ceph_writepages_start(struct address_space *mapping,
734 struct writeback_control *wbc)
735{
736 struct inode *inode = mapping->host;
Sage Weil1d3576f2009-10-06 11:31:09 -0700737 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800738 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
739 struct ceph_vino vino = ceph_vino(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700740 pgoff_t index, start, end;
741 int range_whole = 0;
742 int should_loop = 1;
743 pgoff_t max_pages = 0, max_pages_ever = 0;
Sage Weil80e755f2010-03-31 21:52:10 -0700744 struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700745 struct pagevec pvec;
746 int done = 0;
747 int rc = 0;
748 unsigned wsize = 1 << inode->i_blkbits;
749 struct ceph_osd_request *req = NULL;
Dan Carpenter021b77b2014-11-28 11:33:34 +0300750 int do_sync = 0;
Yan, Zhenge1966b42015-06-18 03:10:58 +0800751 loff_t snap_size, i_size;
752 u64 truncate_size;
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800753 u32 truncate_seq;
Sage Weil1d3576f2009-10-06 11:31:09 -0700754
755 /*
756 * Include a 'sync' in the OSD request if this is a data
757 * integrity write (e.g., O_SYNC write or fsync()), or if our
758 * cap is being revoked.
759 */
majianpengc62988e2013-06-19 15:12:06 +0800760 if ((wbc->sync_mode == WB_SYNC_ALL) ||
761 ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER))
Sage Weil1d3576f2009-10-06 11:31:09 -0700762 do_sync = 1;
763 dout("writepages_start %p dosync=%d (mode=%s)\n",
764 inode, do_sync,
765 wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
766 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
767
Yan, Zheng48fec5d2015-07-01 16:27:46 +0800768 if (ACCESS_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
Yan, Zheng6c93df52016-04-15 13:56:12 +0800769 if (ci->i_wrbuffer_ref > 0) {
770 pr_warn_ratelimited(
771 "writepage_start %p %lld forced umount\n",
772 inode, ceph_ino(inode));
773 }
Yan, Zhenga341d4d2015-07-01 17:03:23 +0800774 mapping_set_error(mapping, -EIO);
Sage Weil1d3576f2009-10-06 11:31:09 -0700775 return -EIO; /* we're in a forced umount, don't write! */
776 }
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700777 if (fsc->mount_options->wsize && fsc->mount_options->wsize < wsize)
778 wsize = fsc->mount_options->wsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300779 if (wsize < PAGE_SIZE)
780 wsize = PAGE_SIZE;
781 max_pages_ever = wsize >> PAGE_SHIFT;
Sage Weil1d3576f2009-10-06 11:31:09 -0700782
783 pagevec_init(&pvec, 0);
784
Sage Weil1d3576f2009-10-06 11:31:09 -0700785 /* where to start/end? */
786 if (wbc->range_cyclic) {
787 start = mapping->writeback_index; /* Start from prev offset */
788 end = -1;
789 dout(" cyclic, start at %lu\n", start);
790 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300791 start = wbc->range_start >> PAGE_SHIFT;
792 end = wbc->range_end >> PAGE_SHIFT;
Sage Weil1d3576f2009-10-06 11:31:09 -0700793 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
794 range_whole = 1;
795 should_loop = 0;
796 dout(" not cyclic, %lu to %lu\n", start, end);
797 }
798 index = start;
799
800retry:
801 /* find oldest snap context with dirty data */
802 ceph_put_snap_context(snapc);
Yan, Zhenge1966b42015-06-18 03:10:58 +0800803 snap_size = -1;
Sage Weil1d3576f2009-10-06 11:31:09 -0700804 snapc = get_oldest_context(inode, &snap_size);
805 if (!snapc) {
806 /* hmm, why does writepages get called when there
807 is no dirty data? */
808 dout(" no snap context with dirty data?\n");
809 goto out;
810 }
811 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
812 snapc, snapc->seq, snapc->num_snaps);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800813
814 spin_lock(&ci->i_ceph_lock);
815 truncate_seq = ci->i_truncate_seq;
816 truncate_size = ci->i_truncate_size;
Yan, Zhenge1966b42015-06-18 03:10:58 +0800817 i_size = i_size_read(inode);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800818 spin_unlock(&ci->i_ceph_lock);
819
Sage Weil1d3576f2009-10-06 11:31:09 -0700820 if (last_snapc && snapc != last_snapc) {
821 /* if we switched to a newer snapc, restart our scan at the
822 * start of the original file range. */
823 dout(" snapc differs from last pass, restarting at %lu\n",
824 index);
825 index = start;
826 }
827 last_snapc = snapc;
828
829 while (!done && index <= end) {
830 unsigned i;
831 int first;
Yan, Zheng5b646402016-01-07 16:00:17 +0800832 pgoff_t strip_unit_end = 0;
833 int num_ops = 0, op_idx;
834 int pvec_pages, locked_pages = 0;
835 struct page **pages = NULL, **data_pages;
Alex Eldere5975c72013-03-14 14:09:05 -0500836 mempool_t *pool = NULL; /* Becomes non-null if mempool used */
Sage Weil1d3576f2009-10-06 11:31:09 -0700837 struct page *page;
838 int want;
Yan, Zheng5b646402016-01-07 16:00:17 +0800839 u64 offset = 0, len = 0;
Sage Weil1d3576f2009-10-06 11:31:09 -0700840
Sage Weil1d3576f2009-10-06 11:31:09 -0700841 max_pages = max_pages_ever;
842
843get_more_pages:
844 first = -1;
845 want = min(end - index,
846 min((pgoff_t)PAGEVEC_SIZE,
847 max_pages - (pgoff_t)locked_pages) - 1)
848 + 1;
849 pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index,
850 PAGECACHE_TAG_DIRTY,
851 want);
852 dout("pagevec_lookup_tag got %d\n", pvec_pages);
853 if (!pvec_pages && !locked_pages)
854 break;
855 for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
856 page = pvec.pages[i];
857 dout("? %p idx %lu\n", page, page->index);
858 if (locked_pages == 0)
859 lock_page(page); /* first page */
860 else if (!trylock_page(page))
861 break;
862
863 /* only dirty pages, or our accounting breaks */
864 if (unlikely(!PageDirty(page)) ||
865 unlikely(page->mapping != mapping)) {
866 dout("!dirty or !mapping %p\n", page);
867 unlock_page(page);
868 break;
869 }
870 if (!wbc->range_cyclic && page->index > end) {
871 dout("end of range %p\n", page);
872 done = 1;
873 unlock_page(page);
874 break;
875 }
Yan, Zheng5b646402016-01-07 16:00:17 +0800876 if (strip_unit_end && (page->index > strip_unit_end)) {
877 dout("end of strip unit %p\n", page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700878 unlock_page(page);
879 break;
880 }
881 if (wbc->sync_mode != WB_SYNC_NONE) {
882 dout("waiting on writeback %p\n", page);
883 wait_on_page_writeback(page);
884 }
Yan, Zhenge1966b42015-06-18 03:10:58 +0800885 if (page_offset(page) >=
886 (snap_size == -1 ? i_size : snap_size)) {
887 dout("%p page eof %llu\n", page,
888 (snap_size == -1 ? i_size : snap_size));
Sage Weil1d3576f2009-10-06 11:31:09 -0700889 done = 1;
890 unlock_page(page);
891 break;
892 }
893 if (PageWriteback(page)) {
894 dout("%p under writeback\n", page);
895 unlock_page(page);
896 break;
897 }
898
899 /* only if matching snap context */
Yan, Zheng61600ef2012-05-28 14:44:30 +0800900 pgsnapc = page_snap_context(page);
Sage Weil80e755f2010-03-31 21:52:10 -0700901 if (pgsnapc->seq > snapc->seq) {
902 dout("page snapc %p %lld > oldest %p %lld\n",
903 pgsnapc, pgsnapc->seq, snapc, snapc->seq);
Sage Weil1d3576f2009-10-06 11:31:09 -0700904 unlock_page(page);
905 if (!locked_pages)
906 continue; /* keep looking for snap */
907 break;
908 }
909
910 if (!clear_page_dirty_for_io(page)) {
911 dout("%p !clear_page_dirty_for_io\n", page);
912 unlock_page(page);
913 break;
914 }
915
Alex Eldere5975c72013-03-14 14:09:05 -0500916 /*
917 * We have something to write. If this is
918 * the first locked page this time through,
Yan, Zheng5b646402016-01-07 16:00:17 +0800919 * calculate max possinle write size and
920 * allocate a page array
Alex Eldere5975c72013-03-14 14:09:05 -0500921 */
Sage Weil1d3576f2009-10-06 11:31:09 -0700922 if (locked_pages == 0) {
Yan, Zheng5b646402016-01-07 16:00:17 +0800923 u64 objnum;
924 u64 objoff;
925
Sage Weil1d3576f2009-10-06 11:31:09 -0700926 /* prepare async write request */
Alex Eldere5975c72013-03-14 14:09:05 -0500927 offset = (u64)page_offset(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700928 len = wsize;
Yan, Zheng5b646402016-01-07 16:00:17 +0800929
930 rc = ceph_calc_file_object_mapping(&ci->i_layout,
931 offset, len,
932 &objnum, &objoff,
933 &len);
934 if (rc < 0) {
Henry C Chang8c718972011-05-03 09:45:16 +0000935 unlock_page(page);
936 break;
937 }
938
Yan, Zheng5b646402016-01-07 16:00:17 +0800939 num_ops = 1 + do_sync;
940 strip_unit_end = page->index +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300941 ((len - 1) >> PAGE_SHIFT);
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800942
Yan, Zheng5b646402016-01-07 16:00:17 +0800943 BUG_ON(pages);
Alex Elder88486952013-03-14 14:09:05 -0500944 max_pages = calc_pages_for(0, (u64)len);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800945 pages = kmalloc(max_pages * sizeof (*pages),
946 GFP_NOFS);
Alex Elder88486952013-03-14 14:09:05 -0500947 if (!pages) {
948 pool = fsc->wb_pagevec_pool;
Alex Elder88486952013-03-14 14:09:05 -0500949 pages = mempool_alloc(pool, GFP_NOFS);
Alex Eldere5975c72013-03-14 14:09:05 -0500950 BUG_ON(!pages);
Alex Elder88486952013-03-14 14:09:05 -0500951 }
Yan, Zheng5b646402016-01-07 16:00:17 +0800952
953 len = 0;
954 } else if (page->index !=
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300955 (offset + len) >> PAGE_SHIFT) {
Yan, Zheng5b646402016-01-07 16:00:17 +0800956 if (num_ops >= (pool ? CEPH_OSD_SLAB_OPS :
957 CEPH_OSD_MAX_OPS)) {
958 redirty_page_for_writepage(wbc, page);
959 unlock_page(page);
960 break;
961 }
962
963 num_ops++;
964 offset = (u64)page_offset(page);
965 len = 0;
Sage Weil1d3576f2009-10-06 11:31:09 -0700966 }
967
968 /* note position of first page in pvec */
969 if (first < 0)
970 first = i;
971 dout("%p will write page %p idx %lu\n",
972 inode, page, page->index);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800973
Yan, Zheng5b646402016-01-07 16:00:17 +0800974 if (atomic_long_inc_return(&fsc->writeback_count) >
975 CONGESTION_ON_THRESH(
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700976 fsc->mount_options->congestion_kb)) {
977 set_bdi_congested(&fsc->backing_dev_info,
Sage Weil213c99e2010-08-03 10:25:11 -0700978 BLK_RW_ASYNC);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800979 }
980
Alex Eldere5975c72013-03-14 14:09:05 -0500981 pages[locked_pages] = page;
Sage Weil1d3576f2009-10-06 11:31:09 -0700982 locked_pages++;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300983 len += PAGE_SIZE;
Sage Weil1d3576f2009-10-06 11:31:09 -0700984 }
985
986 /* did we get anything? */
987 if (!locked_pages)
988 goto release_pvec_pages;
989 if (i) {
990 int j;
991 BUG_ON(!locked_pages || first < 0);
992
993 if (pvec_pages && i == pvec_pages &&
994 locked_pages < max_pages) {
995 dout("reached end pvec, trying for more\n");
996 pagevec_reinit(&pvec);
997 goto get_more_pages;
998 }
999
1000 /* shift unused pages over in the pvec... we
1001 * will need to release them below. */
1002 for (j = i; j < pvec_pages; j++) {
Yan, Zheng5b646402016-01-07 16:00:17 +08001003 dout(" pvec leftover page %p\n", pvec.pages[j]);
Sage Weil1d3576f2009-10-06 11:31:09 -07001004 pvec.pages[j-i+first] = pvec.pages[j];
1005 }
1006 pvec.nr -= i-first;
1007 }
1008
Yan, Zheng5b646402016-01-07 16:00:17 +08001009new_request:
Alex Eldere5975c72013-03-14 14:09:05 -05001010 offset = page_offset(pages[0]);
Yan, Zheng5b646402016-01-07 16:00:17 +08001011 len = wsize;
1012
1013 req = ceph_osdc_new_request(&fsc->client->osdc,
1014 &ci->i_layout, vino,
1015 offset, &len, 0, num_ops,
1016 CEPH_OSD_OP_WRITE,
1017 CEPH_OSD_FLAG_WRITE |
1018 CEPH_OSD_FLAG_ONDISK,
1019 snapc, truncate_seq,
1020 truncate_size, false);
1021 if (IS_ERR(req)) {
1022 req = ceph_osdc_new_request(&fsc->client->osdc,
1023 &ci->i_layout, vino,
1024 offset, &len, 0,
1025 min(num_ops,
1026 CEPH_OSD_SLAB_OPS),
1027 CEPH_OSD_OP_WRITE,
1028 CEPH_OSD_FLAG_WRITE |
1029 CEPH_OSD_FLAG_ONDISK,
1030 snapc, truncate_seq,
1031 truncate_size, true);
1032 BUG_ON(IS_ERR(req));
Yan, Zhenge1966b42015-06-18 03:10:58 +08001033 }
Yan, Zheng5b646402016-01-07 16:00:17 +08001034 BUG_ON(len < page_offset(pages[locked_pages - 1]) +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001035 PAGE_SIZE - offset);
Sage Weil1d3576f2009-10-06 11:31:09 -07001036
Yan, Zheng5b646402016-01-07 16:00:17 +08001037 req->r_callback = writepages_finish;
1038 req->r_inode = inode;
1039
1040 /* Format the osd request message and submit the write */
1041 len = 0;
1042 data_pages = pages;
1043 op_idx = 0;
1044 for (i = 0; i < locked_pages; i++) {
1045 u64 cur_offset = page_offset(pages[i]);
1046 if (offset + len != cur_offset) {
1047 if (op_idx + do_sync + 1 == req->r_num_ops)
1048 break;
1049 osd_req_op_extent_dup_last(req, op_idx,
1050 cur_offset - offset);
1051 dout("writepages got pages at %llu~%llu\n",
1052 offset, len);
1053 osd_req_op_extent_osd_data_pages(req, op_idx,
1054 data_pages, len, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05001055 !!pool, false);
Yan, Zheng5b646402016-01-07 16:00:17 +08001056 osd_req_op_extent_update(req, op_idx, len);
Alex Eldere5975c72013-03-14 14:09:05 -05001057
Yan, Zheng5b646402016-01-07 16:00:17 +08001058 len = 0;
1059 offset = cur_offset;
1060 data_pages = pages + i;
1061 op_idx++;
1062 }
1063
1064 set_page_writeback(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001065 len += PAGE_SIZE;
Yan, Zheng5b646402016-01-07 16:00:17 +08001066 }
1067
1068 if (snap_size != -1) {
1069 len = min(len, snap_size - offset);
1070 } else if (i == locked_pages) {
1071 /* writepages_finish() clears writeback pages
1072 * according to the data length, so make sure
1073 * data length covers all locked pages */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001074 u64 min_len = len + 1 - PAGE_SIZE;
Yan, Zheng5b646402016-01-07 16:00:17 +08001075 len = min(len, (u64)i_size_read(inode) - offset);
1076 len = max(len, min_len);
1077 }
1078 dout("writepages got pages at %llu~%llu\n", offset, len);
1079
1080 osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len,
1081 0, !!pool, false);
1082 osd_req_op_extent_update(req, op_idx, len);
1083
1084 if (do_sync) {
1085 op_idx++;
1086 osd_req_op_init(req, op_idx, CEPH_OSD_OP_STARTSYNC, 0);
1087 }
1088 BUG_ON(op_idx + 1 != req->r_num_ops);
1089
Alex Eldere5975c72013-03-14 14:09:05 -05001090 pool = NULL;
Yan, Zheng5b646402016-01-07 16:00:17 +08001091 if (i < locked_pages) {
1092 BUG_ON(num_ops <= req->r_num_ops);
1093 num_ops -= req->r_num_ops;
1094 num_ops += do_sync;
1095 locked_pages -= i;
Alex Eldere5975c72013-03-14 14:09:05 -05001096
Yan, Zheng5b646402016-01-07 16:00:17 +08001097 /* allocate new pages array for next request */
1098 data_pages = pages;
1099 pages = kmalloc(locked_pages * sizeof (*pages),
1100 GFP_NOFS);
1101 if (!pages) {
1102 pool = fsc->wb_pagevec_pool;
1103 pages = mempool_alloc(pool, GFP_NOFS);
1104 BUG_ON(!pages);
1105 }
1106 memcpy(pages, data_pages + i,
1107 locked_pages * sizeof(*pages));
1108 memset(data_pages + i, 0,
1109 locked_pages * sizeof(*pages));
1110 } else {
1111 BUG_ON(num_ops != req->r_num_ops);
1112 index = pages[i - 1]->index + 1;
1113 /* request message now owns the pages array */
1114 pages = NULL;
1115 }
Alex Eldere5975c72013-03-14 14:09:05 -05001116
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001117 req->r_mtime = inode->i_mtime;
Sage Weil9d6fcb02011-05-12 15:48:16 -07001118 rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
1119 BUG_ON(rc);
Sage Weil1d3576f2009-10-06 11:31:09 -07001120 req = NULL;
1121
Yan, Zheng5b646402016-01-07 16:00:17 +08001122 wbc->nr_to_write -= i;
1123 if (pages)
1124 goto new_request;
1125
Sage Weil1d3576f2009-10-06 11:31:09 -07001126 if (wbc->nr_to_write <= 0)
1127 done = 1;
1128
1129release_pvec_pages:
1130 dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
1131 pvec.nr ? pvec.pages[0] : NULL);
1132 pagevec_release(&pvec);
1133
1134 if (locked_pages && !done)
1135 goto retry;
1136 }
1137
1138 if (should_loop && !done) {
1139 /* more to do; loop back to beginning of file */
1140 dout("writepages looping back to beginning of file\n");
1141 should_loop = 0;
1142 index = 0;
1143 goto retry;
1144 }
1145
1146 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1147 mapping->writeback_index = index;
1148
1149out:
Ilya Dryomov3ed97d62016-04-26 15:05:29 +02001150 ceph_osdc_put_request(req);
Sage Weil1d3576f2009-10-06 11:31:09 -07001151 ceph_put_snap_context(snapc);
1152 dout("writepages done, rc = %d\n", rc);
Sage Weil1d3576f2009-10-06 11:31:09 -07001153 return rc;
1154}
1155
1156
1157
1158/*
1159 * See if a given @snapc is either writeable, or already written.
1160 */
1161static int context_is_writeable_or_written(struct inode *inode,
1162 struct ceph_snap_context *snapc)
1163{
1164 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL);
Sage Weil6298a332010-03-31 22:01:38 -07001165 int ret = !oldest || snapc->seq <= oldest->seq;
1166
1167 ceph_put_snap_context(oldest);
1168 return ret;
Sage Weil1d3576f2009-10-06 11:31:09 -07001169}
1170
1171/*
1172 * We are only allowed to write into/dirty the page if the page is
1173 * clean, or already dirty within the same snap context.
Sage Weil8f883c22010-03-19 13:27:53 -07001174 *
1175 * called with page locked.
1176 * return success with page locked,
1177 * or any failure (incl -EAGAIN) with page unlocked.
Sage Weil1d3576f2009-10-06 11:31:09 -07001178 */
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001179static int ceph_update_writeable_page(struct file *file,
1180 loff_t pos, unsigned len,
1181 struct page *page)
Sage Weil1d3576f2009-10-06 11:31:09 -07001182{
Al Viro496ad9a2013-01-23 17:07:38 -05001183 struct inode *inode = file_inode(file);
Yan, Zheng6c93df52016-04-15 13:56:12 +08001184 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -07001185 struct ceph_inode_info *ci = ceph_inode(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001186 loff_t page_off = pos & PAGE_MASK;
1187 int pos_in_page = pos & ~PAGE_MASK;
Sage Weil1d3576f2009-10-06 11:31:09 -07001188 int end_in_page = pos_in_page + len;
1189 loff_t i_size;
Sage Weil1d3576f2009-10-06 11:31:09 -07001190 int r;
Sage Weil80e755f2010-03-31 21:52:10 -07001191 struct ceph_snap_context *snapc, *oldest;
Sage Weil1d3576f2009-10-06 11:31:09 -07001192
Yan, Zheng6c93df52016-04-15 13:56:12 +08001193 if (ACCESS_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
1194 dout(" page %p forced umount\n", page);
1195 unlock_page(page);
1196 return -EIO;
1197 }
1198
Sage Weil1d3576f2009-10-06 11:31:09 -07001199retry_locked:
1200 /* writepages currently holds page lock, but if we change that later, */
1201 wait_on_page_writeback(page);
1202
Yan, Zheng61600ef2012-05-28 14:44:30 +08001203 snapc = page_snap_context(page);
Sage Weil80e755f2010-03-31 21:52:10 -07001204 if (snapc && snapc != ci->i_head_snapc) {
Sage Weil1d3576f2009-10-06 11:31:09 -07001205 /*
1206 * this page is already dirty in another (older) snap
1207 * context! is it writeable now?
1208 */
Sage Weil80e755f2010-03-31 21:52:10 -07001209 oldest = get_oldest_context(inode, NULL);
Sage Weil1d3576f2009-10-06 11:31:09 -07001210
Sage Weil80e755f2010-03-31 21:52:10 -07001211 if (snapc->seq > oldest->seq) {
Sage Weil6298a332010-03-31 22:01:38 -07001212 ceph_put_snap_context(oldest);
Sage Weil1d3576f2009-10-06 11:31:09 -07001213 dout(" page %p snapc %p not current or oldest\n",
Sage Weil6298a332010-03-31 22:01:38 -07001214 page, snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -07001215 /*
1216 * queue for writeback, and wait for snapc to
1217 * be writeable or written
1218 */
Sage Weil6298a332010-03-31 22:01:38 -07001219 snapc = ceph_get_snap_context(snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -07001220 unlock_page(page);
Sage Weil3c6f6b72010-02-09 15:24:44 -08001221 ceph_queue_writeback(inode);
Yan, Zhenga78bbd42016-05-13 11:30:24 +08001222 r = wait_event_killable(ci->i_cap_wq,
Sage Weil1d3576f2009-10-06 11:31:09 -07001223 context_is_writeable_or_written(inode, snapc));
1224 ceph_put_snap_context(snapc);
Sage Weil8f883c22010-03-19 13:27:53 -07001225 if (r == -ERESTARTSYS)
1226 return r;
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001227 return -EAGAIN;
Sage Weil1d3576f2009-10-06 11:31:09 -07001228 }
Sage Weil6298a332010-03-31 22:01:38 -07001229 ceph_put_snap_context(oldest);
Sage Weil1d3576f2009-10-06 11:31:09 -07001230
1231 /* yay, writeable, do it now (without dropping page lock) */
1232 dout(" page %p snapc %p not current, but oldest\n",
1233 page, snapc);
1234 if (!clear_page_dirty_for_io(page))
1235 goto retry_locked;
1236 r = writepage_nounlock(page, NULL);
1237 if (r < 0)
1238 goto fail_nosnap;
1239 goto retry_locked;
1240 }
1241
1242 if (PageUptodate(page)) {
1243 dout(" page %p already uptodate\n", page);
1244 return 0;
1245 }
1246
1247 /* full page? */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001248 if (pos_in_page == 0 && len == PAGE_SIZE)
Sage Weil1d3576f2009-10-06 11:31:09 -07001249 return 0;
1250
1251 /* past end of file? */
Yan, Zheng99c88e62015-12-30 11:32:46 +08001252 i_size = i_size_read(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -07001253
Sage Weil1d3576f2009-10-06 11:31:09 -07001254 if (page_off >= i_size ||
1255 (pos_in_page == 0 && (pos+len) >= i_size &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001256 end_in_page - pos_in_page != PAGE_SIZE)) {
Sage Weil1d3576f2009-10-06 11:31:09 -07001257 dout(" zeroing %p 0 - %d and %d - %d\n",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001258 page, pos_in_page, end_in_page, (int)PAGE_SIZE);
Sage Weil1d3576f2009-10-06 11:31:09 -07001259 zero_user_segments(page,
1260 0, pos_in_page,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001261 end_in_page, PAGE_SIZE);
Sage Weil1d3576f2009-10-06 11:31:09 -07001262 return 0;
1263 }
1264
1265 /* we need to read it. */
Sage Weil1d3576f2009-10-06 11:31:09 -07001266 r = readpage_nounlock(file, page);
1267 if (r < 0)
1268 goto fail_nosnap;
1269 goto retry_locked;
Sage Weil1d3576f2009-10-06 11:31:09 -07001270fail_nosnap:
1271 unlock_page(page);
1272 return r;
1273}
1274
1275/*
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001276 * We are only allowed to write into/dirty the page if the page is
1277 * clean, or already dirty within the same snap context.
1278 */
1279static int ceph_write_begin(struct file *file, struct address_space *mapping,
1280 loff_t pos, unsigned len, unsigned flags,
1281 struct page **pagep, void **fsdata)
1282{
Al Viro496ad9a2013-01-23 17:07:38 -05001283 struct inode *inode = file_inode(file);
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001284 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001285 pgoff_t index = pos >> PAGE_SHIFT;
Sage Weil7971bd92013-05-01 21:15:58 -07001286 int r;
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001287
1288 do {
Sage Weil8f883c22010-03-19 13:27:53 -07001289 /* get a page */
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001290 page = grab_cache_page_write_begin(mapping, index, 0);
Sage Weil7971bd92013-05-01 21:15:58 -07001291 if (!page)
1292 return -ENOMEM;
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001293
1294 dout("write_begin file %p inode %p page %p %d~%d\n", file,
Sage Weil213c99e2010-08-03 10:25:11 -07001295 inode, page, (int)pos, (int)len);
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001296
1297 r = ceph_update_writeable_page(file, pos, len, page);
Taesoo Kimc1d00b22015-03-20 17:36:56 -04001298 if (r < 0)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001299 put_page(page);
Taesoo Kimc1d00b22015-03-20 17:36:56 -04001300 else
1301 *pagep = page;
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001302 } while (r == -EAGAIN);
1303
1304 return r;
1305}
1306
1307/*
Sage Weil1d3576f2009-10-06 11:31:09 -07001308 * we don't do anything in here that simple_write_end doesn't do
Yan, Zheng5dda377c2015-04-30 14:40:54 +08001309 * except adjust dirty page accounting
Sage Weil1d3576f2009-10-06 11:31:09 -07001310 */
1311static int ceph_write_end(struct file *file, struct address_space *mapping,
1312 loff_t pos, unsigned len, unsigned copied,
1313 struct page *page, void *fsdata)
1314{
Al Viro496ad9a2013-01-23 17:07:38 -05001315 struct inode *inode = file_inode(file);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001316 unsigned from = pos & (PAGE_SIZE - 1);
Sage Weil1d3576f2009-10-06 11:31:09 -07001317 int check_cap = 0;
1318
1319 dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
1320 inode, page, (int)pos, (int)copied, (int)len);
1321
1322 /* zero the stale part of the page if we did a short copy */
1323 if (copied < len)
1324 zero_user_segment(page, from+copied, len);
1325
1326 /* did file size increase? */
Yan, Zheng99c88e62015-12-30 11:32:46 +08001327 if (pos+copied > i_size_read(inode))
Sage Weil1d3576f2009-10-06 11:31:09 -07001328 check_cap = ceph_inode_set_size(inode, pos+copied);
1329
1330 if (!PageUptodate(page))
1331 SetPageUptodate(page);
1332
1333 set_page_dirty(page);
1334
1335 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001336 put_page(page);
Sage Weil1d3576f2009-10-06 11:31:09 -07001337
1338 if (check_cap)
1339 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
1340
1341 return copied;
1342}
1343
1344/*
1345 * we set .direct_IO to indicate direct io is supported, but since we
1346 * intercept O_DIRECT reads and writes early, this function should
1347 * never get called.
1348 */
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001349static ssize_t ceph_direct_io(struct kiocb *iocb, struct iov_iter *iter)
Sage Weil1d3576f2009-10-06 11:31:09 -07001350{
1351 WARN_ON(1);
1352 return -EINVAL;
1353}
1354
1355const struct address_space_operations ceph_aops = {
1356 .readpage = ceph_readpage,
1357 .readpages = ceph_readpages,
1358 .writepage = ceph_writepage,
1359 .writepages = ceph_writepages_start,
1360 .write_begin = ceph_write_begin,
1361 .write_end = ceph_write_end,
1362 .set_page_dirty = ceph_set_page_dirty,
1363 .invalidatepage = ceph_invalidatepage,
1364 .releasepage = ceph_releasepage,
1365 .direct_IO = ceph_direct_io,
1366};
1367
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001368static void ceph_block_sigs(sigset_t *oldset)
1369{
1370 sigset_t mask;
1371 siginitsetinv(&mask, sigmask(SIGKILL));
1372 sigprocmask(SIG_BLOCK, &mask, oldset);
1373}
1374
1375static void ceph_restore_sigs(sigset_t *oldset)
1376{
1377 sigprocmask(SIG_SETMASK, oldset, NULL);
1378}
Sage Weil1d3576f2009-10-06 11:31:09 -07001379
1380/*
1381 * vm ops
1382 */
Yan, Zheng61f68812013-11-28 14:28:14 +08001383static int ceph_filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1384{
1385 struct inode *inode = file_inode(vma->vm_file);
1386 struct ceph_inode_info *ci = ceph_inode(inode);
1387 struct ceph_file_info *fi = vma->vm_file->private_data;
Yan, Zheng3738daa2014-11-14 22:10:07 +08001388 struct page *pinned_page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001389 loff_t off = vmf->pgoff << PAGE_SHIFT;
Yan, Zheng61f68812013-11-28 14:28:14 +08001390 int want, got, ret;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001391 sigset_t oldset;
1392
1393 ceph_block_sigs(&oldset);
Yan, Zheng61f68812013-11-28 14:28:14 +08001394
1395 dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001396 inode, ceph_vinop(inode), off, (size_t)PAGE_SIZE);
Yan, Zheng61f68812013-11-28 14:28:14 +08001397 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1398 want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1399 else
1400 want = CEPH_CAP_FILE_CACHE;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001401
1402 got = 0;
1403 ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, -1, &got, &pinned_page);
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001404 if (ret < 0)
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001405 goto out_restore;
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001406
Yan, Zheng61f68812013-11-28 14:28:14 +08001407 dout("filemap_fault %p %llu~%zd got cap refs on %s\n",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001408 inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got));
Yan, Zheng61f68812013-11-28 14:28:14 +08001409
Yan, Zheng83701242014-11-14 22:36:18 +08001410 if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
Yan, Zhenge99b0ea2016-10-25 10:51:55 +08001411 ci->i_inline_version == CEPH_INLINE_NONE) {
1412 current->journal_info = vma->vm_file;
Yan, Zheng83701242014-11-14 22:36:18 +08001413 ret = filemap_fault(vma, vmf);
Yan, Zhenge99b0ea2016-10-25 10:51:55 +08001414 current->journal_info = NULL;
1415 } else
Yan, Zheng83701242014-11-14 22:36:18 +08001416 ret = -EAGAIN;
Yan, Zheng61f68812013-11-28 14:28:14 +08001417
1418 dout("filemap_fault %p %llu~%zd dropping cap refs on %s ret %d\n",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001419 inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got), ret);
Yan, Zheng3738daa2014-11-14 22:10:07 +08001420 if (pinned_page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001421 put_page(pinned_page);
Yan, Zheng61f68812013-11-28 14:28:14 +08001422 ceph_put_cap_refs(ci, got);
1423
Yan, Zheng83701242014-11-14 22:36:18 +08001424 if (ret != -EAGAIN)
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001425 goto out_restore;
Yan, Zheng83701242014-11-14 22:36:18 +08001426
1427 /* read inline data */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001428 if (off >= PAGE_SIZE) {
Yan, Zheng83701242014-11-14 22:36:18 +08001429 /* does not support inline data > PAGE_SIZE */
1430 ret = VM_FAULT_SIGBUS;
1431 } else {
1432 int ret1;
1433 struct address_space *mapping = inode->i_mapping;
1434 struct page *page = find_or_create_page(mapping, 0,
Michal Hockoc62d2552015-11-06 16:28:49 -08001435 mapping_gfp_constraint(mapping,
1436 ~__GFP_FS));
Yan, Zheng83701242014-11-14 22:36:18 +08001437 if (!page) {
1438 ret = VM_FAULT_OOM;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001439 goto out_inline;
Yan, Zheng83701242014-11-14 22:36:18 +08001440 }
1441 ret1 = __ceph_do_getattr(inode, page,
1442 CEPH_STAT_CAP_INLINE_DATA, true);
1443 if (ret1 < 0 || off >= i_size_read(inode)) {
1444 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001445 put_page(page);
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001446 if (ret1 < 0)
1447 ret = ret1;
1448 else
1449 ret = VM_FAULT_SIGBUS;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001450 goto out_inline;
Yan, Zheng83701242014-11-14 22:36:18 +08001451 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001452 if (ret1 < PAGE_SIZE)
1453 zero_user_segment(page, ret1, PAGE_SIZE);
Yan, Zheng83701242014-11-14 22:36:18 +08001454 else
1455 flush_dcache_page(page);
1456 SetPageUptodate(page);
1457 vmf->page = page;
1458 ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001459out_inline:
1460 dout("filemap_fault %p %llu~%zd read inline data ret %d\n",
1461 inode, off, (size_t)PAGE_SIZE, ret);
Yan, Zheng83701242014-11-14 22:36:18 +08001462 }
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001463out_restore:
1464 ceph_restore_sigs(&oldset);
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001465 if (ret < 0)
1466 ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
1467
Yan, Zheng61f68812013-11-28 14:28:14 +08001468 return ret;
1469}
Sage Weil1d3576f2009-10-06 11:31:09 -07001470
1471/*
1472 * Reuse write_begin here for simplicity.
1473 */
1474static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1475{
Al Viro496ad9a2013-01-23 17:07:38 -05001476 struct inode *inode = file_inode(vma->vm_file);
Yan, Zheng61f68812013-11-28 14:28:14 +08001477 struct ceph_inode_info *ci = ceph_inode(inode);
1478 struct ceph_file_info *fi = vma->vm_file->private_data;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001479 struct ceph_cap_flush *prealloc_cf;
Yan, Zheng61f68812013-11-28 14:28:14 +08001480 struct page *page = vmf->page;
Alex Elder6285bc22012-10-02 10:25:51 -05001481 loff_t off = page_offset(page);
Yan, Zheng61f68812013-11-28 14:28:14 +08001482 loff_t size = i_size_read(inode);
1483 size_t len;
1484 int want, got, ret;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001485 sigset_t oldset;
Sage Weil1d3576f2009-10-06 11:31:09 -07001486
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001487 prealloc_cf = ceph_alloc_cap_flush();
1488 if (!prealloc_cf)
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001489 return VM_FAULT_OOM;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001490
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001491 ceph_block_sigs(&oldset);
Sage Weil1d3576f2009-10-06 11:31:09 -07001492
Yan, Zheng28127bd2014-11-14 22:38:29 +08001493 if (ci->i_inline_version != CEPH_INLINE_NONE) {
1494 struct page *locked_page = NULL;
1495 if (off == 0) {
1496 lock_page(page);
1497 locked_page = page;
1498 }
1499 ret = ceph_uninline_data(vma->vm_file, locked_page);
1500 if (locked_page)
1501 unlock_page(locked_page);
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001502 if (ret < 0)
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001503 goto out_free;
Yan, Zheng28127bd2014-11-14 22:38:29 +08001504 }
1505
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001506 if (off + PAGE_SIZE <= size)
1507 len = PAGE_SIZE;
Sage Weil1d3576f2009-10-06 11:31:09 -07001508 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001509 len = size & ~PAGE_MASK;
Sage Weil1d3576f2009-10-06 11:31:09 -07001510
Yan, Zheng61f68812013-11-28 14:28:14 +08001511 dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
1512 inode, ceph_vinop(inode), off, len, size);
1513 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1514 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1515 else
1516 want = CEPH_CAP_FILE_BUFFER;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001517
1518 got = 0;
1519 ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, off + len,
1520 &got, NULL);
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001521 if (ret < 0)
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001522 goto out_free;
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001523
Yan, Zheng61f68812013-11-28 14:28:14 +08001524 dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
1525 inode, off, len, ceph_cap_string(got));
1526
1527 /* Update time before taking page lock */
1528 file_update_time(vma->vm_file);
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001529
Yan, Zhengf0b33df2016-05-10 19:09:06 +08001530 do {
1531 lock_page(page);
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001532
Yan, Zhengf0b33df2016-05-10 19:09:06 +08001533 if ((off > size) || (page->mapping != inode->i_mapping)) {
1534 unlock_page(page);
1535 ret = VM_FAULT_NOPAGE;
1536 break;
1537 }
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001538
Yan, Zhengf0b33df2016-05-10 19:09:06 +08001539 ret = ceph_update_writeable_page(vma->vm_file, off, len, page);
1540 if (ret >= 0) {
1541 /* success. we'll keep the page locked. */
1542 set_page_dirty(page);
1543 ret = VM_FAULT_LOCKED;
1544 }
1545 } while (ret == -EAGAIN);
1546
Yan, Zheng28127bd2014-11-14 22:38:29 +08001547 if (ret == VM_FAULT_LOCKED ||
1548 ci->i_inline_version != CEPH_INLINE_NONE) {
Yan, Zheng61f68812013-11-28 14:28:14 +08001549 int dirty;
1550 spin_lock(&ci->i_ceph_lock);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001551 ci->i_inline_version = CEPH_INLINE_NONE;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001552 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1553 &prealloc_cf);
Yan, Zheng61f68812013-11-28 14:28:14 +08001554 spin_unlock(&ci->i_ceph_lock);
1555 if (dirty)
1556 __mark_inode_dirty(inode, dirty);
1557 }
1558
1559 dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %d\n",
1560 inode, off, len, ceph_cap_string(got), ret);
1561 ceph_put_cap_refs(ci, got);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001562out_free:
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001563 ceph_restore_sigs(&oldset);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001564 ceph_free_cap_flush(prealloc_cf);
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001565 if (ret < 0)
1566 ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
Sage Weil1d3576f2009-10-06 11:31:09 -07001567 return ret;
1568}
1569
Yan, Zheng31c542a2014-11-14 21:41:55 +08001570void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
1571 char *data, size_t len)
1572{
1573 struct address_space *mapping = inode->i_mapping;
1574 struct page *page;
1575
1576 if (locked_page) {
1577 page = locked_page;
1578 } else {
1579 if (i_size_read(inode) == 0)
1580 return;
1581 page = find_or_create_page(mapping, 0,
Michal Hockoc62d2552015-11-06 16:28:49 -08001582 mapping_gfp_constraint(mapping,
1583 ~__GFP_FS));
Yan, Zheng31c542a2014-11-14 21:41:55 +08001584 if (!page)
1585 return;
1586 if (PageUptodate(page)) {
1587 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001588 put_page(page);
Yan, Zheng31c542a2014-11-14 21:41:55 +08001589 return;
1590 }
1591 }
1592
Ilya Dryomov0668ff52014-12-19 13:10:10 +03001593 dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
Yan, Zheng31c542a2014-11-14 21:41:55 +08001594 inode, ceph_vinop(inode), len, locked_page);
1595
1596 if (len > 0) {
1597 void *kaddr = kmap_atomic(page);
1598 memcpy(kaddr, data, len);
1599 kunmap_atomic(kaddr);
1600 }
1601
1602 if (page != locked_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001603 if (len < PAGE_SIZE)
1604 zero_user_segment(page, len, PAGE_SIZE);
Yan, Zheng31c542a2014-11-14 21:41:55 +08001605 else
1606 flush_dcache_page(page);
1607
1608 SetPageUptodate(page);
1609 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001610 put_page(page);
Yan, Zheng31c542a2014-11-14 21:41:55 +08001611 }
1612}
1613
Yan, Zheng28127bd2014-11-14 22:38:29 +08001614int ceph_uninline_data(struct file *filp, struct page *locked_page)
1615{
1616 struct inode *inode = file_inode(filp);
1617 struct ceph_inode_info *ci = ceph_inode(inode);
1618 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1619 struct ceph_osd_request *req;
1620 struct page *page = NULL;
1621 u64 len, inline_version;
1622 int err = 0;
1623 bool from_pagecache = false;
1624
1625 spin_lock(&ci->i_ceph_lock);
1626 inline_version = ci->i_inline_version;
1627 spin_unlock(&ci->i_ceph_lock);
1628
1629 dout("uninline_data %p %llx.%llx inline_version %llu\n",
1630 inode, ceph_vinop(inode), inline_version);
1631
1632 if (inline_version == 1 || /* initial version, no data */
1633 inline_version == CEPH_INLINE_NONE)
1634 goto out;
1635
1636 if (locked_page) {
1637 page = locked_page;
1638 WARN_ON(!PageUptodate(page));
1639 } else if (ceph_caps_issued(ci) &
1640 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) {
1641 page = find_get_page(inode->i_mapping, 0);
1642 if (page) {
1643 if (PageUptodate(page)) {
1644 from_pagecache = true;
1645 lock_page(page);
1646 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001647 put_page(page);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001648 page = NULL;
1649 }
1650 }
1651 }
1652
1653 if (page) {
1654 len = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001655 if (len > PAGE_SIZE)
1656 len = PAGE_SIZE;
Yan, Zheng28127bd2014-11-14 22:38:29 +08001657 } else {
1658 page = __page_cache_alloc(GFP_NOFS);
1659 if (!page) {
1660 err = -ENOMEM;
1661 goto out;
1662 }
1663 err = __ceph_do_getattr(inode, page,
1664 CEPH_STAT_CAP_INLINE_DATA, true);
1665 if (err < 0) {
1666 /* no inline data */
1667 if (err == -ENODATA)
1668 err = 0;
1669 goto out;
1670 }
1671 len = err;
1672 }
1673
1674 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1675 ceph_vino(inode), 0, &len, 0, 1,
1676 CEPH_OSD_OP_CREATE,
1677 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Ilya Dryomov34b759b2016-02-16 15:00:24 +01001678 NULL, 0, 0, false);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001679 if (IS_ERR(req)) {
1680 err = PTR_ERR(req);
1681 goto out;
1682 }
1683
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001684 req->r_mtime = inode->i_mtime;
Yan, Zheng28127bd2014-11-14 22:38:29 +08001685 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1686 if (!err)
1687 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1688 ceph_osdc_put_request(req);
1689 if (err < 0)
1690 goto out;
1691
1692 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1693 ceph_vino(inode), 0, &len, 1, 3,
1694 CEPH_OSD_OP_WRITE,
1695 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Ilya Dryomov34b759b2016-02-16 15:00:24 +01001696 NULL, ci->i_truncate_seq,
1697 ci->i_truncate_size, false);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001698 if (IS_ERR(req)) {
1699 err = PTR_ERR(req);
1700 goto out;
1701 }
1702
1703 osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false);
1704
Yan, Zhengec137c12015-04-13 11:25:07 +08001705 {
1706 __le64 xattr_buf = cpu_to_le64(inline_version);
1707 err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
1708 "inline_version", &xattr_buf,
1709 sizeof(xattr_buf),
1710 CEPH_OSD_CMPXATTR_OP_GT,
1711 CEPH_OSD_CMPXATTR_MODE_U64);
1712 if (err)
1713 goto out_put;
1714 }
Yan, Zheng28127bd2014-11-14 22:38:29 +08001715
Yan, Zhengec137c12015-04-13 11:25:07 +08001716 {
1717 char xattr_buf[32];
1718 int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
1719 "%llu", inline_version);
1720 err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
1721 "inline_version",
1722 xattr_buf, xattr_len, 0, 0);
1723 if (err)
1724 goto out_put;
1725 }
Yan, Zheng28127bd2014-11-14 22:38:29 +08001726
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001727 req->r_mtime = inode->i_mtime;
Yan, Zheng28127bd2014-11-14 22:38:29 +08001728 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1729 if (!err)
1730 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1731out_put:
1732 ceph_osdc_put_request(req);
1733 if (err == -ECANCELED)
1734 err = 0;
1735out:
1736 if (page && page != locked_page) {
1737 if (from_pagecache) {
1738 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001739 put_page(page);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001740 } else
1741 __free_pages(page, 0);
1742 }
1743
1744 dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
1745 inode, ceph_vinop(inode), inline_version, err);
1746 return err;
1747}
1748
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07001749static const struct vm_operations_struct ceph_vmops = {
Yan, Zheng61f68812013-11-28 14:28:14 +08001750 .fault = ceph_filemap_fault,
Sage Weil1d3576f2009-10-06 11:31:09 -07001751 .page_mkwrite = ceph_page_mkwrite,
1752};
1753
1754int ceph_mmap(struct file *file, struct vm_area_struct *vma)
1755{
1756 struct address_space *mapping = file->f_mapping;
1757
1758 if (!mapping->a_ops->readpage)
1759 return -ENOEXEC;
1760 file_accessed(file);
1761 vma->vm_ops = &ceph_vmops;
Sage Weil1d3576f2009-10-06 11:31:09 -07001762 return 0;
1763}
Yan, Zheng10183a62015-04-27 15:33:28 +08001764
1765enum {
1766 POOL_READ = 1,
1767 POOL_WRITE = 2,
1768};
1769
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001770static int __ceph_pool_perm_get(struct ceph_inode_info *ci,
1771 s64 pool, struct ceph_string *pool_ns)
Yan, Zheng10183a62015-04-27 15:33:28 +08001772{
1773 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1774 struct ceph_mds_client *mdsc = fsc->mdsc;
1775 struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
1776 struct rb_node **p, *parent;
1777 struct ceph_pool_perm *perm;
1778 struct page **pages;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001779 size_t pool_ns_len;
Yan, Zheng10183a62015-04-27 15:33:28 +08001780 int err = 0, err2 = 0, have = 0;
1781
1782 down_read(&mdsc->pool_perm_rwsem);
1783 p = &mdsc->pool_perm_tree.rb_node;
1784 while (*p) {
1785 perm = rb_entry(*p, struct ceph_pool_perm, node);
1786 if (pool < perm->pool)
1787 p = &(*p)->rb_left;
1788 else if (pool > perm->pool)
1789 p = &(*p)->rb_right;
1790 else {
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001791 int ret = ceph_compare_string(pool_ns,
1792 perm->pool_ns,
1793 perm->pool_ns_len);
1794 if (ret < 0)
1795 p = &(*p)->rb_left;
1796 else if (ret > 0)
1797 p = &(*p)->rb_right;
1798 else {
1799 have = perm->perm;
1800 break;
1801 }
Yan, Zheng10183a62015-04-27 15:33:28 +08001802 }
1803 }
1804 up_read(&mdsc->pool_perm_rwsem);
1805 if (*p)
1806 goto out;
1807
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001808 if (pool_ns)
1809 dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n",
1810 pool, (int)pool_ns->len, pool_ns->str);
1811 else
1812 dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool);
Yan, Zheng10183a62015-04-27 15:33:28 +08001813
1814 down_write(&mdsc->pool_perm_rwsem);
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001815 p = &mdsc->pool_perm_tree.rb_node;
Yan, Zheng10183a62015-04-27 15:33:28 +08001816 parent = NULL;
1817 while (*p) {
1818 parent = *p;
1819 perm = rb_entry(parent, struct ceph_pool_perm, node);
1820 if (pool < perm->pool)
1821 p = &(*p)->rb_left;
1822 else if (pool > perm->pool)
1823 p = &(*p)->rb_right;
1824 else {
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001825 int ret = ceph_compare_string(pool_ns,
1826 perm->pool_ns,
1827 perm->pool_ns_len);
1828 if (ret < 0)
1829 p = &(*p)->rb_left;
1830 else if (ret > 0)
1831 p = &(*p)->rb_right;
1832 else {
1833 have = perm->perm;
1834 break;
1835 }
Yan, Zheng10183a62015-04-27 15:33:28 +08001836 }
1837 }
1838 if (*p) {
1839 up_write(&mdsc->pool_perm_rwsem);
1840 goto out;
1841 }
1842
Ilya Dryomov34b759b2016-02-16 15:00:24 +01001843 rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
Yan, Zheng10183a62015-04-27 15:33:28 +08001844 1, false, GFP_NOFS);
1845 if (!rd_req) {
1846 err = -ENOMEM;
1847 goto out_unlock;
1848 }
1849
1850 rd_req->r_flags = CEPH_OSD_FLAG_READ;
1851 osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
1852 rd_req->r_base_oloc.pool = pool;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001853 if (pool_ns)
1854 rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns);
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001855 ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino);
Yan, Zheng10183a62015-04-27 15:33:28 +08001856
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02001857 err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS);
1858 if (err)
1859 goto out_unlock;
Yan, Zheng10183a62015-04-27 15:33:28 +08001860
Ilya Dryomov34b759b2016-02-16 15:00:24 +01001861 wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
Yan, Zheng10183a62015-04-27 15:33:28 +08001862 1, false, GFP_NOFS);
1863 if (!wr_req) {
1864 err = -ENOMEM;
1865 goto out_unlock;
1866 }
1867
Ilya Dryomovfe5da052016-04-28 16:07:24 +02001868 wr_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ACK;
Yan, Zheng10183a62015-04-27 15:33:28 +08001869 osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001870 ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc);
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001871 ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid);
Yan, Zheng10183a62015-04-27 15:33:28 +08001872
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02001873 err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS);
1874 if (err)
1875 goto out_unlock;
Yan, Zheng10183a62015-04-27 15:33:28 +08001876
1877 /* one page should be large enough for STAT data */
1878 pages = ceph_alloc_page_vector(1, GFP_KERNEL);
1879 if (IS_ERR(pages)) {
1880 err = PTR_ERR(pages);
1881 goto out_unlock;
1882 }
1883
1884 osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
1885 0, false, true);
Yan, Zheng10183a62015-04-27 15:33:28 +08001886 err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false);
1887
Ilya Dryomovbb873b52016-05-26 00:29:52 +02001888 wr_req->r_mtime = ci->vfs_inode.i_mtime;
Yan, Zheng10183a62015-04-27 15:33:28 +08001889 err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false);
1890
1891 if (!err)
1892 err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
1893 if (!err2)
1894 err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
1895
1896 if (err >= 0 || err == -ENOENT)
1897 have |= POOL_READ;
1898 else if (err != -EPERM)
1899 goto out_unlock;
1900
1901 if (err2 == 0 || err2 == -EEXIST)
1902 have |= POOL_WRITE;
1903 else if (err2 != -EPERM) {
1904 err = err2;
1905 goto out_unlock;
1906 }
1907
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001908 pool_ns_len = pool_ns ? pool_ns->len : 0;
1909 perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS);
Yan, Zheng10183a62015-04-27 15:33:28 +08001910 if (!perm) {
1911 err = -ENOMEM;
1912 goto out_unlock;
1913 }
1914
1915 perm->pool = pool;
1916 perm->perm = have;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001917 perm->pool_ns_len = pool_ns_len;
1918 if (pool_ns_len > 0)
1919 memcpy(perm->pool_ns, pool_ns->str, pool_ns_len);
1920 perm->pool_ns[pool_ns_len] = 0;
1921
Yan, Zheng10183a62015-04-27 15:33:28 +08001922 rb_link_node(&perm->node, parent, p);
1923 rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
1924 err = 0;
1925out_unlock:
1926 up_write(&mdsc->pool_perm_rwsem);
1927
Ilya Dryomov3ed97d62016-04-26 15:05:29 +02001928 ceph_osdc_put_request(rd_req);
1929 ceph_osdc_put_request(wr_req);
Yan, Zheng10183a62015-04-27 15:33:28 +08001930out:
1931 if (!err)
1932 err = have;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001933 if (pool_ns)
1934 dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n",
1935 pool, (int)pool_ns->len, pool_ns->str, err);
1936 else
1937 dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err);
Yan, Zheng10183a62015-04-27 15:33:28 +08001938 return err;
1939}
1940
1941int ceph_pool_perm_check(struct ceph_inode_info *ci, int need)
1942{
Yan, Zheng76271512016-02-03 21:24:49 +08001943 s64 pool;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001944 struct ceph_string *pool_ns;
Yan, Zheng10183a62015-04-27 15:33:28 +08001945 int ret, flags;
1946
1947 if (ceph_test_mount_opt(ceph_inode_to_client(&ci->vfs_inode),
1948 NOPOOLPERM))
1949 return 0;
1950
1951 spin_lock(&ci->i_ceph_lock);
1952 flags = ci->i_ceph_flags;
Yan, Zheng76271512016-02-03 21:24:49 +08001953 pool = ci->i_layout.pool_id;
Yan, Zheng10183a62015-04-27 15:33:28 +08001954 spin_unlock(&ci->i_ceph_lock);
1955check:
1956 if (flags & CEPH_I_POOL_PERM) {
1957 if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
Yan, Zheng76271512016-02-03 21:24:49 +08001958 dout("ceph_pool_perm_check pool %lld no read perm\n",
Yan, Zheng10183a62015-04-27 15:33:28 +08001959 pool);
1960 return -EPERM;
1961 }
1962 if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
Yan, Zheng76271512016-02-03 21:24:49 +08001963 dout("ceph_pool_perm_check pool %lld no write perm\n",
Yan, Zheng10183a62015-04-27 15:33:28 +08001964 pool);
1965 return -EPERM;
1966 }
1967 return 0;
1968 }
1969
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001970 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
1971 ret = __ceph_pool_perm_get(ci, pool, pool_ns);
1972 ceph_put_string(pool_ns);
Yan, Zheng10183a62015-04-27 15:33:28 +08001973 if (ret < 0)
1974 return ret;
1975
1976 flags = CEPH_I_POOL_PERM;
1977 if (ret & POOL_READ)
1978 flags |= CEPH_I_POOL_RD;
1979 if (ret & POOL_WRITE)
1980 flags |= CEPH_I_POOL_WR;
1981
1982 spin_lock(&ci->i_ceph_lock);
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001983 if (pool == ci->i_layout.pool_id &&
1984 pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
1985 ci->i_ceph_flags |= flags;
Yan, Zheng10183a62015-04-27 15:33:28 +08001986 } else {
Yan, Zheng76271512016-02-03 21:24:49 +08001987 pool = ci->i_layout.pool_id;
Yan, Zheng10183a62015-04-27 15:33:28 +08001988 flags = ci->i_ceph_flags;
1989 }
1990 spin_unlock(&ci->i_ceph_lock);
1991 goto check;
1992}
1993
1994void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
1995{
1996 struct ceph_pool_perm *perm;
1997 struct rb_node *n;
1998
1999 while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
2000 n = rb_first(&mdsc->pool_perm_tree);
2001 perm = rb_entry(n, struct ceph_pool_perm, node);
2002 rb_erase(n, &mdsc->pool_perm_tree);
2003 kfree(perm);
2004 }
2005}