blob: 5e15a21dbf9f3f9d705c2889761e04749c45531a [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,
Nick Piggin15b46502008-10-15 22:04:32 -070087 loff_t pos, unsigned len, struct page *page)
David Howells31143d52007-05-09 02:33:46 -070088{
Nick Piggin15b46502008-10-15 22:04:32 -070089 loff_t i_size;
90 unsigned eof;
David Howells31143d52007-05-09 02:33:46 -070091 int ret;
92
Nick Piggin15b46502008-10-15 22:04:32 -070093 _enter(",,%llu,%u", (unsigned long long)pos, len);
David Howells31143d52007-05-09 02:33:46 -070094
Nick Piggin15b46502008-10-15 22:04:32 -070095 ASSERTCMP(len, <=, PAGE_CACHE_SIZE);
David Howells31143d52007-05-09 02:33:46 -070096
Nick Piggin15b46502008-10-15 22:04:32 -070097 i_size = i_size_read(&vnode->vfs_inode);
98 if (pos + len > i_size)
99 eof = i_size;
100 else
101 eof = PAGE_CACHE_SIZE;
102
103 ret = afs_vnode_fetch_data(vnode, key, 0, eof, page);
David Howells31143d52007-05-09 02:33:46 -0700104 if (ret < 0) {
105 if (ret == -ENOENT) {
106 _debug("got NOENT from server"
107 " - marking file deleted and stale");
108 set_bit(AFS_VNODE_DELETED, &vnode->flags);
109 ret = -ESTALE;
110 }
111 }
112
113 _leave(" = %d", ret);
114 return ret;
115}
116
117/*
David Howells31143d52007-05-09 02:33:46 -0700118 * prepare to perform part of a write to a page
David Howells31143d52007-05-09 02:33:46 -0700119 */
Nick Piggin15b46502008-10-15 22:04:32 -0700120int afs_write_begin(struct file *file, struct address_space *mapping,
121 loff_t pos, unsigned len, unsigned flags,
122 struct page **pagep, void **fsdata)
David Howells31143d52007-05-09 02:33:46 -0700123{
124 struct afs_writeback *candidate, *wb;
125 struct afs_vnode *vnode = AFS_FS_I(file->f_dentry->d_inode);
Nick Piggin15b46502008-10-15 22:04:32 -0700126 struct page *page;
David Howells31143d52007-05-09 02:33:46 -0700127 struct key *key = file->private_data;
Nick Piggin15b46502008-10-15 22:04:32 -0700128 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
129 unsigned to = from + len;
130 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
David Howells31143d52007-05-09 02:33:46 -0700131 int ret;
132
133 _enter("{%x:%u},{%lx},%u,%u",
Nick Piggin15b46502008-10-15 22:04:32 -0700134 vnode->fid.vid, vnode->fid.vnode, index, from, to);
David Howells31143d52007-05-09 02:33:46 -0700135
136 candidate = kzalloc(sizeof(*candidate), GFP_KERNEL);
137 if (!candidate)
138 return -ENOMEM;
139 candidate->vnode = vnode;
Nick Piggin15b46502008-10-15 22:04:32 -0700140 candidate->first = candidate->last = index;
141 candidate->offset_first = from;
David Howells31143d52007-05-09 02:33:46 -0700142 candidate->to_last = to;
143 candidate->usage = 1;
144 candidate->state = AFS_WBACK_PENDING;
145 init_waitqueue_head(&candidate->waitq);
146
Nick Piggin54566b22009-01-04 12:00:53 -0800147 page = grab_cache_page_write_begin(mapping, index, flags);
Nick Piggin15b46502008-10-15 22:04:32 -0700148 if (!page) {
149 kfree(candidate);
150 return -ENOMEM;
151 }
152 *pagep = page;
153 /* page won't leak in error case: it eventually gets cleaned off LRU */
154
David Howells31143d52007-05-09 02:33:46 -0700155 if (!PageUptodate(page)) {
156 _debug("not up to date");
Nick Piggin15b46502008-10-15 22:04:32 -0700157 ret = afs_fill_page(vnode, key, pos, len, page);
David Howells31143d52007-05-09 02:33:46 -0700158 if (ret < 0) {
159 kfree(candidate);
160 _leave(" = %d [prep]", ret);
161 return ret;
162 }
Nick Piggin15b46502008-10-15 22:04:32 -0700163 SetPageUptodate(page);
David Howells31143d52007-05-09 02:33:46 -0700164 }
165
166try_again:
David Howells31143d52007-05-09 02:33:46 -0700167 spin_lock(&vnode->writeback_lock);
168
169 /* see if this page is already pending a writeback under a suitable key
170 * - if so we can just join onto that one */
171 wb = (struct afs_writeback *) page_private(page);
172 if (wb) {
173 if (wb->key == key && wb->state == AFS_WBACK_PENDING)
174 goto subsume_in_current_wb;
175 goto flush_conflicting_wb;
176 }
177
178 if (index > 0) {
179 /* see if we can find an already pending writeback that we can
180 * append this page to */
181 list_for_each_entry(wb, &vnode->writebacks, link) {
182 if (wb->last == index - 1 && wb->key == key &&
183 wb->state == AFS_WBACK_PENDING)
184 goto append_to_previous_wb;
185 }
186 }
187
188 list_add_tail(&candidate->link, &vnode->writebacks);
189 candidate->key = key_get(key);
190 spin_unlock(&vnode->writeback_lock);
191 SetPagePrivate(page);
192 set_page_private(page, (unsigned long) candidate);
193 _leave(" = 0 [new]");
194 return 0;
195
196subsume_in_current_wb:
197 _debug("subsume");
198 ASSERTRANGE(wb->first, <=, index, <=, wb->last);
Nick Piggin15b46502008-10-15 22:04:32 -0700199 if (index == wb->first && from < wb->offset_first)
200 wb->offset_first = from;
David Howells31143d52007-05-09 02:33:46 -0700201 if (index == wb->last && to > wb->to_last)
202 wb->to_last = to;
203 spin_unlock(&vnode->writeback_lock);
204 kfree(candidate);
205 _leave(" = 0 [sub]");
206 return 0;
207
208append_to_previous_wb:
209 _debug("append into %lx-%lx", wb->first, wb->last);
210 wb->usage++;
211 wb->last++;
212 wb->to_last = to;
213 spin_unlock(&vnode->writeback_lock);
214 SetPagePrivate(page);
215 set_page_private(page, (unsigned long) wb);
216 kfree(candidate);
217 _leave(" = 0 [app]");
218 return 0;
219
220 /* the page is currently bound to another context, so if it's dirty we
221 * need to flush it before we can use the new context */
222flush_conflicting_wb:
223 _debug("flush conflict");
224 if (wb->state == AFS_WBACK_PENDING)
225 wb->state = AFS_WBACK_CONFLICTING;
226 spin_unlock(&vnode->writeback_lock);
227 if (PageDirty(page)) {
228 ret = afs_write_back_from_locked_page(wb, page);
229 if (ret < 0) {
230 afs_put_writeback(candidate);
231 _leave(" = %d", ret);
232 return ret;
233 }
234 }
235
236 /* the page holds a ref on the writeback record */
237 afs_put_writeback(wb);
238 set_page_private(page, 0);
239 ClearPagePrivate(page);
240 goto try_again;
241}
242
243/*
244 * finalise part of a write to a page
245 */
Nick Piggin15b46502008-10-15 22:04:32 -0700246int afs_write_end(struct file *file, struct address_space *mapping,
247 loff_t pos, unsigned len, unsigned copied,
248 struct page *page, void *fsdata)
David Howells31143d52007-05-09 02:33:46 -0700249{
250 struct afs_vnode *vnode = AFS_FS_I(file->f_dentry->d_inode);
251 loff_t i_size, maybe_i_size;
252
Nick Piggin15b46502008-10-15 22:04:32 -0700253 _enter("{%x:%u},{%lx}",
254 vnode->fid.vid, vnode->fid.vnode, page->index);
David Howells31143d52007-05-09 02:33:46 -0700255
Nick Piggin15b46502008-10-15 22:04:32 -0700256 maybe_i_size = pos + copied;
David Howells31143d52007-05-09 02:33:46 -0700257
258 i_size = i_size_read(&vnode->vfs_inode);
259 if (maybe_i_size > i_size) {
260 spin_lock(&vnode->writeback_lock);
261 i_size = i_size_read(&vnode->vfs_inode);
262 if (maybe_i_size > i_size)
263 i_size_write(&vnode->vfs_inode, maybe_i_size);
264 spin_unlock(&vnode->writeback_lock);
265 }
266
267 set_page_dirty(page);
David Howells31143d52007-05-09 02:33:46 -0700268 if (PageDirty(page))
269 _debug("dirtied");
Nick Piggin15b46502008-10-15 22:04:32 -0700270 unlock_page(page);
271 page_cache_release(page);
David Howells31143d52007-05-09 02:33:46 -0700272
Nick Piggin15b46502008-10-15 22:04:32 -0700273 return copied;
David Howells31143d52007-05-09 02:33:46 -0700274}
275
276/*
277 * kill all the pages in the given range
278 */
279static void afs_kill_pages(struct afs_vnode *vnode, bool error,
280 pgoff_t first, pgoff_t last)
281{
282 struct pagevec pv;
283 unsigned count, loop;
284
285 _enter("{%x:%u},%lx-%lx",
286 vnode->fid.vid, vnode->fid.vnode, first, last);
287
288 pagevec_init(&pv, 0);
289
290 do {
291 _debug("kill %lx-%lx", first, last);
292
293 count = last - first + 1;
294 if (count > PAGEVEC_SIZE)
295 count = PAGEVEC_SIZE;
296 pv.nr = find_get_pages_contig(vnode->vfs_inode.i_mapping,
297 first, count, pv.pages);
298 ASSERTCMP(pv.nr, ==, count);
299
300 for (loop = 0; loop < count; loop++) {
301 ClearPageUptodate(pv.pages[loop]);
302 if (error)
303 SetPageError(pv.pages[loop]);
304 end_page_writeback(pv.pages[loop]);
305 }
306
307 __pagevec_release(&pv);
308 } while (first < last);
309
310 _leave("");
311}
312
313/*
314 * synchronously write back the locked page and any subsequent non-locked dirty
315 * pages also covered by the same writeback record
316 */
317static int afs_write_back_from_locked_page(struct afs_writeback *wb,
318 struct page *primary_page)
319{
320 struct page *pages[8], *page;
321 unsigned long count;
322 unsigned n, offset, to;
323 pgoff_t start, first, last;
324 int loop, ret;
325
326 _enter(",%lx", primary_page->index);
327
328 count = 1;
329 if (!clear_page_dirty_for_io(primary_page))
330 BUG();
331 if (test_set_page_writeback(primary_page))
332 BUG();
333
334 /* find all consecutive lockable dirty pages, stopping when we find a
335 * page that is not immediately lockable, is not dirty or is missing,
336 * or we reach the end of the range */
337 start = primary_page->index;
338 if (start >= wb->last)
339 goto no_more;
340 start++;
341 do {
342 _debug("more %lx [%lx]", start, count);
343 n = wb->last - start + 1;
344 if (n > ARRAY_SIZE(pages))
345 n = ARRAY_SIZE(pages);
346 n = find_get_pages_contig(wb->vnode->vfs_inode.i_mapping,
347 start, n, pages);
348 _debug("fgpc %u", n);
349 if (n == 0)
350 goto no_more;
351 if (pages[0]->index != start) {
David Howells9d577b62007-05-10 22:22:19 -0700352 do {
353 put_page(pages[--n]);
354 } while (n > 0);
David Howells31143d52007-05-09 02:33:46 -0700355 goto no_more;
356 }
357
358 for (loop = 0; loop < n; loop++) {
359 page = pages[loop];
360 if (page->index > wb->last)
361 break;
Nick Piggin529ae9a2008-08-02 12:01:03 +0200362 if (!trylock_page(page))
David Howells31143d52007-05-09 02:33:46 -0700363 break;
364 if (!PageDirty(page) ||
365 page_private(page) != (unsigned long) wb) {
366 unlock_page(page);
367 break;
368 }
369 if (!clear_page_dirty_for_io(page))
370 BUG();
371 if (test_set_page_writeback(page))
372 BUG();
373 unlock_page(page);
374 put_page(page);
375 }
376 count += loop;
377 if (loop < n) {
378 for (; loop < n; loop++)
379 put_page(pages[loop]);
380 goto no_more;
381 }
382
383 start += loop;
384 } while (start <= wb->last && count < 65536);
385
386no_more:
387 /* we now have a contiguous set of dirty pages, each with writeback set
388 * and the dirty mark cleared; the first page is locked and must remain
389 * so, all the rest are unlocked */
390 first = primary_page->index;
391 last = first + count - 1;
392
393 offset = (first == wb->first) ? wb->offset_first : 0;
394 to = (last == wb->last) ? wb->to_last : PAGE_SIZE;
395
396 _debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to);
397
398 ret = afs_vnode_store_data(wb, first, last, offset, to);
399 if (ret < 0) {
400 switch (ret) {
401 case -EDQUOT:
402 case -ENOSPC:
403 set_bit(AS_ENOSPC,
404 &wb->vnode->vfs_inode.i_mapping->flags);
405 break;
406 case -EROFS:
407 case -EIO:
408 case -EREMOTEIO:
409 case -EFBIG:
410 case -ENOENT:
411 case -ENOMEDIUM:
412 case -ENXIO:
413 afs_kill_pages(wb->vnode, true, first, last);
414 set_bit(AS_EIO, &wb->vnode->vfs_inode.i_mapping->flags);
415 break;
416 case -EACCES:
417 case -EPERM:
418 case -ENOKEY:
419 case -EKEYEXPIRED:
420 case -EKEYREJECTED:
421 case -EKEYREVOKED:
422 afs_kill_pages(wb->vnode, false, first, last);
423 break;
424 default:
425 break;
426 }
427 } else {
428 ret = count;
429 }
430
431 _leave(" = %d", ret);
432 return ret;
433}
434
435/*
436 * write a page back to the server
437 * - the caller locked the page for us
438 */
439int afs_writepage(struct page *page, struct writeback_control *wbc)
440{
441 struct backing_dev_info *bdi = page->mapping->backing_dev_info;
442 struct afs_writeback *wb;
443 int ret;
444
445 _enter("{%lx},", page->index);
446
David Howells31143d52007-05-09 02:33:46 -0700447 wb = (struct afs_writeback *) page_private(page);
448 ASSERT(wb != NULL);
449
450 ret = afs_write_back_from_locked_page(wb, page);
451 unlock_page(page);
452 if (ret < 0) {
453 _leave(" = %d", ret);
454 return 0;
455 }
456
457 wbc->nr_to_write -= ret;
458 if (wbc->nonblocking && bdi_write_congested(bdi))
459 wbc->encountered_congestion = 1;
460
461 _leave(" = 0");
462 return 0;
463}
464
465/*
466 * write a region of pages back to the server
467 */
Adrian Bunkc1206a22007-10-16 23:26:41 -0700468static int afs_writepages_region(struct address_space *mapping,
469 struct writeback_control *wbc,
470 pgoff_t index, pgoff_t end, pgoff_t *_next)
David Howells31143d52007-05-09 02:33:46 -0700471{
472 struct backing_dev_info *bdi = mapping->backing_dev_info;
473 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;
489 page_cache_release(page);
490 _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);
503 page_cache_release(page);
504 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);
512 continue;
513 }
514
515 wb = (struct afs_writeback *) page_private(page);
516 ASSERT(wb != NULL);
517
518 spin_lock(&wb->vnode->writeback_lock);
519 wb->state = AFS_WBACK_WRITING;
520 spin_unlock(&wb->vnode->writeback_lock);
521
522 ret = afs_write_back_from_locked_page(wb, page);
523 unlock_page(page);
524 page_cache_release(page);
525 if (ret < 0) {
526 _leave(" = %d", ret);
527 return ret;
528 }
529
530 wbc->nr_to_write -= ret;
531
532 if (wbc->nonblocking && bdi_write_congested(bdi)) {
533 wbc->encountered_congestion = 1;
534 break;
535 }
536
537 cond_resched();
538 } while (index < end && wbc->nr_to_write > 0);
539
540 *_next = index;
541 _leave(" = 0 [%lx]", *_next);
542 return 0;
543}
544
545/*
546 * write some of the pending data back to the server
547 */
548int afs_writepages(struct address_space *mapping,
549 struct writeback_control *wbc)
550{
551 struct backing_dev_info *bdi = mapping->backing_dev_info;
552 pgoff_t start, end, next;
553 int ret;
554
555 _enter("");
556
557 if (wbc->nonblocking && bdi_write_congested(bdi)) {
558 wbc->encountered_congestion = 1;
559 _leave(" = 0 [congest]");
560 return 0;
561 }
562
563 if (wbc->range_cyclic) {
564 start = mapping->writeback_index;
565 end = -1;
566 ret = afs_writepages_region(mapping, wbc, start, end, &next);
567 if (start > 0 && wbc->nr_to_write > 0 && ret == 0 &&
568 !(wbc->nonblocking && wbc->encountered_congestion))
569 ret = afs_writepages_region(mapping, wbc, 0, start,
570 &next);
571 mapping->writeback_index = next;
572 } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
573 end = (pgoff_t)(LLONG_MAX >> PAGE_CACHE_SHIFT);
574 ret = afs_writepages_region(mapping, wbc, 0, end, &next);
575 if (wbc->nr_to_write > 0)
576 mapping->writeback_index = next;
577 } else {
578 start = wbc->range_start >> PAGE_CACHE_SHIFT;
579 end = wbc->range_end >> PAGE_CACHE_SHIFT;
580 ret = afs_writepages_region(mapping, wbc, start, end, &next);
581 }
582
583 _leave(" = %d", ret);
584 return ret;
585}
586
587/*
588 * write an inode back
589 */
590int afs_write_inode(struct inode *inode, int sync)
591{
592 struct afs_vnode *vnode = AFS_FS_I(inode);
593 int ret;
594
595 _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
596
597 ret = 0;
598 if (sync) {
599 ret = filemap_fdatawait(inode->i_mapping);
600 if (ret < 0)
601 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
602 }
603
604 _leave(" = %d", ret);
605 return ret;
606}
607
608/*
609 * completion of write to server
610 */
611void afs_pages_written_back(struct afs_vnode *vnode, struct afs_call *call)
612{
613 struct afs_writeback *wb = call->wb;
614 struct pagevec pv;
615 unsigned count, loop;
616 pgoff_t first = call->first, last = call->last;
617 bool free_wb;
618
619 _enter("{%x:%u},{%lx-%lx}",
620 vnode->fid.vid, vnode->fid.vnode, first, last);
621
622 ASSERT(wb != NULL);
623
624 pagevec_init(&pv, 0);
625
626 do {
David Howells5bbf5d32007-05-10 03:15:23 -0700627 _debug("done %lx-%lx", first, last);
David Howells31143d52007-05-09 02:33:46 -0700628
629 count = last - first + 1;
630 if (count > PAGEVEC_SIZE)
631 count = PAGEVEC_SIZE;
632 pv.nr = find_get_pages_contig(call->mapping, first, count,
633 pv.pages);
634 ASSERTCMP(pv.nr, ==, count);
635
636 spin_lock(&vnode->writeback_lock);
637 for (loop = 0; loop < count; loop++) {
638 struct page *page = pv.pages[loop];
639 end_page_writeback(page);
640 if (page_private(page) == (unsigned long) wb) {
641 set_page_private(page, 0);
642 ClearPagePrivate(page);
643 wb->usage--;
644 }
645 }
646 free_wb = false;
647 if (wb->usage == 0) {
648 afs_unlink_writeback(wb);
649 free_wb = true;
650 }
651 spin_unlock(&vnode->writeback_lock);
652 first += count;
653 if (free_wb) {
654 afs_free_writeback(wb);
655 wb = NULL;
656 }
657
658 __pagevec_release(&pv);
David Howells5bbf5d32007-05-10 03:15:23 -0700659 } while (first <= last);
David Howells31143d52007-05-09 02:33:46 -0700660
661 _leave("");
662}
663
664/*
665 * write to an AFS file
666 */
667ssize_t afs_file_write(struct kiocb *iocb, const struct iovec *iov,
668 unsigned long nr_segs, loff_t pos)
669{
670 struct dentry *dentry = iocb->ki_filp->f_path.dentry;
671 struct afs_vnode *vnode = AFS_FS_I(dentry->d_inode);
672 ssize_t result;
673 size_t count = iov_length(iov, nr_segs);
David Howells31143d52007-05-09 02:33:46 -0700674
675 _enter("{%x.%u},{%zu},%lu,",
676 vnode->fid.vid, vnode->fid.vnode, count, nr_segs);
677
678 if (IS_SWAPFILE(&vnode->vfs_inode)) {
679 printk(KERN_INFO
680 "AFS: Attempt to write to active swap file!\n");
681 return -EBUSY;
682 }
683
684 if (!count)
685 return 0;
686
687 result = generic_file_aio_write(iocb, iov, nr_segs, pos);
688 if (IS_ERR_VALUE(result)) {
689 _leave(" = %zd", result);
690 return result;
691 }
692
David Howells31143d52007-05-09 02:33:46 -0700693 _leave(" = %zd", result);
694 return result;
695}
696
697/*
698 * flush the vnode to the fileserver
699 */
700int afs_writeback_all(struct afs_vnode *vnode)
701{
702 struct address_space *mapping = vnode->vfs_inode.i_mapping;
703 struct writeback_control wbc = {
704 .bdi = mapping->backing_dev_info,
705 .sync_mode = WB_SYNC_ALL,
706 .nr_to_write = LONG_MAX,
David Howells31143d52007-05-09 02:33:46 -0700707 .range_cyclic = 1,
708 };
709 int ret;
710
711 _enter("");
712
713 ret = mapping->a_ops->writepages(mapping, &wbc);
714 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
715
716 _leave(" = %d", ret);
717 return ret;
718}
719
720/*
721 * flush any dirty pages for this process, and check for write errors.
722 * - the return status from this call provides a reliable indication of
723 * whether any write errors occurred for this process.
724 */
725int afs_fsync(struct file *file, struct dentry *dentry, int datasync)
726{
727 struct afs_writeback *wb, *xwb;
728 struct afs_vnode *vnode = AFS_FS_I(dentry->d_inode);
729 int ret;
730
731 _enter("{%x:%u},{n=%s},%d",
732 vnode->fid.vid, vnode->fid.vnode, dentry->d_name.name,
733 datasync);
734
735 /* use a writeback record as a marker in the queue - when this reaches
736 * the front of the queue, all the outstanding writes are either
737 * completed or rejected */
738 wb = kzalloc(sizeof(*wb), GFP_KERNEL);
739 if (!wb)
740 return -ENOMEM;
741 wb->vnode = vnode;
742 wb->first = 0;
743 wb->last = -1;
744 wb->offset_first = 0;
745 wb->to_last = PAGE_SIZE;
746 wb->usage = 1;
747 wb->state = AFS_WBACK_SYNCING;
748 init_waitqueue_head(&wb->waitq);
749
750 spin_lock(&vnode->writeback_lock);
751 list_for_each_entry(xwb, &vnode->writebacks, link) {
752 if (xwb->state == AFS_WBACK_PENDING)
753 xwb->state = AFS_WBACK_CONFLICTING;
754 }
755 list_add_tail(&wb->link, &vnode->writebacks);
756 spin_unlock(&vnode->writeback_lock);
757
758 /* push all the outstanding writebacks to the server */
759 ret = afs_writeback_all(vnode);
760 if (ret < 0) {
761 afs_put_writeback(wb);
762 _leave(" = %d [wb]", ret);
763 return ret;
764 }
765
766 /* wait for the preceding writes to actually complete */
767 ret = wait_event_interruptible(wb->waitq,
768 wb->state == AFS_WBACK_COMPLETE ||
769 vnode->writebacks.next == &wb->link);
770 afs_put_writeback(wb);
771 _leave(" = %d", ret);
772 return ret;
773}
David Howells9b3f26c2009-04-03 16:42:41 +0100774
775/*
776 * notification that a previously read-only page is about to become writable
777 * - if it returns an error, the caller will deliver a bus error signal
778 */
779int afs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
780{
781 struct afs_vnode *vnode = AFS_FS_I(vma->vm_file->f_mapping->host);
782
783 _enter("{{%x:%u}},{%lx}",
784 vnode->fid.vid, vnode->fid.vnode, page->index);
785
786 /* wait for the page to be written to the cache before we allow it to
787 * be modified */
788#ifdef CONFIG_AFS_FSCACHE
789 fscache_wait_on_page_write(vnode->cache, page);
790#endif
791
792 _leave(" = 0");
793 return 0;
794}