1 /* handling of writes to regular files and writing back to the server
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
11 #include <linux/backing-dev.h>
12 #include <linux/slab.h>
14 #include <linux/pagemap.h>
15 #include <linux/writeback.h>
16 #include <linux/pagevec.h>
19 static int afs_write_back_from_locked_page(struct afs_writeback
*wb
,
23 * mark a page as having been made dirty and thus needing writeback
25 int afs_set_page_dirty(struct page
*page
)
28 return __set_page_dirty_nobuffers(page
);
32 * unlink a writeback record because its usage has reached zero
33 * - must be called with the wb->vnode->writeback_lock held
35 static void afs_unlink_writeback(struct afs_writeback
*wb
)
37 struct afs_writeback
*front
;
38 struct afs_vnode
*vnode
= wb
->vnode
;
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
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
);
55 * free a writeback record
57 static void afs_free_writeback(struct afs_writeback
*wb
)
65 * dispose of a reference to a writeback record
67 void afs_put_writeback(struct afs_writeback
*wb
)
69 struct afs_vnode
*vnode
= wb
->vnode
;
71 _enter("{%d}", wb
->usage
);
73 spin_lock(&vnode
->writeback_lock
);
75 afs_unlink_writeback(wb
);
78 spin_unlock(&vnode
->writeback_lock
);
80 afs_free_writeback(wb
);
84 * partly or wholly fill a page that's under preparation for writing
86 static int afs_fill_page(struct afs_vnode
*vnode
, struct key
*key
,
87 loff_t pos
, struct page
*page
)
93 _enter(",,%llu", (unsigned long long)pos
);
95 i_size
= i_size_read(&vnode
->vfs_inode
);
96 if (pos
+ PAGE_CACHE_SIZE
> i_size
)
99 len
= PAGE_CACHE_SIZE
;
101 ret
= afs_vnode_fetch_data(vnode
, key
, pos
, len
, page
);
103 if (ret
== -ENOENT
) {
104 _debug("got NOENT from server"
105 " - marking file deleted and stale");
106 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
111 _leave(" = %d", ret
);
116 * prepare to perform part of a write to a page
118 int afs_write_begin(struct file
*file
, struct address_space
*mapping
,
119 loff_t pos
, unsigned len
, unsigned flags
,
120 struct page
**pagep
, void **fsdata
)
122 struct afs_writeback
*candidate
, *wb
;
123 struct afs_vnode
*vnode
= AFS_FS_I(file_inode(file
));
125 struct key
*key
= file
->private_data
;
126 unsigned from
= pos
& (PAGE_CACHE_SIZE
- 1);
127 unsigned to
= from
+ len
;
128 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
131 _enter("{%x:%u},{%lx},%u,%u",
132 vnode
->fid
.vid
, vnode
->fid
.vnode
, index
, from
, to
);
134 candidate
= kzalloc(sizeof(*candidate
), GFP_KERNEL
);
137 candidate
->vnode
= vnode
;
138 candidate
->first
= candidate
->last
= index
;
139 candidate
->offset_first
= from
;
140 candidate
->to_last
= to
;
141 INIT_LIST_HEAD(&candidate
->link
);
142 candidate
->usage
= 1;
143 candidate
->state
= AFS_WBACK_PENDING
;
144 init_waitqueue_head(&candidate
->waitq
);
146 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
152 /* page won't leak in error case: it eventually gets cleaned off LRU */
154 if (!PageUptodate(page
) && len
!= PAGE_CACHE_SIZE
) {
155 ret
= afs_fill_page(vnode
, key
, index
<< PAGE_CACHE_SHIFT
, page
);
158 _leave(" = %d [prep]", ret
);
161 SetPageUptodate(page
);
165 spin_lock(&vnode
->writeback_lock
);
167 /* see if this page is already pending a writeback under a suitable key
168 * - if so we can just join onto that one */
169 wb
= (struct afs_writeback
*) page_private(page
);
171 if (wb
->key
== key
&& wb
->state
== AFS_WBACK_PENDING
)
172 goto subsume_in_current_wb
;
173 goto flush_conflicting_wb
;
177 /* see if we can find an already pending writeback that we can
178 * append this page to */
179 list_for_each_entry(wb
, &vnode
->writebacks
, link
) {
180 if (wb
->last
== index
- 1 && wb
->key
== key
&&
181 wb
->state
== AFS_WBACK_PENDING
)
182 goto append_to_previous_wb
;
186 list_add_tail(&candidate
->link
, &vnode
->writebacks
);
187 candidate
->key
= key_get(key
);
188 spin_unlock(&vnode
->writeback_lock
);
189 SetPagePrivate(page
);
190 set_page_private(page
, (unsigned long) candidate
);
191 _leave(" = 0 [new]");
194 subsume_in_current_wb
:
196 ASSERTRANGE(wb
->first
, <=, index
, <=, wb
->last
);
197 if (index
== wb
->first
&& from
< wb
->offset_first
)
198 wb
->offset_first
= from
;
199 if (index
== wb
->last
&& to
> wb
->to_last
)
201 spin_unlock(&vnode
->writeback_lock
);
203 _leave(" = 0 [sub]");
206 append_to_previous_wb
:
207 _debug("append into %lx-%lx", wb
->first
, wb
->last
);
211 spin_unlock(&vnode
->writeback_lock
);
212 SetPagePrivate(page
);
213 set_page_private(page
, (unsigned long) wb
);
215 _leave(" = 0 [app]");
218 /* the page is currently bound to another context, so if it's dirty we
219 * need to flush it before we can use the new context */
220 flush_conflicting_wb
:
221 _debug("flush conflict");
222 if (wb
->state
== AFS_WBACK_PENDING
)
223 wb
->state
= AFS_WBACK_CONFLICTING
;
224 spin_unlock(&vnode
->writeback_lock
);
225 if (PageDirty(page
)) {
226 ret
= afs_write_back_from_locked_page(wb
, page
);
228 afs_put_writeback(candidate
);
229 _leave(" = %d", ret
);
234 /* the page holds a ref on the writeback record */
235 afs_put_writeback(wb
);
236 set_page_private(page
, 0);
237 ClearPagePrivate(page
);
242 * finalise part of a write to a page
244 int afs_write_end(struct file
*file
, struct address_space
*mapping
,
245 loff_t pos
, unsigned len
, unsigned copied
,
246 struct page
*page
, void *fsdata
)
248 struct afs_vnode
*vnode
= AFS_FS_I(file_inode(file
));
249 loff_t i_size
, maybe_i_size
;
251 _enter("{%x:%u},{%lx}",
252 vnode
->fid
.vid
, vnode
->fid
.vnode
, page
->index
);
254 maybe_i_size
= pos
+ copied
;
256 i_size
= i_size_read(&vnode
->vfs_inode
);
257 if (maybe_i_size
> i_size
) {
258 spin_lock(&vnode
->writeback_lock
);
259 i_size
= i_size_read(&vnode
->vfs_inode
);
260 if (maybe_i_size
> i_size
)
261 i_size_write(&vnode
->vfs_inode
, maybe_i_size
);
262 spin_unlock(&vnode
->writeback_lock
);
265 set_page_dirty(page
);
269 page_cache_release(page
);
275 * kill all the pages in the given range
277 static void afs_kill_pages(struct afs_vnode
*vnode
, bool error
,
278 pgoff_t first
, pgoff_t last
)
281 unsigned count
, loop
;
283 _enter("{%x:%u},%lx-%lx",
284 vnode
->fid
.vid
, vnode
->fid
.vnode
, first
, last
);
286 pagevec_init(&pv
, 0);
289 _debug("kill %lx-%lx", first
, last
);
291 count
= last
- first
+ 1;
292 if (count
> PAGEVEC_SIZE
)
293 count
= PAGEVEC_SIZE
;
294 pv
.nr
= find_get_pages_contig(vnode
->vfs_inode
.i_mapping
,
295 first
, count
, pv
.pages
);
296 ASSERTCMP(pv
.nr
, ==, count
);
298 for (loop
= 0; loop
< count
; loop
++) {
299 ClearPageUptodate(pv
.pages
[loop
]);
301 SetPageError(pv
.pages
[loop
]);
302 end_page_writeback(pv
.pages
[loop
]);
305 __pagevec_release(&pv
);
306 } while (first
< last
);
312 * synchronously write back the locked page and any subsequent non-locked dirty
313 * pages also covered by the same writeback record
315 static int afs_write_back_from_locked_page(struct afs_writeback
*wb
,
316 struct page
*primary_page
)
318 struct page
*pages
[8], *page
;
320 unsigned n
, offset
, to
;
321 pgoff_t start
, first
, last
;
324 _enter(",%lx", primary_page
->index
);
327 if (!clear_page_dirty_for_io(primary_page
))
329 if (test_set_page_writeback(primary_page
))
332 /* find all consecutive lockable dirty pages, stopping when we find a
333 * page that is not immediately lockable, is not dirty or is missing,
334 * or we reach the end of the range */
335 start
= primary_page
->index
;
336 if (start
>= wb
->last
)
340 _debug("more %lx [%lx]", start
, count
);
341 n
= wb
->last
- start
+ 1;
342 if (n
> ARRAY_SIZE(pages
))
343 n
= ARRAY_SIZE(pages
);
344 n
= find_get_pages_contig(wb
->vnode
->vfs_inode
.i_mapping
,
346 _debug("fgpc %u", n
);
349 if (pages
[0]->index
!= start
) {
351 put_page(pages
[--n
]);
356 for (loop
= 0; loop
< n
; loop
++) {
358 if (page
->index
> wb
->last
)
360 if (!trylock_page(page
))
362 if (!PageDirty(page
) ||
363 page_private(page
) != (unsigned long) wb
) {
367 if (!clear_page_dirty_for_io(page
))
369 if (test_set_page_writeback(page
))
376 for (; loop
< n
; loop
++)
377 put_page(pages
[loop
]);
382 } while (start
<= wb
->last
&& count
< 65536);
385 /* we now have a contiguous set of dirty pages, each with writeback set
386 * and the dirty mark cleared; the first page is locked and must remain
387 * so, all the rest are unlocked */
388 first
= primary_page
->index
;
389 last
= first
+ count
- 1;
391 offset
= (first
== wb
->first
) ? wb
->offset_first
: 0;
392 to
= (last
== wb
->last
) ? wb
->to_last
: PAGE_SIZE
;
394 _debug("write back %lx[%u..] to %lx[..%u]", first
, offset
, last
, to
);
396 ret
= afs_vnode_store_data(wb
, first
, last
, offset
, to
);
402 &wb
->vnode
->vfs_inode
.i_mapping
->flags
);
411 afs_kill_pages(wb
->vnode
, true, first
, last
);
412 set_bit(AS_EIO
, &wb
->vnode
->vfs_inode
.i_mapping
->flags
);
420 afs_kill_pages(wb
->vnode
, false, first
, last
);
429 _leave(" = %d", ret
);
434 * write a page back to the server
435 * - the caller locked the page for us
437 int afs_writepage(struct page
*page
, struct writeback_control
*wbc
)
439 struct afs_writeback
*wb
;
442 _enter("{%lx},", page
->index
);
444 wb
= (struct afs_writeback
*) page_private(page
);
447 ret
= afs_write_back_from_locked_page(wb
, page
);
450 _leave(" = %d", ret
);
454 wbc
->nr_to_write
-= ret
;
461 * write a region of pages back to the server
463 static int afs_writepages_region(struct address_space
*mapping
,
464 struct writeback_control
*wbc
,
465 pgoff_t index
, pgoff_t end
, pgoff_t
*_next
)
467 struct afs_writeback
*wb
;
471 _enter(",,%lx,%lx,", index
, end
);
474 n
= find_get_pages_tag(mapping
, &index
, PAGECACHE_TAG_DIRTY
,
479 _debug("wback %lx", page
->index
);
481 if (page
->index
> end
) {
483 page_cache_release(page
);
484 _leave(" = 0 [%lx]", *_next
);
488 /* at this point we hold neither mapping->tree_lock nor lock on
489 * the page itself: the page may be truncated or invalidated
490 * (changing page->mapping to NULL), or even swizzled back from
491 * swapper_space to tmpfs file mapping
495 if (page
->mapping
!= mapping
) {
497 page_cache_release(page
);
501 if (wbc
->sync_mode
!= WB_SYNC_NONE
)
502 wait_on_page_writeback(page
);
504 if (PageWriteback(page
) || !PageDirty(page
)) {
509 wb
= (struct afs_writeback
*) page_private(page
);
512 spin_lock(&wb
->vnode
->writeback_lock
);
513 wb
->state
= AFS_WBACK_WRITING
;
514 spin_unlock(&wb
->vnode
->writeback_lock
);
516 ret
= afs_write_back_from_locked_page(wb
, page
);
518 page_cache_release(page
);
520 _leave(" = %d", ret
);
524 wbc
->nr_to_write
-= ret
;
527 } while (index
< end
&& wbc
->nr_to_write
> 0);
530 _leave(" = 0 [%lx]", *_next
);
535 * write some of the pending data back to the server
537 int afs_writepages(struct address_space
*mapping
,
538 struct writeback_control
*wbc
)
540 pgoff_t start
, end
, next
;
545 if (wbc
->range_cyclic
) {
546 start
= mapping
->writeback_index
;
548 ret
= afs_writepages_region(mapping
, wbc
, start
, end
, &next
);
549 if (start
> 0 && wbc
->nr_to_write
> 0 && ret
== 0)
550 ret
= afs_writepages_region(mapping
, wbc
, 0, start
,
552 mapping
->writeback_index
= next
;
553 } else if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
) {
554 end
= (pgoff_t
)(LLONG_MAX
>> PAGE_CACHE_SHIFT
);
555 ret
= afs_writepages_region(mapping
, wbc
, 0, end
, &next
);
556 if (wbc
->nr_to_write
> 0)
557 mapping
->writeback_index
= next
;
559 start
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
560 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
561 ret
= afs_writepages_region(mapping
, wbc
, start
, end
, &next
);
564 _leave(" = %d", ret
);
569 * completion of write to server
571 void afs_pages_written_back(struct afs_vnode
*vnode
, struct afs_call
*call
)
573 struct afs_writeback
*wb
= call
->wb
;
575 unsigned count
, loop
;
576 pgoff_t first
= call
->first
, last
= call
->last
;
579 _enter("{%x:%u},{%lx-%lx}",
580 vnode
->fid
.vid
, vnode
->fid
.vnode
, first
, last
);
584 pagevec_init(&pv
, 0);
587 _debug("done %lx-%lx", first
, last
);
589 count
= last
- first
+ 1;
590 if (count
> PAGEVEC_SIZE
)
591 count
= PAGEVEC_SIZE
;
592 pv
.nr
= find_get_pages_contig(call
->mapping
, first
, count
,
594 ASSERTCMP(pv
.nr
, ==, count
);
596 spin_lock(&vnode
->writeback_lock
);
597 for (loop
= 0; loop
< count
; loop
++) {
598 struct page
*page
= pv
.pages
[loop
];
599 end_page_writeback(page
);
600 if (page_private(page
) == (unsigned long) wb
) {
601 set_page_private(page
, 0);
602 ClearPagePrivate(page
);
607 if (wb
->usage
== 0) {
608 afs_unlink_writeback(wb
);
611 spin_unlock(&vnode
->writeback_lock
);
614 afs_free_writeback(wb
);
618 __pagevec_release(&pv
);
619 } while (first
<= last
);
625 * write to an AFS file
627 ssize_t
afs_file_write(struct kiocb
*iocb
, struct iov_iter
*from
)
629 struct afs_vnode
*vnode
= AFS_FS_I(file_inode(iocb
->ki_filp
));
631 size_t count
= iov_iter_count(from
);
633 _enter("{%x.%u},{%zu},",
634 vnode
->fid
.vid
, vnode
->fid
.vnode
, count
);
636 if (IS_SWAPFILE(&vnode
->vfs_inode
)) {
638 "AFS: Attempt to write to active swap file!\n");
645 result
= generic_file_write_iter(iocb
, from
);
646 if (IS_ERR_VALUE(result
)) {
647 _leave(" = %zd", result
);
651 _leave(" = %zd", result
);
656 * flush the vnode to the fileserver
658 int afs_writeback_all(struct afs_vnode
*vnode
)
660 struct address_space
*mapping
= vnode
->vfs_inode
.i_mapping
;
661 struct writeback_control wbc
= {
662 .sync_mode
= WB_SYNC_ALL
,
663 .nr_to_write
= LONG_MAX
,
670 ret
= mapping
->a_ops
->writepages(mapping
, &wbc
);
671 __mark_inode_dirty(mapping
->host
, I_DIRTY_PAGES
);
673 _leave(" = %d", ret
);
678 * flush any dirty pages for this process, and check for write errors.
679 * - the return status from this call provides a reliable indication of
680 * whether any write errors occurred for this process.
682 int afs_fsync(struct file
*file
, loff_t start
, loff_t end
, int datasync
)
684 struct inode
*inode
= file_inode(file
);
685 struct afs_writeback
*wb
, *xwb
;
686 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
689 _enter("{%x:%u},{n=%pD},%d",
690 vnode
->fid
.vid
, vnode
->fid
.vnode
, file
,
693 ret
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
698 /* use a writeback record as a marker in the queue - when this reaches
699 * the front of the queue, all the outstanding writes are either
700 * completed or rejected */
701 wb
= kzalloc(sizeof(*wb
), GFP_KERNEL
);
709 wb
->offset_first
= 0;
710 wb
->to_last
= PAGE_SIZE
;
712 wb
->state
= AFS_WBACK_SYNCING
;
713 init_waitqueue_head(&wb
->waitq
);
715 spin_lock(&vnode
->writeback_lock
);
716 list_for_each_entry(xwb
, &vnode
->writebacks
, link
) {
717 if (xwb
->state
== AFS_WBACK_PENDING
)
718 xwb
->state
= AFS_WBACK_CONFLICTING
;
720 list_add_tail(&wb
->link
, &vnode
->writebacks
);
721 spin_unlock(&vnode
->writeback_lock
);
723 /* push all the outstanding writebacks to the server */
724 ret
= afs_writeback_all(vnode
);
726 afs_put_writeback(wb
);
727 _leave(" = %d [wb]", ret
);
731 /* wait for the preceding writes to actually complete */
732 ret
= wait_event_interruptible(wb
->waitq
,
733 wb
->state
== AFS_WBACK_COMPLETE
||
734 vnode
->writebacks
.next
== &wb
->link
);
735 afs_put_writeback(wb
);
736 _leave(" = %d", ret
);
743 * notification that a previously read-only page is about to become writable
744 * - if it returns an error, the caller will deliver a bus error signal
746 int afs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
748 struct afs_vnode
*vnode
= AFS_FS_I(vma
->vm_file
->f_mapping
->host
);
750 _enter("{{%x:%u}},{%lx}",
751 vnode
->fid
.vid
, vnode
->fid
.vnode
, page
->index
);
753 /* wait for the page to be written to the cache before we allow it to
755 #ifdef CONFIG_AFS_FSCACHE
756 fscache_wait_on_page_write(vnode
->cache
, page
);