blob: ca4909baf5e6ca36a854013a054f1c1bbce61661 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells31143d52007-05-09 02:33:46 -07002/* handling of writes to regular files and writing back to the server
3 *
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
David Howells31143d52007-05-09 02:33:46 -07006 */
David Howells4343d002017-11-02 15:27:52 +00007
Alexey Dobriyan4af3c9c2007-10-16 23:29:23 -07008#include <linux/backing-dev.h>
David Howells31143d52007-05-09 02:33:46 -07009#include <linux/slab.h>
10#include <linux/fs.h>
11#include <linux/pagemap.h>
12#include <linux/writeback.h>
13#include <linux/pagevec.h>
David Howells3003bbd2020-02-06 14:22:29 +000014#include <linux/netfs.h>
15#include <linux/fscache.h>
David Howells31143d52007-05-09 02:33:46 -070016#include "internal.h"
17
David Howells31143d52007-05-09 02:33:46 -070018/*
19 * mark a page as having been made dirty and thus needing writeback
20 */
21int afs_set_page_dirty(struct page *page)
22{
23 _enter("");
24 return __set_page_dirty_nobuffers(page);
25}
26
27/*
David Howells31143d52007-05-09 02:33:46 -070028 * prepare to perform part of a write to a page
David Howells31143d52007-05-09 02:33:46 -070029 */
Nick Piggin15b46502008-10-15 22:04:32 -070030int afs_write_begin(struct file *file, struct address_space *mapping,
31 loff_t pos, unsigned len, unsigned flags,
David Howells21db2cd2020-10-22 14:03:03 +010032 struct page **_page, void **fsdata)
David Howells31143d52007-05-09 02:33:46 -070033{
Al Viro496ad9a2013-01-23 17:07:38 -050034 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
David Howells78525c72021-08-11 09:49:13 +010035 struct folio *folio;
David Howells4343d002017-11-02 15:27:52 +000036 unsigned long priv;
David Howellse87b03f2020-10-20 09:33:45 +010037 unsigned f, from;
38 unsigned t, to;
39 pgoff_t index;
David Howells31143d52007-05-09 02:33:46 -070040 int ret;
41
David Howellse87b03f2020-10-20 09:33:45 +010042 _enter("{%llx:%llu},%llx,%x",
43 vnode->fid.vid, vnode->fid.vnode, pos, len);
David Howells31143d52007-05-09 02:33:46 -070044
David Howells3003bbd2020-02-06 14:22:29 +000045 /* Prefetch area to be written into the cache if we're caching this
46 * file. We need to do this before we get a lock on the page in case
47 * there's more than one writer competing for the same cache block.
48 */
David Howells78525c72021-08-11 09:49:13 +010049 ret = netfs_write_begin(file, mapping, pos, len, flags, &folio, fsdata,
David Howells3003bbd2020-02-06 14:22:29 +000050 &afs_req_ops, NULL);
51 if (ret < 0)
52 return ret;
David Howells630f5dd2020-02-06 14:22:28 +000053
David Howells78525c72021-08-11 09:49:13 +010054 index = folio_index(folio);
David Howellse87b03f2020-10-20 09:33:45 +010055 from = pos - index * PAGE_SIZE;
56 to = from + len;
57
David Howells31143d52007-05-09 02:33:46 -070058try_again:
David Howells4343d002017-11-02 15:27:52 +000059 /* See if this page is already partially written in a way that we can
60 * merge the new write with.
61 */
David Howells78525c72021-08-11 09:49:13 +010062 if (folio_test_private(folio)) {
63 priv = (unsigned long)folio_get_private(folio);
64 f = afs_folio_dirty_from(folio, priv);
65 t = afs_folio_dirty_to(folio, priv);
David Howells4343d002017-11-02 15:27:52 +000066 ASSERTCMP(f, <=, t);
David Howells31143d52007-05-09 02:33:46 -070067
David Howells78525c72021-08-11 09:49:13 +010068 if (folio_test_writeback(folio)) {
69 trace_afs_folio_dirty(vnode, tracepoint_string("alrdy"), folio);
David Howells5a039c32017-11-18 00:13:30 +000070 goto flush_conflicting_write;
71 }
David Howells5a813272018-04-06 14:17:26 +010072 /* If the file is being filled locally, allow inter-write
73 * spaces to be merged into writes. If it's not, only write
74 * back what the user gives us.
75 */
76 if (!test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags) &&
77 (to < f || from > t))
David Howells4343d002017-11-02 15:27:52 +000078 goto flush_conflicting_write;
David Howells31143d52007-05-09 02:33:46 -070079 }
80
David Howells78525c72021-08-11 09:49:13 +010081 *_page = &folio->page;
David Howells4343d002017-11-02 15:27:52 +000082 _leave(" = 0");
David Howells31143d52007-05-09 02:33:46 -070083 return 0;
84
David Howells4343d002017-11-02 15:27:52 +000085 /* The previous write and this write aren't adjacent or overlapping, so
86 * flush the page out.
87 */
88flush_conflicting_write:
David Howells31143d52007-05-09 02:33:46 -070089 _debug("flush conflict");
David Howells78525c72021-08-11 09:49:13 +010090 ret = folio_write_one(folio);
David Howells21db2cd2020-10-22 14:03:03 +010091 if (ret < 0)
92 goto error;
David Howells31143d52007-05-09 02:33:46 -070093
David Howells78525c72021-08-11 09:49:13 +010094 ret = folio_lock_killable(folio);
David Howells21db2cd2020-10-22 14:03:03 +010095 if (ret < 0)
96 goto error;
David Howells31143d52007-05-09 02:33:46 -070097 goto try_again;
David Howells21db2cd2020-10-22 14:03:03 +010098
99error:
David Howells78525c72021-08-11 09:49:13 +0100100 folio_put(folio);
David Howells21db2cd2020-10-22 14:03:03 +0100101 _leave(" = %d", ret);
102 return ret;
David Howells31143d52007-05-09 02:33:46 -0700103}
104
105/*
106 * finalise part of a write to a page
107 */
Nick Piggin15b46502008-10-15 22:04:32 -0700108int afs_write_end(struct file *file, struct address_space *mapping,
109 loff_t pos, unsigned len, unsigned copied,
David Howells78525c72021-08-11 09:49:13 +0100110 struct page *subpage, void *fsdata)
David Howells31143d52007-05-09 02:33:46 -0700111{
David Howells78525c72021-08-11 09:49:13 +0100112 struct folio *folio = page_folio(subpage);
Al Viro496ad9a2013-01-23 17:07:38 -0500113 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
David Howellsf792e3a2020-10-26 14:05:33 +0000114 unsigned long priv;
David Howells78525c72021-08-11 09:49:13 +0100115 unsigned int f, from = offset_in_folio(folio, pos);
David Howellsf792e3a2020-10-26 14:05:33 +0000116 unsigned int t, to = from + copied;
David Howells31143d52007-05-09 02:33:46 -0700117 loff_t i_size, maybe_i_size;
118
David Howells3b6492d2018-10-20 00:57:57 +0100119 _enter("{%llx:%llu},{%lx}",
David Howells78525c72021-08-11 09:49:13 +0100120 vnode->fid.vid, vnode->fid.vnode, folio_index(folio));
David Howells31143d52007-05-09 02:33:46 -0700121
David Howells78525c72021-08-11 09:49:13 +0100122 if (!folio_test_uptodate(folio)) {
David Howells66e9c6a2021-06-14 14:13:41 +0100123 if (copied < len) {
124 copied = 0;
125 goto out;
126 }
127
David Howells78525c72021-08-11 09:49:13 +0100128 folio_mark_uptodate(folio);
David Howells66e9c6a2021-06-14 14:13:41 +0100129 }
130
David Howells3ad216e2020-11-14 17:27:57 +0000131 if (copied == 0)
132 goto out;
133
Nick Piggin15b46502008-10-15 22:04:32 -0700134 maybe_i_size = pos + copied;
David Howells31143d52007-05-09 02:33:46 -0700135
136 i_size = i_size_read(&vnode->vfs_inode);
137 if (maybe_i_size > i_size) {
David Howells1f32ef72020-06-12 23:58:51 +0100138 write_seqlock(&vnode->cb_lock);
David Howells31143d52007-05-09 02:33:46 -0700139 i_size = i_size_read(&vnode->vfs_inode);
140 if (maybe_i_size > i_size)
David Howells9d37e1c2021-09-08 21:55:19 +0100141 afs_set_i_size(vnode, maybe_i_size);
David Howells1f32ef72020-06-12 23:58:51 +0100142 write_sequnlock(&vnode->cb_lock);
David Howells31143d52007-05-09 02:33:46 -0700143 }
144
David Howells78525c72021-08-11 09:49:13 +0100145 if (folio_test_private(folio)) {
146 priv = (unsigned long)folio_get_private(folio);
147 f = afs_folio_dirty_from(folio, priv);
148 t = afs_folio_dirty_to(folio, priv);
David Howellsf792e3a2020-10-26 14:05:33 +0000149 if (from < f)
150 f = from;
151 if (to > t)
152 t = to;
David Howells78525c72021-08-11 09:49:13 +0100153 priv = afs_folio_dirty(folio, f, t);
154 folio_change_private(folio, (void *)priv);
155 trace_afs_folio_dirty(vnode, tracepoint_string("dirty+"), folio);
David Howellsf792e3a2020-10-26 14:05:33 +0000156 } else {
David Howells78525c72021-08-11 09:49:13 +0100157 priv = afs_folio_dirty(folio, from, to);
158 folio_attach_private(folio, (void *)priv);
159 trace_afs_folio_dirty(vnode, tracepoint_string("dirty"), folio);
David Howellsf792e3a2020-10-26 14:05:33 +0000160 }
161
David Howells78525c72021-08-11 09:49:13 +0100162 if (folio_mark_dirty(folio))
163 _debug("dirtied %lx", folio_index(folio));
David Howellsafae4572018-01-02 10:02:19 +0000164
165out:
David Howells78525c72021-08-11 09:49:13 +0100166 folio_unlock(folio);
167 folio_put(folio);
David Howells3003bbd2020-02-06 14:22:29 +0000168 return copied;
David Howells31143d52007-05-09 02:33:46 -0700169}
170
171/*
172 * kill all the pages in the given range
173 */
David Howells4343d002017-11-02 15:27:52 +0000174static void afs_kill_pages(struct address_space *mapping,
David Howellse87b03f2020-10-20 09:33:45 +0100175 loff_t start, loff_t len)
David Howells31143d52007-05-09 02:33:46 -0700176{
David Howells4343d002017-11-02 15:27:52 +0000177 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
David Howells78525c72021-08-11 09:49:13 +0100178 struct folio *folio;
179 pgoff_t index = start / PAGE_SIZE;
180 pgoff_t last = (start + len - 1) / PAGE_SIZE, next;
David Howells31143d52007-05-09 02:33:46 -0700181
David Howellse87b03f2020-10-20 09:33:45 +0100182 _enter("{%llx:%llu},%llx @%llx",
183 vnode->fid.vid, vnode->fid.vnode, len, start);
David Howells31143d52007-05-09 02:33:46 -0700184
David Howells31143d52007-05-09 02:33:46 -0700185 do {
David Howells78525c72021-08-11 09:49:13 +0100186 _debug("kill %lx (to %lx)", index, last);
David Howells31143d52007-05-09 02:33:46 -0700187
David Howells78525c72021-08-11 09:49:13 +0100188 folio = filemap_get_folio(mapping, index);
189 if (!folio) {
190 next = index + 1;
191 continue;
David Howells31143d52007-05-09 02:33:46 -0700192 }
193
David Howells78525c72021-08-11 09:49:13 +0100194 next = folio_next_index(folio);
195
196 folio_clear_uptodate(folio);
197 folio_end_writeback(folio);
198 folio_lock(folio);
199 generic_error_remove_page(mapping, &folio->page);
200 folio_unlock(folio);
201 folio_put(folio);
202
203 } while (index = next, index <= last);
David Howells31143d52007-05-09 02:33:46 -0700204
205 _leave("");
206}
207
208/*
David Howells4343d002017-11-02 15:27:52 +0000209 * Redirty all the pages in a given range.
David Howells31143d52007-05-09 02:33:46 -0700210 */
David Howells4343d002017-11-02 15:27:52 +0000211static void afs_redirty_pages(struct writeback_control *wbc,
212 struct address_space *mapping,
David Howellse87b03f2020-10-20 09:33:45 +0100213 loff_t start, loff_t len)
David Howells31143d52007-05-09 02:33:46 -0700214{
David Howells4343d002017-11-02 15:27:52 +0000215 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
David Howells78525c72021-08-11 09:49:13 +0100216 struct folio *folio;
217 pgoff_t index = start / PAGE_SIZE;
218 pgoff_t last = (start + len - 1) / PAGE_SIZE, next;
David Howells4343d002017-11-02 15:27:52 +0000219
David Howellse87b03f2020-10-20 09:33:45 +0100220 _enter("{%llx:%llu},%llx @%llx",
221 vnode->fid.vid, vnode->fid.vnode, len, start);
David Howells4343d002017-11-02 15:27:52 +0000222
David Howells4343d002017-11-02 15:27:52 +0000223 do {
David Howellse87b03f2020-10-20 09:33:45 +0100224 _debug("redirty %llx @%llx", len, start);
David Howells4343d002017-11-02 15:27:52 +0000225
David Howells78525c72021-08-11 09:49:13 +0100226 folio = filemap_get_folio(mapping, index);
227 if (!folio) {
228 next = index + 1;
229 continue;
David Howells31143d52007-05-09 02:33:46 -0700230 }
231
David Howells78525c72021-08-11 09:49:13 +0100232 next = index + folio_nr_pages(folio);
233 folio_redirty_for_writepage(wbc, folio);
234 folio_end_writeback(folio);
235 folio_put(folio);
236 } while (index = next, index <= last);
David Howells31143d52007-05-09 02:33:46 -0700237
238 _leave("");
239}
240
241/*
David Howellsa58823a2019-05-09 15:16:10 +0100242 * completion of write to server
243 */
David Howellse87b03f2020-10-20 09:33:45 +0100244static void afs_pages_written_back(struct afs_vnode *vnode, loff_t start, unsigned int len)
David Howellsa58823a2019-05-09 15:16:10 +0100245{
David Howellsbd80d8a2020-02-06 14:22:28 +0000246 struct address_space *mapping = vnode->vfs_inode.i_mapping;
David Howells78525c72021-08-11 09:49:13 +0100247 struct folio *folio;
David Howellse87b03f2020-10-20 09:33:45 +0100248 pgoff_t end;
David Howellsbd80d8a2020-02-06 14:22:28 +0000249
David Howellse87b03f2020-10-20 09:33:45 +0100250 XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE);
David Howellsa58823a2019-05-09 15:16:10 +0100251
David Howellse87b03f2020-10-20 09:33:45 +0100252 _enter("{%llx:%llu},{%x @%llx}",
253 vnode->fid.vid, vnode->fid.vnode, len, start);
David Howellsa58823a2019-05-09 15:16:10 +0100254
David Howellsbd80d8a2020-02-06 14:22:28 +0000255 rcu_read_lock();
David Howellsa58823a2019-05-09 15:16:10 +0100256
David Howellse87b03f2020-10-20 09:33:45 +0100257 end = (start + len - 1) / PAGE_SIZE;
David Howells78525c72021-08-11 09:49:13 +0100258 xas_for_each(&xas, folio, end) {
259 if (!folio_test_writeback(folio)) {
260 kdebug("bad %x @%llx page %lx %lx",
261 len, start, folio_index(folio), end);
262 ASSERT(folio_test_writeback(folio));
David Howellse87b03f2020-10-20 09:33:45 +0100263 }
David Howellsa58823a2019-05-09 15:16:10 +0100264
David Howells78525c72021-08-11 09:49:13 +0100265 trace_afs_folio_dirty(vnode, tracepoint_string("clear"), folio);
266 folio_detach_private(folio);
267 folio_end_writeback(folio);
David Howellsbd80d8a2020-02-06 14:22:28 +0000268 }
David Howellsa58823a2019-05-09 15:16:10 +0100269
David Howellsbd80d8a2020-02-06 14:22:28 +0000270 rcu_read_unlock();
David Howellsa58823a2019-05-09 15:16:10 +0100271
272 afs_prune_wb_keys(vnode);
273 _leave("");
274}
275
276/*
David Howellse49c7b22020-04-10 20:51:51 +0100277 * Find a key to use for the writeback. We cached the keys used to author the
278 * writes on the vnode. *_wbk will contain the last writeback key used or NULL
279 * and we need to start from there if it's set.
280 */
281static int afs_get_writeback_key(struct afs_vnode *vnode,
282 struct afs_wb_key **_wbk)
283{
284 struct afs_wb_key *wbk = NULL;
285 struct list_head *p;
286 int ret = -ENOKEY, ret2;
287
288 spin_lock(&vnode->wb_lock);
289 if (*_wbk)
290 p = (*_wbk)->vnode_link.next;
291 else
292 p = vnode->wb_keys.next;
293
294 while (p != &vnode->wb_keys) {
295 wbk = list_entry(p, struct afs_wb_key, vnode_link);
296 _debug("wbk %u", key_serial(wbk->key));
297 ret2 = key_validate(wbk->key);
298 if (ret2 == 0) {
299 refcount_inc(&wbk->usage);
300 _debug("USE WB KEY %u", key_serial(wbk->key));
301 break;
302 }
303
304 wbk = NULL;
305 if (ret == -ENOKEY)
306 ret = ret2;
307 p = p->next;
308 }
309
310 spin_unlock(&vnode->wb_lock);
311 if (*_wbk)
312 afs_put_wb_key(*_wbk);
313 *_wbk = wbk;
314 return 0;
315}
316
317static void afs_store_data_success(struct afs_operation *op)
318{
319 struct afs_vnode *vnode = op->file[0].vnode;
320
David Howellsda8d0752020-06-13 19:34:59 +0100321 op->ctime = op->file[0].scb.status.mtime_client;
David Howellse49c7b22020-04-10 20:51:51 +0100322 afs_vnode_commit_status(op, &op->file[0]);
323 if (op->error == 0) {
David Howellsd383e342020-10-22 14:40:31 +0100324 if (!op->store.laundering)
David Howellse87b03f2020-10-20 09:33:45 +0100325 afs_pages_written_back(vnode, op->store.pos, op->store.size);
David Howellse49c7b22020-04-10 20:51:51 +0100326 afs_stat_v(vnode, n_stores);
David Howellsbd80d8a2020-02-06 14:22:28 +0000327 atomic_long_add(op->store.size, &afs_v2net(vnode)->n_store_bytes);
David Howellse49c7b22020-04-10 20:51:51 +0100328 }
329}
330
331static const struct afs_operation_ops afs_store_data_operation = {
332 .issue_afs_rpc = afs_fs_store_data,
333 .issue_yfs_rpc = yfs_fs_store_data,
334 .success = afs_store_data_success,
335};
336
337/*
David Howellsd2ddc772017-11-02 15:27:50 +0000338 * write to a file
339 */
David Howellse87b03f2020-10-20 09:33:45 +0100340static int afs_store_data(struct afs_vnode *vnode, struct iov_iter *iter, loff_t pos,
David Howellsbd80d8a2020-02-06 14:22:28 +0000341 bool laundering)
David Howellsd2ddc772017-11-02 15:27:50 +0000342{
David Howellse49c7b22020-04-10 20:51:51 +0100343 struct afs_operation *op;
David Howells4343d002017-11-02 15:27:52 +0000344 struct afs_wb_key *wbk = NULL;
David Howellsbd80d8a2020-02-06 14:22:28 +0000345 loff_t size = iov_iter_count(iter), i_size;
346 int ret = -ENOKEY;
David Howellsd2ddc772017-11-02 15:27:50 +0000347
David Howellsbd80d8a2020-02-06 14:22:28 +0000348 _enter("%s{%llx:%llu.%u},%llx,%llx",
David Howellsd2ddc772017-11-02 15:27:50 +0000349 vnode->volume->name,
350 vnode->fid.vid,
351 vnode->fid.vnode,
352 vnode->fid.unique,
David Howellsbd80d8a2020-02-06 14:22:28 +0000353 size, pos);
David Howellsd2ddc772017-11-02 15:27:50 +0000354
David Howellse49c7b22020-04-10 20:51:51 +0100355 ret = afs_get_writeback_key(vnode, &wbk);
356 if (ret) {
357 _leave(" = %d [no keys]", ret);
358 return ret;
359 }
360
361 op = afs_alloc_operation(wbk->key, vnode->volume);
362 if (IS_ERR(op)) {
363 afs_put_wb_key(wbk);
David Howellsa58823a2019-05-09 15:16:10 +0100364 return -ENOMEM;
David Howellse49c7b22020-04-10 20:51:51 +0100365 }
David Howellsa58823a2019-05-09 15:16:10 +0100366
David Howellsbd80d8a2020-02-06 14:22:28 +0000367 i_size = i_size_read(&vnode->vfs_inode);
368
David Howellse49c7b22020-04-10 20:51:51 +0100369 afs_op_set_vnode(op, 0, vnode);
370 op->file[0].dv_delta = 1;
David Howells22650f12021-04-30 13:47:08 +0100371 op->file[0].modification = true;
David Howellsbd80d8a2020-02-06 14:22:28 +0000372 op->store.write_iter = iter;
373 op->store.pos = pos;
David Howellsbd80d8a2020-02-06 14:22:28 +0000374 op->store.size = size;
375 op->store.i_size = max(pos + size, i_size);
David Howellsd383e342020-10-22 14:40:31 +0100376 op->store.laundering = laundering;
David Howellsb3597942020-06-11 21:50:24 +0100377 op->mtime = vnode->vfs_inode.i_mtime;
David Howells811f04b2020-07-08 09:27:07 +0100378 op->flags |= AFS_OPERATION_UNINTR;
David Howellse49c7b22020-04-10 20:51:51 +0100379 op->ops = &afs_store_data_operation;
David Howells4343d002017-11-02 15:27:52 +0000380
David Howells4343d002017-11-02 15:27:52 +0000381try_next_key:
David Howellse49c7b22020-04-10 20:51:51 +0100382 afs_begin_vnode_operation(op);
383 afs_wait_for_operation(op);
David Howells4343d002017-11-02 15:27:52 +0000384
David Howellse49c7b22020-04-10 20:51:51 +0100385 switch (op->error) {
David Howells4343d002017-11-02 15:27:52 +0000386 case -EACCES:
387 case -EPERM:
388 case -ENOKEY:
389 case -EKEYEXPIRED:
390 case -EKEYREJECTED:
391 case -EKEYREVOKED:
392 _debug("next");
David Howellse49c7b22020-04-10 20:51:51 +0100393
394 ret = afs_get_writeback_key(vnode, &wbk);
395 if (ret == 0) {
396 key_put(op->key);
397 op->key = key_get(wbk->key);
398 goto try_next_key;
399 }
400 break;
David Howells4343d002017-11-02 15:27:52 +0000401 }
402
403 afs_put_wb_key(wbk);
David Howellse49c7b22020-04-10 20:51:51 +0100404 _leave(" = %d", op->error);
405 return afs_put_operation(op);
David Howellsd2ddc772017-11-02 15:27:50 +0000406}
407
408/*
David Howells810caa32020-10-30 10:01:09 +0000409 * Extend the region to be written back to include subsequent contiguously
410 * dirty pages if possible, but don't sleep while doing so.
411 *
412 * If this page holds new content, then we can include filler zeros in the
413 * writeback.
David Howells31143d52007-05-09 02:33:46 -0700414 */
David Howells810caa32020-10-30 10:01:09 +0000415static void afs_extend_writeback(struct address_space *mapping,
416 struct afs_vnode *vnode,
417 long *_count,
David Howellse87b03f2020-10-20 09:33:45 +0100418 loff_t start,
419 loff_t max_len,
420 bool new_content,
421 unsigned int *_len)
David Howells31143d52007-05-09 02:33:46 -0700422{
David Howellse87b03f2020-10-20 09:33:45 +0100423 struct pagevec pvec;
David Howells78525c72021-08-11 09:49:13 +0100424 struct folio *folio;
David Howellse87b03f2020-10-20 09:33:45 +0100425 unsigned long priv;
426 unsigned int psize, filler = 0;
427 unsigned int f, t;
428 loff_t len = *_len;
429 pgoff_t index = (start + len) / PAGE_SIZE;
430 bool stop = true;
431 unsigned int i;
David Howells4343d002017-11-02 15:27:52 +0000432
David Howellse87b03f2020-10-20 09:33:45 +0100433 XA_STATE(xas, &mapping->i_pages, index);
434 pagevec_init(&pvec);
435
David Howells31143d52007-05-09 02:33:46 -0700436 do {
David Howellse87b03f2020-10-20 09:33:45 +0100437 /* Firstly, we gather up a batch of contiguous dirty pages
438 * under the RCU read lock - but we can't clear the dirty flags
439 * there if any of those pages are mapped.
440 */
441 rcu_read_lock();
David Howells31143d52007-05-09 02:33:46 -0700442
David Howells78525c72021-08-11 09:49:13 +0100443 xas_for_each(&xas, folio, ULONG_MAX) {
David Howellse87b03f2020-10-20 09:33:45 +0100444 stop = true;
David Howells78525c72021-08-11 09:49:13 +0100445 if (xas_retry(&xas, folio))
David Howellse87b03f2020-10-20 09:33:45 +0100446 continue;
David Howells78525c72021-08-11 09:49:13 +0100447 if (xa_is_value(folio))
David Howells5a813272018-04-06 14:17:26 +0100448 break;
David Howells78525c72021-08-11 09:49:13 +0100449 if (folio_index(folio) != index)
David Howells31143d52007-05-09 02:33:46 -0700450 break;
David Howellse87b03f2020-10-20 09:33:45 +0100451
David Howells78525c72021-08-11 09:49:13 +0100452 if (!folio_try_get_rcu(folio)) {
David Howellse87b03f2020-10-20 09:33:45 +0100453 xas_reset(&xas);
454 continue;
455 }
456
457 /* Has the page moved or been split? */
David Howells78525c72021-08-11 09:49:13 +0100458 if (unlikely(folio != xas_reload(&xas))) {
459 folio_put(folio);
David Howellse87b03f2020-10-20 09:33:45 +0100460 break;
David Howells581b2022021-09-01 09:15:21 +0100461 }
David Howellse87b03f2020-10-20 09:33:45 +0100462
David Howells78525c72021-08-11 09:49:13 +0100463 if (!folio_trylock(folio)) {
464 folio_put(folio);
David Howells31143d52007-05-09 02:33:46 -0700465 break;
David Howells581b2022021-09-01 09:15:21 +0100466 }
David Howells78525c72021-08-11 09:49:13 +0100467 if (!folio_test_dirty(folio) || folio_test_writeback(folio)) {
468 folio_unlock(folio);
469 folio_put(folio);
David Howells31143d52007-05-09 02:33:46 -0700470 break;
471 }
David Howells4343d002017-11-02 15:27:52 +0000472
David Howells78525c72021-08-11 09:49:13 +0100473 psize = folio_size(folio);
474 priv = (unsigned long)folio_get_private(folio);
475 f = afs_folio_dirty_from(folio, priv);
476 t = afs_folio_dirty_to(folio, priv);
David Howells810caa32020-10-30 10:01:09 +0000477 if (f != 0 && !new_content) {
David Howells78525c72021-08-11 09:49:13 +0100478 folio_unlock(folio);
479 folio_put(folio);
David Howells4343d002017-11-02 15:27:52 +0000480 break;
481 }
David Howells4343d002017-11-02 15:27:52 +0000482
David Howellse87b03f2020-10-20 09:33:45 +0100483 len += filler + t;
484 filler = psize - t;
485 if (len >= max_len || *_count <= 0)
486 stop = true;
487 else if (t == psize || new_content)
488 stop = false;
489
David Howells78525c72021-08-11 09:49:13 +0100490 index += folio_nr_pages(folio);
491 if (!pagevec_add(&pvec, &folio->page))
David Howellse87b03f2020-10-20 09:33:45 +0100492 break;
493 if (stop)
494 break;
495 }
496
497 if (!stop)
498 xas_pause(&xas);
499 rcu_read_unlock();
500
501 /* Now, if we obtained any pages, we can shift them to being
502 * writable and mark them for caching.
503 */
504 if (!pagevec_count(&pvec))
505 break;
506
507 for (i = 0; i < pagevec_count(&pvec); i++) {
David Howells78525c72021-08-11 09:49:13 +0100508 folio = page_folio(pvec.pages[i]);
509 trace_afs_folio_dirty(vnode, tracepoint_string("store+"), folio);
David Howells13524ab2017-11-02 15:27:53 +0000510
David Howells78525c72021-08-11 09:49:13 +0100511 if (!folio_clear_dirty_for_io(folio))
David Howells31143d52007-05-09 02:33:46 -0700512 BUG();
David Howells78525c72021-08-11 09:49:13 +0100513 if (folio_start_writeback(folio))
David Howells31143d52007-05-09 02:33:46 -0700514 BUG();
David Howellse87b03f2020-10-20 09:33:45 +0100515
David Howells78525c72021-08-11 09:49:13 +0100516 *_count -= folio_nr_pages(folio);
517 folio_unlock(folio);
David Howells31143d52007-05-09 02:33:46 -0700518 }
519
David Howellse87b03f2020-10-20 09:33:45 +0100520 pagevec_release(&pvec);
521 cond_resched();
522 } while (!stop);
David Howells31143d52007-05-09 02:33:46 -0700523
David Howellse87b03f2020-10-20 09:33:45 +0100524 *_len = len;
David Howells810caa32020-10-30 10:01:09 +0000525}
526
527/*
528 * Synchronously write back the locked page and any subsequent non-locked dirty
529 * pages.
530 */
David Howells78525c72021-08-11 09:49:13 +0100531static ssize_t afs_write_back_from_locked_folio(struct address_space *mapping,
532 struct writeback_control *wbc,
533 struct folio *folio,
534 loff_t start, loff_t end)
David Howells810caa32020-10-30 10:01:09 +0000535{
536 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
537 struct iov_iter iter;
David Howellse87b03f2020-10-20 09:33:45 +0100538 unsigned long priv;
539 unsigned int offset, to, len, max_len;
540 loff_t i_size = i_size_read(&vnode->vfs_inode);
David Howells810caa32020-10-30 10:01:09 +0000541 bool new_content = test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
David Howellse87b03f2020-10-20 09:33:45 +0100542 long count = wbc->nr_to_write;
David Howells810caa32020-10-30 10:01:09 +0000543 int ret;
544
David Howells78525c72021-08-11 09:49:13 +0100545 _enter(",%lx,%llx-%llx", folio_index(folio), start, end);
David Howells810caa32020-10-30 10:01:09 +0000546
David Howells78525c72021-08-11 09:49:13 +0100547 if (folio_start_writeback(folio))
David Howells810caa32020-10-30 10:01:09 +0000548 BUG();
549
David Howells78525c72021-08-11 09:49:13 +0100550 count -= folio_nr_pages(folio);
David Howellse87b03f2020-10-20 09:33:45 +0100551
David Howells810caa32020-10-30 10:01:09 +0000552 /* Find all consecutive lockable dirty pages that have contiguous
553 * written regions, stopping when we find a page that is not
554 * immediately lockable, is not dirty or is missing, or we reach the
555 * end of the range.
556 */
David Howells78525c72021-08-11 09:49:13 +0100557 priv = (unsigned long)folio_get_private(folio);
558 offset = afs_folio_dirty_from(folio, priv);
559 to = afs_folio_dirty_to(folio, priv);
560 trace_afs_folio_dirty(vnode, tracepoint_string("store"), folio);
David Howells810caa32020-10-30 10:01:09 +0000561
David Howellse87b03f2020-10-20 09:33:45 +0100562 len = to - offset;
563 start += offset;
564 if (start < i_size) {
565 /* Trim the write to the EOF; the extra data is ignored. Also
566 * put an upper limit on the size of a single storedata op.
567 */
568 max_len = 65536 * 4096;
569 max_len = min_t(unsigned long long, max_len, end - start + 1);
570 max_len = min_t(unsigned long long, max_len, i_size - start);
David Howells810caa32020-10-30 10:01:09 +0000571
David Howellse87b03f2020-10-20 09:33:45 +0100572 if (len < max_len &&
David Howells78525c72021-08-11 09:49:13 +0100573 (to == folio_size(folio) || new_content))
David Howellse87b03f2020-10-20 09:33:45 +0100574 afs_extend_writeback(mapping, vnode, &count,
575 start, max_len, new_content, &len);
576 len = min_t(loff_t, len, max_len);
577 }
David Howells810caa32020-10-30 10:01:09 +0000578
David Howells4343d002017-11-02 15:27:52 +0000579 /* We now have a contiguous set of dirty pages, each with writeback
580 * set; the first page is still locked at this point, but all the rest
581 * have been unlocked.
582 */
David Howells78525c72021-08-11 09:49:13 +0100583 folio_unlock(folio);
David Howells4343d002017-11-02 15:27:52 +0000584
David Howellse87b03f2020-10-20 09:33:45 +0100585 if (start < i_size) {
586 _debug("write back %x @%llx [%llx]", len, start, i_size);
David Howells31143d52007-05-09 02:33:46 -0700587
David Howellse87b03f2020-10-20 09:33:45 +0100588 iov_iter_xarray(&iter, WRITE, &mapping->i_pages, start, len);
589 ret = afs_store_data(vnode, &iter, start, false);
David Howellsbd80d8a2020-02-06 14:22:28 +0000590 } else {
David Howellse87b03f2020-10-20 09:33:45 +0100591 _debug("write discard %x @%llx [%llx]", len, start, i_size);
592
David Howellsbd80d8a2020-02-06 14:22:28 +0000593 /* The dirty region was entirely beyond the EOF. */
David Howellse87b03f2020-10-20 09:33:45 +0100594 afs_pages_written_back(vnode, start, len);
David Howellsbd80d8a2020-02-06 14:22:28 +0000595 ret = 0;
596 }
597
David Howells4343d002017-11-02 15:27:52 +0000598 switch (ret) {
599 case 0:
David Howellse87b03f2020-10-20 09:33:45 +0100600 wbc->nr_to_write = count;
601 ret = len;
David Howells4343d002017-11-02 15:27:52 +0000602 break;
603
604 default:
605 pr_notice("kAFS: Unexpected error from FS.StoreData %d\n", ret);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500606 fallthrough;
David Howells4343d002017-11-02 15:27:52 +0000607 case -EACCES:
608 case -EPERM:
609 case -ENOKEY:
610 case -EKEYEXPIRED:
611 case -EKEYREJECTED:
612 case -EKEYREVOKED:
David Howellse87b03f2020-10-20 09:33:45 +0100613 afs_redirty_pages(wbc, mapping, start, len);
David Howells4343d002017-11-02 15:27:52 +0000614 mapping_set_error(mapping, ret);
615 break;
616
617 case -EDQUOT:
618 case -ENOSPC:
David Howellse87b03f2020-10-20 09:33:45 +0100619 afs_redirty_pages(wbc, mapping, start, len);
David Howells4343d002017-11-02 15:27:52 +0000620 mapping_set_error(mapping, -ENOSPC);
621 break;
622
623 case -EROFS:
624 case -EIO:
625 case -EREMOTEIO:
626 case -EFBIG:
627 case -ENOENT:
628 case -ENOMEDIUM:
629 case -ENXIO:
David Howellsf51375c2018-10-20 00:57:57 +0100630 trace_afs_file_error(vnode, ret, afs_file_error_writeback_fail);
David Howellse87b03f2020-10-20 09:33:45 +0100631 afs_kill_pages(mapping, start, len);
David Howells4343d002017-11-02 15:27:52 +0000632 mapping_set_error(mapping, ret);
633 break;
David Howells31143d52007-05-09 02:33:46 -0700634 }
635
636 _leave(" = %d", ret);
637 return ret;
638}
639
640/*
641 * write a page back to the server
642 * - the caller locked the page for us
643 */
David Howells78525c72021-08-11 09:49:13 +0100644int afs_writepage(struct page *subpage, struct writeback_control *wbc)
David Howells31143d52007-05-09 02:33:46 -0700645{
David Howells78525c72021-08-11 09:49:13 +0100646 struct folio *folio = page_folio(subpage);
David Howellse87b03f2020-10-20 09:33:45 +0100647 ssize_t ret;
648 loff_t start;
David Howells31143d52007-05-09 02:33:46 -0700649
David Howells78525c72021-08-11 09:49:13 +0100650 _enter("{%lx},", folio_index(folio));
David Howells31143d52007-05-09 02:33:46 -0700651
David Howells78525c72021-08-11 09:49:13 +0100652 start = folio_index(folio) * PAGE_SIZE;
653 ret = afs_write_back_from_locked_folio(folio_mapping(folio), wbc,
654 folio, start, LLONG_MAX - start);
David Howells31143d52007-05-09 02:33:46 -0700655 if (ret < 0) {
David Howellse87b03f2020-10-20 09:33:45 +0100656 _leave(" = %zd", ret);
657 return ret;
David Howells31143d52007-05-09 02:33:46 -0700658 }
659
David Howells31143d52007-05-09 02:33:46 -0700660 _leave(" = 0");
661 return 0;
662}
663
664/*
665 * write a region of pages back to the server
666 */
Adrian Bunkc1206a22007-10-16 23:26:41 -0700667static int afs_writepages_region(struct address_space *mapping,
668 struct writeback_control *wbc,
David Howellse87b03f2020-10-20 09:33:45 +0100669 loff_t start, loff_t end, loff_t *_next)
David Howells31143d52007-05-09 02:33:46 -0700670{
David Howells78525c72021-08-11 09:49:13 +0100671 struct folio *folio;
672 struct page *head_page;
David Howellse87b03f2020-10-20 09:33:45 +0100673 ssize_t ret;
674 int n;
David Howells31143d52007-05-09 02:33:46 -0700675
David Howellse87b03f2020-10-20 09:33:45 +0100676 _enter("%llx,%llx,", start, end);
David Howells31143d52007-05-09 02:33:46 -0700677
678 do {
David Howellse87b03f2020-10-20 09:33:45 +0100679 pgoff_t index = start / PAGE_SIZE;
680
681 n = find_get_pages_range_tag(mapping, &index, end / PAGE_SIZE,
David Howells78525c72021-08-11 09:49:13 +0100682 PAGECACHE_TAG_DIRTY, 1, &head_page);
David Howells31143d52007-05-09 02:33:46 -0700683 if (!n)
684 break;
685
David Howells78525c72021-08-11 09:49:13 +0100686 folio = page_folio(head_page);
687 start = folio_pos(folio); /* May regress with THPs */
David Howellse87b03f2020-10-20 09:33:45 +0100688
David Howells78525c72021-08-11 09:49:13 +0100689 _debug("wback %lx", folio_index(folio));
David Howells31143d52007-05-09 02:33:46 -0700690
David Howellse87b03f2020-10-20 09:33:45 +0100691 /* At this point we hold neither the i_pages lock nor the
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700692 * page lock: the page may be truncated or invalidated
693 * (changing page->mapping to NULL), or even swizzled
694 * back from swapper_space to tmpfs file mapping
David Howells31143d52007-05-09 02:33:46 -0700695 */
David Howellse87b03f2020-10-20 09:33:45 +0100696 if (wbc->sync_mode != WB_SYNC_NONE) {
David Howells78525c72021-08-11 09:49:13 +0100697 ret = folio_lock_killable(folio);
David Howellse87b03f2020-10-20 09:33:45 +0100698 if (ret < 0) {
David Howells78525c72021-08-11 09:49:13 +0100699 folio_put(folio);
David Howellse87b03f2020-10-20 09:33:45 +0100700 return ret;
701 }
702 } else {
David Howells78525c72021-08-11 09:49:13 +0100703 if (!folio_trylock(folio)) {
704 folio_put(folio);
David Howellse87b03f2020-10-20 09:33:45 +0100705 return 0;
706 }
David Howells4343d002017-11-02 15:27:52 +0000707 }
David Howells31143d52007-05-09 02:33:46 -0700708
David Howells78525c72021-08-11 09:49:13 +0100709 if (folio_mapping(folio) != mapping ||
710 !folio_test_dirty(folio)) {
711 start += folio_size(folio);
712 folio_unlock(folio);
713 folio_put(folio);
David Howells31143d52007-05-09 02:33:46 -0700714 continue;
715 }
716
David Howells78525c72021-08-11 09:49:13 +0100717 if (folio_test_writeback(folio)) {
718 folio_unlock(folio);
David Howellsc5051c72017-03-16 16:27:49 +0000719 if (wbc->sync_mode != WB_SYNC_NONE)
David Howells78525c72021-08-11 09:49:13 +0100720 folio_wait_writeback(folio);
721 folio_put(folio);
David Howells31143d52007-05-09 02:33:46 -0700722 continue;
723 }
724
David Howells78525c72021-08-11 09:49:13 +0100725 if (!folio_clear_dirty_for_io(folio))
David Howells65a15102017-03-16 16:27:49 +0000726 BUG();
David Howells78525c72021-08-11 09:49:13 +0100727 ret = afs_write_back_from_locked_folio(mapping, wbc, folio, start, end);
728 folio_put(folio);
David Howells31143d52007-05-09 02:33:46 -0700729 if (ret < 0) {
David Howellse87b03f2020-10-20 09:33:45 +0100730 _leave(" = %zd", ret);
David Howells31143d52007-05-09 02:33:46 -0700731 return ret;
732 }
733
Marc Dionnedc255732021-06-06 21:21:27 +0100734 start += ret;
David Howells31143d52007-05-09 02:33:46 -0700735
David Howells31143d52007-05-09 02:33:46 -0700736 cond_resched();
David Howellse87b03f2020-10-20 09:33:45 +0100737 } while (wbc->nr_to_write > 0);
David Howells31143d52007-05-09 02:33:46 -0700738
David Howellse87b03f2020-10-20 09:33:45 +0100739 *_next = start;
740 _leave(" = 0 [%llx]", *_next);
David Howells31143d52007-05-09 02:33:46 -0700741 return 0;
742}
743
744/*
745 * write some of the pending data back to the server
746 */
747int afs_writepages(struct address_space *mapping,
748 struct writeback_control *wbc)
749{
David Howellsec0fa0b2020-10-07 14:22:12 +0100750 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
David Howellse87b03f2020-10-20 09:33:45 +0100751 loff_t start, next;
David Howells31143d52007-05-09 02:33:46 -0700752 int ret;
753
754 _enter("");
755
David Howellsec0fa0b2020-10-07 14:22:12 +0100756 /* We have to be careful as we can end up racing with setattr()
757 * truncating the pagecache since the caller doesn't take a lock here
758 * to prevent it.
759 */
760 if (wbc->sync_mode == WB_SYNC_ALL)
761 down_read(&vnode->validate_lock);
762 else if (!down_read_trylock(&vnode->validate_lock))
763 return 0;
764
David Howells31143d52007-05-09 02:33:46 -0700765 if (wbc->range_cyclic) {
David Howellse87b03f2020-10-20 09:33:45 +0100766 start = mapping->writeback_index * PAGE_SIZE;
767 ret = afs_writepages_region(mapping, wbc, start, LLONG_MAX, &next);
Tom Rixafe69492021-04-30 08:50:31 -0700768 if (ret == 0) {
769 mapping->writeback_index = next / PAGE_SIZE;
770 if (start > 0 && wbc->nr_to_write > 0) {
771 ret = afs_writepages_region(mapping, wbc, 0,
772 start, &next);
773 if (ret == 0)
774 mapping->writeback_index =
775 next / PAGE_SIZE;
776 }
777 }
David Howells31143d52007-05-09 02:33:46 -0700778 } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
David Howellse87b03f2020-10-20 09:33:45 +0100779 ret = afs_writepages_region(mapping, wbc, 0, LLONG_MAX, &next);
Tom Rixafe69492021-04-30 08:50:31 -0700780 if (wbc->nr_to_write > 0 && ret == 0)
David Howells5a972472021-07-12 17:04:47 +0100781 mapping->writeback_index = next / PAGE_SIZE;
David Howells31143d52007-05-09 02:33:46 -0700782 } else {
David Howellse87b03f2020-10-20 09:33:45 +0100783 ret = afs_writepages_region(mapping, wbc,
784 wbc->range_start, wbc->range_end, &next);
David Howells31143d52007-05-09 02:33:46 -0700785 }
786
David Howellsec0fa0b2020-10-07 14:22:12 +0100787 up_read(&vnode->validate_lock);
David Howells31143d52007-05-09 02:33:46 -0700788 _leave(" = %d", ret);
789 return ret;
790}
791
792/*
David Howells31143d52007-05-09 02:33:46 -0700793 * write to an AFS file
794 */
Al Viro50b55512014-04-03 14:13:46 -0400795ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from)
David Howells31143d52007-05-09 02:33:46 -0700796{
Al Viro496ad9a2013-01-23 17:07:38 -0500797 struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
David Howells3978d812021-09-01 19:22:50 +0100798 struct afs_file *af = iocb->ki_filp->private_data;
David Howells31143d52007-05-09 02:33:46 -0700799 ssize_t result;
Al Viro50b55512014-04-03 14:13:46 -0400800 size_t count = iov_iter_count(from);
David Howells31143d52007-05-09 02:33:46 -0700801
David Howells3b6492d2018-10-20 00:57:57 +0100802 _enter("{%llx:%llu},{%zu},",
Al Viro50b55512014-04-03 14:13:46 -0400803 vnode->fid.vid, vnode->fid.vnode, count);
David Howells31143d52007-05-09 02:33:46 -0700804
805 if (IS_SWAPFILE(&vnode->vfs_inode)) {
806 printk(KERN_INFO
807 "AFS: Attempt to write to active swap file!\n");
808 return -EBUSY;
809 }
810
811 if (!count)
812 return 0;
813
David Howells3978d812021-09-01 19:22:50 +0100814 result = afs_validate(vnode, af->key);
815 if (result < 0)
816 return result;
817
Al Viro50b55512014-04-03 14:13:46 -0400818 result = generic_file_write_iter(iocb, from);
David Howells31143d52007-05-09 02:33:46 -0700819
David Howells31143d52007-05-09 02:33:46 -0700820 _leave(" = %zd", result);
821 return result;
822}
823
824/*
David Howells31143d52007-05-09 02:33:46 -0700825 * flush any dirty pages for this process, and check for write errors.
826 * - the return status from this call provides a reliable indication of
827 * whether any write errors occurred for this process.
828 */
Josef Bacik02c24a82011-07-16 20:44:56 -0400829int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
David Howells31143d52007-05-09 02:33:46 -0700830{
David Howells3978d812021-09-01 19:22:50 +0100831 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
832 struct afs_file *af = file->private_data;
833 int ret;
David Howells31143d52007-05-09 02:33:46 -0700834
David Howells3b6492d2018-10-20 00:57:57 +0100835 _enter("{%llx:%llu},{n=%pD},%d",
Al Viro3c981bf2013-09-03 13:37:45 -0400836 vnode->fid.vid, vnode->fid.vnode, file,
David Howells31143d52007-05-09 02:33:46 -0700837 datasync);
838
David Howells3978d812021-09-01 19:22:50 +0100839 ret = afs_validate(vnode, af->key);
840 if (ret < 0)
841 return ret;
842
David Howells4343d002017-11-02 15:27:52 +0000843 return file_write_and_wait_range(file, start, end);
David Howells31143d52007-05-09 02:33:46 -0700844}
David Howells9b3f26c2009-04-03 16:42:41 +0100845
846/*
847 * notification that a previously read-only page is about to become writable
848 * - if it returns an error, the caller will deliver a bus error signal
849 */
Souptick Joarder0722f182018-08-23 17:00:48 -0700850vm_fault_t afs_page_mkwrite(struct vm_fault *vmf)
David Howells9b3f26c2009-04-03 16:42:41 +0100851{
Matthew Wilcox (Oracle)490e0162021-03-04 11:09:17 -0500852 struct folio *folio = page_folio(vmf->page);
David Howells1cf7a152017-11-02 15:27:52 +0000853 struct file *file = vmf->vma->vm_file;
854 struct inode *inode = file_inode(file);
855 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells3978d812021-09-01 19:22:50 +0100856 struct afs_file *af = file->private_data;
David Howells1cf7a152017-11-02 15:27:52 +0000857 unsigned long priv;
Matthew Wilcox (Oracle)9620ad82021-06-16 22:22:28 +0100858 vm_fault_t ret = VM_FAULT_RETRY;
David Howells9b3f26c2009-04-03 16:42:41 +0100859
David Howells78525c72021-08-11 09:49:13 +0100860 _enter("{{%llx:%llu}},{%lx}", vnode->fid.vid, vnode->fid.vnode, folio_index(folio));
David Howells9b3f26c2009-04-03 16:42:41 +0100861
David Howells3978d812021-09-01 19:22:50 +0100862 afs_validate(vnode, af->key);
863
David Howells1cf7a152017-11-02 15:27:52 +0000864 sb_start_pagefault(inode->i_sb);
865
866 /* Wait for the page to be written to the cache before we allow it to
867 * be modified. We then assume the entire page will need writing back.
868 */
David Howells630f5dd2020-02-06 14:22:28 +0000869#ifdef CONFIG_AFS_FSCACHE
David Howells78525c72021-08-11 09:49:13 +0100870 if (folio_test_fscache(folio) &&
871 folio_wait_fscache_killable(folio) < 0)
Matthew Wilcox (Oracle)9620ad82021-06-16 22:22:28 +0100872 goto out;
David Howells630f5dd2020-02-06 14:22:28 +0000873#endif
David Howells9b3f26c2009-04-03 16:42:41 +0100874
Matthew Wilcox (Oracle)490e0162021-03-04 11:09:17 -0500875 if (folio_wait_writeback_killable(folio))
Matthew Wilcox (Oracle)9620ad82021-06-16 22:22:28 +0100876 goto out;
David Howells1cf7a152017-11-02 15:27:52 +0000877
David Howells78525c72021-08-11 09:49:13 +0100878 if (folio_lock_killable(folio) < 0)
Matthew Wilcox (Oracle)9620ad82021-06-16 22:22:28 +0100879 goto out;
David Howells1cf7a152017-11-02 15:27:52 +0000880
David Howells78525c72021-08-11 09:49:13 +0100881 /* We mustn't change folio->private until writeback is complete as that
David Howells1cf7a152017-11-02 15:27:52 +0000882 * details the portion of the page we need to write back and we might
883 * need to redirty the page if there's a problem.
884 */
Matthew Wilcox (Oracle)490e0162021-03-04 11:09:17 -0500885 if (folio_wait_writeback_killable(folio) < 0) {
886 folio_unlock(folio);
Matthew Wilcox (Oracle)9620ad82021-06-16 22:22:28 +0100887 goto out;
David Howells5cbf0392020-02-06 14:22:29 +0000888 }
David Howells1cf7a152017-11-02 15:27:52 +0000889
David Howells78525c72021-08-11 09:49:13 +0100890 priv = afs_folio_dirty(folio, 0, folio_size(folio));
891 priv = afs_folio_dirty_mmapped(priv);
892 if (folio_test_private(folio)) {
893 folio_change_private(folio, (void *)priv);
894 trace_afs_folio_dirty(vnode, tracepoint_string("mkwrite+"), folio);
David Howellse87b03f2020-10-20 09:33:45 +0100895 } else {
David Howells78525c72021-08-11 09:49:13 +0100896 folio_attach_private(folio, (void *)priv);
897 trace_afs_folio_dirty(vnode, tracepoint_string("mkwrite"), folio);
David Howellse87b03f2020-10-20 09:33:45 +0100898 }
David Howellsbb413482020-06-12 00:15:13 +0100899 file_update_time(file);
David Howells1cf7a152017-11-02 15:27:52 +0000900
Matthew Wilcox (Oracle)9620ad82021-06-16 22:22:28 +0100901 ret = VM_FAULT_LOCKED;
902out:
David Howells1cf7a152017-11-02 15:27:52 +0000903 sb_end_pagefault(inode->i_sb);
Matthew Wilcox (Oracle)9620ad82021-06-16 22:22:28 +0100904 return ret;
David Howells9b3f26c2009-04-03 16:42:41 +0100905}
David Howells4343d002017-11-02 15:27:52 +0000906
907/*
908 * Prune the keys cached for writeback. The caller must hold vnode->wb_lock.
909 */
910void afs_prune_wb_keys(struct afs_vnode *vnode)
911{
912 LIST_HEAD(graveyard);
913 struct afs_wb_key *wbk, *tmp;
914
915 /* Discard unused keys */
916 spin_lock(&vnode->wb_lock);
917
918 if (!mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_WRITEBACK) &&
919 !mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_DIRTY)) {
920 list_for_each_entry_safe(wbk, tmp, &vnode->wb_keys, vnode_link) {
921 if (refcount_read(&wbk->usage) == 1)
922 list_move(&wbk->vnode_link, &graveyard);
923 }
924 }
925
926 spin_unlock(&vnode->wb_lock);
927
928 while (!list_empty(&graveyard)) {
929 wbk = list_entry(graveyard.next, struct afs_wb_key, vnode_link);
930 list_del(&wbk->vnode_link);
931 afs_put_wb_key(wbk);
932 }
933}
934
935/*
936 * Clean up a page during invalidation.
937 */
David Howells78525c72021-08-11 09:49:13 +0100938int afs_launder_page(struct page *subpage)
David Howells4343d002017-11-02 15:27:52 +0000939{
David Howells78525c72021-08-11 09:49:13 +0100940 struct folio *folio = page_folio(subpage);
941 struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio));
David Howellsbd80d8a2020-02-06 14:22:28 +0000942 struct iov_iter iter;
943 struct bio_vec bv[1];
David Howells4343d002017-11-02 15:27:52 +0000944 unsigned long priv;
945 unsigned int f, t;
946 int ret = 0;
947
David Howells78525c72021-08-11 09:49:13 +0100948 _enter("{%lx}", folio_index(folio));
David Howells4343d002017-11-02 15:27:52 +0000949
David Howells78525c72021-08-11 09:49:13 +0100950 priv = (unsigned long)folio_get_private(folio);
951 if (folio_clear_dirty_for_io(folio)) {
David Howells4343d002017-11-02 15:27:52 +0000952 f = 0;
David Howells78525c72021-08-11 09:49:13 +0100953 t = folio_size(folio);
954 if (folio_test_private(folio)) {
955 f = afs_folio_dirty_from(folio, priv);
956 t = afs_folio_dirty_to(folio, priv);
David Howells4343d002017-11-02 15:27:52 +0000957 }
958
David Howells78525c72021-08-11 09:49:13 +0100959 bv[0].bv_page = &folio->page;
David Howellsbd80d8a2020-02-06 14:22:28 +0000960 bv[0].bv_offset = f;
961 bv[0].bv_len = t - f;
962 iov_iter_bvec(&iter, WRITE, bv, 1, bv[0].bv_len);
963
David Howells78525c72021-08-11 09:49:13 +0100964 trace_afs_folio_dirty(vnode, tracepoint_string("launder"), folio);
965 ret = afs_store_data(vnode, &iter, folio_pos(folio) + f, true);
David Howells4343d002017-11-02 15:27:52 +0000966 }
967
David Howells78525c72021-08-11 09:49:13 +0100968 trace_afs_folio_dirty(vnode, tracepoint_string("laundered"), folio);
969 folio_detach_private(folio);
970 folio_wait_fscache(folio);
David Howells4343d002017-11-02 15:27:52 +0000971 return ret;
David Howells31143d52007-05-09 02:33:46 -0700972}