blob: 3fba2b573c86a72e66df4b4878015fc53000a238 [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,
Anton Blanchard5e7f2332011-06-13 22:31:12 +010087 loff_t pos, struct page *page)
David Howells31143d52007-05-09 02:33:46 -070088{
Nick Piggin15b46502008-10-15 22:04:32 -070089 loff_t i_size;
David Howells31143d52007-05-09 02:33:46 -070090 int ret;
Anton Blanchard5e7f2332011-06-13 22:31:12 +010091 int len;
David Howells31143d52007-05-09 02:33:46 -070092
Anton Blanchard5e7f2332011-06-13 22:31:12 +010093 _enter(",,%llu", (unsigned long long)pos);
David Howells31143d52007-05-09 02:33:46 -070094
Nick Piggin15b46502008-10-15 22:04:32 -070095 i_size = i_size_read(&vnode->vfs_inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030096 if (pos + PAGE_SIZE > i_size)
Anton Blanchard5e7f2332011-06-13 22:31:12 +010097 len = i_size - pos;
Nick Piggin15b46502008-10-15 22:04:32 -070098 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030099 len = PAGE_SIZE;
Nick Piggin15b46502008-10-15 22:04:32 -0700100
Anton Blanchard5e7f2332011-06-13 22:31:12 +0100101 ret = afs_vnode_fetch_data(vnode, key, pos, len, page);
David Howells31143d52007-05-09 02:33:46 -0700102 if (ret < 0) {
103 if (ret == -ENOENT) {
104 _debug("got NOENT from server"
105 " - marking file deleted and stale");
106 set_bit(AFS_VNODE_DELETED, &vnode->flags);
107 ret = -ESTALE;
108 }
109 }
110
111 _leave(" = %d", ret);
112 return ret;
113}
114
115/*
David Howells31143d52007-05-09 02:33:46 -0700116 * prepare to perform part of a write to a page
David Howells31143d52007-05-09 02:33:46 -0700117 */
Nick Piggin15b46502008-10-15 22:04:32 -0700118int afs_write_begin(struct file *file, struct address_space *mapping,
119 loff_t pos, unsigned len, unsigned flags,
120 struct page **pagep, void **fsdata)
David Howells31143d52007-05-09 02:33:46 -0700121{
122 struct afs_writeback *candidate, *wb;
Al Viro496ad9a2013-01-23 17:07:38 -0500123 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
Nick Piggin15b46502008-10-15 22:04:32 -0700124 struct page *page;
David Howells31143d52007-05-09 02:33:46 -0700125 struct key *key = file->private_data;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300126 unsigned from = pos & (PAGE_SIZE - 1);
Nick Piggin15b46502008-10-15 22:04:32 -0700127 unsigned to = from + len;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300128 pgoff_t index = pos >> PAGE_SHIFT;
David Howells31143d52007-05-09 02:33:46 -0700129 int ret;
130
131 _enter("{%x:%u},{%lx},%u,%u",
Nick Piggin15b46502008-10-15 22:04:32 -0700132 vnode->fid.vid, vnode->fid.vnode, index, from, to);
David Howells31143d52007-05-09 02:33:46 -0700133
134 candidate = kzalloc(sizeof(*candidate), GFP_KERNEL);
135 if (!candidate)
136 return -ENOMEM;
137 candidate->vnode = vnode;
Nick Piggin15b46502008-10-15 22:04:32 -0700138 candidate->first = candidate->last = index;
139 candidate->offset_first = from;
David Howells31143d52007-05-09 02:33:46 -0700140 candidate->to_last = to;
Anton Blanchardf129ccc2011-02-25 15:33:02 +0000141 INIT_LIST_HEAD(&candidate->link);
David Howells31143d52007-05-09 02:33:46 -0700142 candidate->usage = 1;
143 candidate->state = AFS_WBACK_PENDING;
144 init_waitqueue_head(&candidate->waitq);
145
Nick Piggin54566b22009-01-04 12:00:53 -0800146 page = grab_cache_page_write_begin(mapping, index, flags);
Nick Piggin15b46502008-10-15 22:04:32 -0700147 if (!page) {
148 kfree(candidate);
149 return -ENOMEM;
150 }
Nick Piggin15b46502008-10-15 22:04:32 -0700151
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300152 if (!PageUptodate(page) && len != PAGE_SIZE) {
153 ret = afs_fill_page(vnode, key, index << PAGE_SHIFT, page);
David Howells31143d52007-05-09 02:33:46 -0700154 if (ret < 0) {
David Howells856bb4b2017-03-16 16:27:48 +0000155 unlock_page(page);
156 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700157 kfree(candidate);
158 _leave(" = %d [prep]", ret);
159 return ret;
160 }
Nick Piggin15b46502008-10-15 22:04:32 -0700161 SetPageUptodate(page);
David Howells31143d52007-05-09 02:33:46 -0700162 }
163
David Howells856bb4b2017-03-16 16:27:48 +0000164 /* page won't leak in error case: it eventually gets cleaned off LRU */
165 *pagep = page;
166
David Howells31143d52007-05-09 02:33:46 -0700167try_again:
David Howells31143d52007-05-09 02:33:46 -0700168 spin_lock(&vnode->writeback_lock);
169
170 /* see if this page is already pending a writeback under a suitable key
171 * - if so we can just join onto that one */
172 wb = (struct afs_writeback *) page_private(page);
173 if (wb) {
174 if (wb->key == key && wb->state == AFS_WBACK_PENDING)
175 goto subsume_in_current_wb;
176 goto flush_conflicting_wb;
177 }
178
179 if (index > 0) {
180 /* see if we can find an already pending writeback that we can
181 * append this page to */
182 list_for_each_entry(wb, &vnode->writebacks, link) {
183 if (wb->last == index - 1 && wb->key == key &&
184 wb->state == AFS_WBACK_PENDING)
185 goto append_to_previous_wb;
186 }
187 }
188
189 list_add_tail(&candidate->link, &vnode->writebacks);
190 candidate->key = key_get(key);
191 spin_unlock(&vnode->writeback_lock);
192 SetPagePrivate(page);
193 set_page_private(page, (unsigned long) candidate);
194 _leave(" = 0 [new]");
195 return 0;
196
197subsume_in_current_wb:
198 _debug("subsume");
199 ASSERTRANGE(wb->first, <=, index, <=, wb->last);
Nick Piggin15b46502008-10-15 22:04:32 -0700200 if (index == wb->first && from < wb->offset_first)
201 wb->offset_first = from;
David Howells31143d52007-05-09 02:33:46 -0700202 if (index == wb->last && to > wb->to_last)
203 wb->to_last = to;
204 spin_unlock(&vnode->writeback_lock);
205 kfree(candidate);
206 _leave(" = 0 [sub]");
207 return 0;
208
209append_to_previous_wb:
210 _debug("append into %lx-%lx", wb->first, wb->last);
211 wb->usage++;
212 wb->last++;
213 wb->to_last = to;
214 spin_unlock(&vnode->writeback_lock);
215 SetPagePrivate(page);
216 set_page_private(page, (unsigned long) wb);
217 kfree(candidate);
218 _leave(" = 0 [app]");
219 return 0;
220
221 /* the page is currently bound to another context, so if it's dirty we
222 * need to flush it before we can use the new context */
223flush_conflicting_wb:
224 _debug("flush conflict");
225 if (wb->state == AFS_WBACK_PENDING)
226 wb->state = AFS_WBACK_CONFLICTING;
227 spin_unlock(&vnode->writeback_lock);
228 if (PageDirty(page)) {
229 ret = afs_write_back_from_locked_page(wb, page);
230 if (ret < 0) {
231 afs_put_writeback(candidate);
232 _leave(" = %d", ret);
233 return ret;
234 }
235 }
236
237 /* the page holds a ref on the writeback record */
238 afs_put_writeback(wb);
239 set_page_private(page, 0);
240 ClearPagePrivate(page);
241 goto try_again;
242}
243
244/*
245 * finalise part of a write to a page
246 */
Nick Piggin15b46502008-10-15 22:04:32 -0700247int afs_write_end(struct file *file, struct address_space *mapping,
248 loff_t pos, unsigned len, unsigned copied,
249 struct page *page, void *fsdata)
David Howells31143d52007-05-09 02:33:46 -0700250{
Al Viro496ad9a2013-01-23 17:07:38 -0500251 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
David Howells31143d52007-05-09 02:33:46 -0700252 loff_t i_size, maybe_i_size;
253
Nick Piggin15b46502008-10-15 22:04:32 -0700254 _enter("{%x:%u},{%lx}",
255 vnode->fid.vid, vnode->fid.vnode, page->index);
David Howells31143d52007-05-09 02:33:46 -0700256
Nick Piggin15b46502008-10-15 22:04:32 -0700257 maybe_i_size = pos + copied;
David Howells31143d52007-05-09 02:33:46 -0700258
259 i_size = i_size_read(&vnode->vfs_inode);
260 if (maybe_i_size > i_size) {
261 spin_lock(&vnode->writeback_lock);
262 i_size = i_size_read(&vnode->vfs_inode);
263 if (maybe_i_size > i_size)
264 i_size_write(&vnode->vfs_inode, maybe_i_size);
265 spin_unlock(&vnode->writeback_lock);
266 }
267
268 set_page_dirty(page);
David Howells31143d52007-05-09 02:33:46 -0700269 if (PageDirty(page))
270 _debug("dirtied");
Nick Piggin15b46502008-10-15 22:04:32 -0700271 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300272 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700273
Nick Piggin15b46502008-10-15 22:04:32 -0700274 return copied;
David Howells31143d52007-05-09 02:33:46 -0700275}
276
277/*
278 * kill all the pages in the given range
279 */
280static void afs_kill_pages(struct afs_vnode *vnode, bool error,
281 pgoff_t first, pgoff_t last)
282{
283 struct pagevec pv;
284 unsigned count, loop;
285
286 _enter("{%x:%u},%lx-%lx",
287 vnode->fid.vid, vnode->fid.vnode, first, last);
288
289 pagevec_init(&pv, 0);
290
291 do {
292 _debug("kill %lx-%lx", first, last);
293
294 count = last - first + 1;
295 if (count > PAGEVEC_SIZE)
296 count = PAGEVEC_SIZE;
297 pv.nr = find_get_pages_contig(vnode->vfs_inode.i_mapping,
298 first, count, pv.pages);
299 ASSERTCMP(pv.nr, ==, count);
300
301 for (loop = 0; loop < count; loop++) {
David Howellsd43dda02017-03-16 16:27:48 +0000302 struct page *page = pv.pages[loop];
303 ClearPageUptodate(page);
David Howells31143d52007-05-09 02:33:46 -0700304 if (error)
David Howellsd43dda02017-03-16 16:27:48 +0000305 SetPageError(page);
306 if (PageWriteback(page))
307 end_page_writeback(page);
308 if (page->index >= first)
309 first = page->index + 1;
David Howells31143d52007-05-09 02:33:46 -0700310 }
311
312 __pagevec_release(&pv);
313 } while (first < last);
314
315 _leave("");
316}
317
318/*
319 * synchronously write back the locked page and any subsequent non-locked dirty
320 * pages also covered by the same writeback record
321 */
322static int afs_write_back_from_locked_page(struct afs_writeback *wb,
323 struct page *primary_page)
324{
325 struct page *pages[8], *page;
326 unsigned long count;
327 unsigned n, offset, to;
328 pgoff_t start, first, last;
329 int loop, ret;
330
331 _enter(",%lx", primary_page->index);
332
333 count = 1;
334 if (!clear_page_dirty_for_io(primary_page))
335 BUG();
336 if (test_set_page_writeback(primary_page))
337 BUG();
338
339 /* find all consecutive lockable dirty pages, stopping when we find a
340 * page that is not immediately lockable, is not dirty or is missing,
341 * or we reach the end of the range */
342 start = primary_page->index;
343 if (start >= wb->last)
344 goto no_more;
345 start++;
346 do {
347 _debug("more %lx [%lx]", start, count);
348 n = wb->last - start + 1;
349 if (n > ARRAY_SIZE(pages))
350 n = ARRAY_SIZE(pages);
351 n = find_get_pages_contig(wb->vnode->vfs_inode.i_mapping,
352 start, n, pages);
353 _debug("fgpc %u", n);
354 if (n == 0)
355 goto no_more;
356 if (pages[0]->index != start) {
David Howells9d577b62007-05-10 22:22:19 -0700357 do {
358 put_page(pages[--n]);
359 } while (n > 0);
David Howells31143d52007-05-09 02:33:46 -0700360 goto no_more;
361 }
362
363 for (loop = 0; loop < n; loop++) {
364 page = pages[loop];
365 if (page->index > wb->last)
366 break;
Nick Piggin529ae9a2008-08-02 12:01:03 +0200367 if (!trylock_page(page))
David Howells31143d52007-05-09 02:33:46 -0700368 break;
369 if (!PageDirty(page) ||
370 page_private(page) != (unsigned long) wb) {
371 unlock_page(page);
372 break;
373 }
374 if (!clear_page_dirty_for_io(page))
375 BUG();
376 if (test_set_page_writeback(page))
377 BUG();
378 unlock_page(page);
379 put_page(page);
380 }
381 count += loop;
382 if (loop < n) {
383 for (; loop < n; loop++)
384 put_page(pages[loop]);
385 goto no_more;
386 }
387
388 start += loop;
389 } while (start <= wb->last && count < 65536);
390
391no_more:
392 /* we now have a contiguous set of dirty pages, each with writeback set
393 * and the dirty mark cleared; the first page is locked and must remain
394 * so, all the rest are unlocked */
395 first = primary_page->index;
396 last = first + count - 1;
397
398 offset = (first == wb->first) ? wb->offset_first : 0;
399 to = (last == wb->last) ? wb->to_last : PAGE_SIZE;
400
401 _debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to);
402
403 ret = afs_vnode_store_data(wb, first, last, offset, to);
404 if (ret < 0) {
405 switch (ret) {
406 case -EDQUOT:
407 case -ENOSPC:
Michal Hocko5114a972016-10-11 13:56:01 -0700408 mapping_set_error(wb->vnode->vfs_inode.i_mapping, -ENOSPC);
David Howells31143d52007-05-09 02:33:46 -0700409 break;
410 case -EROFS:
411 case -EIO:
412 case -EREMOTEIO:
413 case -EFBIG:
414 case -ENOENT:
415 case -ENOMEDIUM:
416 case -ENXIO:
417 afs_kill_pages(wb->vnode, true, first, last);
Michal Hocko5114a972016-10-11 13:56:01 -0700418 mapping_set_error(wb->vnode->vfs_inode.i_mapping, -EIO);
David Howells31143d52007-05-09 02:33:46 -0700419 break;
420 case -EACCES:
421 case -EPERM:
422 case -ENOKEY:
423 case -EKEYEXPIRED:
424 case -EKEYREJECTED:
425 case -EKEYREVOKED:
426 afs_kill_pages(wb->vnode, false, first, last);
427 break;
428 default:
429 break;
430 }
431 } else {
432 ret = count;
433 }
434
435 _leave(" = %d", ret);
436 return ret;
437}
438
439/*
440 * write a page back to the server
441 * - the caller locked the page for us
442 */
443int afs_writepage(struct page *page, struct writeback_control *wbc)
444{
David Howells31143d52007-05-09 02:33:46 -0700445 struct afs_writeback *wb;
446 int ret;
447
448 _enter("{%lx},", page->index);
449
David Howells31143d52007-05-09 02:33:46 -0700450 wb = (struct afs_writeback *) page_private(page);
451 ASSERT(wb != NULL);
452
453 ret = afs_write_back_from_locked_page(wb, page);
454 unlock_page(page);
455 if (ret < 0) {
456 _leave(" = %d", ret);
457 return 0;
458 }
459
460 wbc->nr_to_write -= ret;
David Howells31143d52007-05-09 02:33:46 -0700461
462 _leave(" = 0");
463 return 0;
464}
465
466/*
467 * write a region of pages back to the server
468 */
Adrian Bunkc1206a22007-10-16 23:26:41 -0700469static int afs_writepages_region(struct address_space *mapping,
470 struct writeback_control *wbc,
471 pgoff_t index, pgoff_t end, pgoff_t *_next)
David Howells31143d52007-05-09 02:33:46 -0700472{
David Howells31143d52007-05-09 02:33:46 -0700473 struct afs_writeback *wb;
474 struct page *page;
475 int ret, n;
476
477 _enter(",,%lx,%lx,", index, end);
478
479 do {
480 n = find_get_pages_tag(mapping, &index, PAGECACHE_TAG_DIRTY,
481 1, &page);
482 if (!n)
483 break;
484
485 _debug("wback %lx", page->index);
486
487 if (page->index > end) {
488 *_next = index;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300489 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700490 _leave(" = 0 [%lx]", *_next);
491 return 0;
492 }
493
494 /* at this point we hold neither mapping->tree_lock nor lock on
495 * the page itself: the page may be truncated or invalidated
496 * (changing page->mapping to NULL), or even swizzled back from
497 * swapper_space to tmpfs file mapping
498 */
499 lock_page(page);
500
501 if (page->mapping != mapping) {
502 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300503 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700504 continue;
505 }
506
507 if (wbc->sync_mode != WB_SYNC_NONE)
508 wait_on_page_writeback(page);
509
510 if (PageWriteback(page) || !PageDirty(page)) {
511 unlock_page(page);
David Howellsc250fae2017-03-16 16:27:43 +0000512 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700513 continue;
514 }
515
516 wb = (struct afs_writeback *) page_private(page);
517 ASSERT(wb != NULL);
518
519 spin_lock(&wb->vnode->writeback_lock);
520 wb->state = AFS_WBACK_WRITING;
521 spin_unlock(&wb->vnode->writeback_lock);
522
523 ret = afs_write_back_from_locked_page(wb, page);
524 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300525 put_page(page);
David Howells31143d52007-05-09 02:33:46 -0700526 if (ret < 0) {
527 _leave(" = %d", ret);
528 return ret;
529 }
530
531 wbc->nr_to_write -= ret;
532
David Howells31143d52007-05-09 02:33:46 -0700533 cond_resched();
534 } while (index < end && wbc->nr_to_write > 0);
535
536 *_next = index;
537 _leave(" = 0 [%lx]", *_next);
538 return 0;
539}
540
541/*
542 * write some of the pending data back to the server
543 */
544int afs_writepages(struct address_space *mapping,
545 struct writeback_control *wbc)
546{
David Howells31143d52007-05-09 02:33:46 -0700547 pgoff_t start, end, next;
548 int ret;
549
550 _enter("");
551
David Howells31143d52007-05-09 02:33:46 -0700552 if (wbc->range_cyclic) {
553 start = mapping->writeback_index;
554 end = -1;
555 ret = afs_writepages_region(mapping, wbc, start, end, &next);
Wu Fengguang1b430be2010-10-26 14:21:26 -0700556 if (start > 0 && wbc->nr_to_write > 0 && ret == 0)
David Howells31143d52007-05-09 02:33:46 -0700557 ret = afs_writepages_region(mapping, wbc, 0, start,
558 &next);
559 mapping->writeback_index = next;
560 } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300561 end = (pgoff_t)(LLONG_MAX >> PAGE_SHIFT);
David Howells31143d52007-05-09 02:33:46 -0700562 ret = afs_writepages_region(mapping, wbc, 0, end, &next);
563 if (wbc->nr_to_write > 0)
564 mapping->writeback_index = next;
565 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300566 start = wbc->range_start >> PAGE_SHIFT;
567 end = wbc->range_end >> PAGE_SHIFT;
David Howells31143d52007-05-09 02:33:46 -0700568 ret = afs_writepages_region(mapping, wbc, start, end, &next);
569 }
570
571 _leave(" = %d", ret);
572 return ret;
573}
574
575/*
David Howells31143d52007-05-09 02:33:46 -0700576 * completion of write to server
577 */
578void afs_pages_written_back(struct afs_vnode *vnode, struct afs_call *call)
579{
580 struct afs_writeback *wb = call->wb;
581 struct pagevec pv;
582 unsigned count, loop;
583 pgoff_t first = call->first, last = call->last;
584 bool free_wb;
585
586 _enter("{%x:%u},{%lx-%lx}",
587 vnode->fid.vid, vnode->fid.vnode, first, last);
588
589 ASSERT(wb != NULL);
590
591 pagevec_init(&pv, 0);
592
593 do {
David Howells5bbf5d32007-05-10 03:15:23 -0700594 _debug("done %lx-%lx", first, last);
David Howells31143d52007-05-09 02:33:46 -0700595
596 count = last - first + 1;
597 if (count > PAGEVEC_SIZE)
598 count = PAGEVEC_SIZE;
599 pv.nr = find_get_pages_contig(call->mapping, first, count,
600 pv.pages);
601 ASSERTCMP(pv.nr, ==, count);
602
603 spin_lock(&vnode->writeback_lock);
604 for (loop = 0; loop < count; loop++) {
605 struct page *page = pv.pages[loop];
606 end_page_writeback(page);
607 if (page_private(page) == (unsigned long) wb) {
608 set_page_private(page, 0);
609 ClearPagePrivate(page);
610 wb->usage--;
611 }
612 }
613 free_wb = false;
614 if (wb->usage == 0) {
615 afs_unlink_writeback(wb);
616 free_wb = true;
617 }
618 spin_unlock(&vnode->writeback_lock);
619 first += count;
620 if (free_wb) {
621 afs_free_writeback(wb);
622 wb = NULL;
623 }
624
625 __pagevec_release(&pv);
David Howells5bbf5d32007-05-10 03:15:23 -0700626 } while (first <= last);
David Howells31143d52007-05-09 02:33:46 -0700627
628 _leave("");
629}
630
631/*
632 * write to an AFS file
633 */
Al Viro50b55512014-04-03 14:13:46 -0400634ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from)
David Howells31143d52007-05-09 02:33:46 -0700635{
Al Viro496ad9a2013-01-23 17:07:38 -0500636 struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
David Howells31143d52007-05-09 02:33:46 -0700637 ssize_t result;
Al Viro50b55512014-04-03 14:13:46 -0400638 size_t count = iov_iter_count(from);
David Howells31143d52007-05-09 02:33:46 -0700639
Al Viro50b55512014-04-03 14:13:46 -0400640 _enter("{%x.%u},{%zu},",
641 vnode->fid.vid, vnode->fid.vnode, count);
David Howells31143d52007-05-09 02:33:46 -0700642
643 if (IS_SWAPFILE(&vnode->vfs_inode)) {
644 printk(KERN_INFO
645 "AFS: Attempt to write to active swap file!\n");
646 return -EBUSY;
647 }
648
649 if (!count)
650 return 0;
651
Al Viro50b55512014-04-03 14:13:46 -0400652 result = generic_file_write_iter(iocb, from);
David Howells31143d52007-05-09 02:33:46 -0700653
David Howells31143d52007-05-09 02:33:46 -0700654 _leave(" = %zd", result);
655 return result;
656}
657
658/*
659 * flush the vnode to the fileserver
660 */
661int afs_writeback_all(struct afs_vnode *vnode)
662{
663 struct address_space *mapping = vnode->vfs_inode.i_mapping;
664 struct writeback_control wbc = {
David Howells31143d52007-05-09 02:33:46 -0700665 .sync_mode = WB_SYNC_ALL,
666 .nr_to_write = LONG_MAX,
David Howells31143d52007-05-09 02:33:46 -0700667 .range_cyclic = 1,
668 };
669 int ret;
670
671 _enter("");
672
673 ret = mapping->a_ops->writepages(mapping, &wbc);
674 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
675
676 _leave(" = %d", ret);
677 return ret;
678}
679
680/*
681 * flush any dirty pages for this process, and check for write errors.
682 * - the return status from this call provides a reliable indication of
683 * whether any write errors occurred for this process.
684 */
Josef Bacik02c24a82011-07-16 20:44:56 -0400685int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
David Howells31143d52007-05-09 02:33:46 -0700686{
Al Viro3c981bf2013-09-03 13:37:45 -0400687 struct inode *inode = file_inode(file);
David Howells31143d52007-05-09 02:33:46 -0700688 struct afs_writeback *wb, *xwb;
Al Viro3c981bf2013-09-03 13:37:45 -0400689 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells31143d52007-05-09 02:33:46 -0700690 int ret;
691
Al Viro3c981bf2013-09-03 13:37:45 -0400692 _enter("{%x:%u},{n=%pD},%d",
693 vnode->fid.vid, vnode->fid.vnode, file,
David Howells31143d52007-05-09 02:33:46 -0700694 datasync);
695
Josef Bacik02c24a82011-07-16 20:44:56 -0400696 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
697 if (ret)
698 return ret;
Al Viro59551022016-01-22 15:40:57 -0500699 inode_lock(inode);
Josef Bacik02c24a82011-07-16 20:44:56 -0400700
David Howells31143d52007-05-09 02:33:46 -0700701 /* use a writeback record as a marker in the queue - when this reaches
702 * the front of the queue, all the outstanding writes are either
703 * completed or rejected */
704 wb = kzalloc(sizeof(*wb), GFP_KERNEL);
Josef Bacik02c24a82011-07-16 20:44:56 -0400705 if (!wb) {
706 ret = -ENOMEM;
707 goto out;
708 }
David Howells31143d52007-05-09 02:33:46 -0700709 wb->vnode = vnode;
710 wb->first = 0;
711 wb->last = -1;
712 wb->offset_first = 0;
713 wb->to_last = PAGE_SIZE;
714 wb->usage = 1;
715 wb->state = AFS_WBACK_SYNCING;
716 init_waitqueue_head(&wb->waitq);
717
718 spin_lock(&vnode->writeback_lock);
719 list_for_each_entry(xwb, &vnode->writebacks, link) {
720 if (xwb->state == AFS_WBACK_PENDING)
721 xwb->state = AFS_WBACK_CONFLICTING;
722 }
723 list_add_tail(&wb->link, &vnode->writebacks);
724 spin_unlock(&vnode->writeback_lock);
725
726 /* push all the outstanding writebacks to the server */
727 ret = afs_writeback_all(vnode);
728 if (ret < 0) {
729 afs_put_writeback(wb);
730 _leave(" = %d [wb]", ret);
Josef Bacik02c24a82011-07-16 20:44:56 -0400731 goto out;
David Howells31143d52007-05-09 02:33:46 -0700732 }
733
734 /* wait for the preceding writes to actually complete */
735 ret = wait_event_interruptible(wb->waitq,
736 wb->state == AFS_WBACK_COMPLETE ||
737 vnode->writebacks.next == &wb->link);
738 afs_put_writeback(wb);
739 _leave(" = %d", ret);
Josef Bacik02c24a82011-07-16 20:44:56 -0400740out:
Al Viro59551022016-01-22 15:40:57 -0500741 inode_unlock(inode);
David Howells31143d52007-05-09 02:33:46 -0700742 return ret;
743}
David Howells9b3f26c2009-04-03 16:42:41 +0100744
745/*
David Howells7286fad2017-03-16 16:27:45 +0000746 * Flush out all outstanding writes on a file opened for writing when it is
747 * closed.
748 */
749int afs_flush(struct file *file, fl_owner_t id)
750{
751 _enter("");
752
753 if ((file->f_mode & FMODE_WRITE) == 0)
754 return 0;
755
756 return vfs_fsync(file, 0);
757}
758
759/*
David Howells9b3f26c2009-04-03 16:42:41 +0100760 * notification that a previously read-only page is about to become writable
761 * - if it returns an error, the caller will deliver a bus error signal
762 */
763int afs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
764{
765 struct afs_vnode *vnode = AFS_FS_I(vma->vm_file->f_mapping->host);
766
767 _enter("{{%x:%u}},{%lx}",
768 vnode->fid.vid, vnode->fid.vnode, page->index);
769
770 /* wait for the page to be written to the cache before we allow it to
771 * be modified */
772#ifdef CONFIG_AFS_FSCACHE
773 fscache_wait_on_page_write(vnode->cache, page);
774#endif
775
776 _leave(" = 0");
777 return 0;
778}