blob: 4472882f06df92d91831f60086fe32606862970a [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 Howells4343d002017-11-02 15:27:52 +000020/*
David Howells31143d52007-05-09 02:33:46 -070021 * 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 Howells31143d52007-05-09 02:33:46 -070036 int ret;
37
Anton Blanchard5e7f2332011-06-13 22:31:12 +010038 _enter(",,%llu", (unsigned long long)pos);
David Howells31143d52007-05-09 02:33:46 -070039
David Howells196ee9c2017-01-05 10:38:34 +000040 req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *),
41 GFP_KERNEL);
42 if (!req)
43 return -ENOMEM;
44
45 atomic_set(&req->usage, 1);
46 req->pos = pos;
David Howellse8e581a2017-03-16 16:27:44 +000047 req->len = len;
David Howells196ee9c2017-01-05 10:38:34 +000048 req->nr_pages = 1;
49 req->pages[0] = page;
David Howells5611ef22017-03-16 16:27:43 +000050 get_page(page);
David Howells196ee9c2017-01-05 10:38:34 +000051
David Howellsd2ddc772017-11-02 15:27:50 +000052 ret = afs_fetch_data(vnode, key, req);
David Howells196ee9c2017-01-05 10:38:34 +000053 afs_put_read(req);
David Howells31143d52007-05-09 02:33:46 -070054 if (ret < 0) {
55 if (ret == -ENOENT) {
56 _debug("got NOENT from server"
57 " - marking file deleted and stale");
58 set_bit(AFS_VNODE_DELETED, &vnode->flags);
59 ret = -ESTALE;
60 }
61 }
62
63 _leave(" = %d", ret);
64 return ret;
65}
66
67/*
David Howells31143d52007-05-09 02:33:46 -070068 * prepare to perform part of a write to a page
David Howells31143d52007-05-09 02:33:46 -070069 */
Nick Piggin15b46502008-10-15 22:04:32 -070070int afs_write_begin(struct file *file, struct address_space *mapping,
71 loff_t pos, unsigned len, unsigned flags,
72 struct page **pagep, void **fsdata)
David Howells31143d52007-05-09 02:33:46 -070073{
Al Viro496ad9a2013-01-23 17:07:38 -050074 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
Nick Piggin15b46502008-10-15 22:04:32 -070075 struct page *page;
David Howells215804a2017-11-02 15:27:52 +000076 struct key *key = afs_file_key(file);
David Howells4343d002017-11-02 15:27:52 +000077 unsigned long priv;
78 unsigned f, from = pos & (PAGE_SIZE - 1);
79 unsigned t, to = from + len;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030080 pgoff_t index = pos >> PAGE_SHIFT;
David Howells31143d52007-05-09 02:33:46 -070081 int ret;
82
83 _enter("{%x:%u},{%lx},%u,%u",
Nick Piggin15b46502008-10-15 22:04:32 -070084 vnode->fid.vid, vnode->fid.vnode, index, from, to);
David Howells31143d52007-05-09 02:33:46 -070085
David Howells4343d002017-11-02 15:27:52 +000086 /* We want to store information about how much of a page is altered in
87 * page->private.
88 */
89 BUILD_BUG_ON(PAGE_SIZE > 32768 && sizeof(page->private) < 8);
David Howells31143d52007-05-09 02:33:46 -070090
Nick Piggin54566b22009-01-04 12:00:53 -080091 page = grab_cache_page_write_begin(mapping, index, flags);
David Howells4343d002017-11-02 15:27:52 +000092 if (!page)
Nick Piggin15b46502008-10-15 22:04:32 -070093 return -ENOMEM;
Nick Piggin15b46502008-10-15 22:04:32 -070094
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030095 if (!PageUptodate(page) && len != PAGE_SIZE) {
David Howellse8e581a2017-03-16 16:27:44 +000096 ret = afs_fill_page(vnode, key, pos & PAGE_MASK, PAGE_SIZE, page);
David Howells31143d52007-05-09 02:33:46 -070097 if (ret < 0) {
David Howells6d06b0d2017-03-16 16:27:48 +000098 unlock_page(page);
99 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700100 _leave(" = %d [prep]", ret);
101 return ret;
102 }
Nick Piggin15b46502008-10-15 22:04:32 -0700103 SetPageUptodate(page);
David Howells31143d52007-05-09 02:33:46 -0700104 }
105
David Howells6d06b0d2017-03-16 16:27:48 +0000106 /* page won't leak in error case: it eventually gets cleaned off LRU */
107 *pagep = page;
108
David Howells31143d52007-05-09 02:33:46 -0700109try_again:
David Howells4343d002017-11-02 15:27:52 +0000110 /* See if this page is already partially written in a way that we can
111 * merge the new write with.
112 */
113 t = f = 0;
114 if (PagePrivate(page)) {
115 priv = page_private(page);
116 f = priv & AFS_PRIV_MAX;
117 t = priv >> AFS_PRIV_SHIFT;
118 ASSERTCMP(f, <=, t);
David Howells31143d52007-05-09 02:33:46 -0700119 }
120
David Howells4343d002017-11-02 15:27:52 +0000121 if (f != t) {
122 if (to < f || from > t)
123 goto flush_conflicting_write;
124 if (from < f)
125 f = from;
126 if (to > t)
127 t = to;
128 } else {
129 f = from;
130 t = to;
David Howells31143d52007-05-09 02:33:46 -0700131 }
132
David Howells4343d002017-11-02 15:27:52 +0000133 priv = (unsigned long)t << AFS_PRIV_SHIFT;
134 priv |= f;
David Howells13524ab2017-11-02 15:27:53 +0000135 trace_afs_page_dirty(vnode, tracepoint_string("begin"),
136 page->index, priv);
David Howells31143d52007-05-09 02:33:46 -0700137 SetPagePrivate(page);
David Howells4343d002017-11-02 15:27:52 +0000138 set_page_private(page, priv);
139 _leave(" = 0");
David Howells31143d52007-05-09 02:33:46 -0700140 return 0;
141
David Howells4343d002017-11-02 15:27:52 +0000142 /* The previous write and this write aren't adjacent or overlapping, so
143 * flush the page out.
144 */
145flush_conflicting_write:
David Howells31143d52007-05-09 02:33:46 -0700146 _debug("flush conflict");
David Howells4343d002017-11-02 15:27:52 +0000147 ret = write_one_page(page);
148 if (ret < 0) {
149 _leave(" = %d", ret);
150 return ret;
David Howells31143d52007-05-09 02:33:46 -0700151 }
152
David Howells4343d002017-11-02 15:27:52 +0000153 ret = lock_page_killable(page);
154 if (ret < 0) {
155 _leave(" = %d", ret);
156 return ret;
157 }
David Howells31143d52007-05-09 02:33:46 -0700158 goto try_again;
159}
160
161/*
162 * finalise part of a write to a page
163 */
Nick Piggin15b46502008-10-15 22:04:32 -0700164int afs_write_end(struct file *file, struct address_space *mapping,
165 loff_t pos, unsigned len, unsigned copied,
166 struct page *page, void *fsdata)
David Howells31143d52007-05-09 02:33:46 -0700167{
Al Viro496ad9a2013-01-23 17:07:38 -0500168 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
David Howells215804a2017-11-02 15:27:52 +0000169 struct key *key = afs_file_key(file);
David Howells31143d52007-05-09 02:33:46 -0700170 loff_t i_size, maybe_i_size;
David Howellse8e581a2017-03-16 16:27:44 +0000171 int ret;
David Howells31143d52007-05-09 02:33:46 -0700172
Nick Piggin15b46502008-10-15 22:04:32 -0700173 _enter("{%x:%u},{%lx}",
174 vnode->fid.vid, vnode->fid.vnode, page->index);
David Howells31143d52007-05-09 02:33:46 -0700175
Nick Piggin15b46502008-10-15 22:04:32 -0700176 maybe_i_size = pos + copied;
David Howells31143d52007-05-09 02:33:46 -0700177
178 i_size = i_size_read(&vnode->vfs_inode);
179 if (maybe_i_size > i_size) {
David Howells4343d002017-11-02 15:27:52 +0000180 spin_lock(&vnode->wb_lock);
David Howells31143d52007-05-09 02:33:46 -0700181 i_size = i_size_read(&vnode->vfs_inode);
182 if (maybe_i_size > i_size)
183 i_size_write(&vnode->vfs_inode, maybe_i_size);
David Howells4343d002017-11-02 15:27:52 +0000184 spin_unlock(&vnode->wb_lock);
David Howells31143d52007-05-09 02:33:46 -0700185 }
186
David Howellse8e581a2017-03-16 16:27:44 +0000187 if (!PageUptodate(page)) {
188 if (copied < len) {
189 /* Try and load any missing data from the server. The
190 * unmarshalling routine will take care of clearing any
191 * bits that are beyond the EOF.
192 */
193 ret = afs_fill_page(vnode, key, pos + copied,
194 len - copied, page);
195 if (ret < 0)
196 return ret;
197 }
198 SetPageUptodate(page);
199 }
200
David Howells31143d52007-05-09 02:33:46 -0700201 set_page_dirty(page);
David Howells31143d52007-05-09 02:33:46 -0700202 if (PageDirty(page))
203 _debug("dirtied");
Nick Piggin15b46502008-10-15 22:04:32 -0700204 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300205 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700206
Nick Piggin15b46502008-10-15 22:04:32 -0700207 return copied;
David Howells31143d52007-05-09 02:33:46 -0700208}
209
210/*
211 * kill all the pages in the given range
212 */
David Howells4343d002017-11-02 15:27:52 +0000213static void afs_kill_pages(struct address_space *mapping,
David Howells31143d52007-05-09 02:33:46 -0700214 pgoff_t first, pgoff_t last)
215{
David Howells4343d002017-11-02 15:27:52 +0000216 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
David Howells31143d52007-05-09 02:33:46 -0700217 struct pagevec pv;
218 unsigned count, loop;
219
220 _enter("{%x:%u},%lx-%lx",
221 vnode->fid.vid, vnode->fid.vnode, first, last);
222
223 pagevec_init(&pv, 0);
224
225 do {
226 _debug("kill %lx-%lx", first, last);
227
228 count = last - first + 1;
229 if (count > PAGEVEC_SIZE)
230 count = PAGEVEC_SIZE;
David Howells4343d002017-11-02 15:27:52 +0000231 pv.nr = find_get_pages_contig(mapping, first, count, pv.pages);
David Howells31143d52007-05-09 02:33:46 -0700232 ASSERTCMP(pv.nr, ==, count);
233
234 for (loop = 0; loop < count; loop++) {
David Howells7286a352017-03-16 16:27:48 +0000235 struct page *page = pv.pages[loop];
236 ClearPageUptodate(page);
David Howells4343d002017-11-02 15:27:52 +0000237 SetPageError(page);
238 end_page_writeback(page);
239 if (page->index >= first)
240 first = page->index + 1;
241 lock_page(page);
242 generic_error_remove_page(mapping, page);
243 }
244
245 __pagevec_release(&pv);
246 } while (first <= last);
247
248 _leave("");
249}
250
251/*
252 * Redirty all the pages in a given range.
253 */
254static void afs_redirty_pages(struct writeback_control *wbc,
255 struct address_space *mapping,
256 pgoff_t first, pgoff_t last)
257{
258 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
259 struct pagevec pv;
260 unsigned count, loop;
261
262 _enter("{%x:%u},%lx-%lx",
263 vnode->fid.vid, vnode->fid.vnode, first, last);
264
265 pagevec_init(&pv, 0);
266
267 do {
268 _debug("redirty %lx-%lx", first, last);
269
270 count = last - first + 1;
271 if (count > PAGEVEC_SIZE)
272 count = PAGEVEC_SIZE;
273 pv.nr = find_get_pages_contig(mapping, first, count, pv.pages);
274 ASSERTCMP(pv.nr, ==, count);
275
276 for (loop = 0; loop < count; loop++) {
277 struct page *page = pv.pages[loop];
278
279 redirty_page_for_writepage(wbc, page);
280 end_page_writeback(page);
David Howells7286a352017-03-16 16:27:48 +0000281 if (page->index >= first)
282 first = page->index + 1;
David Howells31143d52007-05-09 02:33:46 -0700283 }
284
285 __pagevec_release(&pv);
David Howells4343d002017-11-02 15:27:52 +0000286 } while (first <= last);
David Howells31143d52007-05-09 02:33:46 -0700287
288 _leave("");
289}
290
291/*
David Howellsd2ddc772017-11-02 15:27:50 +0000292 * write to a file
293 */
David Howells4343d002017-11-02 15:27:52 +0000294static int afs_store_data(struct address_space *mapping,
295 pgoff_t first, pgoff_t last,
David Howellsd2ddc772017-11-02 15:27:50 +0000296 unsigned offset, unsigned to)
297{
David Howells4343d002017-11-02 15:27:52 +0000298 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
David Howellsd2ddc772017-11-02 15:27:50 +0000299 struct afs_fs_cursor fc;
David Howells4343d002017-11-02 15:27:52 +0000300 struct afs_wb_key *wbk = NULL;
301 struct list_head *p;
302 int ret = -ENOKEY, ret2;
David Howellsd2ddc772017-11-02 15:27:50 +0000303
David Howells4343d002017-11-02 15:27:52 +0000304 _enter("%s{%x:%u.%u},%lx,%lx,%x,%x",
David Howellsd2ddc772017-11-02 15:27:50 +0000305 vnode->volume->name,
306 vnode->fid.vid,
307 vnode->fid.vnode,
308 vnode->fid.unique,
David Howellsd2ddc772017-11-02 15:27:50 +0000309 first, last, offset, to);
310
David Howells4343d002017-11-02 15:27:52 +0000311 spin_lock(&vnode->wb_lock);
312 p = vnode->wb_keys.next;
313
314 /* Iterate through the list looking for a valid key to use. */
315try_next_key:
316 while (p != &vnode->wb_keys) {
317 wbk = list_entry(p, struct afs_wb_key, vnode_link);
318 _debug("wbk %u", key_serial(wbk->key));
319 ret2 = key_validate(wbk->key);
320 if (ret2 == 0)
321 goto found_key;
322 if (ret == -ENOKEY)
323 ret = ret2;
324 p = p->next;
325 }
326
327 spin_unlock(&vnode->wb_lock);
328 afs_put_wb_key(wbk);
329 _leave(" = %d [no keys]", ret);
330 return ret;
331
332found_key:
333 refcount_inc(&wbk->usage);
334 spin_unlock(&vnode->wb_lock);
335
336 _debug("USE WB KEY %u", key_serial(wbk->key));
337
David Howellsd2ddc772017-11-02 15:27:50 +0000338 ret = -ERESTARTSYS;
David Howells4343d002017-11-02 15:27:52 +0000339 if (afs_begin_vnode_operation(&fc, vnode, wbk->key)) {
David Howellsd2ddc772017-11-02 15:27:50 +0000340 while (afs_select_fileserver(&fc)) {
341 fc.cb_break = vnode->cb_break + vnode->cb_s_break;
David Howells4343d002017-11-02 15:27:52 +0000342 afs_fs_store_data(&fc, mapping, first, last, offset, to);
David Howellsd2ddc772017-11-02 15:27:50 +0000343 }
344
345 afs_check_for_remote_deletion(&fc, fc.vnode);
346 afs_vnode_commit_status(&fc, vnode, fc.cb_break);
347 ret = afs_end_vnode_operation(&fc);
348 }
349
David Howells4343d002017-11-02 15:27:52 +0000350 switch (ret) {
351 case -EACCES:
352 case -EPERM:
353 case -ENOKEY:
354 case -EKEYEXPIRED:
355 case -EKEYREJECTED:
356 case -EKEYREVOKED:
357 _debug("next");
358 spin_lock(&vnode->wb_lock);
359 p = wbk->vnode_link.next;
360 afs_put_wb_key(wbk);
361 goto try_next_key;
362 }
363
364 afs_put_wb_key(wbk);
David Howellsd2ddc772017-11-02 15:27:50 +0000365 _leave(" = %d", ret);
366 return ret;
367}
368
369/*
David Howells4343d002017-11-02 15:27:52 +0000370 * Synchronously write back the locked page and any subsequent non-locked dirty
371 * pages.
David Howells31143d52007-05-09 02:33:46 -0700372 */
David Howells4343d002017-11-02 15:27:52 +0000373static int afs_write_back_from_locked_page(struct address_space *mapping,
374 struct writeback_control *wbc,
375 struct page *primary_page,
376 pgoff_t final_page)
David Howells31143d52007-05-09 02:33:46 -0700377{
David Howells13524ab2017-11-02 15:27:53 +0000378 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
David Howells31143d52007-05-09 02:33:46 -0700379 struct page *pages[8], *page;
David Howells4343d002017-11-02 15:27:52 +0000380 unsigned long count, priv;
381 unsigned n, offset, to, f, t;
David Howells31143d52007-05-09 02:33:46 -0700382 pgoff_t start, first, last;
383 int loop, ret;
384
385 _enter(",%lx", primary_page->index);
386
387 count = 1;
David Howells31143d52007-05-09 02:33:46 -0700388 if (test_set_page_writeback(primary_page))
389 BUG();
390
David Howells4343d002017-11-02 15:27:52 +0000391 /* Find all consecutive lockable dirty pages that have contiguous
392 * written regions, stopping when we find a page that is not
393 * immediately lockable, is not dirty or is missing, or we reach the
394 * end of the range.
395 */
David Howells31143d52007-05-09 02:33:46 -0700396 start = primary_page->index;
David Howells4343d002017-11-02 15:27:52 +0000397 priv = page_private(primary_page);
398 offset = priv & AFS_PRIV_MAX;
399 to = priv >> AFS_PRIV_SHIFT;
David Howells13524ab2017-11-02 15:27:53 +0000400 trace_afs_page_dirty(vnode, tracepoint_string("store"),
401 primary_page->index, priv);
David Howells4343d002017-11-02 15:27:52 +0000402
403 WARN_ON(offset == to);
David Howells13524ab2017-11-02 15:27:53 +0000404 if (offset == to)
405 trace_afs_page_dirty(vnode, tracepoint_string("WARN"),
406 primary_page->index, priv);
David Howells4343d002017-11-02 15:27:52 +0000407
408 if (start >= final_page || to < PAGE_SIZE)
David Howells31143d52007-05-09 02:33:46 -0700409 goto no_more;
David Howells4343d002017-11-02 15:27:52 +0000410
David Howells31143d52007-05-09 02:33:46 -0700411 start++;
412 do {
413 _debug("more %lx [%lx]", start, count);
David Howells4343d002017-11-02 15:27:52 +0000414 n = final_page - start + 1;
David Howells31143d52007-05-09 02:33:46 -0700415 if (n > ARRAY_SIZE(pages))
416 n = ARRAY_SIZE(pages);
David Howells4343d002017-11-02 15:27:52 +0000417 n = find_get_pages_contig(mapping, start, ARRAY_SIZE(pages), pages);
David Howells31143d52007-05-09 02:33:46 -0700418 _debug("fgpc %u", n);
419 if (n == 0)
420 goto no_more;
421 if (pages[0]->index != start) {
David Howells9d577b62007-05-10 22:22:19 -0700422 do {
423 put_page(pages[--n]);
424 } while (n > 0);
David Howells31143d52007-05-09 02:33:46 -0700425 goto no_more;
426 }
427
428 for (loop = 0; loop < n; loop++) {
David Howells4343d002017-11-02 15:27:52 +0000429 if (to != PAGE_SIZE)
430 break;
David Howells31143d52007-05-09 02:33:46 -0700431 page = pages[loop];
David Howells4343d002017-11-02 15:27:52 +0000432 if (page->index > final_page)
David Howells31143d52007-05-09 02:33:46 -0700433 break;
Nick Piggin529ae9a2008-08-02 12:01:03 +0200434 if (!trylock_page(page))
David Howells31143d52007-05-09 02:33:46 -0700435 break;
David Howells4343d002017-11-02 15:27:52 +0000436 if (!PageDirty(page) || PageWriteback(page)) {
David Howells31143d52007-05-09 02:33:46 -0700437 unlock_page(page);
438 break;
439 }
David Howells4343d002017-11-02 15:27:52 +0000440
441 priv = page_private(page);
442 f = priv & AFS_PRIV_MAX;
443 t = priv >> AFS_PRIV_SHIFT;
444 if (f != 0) {
445 unlock_page(page);
446 break;
447 }
448 to = t;
449
David Howells13524ab2017-11-02 15:27:53 +0000450 trace_afs_page_dirty(vnode, tracepoint_string("store+"),
451 page->index, priv);
452
David Howells31143d52007-05-09 02:33:46 -0700453 if (!clear_page_dirty_for_io(page))
454 BUG();
455 if (test_set_page_writeback(page))
456 BUG();
457 unlock_page(page);
458 put_page(page);
459 }
460 count += loop;
461 if (loop < n) {
462 for (; loop < n; loop++)
463 put_page(pages[loop]);
464 goto no_more;
465 }
466
467 start += loop;
David Howells4343d002017-11-02 15:27:52 +0000468 } while (start <= final_page && count < 65536);
David Howells31143d52007-05-09 02:33:46 -0700469
470no_more:
David Howells4343d002017-11-02 15:27:52 +0000471 /* We now have a contiguous set of dirty pages, each with writeback
472 * set; the first page is still locked at this point, but all the rest
473 * have been unlocked.
474 */
475 unlock_page(primary_page);
476
David Howells31143d52007-05-09 02:33:46 -0700477 first = primary_page->index;
478 last = first + count - 1;
479
David Howells31143d52007-05-09 02:33:46 -0700480 _debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to);
481
David Howells4343d002017-11-02 15:27:52 +0000482 ret = afs_store_data(mapping, first, last, offset, to);
483 switch (ret) {
484 case 0:
David Howells31143d52007-05-09 02:33:46 -0700485 ret = count;
David Howells4343d002017-11-02 15:27:52 +0000486 break;
487
488 default:
489 pr_notice("kAFS: Unexpected error from FS.StoreData %d\n", ret);
490 /* Fall through */
491 case -EACCES:
492 case -EPERM:
493 case -ENOKEY:
494 case -EKEYEXPIRED:
495 case -EKEYREJECTED:
496 case -EKEYREVOKED:
497 afs_redirty_pages(wbc, mapping, first, last);
498 mapping_set_error(mapping, ret);
499 break;
500
501 case -EDQUOT:
502 case -ENOSPC:
503 afs_redirty_pages(wbc, mapping, first, last);
504 mapping_set_error(mapping, -ENOSPC);
505 break;
506
507 case -EROFS:
508 case -EIO:
509 case -EREMOTEIO:
510 case -EFBIG:
511 case -ENOENT:
512 case -ENOMEDIUM:
513 case -ENXIO:
514 afs_kill_pages(mapping, first, last);
515 mapping_set_error(mapping, ret);
516 break;
David Howells31143d52007-05-09 02:33:46 -0700517 }
518
519 _leave(" = %d", ret);
520 return ret;
521}
522
523/*
524 * write a page back to the server
525 * - the caller locked the page for us
526 */
527int afs_writepage(struct page *page, struct writeback_control *wbc)
528{
David Howells31143d52007-05-09 02:33:46 -0700529 int ret;
530
531 _enter("{%lx},", page->index);
532
David Howells4343d002017-11-02 15:27:52 +0000533 ret = afs_write_back_from_locked_page(page->mapping, wbc, page,
534 wbc->range_end >> PAGE_SHIFT);
David Howells31143d52007-05-09 02:33:46 -0700535 if (ret < 0) {
536 _leave(" = %d", ret);
537 return 0;
538 }
539
540 wbc->nr_to_write -= ret;
David Howells31143d52007-05-09 02:33:46 -0700541
542 _leave(" = 0");
543 return 0;
544}
545
546/*
547 * write a region of pages back to the server
548 */
Adrian Bunkc1206a22007-10-16 23:26:41 -0700549static int afs_writepages_region(struct address_space *mapping,
550 struct writeback_control *wbc,
551 pgoff_t index, pgoff_t end, pgoff_t *_next)
David Howells31143d52007-05-09 02:33:46 -0700552{
David Howells31143d52007-05-09 02:33:46 -0700553 struct page *page;
554 int ret, n;
555
556 _enter(",,%lx,%lx,", index, end);
557
558 do {
559 n = find_get_pages_tag(mapping, &index, PAGECACHE_TAG_DIRTY,
560 1, &page);
561 if (!n)
562 break;
563
564 _debug("wback %lx", page->index);
565
566 if (page->index > end) {
567 *_next = index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300568 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700569 _leave(" = 0 [%lx]", *_next);
570 return 0;
571 }
572
573 /* at this point we hold neither mapping->tree_lock nor lock on
574 * the page itself: the page may be truncated or invalidated
575 * (changing page->mapping to NULL), or even swizzled back from
576 * swapper_space to tmpfs file mapping
577 */
David Howells4343d002017-11-02 15:27:52 +0000578 ret = lock_page_killable(page);
579 if (ret < 0) {
580 put_page(page);
581 _leave(" = %d", ret);
582 return ret;
583 }
David Howells31143d52007-05-09 02:33:46 -0700584
David Howellsc5051c72017-03-16 16:27:49 +0000585 if (page->mapping != mapping || !PageDirty(page)) {
David Howells31143d52007-05-09 02:33:46 -0700586 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300587 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700588 continue;
589 }
590
David Howellsc5051c72017-03-16 16:27:49 +0000591 if (PageWriteback(page)) {
David Howells31143d52007-05-09 02:33:46 -0700592 unlock_page(page);
David Howellsc5051c72017-03-16 16:27:49 +0000593 if (wbc->sync_mode != WB_SYNC_NONE)
594 wait_on_page_writeback(page);
David Howells29c8bbb2017-03-16 16:27:43 +0000595 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700596 continue;
597 }
598
David Howells65a15102017-03-16 16:27:49 +0000599 if (!clear_page_dirty_for_io(page))
600 BUG();
David Howells4343d002017-11-02 15:27:52 +0000601 ret = afs_write_back_from_locked_page(mapping, wbc, page, end);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300602 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700603 if (ret < 0) {
604 _leave(" = %d", ret);
605 return ret;
606 }
607
608 wbc->nr_to_write -= ret;
609
David Howells31143d52007-05-09 02:33:46 -0700610 cond_resched();
611 } while (index < end && wbc->nr_to_write > 0);
612
613 *_next = index;
614 _leave(" = 0 [%lx]", *_next);
615 return 0;
616}
617
618/*
619 * write some of the pending data back to the server
620 */
621int afs_writepages(struct address_space *mapping,
622 struct writeback_control *wbc)
623{
David Howells31143d52007-05-09 02:33:46 -0700624 pgoff_t start, end, next;
625 int ret;
626
627 _enter("");
628
David Howells31143d52007-05-09 02:33:46 -0700629 if (wbc->range_cyclic) {
630 start = mapping->writeback_index;
631 end = -1;
632 ret = afs_writepages_region(mapping, wbc, start, end, &next);
Wu Fengguang1b430be2010-10-26 14:21:26 -0700633 if (start > 0 && wbc->nr_to_write > 0 && ret == 0)
David Howells31143d52007-05-09 02:33:46 -0700634 ret = afs_writepages_region(mapping, wbc, 0, start,
635 &next);
636 mapping->writeback_index = next;
637 } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300638 end = (pgoff_t)(LLONG_MAX >> PAGE_SHIFT);
David Howells31143d52007-05-09 02:33:46 -0700639 ret = afs_writepages_region(mapping, wbc, 0, end, &next);
640 if (wbc->nr_to_write > 0)
641 mapping->writeback_index = next;
642 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300643 start = wbc->range_start >> PAGE_SHIFT;
644 end = wbc->range_end >> PAGE_SHIFT;
David Howells31143d52007-05-09 02:33:46 -0700645 ret = afs_writepages_region(mapping, wbc, start, end, &next);
646 }
647
648 _leave(" = %d", ret);
649 return ret;
650}
651
652/*
David Howells31143d52007-05-09 02:33:46 -0700653 * completion of write to server
654 */
655void afs_pages_written_back(struct afs_vnode *vnode, struct afs_call *call)
656{
David Howells31143d52007-05-09 02:33:46 -0700657 struct pagevec pv;
David Howells13524ab2017-11-02 15:27:53 +0000658 unsigned long priv;
David Howells31143d52007-05-09 02:33:46 -0700659 unsigned count, loop;
660 pgoff_t first = call->first, last = call->last;
David Howells31143d52007-05-09 02:33:46 -0700661
662 _enter("{%x:%u},{%lx-%lx}",
663 vnode->fid.vid, vnode->fid.vnode, first, last);
664
David Howells31143d52007-05-09 02:33:46 -0700665 pagevec_init(&pv, 0);
666
667 do {
David Howells5bbf5d32007-05-10 03:15:23 -0700668 _debug("done %lx-%lx", first, last);
David Howells31143d52007-05-09 02:33:46 -0700669
670 count = last - first + 1;
671 if (count > PAGEVEC_SIZE)
672 count = PAGEVEC_SIZE;
David Howells4343d002017-11-02 15:27:52 +0000673 pv.nr = find_get_pages_contig(vnode->vfs_inode.i_mapping,
674 first, count, pv.pages);
David Howells31143d52007-05-09 02:33:46 -0700675 ASSERTCMP(pv.nr, ==, count);
676
David Howells31143d52007-05-09 02:33:46 -0700677 for (loop = 0; loop < count; loop++) {
David Howells13524ab2017-11-02 15:27:53 +0000678 priv = page_private(pv.pages[loop]);
679 trace_afs_page_dirty(vnode, tracepoint_string("clear"),
680 pv.pages[loop]->index, priv);
David Howells4343d002017-11-02 15:27:52 +0000681 set_page_private(pv.pages[loop], 0);
682 end_page_writeback(pv.pages[loop]);
David Howells31143d52007-05-09 02:33:46 -0700683 }
David Howells31143d52007-05-09 02:33:46 -0700684 first += count;
David Howells31143d52007-05-09 02:33:46 -0700685 __pagevec_release(&pv);
David Howells5bbf5d32007-05-10 03:15:23 -0700686 } while (first <= last);
David Howells31143d52007-05-09 02:33:46 -0700687
David Howells4343d002017-11-02 15:27:52 +0000688 afs_prune_wb_keys(vnode);
David Howells31143d52007-05-09 02:33:46 -0700689 _leave("");
690}
691
692/*
693 * write to an AFS file
694 */
Al Viro50b55512014-04-03 14:13:46 -0400695ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from)
David Howells31143d52007-05-09 02:33:46 -0700696{
Al Viro496ad9a2013-01-23 17:07:38 -0500697 struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
David Howells31143d52007-05-09 02:33:46 -0700698 ssize_t result;
Al Viro50b55512014-04-03 14:13:46 -0400699 size_t count = iov_iter_count(from);
David Howells31143d52007-05-09 02:33:46 -0700700
Al Viro50b55512014-04-03 14:13:46 -0400701 _enter("{%x.%u},{%zu},",
702 vnode->fid.vid, vnode->fid.vnode, count);
David Howells31143d52007-05-09 02:33:46 -0700703
704 if (IS_SWAPFILE(&vnode->vfs_inode)) {
705 printk(KERN_INFO
706 "AFS: Attempt to write to active swap file!\n");
707 return -EBUSY;
708 }
709
710 if (!count)
711 return 0;
712
Al Viro50b55512014-04-03 14:13:46 -0400713 result = generic_file_write_iter(iocb, from);
David Howells31143d52007-05-09 02:33:46 -0700714
David Howells31143d52007-05-09 02:33:46 -0700715 _leave(" = %zd", result);
716 return result;
717}
718
719/*
David Howells31143d52007-05-09 02:33:46 -0700720 * flush any dirty pages for this process, and check for write errors.
721 * - the return status from this call provides a reliable indication of
722 * whether any write errors occurred for this process.
723 */
Josef Bacik02c24a82011-07-16 20:44:56 -0400724int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
David Howells31143d52007-05-09 02:33:46 -0700725{
Al Viro3c981bf2013-09-03 13:37:45 -0400726 struct inode *inode = file_inode(file);
Al Viro3c981bf2013-09-03 13:37:45 -0400727 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells31143d52007-05-09 02:33:46 -0700728
Al Viro3c981bf2013-09-03 13:37:45 -0400729 _enter("{%x:%u},{n=%pD},%d",
730 vnode->fid.vid, vnode->fid.vnode, file,
David Howells31143d52007-05-09 02:33:46 -0700731 datasync);
732
David Howells4343d002017-11-02 15:27:52 +0000733 return file_write_and_wait_range(file, start, end);
David Howells31143d52007-05-09 02:33:46 -0700734}
David Howells9b3f26c2009-04-03 16:42:41 +0100735
736/*
David Howells58fed942017-03-16 16:27:45 +0000737 * Flush out all outstanding writes on a file opened for writing when it is
738 * closed.
739 */
740int afs_flush(struct file *file, fl_owner_t id)
741{
742 _enter("");
743
744 if ((file->f_mode & FMODE_WRITE) == 0)
745 return 0;
746
747 return vfs_fsync(file, 0);
748}
749
750/*
David Howells9b3f26c2009-04-03 16:42:41 +0100751 * notification that a previously read-only page is about to become writable
752 * - if it returns an error, the caller will deliver a bus error signal
753 */
David Howells1cf7a152017-11-02 15:27:52 +0000754int afs_page_mkwrite(struct vm_fault *vmf)
David Howells9b3f26c2009-04-03 16:42:41 +0100755{
David Howells1cf7a152017-11-02 15:27:52 +0000756 struct file *file = vmf->vma->vm_file;
757 struct inode *inode = file_inode(file);
758 struct afs_vnode *vnode = AFS_FS_I(inode);
759 unsigned long priv;
David Howells9b3f26c2009-04-03 16:42:41 +0100760
761 _enter("{{%x:%u}},{%lx}",
David Howells1cf7a152017-11-02 15:27:52 +0000762 vnode->fid.vid, vnode->fid.vnode, vmf->page->index);
David Howells9b3f26c2009-04-03 16:42:41 +0100763
David Howells1cf7a152017-11-02 15:27:52 +0000764 sb_start_pagefault(inode->i_sb);
765
766 /* Wait for the page to be written to the cache before we allow it to
767 * be modified. We then assume the entire page will need writing back.
768 */
David Howells9b3f26c2009-04-03 16:42:41 +0100769#ifdef CONFIG_AFS_FSCACHE
David Howells1cf7a152017-11-02 15:27:52 +0000770 fscache_wait_on_page_write(vnode->cache, vmf->page);
David Howells9b3f26c2009-04-03 16:42:41 +0100771#endif
772
David Howells1cf7a152017-11-02 15:27:52 +0000773 if (PageWriteback(vmf->page) &&
774 wait_on_page_bit_killable(vmf->page, PG_writeback) < 0)
775 return VM_FAULT_RETRY;
776
777 if (lock_page_killable(vmf->page) < 0)
778 return VM_FAULT_RETRY;
779
780 /* We mustn't change page->private until writeback is complete as that
781 * details the portion of the page we need to write back and we might
782 * need to redirty the page if there's a problem.
783 */
784 wait_on_page_writeback(vmf->page);
785
786 priv = (unsigned long)PAGE_SIZE << AFS_PRIV_SHIFT; /* To */
787 priv |= 0; /* From */
David Howells13524ab2017-11-02 15:27:53 +0000788 trace_afs_page_dirty(vnode, tracepoint_string("mkwrite"),
789 vmf->page->index, priv);
David Howells1cf7a152017-11-02 15:27:52 +0000790 SetPagePrivate(vmf->page);
791 set_page_private(vmf->page, priv);
792
793 sb_end_pagefault(inode->i_sb);
794 return VM_FAULT_LOCKED;
David Howells9b3f26c2009-04-03 16:42:41 +0100795}
David Howells4343d002017-11-02 15:27:52 +0000796
797/*
798 * Prune the keys cached for writeback. The caller must hold vnode->wb_lock.
799 */
800void afs_prune_wb_keys(struct afs_vnode *vnode)
801{
802 LIST_HEAD(graveyard);
803 struct afs_wb_key *wbk, *tmp;
804
805 /* Discard unused keys */
806 spin_lock(&vnode->wb_lock);
807
808 if (!mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_WRITEBACK) &&
809 !mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_DIRTY)) {
810 list_for_each_entry_safe(wbk, tmp, &vnode->wb_keys, vnode_link) {
811 if (refcount_read(&wbk->usage) == 1)
812 list_move(&wbk->vnode_link, &graveyard);
813 }
814 }
815
816 spin_unlock(&vnode->wb_lock);
817
818 while (!list_empty(&graveyard)) {
819 wbk = list_entry(graveyard.next, struct afs_wb_key, vnode_link);
820 list_del(&wbk->vnode_link);
821 afs_put_wb_key(wbk);
822 }
823}
824
825/*
826 * Clean up a page during invalidation.
827 */
828int afs_launder_page(struct page *page)
829{
830 struct address_space *mapping = page->mapping;
831 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
832 unsigned long priv;
833 unsigned int f, t;
834 int ret = 0;
835
836 _enter("{%lx}", page->index);
837
838 priv = page_private(page);
839 if (clear_page_dirty_for_io(page)) {
840 f = 0;
841 t = PAGE_SIZE;
842 if (PagePrivate(page)) {
843 f = priv & AFS_PRIV_MAX;
844 t = priv >> AFS_PRIV_SHIFT;
845 }
846
David Howells13524ab2017-11-02 15:27:53 +0000847 trace_afs_page_dirty(vnode, tracepoint_string("launder"),
848 page->index, priv);
David Howells4343d002017-11-02 15:27:52 +0000849 ret = afs_store_data(mapping, page->index, page->index, t, f);
850 }
851
David Howells13524ab2017-11-02 15:27:53 +0000852 trace_afs_page_dirty(vnode, tracepoint_string("laundered"),
853 page->index, priv);
David Howells4343d002017-11-02 15:27:52 +0000854 set_page_private(page, 0);
855 ClearPagePrivate(page);
856
857#ifdef CONFIG_AFS_FSCACHE
858 if (PageFsCache(page)) {
859 fscache_wait_on_page_write(vnode->cache, page);
860 fscache_uncache_page(vnode->cache, page);
861 }
862#endif
863 return ret;
864}