blob: 8bcab95f11273aeeb94e0347b9696960e0f1138a [file] [log] [blame]
David Howells31143d52007-05-09 02:33:46 -07001/* handling of writes to regular files and writing back to the server
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
David Howells4343d002017-11-02 15:27:52 +000011
Alexey Dobriyan4af3c9c2007-10-16 23:29:23 -070012#include <linux/backing-dev.h>
David Howells31143d52007-05-09 02:33:46 -070013#include <linux/slab.h>
14#include <linux/fs.h>
15#include <linux/pagemap.h>
16#include <linux/writeback.h>
17#include <linux/pagevec.h>
18#include "internal.h"
19
David Howells31143d52007-05-09 02:33:46 -070020/*
21 * mark a page as having been made dirty and thus needing writeback
22 */
23int afs_set_page_dirty(struct page *page)
24{
25 _enter("");
26 return __set_page_dirty_nobuffers(page);
27}
28
29/*
David Howells31143d52007-05-09 02:33:46 -070030 * partly or wholly fill a page that's under preparation for writing
31 */
32static int afs_fill_page(struct afs_vnode *vnode, struct key *key,
David Howellse8e581a2017-03-16 16:27:44 +000033 loff_t pos, unsigned int len, struct page *page)
David Howells31143d52007-05-09 02:33:46 -070034{
David Howells196ee9c2017-01-05 10:38:34 +000035 struct afs_read *req;
David Howells2a0b4f62018-10-20 00:57:57 +010036 size_t p;
37 void *data;
David Howells31143d52007-05-09 02:33:46 -070038 int ret;
39
Anton Blanchard5e7f2332011-06-13 22:31:12 +010040 _enter(",,%llu", (unsigned long long)pos);
David Howells31143d52007-05-09 02:33:46 -070041
David Howells2a0b4f62018-10-20 00:57:57 +010042 if (pos >= vnode->vfs_inode.i_size) {
43 p = pos & ~PAGE_MASK;
44 ASSERTCMP(p + len, <=, PAGE_SIZE);
45 data = kmap(page);
46 memset(data + p, 0, len);
47 kunmap(page);
48 return 0;
49 }
50
David Howells196ee9c2017-01-05 10:38:34 +000051 req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *),
52 GFP_KERNEL);
53 if (!req)
54 return -ENOMEM;
55
David Howellsf3ddee82018-04-06 14:17:25 +010056 refcount_set(&req->usage, 1);
David Howells196ee9c2017-01-05 10:38:34 +000057 req->pos = pos;
David Howellse8e581a2017-03-16 16:27:44 +000058 req->len = len;
David Howells196ee9c2017-01-05 10:38:34 +000059 req->nr_pages = 1;
David Howellsf3ddee82018-04-06 14:17:25 +010060 req->pages = req->array;
David Howells196ee9c2017-01-05 10:38:34 +000061 req->pages[0] = page;
David Howells5611ef22017-03-16 16:27:43 +000062 get_page(page);
David Howells196ee9c2017-01-05 10:38:34 +000063
David Howellsd2ddc772017-11-02 15:27:50 +000064 ret = afs_fetch_data(vnode, key, req);
David Howells196ee9c2017-01-05 10:38:34 +000065 afs_put_read(req);
David Howells31143d52007-05-09 02:33:46 -070066 if (ret < 0) {
67 if (ret == -ENOENT) {
68 _debug("got NOENT from server"
69 " - marking file deleted and stale");
70 set_bit(AFS_VNODE_DELETED, &vnode->flags);
71 ret = -ESTALE;
72 }
73 }
74
75 _leave(" = %d", ret);
76 return ret;
77}
78
79/*
David Howells31143d52007-05-09 02:33:46 -070080 * prepare to perform part of a write to a page
David Howells31143d52007-05-09 02:33:46 -070081 */
Nick Piggin15b46502008-10-15 22:04:32 -070082int afs_write_begin(struct file *file, struct address_space *mapping,
83 loff_t pos, unsigned len, unsigned flags,
84 struct page **pagep, void **fsdata)
David Howells31143d52007-05-09 02:33:46 -070085{
Al Viro496ad9a2013-01-23 17:07:38 -050086 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
Nick Piggin15b46502008-10-15 22:04:32 -070087 struct page *page;
David Howells215804a2017-11-02 15:27:52 +000088 struct key *key = afs_file_key(file);
David Howells4343d002017-11-02 15:27:52 +000089 unsigned long priv;
90 unsigned f, from = pos & (PAGE_SIZE - 1);
91 unsigned t, to = from + len;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030092 pgoff_t index = pos >> PAGE_SHIFT;
David Howells31143d52007-05-09 02:33:46 -070093 int ret;
94
David Howells3b6492d2018-10-20 00:57:57 +010095 _enter("{%llx:%llu},{%lx},%u,%u",
Nick Piggin15b46502008-10-15 22:04:32 -070096 vnode->fid.vid, vnode->fid.vnode, index, from, to);
David Howells31143d52007-05-09 02:33:46 -070097
David Howells4343d002017-11-02 15:27:52 +000098 /* We want to store information about how much of a page is altered in
99 * page->private.
100 */
101 BUILD_BUG_ON(PAGE_SIZE > 32768 && sizeof(page->private) < 8);
David Howells31143d52007-05-09 02:33:46 -0700102
Nick Piggin54566b22009-01-04 12:00:53 -0800103 page = grab_cache_page_write_begin(mapping, index, flags);
David Howells4343d002017-11-02 15:27:52 +0000104 if (!page)
Nick Piggin15b46502008-10-15 22:04:32 -0700105 return -ENOMEM;
Nick Piggin15b46502008-10-15 22:04:32 -0700106
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300107 if (!PageUptodate(page) && len != PAGE_SIZE) {
David Howellse8e581a2017-03-16 16:27:44 +0000108 ret = afs_fill_page(vnode, key, pos & PAGE_MASK, PAGE_SIZE, page);
David Howells31143d52007-05-09 02:33:46 -0700109 if (ret < 0) {
David Howells6d06b0d2017-03-16 16:27:48 +0000110 unlock_page(page);
111 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700112 _leave(" = %d [prep]", ret);
113 return ret;
114 }
Nick Piggin15b46502008-10-15 22:04:32 -0700115 SetPageUptodate(page);
David Howells31143d52007-05-09 02:33:46 -0700116 }
117
David Howells6d06b0d2017-03-16 16:27:48 +0000118 /* page won't leak in error case: it eventually gets cleaned off LRU */
119 *pagep = page;
120
David Howells31143d52007-05-09 02:33:46 -0700121try_again:
David Howells4343d002017-11-02 15:27:52 +0000122 /* See if this page is already partially written in a way that we can
123 * merge the new write with.
124 */
125 t = f = 0;
126 if (PagePrivate(page)) {
127 priv = page_private(page);
128 f = priv & AFS_PRIV_MAX;
129 t = priv >> AFS_PRIV_SHIFT;
130 ASSERTCMP(f, <=, t);
David Howells31143d52007-05-09 02:33:46 -0700131 }
132
David Howells4343d002017-11-02 15:27:52 +0000133 if (f != t) {
David Howells5a039c32017-11-18 00:13:30 +0000134 if (PageWriteback(page)) {
135 trace_afs_page_dirty(vnode, tracepoint_string("alrdy"),
136 page->index, priv);
137 goto flush_conflicting_write;
138 }
David Howells5a813272018-04-06 14:17:26 +0100139 /* If the file is being filled locally, allow inter-write
140 * spaces to be merged into writes. If it's not, only write
141 * back what the user gives us.
142 */
143 if (!test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags) &&
144 (to < f || from > t))
David Howells4343d002017-11-02 15:27:52 +0000145 goto flush_conflicting_write;
146 if (from < f)
147 f = from;
148 if (to > t)
149 t = to;
150 } else {
151 f = from;
152 t = to;
David Howells31143d52007-05-09 02:33:46 -0700153 }
154
David Howells4343d002017-11-02 15:27:52 +0000155 priv = (unsigned long)t << AFS_PRIV_SHIFT;
156 priv |= f;
David Howells13524ab2017-11-02 15:27:53 +0000157 trace_afs_page_dirty(vnode, tracepoint_string("begin"),
158 page->index, priv);
David Howells31143d52007-05-09 02:33:46 -0700159 SetPagePrivate(page);
David Howells4343d002017-11-02 15:27:52 +0000160 set_page_private(page, priv);
161 _leave(" = 0");
David Howells31143d52007-05-09 02:33:46 -0700162 return 0;
163
David Howells4343d002017-11-02 15:27:52 +0000164 /* The previous write and this write aren't adjacent or overlapping, so
165 * flush the page out.
166 */
167flush_conflicting_write:
David Howells31143d52007-05-09 02:33:46 -0700168 _debug("flush conflict");
David Howells4343d002017-11-02 15:27:52 +0000169 ret = write_one_page(page);
170 if (ret < 0) {
171 _leave(" = %d", ret);
172 return ret;
David Howells31143d52007-05-09 02:33:46 -0700173 }
174
David Howells4343d002017-11-02 15:27:52 +0000175 ret = lock_page_killable(page);
176 if (ret < 0) {
177 _leave(" = %d", ret);
178 return ret;
179 }
David Howells31143d52007-05-09 02:33:46 -0700180 goto try_again;
181}
182
183/*
184 * finalise part of a write to a page
185 */
Nick Piggin15b46502008-10-15 22:04:32 -0700186int afs_write_end(struct file *file, struct address_space *mapping,
187 loff_t pos, unsigned len, unsigned copied,
188 struct page *page, void *fsdata)
David Howells31143d52007-05-09 02:33:46 -0700189{
Al Viro496ad9a2013-01-23 17:07:38 -0500190 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
David Howells215804a2017-11-02 15:27:52 +0000191 struct key *key = afs_file_key(file);
David Howells31143d52007-05-09 02:33:46 -0700192 loff_t i_size, maybe_i_size;
David Howellse8e581a2017-03-16 16:27:44 +0000193 int ret;
David Howells31143d52007-05-09 02:33:46 -0700194
David Howells3b6492d2018-10-20 00:57:57 +0100195 _enter("{%llx:%llu},{%lx}",
Nick Piggin15b46502008-10-15 22:04:32 -0700196 vnode->fid.vid, vnode->fid.vnode, page->index);
David Howells31143d52007-05-09 02:33:46 -0700197
Nick Piggin15b46502008-10-15 22:04:32 -0700198 maybe_i_size = pos + copied;
David Howells31143d52007-05-09 02:33:46 -0700199
200 i_size = i_size_read(&vnode->vfs_inode);
201 if (maybe_i_size > i_size) {
David Howells4343d002017-11-02 15:27:52 +0000202 spin_lock(&vnode->wb_lock);
David Howells31143d52007-05-09 02:33:46 -0700203 i_size = i_size_read(&vnode->vfs_inode);
204 if (maybe_i_size > i_size)
205 i_size_write(&vnode->vfs_inode, maybe_i_size);
David Howells4343d002017-11-02 15:27:52 +0000206 spin_unlock(&vnode->wb_lock);
David Howells31143d52007-05-09 02:33:46 -0700207 }
208
David Howellse8e581a2017-03-16 16:27:44 +0000209 if (!PageUptodate(page)) {
210 if (copied < len) {
211 /* Try and load any missing data from the server. The
212 * unmarshalling routine will take care of clearing any
213 * bits that are beyond the EOF.
214 */
215 ret = afs_fill_page(vnode, key, pos + copied,
216 len - copied, page);
217 if (ret < 0)
David Howellsafae4572018-01-02 10:02:19 +0000218 goto out;
David Howellse8e581a2017-03-16 16:27:44 +0000219 }
220 SetPageUptodate(page);
221 }
222
David Howells31143d52007-05-09 02:33:46 -0700223 set_page_dirty(page);
David Howells31143d52007-05-09 02:33:46 -0700224 if (PageDirty(page))
225 _debug("dirtied");
David Howellsafae4572018-01-02 10:02:19 +0000226 ret = copied;
227
228out:
Nick Piggin15b46502008-10-15 22:04:32 -0700229 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300230 put_page(page);
David Howellsafae4572018-01-02 10:02:19 +0000231 return ret;
David Howells31143d52007-05-09 02:33:46 -0700232}
233
234/*
235 * kill all the pages in the given range
236 */
David Howells4343d002017-11-02 15:27:52 +0000237static void afs_kill_pages(struct address_space *mapping,
David Howells31143d52007-05-09 02:33:46 -0700238 pgoff_t first, pgoff_t last)
239{
David Howells4343d002017-11-02 15:27:52 +0000240 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
David Howells31143d52007-05-09 02:33:46 -0700241 struct pagevec pv;
242 unsigned count, loop;
243
David Howells3b6492d2018-10-20 00:57:57 +0100244 _enter("{%llx:%llu},%lx-%lx",
David Howells31143d52007-05-09 02:33:46 -0700245 vnode->fid.vid, vnode->fid.vnode, first, last);
246
Mel Gorman86679822017-11-15 17:37:52 -0800247 pagevec_init(&pv);
David Howells31143d52007-05-09 02:33:46 -0700248
249 do {
250 _debug("kill %lx-%lx", first, last);
251
252 count = last - first + 1;
253 if (count > PAGEVEC_SIZE)
254 count = PAGEVEC_SIZE;
David Howells4343d002017-11-02 15:27:52 +0000255 pv.nr = find_get_pages_contig(mapping, first, count, pv.pages);
David Howells31143d52007-05-09 02:33:46 -0700256 ASSERTCMP(pv.nr, ==, count);
257
258 for (loop = 0; loop < count; loop++) {
David Howells7286a352017-03-16 16:27:48 +0000259 struct page *page = pv.pages[loop];
260 ClearPageUptodate(page);
David Howells4343d002017-11-02 15:27:52 +0000261 SetPageError(page);
262 end_page_writeback(page);
David Howells7286a352017-03-16 16:27:48 +0000263 if (page->index >= first)
264 first = page->index + 1;
David Howells4343d002017-11-02 15:27:52 +0000265 lock_page(page);
266 generic_error_remove_page(mapping, page);
Marc Dionne21bd68f2019-04-13 08:37:37 +0100267 unlock_page(page);
David Howells31143d52007-05-09 02:33:46 -0700268 }
269
270 __pagevec_release(&pv);
David Howells4343d002017-11-02 15:27:52 +0000271 } while (first <= last);
David Howells31143d52007-05-09 02:33:46 -0700272
273 _leave("");
274}
275
276/*
David Howells4343d002017-11-02 15:27:52 +0000277 * Redirty all the pages in a given range.
David Howells31143d52007-05-09 02:33:46 -0700278 */
David Howells4343d002017-11-02 15:27:52 +0000279static void afs_redirty_pages(struct writeback_control *wbc,
280 struct address_space *mapping,
281 pgoff_t first, pgoff_t last)
David Howells31143d52007-05-09 02:33:46 -0700282{
David Howells4343d002017-11-02 15:27:52 +0000283 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
284 struct pagevec pv;
285 unsigned count, loop;
286
David Howells3b6492d2018-10-20 00:57:57 +0100287 _enter("{%llx:%llu},%lx-%lx",
David Howells4343d002017-11-02 15:27:52 +0000288 vnode->fid.vid, vnode->fid.vnode, first, last);
289
Linus Torvalds487e2c92017-11-16 11:41:22 -0800290 pagevec_init(&pv);
David Howells4343d002017-11-02 15:27:52 +0000291
292 do {
293 _debug("redirty %lx-%lx", first, last);
294
295 count = last - first + 1;
296 if (count > PAGEVEC_SIZE)
297 count = PAGEVEC_SIZE;
298 pv.nr = find_get_pages_contig(mapping, first, count, pv.pages);
299 ASSERTCMP(pv.nr, ==, count);
300
301 for (loop = 0; loop < count; loop++) {
302 struct page *page = pv.pages[loop];
303
304 redirty_page_for_writepage(wbc, page);
305 end_page_writeback(page);
David Howells31143d52007-05-09 02:33:46 -0700306 if (page->index >= first)
307 first = page->index + 1;
308 }
309
310 __pagevec_release(&pv);
David Howells4343d002017-11-02 15:27:52 +0000311 } while (first <= last);
David Howells31143d52007-05-09 02:33:46 -0700312
313 _leave("");
314}
315
316/*
David Howellsa58823a2019-05-09 15:16:10 +0100317 * completion of write to server
318 */
319static void afs_pages_written_back(struct afs_vnode *vnode,
320 pgoff_t first, pgoff_t last)
321{
322 struct pagevec pv;
323 unsigned long priv;
324 unsigned count, loop;
325
326 _enter("{%llx:%llu},{%lx-%lx}",
327 vnode->fid.vid, vnode->fid.vnode, first, last);
328
329 pagevec_init(&pv);
330
331 do {
332 _debug("done %lx-%lx", first, last);
333
334 count = last - first + 1;
335 if (count > PAGEVEC_SIZE)
336 count = PAGEVEC_SIZE;
337 pv.nr = find_get_pages_contig(vnode->vfs_inode.i_mapping,
338 first, count, pv.pages);
339 ASSERTCMP(pv.nr, ==, count);
340
341 for (loop = 0; loop < count; loop++) {
342 priv = page_private(pv.pages[loop]);
343 trace_afs_page_dirty(vnode, tracepoint_string("clear"),
344 pv.pages[loop]->index, priv);
345 set_page_private(pv.pages[loop], 0);
346 end_page_writeback(pv.pages[loop]);
347 }
348 first += count;
349 __pagevec_release(&pv);
350 } while (first <= last);
351
352 afs_prune_wb_keys(vnode);
353 _leave("");
354}
355
356/*
David Howellsd2ddc772017-11-02 15:27:50 +0000357 * write to a file
358 */
David Howells4343d002017-11-02 15:27:52 +0000359static int afs_store_data(struct address_space *mapping,
360 pgoff_t first, pgoff_t last,
David Howellsd2ddc772017-11-02 15:27:50 +0000361 unsigned offset, unsigned to)
362{
David Howells4343d002017-11-02 15:27:52 +0000363 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
David Howellsd2ddc772017-11-02 15:27:50 +0000364 struct afs_fs_cursor fc;
David Howellsa58823a2019-05-09 15:16:10 +0100365 struct afs_status_cb *scb;
David Howells4343d002017-11-02 15:27:52 +0000366 struct afs_wb_key *wbk = NULL;
367 struct list_head *p;
368 int ret = -ENOKEY, ret2;
David Howellsd2ddc772017-11-02 15:27:50 +0000369
David Howells3b6492d2018-10-20 00:57:57 +0100370 _enter("%s{%llx:%llu.%u},%lx,%lx,%x,%x",
David Howellsd2ddc772017-11-02 15:27:50 +0000371 vnode->volume->name,
372 vnode->fid.vid,
373 vnode->fid.vnode,
374 vnode->fid.unique,
David Howellsd2ddc772017-11-02 15:27:50 +0000375 first, last, offset, to);
376
David Howellsa58823a2019-05-09 15:16:10 +0100377 scb = kzalloc(sizeof(struct afs_status_cb), GFP_NOFS);
378 if (!scb)
379 return -ENOMEM;
380
David Howells4343d002017-11-02 15:27:52 +0000381 spin_lock(&vnode->wb_lock);
382 p = vnode->wb_keys.next;
383
384 /* Iterate through the list looking for a valid key to use. */
385try_next_key:
386 while (p != &vnode->wb_keys) {
387 wbk = list_entry(p, struct afs_wb_key, vnode_link);
388 _debug("wbk %u", key_serial(wbk->key));
389 ret2 = key_validate(wbk->key);
390 if (ret2 == 0)
391 goto found_key;
392 if (ret == -ENOKEY)
393 ret = ret2;
394 p = p->next;
395 }
396
397 spin_unlock(&vnode->wb_lock);
398 afs_put_wb_key(wbk);
David Howellsa58823a2019-05-09 15:16:10 +0100399 kfree(scb);
David Howells4343d002017-11-02 15:27:52 +0000400 _leave(" = %d [no keys]", ret);
401 return ret;
402
403found_key:
404 refcount_inc(&wbk->usage);
405 spin_unlock(&vnode->wb_lock);
406
407 _debug("USE WB KEY %u", key_serial(wbk->key));
408
David Howellsd2ddc772017-11-02 15:27:50 +0000409 ret = -ERESTARTSYS;
David Howells20b83912019-05-08 16:16:31 +0100410 if (afs_begin_vnode_operation(&fc, vnode, wbk->key, false)) {
David Howellsa58823a2019-05-09 15:16:10 +0100411 afs_dataversion_t data_version = vnode->status.data_version + 1;
412
David Howellsd2ddc772017-11-02 15:27:50 +0000413 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +0100414 fc.cb_break = afs_calc_vnode_cb_break(vnode);
David Howellsa58823a2019-05-09 15:16:10 +0100415 afs_fs_store_data(&fc, mapping, first, last, offset, to, scb);
David Howellsd2ddc772017-11-02 15:27:50 +0000416 }
417
David Howellsa58823a2019-05-09 15:16:10 +0100418 afs_check_for_remote_deletion(&fc, vnode);
419 afs_vnode_commit_status(&fc, vnode, fc.cb_break,
420 &data_version, scb);
421 if (fc.ac.error == 0)
422 afs_pages_written_back(vnode, first, last);
David Howellsd2ddc772017-11-02 15:27:50 +0000423 ret = afs_end_vnode_operation(&fc);
424 }
425
David Howells4343d002017-11-02 15:27:52 +0000426 switch (ret) {
David Howells76a5cb62018-04-06 14:17:26 +0100427 case 0:
428 afs_stat_v(vnode, n_stores);
429 atomic_long_add((last * PAGE_SIZE + to) -
430 (first * PAGE_SIZE + offset),
431 &afs_v2net(vnode)->n_store_bytes);
432 break;
David Howells4343d002017-11-02 15:27:52 +0000433 case -EACCES:
434 case -EPERM:
435 case -ENOKEY:
436 case -EKEYEXPIRED:
437 case -EKEYREJECTED:
438 case -EKEYREVOKED:
439 _debug("next");
440 spin_lock(&vnode->wb_lock);
441 p = wbk->vnode_link.next;
442 afs_put_wb_key(wbk);
443 goto try_next_key;
444 }
445
446 afs_put_wb_key(wbk);
David Howellsa58823a2019-05-09 15:16:10 +0100447 kfree(scb);
David Howellsd2ddc772017-11-02 15:27:50 +0000448 _leave(" = %d", ret);
449 return ret;
450}
451
452/*
David Howells4343d002017-11-02 15:27:52 +0000453 * Synchronously write back the locked page and any subsequent non-locked dirty
454 * pages.
David Howells31143d52007-05-09 02:33:46 -0700455 */
David Howells4343d002017-11-02 15:27:52 +0000456static int afs_write_back_from_locked_page(struct address_space *mapping,
457 struct writeback_control *wbc,
458 struct page *primary_page,
459 pgoff_t final_page)
David Howells31143d52007-05-09 02:33:46 -0700460{
David Howells13524ab2017-11-02 15:27:53 +0000461 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
David Howells31143d52007-05-09 02:33:46 -0700462 struct page *pages[8], *page;
David Howells4343d002017-11-02 15:27:52 +0000463 unsigned long count, priv;
464 unsigned n, offset, to, f, t;
David Howells31143d52007-05-09 02:33:46 -0700465 pgoff_t start, first, last;
466 int loop, ret;
467
468 _enter(",%lx", primary_page->index);
469
470 count = 1;
David Howells31143d52007-05-09 02:33:46 -0700471 if (test_set_page_writeback(primary_page))
472 BUG();
473
David Howells4343d002017-11-02 15:27:52 +0000474 /* Find all consecutive lockable dirty pages that have contiguous
475 * written regions, stopping when we find a page that is not
476 * immediately lockable, is not dirty or is missing, or we reach the
477 * end of the range.
478 */
David Howells31143d52007-05-09 02:33:46 -0700479 start = primary_page->index;
David Howells4343d002017-11-02 15:27:52 +0000480 priv = page_private(primary_page);
481 offset = priv & AFS_PRIV_MAX;
482 to = priv >> AFS_PRIV_SHIFT;
David Howells13524ab2017-11-02 15:27:53 +0000483 trace_afs_page_dirty(vnode, tracepoint_string("store"),
484 primary_page->index, priv);
David Howells4343d002017-11-02 15:27:52 +0000485
486 WARN_ON(offset == to);
David Howells13524ab2017-11-02 15:27:53 +0000487 if (offset == to)
488 trace_afs_page_dirty(vnode, tracepoint_string("WARN"),
489 primary_page->index, priv);
David Howells4343d002017-11-02 15:27:52 +0000490
David Howells5a813272018-04-06 14:17:26 +0100491 if (start >= final_page ||
492 (to < PAGE_SIZE && !test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags)))
David Howells31143d52007-05-09 02:33:46 -0700493 goto no_more;
David Howells4343d002017-11-02 15:27:52 +0000494
David Howells31143d52007-05-09 02:33:46 -0700495 start++;
496 do {
497 _debug("more %lx [%lx]", start, count);
David Howells4343d002017-11-02 15:27:52 +0000498 n = final_page - start + 1;
David Howells31143d52007-05-09 02:33:46 -0700499 if (n > ARRAY_SIZE(pages))
500 n = ARRAY_SIZE(pages);
David Howells4343d002017-11-02 15:27:52 +0000501 n = find_get_pages_contig(mapping, start, ARRAY_SIZE(pages), pages);
David Howells31143d52007-05-09 02:33:46 -0700502 _debug("fgpc %u", n);
503 if (n == 0)
504 goto no_more;
505 if (pages[0]->index != start) {
David Howells9d577b62007-05-10 22:22:19 -0700506 do {
507 put_page(pages[--n]);
508 } while (n > 0);
David Howells31143d52007-05-09 02:33:46 -0700509 goto no_more;
510 }
511
512 for (loop = 0; loop < n; loop++) {
513 page = pages[loop];
David Howells5a813272018-04-06 14:17:26 +0100514 if (to != PAGE_SIZE &&
515 !test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags))
516 break;
David Howells4343d002017-11-02 15:27:52 +0000517 if (page->index > final_page)
David Howells31143d52007-05-09 02:33:46 -0700518 break;
Nick Piggin529ae9a2008-08-02 12:01:03 +0200519 if (!trylock_page(page))
David Howells31143d52007-05-09 02:33:46 -0700520 break;
David Howells4343d002017-11-02 15:27:52 +0000521 if (!PageDirty(page) || PageWriteback(page)) {
David Howells31143d52007-05-09 02:33:46 -0700522 unlock_page(page);
523 break;
524 }
David Howells4343d002017-11-02 15:27:52 +0000525
526 priv = page_private(page);
527 f = priv & AFS_PRIV_MAX;
528 t = priv >> AFS_PRIV_SHIFT;
David Howells5a813272018-04-06 14:17:26 +0100529 if (f != 0 &&
530 !test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags)) {
David Howells4343d002017-11-02 15:27:52 +0000531 unlock_page(page);
532 break;
533 }
534 to = t;
535
David Howells13524ab2017-11-02 15:27:53 +0000536 trace_afs_page_dirty(vnode, tracepoint_string("store+"),
537 page->index, priv);
538
David Howells31143d52007-05-09 02:33:46 -0700539 if (!clear_page_dirty_for_io(page))
540 BUG();
541 if (test_set_page_writeback(page))
542 BUG();
543 unlock_page(page);
544 put_page(page);
545 }
546 count += loop;
547 if (loop < n) {
548 for (; loop < n; loop++)
549 put_page(pages[loop]);
550 goto no_more;
551 }
552
553 start += loop;
David Howells4343d002017-11-02 15:27:52 +0000554 } while (start <= final_page && count < 65536);
David Howells31143d52007-05-09 02:33:46 -0700555
556no_more:
David Howells4343d002017-11-02 15:27:52 +0000557 /* We now have a contiguous set of dirty pages, each with writeback
558 * set; the first page is still locked at this point, but all the rest
559 * have been unlocked.
560 */
561 unlock_page(primary_page);
562
David Howells31143d52007-05-09 02:33:46 -0700563 first = primary_page->index;
564 last = first + count - 1;
565
David Howells31143d52007-05-09 02:33:46 -0700566 _debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to);
567
David Howells4343d002017-11-02 15:27:52 +0000568 ret = afs_store_data(mapping, first, last, offset, to);
569 switch (ret) {
570 case 0:
David Howells31143d52007-05-09 02:33:46 -0700571 ret = count;
David Howells4343d002017-11-02 15:27:52 +0000572 break;
573
574 default:
575 pr_notice("kAFS: Unexpected error from FS.StoreData %d\n", ret);
576 /* Fall through */
577 case -EACCES:
578 case -EPERM:
579 case -ENOKEY:
580 case -EKEYEXPIRED:
581 case -EKEYREJECTED:
582 case -EKEYREVOKED:
583 afs_redirty_pages(wbc, mapping, first, last);
584 mapping_set_error(mapping, ret);
585 break;
586
587 case -EDQUOT:
588 case -ENOSPC:
589 afs_redirty_pages(wbc, mapping, first, last);
590 mapping_set_error(mapping, -ENOSPC);
591 break;
592
593 case -EROFS:
594 case -EIO:
595 case -EREMOTEIO:
596 case -EFBIG:
597 case -ENOENT:
598 case -ENOMEDIUM:
599 case -ENXIO:
David Howellsf51375c2018-10-20 00:57:57 +0100600 trace_afs_file_error(vnode, ret, afs_file_error_writeback_fail);
David Howells4343d002017-11-02 15:27:52 +0000601 afs_kill_pages(mapping, first, last);
602 mapping_set_error(mapping, ret);
603 break;
David Howells31143d52007-05-09 02:33:46 -0700604 }
605
606 _leave(" = %d", ret);
607 return ret;
608}
609
610/*
611 * write a page back to the server
612 * - the caller locked the page for us
613 */
614int afs_writepage(struct page *page, struct writeback_control *wbc)
615{
David Howells31143d52007-05-09 02:33:46 -0700616 int ret;
617
618 _enter("{%lx},", page->index);
619
David Howells4343d002017-11-02 15:27:52 +0000620 ret = afs_write_back_from_locked_page(page->mapping, wbc, page,
621 wbc->range_end >> PAGE_SHIFT);
David Howells31143d52007-05-09 02:33:46 -0700622 if (ret < 0) {
623 _leave(" = %d", ret);
624 return 0;
625 }
626
627 wbc->nr_to_write -= ret;
David Howells31143d52007-05-09 02:33:46 -0700628
629 _leave(" = 0");
630 return 0;
631}
632
633/*
634 * write a region of pages back to the server
635 */
Adrian Bunkc1206a22007-10-16 23:26:41 -0700636static int afs_writepages_region(struct address_space *mapping,
637 struct writeback_control *wbc,
638 pgoff_t index, pgoff_t end, pgoff_t *_next)
David Howells31143d52007-05-09 02:33:46 -0700639{
David Howells31143d52007-05-09 02:33:46 -0700640 struct page *page;
641 int ret, n;
642
643 _enter(",,%lx,%lx,", index, end);
644
645 do {
Jan Karaaef6e412017-11-15 17:35:23 -0800646 n = find_get_pages_range_tag(mapping, &index, end,
647 PAGECACHE_TAG_DIRTY, 1, &page);
David Howells31143d52007-05-09 02:33:46 -0700648 if (!n)
649 break;
650
651 _debug("wback %lx", page->index);
652
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700653 /*
654 * at this point we hold neither the i_pages lock nor the
655 * page lock: the page may be truncated or invalidated
656 * (changing page->mapping to NULL), or even swizzled
657 * back from swapper_space to tmpfs file mapping
David Howells31143d52007-05-09 02:33:46 -0700658 */
David Howells4343d002017-11-02 15:27:52 +0000659 ret = lock_page_killable(page);
660 if (ret < 0) {
661 put_page(page);
662 _leave(" = %d", ret);
663 return ret;
664 }
David Howells31143d52007-05-09 02:33:46 -0700665
David Howellsc5051c72017-03-16 16:27:49 +0000666 if (page->mapping != mapping || !PageDirty(page)) {
David Howells31143d52007-05-09 02:33:46 -0700667 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300668 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700669 continue;
670 }
671
David Howellsc5051c72017-03-16 16:27:49 +0000672 if (PageWriteback(page)) {
David Howells31143d52007-05-09 02:33:46 -0700673 unlock_page(page);
David Howellsc5051c72017-03-16 16:27:49 +0000674 if (wbc->sync_mode != WB_SYNC_NONE)
675 wait_on_page_writeback(page);
David Howells29c8bbb2017-03-16 16:27:43 +0000676 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700677 continue;
678 }
679
David Howells65a15102017-03-16 16:27:49 +0000680 if (!clear_page_dirty_for_io(page))
681 BUG();
David Howells4343d002017-11-02 15:27:52 +0000682 ret = afs_write_back_from_locked_page(mapping, wbc, page, end);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300683 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700684 if (ret < 0) {
685 _leave(" = %d", ret);
686 return ret;
687 }
688
689 wbc->nr_to_write -= ret;
690
David Howells31143d52007-05-09 02:33:46 -0700691 cond_resched();
692 } while (index < end && wbc->nr_to_write > 0);
693
694 *_next = index;
695 _leave(" = 0 [%lx]", *_next);
696 return 0;
697}
698
699/*
700 * write some of the pending data back to the server
701 */
702int afs_writepages(struct address_space *mapping,
703 struct writeback_control *wbc)
704{
David Howells31143d52007-05-09 02:33:46 -0700705 pgoff_t start, end, next;
706 int ret;
707
708 _enter("");
709
David Howells31143d52007-05-09 02:33:46 -0700710 if (wbc->range_cyclic) {
711 start = mapping->writeback_index;
712 end = -1;
713 ret = afs_writepages_region(mapping, wbc, start, end, &next);
Wu Fengguang1b430be2010-10-26 14:21:26 -0700714 if (start > 0 && wbc->nr_to_write > 0 && ret == 0)
David Howells31143d52007-05-09 02:33:46 -0700715 ret = afs_writepages_region(mapping, wbc, 0, start,
716 &next);
717 mapping->writeback_index = next;
718 } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300719 end = (pgoff_t)(LLONG_MAX >> PAGE_SHIFT);
David Howells31143d52007-05-09 02:33:46 -0700720 ret = afs_writepages_region(mapping, wbc, 0, end, &next);
721 if (wbc->nr_to_write > 0)
722 mapping->writeback_index = next;
723 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300724 start = wbc->range_start >> PAGE_SHIFT;
725 end = wbc->range_end >> PAGE_SHIFT;
David Howells31143d52007-05-09 02:33:46 -0700726 ret = afs_writepages_region(mapping, wbc, start, end, &next);
727 }
728
729 _leave(" = %d", ret);
730 return ret;
731}
732
733/*
David Howells31143d52007-05-09 02:33:46 -0700734 * write to an AFS file
735 */
Al Viro50b55512014-04-03 14:13:46 -0400736ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from)
David Howells31143d52007-05-09 02:33:46 -0700737{
Al Viro496ad9a2013-01-23 17:07:38 -0500738 struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
David Howells31143d52007-05-09 02:33:46 -0700739 ssize_t result;
Al Viro50b55512014-04-03 14:13:46 -0400740 size_t count = iov_iter_count(from);
David Howells31143d52007-05-09 02:33:46 -0700741
David Howells3b6492d2018-10-20 00:57:57 +0100742 _enter("{%llx:%llu},{%zu},",
Al Viro50b55512014-04-03 14:13:46 -0400743 vnode->fid.vid, vnode->fid.vnode, count);
David Howells31143d52007-05-09 02:33:46 -0700744
745 if (IS_SWAPFILE(&vnode->vfs_inode)) {
746 printk(KERN_INFO
747 "AFS: Attempt to write to active swap file!\n");
748 return -EBUSY;
749 }
750
751 if (!count)
752 return 0;
753
Al Viro50b55512014-04-03 14:13:46 -0400754 result = generic_file_write_iter(iocb, from);
David Howells31143d52007-05-09 02:33:46 -0700755
David Howells31143d52007-05-09 02:33:46 -0700756 _leave(" = %zd", result);
757 return result;
758}
759
760/*
David Howells31143d52007-05-09 02:33:46 -0700761 * flush any dirty pages for this process, and check for write errors.
762 * - the return status from this call provides a reliable indication of
763 * whether any write errors occurred for this process.
764 */
Josef Bacik02c24a82011-07-16 20:44:56 -0400765int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
David Howells31143d52007-05-09 02:33:46 -0700766{
Al Viro3c981bf2013-09-03 13:37:45 -0400767 struct inode *inode = file_inode(file);
Al Viro3c981bf2013-09-03 13:37:45 -0400768 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells31143d52007-05-09 02:33:46 -0700769
David Howells3b6492d2018-10-20 00:57:57 +0100770 _enter("{%llx:%llu},{n=%pD},%d",
Al Viro3c981bf2013-09-03 13:37:45 -0400771 vnode->fid.vid, vnode->fid.vnode, file,
David Howells31143d52007-05-09 02:33:46 -0700772 datasync);
773
David Howells4343d002017-11-02 15:27:52 +0000774 return file_write_and_wait_range(file, start, end);
David Howells31143d52007-05-09 02:33:46 -0700775}
David Howells9b3f26c2009-04-03 16:42:41 +0100776
777/*
778 * notification that a previously read-only page is about to become writable
779 * - if it returns an error, the caller will deliver a bus error signal
780 */
Souptick Joarder0722f182018-08-23 17:00:48 -0700781vm_fault_t afs_page_mkwrite(struct vm_fault *vmf)
David Howells9b3f26c2009-04-03 16:42:41 +0100782{
David Howells1cf7a152017-11-02 15:27:52 +0000783 struct file *file = vmf->vma->vm_file;
784 struct inode *inode = file_inode(file);
785 struct afs_vnode *vnode = AFS_FS_I(inode);
786 unsigned long priv;
David Howells9b3f26c2009-04-03 16:42:41 +0100787
David Howells3b6492d2018-10-20 00:57:57 +0100788 _enter("{{%llx:%llu}},{%lx}",
David Howells1cf7a152017-11-02 15:27:52 +0000789 vnode->fid.vid, vnode->fid.vnode, vmf->page->index);
David Howells9b3f26c2009-04-03 16:42:41 +0100790
David Howells1cf7a152017-11-02 15:27:52 +0000791 sb_start_pagefault(inode->i_sb);
792
793 /* Wait for the page to be written to the cache before we allow it to
794 * be modified. We then assume the entire page will need writing back.
795 */
David Howells9b3f26c2009-04-03 16:42:41 +0100796#ifdef CONFIG_AFS_FSCACHE
David Howells1cf7a152017-11-02 15:27:52 +0000797 fscache_wait_on_page_write(vnode->cache, vmf->page);
David Howells9b3f26c2009-04-03 16:42:41 +0100798#endif
799
David Howells1cf7a152017-11-02 15:27:52 +0000800 if (PageWriteback(vmf->page) &&
801 wait_on_page_bit_killable(vmf->page, PG_writeback) < 0)
802 return VM_FAULT_RETRY;
803
804 if (lock_page_killable(vmf->page) < 0)
805 return VM_FAULT_RETRY;
806
807 /* We mustn't change page->private until writeback is complete as that
808 * details the portion of the page we need to write back and we might
809 * need to redirty the page if there's a problem.
810 */
811 wait_on_page_writeback(vmf->page);
812
813 priv = (unsigned long)PAGE_SIZE << AFS_PRIV_SHIFT; /* To */
814 priv |= 0; /* From */
David Howells13524ab2017-11-02 15:27:53 +0000815 trace_afs_page_dirty(vnode, tracepoint_string("mkwrite"),
816 vmf->page->index, priv);
David Howells1cf7a152017-11-02 15:27:52 +0000817 SetPagePrivate(vmf->page);
818 set_page_private(vmf->page, priv);
819
820 sb_end_pagefault(inode->i_sb);
821 return VM_FAULT_LOCKED;
David Howells9b3f26c2009-04-03 16:42:41 +0100822}
David Howells4343d002017-11-02 15:27:52 +0000823
824/*
825 * Prune the keys cached for writeback. The caller must hold vnode->wb_lock.
826 */
827void afs_prune_wb_keys(struct afs_vnode *vnode)
828{
829 LIST_HEAD(graveyard);
830 struct afs_wb_key *wbk, *tmp;
831
832 /* Discard unused keys */
833 spin_lock(&vnode->wb_lock);
834
835 if (!mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_WRITEBACK) &&
836 !mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_DIRTY)) {
837 list_for_each_entry_safe(wbk, tmp, &vnode->wb_keys, vnode_link) {
838 if (refcount_read(&wbk->usage) == 1)
839 list_move(&wbk->vnode_link, &graveyard);
840 }
841 }
842
843 spin_unlock(&vnode->wb_lock);
844
845 while (!list_empty(&graveyard)) {
846 wbk = list_entry(graveyard.next, struct afs_wb_key, vnode_link);
847 list_del(&wbk->vnode_link);
848 afs_put_wb_key(wbk);
849 }
850}
851
852/*
853 * Clean up a page during invalidation.
854 */
855int afs_launder_page(struct page *page)
856{
857 struct address_space *mapping = page->mapping;
858 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
859 unsigned long priv;
860 unsigned int f, t;
861 int ret = 0;
862
863 _enter("{%lx}", page->index);
864
865 priv = page_private(page);
866 if (clear_page_dirty_for_io(page)) {
867 f = 0;
868 t = PAGE_SIZE;
869 if (PagePrivate(page)) {
870 f = priv & AFS_PRIV_MAX;
871 t = priv >> AFS_PRIV_SHIFT;
872 }
873
David Howells13524ab2017-11-02 15:27:53 +0000874 trace_afs_page_dirty(vnode, tracepoint_string("launder"),
875 page->index, priv);
David Howells4343d002017-11-02 15:27:52 +0000876 ret = afs_store_data(mapping, page->index, page->index, t, f);
877 }
878
David Howells13524ab2017-11-02 15:27:53 +0000879 trace_afs_page_dirty(vnode, tracepoint_string("laundered"),
880 page->index, priv);
David Howells4343d002017-11-02 15:27:52 +0000881 set_page_private(page, 0);
882 ClearPagePrivate(page);
883
884#ifdef CONFIG_AFS_FSCACHE
885 if (PageFsCache(page)) {
886 fscache_wait_on_page_write(vnode->cache, page);
887 fscache_uncache_page(vnode->cache, page);
888 }
889#endif
890 return ret;
David Howells31143d52007-05-09 02:33:46 -0700891}