blob: c1570fada3d8ec312793140526ebff394f069aa4 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Sage Weil1d3576f2009-10-06 11:31:09 -07003
4#include <linux/backing-dev.h>
5#include <linux/fs.h>
6#include <linux/mm.h>
7#include <linux/pagemap.h>
8#include <linux/writeback.h> /* generic_writepages */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Sage Weil1d3576f2009-10-06 11:31:09 -070010#include <linux/pagevec.h>
11#include <linux/task_io_accounting_ops.h>
Ingo Molnarf361bf42017-02-03 23:47:37 +010012#include <linux/signal.h>
Jeff Layton5c308352019-06-06 08:57:27 -040013#include <linux/iversion.h>
Xiubo Li97e27aa2020-03-19 23:45:01 -040014#include <linux/ktime.h>
Jeff Laytonf0702872020-06-01 10:10:21 -040015#include <linux/netfs.h>
Sage Weil1d3576f2009-10-06 11:31:09 -070016
17#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070018#include "mds_client.h"
Milosz Tanski99ccbd22013-08-21 17:29:54 -040019#include "cache.h"
Xiubo Li97e27aa2020-03-19 23:45:01 -040020#include "metric.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070021#include <linux/ceph/osd_client.h>
Ilya Dryomov08c1ac52018-02-17 10:41:20 +010022#include <linux/ceph/striper.h>
Sage Weil1d3576f2009-10-06 11:31:09 -070023
24/*
25 * Ceph address space ops.
26 *
27 * There are a few funny things going on here.
28 *
29 * The page->private field is used to reference a struct
30 * ceph_snap_context for _every_ dirty page. This indicates which
31 * snapshot the page was logically dirtied in, and thus which snap
32 * context needs to be associated with the osd write during writeback.
33 *
34 * Similarly, struct ceph_inode_info maintains a set of counters to
Lucas De Marchi25985ed2011-03-30 22:57:33 -030035 * count dirty pages on the inode. In the absence of snapshots,
Sage Weil1d3576f2009-10-06 11:31:09 -070036 * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count.
37 *
38 * When a snapshot is taken (that is, when the client receives
39 * notification that a snapshot was taken), each inode with caps and
40 * with dirty pages (dirty pages implies there is a cap) gets a new
41 * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending
42 * order, new snaps go to the tail). The i_wrbuffer_ref_head count is
43 * moved to capsnap->dirty. (Unless a sync write is currently in
44 * progress. In that case, the capsnap is said to be "pending", new
45 * writes cannot start, and the capsnap isn't "finalized" until the
46 * write completes (or fails) and a final size/mtime for the inode for
47 * that snap can be settled upon.) i_wrbuffer_ref_head is reset to 0.
48 *
49 * On writeback, we must submit writes to the osd IN SNAP ORDER. So,
50 * we look for the first capsnap in i_cap_snaps and write out pages in
51 * that snap context _only_. Then we move on to the next capsnap,
52 * eventually reaching the "live" or "head" context (i.e., pages that
53 * are not yet snapped) and are writing the most recently dirtied
54 * pages.
55 *
56 * Invalidate and so forth must take care to ensure the dirty page
57 * accounting is preserved.
58 */
59
Yehuda Sadeh2baba252009-12-18 13:51:57 -080060#define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10))
61#define CONGESTION_OFF_THRESH(congestion_kb) \
62 (CONGESTION_ON_THRESH(congestion_kb) - \
63 (CONGESTION_ON_THRESH(congestion_kb) >> 2))
64
Jeff Laytond8013272020-06-05 10:43:21 -040065static int ceph_netfs_check_write_begin(struct file *file, loff_t pos, unsigned int len,
66 struct page *page, void **_fsdata);
67
Yan, Zheng61600ef2012-05-28 14:44:30 +080068static inline struct ceph_snap_context *page_snap_context(struct page *page)
69{
70 if (PagePrivate(page))
71 return (void *)page->private;
72 return NULL;
73}
Sage Weil1d3576f2009-10-06 11:31:09 -070074
75/*
76 * Dirty a page. Optimistically adjust accounting, on the assumption
77 * that we won't race with invalidate. If we do, readjust.
78 */
79static int ceph_set_page_dirty(struct page *page)
80{
81 struct address_space *mapping = page->mapping;
82 struct inode *inode;
83 struct ceph_inode_info *ci;
Sage Weil1d3576f2009-10-06 11:31:09 -070084 struct ceph_snap_context *snapc;
Sha Zhengju7d6e1f52013-08-21 16:27:34 +080085 int ret;
Sage Weil1d3576f2009-10-06 11:31:09 -070086
87 if (unlikely(!mapping))
88 return !TestSetPageDirty(page);
89
Sha Zhengju7d6e1f52013-08-21 16:27:34 +080090 if (PageDirty(page)) {
Sage Weil1d3576f2009-10-06 11:31:09 -070091 dout("%p set_page_dirty %p idx %lu -- already dirty\n",
92 mapping->host, page, page->index);
Sha Zhengju7d6e1f52013-08-21 16:27:34 +080093 BUG_ON(!PagePrivate(page));
Sage Weil1d3576f2009-10-06 11:31:09 -070094 return 0;
95 }
96
97 inode = mapping->host;
98 ci = ceph_inode(inode);
99
Sage Weil1d3576f2009-10-06 11:31:09 -0700100 /* dirty the head */
Sage Weilbe655592011-11-30 09:47:09 -0800101 spin_lock(&ci->i_ceph_lock);
Yan, Zheng5dda377c2015-04-30 14:40:54 +0800102 BUG_ON(ci->i_wr_ref == 0); // caller should hold Fw reference
103 if (__ceph_have_pending_cap_snap(ci)) {
104 struct ceph_cap_snap *capsnap =
105 list_last_entry(&ci->i_cap_snaps,
106 struct ceph_cap_snap,
107 ci_item);
108 snapc = ceph_get_snap_context(capsnap->context);
109 capsnap->dirty_pages++;
110 } else {
111 BUG_ON(!ci->i_head_snapc);
112 snapc = ceph_get_snap_context(ci->i_head_snapc);
113 ++ci->i_wrbuffer_ref_head;
114 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700115 if (ci->i_wrbuffer_ref == 0)
Dave Chinner0444d762011-03-29 18:08:50 +1100116 ihold(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700117 ++ci->i_wrbuffer_ref;
118 dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d "
119 "snapc %p seq %lld (%d snaps)\n",
120 mapping->host, page, page->index,
121 ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1,
122 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
123 snapc, snapc->seq, snapc->num_snaps);
Sage Weilbe655592011-11-30 09:47:09 -0800124 spin_unlock(&ci->i_ceph_lock);
Sage Weil1d3576f2009-10-06 11:31:09 -0700125
Sha Zhengju7d6e1f52013-08-21 16:27:34 +0800126 /*
127 * Reference snap context in page->private. Also set
128 * PagePrivate so that we get invalidatepage callback.
129 */
130 BUG_ON(PagePrivate(page));
Jeff Layton379fc7f2021-03-23 15:16:52 -0400131 attach_page_private(page, snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700132
Sha Zhengju7d6e1f52013-08-21 16:27:34 +0800133 ret = __set_page_dirty_nobuffers(page);
134 WARN_ON(!PageLocked(page));
135 WARN_ON(!page->mapping);
Sage Weil1d3576f2009-10-06 11:31:09 -0700136
Sha Zhengju7d6e1f52013-08-21 16:27:34 +0800137 return ret;
Sage Weil1d3576f2009-10-06 11:31:09 -0700138}
139
140/*
141 * If we are truncating the full page (i.e. offset == 0), adjust the
142 * dirty page counters appropriately. Only called if there is private
143 * data on the page.
144 */
Lukas Czernerd47992f2013-05-21 23:17:23 -0400145static void ceph_invalidatepage(struct page *page, unsigned int offset,
146 unsigned int length)
Sage Weil1d3576f2009-10-06 11:31:09 -0700147{
Alexander Beregalov4ce1e9a2010-02-22 17:17:44 +0300148 struct inode *inode;
Sage Weil1d3576f2009-10-06 11:31:09 -0700149 struct ceph_inode_info *ci;
Jeff Layton379fc7f2021-03-23 15:16:52 -0400150 struct ceph_snap_context *snapc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700151
Jeff Layton7c46b312021-01-21 16:27:14 -0500152 wait_on_page_fscache(page);
153
Alexander Beregalov4ce1e9a2010-02-22 17:17:44 +0300154 inode = page->mapping->host;
Milosz Tanskib150f5c12013-08-09 12:59:55 -0400155 ci = ceph_inode(inode);
156
Jeff Layton8ff2d292021-04-05 10:40:56 -0400157 if (offset != 0 || length != thp_size(page)) {
Milosz Tanskib150f5c12013-08-09 12:59:55 -0400158 dout("%p invalidatepage %p idx %lu partial dirty page %u~%u\n",
159 inode, page, page->index, offset, length);
160 return;
161 }
Alexander Beregalov4ce1e9a2010-02-22 17:17:44 +0300162
Yan, Zhengb072d772017-08-30 11:27:29 +0800163 WARN_ON(!PageLocked(page));
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400164 if (!PagePrivate(page))
165 return;
166
Milosz Tanskib150f5c12013-08-09 12:59:55 -0400167 dout("%p invalidatepage %p idx %lu full dirty page\n",
168 inode, page, page->index);
169
Jeff Layton379fc7f2021-03-23 15:16:52 -0400170 snapc = detach_page_private(page);
Milosz Tanskib150f5c12013-08-09 12:59:55 -0400171 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
172 ceph_put_snap_context(snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700173}
174
Jeff Layton7c46b312021-01-21 16:27:14 -0500175static int ceph_releasepage(struct page *page, gfp_t gfp)
Sage Weil1d3576f2009-10-06 11:31:09 -0700176{
NeilBrowne55f1a12016-08-31 12:59:29 +1000177 dout("%p releasepage %p idx %lu (%sdirty)\n", page->mapping->host,
178 page, page->index, PageDirty(page) ? "" : "not ");
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400179
Jeff Layton7c46b312021-01-21 16:27:14 -0500180 if (PageFsCache(page)) {
181 if (!(gfp & __GFP_DIRECT_RECLAIM) || !(gfp & __GFP_FS))
182 return 0;
183 wait_on_page_fscache(page);
184 }
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400185 return !PagePrivate(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700186}
187
Jeff Laytonf0702872020-06-01 10:10:21 -0400188static void ceph_netfs_expand_readahead(struct netfs_read_request *rreq)
189{
190 struct inode *inode = rreq->mapping->host;
191 struct ceph_inode_info *ci = ceph_inode(inode);
192 struct ceph_file_layout *lo = &ci->i_layout;
193 u32 blockoff;
194 u64 blockno;
195
196 /* Expand the start downward */
197 blockno = div_u64_rem(rreq->start, lo->stripe_unit, &blockoff);
198 rreq->start = blockno * lo->stripe_unit;
199 rreq->len += blockoff;
200
201 /* Now, round up the length to the next block */
202 rreq->len = roundup(rreq->len, lo->stripe_unit);
203}
204
205static bool ceph_netfs_clamp_length(struct netfs_read_subrequest *subreq)
206{
207 struct inode *inode = subreq->rreq->mapping->host;
208 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
209 struct ceph_inode_info *ci = ceph_inode(inode);
210 u64 objno, objoff;
211 u32 xlen;
212
213 /* Truncate the extent at the end of the current block */
214 ceph_calc_file_object_mapping(&ci->i_layout, subreq->start, subreq->len,
215 &objno, &objoff, &xlen);
216 subreq->len = min(xlen, fsc->mount_options->rsize);
217 return true;
218}
219
220static void finish_netfs_read(struct ceph_osd_request *req)
221{
222 struct ceph_fs_client *fsc = ceph_inode_to_client(req->r_inode);
223 struct ceph_osd_data *osd_data = osd_req_op_extent_osd_data(req, 0);
224 struct netfs_read_subrequest *subreq = req->r_priv;
225 int num_pages;
226 int err = req->r_result;
227
Xiubo Li8ae99ae2021-03-22 20:28:49 +0800228 ceph_update_read_metrics(&fsc->mdsc->metric, req->r_start_latency,
Jeff Laytonf0702872020-06-01 10:10:21 -0400229 req->r_end_latency, err);
230
231 dout("%s: result %d subreq->len=%zu i_size=%lld\n", __func__, req->r_result,
232 subreq->len, i_size_read(req->r_inode));
233
234 /* no object means success but no data */
235 if (err == -ENOENT)
236 err = 0;
237 else if (err == -EBLOCKLISTED)
238 fsc->blocklisted = true;
239
240 if (err >= 0 && err < subreq->len)
241 __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
242
243 netfs_subreq_terminated(subreq, err, true);
244
245 num_pages = calc_pages_for(osd_data->alignment, osd_data->length);
246 ceph_put_page_vector(osd_data->pages, num_pages, false);
247 iput(req->r_inode);
248}
249
250static void ceph_netfs_issue_op(struct netfs_read_subrequest *subreq)
251{
252 struct netfs_read_request *rreq = subreq->rreq;
253 struct inode *inode = rreq->mapping->host;
254 struct ceph_inode_info *ci = ceph_inode(inode);
255 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
256 struct ceph_osd_request *req;
257 struct ceph_vino vino = ceph_vino(inode);
258 struct iov_iter iter;
259 struct page **pages;
260 size_t page_off;
261 int err = 0;
262 u64 len = subreq->len;
263
264 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, vino, subreq->start, &len,
265 0, 1, CEPH_OSD_OP_READ,
266 CEPH_OSD_FLAG_READ | fsc->client->osdc.client->options->read_from_replica,
267 NULL, ci->i_truncate_seq, ci->i_truncate_size, false);
268 if (IS_ERR(req)) {
269 err = PTR_ERR(req);
270 req = NULL;
271 goto out;
272 }
273
274 dout("%s: pos=%llu orig_len=%zu len=%llu\n", __func__, subreq->start, subreq->len, len);
275 iov_iter_xarray(&iter, READ, &rreq->mapping->i_pages, subreq->start, len);
276 err = iov_iter_get_pages_alloc(&iter, &pages, len, &page_off);
277 if (err < 0) {
278 dout("%s: iov_ter_get_pages_alloc returned %d\n", __func__, err);
279 goto out;
280 }
281
282 /* should always give us a page-aligned read */
283 WARN_ON_ONCE(page_off);
284 len = err;
285
286 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false);
287 req->r_callback = finish_netfs_read;
288 req->r_priv = subreq;
289 req->r_inode = inode;
290 ihold(inode);
291
292 err = ceph_osdc_start_request(req->r_osdc, req, false);
293 if (err)
294 iput(inode);
295out:
296 ceph_osdc_put_request(req);
297 if (err)
298 netfs_subreq_terminated(subreq, err, false);
299 dout("%s: result %d\n", __func__, err);
300}
301
302static void ceph_init_rreq(struct netfs_read_request *rreq, struct file *file)
303{
304}
305
Jeff Layton49870052020-07-09 14:43:23 -0400306static void ceph_readahead_cleanup(struct address_space *mapping, void *priv)
307{
308 struct inode *inode = mapping->host;
309 struct ceph_inode_info *ci = ceph_inode(inode);
310 int got = (uintptr_t)priv;
311
312 if (got)
313 ceph_put_cap_refs(ci, got);
314}
315
Jeff Laytonf0702872020-06-01 10:10:21 -0400316const struct netfs_read_request_ops ceph_netfs_read_ops = {
317 .init_rreq = ceph_init_rreq,
318 .is_cache_enabled = ceph_is_cache_enabled,
319 .begin_cache_operation = ceph_begin_cache_operation,
320 .issue_op = ceph_netfs_issue_op,
321 .expand_readahead = ceph_netfs_expand_readahead,
322 .clamp_length = ceph_netfs_clamp_length,
Jeff Laytond8013272020-06-05 10:43:21 -0400323 .check_write_begin = ceph_netfs_check_write_begin,
Jeff Layton49870052020-07-09 14:43:23 -0400324 .cleanup = ceph_readahead_cleanup,
Jeff Laytonf0702872020-06-01 10:10:21 -0400325};
326
327/* read a single page, without unlocking it. */
328static int ceph_readpage(struct file *file, struct page *page)
329{
330 struct inode *inode = file_inode(file);
331 struct ceph_inode_info *ci = ceph_inode(inode);
332 struct ceph_vino vino = ceph_vino(inode);
333 u64 off = page_offset(page);
Jeff Layton8ff2d292021-04-05 10:40:56 -0400334 u64 len = thp_size(page);
Jeff Laytonf0702872020-06-01 10:10:21 -0400335
336 if (ci->i_inline_version != CEPH_INLINE_NONE) {
337 /*
338 * Uptodate inline data should have been added
339 * into page cache while getting Fcr caps.
340 */
341 if (off == 0) {
342 unlock_page(page);
343 return -EINVAL;
344 }
Jeff Layton8ff2d292021-04-05 10:40:56 -0400345 zero_user_segment(page, 0, thp_size(page));
Jeff Laytonf0702872020-06-01 10:10:21 -0400346 SetPageUptodate(page);
347 unlock_page(page);
348 return 0;
349 }
350
351 dout("readpage ino %llx.%llx file %p off %llu len %llu page %p index %lu\n",
352 vino.ino, vino.snap, file, off, len, page, page->index);
353
354 return netfs_readpage(file, page, &ceph_netfs_read_ops, NULL);
355}
356
Jeff Layton49870052020-07-09 14:43:23 -0400357static void ceph_readahead(struct readahead_control *ractl)
Sage Weil1d3576f2009-10-06 11:31:09 -0700358{
Jeff Layton49870052020-07-09 14:43:23 -0400359 struct inode *inode = file_inode(ractl->file);
360 struct ceph_file_info *fi = ractl->file->private_data;
361 struct ceph_rw_context *rw_ctx;
Yan, Zheng2b1ac852016-10-25 10:51:55 +0800362 int got = 0;
363 int ret = 0;
364
Yan, Zheng83701242014-11-14 22:36:18 +0800365 if (ceph_inode(inode)->i_inline_version != CEPH_INLINE_NONE)
Jeff Layton49870052020-07-09 14:43:23 -0400366 return;
Yan, Zheng83701242014-11-14 22:36:18 +0800367
Chengguang Xu73737682018-02-28 19:43:47 +0800368 rw_ctx = ceph_find_rw_context(fi);
Jeff Layton49870052020-07-09 14:43:23 -0400369 if (!rw_ctx) {
370 /*
371 * readahead callers do not necessarily hold Fcb caps
372 * (e.g. fadvise, madvise).
373 */
374 int want = CEPH_CAP_FILE_CACHE;
375
376 ret = ceph_try_get_caps(inode, CEPH_CAP_FILE_RD, want, true, &got);
377 if (ret < 0)
378 dout("start_read %p, error getting cap\n", inode);
379 else if (!(got & want))
380 dout("start_read %p, no cache cap\n", inode);
381
382 if (ret <= 0)
383 return;
Sage Weil1d3576f2009-10-06 11:31:09 -0700384 }
Jeff Layton49870052020-07-09 14:43:23 -0400385 netfs_readahead(ractl, &ceph_netfs_read_ops, (void *)(uintptr_t)got);
Sage Weil1d3576f2009-10-06 11:31:09 -0700386}
387
Yan, Zheng1f934b02017-08-30 11:36:06 +0800388struct ceph_writeback_ctl
389{
390 loff_t i_size;
391 u64 truncate_size;
392 u32 truncate_seq;
393 bool size_stable;
Yan, Zheng2a2d9272017-09-01 16:53:58 +0800394 bool head_snapc;
Yan, Zheng1f934b02017-08-30 11:36:06 +0800395};
396
Sage Weil1d3576f2009-10-06 11:31:09 -0700397/*
398 * Get ref for the oldest snapc for an inode with dirty data... that is, the
399 * only snap context we are allowed to write back.
Sage Weil1d3576f2009-10-06 11:31:09 -0700400 */
Yan, Zheng1f934b02017-08-30 11:36:06 +0800401static struct ceph_snap_context *
Yan, Zheng05455e12017-09-02 10:50:48 +0800402get_oldest_context(struct inode *inode, struct ceph_writeback_ctl *ctl,
403 struct ceph_snap_context *page_snapc)
Sage Weil1d3576f2009-10-06 11:31:09 -0700404{
405 struct ceph_inode_info *ci = ceph_inode(inode);
406 struct ceph_snap_context *snapc = NULL;
407 struct ceph_cap_snap *capsnap = NULL;
408
Sage Weilbe655592011-11-30 09:47:09 -0800409 spin_lock(&ci->i_ceph_lock);
Sage Weil1d3576f2009-10-06 11:31:09 -0700410 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
411 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
412 capsnap->context, capsnap->dirty_pages);
Yan, Zheng05455e12017-09-02 10:50:48 +0800413 if (!capsnap->dirty_pages)
414 continue;
415
416 /* get i_size, truncate_{seq,size} for page_snapc? */
417 if (snapc && capsnap->context != page_snapc)
418 continue;
419
420 if (ctl) {
421 if (capsnap->writing) {
422 ctl->i_size = i_size_read(inode);
423 ctl->size_stable = false;
424 } else {
425 ctl->i_size = capsnap->size;
426 ctl->size_stable = true;
Yan, Zheng1f934b02017-08-30 11:36:06 +0800427 }
Yan, Zheng05455e12017-09-02 10:50:48 +0800428 ctl->truncate_size = capsnap->truncate_size;
429 ctl->truncate_seq = capsnap->truncate_seq;
Yan, Zheng2a2d9272017-09-01 16:53:58 +0800430 ctl->head_snapc = false;
Sage Weil1d3576f2009-10-06 11:31:09 -0700431 }
Yan, Zheng05455e12017-09-02 10:50:48 +0800432
433 if (snapc)
434 break;
435
436 snapc = ceph_get_snap_context(capsnap->context);
437 if (!page_snapc ||
438 page_snapc == snapc ||
439 page_snapc->seq > snapc->seq)
440 break;
Sage Weil1d3576f2009-10-06 11:31:09 -0700441 }
Sage Weil7d8cb262010-08-24 08:44:16 -0700442 if (!snapc && ci->i_wrbuffer_ref_head) {
Sage Weil80e755f2010-03-31 21:52:10 -0700443 snapc = ceph_get_snap_context(ci->i_head_snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700444 dout(" head snapc %p has %d dirty pages\n",
445 snapc, ci->i_wrbuffer_ref_head);
Yan, Zheng1f934b02017-08-30 11:36:06 +0800446 if (ctl) {
447 ctl->i_size = i_size_read(inode);
448 ctl->truncate_size = ci->i_truncate_size;
449 ctl->truncate_seq = ci->i_truncate_seq;
450 ctl->size_stable = false;
Yan, Zheng2a2d9272017-09-01 16:53:58 +0800451 ctl->head_snapc = true;
Yan, Zheng1f934b02017-08-30 11:36:06 +0800452 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700453 }
Sage Weilbe655592011-11-30 09:47:09 -0800454 spin_unlock(&ci->i_ceph_lock);
Sage Weil1d3576f2009-10-06 11:31:09 -0700455 return snapc;
456}
457
Yan, Zheng1f934b02017-08-30 11:36:06 +0800458static u64 get_writepages_data_length(struct inode *inode,
459 struct page *page, u64 start)
460{
461 struct ceph_inode_info *ci = ceph_inode(inode);
462 struct ceph_snap_context *snapc = page_snap_context(page);
463 struct ceph_cap_snap *capsnap = NULL;
464 u64 end = i_size_read(inode);
465
466 if (snapc != ci->i_head_snapc) {
467 bool found = false;
468 spin_lock(&ci->i_ceph_lock);
469 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
470 if (capsnap->context == snapc) {
471 if (!capsnap->writing)
472 end = capsnap->size;
473 found = true;
474 break;
475 }
476 }
477 spin_unlock(&ci->i_ceph_lock);
478 WARN_ON(!found);
479 }
Jeff Layton8ff2d292021-04-05 10:40:56 -0400480 if (end > page_offset(page) + thp_size(page))
481 end = page_offset(page) + thp_size(page);
Yan, Zheng1f934b02017-08-30 11:36:06 +0800482 return end > start ? end - start : 0;
483}
484
Sage Weil1d3576f2009-10-06 11:31:09 -0700485/*
486 * Write a single page, but leave the page locked.
487 *
Jeff Laytonb72b13e2019-07-02 12:35:52 -0400488 * If we get a write error, mark the mapping for error, but still adjust the
Sage Weil1d3576f2009-10-06 11:31:09 -0700489 * dirty page accounting (i.e., page is no longer dirty).
490 */
491static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
492{
Jeff Layton63909872020-07-14 14:37:15 -0400493 struct inode *inode = page->mapping->host;
494 struct ceph_inode_info *ci = ceph_inode(inode);
495 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
Sage Weil6298a332010-03-31 22:01:38 -0700496 struct ceph_snap_context *snapc, *oldest;
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800497 loff_t page_off = page_offset(page);
Jeff Layton63909872020-07-14 14:37:15 -0400498 int err;
Jeff Layton8ff2d292021-04-05 10:40:56 -0400499 loff_t len = thp_size(page);
Yan, Zheng1f934b02017-08-30 11:36:06 +0800500 struct ceph_writeback_ctl ceph_wbc;
Jeff Layton63909872020-07-14 14:37:15 -0400501 struct ceph_osd_client *osdc = &fsc->client->osdc;
502 struct ceph_osd_request *req;
Sage Weil1d3576f2009-10-06 11:31:09 -0700503
504 dout("writepage %p idx %lu\n", page, page->index);
505
Sage Weil1d3576f2009-10-06 11:31:09 -0700506 /* verify this is a writeable snap context */
Yan, Zheng61600ef2012-05-28 14:44:30 +0800507 snapc = page_snap_context(page);
Markus Elfringd37b1d92017-08-20 20:22:02 +0200508 if (!snapc) {
Sage Weil1d3576f2009-10-06 11:31:09 -0700509 dout("writepage %p page %p not dirty?\n", inode, page);
Yan, Zheng43986882017-05-23 17:48:28 +0800510 return 0;
Sage Weil1d3576f2009-10-06 11:31:09 -0700511 }
Yan, Zheng05455e12017-09-02 10:50:48 +0800512 oldest = get_oldest_context(inode, &ceph_wbc, snapc);
Sage Weil6298a332010-03-31 22:01:38 -0700513 if (snapc->seq > oldest->seq) {
Sage Weil1d3576f2009-10-06 11:31:09 -0700514 dout("writepage %p page %p snapc %p not writeable - noop\n",
Yan, Zheng61600ef2012-05-28 14:44:30 +0800515 inode, page, snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700516 /* we should only noop if called by kswapd */
Yan, Zhengfa71fef2017-05-23 17:18:53 +0800517 WARN_ON(!(current->flags & PF_MEMALLOC));
Sage Weil6298a332010-03-31 22:01:38 -0700518 ceph_put_snap_context(oldest);
Yan, Zhengfa71fef2017-05-23 17:18:53 +0800519 redirty_page_for_writepage(wbc, page);
Yan, Zheng43986882017-05-23 17:48:28 +0800520 return 0;
Sage Weil1d3576f2009-10-06 11:31:09 -0700521 }
Sage Weil6298a332010-03-31 22:01:38 -0700522 ceph_put_snap_context(oldest);
Sage Weil1d3576f2009-10-06 11:31:09 -0700523
524 /* is this a partial page at end of file? */
Yan, Zheng1f934b02017-08-30 11:36:06 +0800525 if (page_off >= ceph_wbc.i_size) {
526 dout("%p page eof %llu\n", page, ceph_wbc.i_size);
Jeff Layton8ff2d292021-04-05 10:40:56 -0400527 page->mapping->a_ops->invalidatepage(page, 0, thp_size(page));
Yan, Zheng43986882017-05-23 17:48:28 +0800528 return 0;
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800529 }
Yan, Zheng43986882017-05-23 17:48:28 +0800530
Yan, Zheng1f934b02017-08-30 11:36:06 +0800531 if (ceph_wbc.i_size < page_off + len)
532 len = ceph_wbc.i_size - page_off;
Sage Weil1d3576f2009-10-06 11:31:09 -0700533
Jeff Layton63909872020-07-14 14:37:15 -0400534 dout("writepage %p page %p index %lu on %llu~%llu snapc %p seq %lld\n",
Yan, Zheng1c0a9c22017-08-16 17:24:58 +0800535 inode, page, page->index, page_off, len, snapc, snapc->seq);
Sage Weil1d3576f2009-10-06 11:31:09 -0700536
Yan, Zheng314c4732017-12-15 16:57:40 +0800537 if (atomic_long_inc_return(&fsc->writeback_count) >
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700538 CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
Jan Kara09dc9fc2017-04-12 12:24:33 +0200539 set_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800540
Sage Weil1d3576f2009-10-06 11:31:09 -0700541 set_page_writeback(page);
Jeff Layton63909872020-07-14 14:37:15 -0400542 req = ceph_osdc_new_request(osdc, &ci->i_layout, ceph_vino(inode), page_off, &len, 0, 1,
543 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE, snapc,
544 ceph_wbc.truncate_seq, ceph_wbc.truncate_size,
545 true);
546 if (IS_ERR(req)) {
547 redirty_page_for_writepage(wbc, page);
548 end_page_writeback(page);
549 return PTR_ERR(req);
550 }
551
552 /* it may be a short write due to an object boundary */
Jeff Layton8ff2d292021-04-05 10:40:56 -0400553 WARN_ON_ONCE(len > thp_size(page));
Jeff Layton63909872020-07-14 14:37:15 -0400554 osd_req_op_extent_osd_data_pages(req, 0, &page, len, 0, false, false);
555 dout("writepage %llu~%llu (%llu bytes)\n", page_off, len, len);
556
557 req->r_mtime = inode->i_mtime;
558 err = ceph_osdc_start_request(osdc, req, true);
559 if (!err)
560 err = ceph_osdc_wait_request(osdc, req);
561
Xiubo Li8ae99ae2021-03-22 20:28:49 +0800562 ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
Jeff Layton63909872020-07-14 14:37:15 -0400563 req->r_end_latency, err);
564
565 ceph_osdc_put_request(req);
566 if (err == 0)
567 err = len;
568
Sage Weil1d3576f2009-10-06 11:31:09 -0700569 if (err < 0) {
Yan, Zhengad15ec02016-05-13 17:29:51 +0800570 struct writeback_control tmp_wbc;
571 if (!wbc)
572 wbc = &tmp_wbc;
573 if (err == -ERESTARTSYS) {
574 /* killed by SIGKILL */
575 dout("writepage interrupted page %p\n", page);
576 redirty_page_for_writepage(wbc, page);
577 end_page_writeback(page);
Yan, Zheng43986882017-05-23 17:48:28 +0800578 return err;
Yan, Zhengad15ec02016-05-13 17:29:51 +0800579 }
Ilya Dryomov0b98acd2020-09-14 13:39:19 +0200580 if (err == -EBLOCKLISTED)
581 fsc->blocklisted = true;
Yan, Zhengad15ec02016-05-13 17:29:51 +0800582 dout("writepage setting page/mapping error %d %p\n",
583 err, page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700584 mapping_set_error(&inode->i_data, err);
Yan, Zhengad15ec02016-05-13 17:29:51 +0800585 wbc->pages_skipped++;
Sage Weil1d3576f2009-10-06 11:31:09 -0700586 } else {
587 dout("writepage cleaned page %p\n", page);
588 err = 0; /* vfs expects us to return 0 */
589 }
Jeff Layton379fc7f2021-03-23 15:16:52 -0400590 oldest = detach_page_private(page);
591 WARN_ON_ONCE(oldest != snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700592 end_page_writeback(page);
593 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
Sage Weil6298a332010-03-31 22:01:38 -0700594 ceph_put_snap_context(snapc); /* page's reference */
Yan, Zheng314c4732017-12-15 16:57:40 +0800595
596 if (atomic_long_dec_return(&fsc->writeback_count) <
597 CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
598 clear_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
599
Sage Weil1d3576f2009-10-06 11:31:09 -0700600 return err;
601}
602
603static int ceph_writepage(struct page *page, struct writeback_control *wbc)
604{
Yehuda Sadehdbd646a2009-12-16 14:51:06 -0800605 int err;
606 struct inode *inode = page->mapping->host;
607 BUG_ON(!inode);
Sage Weil70b666c2011-05-27 09:24:26 -0700608 ihold(inode);
Yehuda Sadehdbd646a2009-12-16 14:51:06 -0800609 err = writepage_nounlock(page, wbc);
Yan, Zhengad15ec02016-05-13 17:29:51 +0800610 if (err == -ERESTARTSYS) {
611 /* direct memory reclaimer was killed by SIGKILL. return 0
612 * to prevent caller from setting mapping/page error */
613 err = 0;
614 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700615 unlock_page(page);
Yehuda Sadehdbd646a2009-12-16 14:51:06 -0800616 iput(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700617 return err;
618}
619
Sage Weil1d3576f2009-10-06 11:31:09 -0700620/*
Sage Weil1d3576f2009-10-06 11:31:09 -0700621 * async writeback completion handler.
622 *
623 * If we get an error, set the mapping error bit, but not the individual
624 * page error bits.
625 */
Ilya Dryomov85e084f2016-04-28 16:07:24 +0200626static void writepages_finish(struct ceph_osd_request *req)
Sage Weil1d3576f2009-10-06 11:31:09 -0700627{
628 struct inode *inode = req->r_inode;
Sage Weil1d3576f2009-10-06 11:31:09 -0700629 struct ceph_inode_info *ci = ceph_inode(inode);
Alex Elder87060c12013-04-03 01:28:58 -0500630 struct ceph_osd_data *osd_data;
Sage Weil1d3576f2009-10-06 11:31:09 -0700631 struct page *page;
Yan, Zheng5b646402016-01-07 16:00:17 +0800632 int num_pages, total_pages = 0;
633 int i, j;
634 int rc = req->r_result;
Sage Weil1d3576f2009-10-06 11:31:09 -0700635 struct ceph_snap_context *snapc = req->r_snapc;
636 struct address_space *mapping = inode->i_mapping;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700637 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
Yan, Zheng5b646402016-01-07 16:00:17 +0800638 bool remove_page;
Sage Weil1d3576f2009-10-06 11:31:09 -0700639
Yan, Zheng5b646402016-01-07 16:00:17 +0800640 dout("writepages_finish %p rc %d\n", inode, rc);
Jeff Layton26544c622017-04-04 08:39:46 -0400641 if (rc < 0) {
Sage Weil1d3576f2009-10-06 11:31:09 -0700642 mapping_set_error(mapping, rc);
Jeff Layton26544c622017-04-04 08:39:46 -0400643 ceph_set_error_write(ci);
Ilya Dryomov0b98acd2020-09-14 13:39:19 +0200644 if (rc == -EBLOCKLISTED)
645 fsc->blocklisted = true;
Jeff Layton26544c622017-04-04 08:39:46 -0400646 } else {
647 ceph_clear_error_write(ci);
648 }
Yan, Zheng5b646402016-01-07 16:00:17 +0800649
Xiubo Li8ae99ae2021-03-22 20:28:49 +0800650 ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
Xiubo Li97e27aa2020-03-19 23:45:01 -0400651 req->r_end_latency, rc);
652
Yan, Zheng5b646402016-01-07 16:00:17 +0800653 /*
654 * We lost the cache cap, need to truncate the page before
655 * it is unlocked, otherwise we'd truncate it later in the
656 * page truncation thread, possibly losing some data that
657 * raced its way in
658 */
659 remove_page = !(ceph_caps_issued(ci) &
660 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
Sage Weil1d3576f2009-10-06 11:31:09 -0700661
662 /* clean all pages */
Yan, Zheng5b646402016-01-07 16:00:17 +0800663 for (i = 0; i < req->r_num_ops; i++) {
664 if (req->r_ops[i].op != CEPH_OSD_OP_WRITE)
665 break;
Sage Weil1d3576f2009-10-06 11:31:09 -0700666
Yan, Zheng5b646402016-01-07 16:00:17 +0800667 osd_data = osd_req_op_extent_osd_data(req, i);
668 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
669 num_pages = calc_pages_for((u64)osd_data->alignment,
670 (u64)osd_data->length);
671 total_pages += num_pages;
672 for (j = 0; j < num_pages; j++) {
673 page = osd_data->pages[j];
674 BUG_ON(!page);
675 WARN_ON(!PageUptodate(page));
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800676
Yan, Zheng5b646402016-01-07 16:00:17 +0800677 if (atomic_long_dec_return(&fsc->writeback_count) <
678 CONGESTION_OFF_THRESH(
679 fsc->mount_options->congestion_kb))
Jan Kara09dc9fc2017-04-12 12:24:33 +0200680 clear_bdi_congested(inode_to_bdi(inode),
Yan, Zheng5b646402016-01-07 16:00:17 +0800681 BLK_RW_ASYNC);
Yehuda Sadehe63dc5c2010-02-19 00:07:01 +0000682
Jeff Layton379fc7f2021-03-23 15:16:52 -0400683 ceph_put_snap_context(detach_page_private(page));
Yan, Zheng5b646402016-01-07 16:00:17 +0800684 end_page_writeback(page);
Jeff Layton379fc7f2021-03-23 15:16:52 -0400685 dout("unlocking %p\n", page);
Yehuda Sadehe63dc5c2010-02-19 00:07:01 +0000686
Yan, Zheng5b646402016-01-07 16:00:17 +0800687 if (remove_page)
688 generic_error_remove_page(inode->i_mapping,
689 page);
690
691 unlock_page(page);
692 }
693 dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n",
694 inode, osd_data->length, rc >= 0 ? num_pages : 0);
695
John Hubbard96ac9152019-08-08 20:56:47 -0700696 release_pages(osd_data->pages, num_pages);
Sage Weil1d3576f2009-10-06 11:31:09 -0700697 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700698
Yan, Zheng5b646402016-01-07 16:00:17 +0800699 ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
700
701 osd_data = osd_req_op_extent_osd_data(req, 0);
Alex Elder87060c12013-04-03 01:28:58 -0500702 if (osd_data->pages_from_pool)
Jeff Laytona0102bd2020-07-30 11:03:55 -0400703 mempool_free(osd_data->pages, ceph_wb_pagevec_pool);
Sage Weil1d3576f2009-10-06 11:31:09 -0700704 else
Alex Elder87060c12013-04-03 01:28:58 -0500705 kfree(osd_data->pages);
Sage Weil1d3576f2009-10-06 11:31:09 -0700706 ceph_osdc_put_request(req);
707}
708
Sage Weil1d3576f2009-10-06 11:31:09 -0700709/*
710 * initiate async writeback
711 */
712static int ceph_writepages_start(struct address_space *mapping,
713 struct writeback_control *wbc)
714{
715 struct inode *inode = mapping->host;
Sage Weil1d3576f2009-10-06 11:31:09 -0700716 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800717 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
718 struct ceph_vino vino = ceph_vino(inode);
Yan, Zheng2a2d9272017-09-01 16:53:58 +0800719 pgoff_t index, start_index, end = -1;
Sage Weil80e755f2010-03-31 21:52:10 -0700720 struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
Sage Weil1d3576f2009-10-06 11:31:09 -0700721 struct pagevec pvec;
Sage Weil1d3576f2009-10-06 11:31:09 -0700722 int rc = 0;
Fabian Frederick93407472017-02-27 14:28:32 -0800723 unsigned int wsize = i_blocksize(inode);
Sage Weil1d3576f2009-10-06 11:31:09 -0700724 struct ceph_osd_request *req = NULL;
Yan, Zheng1f934b02017-08-30 11:36:06 +0800725 struct ceph_writeback_ctl ceph_wbc;
Yan, Zheng590e9d92017-09-03 00:04:31 +0800726 bool should_loop, range_whole = false;
Yan, Zhengaf9cc402018-03-04 16:36:01 +0800727 bool done = false;
Sage Weil1d3576f2009-10-06 11:31:09 -0700728
Yanhu Cao3fb99d42017-07-21 17:20:10 +0800729 dout("writepages_start %p (mode=%s)\n", inode,
Sage Weil1d3576f2009-10-06 11:31:09 -0700730 wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
731 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
732
Jeff Layton50c91322020-09-25 07:55:39 -0400733 if (READ_ONCE(fsc->mount_state) >= CEPH_MOUNT_SHUTDOWN) {
Yan, Zheng6c93df52016-04-15 13:56:12 +0800734 if (ci->i_wrbuffer_ref > 0) {
735 pr_warn_ratelimited(
736 "writepage_start %p %lld forced umount\n",
737 inode, ceph_ino(inode));
738 }
Yan, Zhenga341d4d2015-07-01 17:03:23 +0800739 mapping_set_error(mapping, -EIO);
Sage Weil1d3576f2009-10-06 11:31:09 -0700740 return -EIO; /* we're in a forced umount, don't write! */
741 }
Yan, Zheng95cca2b2017-07-11 17:34:46 +0800742 if (fsc->mount_options->wsize < wsize)
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700743 wsize = fsc->mount_options->wsize;
Sage Weil1d3576f2009-10-06 11:31:09 -0700744
Mel Gorman86679822017-11-15 17:37:52 -0800745 pagevec_init(&pvec);
Sage Weil1d3576f2009-10-06 11:31:09 -0700746
Yan, Zheng590e9d92017-09-03 00:04:31 +0800747 start_index = wbc->range_cyclic ? mapping->writeback_index : 0;
Yan, Zheng2a2d9272017-09-01 16:53:58 +0800748 index = start_index;
Sage Weil1d3576f2009-10-06 11:31:09 -0700749
750retry:
751 /* find oldest snap context with dirty data */
Yan, Zheng05455e12017-09-02 10:50:48 +0800752 snapc = get_oldest_context(inode, &ceph_wbc, NULL);
Sage Weil1d3576f2009-10-06 11:31:09 -0700753 if (!snapc) {
754 /* hmm, why does writepages get called when there
755 is no dirty data? */
756 dout(" no snap context with dirty data?\n");
757 goto out;
758 }
759 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
760 snapc, snapc->seq, snapc->num_snaps);
Yan, Zhengfc2744a2013-05-31 16:48:29 +0800761
Yan, Zheng2a2d9272017-09-01 16:53:58 +0800762 should_loop = false;
763 if (ceph_wbc.head_snapc && snapc != last_snapc) {
764 /* where to start/end? */
765 if (wbc->range_cyclic) {
766 index = start_index;
767 end = -1;
768 if (index > 0)
769 should_loop = true;
770 dout(" cyclic, start at %lu\n", index);
771 } else {
772 index = wbc->range_start >> PAGE_SHIFT;
773 end = wbc->range_end >> PAGE_SHIFT;
774 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
775 range_whole = true;
776 dout(" not cyclic, %lu to %lu\n", index, end);
777 }
778 } else if (!ceph_wbc.head_snapc) {
779 /* Do not respect wbc->range_{start,end}. Dirty pages
780 * in that range can be associated with newer snapc.
781 * They are not writeable until we write all dirty pages
782 * associated with 'snapc' get written */
Yan, Zheng1582af22018-03-06 15:14:54 +0800783 if (index > 0)
Yan, Zheng2a2d9272017-09-01 16:53:58 +0800784 should_loop = true;
785 dout(" non-head snapc, range whole\n");
Sage Weil1d3576f2009-10-06 11:31:09 -0700786 }
Yan, Zheng2a2d9272017-09-01 16:53:58 +0800787
788 ceph_put_snap_context(last_snapc);
Sage Weil1d3576f2009-10-06 11:31:09 -0700789 last_snapc = snapc;
790
Yan, Zhengaf9cc402018-03-04 16:36:01 +0800791 while (!done && index <= end) {
Yan, Zheng5b646402016-01-07 16:00:17 +0800792 int num_ops = 0, op_idx;
Yan, Zheng0e5ecac2017-08-31 19:20:40 +0800793 unsigned i, pvec_pages, max_pages, locked_pages = 0;
Yan, Zheng5b646402016-01-07 16:00:17 +0800794 struct page **pages = NULL, **data_pages;
Sage Weil1d3576f2009-10-06 11:31:09 -0700795 struct page *page;
Yan, Zheng0e5ecac2017-08-31 19:20:40 +0800796 pgoff_t strip_unit_end = 0;
Yan, Zheng5b646402016-01-07 16:00:17 +0800797 u64 offset = 0, len = 0;
Jeff Laytona0102bd2020-07-30 11:03:55 -0400798 bool from_pool = false;
Sage Weil1d3576f2009-10-06 11:31:09 -0700799
Yan, Zheng0e5ecac2017-08-31 19:20:40 +0800800 max_pages = wsize >> PAGE_SHIFT;
Sage Weil1d3576f2009-10-06 11:31:09 -0700801
802get_more_pages:
Jeff Layton2e169292020-09-14 13:30:36 -0400803 pvec_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
804 end, PAGECACHE_TAG_DIRTY);
Jan Kara0ed75fc2017-11-15 17:34:41 -0800805 dout("pagevec_lookup_range_tag got %d\n", pvec_pages);
Sage Weil1d3576f2009-10-06 11:31:09 -0700806 if (!pvec_pages && !locked_pages)
807 break;
808 for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
809 page = pvec.pages[i];
810 dout("? %p idx %lu\n", page, page->index);
811 if (locked_pages == 0)
812 lock_page(page); /* first page */
813 else if (!trylock_page(page))
814 break;
815
816 /* only dirty pages, or our accounting breaks */
817 if (unlikely(!PageDirty(page)) ||
818 unlikely(page->mapping != mapping)) {
819 dout("!dirty or !mapping %p\n", page);
820 unlock_page(page);
Yan, Zheng0713e5f2017-08-31 16:55:48 +0800821 continue;
Sage Weil1d3576f2009-10-06 11:31:09 -0700822 }
Yan, Zhengaf9cc402018-03-04 16:36:01 +0800823 /* only if matching snap context */
824 pgsnapc = page_snap_context(page);
825 if (pgsnapc != snapc) {
826 dout("page snapc %p %lld != oldest %p %lld\n",
827 pgsnapc, pgsnapc->seq, snapc, snapc->seq);
Yan, Zheng1582af22018-03-06 15:14:54 +0800828 if (!should_loop &&
829 !ceph_wbc.head_snapc &&
830 wbc->sync_mode != WB_SYNC_NONE)
831 should_loop = true;
Sage Weil1d3576f2009-10-06 11:31:09 -0700832 unlock_page(page);
Yan, Zhengaf9cc402018-03-04 16:36:01 +0800833 continue;
Sage Weil1d3576f2009-10-06 11:31:09 -0700834 }
Yan, Zheng1f934b02017-08-30 11:36:06 +0800835 if (page_offset(page) >= ceph_wbc.i_size) {
836 dout("%p page eof %llu\n",
837 page, ceph_wbc.i_size);
Erqi Chenc95f1c52019-07-24 10:26:09 +0800838 if ((ceph_wbc.size_stable ||
839 page_offset(page) >= i_size_read(inode)) &&
840 clear_page_dirty_for_io(page))
Yan, Zhengaf9cc402018-03-04 16:36:01 +0800841 mapping->a_ops->invalidatepage(page,
Jeff Layton8ff2d292021-04-05 10:40:56 -0400842 0, thp_size(page));
Yan, Zhengaf9cc402018-03-04 16:36:01 +0800843 unlock_page(page);
844 continue;
845 }
846 if (strip_unit_end && (page->index > strip_unit_end)) {
847 dout("end of strip unit %p\n", page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700848 unlock_page(page);
849 break;
850 }
851 if (PageWriteback(page)) {
Yan, Zheng0713e5f2017-08-31 16:55:48 +0800852 if (wbc->sync_mode == WB_SYNC_NONE) {
853 dout("%p under writeback\n", page);
854 unlock_page(page);
855 continue;
856 }
857 dout("waiting on writeback %p\n", page);
858 wait_on_page_writeback(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700859 }
860
Sage Weil1d3576f2009-10-06 11:31:09 -0700861 if (!clear_page_dirty_for_io(page)) {
862 dout("%p !clear_page_dirty_for_io\n", page);
863 unlock_page(page);
Yan, Zheng0713e5f2017-08-31 16:55:48 +0800864 continue;
Sage Weil1d3576f2009-10-06 11:31:09 -0700865 }
866
Alex Eldere5975c72013-03-14 14:09:05 -0500867 /*
868 * We have something to write. If this is
869 * the first locked page this time through,
Yan, Zheng5b646402016-01-07 16:00:17 +0800870 * calculate max possinle write size and
871 * allocate a page array
Alex Eldere5975c72013-03-14 14:09:05 -0500872 */
Sage Weil1d3576f2009-10-06 11:31:09 -0700873 if (locked_pages == 0) {
Yan, Zheng5b646402016-01-07 16:00:17 +0800874 u64 objnum;
875 u64 objoff;
Ilya Dryomovdccbf082018-02-17 09:29:58 +0100876 u32 xlen;
Yan, Zheng5b646402016-01-07 16:00:17 +0800877
Sage Weil1d3576f2009-10-06 11:31:09 -0700878 /* prepare async write request */
Alex Eldere5975c72013-03-14 14:09:05 -0500879 offset = (u64)page_offset(page);
Ilya Dryomovdccbf082018-02-17 09:29:58 +0100880 ceph_calc_file_object_mapping(&ci->i_layout,
881 offset, wsize,
882 &objnum, &objoff,
883 &xlen);
884 len = xlen;
Henry C Chang8c718972011-05-03 09:45:16 +0000885
Yanhu Cao3fb99d42017-07-21 17:20:10 +0800886 num_ops = 1;
Yan, Zheng5b646402016-01-07 16:00:17 +0800887 strip_unit_end = page->index +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300888 ((len - 1) >> PAGE_SHIFT);
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800889
Yan, Zheng5b646402016-01-07 16:00:17 +0800890 BUG_ON(pages);
Alex Elder88486952013-03-14 14:09:05 -0500891 max_pages = calc_pages_for(0, (u64)len);
Kees Cook6da2ec52018-06-12 13:55:00 -0700892 pages = kmalloc_array(max_pages,
893 sizeof(*pages),
894 GFP_NOFS);
Alex Elder88486952013-03-14 14:09:05 -0500895 if (!pages) {
Jeff Laytona0102bd2020-07-30 11:03:55 -0400896 from_pool = true;
897 pages = mempool_alloc(ceph_wb_pagevec_pool, GFP_NOFS);
Alex Eldere5975c72013-03-14 14:09:05 -0500898 BUG_ON(!pages);
Alex Elder88486952013-03-14 14:09:05 -0500899 }
Yan, Zheng5b646402016-01-07 16:00:17 +0800900
901 len = 0;
902 } else if (page->index !=
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300903 (offset + len) >> PAGE_SHIFT) {
Jeff Laytona0102bd2020-07-30 11:03:55 -0400904 if (num_ops >= (from_pool ? CEPH_OSD_SLAB_OPS :
905 CEPH_OSD_MAX_OPS)) {
Yan, Zheng5b646402016-01-07 16:00:17 +0800906 redirty_page_for_writepage(wbc, page);
907 unlock_page(page);
908 break;
909 }
910
911 num_ops++;
912 offset = (u64)page_offset(page);
913 len = 0;
Sage Weil1d3576f2009-10-06 11:31:09 -0700914 }
915
916 /* note position of first page in pvec */
Sage Weil1d3576f2009-10-06 11:31:09 -0700917 dout("%p will write page %p idx %lu\n",
918 inode, page, page->index);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800919
Yan, Zheng5b646402016-01-07 16:00:17 +0800920 if (atomic_long_inc_return(&fsc->writeback_count) >
921 CONGESTION_ON_THRESH(
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700922 fsc->mount_options->congestion_kb)) {
Jan Kara09dc9fc2017-04-12 12:24:33 +0200923 set_bdi_congested(inode_to_bdi(inode),
Sage Weil213c99e2010-08-03 10:25:11 -0700924 BLK_RW_ASYNC);
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800925 }
926
Yan, Zheng0713e5f2017-08-31 16:55:48 +0800927
928 pages[locked_pages++] = page;
929 pvec.pages[i] = NULL;
930
Jeff Layton8ff2d292021-04-05 10:40:56 -0400931 len += thp_size(page);
Sage Weil1d3576f2009-10-06 11:31:09 -0700932 }
933
934 /* did we get anything? */
935 if (!locked_pages)
936 goto release_pvec_pages;
937 if (i) {
Yan, Zheng0713e5f2017-08-31 16:55:48 +0800938 unsigned j, n = 0;
939 /* shift unused page to beginning of pvec */
940 for (j = 0; j < pvec_pages; j++) {
941 if (!pvec.pages[j])
942 continue;
943 if (n < j)
944 pvec.pages[n] = pvec.pages[j];
945 n++;
946 }
947 pvec.nr = n;
Sage Weil1d3576f2009-10-06 11:31:09 -0700948
949 if (pvec_pages && i == pvec_pages &&
950 locked_pages < max_pages) {
951 dout("reached end pvec, trying for more\n");
Yan, Zheng0713e5f2017-08-31 16:55:48 +0800952 pagevec_release(&pvec);
Sage Weil1d3576f2009-10-06 11:31:09 -0700953 goto get_more_pages;
954 }
Sage Weil1d3576f2009-10-06 11:31:09 -0700955 }
956
Yan, Zheng5b646402016-01-07 16:00:17 +0800957new_request:
Alex Eldere5975c72013-03-14 14:09:05 -0500958 offset = page_offset(pages[0]);
Yan, Zheng5b646402016-01-07 16:00:17 +0800959 len = wsize;
960
961 req = ceph_osdc_new_request(&fsc->client->osdc,
962 &ci->i_layout, vino,
963 offset, &len, 0, num_ops,
Yan, Zheng1f934b02017-08-30 11:36:06 +0800964 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
965 snapc, ceph_wbc.truncate_seq,
966 ceph_wbc.truncate_size, false);
Yan, Zheng5b646402016-01-07 16:00:17 +0800967 if (IS_ERR(req)) {
968 req = ceph_osdc_new_request(&fsc->client->osdc,
969 &ci->i_layout, vino,
970 offset, &len, 0,
971 min(num_ops,
972 CEPH_OSD_SLAB_OPS),
973 CEPH_OSD_OP_WRITE,
Ilya Dryomov54ea0042017-02-11 18:48:41 +0100974 CEPH_OSD_FLAG_WRITE,
Yan, Zheng1f934b02017-08-30 11:36:06 +0800975 snapc, ceph_wbc.truncate_seq,
976 ceph_wbc.truncate_size, true);
Yan, Zheng5b646402016-01-07 16:00:17 +0800977 BUG_ON(IS_ERR(req));
Yan, Zhenge1966b42015-06-18 03:10:58 +0800978 }
Yan, Zheng5b646402016-01-07 16:00:17 +0800979 BUG_ON(len < page_offset(pages[locked_pages - 1]) +
Jeff Layton8ff2d292021-04-05 10:40:56 -0400980 thp_size(page) - offset);
Sage Weil1d3576f2009-10-06 11:31:09 -0700981
Yan, Zheng5b646402016-01-07 16:00:17 +0800982 req->r_callback = writepages_finish;
983 req->r_inode = inode;
984
985 /* Format the osd request message and submit the write */
986 len = 0;
987 data_pages = pages;
988 op_idx = 0;
989 for (i = 0; i < locked_pages; i++) {
990 u64 cur_offset = page_offset(pages[i]);
991 if (offset + len != cur_offset) {
Yanhu Cao3fb99d42017-07-21 17:20:10 +0800992 if (op_idx + 1 == req->r_num_ops)
Yan, Zheng5b646402016-01-07 16:00:17 +0800993 break;
994 osd_req_op_extent_dup_last(req, op_idx,
995 cur_offset - offset);
996 dout("writepages got pages at %llu~%llu\n",
997 offset, len);
998 osd_req_op_extent_osd_data_pages(req, op_idx,
999 data_pages, len, 0,
Jeff Laytona0102bd2020-07-30 11:03:55 -04001000 from_pool, false);
Yan, Zheng5b646402016-01-07 16:00:17 +08001001 osd_req_op_extent_update(req, op_idx, len);
Alex Eldere5975c72013-03-14 14:09:05 -05001002
Yan, Zheng5b646402016-01-07 16:00:17 +08001003 len = 0;
1004 offset = cur_offset;
1005 data_pages = pages + i;
1006 op_idx++;
1007 }
1008
1009 set_page_writeback(pages[i]);
Jeff Layton8ff2d292021-04-05 10:40:56 -04001010 len += thp_size(page);
Yan, Zheng5b646402016-01-07 16:00:17 +08001011 }
1012
Yan, Zheng1f934b02017-08-30 11:36:06 +08001013 if (ceph_wbc.size_stable) {
1014 len = min(len, ceph_wbc.i_size - offset);
Yan, Zheng5b646402016-01-07 16:00:17 +08001015 } else if (i == locked_pages) {
1016 /* writepages_finish() clears writeback pages
1017 * according to the data length, so make sure
1018 * data length covers all locked pages */
Jeff Layton8ff2d292021-04-05 10:40:56 -04001019 u64 min_len = len + 1 - thp_size(page);
Yan, Zheng1f934b02017-08-30 11:36:06 +08001020 len = get_writepages_data_length(inode, pages[i - 1],
1021 offset);
Yan, Zheng5b646402016-01-07 16:00:17 +08001022 len = max(len, min_len);
1023 }
1024 dout("writepages got pages at %llu~%llu\n", offset, len);
1025
1026 osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len,
Jeff Laytona0102bd2020-07-30 11:03:55 -04001027 0, from_pool, false);
Yan, Zheng5b646402016-01-07 16:00:17 +08001028 osd_req_op_extent_update(req, op_idx, len);
1029
Yan, Zheng5b646402016-01-07 16:00:17 +08001030 BUG_ON(op_idx + 1 != req->r_num_ops);
1031
Jeff Laytona0102bd2020-07-30 11:03:55 -04001032 from_pool = false;
Yan, Zheng5b646402016-01-07 16:00:17 +08001033 if (i < locked_pages) {
1034 BUG_ON(num_ops <= req->r_num_ops);
1035 num_ops -= req->r_num_ops;
Yan, Zheng5b646402016-01-07 16:00:17 +08001036 locked_pages -= i;
Alex Eldere5975c72013-03-14 14:09:05 -05001037
Yan, Zheng5b646402016-01-07 16:00:17 +08001038 /* allocate new pages array for next request */
1039 data_pages = pages;
Kees Cook6da2ec52018-06-12 13:55:00 -07001040 pages = kmalloc_array(locked_pages, sizeof(*pages),
1041 GFP_NOFS);
Yan, Zheng5b646402016-01-07 16:00:17 +08001042 if (!pages) {
Jeff Laytona0102bd2020-07-30 11:03:55 -04001043 from_pool = true;
1044 pages = mempool_alloc(ceph_wb_pagevec_pool, GFP_NOFS);
Yan, Zheng5b646402016-01-07 16:00:17 +08001045 BUG_ON(!pages);
1046 }
1047 memcpy(pages, data_pages + i,
1048 locked_pages * sizeof(*pages));
1049 memset(data_pages + i, 0,
1050 locked_pages * sizeof(*pages));
1051 } else {
1052 BUG_ON(num_ops != req->r_num_ops);
1053 index = pages[i - 1]->index + 1;
1054 /* request message now owns the pages array */
1055 pages = NULL;
1056 }
Alex Eldere5975c72013-03-14 14:09:05 -05001057
Arnd Bergmannfac02dd2018-07-13 22:18:37 +02001058 req->r_mtime = inode->i_mtime;
Sage Weil9d6fcb02011-05-12 15:48:16 -07001059 rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
1060 BUG_ON(rc);
Sage Weil1d3576f2009-10-06 11:31:09 -07001061 req = NULL;
1062
Yan, Zheng5b646402016-01-07 16:00:17 +08001063 wbc->nr_to_write -= i;
1064 if (pages)
1065 goto new_request;
1066
Yan, Zheng2a2d9272017-09-01 16:53:58 +08001067 /*
1068 * We stop writing back only if we are not doing
1069 * integrity sync. In case of integrity sync we have to
1070 * keep going until we have written all the pages
1071 * we tagged for writeback prior to entering this loop.
1072 */
1073 if (wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE)
Yan, Zhengaf9cc402018-03-04 16:36:01 +08001074 done = true;
Sage Weil1d3576f2009-10-06 11:31:09 -07001075
1076release_pvec_pages:
1077 dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
1078 pvec.nr ? pvec.pages[0] : NULL);
1079 pagevec_release(&pvec);
Sage Weil1d3576f2009-10-06 11:31:09 -07001080 }
1081
1082 if (should_loop && !done) {
1083 /* more to do; loop back to beginning of file */
1084 dout("writepages looping back to beginning of file\n");
Yan, Zheng2a2d9272017-09-01 16:53:58 +08001085 end = start_index - 1; /* OK even when start_index == 0 */
Yan, Zhengf2756352017-09-01 17:03:16 +08001086
1087 /* to write dirty pages associated with next snapc,
1088 * we need to wait until current writes complete */
1089 if (wbc->sync_mode != WB_SYNC_NONE &&
1090 start_index == 0 && /* all dirty pages were checked */
1091 !ceph_wbc.head_snapc) {
1092 struct page *page;
1093 unsigned i, nr;
1094 index = 0;
1095 while ((index <= end) &&
1096 (nr = pagevec_lookup_tag(&pvec, mapping, &index,
Jan Kara67fd7072017-11-15 17:35:19 -08001097 PAGECACHE_TAG_WRITEBACK))) {
Yan, Zhengf2756352017-09-01 17:03:16 +08001098 for (i = 0; i < nr; i++) {
1099 page = pvec.pages[i];
1100 if (page_snap_context(page) != snapc)
1101 continue;
1102 wait_on_page_writeback(page);
1103 }
1104 pagevec_release(&pvec);
1105 cond_resched();
1106 }
1107 }
1108
Yan, Zheng2a2d9272017-09-01 16:53:58 +08001109 start_index = 0;
Sage Weil1d3576f2009-10-06 11:31:09 -07001110 index = 0;
1111 goto retry;
1112 }
1113
1114 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1115 mapping->writeback_index = index;
1116
1117out:
Ilya Dryomov3ed97d62016-04-26 15:05:29 +02001118 ceph_osdc_put_request(req);
Yan, Zheng2a2d9272017-09-01 16:53:58 +08001119 ceph_put_snap_context(last_snapc);
1120 dout("writepages dend - startone, rc = %d\n", rc);
Sage Weil1d3576f2009-10-06 11:31:09 -07001121 return rc;
1122}
1123
1124
1125
1126/*
1127 * See if a given @snapc is either writeable, or already written.
1128 */
1129static int context_is_writeable_or_written(struct inode *inode,
1130 struct ceph_snap_context *snapc)
1131{
Yan, Zheng05455e12017-09-02 10:50:48 +08001132 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL, NULL);
Sage Weil6298a332010-03-31 22:01:38 -07001133 int ret = !oldest || snapc->seq <= oldest->seq;
1134
1135 ceph_put_snap_context(oldest);
1136 return ret;
Sage Weil1d3576f2009-10-06 11:31:09 -07001137}
1138
Jeff Layton18d620f2020-05-28 13:56:54 -04001139/**
1140 * ceph_find_incompatible - find an incompatible context and return it
Jeff Layton18d620f2020-05-28 13:56:54 -04001141 * @page: page being dirtied
1142 *
1143 * We are only allowed to write into/dirty a page if the page is
1144 * clean, or already dirty within the same snap context. Returns a
1145 * conflicting context if there is one, NULL if there isn't, or a
1146 * negative error code on other errors.
1147 *
1148 * Must be called with page lock held.
1149 */
1150static struct ceph_snap_context *
Jeff Laytond45156b2020-05-28 14:59:49 -04001151ceph_find_incompatible(struct page *page)
Jeff Layton18d620f2020-05-28 13:56:54 -04001152{
Jeff Laytond45156b2020-05-28 14:59:49 -04001153 struct inode *inode = page->mapping->host;
Jeff Layton18d620f2020-05-28 13:56:54 -04001154 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1155 struct ceph_inode_info *ci = ceph_inode(inode);
1156
Jeff Layton50c91322020-09-25 07:55:39 -04001157 if (READ_ONCE(fsc->mount_state) >= CEPH_MOUNT_SHUTDOWN) {
Jeff Layton18d620f2020-05-28 13:56:54 -04001158 dout(" page %p forced umount\n", page);
1159 return ERR_PTR(-EIO);
1160 }
1161
1162 for (;;) {
1163 struct ceph_snap_context *snapc, *oldest;
1164
1165 wait_on_page_writeback(page);
1166
1167 snapc = page_snap_context(page);
1168 if (!snapc || snapc == ci->i_head_snapc)
1169 break;
1170
1171 /*
1172 * this page is already dirty in another (older) snap
1173 * context! is it writeable now?
1174 */
1175 oldest = get_oldest_context(inode, NULL, NULL);
1176 if (snapc->seq > oldest->seq) {
1177 /* not writeable -- return it for the caller to deal with */
1178 ceph_put_snap_context(oldest);
1179 dout(" page %p snapc %p not current or oldest\n", page, snapc);
1180 return ceph_get_snap_context(snapc);
1181 }
1182 ceph_put_snap_context(oldest);
1183
1184 /* yay, writeable, do it now (without dropping page lock) */
1185 dout(" page %p snapc %p not current, but oldest\n", page, snapc);
1186 if (clear_page_dirty_for_io(page)) {
1187 int r = writepage_nounlock(page, NULL);
1188 if (r < 0)
1189 return ERR_PTR(r);
1190 }
1191 }
1192 return NULL;
1193}
1194
Jeff Laytond8013272020-06-05 10:43:21 -04001195static int ceph_netfs_check_write_begin(struct file *file, loff_t pos, unsigned int len,
1196 struct page *page, void **_fsdata)
1197{
1198 struct inode *inode = file_inode(file);
1199 struct ceph_inode_info *ci = ceph_inode(inode);
1200 struct ceph_snap_context *snapc;
1201
1202 snapc = ceph_find_incompatible(page);
1203 if (snapc) {
1204 int r;
1205
1206 unlock_page(page);
1207 put_page(page);
1208 if (IS_ERR(snapc))
1209 return PTR_ERR(snapc);
1210
1211 ceph_queue_writeback(inode);
1212 r = wait_event_killable(ci->i_cap_wq,
1213 context_is_writeable_or_written(inode, snapc));
1214 ceph_put_snap_context(snapc);
1215 return r == 0 ? -EAGAIN : r;
1216 }
1217 return 0;
1218}
1219
Sage Weil1d3576f2009-10-06 11:31:09 -07001220/*
1221 * We are only allowed to write into/dirty the page if the page is
1222 * clean, or already dirty within the same snap context.
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001223 */
1224static int ceph_write_begin(struct file *file, struct address_space *mapping,
1225 loff_t pos, unsigned len, unsigned flags,
1226 struct page **pagep, void **fsdata)
1227{
Al Viro496ad9a2013-01-23 17:07:38 -05001228 struct inode *inode = file_inode(file);
Jeff Layton1cc16992020-06-05 09:05:17 -04001229 struct ceph_inode_info *ci = ceph_inode(inode);
Jeff Layton1cc16992020-06-05 09:05:17 -04001230 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001231 pgoff_t index = pos >> PAGE_SHIFT;
Jeff Laytond8013272020-06-05 10:43:21 -04001232 int r;
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001233
Jeff Laytond8013272020-06-05 10:43:21 -04001234 /*
1235 * Uninlining should have already been done and everything updated, EXCEPT
1236 * for inline_version sent to the MDS.
1237 */
1238 if (ci->i_inline_version != CEPH_INLINE_NONE) {
Jeff Layton4a357f52020-11-10 16:24:40 -05001239 page = grab_cache_page_write_begin(mapping, index, flags);
Jeff Laytond8013272020-06-05 10:43:21 -04001240 if (!page)
1241 return -ENOMEM;
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001242
Jeff Laytond8013272020-06-05 10:43:21 -04001243 /*
1244 * The inline_version on a new inode is set to 1. If that's the
1245 * case, then the page is brand new and isn't yet Uptodate.
1246 */
1247 r = 0;
1248 if (index == 0 && ci->i_inline_version != 1) {
1249 if (!PageUptodate(page)) {
1250 WARN_ONCE(1, "ceph: write_begin called on still-inlined inode (inline_version %llu)!\n",
1251 ci->i_inline_version);
1252 r = -EINVAL;
Jeff Layton1cc16992020-06-05 09:05:17 -04001253 }
Jeff Laytond8013272020-06-05 10:43:21 -04001254 goto out;
Jeff Layton1cc16992020-06-05 09:05:17 -04001255 }
Jeff Layton8ff2d292021-04-05 10:40:56 -04001256 zero_user_segment(page, 0, thp_size(page));
Jeff Laytond8013272020-06-05 10:43:21 -04001257 SetPageUptodate(page);
1258 goto out;
Jeff Layton1cc16992020-06-05 09:05:17 -04001259 }
1260
Jeff Laytond8013272020-06-05 10:43:21 -04001261 r = netfs_write_begin(file, inode->i_mapping, pos, len, 0, &page, NULL,
1262 &ceph_netfs_read_ops, NULL);
1263out:
1264 if (r == 0)
1265 wait_on_page_fscache(page);
Jeff Layton1cc16992020-06-05 09:05:17 -04001266 if (r < 0) {
Jeff Laytond8013272020-06-05 10:43:21 -04001267 if (page)
Jeff Layton1cc16992020-06-05 09:05:17 -04001268 put_page(page);
Jeff Layton1cc16992020-06-05 09:05:17 -04001269 } else {
Jeff Laytond8013272020-06-05 10:43:21 -04001270 WARN_ON_ONCE(!PageLocked(page));
Jeff Layton1cc16992020-06-05 09:05:17 -04001271 *pagep = page;
1272 }
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001273 return r;
1274}
1275
1276/*
Sage Weil1d3576f2009-10-06 11:31:09 -07001277 * we don't do anything in here that simple_write_end doesn't do
Yan, Zheng5dda377c2015-04-30 14:40:54 +08001278 * except adjust dirty page accounting
Sage Weil1d3576f2009-10-06 11:31:09 -07001279 */
1280static int ceph_write_end(struct file *file, struct address_space *mapping,
1281 loff_t pos, unsigned len, unsigned copied,
1282 struct page *page, void *fsdata)
1283{
Al Viro496ad9a2013-01-23 17:07:38 -05001284 struct inode *inode = file_inode(file);
Yan, Zhengefb0ca72017-05-22 12:03:32 +08001285 bool check_cap = false;
Sage Weil1d3576f2009-10-06 11:31:09 -07001286
1287 dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
1288 inode, page, (int)pos, (int)copied, (int)len);
1289
1290 /* zero the stale part of the page if we did a short copy */
Al Virob9de3132016-09-05 22:20:03 -04001291 if (!PageUptodate(page)) {
1292 if (copied < len) {
1293 copied = 0;
1294 goto out;
1295 }
1296 SetPageUptodate(page);
1297 }
Sage Weil1d3576f2009-10-06 11:31:09 -07001298
1299 /* did file size increase? */
Yan, Zheng99c88e62015-12-30 11:32:46 +08001300 if (pos+copied > i_size_read(inode))
Sage Weil1d3576f2009-10-06 11:31:09 -07001301 check_cap = ceph_inode_set_size(inode, pos+copied);
1302
Sage Weil1d3576f2009-10-06 11:31:09 -07001303 set_page_dirty(page);
1304
Al Virob9de3132016-09-05 22:20:03 -04001305out:
Sage Weil1d3576f2009-10-06 11:31:09 -07001306 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001307 put_page(page);
Sage Weil1d3576f2009-10-06 11:31:09 -07001308
1309 if (check_cap)
1310 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
1311
1312 return copied;
1313}
1314
1315/*
1316 * we set .direct_IO to indicate direct io is supported, but since we
1317 * intercept O_DIRECT reads and writes early, this function should
1318 * never get called.
1319 */
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001320static ssize_t ceph_direct_io(struct kiocb *iocb, struct iov_iter *iter)
Sage Weil1d3576f2009-10-06 11:31:09 -07001321{
1322 WARN_ON(1);
1323 return -EINVAL;
1324}
1325
1326const struct address_space_operations ceph_aops = {
1327 .readpage = ceph_readpage,
Jeff Layton49870052020-07-09 14:43:23 -04001328 .readahead = ceph_readahead,
Sage Weil1d3576f2009-10-06 11:31:09 -07001329 .writepage = ceph_writepage,
1330 .writepages = ceph_writepages_start,
1331 .write_begin = ceph_write_begin,
1332 .write_end = ceph_write_end,
1333 .set_page_dirty = ceph_set_page_dirty,
1334 .invalidatepage = ceph_invalidatepage,
1335 .releasepage = ceph_releasepage,
1336 .direct_IO = ceph_direct_io,
1337};
1338
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001339static void ceph_block_sigs(sigset_t *oldset)
1340{
1341 sigset_t mask;
1342 siginitsetinv(&mask, sigmask(SIGKILL));
1343 sigprocmask(SIG_BLOCK, &mask, oldset);
1344}
1345
1346static void ceph_restore_sigs(sigset_t *oldset)
1347{
1348 sigprocmask(SIG_SETMASK, oldset, NULL);
1349}
Sage Weil1d3576f2009-10-06 11:31:09 -07001350
1351/*
1352 * vm ops
1353 */
Souptick Joarder24499842018-07-23 21:32:24 +05301354static vm_fault_t ceph_filemap_fault(struct vm_fault *vmf)
Yan, Zheng61f68812013-11-28 14:28:14 +08001355{
Dave Jiang11bac802017-02-24 14:56:41 -08001356 struct vm_area_struct *vma = vmf->vma;
Yan, Zheng61f68812013-11-28 14:28:14 +08001357 struct inode *inode = file_inode(vma->vm_file);
1358 struct ceph_inode_info *ci = ceph_inode(inode);
1359 struct ceph_file_info *fi = vma->vm_file->private_data;
Matthew Wilcox (Oracle)c403c3a22020-10-04 19:04:24 +01001360 loff_t off = (loff_t)vmf->pgoff << PAGE_SHIFT;
Souptick Joarder24499842018-07-23 21:32:24 +05301361 int want, got, err;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001362 sigset_t oldset;
Souptick Joarder24499842018-07-23 21:32:24 +05301363 vm_fault_t ret = VM_FAULT_SIGBUS;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001364
1365 ceph_block_sigs(&oldset);
Yan, Zheng61f68812013-11-28 14:28:14 +08001366
Jeff Layton8ff2d292021-04-05 10:40:56 -04001367 dout("filemap_fault %p %llx.%llx %llu trying to get caps\n",
1368 inode, ceph_vinop(inode), off);
Yan, Zheng61f68812013-11-28 14:28:14 +08001369 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1370 want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1371 else
1372 want = CEPH_CAP_FILE_CACHE;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001373
1374 got = 0;
Jeff Laytone72968e2021-04-05 12:19:35 -04001375 err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_RD, want, -1, &got);
Souptick Joarder24499842018-07-23 21:32:24 +05301376 if (err < 0)
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001377 goto out_restore;
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001378
Jeff Layton8ff2d292021-04-05 10:40:56 -04001379 dout("filemap_fault %p %llu got cap refs on %s\n",
1380 inode, off, ceph_cap_string(got));
Yan, Zheng61f68812013-11-28 14:28:14 +08001381
Yan, Zheng83701242014-11-14 22:36:18 +08001382 if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
Yan, Zheng2b1ac852016-10-25 10:51:55 +08001383 ci->i_inline_version == CEPH_INLINE_NONE) {
Yan, Zheng5d988302017-12-15 11:15:36 +08001384 CEPH_DEFINE_RW_CONTEXT(rw_ctx, got);
1385 ceph_add_rw_context(fi, &rw_ctx);
Dave Jiang11bac802017-02-24 14:56:41 -08001386 ret = filemap_fault(vmf);
Yan, Zheng5d988302017-12-15 11:15:36 +08001387 ceph_del_rw_context(fi, &rw_ctx);
Jeff Layton8ff2d292021-04-05 10:40:56 -04001388 dout("filemap_fault %p %llu drop cap refs %s ret %x\n",
1389 inode, off, ceph_cap_string(got), ret);
Yan, Zheng2b1ac852016-10-25 10:51:55 +08001390 } else
Souptick Joarder24499842018-07-23 21:32:24 +05301391 err = -EAGAIN;
Yan, Zheng61f68812013-11-28 14:28:14 +08001392
Yan, Zheng61f68812013-11-28 14:28:14 +08001393 ceph_put_cap_refs(ci, got);
1394
Souptick Joarder24499842018-07-23 21:32:24 +05301395 if (err != -EAGAIN)
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001396 goto out_restore;
Yan, Zheng83701242014-11-14 22:36:18 +08001397
1398 /* read inline data */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001399 if (off >= PAGE_SIZE) {
Yan, Zheng83701242014-11-14 22:36:18 +08001400 /* does not support inline data > PAGE_SIZE */
1401 ret = VM_FAULT_SIGBUS;
1402 } else {
Yan, Zheng83701242014-11-14 22:36:18 +08001403 struct address_space *mapping = inode->i_mapping;
1404 struct page *page = find_or_create_page(mapping, 0,
Michal Hockoc62d2552015-11-06 16:28:49 -08001405 mapping_gfp_constraint(mapping,
1406 ~__GFP_FS));
Yan, Zheng83701242014-11-14 22:36:18 +08001407 if (!page) {
1408 ret = VM_FAULT_OOM;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001409 goto out_inline;
Yan, Zheng83701242014-11-14 22:36:18 +08001410 }
Souptick Joarder24499842018-07-23 21:32:24 +05301411 err = __ceph_do_getattr(inode, page,
Yan, Zheng83701242014-11-14 22:36:18 +08001412 CEPH_STAT_CAP_INLINE_DATA, true);
Souptick Joarder24499842018-07-23 21:32:24 +05301413 if (err < 0 || off >= i_size_read(inode)) {
Yan, Zheng83701242014-11-14 22:36:18 +08001414 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001415 put_page(page);
Souptick Joarderc64a2b02019-01-05 01:00:29 +05301416 ret = vmf_error(err);
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001417 goto out_inline;
Yan, Zheng83701242014-11-14 22:36:18 +08001418 }
Souptick Joarder24499842018-07-23 21:32:24 +05301419 if (err < PAGE_SIZE)
1420 zero_user_segment(page, err, PAGE_SIZE);
Yan, Zheng83701242014-11-14 22:36:18 +08001421 else
1422 flush_dcache_page(page);
1423 SetPageUptodate(page);
1424 vmf->page = page;
1425 ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001426out_inline:
Jeff Layton8ff2d292021-04-05 10:40:56 -04001427 dout("filemap_fault %p %llu read inline data ret %x\n",
1428 inode, off, ret);
Yan, Zheng83701242014-11-14 22:36:18 +08001429 }
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001430out_restore:
1431 ceph_restore_sigs(&oldset);
Souptick Joarder24499842018-07-23 21:32:24 +05301432 if (err < 0)
1433 ret = vmf_error(err);
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001434
Yan, Zheng61f68812013-11-28 14:28:14 +08001435 return ret;
1436}
Sage Weil1d3576f2009-10-06 11:31:09 -07001437
Souptick Joarder24499842018-07-23 21:32:24 +05301438static vm_fault_t ceph_page_mkwrite(struct vm_fault *vmf)
Sage Weil1d3576f2009-10-06 11:31:09 -07001439{
Dave Jiang11bac802017-02-24 14:56:41 -08001440 struct vm_area_struct *vma = vmf->vma;
Al Viro496ad9a2013-01-23 17:07:38 -05001441 struct inode *inode = file_inode(vma->vm_file);
Yan, Zheng61f68812013-11-28 14:28:14 +08001442 struct ceph_inode_info *ci = ceph_inode(inode);
1443 struct ceph_file_info *fi = vma->vm_file->private_data;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001444 struct ceph_cap_flush *prealloc_cf;
Yan, Zheng61f68812013-11-28 14:28:14 +08001445 struct page *page = vmf->page;
Alex Elder6285bc22012-10-02 10:25:51 -05001446 loff_t off = page_offset(page);
Yan, Zheng61f68812013-11-28 14:28:14 +08001447 loff_t size = i_size_read(inode);
1448 size_t len;
Souptick Joarder24499842018-07-23 21:32:24 +05301449 int want, got, err;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001450 sigset_t oldset;
Souptick Joarder24499842018-07-23 21:32:24 +05301451 vm_fault_t ret = VM_FAULT_SIGBUS;
Sage Weil1d3576f2009-10-06 11:31:09 -07001452
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001453 prealloc_cf = ceph_alloc_cap_flush();
1454 if (!prealloc_cf)
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001455 return VM_FAULT_OOM;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001456
Jeff Layton249c1df2019-08-01 10:06:40 -04001457 sb_start_pagefault(inode->i_sb);
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001458 ceph_block_sigs(&oldset);
Sage Weil1d3576f2009-10-06 11:31:09 -07001459
Yan, Zheng28127bd2014-11-14 22:38:29 +08001460 if (ci->i_inline_version != CEPH_INLINE_NONE) {
1461 struct page *locked_page = NULL;
1462 if (off == 0) {
1463 lock_page(page);
1464 locked_page = page;
1465 }
Souptick Joarder24499842018-07-23 21:32:24 +05301466 err = ceph_uninline_data(vma->vm_file, locked_page);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001467 if (locked_page)
1468 unlock_page(locked_page);
Souptick Joarder24499842018-07-23 21:32:24 +05301469 if (err < 0)
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001470 goto out_free;
Yan, Zheng28127bd2014-11-14 22:38:29 +08001471 }
1472
Jeff Layton8ff2d292021-04-05 10:40:56 -04001473 if (off + thp_size(page) <= size)
1474 len = thp_size(page);
Sage Weil1d3576f2009-10-06 11:31:09 -07001475 else
Jeff Layton8ff2d292021-04-05 10:40:56 -04001476 len = offset_in_thp(page, size);
Sage Weil1d3576f2009-10-06 11:31:09 -07001477
Yan, Zheng61f68812013-11-28 14:28:14 +08001478 dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
1479 inode, ceph_vinop(inode), off, len, size);
1480 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1481 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1482 else
1483 want = CEPH_CAP_FILE_BUFFER;
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001484
1485 got = 0;
Jeff Laytone72968e2021-04-05 12:19:35 -04001486 err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_WR, want, off + len, &got);
Souptick Joarder24499842018-07-23 21:32:24 +05301487 if (err < 0)
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001488 goto out_free;
Yan, Zheng6ce026e2016-05-10 18:59:13 +08001489
Yan, Zheng61f68812013-11-28 14:28:14 +08001490 dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
1491 inode, off, len, ceph_cap_string(got));
1492
1493 /* Update time before taking page lock */
1494 file_update_time(vma->vm_file);
Jeff Layton5c308352019-06-06 08:57:27 -04001495 inode_inc_iversion_raw(inode);
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001496
Yan, Zhengf0b33df2016-05-10 19:09:06 +08001497 do {
Jeff Laytond45156b2020-05-28 14:59:49 -04001498 struct ceph_snap_context *snapc;
1499
Yan, Zhengf0b33df2016-05-10 19:09:06 +08001500 lock_page(page);
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001501
Andreas Gruenbachercb03c142020-02-13 21:24:22 +01001502 if (page_mkwrite_check_truncate(page, inode) < 0) {
Yan, Zhengf0b33df2016-05-10 19:09:06 +08001503 unlock_page(page);
1504 ret = VM_FAULT_NOPAGE;
1505 break;
1506 }
Yehuda Sadeh4af6b222010-02-09 11:02:51 -08001507
Jeff Laytond45156b2020-05-28 14:59:49 -04001508 snapc = ceph_find_incompatible(page);
1509 if (!snapc) {
Yan, Zhengf0b33df2016-05-10 19:09:06 +08001510 /* success. we'll keep the page locked. */
1511 set_page_dirty(page);
1512 ret = VM_FAULT_LOCKED;
Jeff Laytond45156b2020-05-28 14:59:49 -04001513 break;
Yan, Zhengf0b33df2016-05-10 19:09:06 +08001514 }
Jeff Laytond45156b2020-05-28 14:59:49 -04001515
1516 unlock_page(page);
1517
1518 if (IS_ERR(snapc)) {
1519 ret = VM_FAULT_SIGBUS;
1520 break;
1521 }
1522
1523 ceph_queue_writeback(inode);
1524 err = wait_event_killable(ci->i_cap_wq,
1525 context_is_writeable_or_written(inode, snapc));
1526 ceph_put_snap_context(snapc);
1527 } while (err == 0);
Yan, Zhengf0b33df2016-05-10 19:09:06 +08001528
Yan, Zheng28127bd2014-11-14 22:38:29 +08001529 if (ret == VM_FAULT_LOCKED ||
1530 ci->i_inline_version != CEPH_INLINE_NONE) {
Yan, Zheng61f68812013-11-28 14:28:14 +08001531 int dirty;
1532 spin_lock(&ci->i_ceph_lock);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001533 ci->i_inline_version = CEPH_INLINE_NONE;
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001534 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1535 &prealloc_cf);
Yan, Zheng61f68812013-11-28 14:28:14 +08001536 spin_unlock(&ci->i_ceph_lock);
1537 if (dirty)
1538 __mark_inode_dirty(inode, dirty);
1539 }
1540
Souptick Joarder24499842018-07-23 21:32:24 +05301541 dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %x\n",
Yan, Zheng61f68812013-11-28 14:28:14 +08001542 inode, off, len, ceph_cap_string(got), ret);
Jeff Laytona8810cd2020-12-10 14:39:26 -05001543 ceph_put_cap_refs_async(ci, got);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001544out_free:
Yan, Zheng4f7e89f2016-05-10 18:40:28 +08001545 ceph_restore_sigs(&oldset);
Jeff Layton249c1df2019-08-01 10:06:40 -04001546 sb_end_pagefault(inode->i_sb);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001547 ceph_free_cap_flush(prealloc_cf);
Souptick Joarder24499842018-07-23 21:32:24 +05301548 if (err < 0)
1549 ret = vmf_error(err);
Sage Weil1d3576f2009-10-06 11:31:09 -07001550 return ret;
1551}
1552
Yan, Zheng31c542a2014-11-14 21:41:55 +08001553void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
1554 char *data, size_t len)
1555{
1556 struct address_space *mapping = inode->i_mapping;
1557 struct page *page;
1558
1559 if (locked_page) {
1560 page = locked_page;
1561 } else {
1562 if (i_size_read(inode) == 0)
1563 return;
1564 page = find_or_create_page(mapping, 0,
Michal Hockoc62d2552015-11-06 16:28:49 -08001565 mapping_gfp_constraint(mapping,
1566 ~__GFP_FS));
Yan, Zheng31c542a2014-11-14 21:41:55 +08001567 if (!page)
1568 return;
1569 if (PageUptodate(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 return;
1573 }
1574 }
1575
Ilya Dryomov0668ff52014-12-19 13:10:10 +03001576 dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
Yan, Zheng31c542a2014-11-14 21:41:55 +08001577 inode, ceph_vinop(inode), len, locked_page);
1578
1579 if (len > 0) {
1580 void *kaddr = kmap_atomic(page);
1581 memcpy(kaddr, data, len);
1582 kunmap_atomic(kaddr);
1583 }
1584
1585 if (page != locked_page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001586 if (len < PAGE_SIZE)
1587 zero_user_segment(page, len, PAGE_SIZE);
Yan, Zheng31c542a2014-11-14 21:41:55 +08001588 else
1589 flush_dcache_page(page);
1590
1591 SetPageUptodate(page);
1592 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001593 put_page(page);
Yan, Zheng31c542a2014-11-14 21:41:55 +08001594 }
1595}
1596
Yan, Zheng28127bd2014-11-14 22:38:29 +08001597int ceph_uninline_data(struct file *filp, struct page *locked_page)
1598{
1599 struct inode *inode = file_inode(filp);
1600 struct ceph_inode_info *ci = ceph_inode(inode);
1601 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1602 struct ceph_osd_request *req;
1603 struct page *page = NULL;
1604 u64 len, inline_version;
1605 int err = 0;
1606 bool from_pagecache = false;
1607
1608 spin_lock(&ci->i_ceph_lock);
1609 inline_version = ci->i_inline_version;
1610 spin_unlock(&ci->i_ceph_lock);
1611
1612 dout("uninline_data %p %llx.%llx inline_version %llu\n",
1613 inode, ceph_vinop(inode), inline_version);
1614
1615 if (inline_version == 1 || /* initial version, no data */
1616 inline_version == CEPH_INLINE_NONE)
1617 goto out;
1618
1619 if (locked_page) {
1620 page = locked_page;
1621 WARN_ON(!PageUptodate(page));
1622 } else if (ceph_caps_issued(ci) &
1623 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) {
1624 page = find_get_page(inode->i_mapping, 0);
1625 if (page) {
1626 if (PageUptodate(page)) {
1627 from_pagecache = true;
1628 lock_page(page);
1629 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001630 put_page(page);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001631 page = NULL;
1632 }
1633 }
1634 }
1635
1636 if (page) {
1637 len = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001638 if (len > PAGE_SIZE)
1639 len = PAGE_SIZE;
Yan, Zheng28127bd2014-11-14 22:38:29 +08001640 } else {
1641 page = __page_cache_alloc(GFP_NOFS);
1642 if (!page) {
1643 err = -ENOMEM;
1644 goto out;
1645 }
1646 err = __ceph_do_getattr(inode, page,
1647 CEPH_STAT_CAP_INLINE_DATA, true);
1648 if (err < 0) {
1649 /* no inline data */
1650 if (err == -ENODATA)
1651 err = 0;
1652 goto out;
1653 }
1654 len = err;
1655 }
1656
1657 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1658 ceph_vino(inode), 0, &len, 0, 1,
Ilya Dryomov54ea0042017-02-11 18:48:41 +01001659 CEPH_OSD_OP_CREATE, CEPH_OSD_FLAG_WRITE,
Ilya Dryomov34b759b2016-02-16 15:00:24 +01001660 NULL, 0, 0, false);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001661 if (IS_ERR(req)) {
1662 err = PTR_ERR(req);
1663 goto out;
1664 }
1665
Arnd Bergmannfac02dd2018-07-13 22:18:37 +02001666 req->r_mtime = inode->i_mtime;
Yan, Zheng28127bd2014-11-14 22:38:29 +08001667 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1668 if (!err)
1669 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1670 ceph_osdc_put_request(req);
1671 if (err < 0)
1672 goto out;
1673
1674 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1675 ceph_vino(inode), 0, &len, 1, 3,
Ilya Dryomov54ea0042017-02-11 18:48:41 +01001676 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
Ilya Dryomov34b759b2016-02-16 15:00:24 +01001677 NULL, ci->i_truncate_seq,
1678 ci->i_truncate_size, false);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001679 if (IS_ERR(req)) {
1680 err = PTR_ERR(req);
1681 goto out;
1682 }
1683
1684 osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false);
1685
Yan, Zhengec137c12015-04-13 11:25:07 +08001686 {
1687 __le64 xattr_buf = cpu_to_le64(inline_version);
1688 err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
1689 "inline_version", &xattr_buf,
1690 sizeof(xattr_buf),
1691 CEPH_OSD_CMPXATTR_OP_GT,
1692 CEPH_OSD_CMPXATTR_MODE_U64);
1693 if (err)
1694 goto out_put;
1695 }
Yan, Zheng28127bd2014-11-14 22:38:29 +08001696
Yan, Zhengec137c12015-04-13 11:25:07 +08001697 {
1698 char xattr_buf[32];
1699 int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
1700 "%llu", inline_version);
1701 err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
1702 "inline_version",
1703 xattr_buf, xattr_len, 0, 0);
1704 if (err)
1705 goto out_put;
1706 }
Yan, Zheng28127bd2014-11-14 22:38:29 +08001707
Arnd Bergmannfac02dd2018-07-13 22:18:37 +02001708 req->r_mtime = inode->i_mtime;
Yan, Zheng28127bd2014-11-14 22:38:29 +08001709 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1710 if (!err)
1711 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
Xiubo Li97e27aa2020-03-19 23:45:01 -04001712
Xiubo Li8ae99ae2021-03-22 20:28:49 +08001713 ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
Xiubo Li97e27aa2020-03-19 23:45:01 -04001714 req->r_end_latency, err);
1715
Yan, Zheng28127bd2014-11-14 22:38:29 +08001716out_put:
1717 ceph_osdc_put_request(req);
1718 if (err == -ECANCELED)
1719 err = 0;
1720out:
1721 if (page && page != locked_page) {
1722 if (from_pagecache) {
1723 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001724 put_page(page);
Yan, Zheng28127bd2014-11-14 22:38:29 +08001725 } else
1726 __free_pages(page, 0);
1727 }
1728
1729 dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
1730 inode, ceph_vinop(inode), inline_version, err);
1731 return err;
1732}
1733
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07001734static const struct vm_operations_struct ceph_vmops = {
Yan, Zheng61f68812013-11-28 14:28:14 +08001735 .fault = ceph_filemap_fault,
Sage Weil1d3576f2009-10-06 11:31:09 -07001736 .page_mkwrite = ceph_page_mkwrite,
1737};
1738
1739int ceph_mmap(struct file *file, struct vm_area_struct *vma)
1740{
1741 struct address_space *mapping = file->f_mapping;
1742
1743 if (!mapping->a_ops->readpage)
1744 return -ENOEXEC;
1745 file_accessed(file);
1746 vma->vm_ops = &ceph_vmops;
Sage Weil1d3576f2009-10-06 11:31:09 -07001747 return 0;
1748}
Yan, Zheng10183a62015-04-27 15:33:28 +08001749
1750enum {
1751 POOL_READ = 1,
1752 POOL_WRITE = 2,
1753};
1754
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001755static int __ceph_pool_perm_get(struct ceph_inode_info *ci,
1756 s64 pool, struct ceph_string *pool_ns)
Yan, Zheng10183a62015-04-27 15:33:28 +08001757{
1758 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1759 struct ceph_mds_client *mdsc = fsc->mdsc;
1760 struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
1761 struct rb_node **p, *parent;
1762 struct ceph_pool_perm *perm;
1763 struct page **pages;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001764 size_t pool_ns_len;
Yan, Zheng10183a62015-04-27 15:33:28 +08001765 int err = 0, err2 = 0, have = 0;
1766
1767 down_read(&mdsc->pool_perm_rwsem);
1768 p = &mdsc->pool_perm_tree.rb_node;
1769 while (*p) {
1770 perm = rb_entry(*p, struct ceph_pool_perm, node);
1771 if (pool < perm->pool)
1772 p = &(*p)->rb_left;
1773 else if (pool > perm->pool)
1774 p = &(*p)->rb_right;
1775 else {
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001776 int ret = ceph_compare_string(pool_ns,
1777 perm->pool_ns,
1778 perm->pool_ns_len);
1779 if (ret < 0)
1780 p = &(*p)->rb_left;
1781 else if (ret > 0)
1782 p = &(*p)->rb_right;
1783 else {
1784 have = perm->perm;
1785 break;
1786 }
Yan, Zheng10183a62015-04-27 15:33:28 +08001787 }
1788 }
1789 up_read(&mdsc->pool_perm_rwsem);
1790 if (*p)
1791 goto out;
1792
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001793 if (pool_ns)
1794 dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n",
1795 pool, (int)pool_ns->len, pool_ns->str);
1796 else
1797 dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool);
Yan, Zheng10183a62015-04-27 15:33:28 +08001798
1799 down_write(&mdsc->pool_perm_rwsem);
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001800 p = &mdsc->pool_perm_tree.rb_node;
Yan, Zheng10183a62015-04-27 15:33:28 +08001801 parent = NULL;
1802 while (*p) {
1803 parent = *p;
1804 perm = rb_entry(parent, struct ceph_pool_perm, node);
1805 if (pool < perm->pool)
1806 p = &(*p)->rb_left;
1807 else if (pool > perm->pool)
1808 p = &(*p)->rb_right;
1809 else {
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001810 int ret = ceph_compare_string(pool_ns,
1811 perm->pool_ns,
1812 perm->pool_ns_len);
1813 if (ret < 0)
1814 p = &(*p)->rb_left;
1815 else if (ret > 0)
1816 p = &(*p)->rb_right;
1817 else {
1818 have = perm->perm;
1819 break;
1820 }
Yan, Zheng10183a62015-04-27 15:33:28 +08001821 }
1822 }
1823 if (*p) {
1824 up_write(&mdsc->pool_perm_rwsem);
1825 goto out;
1826 }
1827
Ilya Dryomov34b759b2016-02-16 15:00:24 +01001828 rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
Yan, Zheng10183a62015-04-27 15:33:28 +08001829 1, false, GFP_NOFS);
1830 if (!rd_req) {
1831 err = -ENOMEM;
1832 goto out_unlock;
1833 }
1834
1835 rd_req->r_flags = CEPH_OSD_FLAG_READ;
1836 osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
1837 rd_req->r_base_oloc.pool = pool;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001838 if (pool_ns)
1839 rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns);
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001840 ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino);
Yan, Zheng10183a62015-04-27 15:33:28 +08001841
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02001842 err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS);
1843 if (err)
1844 goto out_unlock;
Yan, Zheng10183a62015-04-27 15:33:28 +08001845
Ilya Dryomov34b759b2016-02-16 15:00:24 +01001846 wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
Yan, Zheng10183a62015-04-27 15:33:28 +08001847 1, false, GFP_NOFS);
1848 if (!wr_req) {
1849 err = -ENOMEM;
1850 goto out_unlock;
1851 }
1852
Ilya Dryomov54ea0042017-02-11 18:48:41 +01001853 wr_req->r_flags = CEPH_OSD_FLAG_WRITE;
Yan, Zheng10183a62015-04-27 15:33:28 +08001854 osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
Ilya Dryomov63244fa2016-04-28 16:07:23 +02001855 ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc);
Ilya Dryomovd30291b2016-04-29 19:54:20 +02001856 ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid);
Yan, Zheng10183a62015-04-27 15:33:28 +08001857
Ilya Dryomov13d1ad12016-04-27 14:15:51 +02001858 err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS);
1859 if (err)
1860 goto out_unlock;
Yan, Zheng10183a62015-04-27 15:33:28 +08001861
1862 /* one page should be large enough for STAT data */
1863 pages = ceph_alloc_page_vector(1, GFP_KERNEL);
1864 if (IS_ERR(pages)) {
1865 err = PTR_ERR(pages);
1866 goto out_unlock;
1867 }
1868
1869 osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
1870 0, false, true);
Yan, Zheng10183a62015-04-27 15:33:28 +08001871 err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false);
1872
Arnd Bergmannfac02dd2018-07-13 22:18:37 +02001873 wr_req->r_mtime = ci->vfs_inode.i_mtime;
Yan, Zheng10183a62015-04-27 15:33:28 +08001874 err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false);
1875
1876 if (!err)
1877 err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
1878 if (!err2)
1879 err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
1880
1881 if (err >= 0 || err == -ENOENT)
1882 have |= POOL_READ;
Yan, Zheng131d7eb2019-07-25 20:16:47 +08001883 else if (err != -EPERM) {
Ilya Dryomov0b98acd2020-09-14 13:39:19 +02001884 if (err == -EBLOCKLISTED)
1885 fsc->blocklisted = true;
Yan, Zheng10183a62015-04-27 15:33:28 +08001886 goto out_unlock;
Yan, Zheng131d7eb2019-07-25 20:16:47 +08001887 }
Yan, Zheng10183a62015-04-27 15:33:28 +08001888
1889 if (err2 == 0 || err2 == -EEXIST)
1890 have |= POOL_WRITE;
1891 else if (err2 != -EPERM) {
Ilya Dryomov0b98acd2020-09-14 13:39:19 +02001892 if (err2 == -EBLOCKLISTED)
1893 fsc->blocklisted = true;
Yan, Zheng10183a62015-04-27 15:33:28 +08001894 err = err2;
1895 goto out_unlock;
1896 }
1897
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001898 pool_ns_len = pool_ns ? pool_ns->len : 0;
1899 perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS);
Yan, Zheng10183a62015-04-27 15:33:28 +08001900 if (!perm) {
1901 err = -ENOMEM;
1902 goto out_unlock;
1903 }
1904
1905 perm->pool = pool;
1906 perm->perm = have;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001907 perm->pool_ns_len = pool_ns_len;
1908 if (pool_ns_len > 0)
1909 memcpy(perm->pool_ns, pool_ns->str, pool_ns_len);
1910 perm->pool_ns[pool_ns_len] = 0;
1911
Yan, Zheng10183a62015-04-27 15:33:28 +08001912 rb_link_node(&perm->node, parent, p);
1913 rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
1914 err = 0;
1915out_unlock:
1916 up_write(&mdsc->pool_perm_rwsem);
1917
Ilya Dryomov3ed97d62016-04-26 15:05:29 +02001918 ceph_osdc_put_request(rd_req);
1919 ceph_osdc_put_request(wr_req);
Yan, Zheng10183a62015-04-27 15:33:28 +08001920out:
1921 if (!err)
1922 err = have;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001923 if (pool_ns)
1924 dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n",
1925 pool, (int)pool_ns->len, pool_ns->str, err);
1926 else
1927 dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err);
Yan, Zheng10183a62015-04-27 15:33:28 +08001928 return err;
1929}
1930
Yan, Zheng5e3ded12019-07-25 20:16:43 +08001931int ceph_pool_perm_check(struct inode *inode, int need)
Yan, Zheng10183a62015-04-27 15:33:28 +08001932{
Yan, Zheng5e3ded12019-07-25 20:16:43 +08001933 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001934 struct ceph_string *pool_ns;
Yan, Zheng5e3ded12019-07-25 20:16:43 +08001935 s64 pool;
Yan, Zheng10183a62015-04-27 15:33:28 +08001936 int ret, flags;
1937
Jeff Laytone9b22502021-01-26 11:49:54 -05001938 /* Only need to do this for regular files */
1939 if (!S_ISREG(inode->i_mode))
1940 return 0;
1941
Yan, Zheng80e80fb2016-12-13 16:03:26 +08001942 if (ci->i_vino.snap != CEPH_NOSNAP) {
1943 /*
1944 * Pool permission check needs to write to the first object.
1945 * But for snapshot, head of the first object may have alread
1946 * been deleted. Skip check to avoid creating orphan object.
1947 */
1948 return 0;
1949 }
1950
Yan, Zheng5e3ded12019-07-25 20:16:43 +08001951 if (ceph_test_mount_opt(ceph_inode_to_client(inode),
Yan, Zheng10183a62015-04-27 15:33:28 +08001952 NOPOOLPERM))
1953 return 0;
1954
1955 spin_lock(&ci->i_ceph_lock);
1956 flags = ci->i_ceph_flags;
Yan, Zheng76271512016-02-03 21:24:49 +08001957 pool = ci->i_layout.pool_id;
Yan, Zheng10183a62015-04-27 15:33:28 +08001958 spin_unlock(&ci->i_ceph_lock);
1959check:
1960 if (flags & CEPH_I_POOL_PERM) {
1961 if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
Yan, Zheng76271512016-02-03 21:24:49 +08001962 dout("ceph_pool_perm_check pool %lld no read perm\n",
Yan, Zheng10183a62015-04-27 15:33:28 +08001963 pool);
1964 return -EPERM;
1965 }
1966 if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
Yan, Zheng76271512016-02-03 21:24:49 +08001967 dout("ceph_pool_perm_check pool %lld no write perm\n",
Yan, Zheng10183a62015-04-27 15:33:28 +08001968 pool);
1969 return -EPERM;
1970 }
1971 return 0;
1972 }
1973
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001974 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
1975 ret = __ceph_pool_perm_get(ci, pool, pool_ns);
1976 ceph_put_string(pool_ns);
Yan, Zheng10183a62015-04-27 15:33:28 +08001977 if (ret < 0)
1978 return ret;
1979
1980 flags = CEPH_I_POOL_PERM;
1981 if (ret & POOL_READ)
1982 flags |= CEPH_I_POOL_RD;
1983 if (ret & POOL_WRITE)
1984 flags |= CEPH_I_POOL_WR;
1985
1986 spin_lock(&ci->i_ceph_lock);
Yan, Zheng779fe0f2016-03-07 09:35:06 +08001987 if (pool == ci->i_layout.pool_id &&
1988 pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
1989 ci->i_ceph_flags |= flags;
Yan, Zheng10183a62015-04-27 15:33:28 +08001990 } else {
Yan, Zheng76271512016-02-03 21:24:49 +08001991 pool = ci->i_layout.pool_id;
Yan, Zheng10183a62015-04-27 15:33:28 +08001992 flags = ci->i_ceph_flags;
1993 }
1994 spin_unlock(&ci->i_ceph_lock);
1995 goto check;
1996}
1997
1998void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
1999{
2000 struct ceph_pool_perm *perm;
2001 struct rb_node *n;
2002
2003 while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
2004 n = rb_first(&mdsc->pool_perm_tree);
2005 perm = rb_entry(n, struct ceph_pool_perm, node);
2006 rb_erase(n, &mdsc->pool_perm_tree);
2007 kfree(perm);
2008 }
2009}