blob: 98eb7adbce91198268a5fcf6f67a20021309442c [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>
14#include "internal.h"
15
David Howells31143d52007-05-09 02:33:46 -070016/*
17 * mark a page as having been made dirty and thus needing writeback
18 */
19int afs_set_page_dirty(struct page *page)
20{
21 _enter("");
22 return __set_page_dirty_nobuffers(page);
23}
24
25/*
David Howells31143d52007-05-09 02:33:46 -070026 * partly or wholly fill a page that's under preparation for writing
27 */
28static int afs_fill_page(struct afs_vnode *vnode, struct key *key,
David Howellse8e581a2017-03-16 16:27:44 +000029 loff_t pos, unsigned int len, struct page *page)
David Howells31143d52007-05-09 02:33:46 -070030{
David Howells196ee9c2017-01-05 10:38:34 +000031 struct afs_read *req;
David Howells2a0b4f62018-10-20 00:57:57 +010032 size_t p;
33 void *data;
David Howells31143d52007-05-09 02:33:46 -070034 int ret;
35
Anton Blanchard5e7f2332011-06-13 22:31:12 +010036 _enter(",,%llu", (unsigned long long)pos);
David Howells31143d52007-05-09 02:33:46 -070037
David Howells2a0b4f62018-10-20 00:57:57 +010038 if (pos >= vnode->vfs_inode.i_size) {
39 p = pos & ~PAGE_MASK;
40 ASSERTCMP(p + len, <=, PAGE_SIZE);
41 data = kmap(page);
42 memset(data + p, 0, len);
43 kunmap(page);
44 return 0;
45 }
46
David Howells196ee9c2017-01-05 10:38:34 +000047 req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *),
48 GFP_KERNEL);
49 if (!req)
50 return -ENOMEM;
51
David Howellsf3ddee82018-04-06 14:17:25 +010052 refcount_set(&req->usage, 1);
David Howells196ee9c2017-01-05 10:38:34 +000053 req->pos = pos;
David Howellse8e581a2017-03-16 16:27:44 +000054 req->len = len;
David Howells196ee9c2017-01-05 10:38:34 +000055 req->nr_pages = 1;
David Howellsf3ddee82018-04-06 14:17:25 +010056 req->pages = req->array;
David Howells196ee9c2017-01-05 10:38:34 +000057 req->pages[0] = page;
David Howells5611ef22017-03-16 16:27:43 +000058 get_page(page);
David Howells196ee9c2017-01-05 10:38:34 +000059
David Howellsd2ddc772017-11-02 15:27:50 +000060 ret = afs_fetch_data(vnode, key, req);
David Howells196ee9c2017-01-05 10:38:34 +000061 afs_put_read(req);
David Howells31143d52007-05-09 02:33:46 -070062 if (ret < 0) {
63 if (ret == -ENOENT) {
64 _debug("got NOENT from server"
65 " - marking file deleted and stale");
66 set_bit(AFS_VNODE_DELETED, &vnode->flags);
67 ret = -ESTALE;
68 }
69 }
70
71 _leave(" = %d", ret);
72 return ret;
73}
74
75/*
David Howells31143d52007-05-09 02:33:46 -070076 * prepare to perform part of a write to a page
David Howells31143d52007-05-09 02:33:46 -070077 */
Nick Piggin15b46502008-10-15 22:04:32 -070078int afs_write_begin(struct file *file, struct address_space *mapping,
79 loff_t pos, unsigned len, unsigned flags,
80 struct page **pagep, void **fsdata)
David Howells31143d52007-05-09 02:33:46 -070081{
Al Viro496ad9a2013-01-23 17:07:38 -050082 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
Nick Piggin15b46502008-10-15 22:04:32 -070083 struct page *page;
David Howells215804a2017-11-02 15:27:52 +000084 struct key *key = afs_file_key(file);
David Howells4343d002017-11-02 15:27:52 +000085 unsigned long priv;
86 unsigned f, from = pos & (PAGE_SIZE - 1);
87 unsigned t, to = from + len;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030088 pgoff_t index = pos >> PAGE_SHIFT;
David Howells31143d52007-05-09 02:33:46 -070089 int ret;
90
David Howells3b6492d2018-10-20 00:57:57 +010091 _enter("{%llx:%llu},{%lx},%u,%u",
Nick Piggin15b46502008-10-15 22:04:32 -070092 vnode->fid.vid, vnode->fid.vnode, index, from, to);
David Howells31143d52007-05-09 02:33:46 -070093
David Howells4343d002017-11-02 15:27:52 +000094 /* We want to store information about how much of a page is altered in
95 * page->private.
96 */
97 BUILD_BUG_ON(PAGE_SIZE > 32768 && sizeof(page->private) < 8);
David Howells31143d52007-05-09 02:33:46 -070098
Nick Piggin54566b22009-01-04 12:00:53 -080099 page = grab_cache_page_write_begin(mapping, index, flags);
David Howells4343d002017-11-02 15:27:52 +0000100 if (!page)
Nick Piggin15b46502008-10-15 22:04:32 -0700101 return -ENOMEM;
Nick Piggin15b46502008-10-15 22:04:32 -0700102
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300103 if (!PageUptodate(page) && len != PAGE_SIZE) {
David Howellse8e581a2017-03-16 16:27:44 +0000104 ret = afs_fill_page(vnode, key, pos & PAGE_MASK, PAGE_SIZE, page);
David Howells31143d52007-05-09 02:33:46 -0700105 if (ret < 0) {
David Howells6d06b0d2017-03-16 16:27:48 +0000106 unlock_page(page);
107 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700108 _leave(" = %d [prep]", ret);
109 return ret;
110 }
Nick Piggin15b46502008-10-15 22:04:32 -0700111 SetPageUptodate(page);
David Howells31143d52007-05-09 02:33:46 -0700112 }
113
David Howells6d06b0d2017-03-16 16:27:48 +0000114 /* page won't leak in error case: it eventually gets cleaned off LRU */
115 *pagep = page;
116
David Howells31143d52007-05-09 02:33:46 -0700117try_again:
David Howells4343d002017-11-02 15:27:52 +0000118 /* See if this page is already partially written in a way that we can
119 * merge the new write with.
120 */
121 t = f = 0;
122 if (PagePrivate(page)) {
123 priv = page_private(page);
124 f = priv & AFS_PRIV_MAX;
125 t = priv >> AFS_PRIV_SHIFT;
126 ASSERTCMP(f, <=, t);
David Howells31143d52007-05-09 02:33:46 -0700127 }
128
David Howells4343d002017-11-02 15:27:52 +0000129 if (f != t) {
David Howells5a039c32017-11-18 00:13:30 +0000130 if (PageWriteback(page)) {
131 trace_afs_page_dirty(vnode, tracepoint_string("alrdy"),
132 page->index, priv);
133 goto flush_conflicting_write;
134 }
David Howells5a813272018-04-06 14:17:26 +0100135 /* If the file is being filled locally, allow inter-write
136 * spaces to be merged into writes. If it's not, only write
137 * back what the user gives us.
138 */
139 if (!test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags) &&
140 (to < f || from > t))
David Howells4343d002017-11-02 15:27:52 +0000141 goto flush_conflicting_write;
142 if (from < f)
143 f = from;
144 if (to > t)
145 t = to;
146 } else {
147 f = from;
148 t = to;
David Howells31143d52007-05-09 02:33:46 -0700149 }
150
David Howells4343d002017-11-02 15:27:52 +0000151 priv = (unsigned long)t << AFS_PRIV_SHIFT;
152 priv |= f;
David Howells13524ab2017-11-02 15:27:53 +0000153 trace_afs_page_dirty(vnode, tracepoint_string("begin"),
154 page->index, priv);
David Howells31143d52007-05-09 02:33:46 -0700155 SetPagePrivate(page);
David Howells4343d002017-11-02 15:27:52 +0000156 set_page_private(page, priv);
157 _leave(" = 0");
David Howells31143d52007-05-09 02:33:46 -0700158 return 0;
159
David Howells4343d002017-11-02 15:27:52 +0000160 /* The previous write and this write aren't adjacent or overlapping, so
161 * flush the page out.
162 */
163flush_conflicting_write:
David Howells31143d52007-05-09 02:33:46 -0700164 _debug("flush conflict");
David Howells4343d002017-11-02 15:27:52 +0000165 ret = write_one_page(page);
166 if (ret < 0) {
167 _leave(" = %d", ret);
168 return ret;
David Howells31143d52007-05-09 02:33:46 -0700169 }
170
David Howells4343d002017-11-02 15:27:52 +0000171 ret = lock_page_killable(page);
172 if (ret < 0) {
173 _leave(" = %d", ret);
174 return ret;
175 }
David Howells31143d52007-05-09 02:33:46 -0700176 goto try_again;
177}
178
179/*
180 * finalise part of a write to a page
181 */
Nick Piggin15b46502008-10-15 22:04:32 -0700182int afs_write_end(struct file *file, struct address_space *mapping,
183 loff_t pos, unsigned len, unsigned copied,
184 struct page *page, void *fsdata)
David Howells31143d52007-05-09 02:33:46 -0700185{
Al Viro496ad9a2013-01-23 17:07:38 -0500186 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
David Howells215804a2017-11-02 15:27:52 +0000187 struct key *key = afs_file_key(file);
David Howells31143d52007-05-09 02:33:46 -0700188 loff_t i_size, maybe_i_size;
David Howellse8e581a2017-03-16 16:27:44 +0000189 int ret;
David Howells31143d52007-05-09 02:33:46 -0700190
David Howells3b6492d2018-10-20 00:57:57 +0100191 _enter("{%llx:%llu},{%lx}",
Nick Piggin15b46502008-10-15 22:04:32 -0700192 vnode->fid.vid, vnode->fid.vnode, page->index);
David Howells31143d52007-05-09 02:33:46 -0700193
Nick Piggin15b46502008-10-15 22:04:32 -0700194 maybe_i_size = pos + copied;
David Howells31143d52007-05-09 02:33:46 -0700195
196 i_size = i_size_read(&vnode->vfs_inode);
197 if (maybe_i_size > i_size) {
David Howells4343d002017-11-02 15:27:52 +0000198 spin_lock(&vnode->wb_lock);
David Howells31143d52007-05-09 02:33:46 -0700199 i_size = i_size_read(&vnode->vfs_inode);
200 if (maybe_i_size > i_size)
201 i_size_write(&vnode->vfs_inode, maybe_i_size);
David Howells4343d002017-11-02 15:27:52 +0000202 spin_unlock(&vnode->wb_lock);
David Howells31143d52007-05-09 02:33:46 -0700203 }
204
David Howellse8e581a2017-03-16 16:27:44 +0000205 if (!PageUptodate(page)) {
206 if (copied < len) {
207 /* Try and load any missing data from the server. The
208 * unmarshalling routine will take care of clearing any
209 * bits that are beyond the EOF.
210 */
211 ret = afs_fill_page(vnode, key, pos + copied,
212 len - copied, page);
213 if (ret < 0)
David Howellsafae4572018-01-02 10:02:19 +0000214 goto out;
David Howellse8e581a2017-03-16 16:27:44 +0000215 }
216 SetPageUptodate(page);
217 }
218
David Howells31143d52007-05-09 02:33:46 -0700219 set_page_dirty(page);
David Howells31143d52007-05-09 02:33:46 -0700220 if (PageDirty(page))
221 _debug("dirtied");
David Howellsafae4572018-01-02 10:02:19 +0000222 ret = copied;
223
224out:
Nick Piggin15b46502008-10-15 22:04:32 -0700225 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300226 put_page(page);
David Howellsafae4572018-01-02 10:02:19 +0000227 return ret;
David Howells31143d52007-05-09 02:33:46 -0700228}
229
230/*
231 * kill all the pages in the given range
232 */
David Howells4343d002017-11-02 15:27:52 +0000233static void afs_kill_pages(struct address_space *mapping,
David Howells31143d52007-05-09 02:33:46 -0700234 pgoff_t first, pgoff_t last)
235{
David Howells4343d002017-11-02 15:27:52 +0000236 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
David Howells31143d52007-05-09 02:33:46 -0700237 struct pagevec pv;
238 unsigned count, loop;
239
David Howells3b6492d2018-10-20 00:57:57 +0100240 _enter("{%llx:%llu},%lx-%lx",
David Howells31143d52007-05-09 02:33:46 -0700241 vnode->fid.vid, vnode->fid.vnode, first, last);
242
Mel Gorman86679822017-11-15 17:37:52 -0800243 pagevec_init(&pv);
David Howells31143d52007-05-09 02:33:46 -0700244
245 do {
246 _debug("kill %lx-%lx", first, last);
247
248 count = last - first + 1;
249 if (count > PAGEVEC_SIZE)
250 count = PAGEVEC_SIZE;
David Howells4343d002017-11-02 15:27:52 +0000251 pv.nr = find_get_pages_contig(mapping, first, count, pv.pages);
David Howells31143d52007-05-09 02:33:46 -0700252 ASSERTCMP(pv.nr, ==, count);
253
254 for (loop = 0; loop < count; loop++) {
David Howells7286a352017-03-16 16:27:48 +0000255 struct page *page = pv.pages[loop];
256 ClearPageUptodate(page);
David Howells4343d002017-11-02 15:27:52 +0000257 SetPageError(page);
258 end_page_writeback(page);
David Howells7286a352017-03-16 16:27:48 +0000259 if (page->index >= first)
260 first = page->index + 1;
David Howells4343d002017-11-02 15:27:52 +0000261 lock_page(page);
262 generic_error_remove_page(mapping, page);
Marc Dionne21bd68f2019-04-13 08:37:37 +0100263 unlock_page(page);
David Howells31143d52007-05-09 02:33:46 -0700264 }
265
266 __pagevec_release(&pv);
David Howells4343d002017-11-02 15:27:52 +0000267 } while (first <= last);
David Howells31143d52007-05-09 02:33:46 -0700268
269 _leave("");
270}
271
272/*
David Howells4343d002017-11-02 15:27:52 +0000273 * Redirty all the pages in a given range.
David Howells31143d52007-05-09 02:33:46 -0700274 */
David Howells4343d002017-11-02 15:27:52 +0000275static void afs_redirty_pages(struct writeback_control *wbc,
276 struct address_space *mapping,
277 pgoff_t first, pgoff_t last)
David Howells31143d52007-05-09 02:33:46 -0700278{
David Howells4343d002017-11-02 15:27:52 +0000279 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
280 struct pagevec pv;
281 unsigned count, loop;
282
David Howells3b6492d2018-10-20 00:57:57 +0100283 _enter("{%llx:%llu},%lx-%lx",
David Howells4343d002017-11-02 15:27:52 +0000284 vnode->fid.vid, vnode->fid.vnode, first, last);
285
Linus Torvalds487e2c92017-11-16 11:41:22 -0800286 pagevec_init(&pv);
David Howells4343d002017-11-02 15:27:52 +0000287
288 do {
289 _debug("redirty %lx-%lx", first, last);
290
291 count = last - first + 1;
292 if (count > PAGEVEC_SIZE)
293 count = PAGEVEC_SIZE;
294 pv.nr = find_get_pages_contig(mapping, first, count, pv.pages);
295 ASSERTCMP(pv.nr, ==, count);
296
297 for (loop = 0; loop < count; loop++) {
298 struct page *page = pv.pages[loop];
299
300 redirty_page_for_writepage(wbc, page);
301 end_page_writeback(page);
David Howells31143d52007-05-09 02:33:46 -0700302 if (page->index >= first)
303 first = page->index + 1;
304 }
305
306 __pagevec_release(&pv);
David Howells4343d002017-11-02 15:27:52 +0000307 } while (first <= last);
David Howells31143d52007-05-09 02:33:46 -0700308
309 _leave("");
310}
311
312/*
David Howellsa58823a2019-05-09 15:16:10 +0100313 * completion of write to server
314 */
315static void afs_pages_written_back(struct afs_vnode *vnode,
316 pgoff_t first, pgoff_t last)
317{
318 struct pagevec pv;
319 unsigned long priv;
320 unsigned count, loop;
321
322 _enter("{%llx:%llu},{%lx-%lx}",
323 vnode->fid.vid, vnode->fid.vnode, first, last);
324
325 pagevec_init(&pv);
326
327 do {
328 _debug("done %lx-%lx", first, last);
329
330 count = last - first + 1;
331 if (count > PAGEVEC_SIZE)
332 count = PAGEVEC_SIZE;
333 pv.nr = find_get_pages_contig(vnode->vfs_inode.i_mapping,
334 first, count, pv.pages);
335 ASSERTCMP(pv.nr, ==, count);
336
337 for (loop = 0; loop < count; loop++) {
338 priv = page_private(pv.pages[loop]);
339 trace_afs_page_dirty(vnode, tracepoint_string("clear"),
340 pv.pages[loop]->index, priv);
341 set_page_private(pv.pages[loop], 0);
342 end_page_writeback(pv.pages[loop]);
343 }
344 first += count;
345 __pagevec_release(&pv);
346 } while (first <= last);
347
348 afs_prune_wb_keys(vnode);
349 _leave("");
350}
351
352/*
David Howellsd2ddc772017-11-02 15:27:50 +0000353 * write to a file
354 */
David Howells4343d002017-11-02 15:27:52 +0000355static int afs_store_data(struct address_space *mapping,
356 pgoff_t first, pgoff_t last,
David Howellsd2ddc772017-11-02 15:27:50 +0000357 unsigned offset, unsigned to)
358{
David Howells4343d002017-11-02 15:27:52 +0000359 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
David Howellsd2ddc772017-11-02 15:27:50 +0000360 struct afs_fs_cursor fc;
David Howellsa58823a2019-05-09 15:16:10 +0100361 struct afs_status_cb *scb;
David Howells4343d002017-11-02 15:27:52 +0000362 struct afs_wb_key *wbk = NULL;
363 struct list_head *p;
364 int ret = -ENOKEY, ret2;
David Howellsd2ddc772017-11-02 15:27:50 +0000365
David Howells3b6492d2018-10-20 00:57:57 +0100366 _enter("%s{%llx:%llu.%u},%lx,%lx,%x,%x",
David Howellsd2ddc772017-11-02 15:27:50 +0000367 vnode->volume->name,
368 vnode->fid.vid,
369 vnode->fid.vnode,
370 vnode->fid.unique,
David Howellsd2ddc772017-11-02 15:27:50 +0000371 first, last, offset, to);
372
David Howellsa58823a2019-05-09 15:16:10 +0100373 scb = kzalloc(sizeof(struct afs_status_cb), GFP_NOFS);
374 if (!scb)
375 return -ENOMEM;
376
David Howells4343d002017-11-02 15:27:52 +0000377 spin_lock(&vnode->wb_lock);
378 p = vnode->wb_keys.next;
379
380 /* Iterate through the list looking for a valid key to use. */
381try_next_key:
382 while (p != &vnode->wb_keys) {
383 wbk = list_entry(p, struct afs_wb_key, vnode_link);
384 _debug("wbk %u", key_serial(wbk->key));
385 ret2 = key_validate(wbk->key);
386 if (ret2 == 0)
387 goto found_key;
388 if (ret == -ENOKEY)
389 ret = ret2;
390 p = p->next;
391 }
392
393 spin_unlock(&vnode->wb_lock);
394 afs_put_wb_key(wbk);
David Howellsa58823a2019-05-09 15:16:10 +0100395 kfree(scb);
David Howells4343d002017-11-02 15:27:52 +0000396 _leave(" = %d [no keys]", ret);
397 return ret;
398
399found_key:
400 refcount_inc(&wbk->usage);
401 spin_unlock(&vnode->wb_lock);
402
403 _debug("USE WB KEY %u", key_serial(wbk->key));
404
David Howellsd2ddc772017-11-02 15:27:50 +0000405 ret = -ERESTARTSYS;
David Howells20b83912019-05-08 16:16:31 +0100406 if (afs_begin_vnode_operation(&fc, vnode, wbk->key, false)) {
David Howellsa58823a2019-05-09 15:16:10 +0100407 afs_dataversion_t data_version = vnode->status.data_version + 1;
408
David Howellsd2ddc772017-11-02 15:27:50 +0000409 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +0100410 fc.cb_break = afs_calc_vnode_cb_break(vnode);
David Howellsa58823a2019-05-09 15:16:10 +0100411 afs_fs_store_data(&fc, mapping, first, last, offset, to, scb);
David Howellsd2ddc772017-11-02 15:27:50 +0000412 }
413
David Howellsa58823a2019-05-09 15:16:10 +0100414 afs_check_for_remote_deletion(&fc, vnode);
415 afs_vnode_commit_status(&fc, vnode, fc.cb_break,
416 &data_version, scb);
417 if (fc.ac.error == 0)
418 afs_pages_written_back(vnode, first, last);
David Howellsd2ddc772017-11-02 15:27:50 +0000419 ret = afs_end_vnode_operation(&fc);
420 }
421
David Howells4343d002017-11-02 15:27:52 +0000422 switch (ret) {
David Howells76a5cb62018-04-06 14:17:26 +0100423 case 0:
424 afs_stat_v(vnode, n_stores);
425 atomic_long_add((last * PAGE_SIZE + to) -
426 (first * PAGE_SIZE + offset),
427 &afs_v2net(vnode)->n_store_bytes);
428 break;
David Howells4343d002017-11-02 15:27:52 +0000429 case -EACCES:
430 case -EPERM:
431 case -ENOKEY:
432 case -EKEYEXPIRED:
433 case -EKEYREJECTED:
434 case -EKEYREVOKED:
435 _debug("next");
436 spin_lock(&vnode->wb_lock);
437 p = wbk->vnode_link.next;
438 afs_put_wb_key(wbk);
439 goto try_next_key;
440 }
441
442 afs_put_wb_key(wbk);
David Howellsa58823a2019-05-09 15:16:10 +0100443 kfree(scb);
David Howellsd2ddc772017-11-02 15:27:50 +0000444 _leave(" = %d", ret);
445 return ret;
446}
447
448/*
David Howells4343d002017-11-02 15:27:52 +0000449 * Synchronously write back the locked page and any subsequent non-locked dirty
450 * pages.
David Howells31143d52007-05-09 02:33:46 -0700451 */
David Howells4343d002017-11-02 15:27:52 +0000452static int afs_write_back_from_locked_page(struct address_space *mapping,
453 struct writeback_control *wbc,
454 struct page *primary_page,
455 pgoff_t final_page)
David Howells31143d52007-05-09 02:33:46 -0700456{
David Howells13524ab2017-11-02 15:27:53 +0000457 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
David Howells31143d52007-05-09 02:33:46 -0700458 struct page *pages[8], *page;
David Howells4343d002017-11-02 15:27:52 +0000459 unsigned long count, priv;
460 unsigned n, offset, to, f, t;
David Howells31143d52007-05-09 02:33:46 -0700461 pgoff_t start, first, last;
462 int loop, ret;
463
464 _enter(",%lx", primary_page->index);
465
466 count = 1;
David Howells31143d52007-05-09 02:33:46 -0700467 if (test_set_page_writeback(primary_page))
468 BUG();
469
David Howells4343d002017-11-02 15:27:52 +0000470 /* Find all consecutive lockable dirty pages that have contiguous
471 * written regions, stopping when we find a page that is not
472 * immediately lockable, is not dirty or is missing, or we reach the
473 * end of the range.
474 */
David Howells31143d52007-05-09 02:33:46 -0700475 start = primary_page->index;
David Howells4343d002017-11-02 15:27:52 +0000476 priv = page_private(primary_page);
477 offset = priv & AFS_PRIV_MAX;
478 to = priv >> AFS_PRIV_SHIFT;
David Howells13524ab2017-11-02 15:27:53 +0000479 trace_afs_page_dirty(vnode, tracepoint_string("store"),
480 primary_page->index, priv);
David Howells4343d002017-11-02 15:27:52 +0000481
482 WARN_ON(offset == to);
David Howells13524ab2017-11-02 15:27:53 +0000483 if (offset == to)
484 trace_afs_page_dirty(vnode, tracepoint_string("WARN"),
485 primary_page->index, priv);
David Howells4343d002017-11-02 15:27:52 +0000486
David Howells5a813272018-04-06 14:17:26 +0100487 if (start >= final_page ||
488 (to < PAGE_SIZE && !test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags)))
David Howells31143d52007-05-09 02:33:46 -0700489 goto no_more;
David Howells4343d002017-11-02 15:27:52 +0000490
David Howells31143d52007-05-09 02:33:46 -0700491 start++;
492 do {
493 _debug("more %lx [%lx]", start, count);
David Howells4343d002017-11-02 15:27:52 +0000494 n = final_page - start + 1;
David Howells31143d52007-05-09 02:33:46 -0700495 if (n > ARRAY_SIZE(pages))
496 n = ARRAY_SIZE(pages);
David Howells4343d002017-11-02 15:27:52 +0000497 n = find_get_pages_contig(mapping, start, ARRAY_SIZE(pages), pages);
David Howells31143d52007-05-09 02:33:46 -0700498 _debug("fgpc %u", n);
499 if (n == 0)
500 goto no_more;
501 if (pages[0]->index != start) {
David Howells9d577b62007-05-10 22:22:19 -0700502 do {
503 put_page(pages[--n]);
504 } while (n > 0);
David Howells31143d52007-05-09 02:33:46 -0700505 goto no_more;
506 }
507
508 for (loop = 0; loop < n; loop++) {
509 page = pages[loop];
David Howells5a813272018-04-06 14:17:26 +0100510 if (to != PAGE_SIZE &&
511 !test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags))
512 break;
David Howells4343d002017-11-02 15:27:52 +0000513 if (page->index > final_page)
David Howells31143d52007-05-09 02:33:46 -0700514 break;
Nick Piggin529ae9a2008-08-02 12:01:03 +0200515 if (!trylock_page(page))
David Howells31143d52007-05-09 02:33:46 -0700516 break;
David Howells4343d002017-11-02 15:27:52 +0000517 if (!PageDirty(page) || PageWriteback(page)) {
David Howells31143d52007-05-09 02:33:46 -0700518 unlock_page(page);
519 break;
520 }
David Howells4343d002017-11-02 15:27:52 +0000521
522 priv = page_private(page);
523 f = priv & AFS_PRIV_MAX;
524 t = priv >> AFS_PRIV_SHIFT;
David Howells5a813272018-04-06 14:17:26 +0100525 if (f != 0 &&
526 !test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags)) {
David Howells4343d002017-11-02 15:27:52 +0000527 unlock_page(page);
528 break;
529 }
530 to = t;
531
David Howells13524ab2017-11-02 15:27:53 +0000532 trace_afs_page_dirty(vnode, tracepoint_string("store+"),
533 page->index, priv);
534
David Howells31143d52007-05-09 02:33:46 -0700535 if (!clear_page_dirty_for_io(page))
536 BUG();
537 if (test_set_page_writeback(page))
538 BUG();
539 unlock_page(page);
540 put_page(page);
541 }
542 count += loop;
543 if (loop < n) {
544 for (; loop < n; loop++)
545 put_page(pages[loop]);
546 goto no_more;
547 }
548
549 start += loop;
David Howells4343d002017-11-02 15:27:52 +0000550 } while (start <= final_page && count < 65536);
David Howells31143d52007-05-09 02:33:46 -0700551
552no_more:
David Howells4343d002017-11-02 15:27:52 +0000553 /* We now have a contiguous set of dirty pages, each with writeback
554 * set; the first page is still locked at this point, but all the rest
555 * have been unlocked.
556 */
557 unlock_page(primary_page);
558
David Howells31143d52007-05-09 02:33:46 -0700559 first = primary_page->index;
560 last = first + count - 1;
561
David Howells31143d52007-05-09 02:33:46 -0700562 _debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to);
563
David Howells4343d002017-11-02 15:27:52 +0000564 ret = afs_store_data(mapping, first, last, offset, to);
565 switch (ret) {
566 case 0:
David Howells31143d52007-05-09 02:33:46 -0700567 ret = count;
David Howells4343d002017-11-02 15:27:52 +0000568 break;
569
570 default:
571 pr_notice("kAFS: Unexpected error from FS.StoreData %d\n", ret);
572 /* Fall through */
573 case -EACCES:
574 case -EPERM:
575 case -ENOKEY:
576 case -EKEYEXPIRED:
577 case -EKEYREJECTED:
578 case -EKEYREVOKED:
579 afs_redirty_pages(wbc, mapping, first, last);
580 mapping_set_error(mapping, ret);
581 break;
582
583 case -EDQUOT:
584 case -ENOSPC:
585 afs_redirty_pages(wbc, mapping, first, last);
586 mapping_set_error(mapping, -ENOSPC);
587 break;
588
589 case -EROFS:
590 case -EIO:
591 case -EREMOTEIO:
592 case -EFBIG:
593 case -ENOENT:
594 case -ENOMEDIUM:
595 case -ENXIO:
David Howellsf51375c2018-10-20 00:57:57 +0100596 trace_afs_file_error(vnode, ret, afs_file_error_writeback_fail);
David Howells4343d002017-11-02 15:27:52 +0000597 afs_kill_pages(mapping, first, last);
598 mapping_set_error(mapping, ret);
599 break;
David Howells31143d52007-05-09 02:33:46 -0700600 }
601
602 _leave(" = %d", ret);
603 return ret;
604}
605
606/*
607 * write a page back to the server
608 * - the caller locked the page for us
609 */
610int afs_writepage(struct page *page, struct writeback_control *wbc)
611{
David Howells31143d52007-05-09 02:33:46 -0700612 int ret;
613
614 _enter("{%lx},", page->index);
615
David Howells4343d002017-11-02 15:27:52 +0000616 ret = afs_write_back_from_locked_page(page->mapping, wbc, page,
617 wbc->range_end >> PAGE_SHIFT);
David Howells31143d52007-05-09 02:33:46 -0700618 if (ret < 0) {
619 _leave(" = %d", ret);
620 return 0;
621 }
622
623 wbc->nr_to_write -= ret;
David Howells31143d52007-05-09 02:33:46 -0700624
625 _leave(" = 0");
626 return 0;
627}
628
629/*
630 * write a region of pages back to the server
631 */
Adrian Bunkc1206a22007-10-16 23:26:41 -0700632static int afs_writepages_region(struct address_space *mapping,
633 struct writeback_control *wbc,
634 pgoff_t index, pgoff_t end, pgoff_t *_next)
David Howells31143d52007-05-09 02:33:46 -0700635{
David Howells31143d52007-05-09 02:33:46 -0700636 struct page *page;
637 int ret, n;
638
639 _enter(",,%lx,%lx,", index, end);
640
641 do {
Jan Karaaef6e412017-11-15 17:35:23 -0800642 n = find_get_pages_range_tag(mapping, &index, end,
643 PAGECACHE_TAG_DIRTY, 1, &page);
David Howells31143d52007-05-09 02:33:46 -0700644 if (!n)
645 break;
646
647 _debug("wback %lx", page->index);
648
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700649 /*
650 * at this point we hold neither the i_pages lock nor the
651 * page lock: the page may be truncated or invalidated
652 * (changing page->mapping to NULL), or even swizzled
653 * back from swapper_space to tmpfs file mapping
David Howells31143d52007-05-09 02:33:46 -0700654 */
David Howells4343d002017-11-02 15:27:52 +0000655 ret = lock_page_killable(page);
656 if (ret < 0) {
657 put_page(page);
658 _leave(" = %d", ret);
659 return ret;
660 }
David Howells31143d52007-05-09 02:33:46 -0700661
David Howellsc5051c72017-03-16 16:27:49 +0000662 if (page->mapping != mapping || !PageDirty(page)) {
David Howells31143d52007-05-09 02:33:46 -0700663 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300664 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700665 continue;
666 }
667
David Howellsc5051c72017-03-16 16:27:49 +0000668 if (PageWriteback(page)) {
David Howells31143d52007-05-09 02:33:46 -0700669 unlock_page(page);
David Howellsc5051c72017-03-16 16:27:49 +0000670 if (wbc->sync_mode != WB_SYNC_NONE)
671 wait_on_page_writeback(page);
David Howells29c8bbb2017-03-16 16:27:43 +0000672 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700673 continue;
674 }
675
David Howells65a15102017-03-16 16:27:49 +0000676 if (!clear_page_dirty_for_io(page))
677 BUG();
David Howells4343d002017-11-02 15:27:52 +0000678 ret = afs_write_back_from_locked_page(mapping, wbc, page, end);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300679 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700680 if (ret < 0) {
681 _leave(" = %d", ret);
682 return ret;
683 }
684
685 wbc->nr_to_write -= ret;
686
David Howells31143d52007-05-09 02:33:46 -0700687 cond_resched();
688 } while (index < end && wbc->nr_to_write > 0);
689
690 *_next = index;
691 _leave(" = 0 [%lx]", *_next);
692 return 0;
693}
694
695/*
696 * write some of the pending data back to the server
697 */
698int afs_writepages(struct address_space *mapping,
699 struct writeback_control *wbc)
700{
David Howells31143d52007-05-09 02:33:46 -0700701 pgoff_t start, end, next;
702 int ret;
703
704 _enter("");
705
David Howells31143d52007-05-09 02:33:46 -0700706 if (wbc->range_cyclic) {
707 start = mapping->writeback_index;
708 end = -1;
709 ret = afs_writepages_region(mapping, wbc, start, end, &next);
Wu Fengguang1b430be2010-10-26 14:21:26 -0700710 if (start > 0 && wbc->nr_to_write > 0 && ret == 0)
David Howells31143d52007-05-09 02:33:46 -0700711 ret = afs_writepages_region(mapping, wbc, 0, start,
712 &next);
713 mapping->writeback_index = next;
714 } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300715 end = (pgoff_t)(LLONG_MAX >> PAGE_SHIFT);
David Howells31143d52007-05-09 02:33:46 -0700716 ret = afs_writepages_region(mapping, wbc, 0, end, &next);
717 if (wbc->nr_to_write > 0)
718 mapping->writeback_index = next;
719 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300720 start = wbc->range_start >> PAGE_SHIFT;
721 end = wbc->range_end >> PAGE_SHIFT;
David Howells31143d52007-05-09 02:33:46 -0700722 ret = afs_writepages_region(mapping, wbc, start, end, &next);
723 }
724
725 _leave(" = %d", ret);
726 return ret;
727}
728
729/*
David Howells31143d52007-05-09 02:33:46 -0700730 * write to an AFS file
731 */
Al Viro50b55512014-04-03 14:13:46 -0400732ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from)
David Howells31143d52007-05-09 02:33:46 -0700733{
Al Viro496ad9a2013-01-23 17:07:38 -0500734 struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
David Howells31143d52007-05-09 02:33:46 -0700735 ssize_t result;
Al Viro50b55512014-04-03 14:13:46 -0400736 size_t count = iov_iter_count(from);
David Howells31143d52007-05-09 02:33:46 -0700737
David Howells3b6492d2018-10-20 00:57:57 +0100738 _enter("{%llx:%llu},{%zu},",
Al Viro50b55512014-04-03 14:13:46 -0400739 vnode->fid.vid, vnode->fid.vnode, count);
David Howells31143d52007-05-09 02:33:46 -0700740
741 if (IS_SWAPFILE(&vnode->vfs_inode)) {
742 printk(KERN_INFO
743 "AFS: Attempt to write to active swap file!\n");
744 return -EBUSY;
745 }
746
747 if (!count)
748 return 0;
749
Al Viro50b55512014-04-03 14:13:46 -0400750 result = generic_file_write_iter(iocb, from);
David Howells31143d52007-05-09 02:33:46 -0700751
David Howells31143d52007-05-09 02:33:46 -0700752 _leave(" = %zd", result);
753 return result;
754}
755
756/*
David Howells31143d52007-05-09 02:33:46 -0700757 * flush any dirty pages for this process, and check for write errors.
758 * - the return status from this call provides a reliable indication of
759 * whether any write errors occurred for this process.
760 */
Josef Bacik02c24a82011-07-16 20:44:56 -0400761int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
David Howells31143d52007-05-09 02:33:46 -0700762{
Al Viro3c981bf2013-09-03 13:37:45 -0400763 struct inode *inode = file_inode(file);
Al Viro3c981bf2013-09-03 13:37:45 -0400764 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells31143d52007-05-09 02:33:46 -0700765
David Howells3b6492d2018-10-20 00:57:57 +0100766 _enter("{%llx:%llu},{n=%pD},%d",
Al Viro3c981bf2013-09-03 13:37:45 -0400767 vnode->fid.vid, vnode->fid.vnode, file,
David Howells31143d52007-05-09 02:33:46 -0700768 datasync);
769
David Howells4343d002017-11-02 15:27:52 +0000770 return file_write_and_wait_range(file, start, end);
David Howells31143d52007-05-09 02:33:46 -0700771}
David Howells9b3f26c2009-04-03 16:42:41 +0100772
773/*
774 * notification that a previously read-only page is about to become writable
775 * - if it returns an error, the caller will deliver a bus error signal
776 */
Souptick Joarder0722f182018-08-23 17:00:48 -0700777vm_fault_t afs_page_mkwrite(struct vm_fault *vmf)
David Howells9b3f26c2009-04-03 16:42:41 +0100778{
David Howells1cf7a152017-11-02 15:27:52 +0000779 struct file *file = vmf->vma->vm_file;
780 struct inode *inode = file_inode(file);
781 struct afs_vnode *vnode = AFS_FS_I(inode);
782 unsigned long priv;
David Howells9b3f26c2009-04-03 16:42:41 +0100783
David Howells3b6492d2018-10-20 00:57:57 +0100784 _enter("{{%llx:%llu}},{%lx}",
David Howells1cf7a152017-11-02 15:27:52 +0000785 vnode->fid.vid, vnode->fid.vnode, vmf->page->index);
David Howells9b3f26c2009-04-03 16:42:41 +0100786
David Howells1cf7a152017-11-02 15:27:52 +0000787 sb_start_pagefault(inode->i_sb);
788
789 /* Wait for the page to be written to the cache before we allow it to
790 * be modified. We then assume the entire page will need writing back.
791 */
David Howells9b3f26c2009-04-03 16:42:41 +0100792#ifdef CONFIG_AFS_FSCACHE
David Howells1cf7a152017-11-02 15:27:52 +0000793 fscache_wait_on_page_write(vnode->cache, vmf->page);
David Howells9b3f26c2009-04-03 16:42:41 +0100794#endif
795
David Howells1cf7a152017-11-02 15:27:52 +0000796 if (PageWriteback(vmf->page) &&
797 wait_on_page_bit_killable(vmf->page, PG_writeback) < 0)
798 return VM_FAULT_RETRY;
799
800 if (lock_page_killable(vmf->page) < 0)
801 return VM_FAULT_RETRY;
802
803 /* We mustn't change page->private until writeback is complete as that
804 * details the portion of the page we need to write back and we might
805 * need to redirty the page if there's a problem.
806 */
807 wait_on_page_writeback(vmf->page);
808
809 priv = (unsigned long)PAGE_SIZE << AFS_PRIV_SHIFT; /* To */
810 priv |= 0; /* From */
David Howells13524ab2017-11-02 15:27:53 +0000811 trace_afs_page_dirty(vnode, tracepoint_string("mkwrite"),
812 vmf->page->index, priv);
David Howells1cf7a152017-11-02 15:27:52 +0000813 SetPagePrivate(vmf->page);
814 set_page_private(vmf->page, priv);
815
816 sb_end_pagefault(inode->i_sb);
817 return VM_FAULT_LOCKED;
David Howells9b3f26c2009-04-03 16:42:41 +0100818}
David Howells4343d002017-11-02 15:27:52 +0000819
820/*
821 * Prune the keys cached for writeback. The caller must hold vnode->wb_lock.
822 */
823void afs_prune_wb_keys(struct afs_vnode *vnode)
824{
825 LIST_HEAD(graveyard);
826 struct afs_wb_key *wbk, *tmp;
827
828 /* Discard unused keys */
829 spin_lock(&vnode->wb_lock);
830
831 if (!mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_WRITEBACK) &&
832 !mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_DIRTY)) {
833 list_for_each_entry_safe(wbk, tmp, &vnode->wb_keys, vnode_link) {
834 if (refcount_read(&wbk->usage) == 1)
835 list_move(&wbk->vnode_link, &graveyard);
836 }
837 }
838
839 spin_unlock(&vnode->wb_lock);
840
841 while (!list_empty(&graveyard)) {
842 wbk = list_entry(graveyard.next, struct afs_wb_key, vnode_link);
843 list_del(&wbk->vnode_link);
844 afs_put_wb_key(wbk);
845 }
846}
847
848/*
849 * Clean up a page during invalidation.
850 */
851int afs_launder_page(struct page *page)
852{
853 struct address_space *mapping = page->mapping;
854 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
855 unsigned long priv;
856 unsigned int f, t;
857 int ret = 0;
858
859 _enter("{%lx}", page->index);
860
861 priv = page_private(page);
862 if (clear_page_dirty_for_io(page)) {
863 f = 0;
864 t = PAGE_SIZE;
865 if (PagePrivate(page)) {
866 f = priv & AFS_PRIV_MAX;
867 t = priv >> AFS_PRIV_SHIFT;
868 }
869
David Howells13524ab2017-11-02 15:27:53 +0000870 trace_afs_page_dirty(vnode, tracepoint_string("launder"),
871 page->index, priv);
David Howells4343d002017-11-02 15:27:52 +0000872 ret = afs_store_data(mapping, page->index, page->index, t, f);
873 }
874
David Howells13524ab2017-11-02 15:27:53 +0000875 trace_afs_page_dirty(vnode, tracepoint_string("laundered"),
876 page->index, priv);
David Howells4343d002017-11-02 15:27:52 +0000877 set_page_private(page, 0);
878 ClearPagePrivate(page);
879
880#ifdef CONFIG_AFS_FSCACHE
881 if (PageFsCache(page)) {
882 fscache_wait_on_page_write(vnode->cache, page);
883 fscache_uncache_page(vnode->cache, page);
884 }
885#endif
886 return ret;
David Howells31143d52007-05-09 02:33:46 -0700887}