blob: 1cdd0e3cd531896d9f3470ce3686cc23fef7a197 [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 */
Alexey Dobriyan4af3c9c2007-10-16 23:29:23 -070011#include <linux/backing-dev.h>
David Howells31143d52007-05-09 02:33:46 -070012#include <linux/slab.h>
13#include <linux/fs.h>
14#include <linux/pagemap.h>
15#include <linux/writeback.h>
16#include <linux/pagevec.h>
17#include "internal.h"
18
19static int afs_write_back_from_locked_page(struct afs_writeback *wb,
20 struct page *page);
21
22/*
23 * mark a page as having been made dirty and thus needing writeback
24 */
25int afs_set_page_dirty(struct page *page)
26{
27 _enter("");
28 return __set_page_dirty_nobuffers(page);
29}
30
31/*
32 * unlink a writeback record because its usage has reached zero
33 * - must be called with the wb->vnode->writeback_lock held
34 */
35static void afs_unlink_writeback(struct afs_writeback *wb)
36{
37 struct afs_writeback *front;
38 struct afs_vnode *vnode = wb->vnode;
39
40 list_del_init(&wb->link);
41 if (!list_empty(&vnode->writebacks)) {
42 /* if an fsync rises to the front of the queue then wake it
43 * up */
44 front = list_entry(vnode->writebacks.next,
45 struct afs_writeback, link);
46 if (front->state == AFS_WBACK_SYNCING) {
47 _debug("wake up sync");
48 front->state = AFS_WBACK_COMPLETE;
49 wake_up(&front->waitq);
50 }
51 }
52}
53
54/*
55 * free a writeback record
56 */
57static void afs_free_writeback(struct afs_writeback *wb)
58{
59 _enter("");
60 key_put(wb->key);
61 kfree(wb);
62}
63
64/*
65 * dispose of a reference to a writeback record
66 */
67void afs_put_writeback(struct afs_writeback *wb)
68{
69 struct afs_vnode *vnode = wb->vnode;
70
71 _enter("{%d}", wb->usage);
72
73 spin_lock(&vnode->writeback_lock);
74 if (--wb->usage == 0)
75 afs_unlink_writeback(wb);
76 else
77 wb = NULL;
78 spin_unlock(&vnode->writeback_lock);
79 if (wb)
80 afs_free_writeback(wb);
81}
82
83/*
84 * partly or wholly fill a page that's under preparation for writing
85 */
86static int afs_fill_page(struct afs_vnode *vnode, struct key *key,
David Howellse8e581a2017-03-16 16:27:44 +000087 loff_t pos, unsigned int len, struct page *page)
David Howells31143d52007-05-09 02:33:46 -070088{
David Howells196ee9c2017-01-05 10:38:34 +000089 struct afs_read *req;
David Howells31143d52007-05-09 02:33:46 -070090 int ret;
91
Anton Blanchard5e7f2332011-06-13 22:31:12 +010092 _enter(",,%llu", (unsigned long long)pos);
David Howells31143d52007-05-09 02:33:46 -070093
David Howells196ee9c2017-01-05 10:38:34 +000094 req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *),
95 GFP_KERNEL);
96 if (!req)
97 return -ENOMEM;
98
99 atomic_set(&req->usage, 1);
100 req->pos = pos;
David Howellse8e581a2017-03-16 16:27:44 +0000101 req->len = len;
David Howells196ee9c2017-01-05 10:38:34 +0000102 req->nr_pages = 1;
103 req->pages[0] = page;
David Howells5611ef22017-03-16 16:27:43 +0000104 get_page(page);
David Howells196ee9c2017-01-05 10:38:34 +0000105
David Howellsd2ddc772017-11-02 15:27:50 +0000106 ret = afs_fetch_data(vnode, key, req);
David Howells196ee9c2017-01-05 10:38:34 +0000107 afs_put_read(req);
David Howells31143d52007-05-09 02:33:46 -0700108 if (ret < 0) {
109 if (ret == -ENOENT) {
110 _debug("got NOENT from server"
111 " - marking file deleted and stale");
112 set_bit(AFS_VNODE_DELETED, &vnode->flags);
113 ret = -ESTALE;
114 }
115 }
116
117 _leave(" = %d", ret);
118 return ret;
119}
120
121/*
David Howells31143d52007-05-09 02:33:46 -0700122 * prepare to perform part of a write to a page
David Howells31143d52007-05-09 02:33:46 -0700123 */
Nick Piggin15b46502008-10-15 22:04:32 -0700124int afs_write_begin(struct file *file, struct address_space *mapping,
125 loff_t pos, unsigned len, unsigned flags,
126 struct page **pagep, void **fsdata)
David Howells31143d52007-05-09 02:33:46 -0700127{
128 struct afs_writeback *candidate, *wb;
Al Viro496ad9a2013-01-23 17:07:38 -0500129 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
Nick Piggin15b46502008-10-15 22:04:32 -0700130 struct page *page;
David Howells215804a2017-11-02 15:27:52 +0000131 struct key *key = afs_file_key(file);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300132 unsigned from = pos & (PAGE_SIZE - 1);
Nick Piggin15b46502008-10-15 22:04:32 -0700133 unsigned to = from + len;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300134 pgoff_t index = pos >> PAGE_SHIFT;
David Howells31143d52007-05-09 02:33:46 -0700135 int ret;
136
137 _enter("{%x:%u},{%lx},%u,%u",
Nick Piggin15b46502008-10-15 22:04:32 -0700138 vnode->fid.vid, vnode->fid.vnode, index, from, to);
David Howells31143d52007-05-09 02:33:46 -0700139
140 candidate = kzalloc(sizeof(*candidate), GFP_KERNEL);
141 if (!candidate)
142 return -ENOMEM;
143 candidate->vnode = vnode;
Nick Piggin15b46502008-10-15 22:04:32 -0700144 candidate->first = candidate->last = index;
145 candidate->offset_first = from;
David Howells31143d52007-05-09 02:33:46 -0700146 candidate->to_last = to;
Anton Blanchardf129ccc2011-02-25 15:33:02 +0000147 INIT_LIST_HEAD(&candidate->link);
David Howells31143d52007-05-09 02:33:46 -0700148 candidate->usage = 1;
149 candidate->state = AFS_WBACK_PENDING;
150 init_waitqueue_head(&candidate->waitq);
151
Nick Piggin54566b22009-01-04 12:00:53 -0800152 page = grab_cache_page_write_begin(mapping, index, flags);
Nick Piggin15b46502008-10-15 22:04:32 -0700153 if (!page) {
154 kfree(candidate);
155 return -ENOMEM;
156 }
Nick Piggin15b46502008-10-15 22:04:32 -0700157
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300158 if (!PageUptodate(page) && len != PAGE_SIZE) {
David Howellse8e581a2017-03-16 16:27:44 +0000159 ret = afs_fill_page(vnode, key, pos & PAGE_MASK, PAGE_SIZE, page);
David Howells31143d52007-05-09 02:33:46 -0700160 if (ret < 0) {
David Howells6d06b0d2017-03-16 16:27:48 +0000161 unlock_page(page);
162 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700163 kfree(candidate);
164 _leave(" = %d [prep]", ret);
165 return ret;
166 }
Nick Piggin15b46502008-10-15 22:04:32 -0700167 SetPageUptodate(page);
David Howells31143d52007-05-09 02:33:46 -0700168 }
169
David Howells6d06b0d2017-03-16 16:27:48 +0000170 /* page won't leak in error case: it eventually gets cleaned off LRU */
171 *pagep = page;
172
David Howells31143d52007-05-09 02:33:46 -0700173try_again:
David Howells31143d52007-05-09 02:33:46 -0700174 spin_lock(&vnode->writeback_lock);
175
176 /* see if this page is already pending a writeback under a suitable key
177 * - if so we can just join onto that one */
178 wb = (struct afs_writeback *) page_private(page);
179 if (wb) {
180 if (wb->key == key && wb->state == AFS_WBACK_PENDING)
181 goto subsume_in_current_wb;
182 goto flush_conflicting_wb;
183 }
184
185 if (index > 0) {
186 /* see if we can find an already pending writeback that we can
187 * append this page to */
188 list_for_each_entry(wb, &vnode->writebacks, link) {
189 if (wb->last == index - 1 && wb->key == key &&
190 wb->state == AFS_WBACK_PENDING)
191 goto append_to_previous_wb;
192 }
193 }
194
195 list_add_tail(&candidate->link, &vnode->writebacks);
196 candidate->key = key_get(key);
197 spin_unlock(&vnode->writeback_lock);
198 SetPagePrivate(page);
199 set_page_private(page, (unsigned long) candidate);
200 _leave(" = 0 [new]");
201 return 0;
202
203subsume_in_current_wb:
204 _debug("subsume");
205 ASSERTRANGE(wb->first, <=, index, <=, wb->last);
Nick Piggin15b46502008-10-15 22:04:32 -0700206 if (index == wb->first && from < wb->offset_first)
207 wb->offset_first = from;
David Howells31143d52007-05-09 02:33:46 -0700208 if (index == wb->last && to > wb->to_last)
209 wb->to_last = to;
210 spin_unlock(&vnode->writeback_lock);
211 kfree(candidate);
212 _leave(" = 0 [sub]");
213 return 0;
214
215append_to_previous_wb:
216 _debug("append into %lx-%lx", wb->first, wb->last);
217 wb->usage++;
218 wb->last++;
219 wb->to_last = to;
220 spin_unlock(&vnode->writeback_lock);
221 SetPagePrivate(page);
222 set_page_private(page, (unsigned long) wb);
223 kfree(candidate);
224 _leave(" = 0 [app]");
225 return 0;
226
227 /* the page is currently bound to another context, so if it's dirty we
228 * need to flush it before we can use the new context */
229flush_conflicting_wb:
230 _debug("flush conflict");
231 if (wb->state == AFS_WBACK_PENDING)
232 wb->state = AFS_WBACK_CONFLICTING;
233 spin_unlock(&vnode->writeback_lock);
David Howells65a15102017-03-16 16:27:49 +0000234 if (clear_page_dirty_for_io(page)) {
David Howells31143d52007-05-09 02:33:46 -0700235 ret = afs_write_back_from_locked_page(wb, page);
236 if (ret < 0) {
237 afs_put_writeback(candidate);
238 _leave(" = %d", ret);
239 return ret;
240 }
241 }
242
243 /* the page holds a ref on the writeback record */
244 afs_put_writeback(wb);
245 set_page_private(page, 0);
246 ClearPagePrivate(page);
247 goto try_again;
248}
249
250/*
251 * finalise part of a write to a page
252 */
Nick Piggin15b46502008-10-15 22:04:32 -0700253int afs_write_end(struct file *file, struct address_space *mapping,
254 loff_t pos, unsigned len, unsigned copied,
255 struct page *page, void *fsdata)
David Howells31143d52007-05-09 02:33:46 -0700256{
Al Viro496ad9a2013-01-23 17:07:38 -0500257 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
David Howells215804a2017-11-02 15:27:52 +0000258 struct key *key = afs_file_key(file);
David Howells31143d52007-05-09 02:33:46 -0700259 loff_t i_size, maybe_i_size;
David Howellse8e581a2017-03-16 16:27:44 +0000260 int ret;
David Howells31143d52007-05-09 02:33:46 -0700261
Nick Piggin15b46502008-10-15 22:04:32 -0700262 _enter("{%x:%u},{%lx}",
263 vnode->fid.vid, vnode->fid.vnode, page->index);
David Howells31143d52007-05-09 02:33:46 -0700264
Nick Piggin15b46502008-10-15 22:04:32 -0700265 maybe_i_size = pos + copied;
David Howells31143d52007-05-09 02:33:46 -0700266
267 i_size = i_size_read(&vnode->vfs_inode);
268 if (maybe_i_size > i_size) {
269 spin_lock(&vnode->writeback_lock);
270 i_size = i_size_read(&vnode->vfs_inode);
271 if (maybe_i_size > i_size)
272 i_size_write(&vnode->vfs_inode, maybe_i_size);
273 spin_unlock(&vnode->writeback_lock);
274 }
275
David Howellse8e581a2017-03-16 16:27:44 +0000276 if (!PageUptodate(page)) {
277 if (copied < len) {
278 /* Try and load any missing data from the server. The
279 * unmarshalling routine will take care of clearing any
280 * bits that are beyond the EOF.
281 */
282 ret = afs_fill_page(vnode, key, pos + copied,
283 len - copied, page);
284 if (ret < 0)
285 return ret;
286 }
287 SetPageUptodate(page);
288 }
289
David Howells31143d52007-05-09 02:33:46 -0700290 set_page_dirty(page);
David Howells31143d52007-05-09 02:33:46 -0700291 if (PageDirty(page))
292 _debug("dirtied");
Nick Piggin15b46502008-10-15 22:04:32 -0700293 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300294 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700295
Nick Piggin15b46502008-10-15 22:04:32 -0700296 return copied;
David Howells31143d52007-05-09 02:33:46 -0700297}
298
299/*
300 * kill all the pages in the given range
301 */
302static void afs_kill_pages(struct afs_vnode *vnode, bool error,
303 pgoff_t first, pgoff_t last)
304{
305 struct pagevec pv;
306 unsigned count, loop;
307
308 _enter("{%x:%u},%lx-%lx",
309 vnode->fid.vid, vnode->fid.vnode, first, last);
310
311 pagevec_init(&pv, 0);
312
313 do {
314 _debug("kill %lx-%lx", first, last);
315
316 count = last - first + 1;
317 if (count > PAGEVEC_SIZE)
318 count = PAGEVEC_SIZE;
319 pv.nr = find_get_pages_contig(vnode->vfs_inode.i_mapping,
320 first, count, pv.pages);
321 ASSERTCMP(pv.nr, ==, count);
322
323 for (loop = 0; loop < count; loop++) {
David Howells7286a352017-03-16 16:27:48 +0000324 struct page *page = pv.pages[loop];
325 ClearPageUptodate(page);
David Howells31143d52007-05-09 02:33:46 -0700326 if (error)
David Howells7286a352017-03-16 16:27:48 +0000327 SetPageError(page);
328 if (PageWriteback(page))
329 end_page_writeback(page);
330 if (page->index >= first)
331 first = page->index + 1;
David Howells31143d52007-05-09 02:33:46 -0700332 }
333
334 __pagevec_release(&pv);
335 } while (first < last);
336
337 _leave("");
338}
339
340/*
David Howellsd2ddc772017-11-02 15:27:50 +0000341 * write to a file
342 */
343static int afs_store_data(struct afs_writeback *wb, pgoff_t first, pgoff_t last,
344 unsigned offset, unsigned to)
345{
346 struct afs_fs_cursor fc;
347 struct afs_vnode *vnode = wb->vnode;
348 int ret;
349
350 _enter("%s{%x:%u.%u},%x,%lx,%lx,%x,%x",
351 vnode->volume->name,
352 vnode->fid.vid,
353 vnode->fid.vnode,
354 vnode->fid.unique,
355 key_serial(wb->key),
356 first, last, offset, to);
357
358 ret = -ERESTARTSYS;
359 if (afs_begin_vnode_operation(&fc, vnode, wb->key)) {
360 while (afs_select_fileserver(&fc)) {
361 fc.cb_break = vnode->cb_break + vnode->cb_s_break;
362 afs_fs_store_data(&fc, wb, first, last, offset, to);
363 }
364
365 afs_check_for_remote_deletion(&fc, fc.vnode);
366 afs_vnode_commit_status(&fc, vnode, fc.cb_break);
367 ret = afs_end_vnode_operation(&fc);
368 }
369
370 _leave(" = %d", ret);
371 return ret;
372}
373
374/*
David Howells31143d52007-05-09 02:33:46 -0700375 * synchronously write back the locked page and any subsequent non-locked dirty
376 * pages also covered by the same writeback record
377 */
378static int afs_write_back_from_locked_page(struct afs_writeback *wb,
379 struct page *primary_page)
380{
381 struct page *pages[8], *page;
382 unsigned long count;
383 unsigned n, offset, to;
384 pgoff_t start, first, last;
385 int loop, ret;
386
387 _enter(",%lx", primary_page->index);
388
389 count = 1;
David Howells31143d52007-05-09 02:33:46 -0700390 if (test_set_page_writeback(primary_page))
391 BUG();
392
393 /* find all consecutive lockable dirty pages, stopping when we find a
394 * page that is not immediately lockable, is not dirty or is missing,
395 * or we reach the end of the range */
396 start = primary_page->index;
397 if (start >= wb->last)
398 goto no_more;
399 start++;
400 do {
401 _debug("more %lx [%lx]", start, count);
402 n = wb->last - start + 1;
403 if (n > ARRAY_SIZE(pages))
404 n = ARRAY_SIZE(pages);
405 n = find_get_pages_contig(wb->vnode->vfs_inode.i_mapping,
406 start, n, pages);
407 _debug("fgpc %u", n);
408 if (n == 0)
409 goto no_more;
410 if (pages[0]->index != start) {
David Howells9d577b62007-05-10 22:22:19 -0700411 do {
412 put_page(pages[--n]);
413 } while (n > 0);
David Howells31143d52007-05-09 02:33:46 -0700414 goto no_more;
415 }
416
417 for (loop = 0; loop < n; loop++) {
418 page = pages[loop];
419 if (page->index > wb->last)
420 break;
Nick Piggin529ae9a2008-08-02 12:01:03 +0200421 if (!trylock_page(page))
David Howells31143d52007-05-09 02:33:46 -0700422 break;
423 if (!PageDirty(page) ||
424 page_private(page) != (unsigned long) wb) {
425 unlock_page(page);
426 break;
427 }
428 if (!clear_page_dirty_for_io(page))
429 BUG();
430 if (test_set_page_writeback(page))
431 BUG();
432 unlock_page(page);
433 put_page(page);
434 }
435 count += loop;
436 if (loop < n) {
437 for (; loop < n; loop++)
438 put_page(pages[loop]);
439 goto no_more;
440 }
441
442 start += loop;
443 } while (start <= wb->last && count < 65536);
444
445no_more:
446 /* we now have a contiguous set of dirty pages, each with writeback set
447 * and the dirty mark cleared; the first page is locked and must remain
448 * so, all the rest are unlocked */
449 first = primary_page->index;
450 last = first + count - 1;
451
452 offset = (first == wb->first) ? wb->offset_first : 0;
453 to = (last == wb->last) ? wb->to_last : PAGE_SIZE;
454
455 _debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to);
456
David Howellsd2ddc772017-11-02 15:27:50 +0000457 ret = afs_store_data(wb, first, last, offset, to);
David Howells31143d52007-05-09 02:33:46 -0700458 if (ret < 0) {
459 switch (ret) {
460 case -EDQUOT:
461 case -ENOSPC:
Michal Hocko5114a972016-10-11 13:56:01 -0700462 mapping_set_error(wb->vnode->vfs_inode.i_mapping, -ENOSPC);
David Howells31143d52007-05-09 02:33:46 -0700463 break;
464 case -EROFS:
465 case -EIO:
466 case -EREMOTEIO:
467 case -EFBIG:
468 case -ENOENT:
469 case -ENOMEDIUM:
470 case -ENXIO:
471 afs_kill_pages(wb->vnode, true, first, last);
Michal Hocko5114a972016-10-11 13:56:01 -0700472 mapping_set_error(wb->vnode->vfs_inode.i_mapping, -EIO);
David Howells31143d52007-05-09 02:33:46 -0700473 break;
474 case -EACCES:
475 case -EPERM:
476 case -ENOKEY:
477 case -EKEYEXPIRED:
478 case -EKEYREJECTED:
479 case -EKEYREVOKED:
480 afs_kill_pages(wb->vnode, false, first, last);
481 break;
482 default:
483 break;
484 }
485 } else {
486 ret = count;
487 }
488
489 _leave(" = %d", ret);
490 return ret;
491}
492
493/*
494 * write a page back to the server
495 * - the caller locked the page for us
496 */
497int afs_writepage(struct page *page, struct writeback_control *wbc)
498{
David Howells31143d52007-05-09 02:33:46 -0700499 struct afs_writeback *wb;
500 int ret;
501
502 _enter("{%lx},", page->index);
503
David Howells31143d52007-05-09 02:33:46 -0700504 wb = (struct afs_writeback *) page_private(page);
505 ASSERT(wb != NULL);
506
507 ret = afs_write_back_from_locked_page(wb, page);
508 unlock_page(page);
509 if (ret < 0) {
510 _leave(" = %d", ret);
511 return 0;
512 }
513
514 wbc->nr_to_write -= ret;
David Howells31143d52007-05-09 02:33:46 -0700515
516 _leave(" = 0");
517 return 0;
518}
519
520/*
521 * write a region of pages back to the server
522 */
Adrian Bunkc1206a22007-10-16 23:26:41 -0700523static int afs_writepages_region(struct address_space *mapping,
524 struct writeback_control *wbc,
525 pgoff_t index, pgoff_t end, pgoff_t *_next)
David Howells31143d52007-05-09 02:33:46 -0700526{
David Howells31143d52007-05-09 02:33:46 -0700527 struct afs_writeback *wb;
528 struct page *page;
529 int ret, n;
530
531 _enter(",,%lx,%lx,", index, end);
532
533 do {
534 n = find_get_pages_tag(mapping, &index, PAGECACHE_TAG_DIRTY,
535 1, &page);
536 if (!n)
537 break;
538
539 _debug("wback %lx", page->index);
540
541 if (page->index > end) {
542 *_next = index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300543 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700544 _leave(" = 0 [%lx]", *_next);
545 return 0;
546 }
547
548 /* at this point we hold neither mapping->tree_lock nor lock on
549 * the page itself: the page may be truncated or invalidated
550 * (changing page->mapping to NULL), or even swizzled back from
551 * swapper_space to tmpfs file mapping
552 */
553 lock_page(page);
554
David Howellsc5051c72017-03-16 16:27:49 +0000555 if (page->mapping != mapping || !PageDirty(page)) {
David Howells31143d52007-05-09 02:33:46 -0700556 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300557 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700558 continue;
559 }
560
David Howellsc5051c72017-03-16 16:27:49 +0000561 if (PageWriteback(page)) {
David Howells31143d52007-05-09 02:33:46 -0700562 unlock_page(page);
David Howellsc5051c72017-03-16 16:27:49 +0000563 if (wbc->sync_mode != WB_SYNC_NONE)
564 wait_on_page_writeback(page);
David Howells29c8bbb2017-03-16 16:27:43 +0000565 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700566 continue;
567 }
568
569 wb = (struct afs_writeback *) page_private(page);
570 ASSERT(wb != NULL);
571
572 spin_lock(&wb->vnode->writeback_lock);
573 wb->state = AFS_WBACK_WRITING;
574 spin_unlock(&wb->vnode->writeback_lock);
575
David Howells65a15102017-03-16 16:27:49 +0000576 if (!clear_page_dirty_for_io(page))
577 BUG();
David Howells31143d52007-05-09 02:33:46 -0700578 ret = afs_write_back_from_locked_page(wb, page);
579 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300580 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700581 if (ret < 0) {
582 _leave(" = %d", ret);
583 return ret;
584 }
585
586 wbc->nr_to_write -= ret;
587
David Howells31143d52007-05-09 02:33:46 -0700588 cond_resched();
589 } while (index < end && wbc->nr_to_write > 0);
590
591 *_next = index;
592 _leave(" = 0 [%lx]", *_next);
593 return 0;
594}
595
596/*
597 * write some of the pending data back to the server
598 */
599int afs_writepages(struct address_space *mapping,
600 struct writeback_control *wbc)
601{
David Howells31143d52007-05-09 02:33:46 -0700602 pgoff_t start, end, next;
603 int ret;
604
605 _enter("");
606
David Howells31143d52007-05-09 02:33:46 -0700607 if (wbc->range_cyclic) {
608 start = mapping->writeback_index;
609 end = -1;
610 ret = afs_writepages_region(mapping, wbc, start, end, &next);
Wu Fengguang1b430be2010-10-26 14:21:26 -0700611 if (start > 0 && wbc->nr_to_write > 0 && ret == 0)
David Howells31143d52007-05-09 02:33:46 -0700612 ret = afs_writepages_region(mapping, wbc, 0, start,
613 &next);
614 mapping->writeback_index = next;
615 } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300616 end = (pgoff_t)(LLONG_MAX >> PAGE_SHIFT);
David Howells31143d52007-05-09 02:33:46 -0700617 ret = afs_writepages_region(mapping, wbc, 0, end, &next);
618 if (wbc->nr_to_write > 0)
619 mapping->writeback_index = next;
620 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300621 start = wbc->range_start >> PAGE_SHIFT;
622 end = wbc->range_end >> PAGE_SHIFT;
David Howells31143d52007-05-09 02:33:46 -0700623 ret = afs_writepages_region(mapping, wbc, start, end, &next);
624 }
625
626 _leave(" = %d", ret);
627 return ret;
628}
629
630/*
David Howells31143d52007-05-09 02:33:46 -0700631 * completion of write to server
632 */
633void afs_pages_written_back(struct afs_vnode *vnode, struct afs_call *call)
634{
635 struct afs_writeback *wb = call->wb;
636 struct pagevec pv;
637 unsigned count, loop;
638 pgoff_t first = call->first, last = call->last;
639 bool free_wb;
640
641 _enter("{%x:%u},{%lx-%lx}",
642 vnode->fid.vid, vnode->fid.vnode, first, last);
643
644 ASSERT(wb != NULL);
645
646 pagevec_init(&pv, 0);
647
648 do {
David Howells5bbf5d32007-05-10 03:15:23 -0700649 _debug("done %lx-%lx", first, last);
David Howells31143d52007-05-09 02:33:46 -0700650
651 count = last - first + 1;
652 if (count > PAGEVEC_SIZE)
653 count = PAGEVEC_SIZE;
654 pv.nr = find_get_pages_contig(call->mapping, first, count,
655 pv.pages);
656 ASSERTCMP(pv.nr, ==, count);
657
658 spin_lock(&vnode->writeback_lock);
659 for (loop = 0; loop < count; loop++) {
660 struct page *page = pv.pages[loop];
661 end_page_writeback(page);
662 if (page_private(page) == (unsigned long) wb) {
663 set_page_private(page, 0);
664 ClearPagePrivate(page);
665 wb->usage--;
666 }
667 }
668 free_wb = false;
669 if (wb->usage == 0) {
670 afs_unlink_writeback(wb);
671 free_wb = true;
672 }
673 spin_unlock(&vnode->writeback_lock);
674 first += count;
675 if (free_wb) {
676 afs_free_writeback(wb);
677 wb = NULL;
678 }
679
680 __pagevec_release(&pv);
David Howells5bbf5d32007-05-10 03:15:23 -0700681 } while (first <= last);
David Howells31143d52007-05-09 02:33:46 -0700682
683 _leave("");
684}
685
686/*
687 * write to an AFS file
688 */
Al Viro50b55512014-04-03 14:13:46 -0400689ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from)
David Howells31143d52007-05-09 02:33:46 -0700690{
Al Viro496ad9a2013-01-23 17:07:38 -0500691 struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
David Howells31143d52007-05-09 02:33:46 -0700692 ssize_t result;
Al Viro50b55512014-04-03 14:13:46 -0400693 size_t count = iov_iter_count(from);
David Howells31143d52007-05-09 02:33:46 -0700694
Al Viro50b55512014-04-03 14:13:46 -0400695 _enter("{%x.%u},{%zu},",
696 vnode->fid.vid, vnode->fid.vnode, count);
David Howells31143d52007-05-09 02:33:46 -0700697
698 if (IS_SWAPFILE(&vnode->vfs_inode)) {
699 printk(KERN_INFO
700 "AFS: Attempt to write to active swap file!\n");
701 return -EBUSY;
702 }
703
704 if (!count)
705 return 0;
706
Al Viro50b55512014-04-03 14:13:46 -0400707 result = generic_file_write_iter(iocb, from);
David Howells31143d52007-05-09 02:33:46 -0700708
David Howells31143d52007-05-09 02:33:46 -0700709 _leave(" = %zd", result);
710 return result;
711}
712
713/*
714 * flush the vnode to the fileserver
715 */
716int afs_writeback_all(struct afs_vnode *vnode)
717{
718 struct address_space *mapping = vnode->vfs_inode.i_mapping;
719 struct writeback_control wbc = {
David Howells31143d52007-05-09 02:33:46 -0700720 .sync_mode = WB_SYNC_ALL,
721 .nr_to_write = LONG_MAX,
David Howells31143d52007-05-09 02:33:46 -0700722 .range_cyclic = 1,
723 };
724 int ret;
725
726 _enter("");
727
728 ret = mapping->a_ops->writepages(mapping, &wbc);
729 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
730
731 _leave(" = %d", ret);
732 return ret;
733}
734
735/*
736 * flush any dirty pages for this process, and check for write errors.
737 * - the return status from this call provides a reliable indication of
738 * whether any write errors occurred for this process.
739 */
Josef Bacik02c24a82011-07-16 20:44:56 -0400740int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
David Howells31143d52007-05-09 02:33:46 -0700741{
Al Viro3c981bf2013-09-03 13:37:45 -0400742 struct inode *inode = file_inode(file);
David Howells31143d52007-05-09 02:33:46 -0700743 struct afs_writeback *wb, *xwb;
Al Viro3c981bf2013-09-03 13:37:45 -0400744 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells31143d52007-05-09 02:33:46 -0700745 int ret;
746
Al Viro3c981bf2013-09-03 13:37:45 -0400747 _enter("{%x:%u},{n=%pD},%d",
748 vnode->fid.vid, vnode->fid.vnode, file,
David Howells31143d52007-05-09 02:33:46 -0700749 datasync);
750
Jeff Layton3b49c9a2017-07-07 15:20:52 -0400751 ret = file_write_and_wait_range(file, start, end);
Josef Bacik02c24a82011-07-16 20:44:56 -0400752 if (ret)
753 return ret;
Al Viro59551022016-01-22 15:40:57 -0500754 inode_lock(inode);
Josef Bacik02c24a82011-07-16 20:44:56 -0400755
David Howells31143d52007-05-09 02:33:46 -0700756 /* use a writeback record as a marker in the queue - when this reaches
757 * the front of the queue, all the outstanding writes are either
758 * completed or rejected */
759 wb = kzalloc(sizeof(*wb), GFP_KERNEL);
Josef Bacik02c24a82011-07-16 20:44:56 -0400760 if (!wb) {
761 ret = -ENOMEM;
762 goto out;
763 }
David Howells31143d52007-05-09 02:33:46 -0700764 wb->vnode = vnode;
765 wb->first = 0;
766 wb->last = -1;
767 wb->offset_first = 0;
768 wb->to_last = PAGE_SIZE;
769 wb->usage = 1;
770 wb->state = AFS_WBACK_SYNCING;
771 init_waitqueue_head(&wb->waitq);
772
773 spin_lock(&vnode->writeback_lock);
774 list_for_each_entry(xwb, &vnode->writebacks, link) {
775 if (xwb->state == AFS_WBACK_PENDING)
776 xwb->state = AFS_WBACK_CONFLICTING;
777 }
778 list_add_tail(&wb->link, &vnode->writebacks);
779 spin_unlock(&vnode->writeback_lock);
780
781 /* push all the outstanding writebacks to the server */
782 ret = afs_writeback_all(vnode);
783 if (ret < 0) {
784 afs_put_writeback(wb);
785 _leave(" = %d [wb]", ret);
Josef Bacik02c24a82011-07-16 20:44:56 -0400786 goto out;
David Howells31143d52007-05-09 02:33:46 -0700787 }
788
789 /* wait for the preceding writes to actually complete */
790 ret = wait_event_interruptible(wb->waitq,
791 wb->state == AFS_WBACK_COMPLETE ||
792 vnode->writebacks.next == &wb->link);
793 afs_put_writeback(wb);
794 _leave(" = %d", ret);
Josef Bacik02c24a82011-07-16 20:44:56 -0400795out:
Al Viro59551022016-01-22 15:40:57 -0500796 inode_unlock(inode);
David Howells31143d52007-05-09 02:33:46 -0700797 return ret;
798}
David Howells9b3f26c2009-04-03 16:42:41 +0100799
800/*
David Howells58fed942017-03-16 16:27:45 +0000801 * Flush out all outstanding writes on a file opened for writing when it is
802 * closed.
803 */
804int afs_flush(struct file *file, fl_owner_t id)
805{
806 _enter("");
807
808 if ((file->f_mode & FMODE_WRITE) == 0)
809 return 0;
810
811 return vfs_fsync(file, 0);
812}
813
814/*
David Howells9b3f26c2009-04-03 16:42:41 +0100815 * notification that a previously read-only page is about to become writable
816 * - if it returns an error, the caller will deliver a bus error signal
817 */
818int afs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
819{
820 struct afs_vnode *vnode = AFS_FS_I(vma->vm_file->f_mapping->host);
821
822 _enter("{{%x:%u}},{%lx}",
823 vnode->fid.vid, vnode->fid.vnode, page->index);
824
825 /* wait for the page to be written to the cache before we allow it to
826 * be modified */
827#ifdef CONFIG_AFS_FSCACHE
828 fscache_wait_on_page_write(vnode->cache, page);
829#endif
830
831 _leave(" = 0");
832 return 0;
833}