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
, unsigned len
, struct page
*page
)
93 _enter(",,%llu,%u", (unsigned long long)pos
, len
);
95 ASSERTCMP(len
, <=, PAGE_CACHE_SIZE
);
97 i_size
= i_size_read(&vnode
->vfs_inode
);
98 if (pos
+ len
> i_size
)
101 eof
= PAGE_CACHE_SIZE
;
103 ret
= afs_vnode_fetch_data(vnode
, key
, 0, eof
, page
);
105 if (ret
== -ENOENT
) {
106 _debug("got NOENT from server"
107 " - marking file deleted and stale");
108 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
113 _leave(" = %d", ret
);
118 * prepare to perform part of a write to a page
120 int afs_write_begin(struct file
*file
, struct address_space
*mapping
,
121 loff_t pos
, unsigned len
, unsigned flags
,
122 struct page
**pagep
, void **fsdata
)
124 struct afs_writeback
*candidate
, *wb
;
125 struct afs_vnode
*vnode
= AFS_FS_I(file
->f_dentry
->d_inode
);
127 struct key
*key
= file
->private_data
;
128 unsigned from
= pos
& (PAGE_CACHE_SIZE
- 1);
129 unsigned to
= from
+ len
;
130 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
133 _enter("{%x:%u},{%lx},%u,%u",
134 vnode
->fid
.vid
, vnode
->fid
.vnode
, index
, from
, to
);
136 candidate
= kzalloc(sizeof(*candidate
), GFP_KERNEL
);
139 candidate
->vnode
= vnode
;
140 candidate
->first
= candidate
->last
= index
;
141 candidate
->offset_first
= from
;
142 candidate
->to_last
= to
;
143 candidate
->usage
= 1;
144 candidate
->state
= AFS_WBACK_PENDING
;
145 init_waitqueue_head(&candidate
->waitq
);
147 page
= __grab_cache_page(mapping
, index
);
153 /* page won't leak in error case: it eventually gets cleaned off LRU */
155 if (!PageUptodate(page
)) {
156 _debug("not up to date");
157 ret
= afs_fill_page(vnode
, key
, pos
, len
, page
);
160 _leave(" = %d [prep]", ret
);
163 SetPageUptodate(page
);
167 spin_lock(&vnode
->writeback_lock
);
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
);
173 if (wb
->key
== key
&& wb
->state
== AFS_WBACK_PENDING
)
174 goto subsume_in_current_wb
;
175 goto flush_conflicting_wb
;
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
;
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]");
196 subsume_in_current_wb
:
198 ASSERTRANGE(wb
->first
, <=, index
, <=, wb
->last
);
199 if (index
== wb
->first
&& from
< wb
->offset_first
)
200 wb
->offset_first
= from
;
201 if (index
== wb
->last
&& to
> wb
->to_last
)
203 spin_unlock(&vnode
->writeback_lock
);
205 _leave(" = 0 [sub]");
208 append_to_previous_wb
:
209 _debug("append into %lx-%lx", wb
->first
, wb
->last
);
213 spin_unlock(&vnode
->writeback_lock
);
214 SetPagePrivate(page
);
215 set_page_private(page
, (unsigned long) wb
);
217 _leave(" = 0 [app]");
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 */
222 flush_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
);
230 afs_put_writeback(candidate
);
231 _leave(" = %d", ret
);
236 /* the page holds a ref on the writeback record */
237 afs_put_writeback(wb
);
238 set_page_private(page
, 0);
239 ClearPagePrivate(page
);
244 * finalise part of a write to a page
246 int afs_write_end(struct file
*file
, struct address_space
*mapping
,
247 loff_t pos
, unsigned len
, unsigned copied
,
248 struct page
*page
, void *fsdata
)
250 struct afs_vnode
*vnode
= AFS_FS_I(file
->f_dentry
->d_inode
);
251 loff_t i_size
, maybe_i_size
;
253 _enter("{%x:%u},{%lx}",
254 vnode
->fid
.vid
, vnode
->fid
.vnode
, page
->index
);
256 maybe_i_size
= pos
+ copied
;
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
);
267 set_page_dirty(page
);
271 page_cache_release(page
);
277 * kill all the pages in the given range
279 static void afs_kill_pages(struct afs_vnode
*vnode
, bool error
,
280 pgoff_t first
, pgoff_t last
)
283 unsigned count
, loop
;
285 _enter("{%x:%u},%lx-%lx",
286 vnode
->fid
.vid
, vnode
->fid
.vnode
, first
, last
);
288 pagevec_init(&pv
, 0);
291 _debug("kill %lx-%lx", first
, last
);
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
);
300 for (loop
= 0; loop
< count
; loop
++) {
301 ClearPageUptodate(pv
.pages
[loop
]);
303 SetPageError(pv
.pages
[loop
]);
304 end_page_writeback(pv
.pages
[loop
]);
307 __pagevec_release(&pv
);
308 } while (first
< last
);
314 * synchronously write back the locked page and any subsequent non-locked dirty
315 * pages also covered by the same writeback record
317 static int afs_write_back_from_locked_page(struct afs_writeback
*wb
,
318 struct page
*primary_page
)
320 struct page
*pages
[8], *page
;
322 unsigned n
, offset
, to
;
323 pgoff_t start
, first
, last
;
326 _enter(",%lx", primary_page
->index
);
329 if (!clear_page_dirty_for_io(primary_page
))
331 if (test_set_page_writeback(primary_page
))
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
)
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
,
348 _debug("fgpc %u", n
);
351 if (pages
[0]->index
!= start
) {
353 put_page(pages
[--n
]);
358 for (loop
= 0; loop
< n
; loop
++) {
360 if (page
->index
> wb
->last
)
362 if (!trylock_page(page
))
364 if (!PageDirty(page
) ||
365 page_private(page
) != (unsigned long) wb
) {
369 if (!clear_page_dirty_for_io(page
))
371 if (test_set_page_writeback(page
))
378 for (; loop
< n
; loop
++)
379 put_page(pages
[loop
]);
384 } while (start
<= wb
->last
&& count
< 65536);
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;
393 offset
= (first
== wb
->first
) ? wb
->offset_first
: 0;
394 to
= (last
== wb
->last
) ? wb
->to_last
: PAGE_SIZE
;
396 _debug("write back %lx[%u..] to %lx[..%u]", first
, offset
, last
, to
);
398 ret
= afs_vnode_store_data(wb
, first
, last
, offset
, to
);
404 &wb
->vnode
->vfs_inode
.i_mapping
->flags
);
413 afs_kill_pages(wb
->vnode
, true, first
, last
);
414 set_bit(AS_EIO
, &wb
->vnode
->vfs_inode
.i_mapping
->flags
);
422 afs_kill_pages(wb
->vnode
, false, first
, last
);
431 _leave(" = %d", ret
);
436 * write a page back to the server
437 * - the caller locked the page for us
439 int afs_writepage(struct page
*page
, struct writeback_control
*wbc
)
441 struct backing_dev_info
*bdi
= page
->mapping
->backing_dev_info
;
442 struct afs_writeback
*wb
;
445 _enter("{%lx},", page
->index
);
447 wb
= (struct afs_writeback
*) page_private(page
);
450 ret
= afs_write_back_from_locked_page(wb
, page
);
453 _leave(" = %d", ret
);
457 wbc
->nr_to_write
-= ret
;
458 if (wbc
->nonblocking
&& bdi_write_congested(bdi
))
459 wbc
->encountered_congestion
= 1;
466 * write a region of pages back to the server
468 static int afs_writepages_region(struct address_space
*mapping
,
469 struct writeback_control
*wbc
,
470 pgoff_t index
, pgoff_t end
, pgoff_t
*_next
)
472 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
473 struct afs_writeback
*wb
;
477 _enter(",,%lx,%lx,", index
, end
);
480 n
= find_get_pages_tag(mapping
, &index
, PAGECACHE_TAG_DIRTY
,
485 _debug("wback %lx", page
->index
);
487 if (page
->index
> end
) {
489 page_cache_release(page
);
490 _leave(" = 0 [%lx]", *_next
);
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
501 if (page
->mapping
!= mapping
) {
503 page_cache_release(page
);
507 if (wbc
->sync_mode
!= WB_SYNC_NONE
)
508 wait_on_page_writeback(page
);
510 if (PageWriteback(page
) || !PageDirty(page
)) {
515 wb
= (struct afs_writeback
*) page_private(page
);
518 spin_lock(&wb
->vnode
->writeback_lock
);
519 wb
->state
= AFS_WBACK_WRITING
;
520 spin_unlock(&wb
->vnode
->writeback_lock
);
522 ret
= afs_write_back_from_locked_page(wb
, page
);
524 page_cache_release(page
);
526 _leave(" = %d", ret
);
530 wbc
->nr_to_write
-= ret
;
532 if (wbc
->nonblocking
&& bdi_write_congested(bdi
)) {
533 wbc
->encountered_congestion
= 1;
538 } while (index
< end
&& wbc
->nr_to_write
> 0);
541 _leave(" = 0 [%lx]", *_next
);
546 * write some of the pending data back to the server
548 int afs_writepages(struct address_space
*mapping
,
549 struct writeback_control
*wbc
)
551 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
552 pgoff_t start
, end
, next
;
557 if (wbc
->nonblocking
&& bdi_write_congested(bdi
)) {
558 wbc
->encountered_congestion
= 1;
559 _leave(" = 0 [congest]");
563 if (wbc
->range_cyclic
) {
564 start
= mapping
->writeback_index
;
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
,
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
;
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
);
583 _leave(" = %d", ret
);
588 * write an inode back
590 int afs_write_inode(struct inode
*inode
, int sync
)
592 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
595 _enter("{%x:%u},", vnode
->fid
.vid
, vnode
->fid
.vnode
);
599 ret
= filemap_fdatawait(inode
->i_mapping
);
601 __mark_inode_dirty(inode
, I_DIRTY_DATASYNC
);
604 _leave(" = %d", ret
);
609 * completion of write to server
611 void afs_pages_written_back(struct afs_vnode
*vnode
, struct afs_call
*call
)
613 struct afs_writeback
*wb
= call
->wb
;
615 unsigned count
, loop
;
616 pgoff_t first
= call
->first
, last
= call
->last
;
619 _enter("{%x:%u},{%lx-%lx}",
620 vnode
->fid
.vid
, vnode
->fid
.vnode
, first
, last
);
624 pagevec_init(&pv
, 0);
627 _debug("done %lx-%lx", first
, last
);
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
,
634 ASSERTCMP(pv
.nr
, ==, count
);
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
);
647 if (wb
->usage
== 0) {
648 afs_unlink_writeback(wb
);
651 spin_unlock(&vnode
->writeback_lock
);
654 afs_free_writeback(wb
);
658 __pagevec_release(&pv
);
659 } while (first
<= last
);
665 * write to an AFS file
667 ssize_t
afs_file_write(struct kiocb
*iocb
, const struct iovec
*iov
,
668 unsigned long nr_segs
, loff_t pos
)
670 struct dentry
*dentry
= iocb
->ki_filp
->f_path
.dentry
;
671 struct afs_vnode
*vnode
= AFS_FS_I(dentry
->d_inode
);
673 size_t count
= iov_length(iov
, nr_segs
);
676 _enter("{%x.%u},{%zu},%lu,",
677 vnode
->fid
.vid
, vnode
->fid
.vnode
, count
, nr_segs
);
679 if (IS_SWAPFILE(&vnode
->vfs_inode
)) {
681 "AFS: Attempt to write to active swap file!\n");
688 result
= generic_file_aio_write(iocb
, iov
, nr_segs
, pos
);
689 if (IS_ERR_VALUE(result
)) {
690 _leave(" = %zd", result
);
694 /* return error values for O_SYNC and IS_SYNC() */
695 if (IS_SYNC(&vnode
->vfs_inode
) || iocb
->ki_filp
->f_flags
& O_SYNC
) {
696 ret
= afs_fsync(iocb
->ki_filp
, dentry
, 1);
701 _leave(" = %zd", result
);
706 * flush the vnode to the fileserver
708 int afs_writeback_all(struct afs_vnode
*vnode
)
710 struct address_space
*mapping
= vnode
->vfs_inode
.i_mapping
;
711 struct writeback_control wbc
= {
712 .bdi
= mapping
->backing_dev_info
,
713 .sync_mode
= WB_SYNC_ALL
,
714 .nr_to_write
= LONG_MAX
,
722 ret
= mapping
->a_ops
->writepages(mapping
, &wbc
);
723 __mark_inode_dirty(mapping
->host
, I_DIRTY_PAGES
);
725 _leave(" = %d", ret
);
730 * flush any dirty pages for this process, and check for write errors.
731 * - the return status from this call provides a reliable indication of
732 * whether any write errors occurred for this process.
734 int afs_fsync(struct file
*file
, struct dentry
*dentry
, int datasync
)
736 struct afs_writeback
*wb
, *xwb
;
737 struct afs_vnode
*vnode
= AFS_FS_I(dentry
->d_inode
);
740 _enter("{%x:%u},{n=%s},%d",
741 vnode
->fid
.vid
, vnode
->fid
.vnode
, dentry
->d_name
.name
,
744 /* use a writeback record as a marker in the queue - when this reaches
745 * the front of the queue, all the outstanding writes are either
746 * completed or rejected */
747 wb
= kzalloc(sizeof(*wb
), GFP_KERNEL
);
753 wb
->offset_first
= 0;
754 wb
->to_last
= PAGE_SIZE
;
756 wb
->state
= AFS_WBACK_SYNCING
;
757 init_waitqueue_head(&wb
->waitq
);
759 spin_lock(&vnode
->writeback_lock
);
760 list_for_each_entry(xwb
, &vnode
->writebacks
, link
) {
761 if (xwb
->state
== AFS_WBACK_PENDING
)
762 xwb
->state
= AFS_WBACK_CONFLICTING
;
764 list_add_tail(&wb
->link
, &vnode
->writebacks
);
765 spin_unlock(&vnode
->writeback_lock
);
767 /* push all the outstanding writebacks to the server */
768 ret
= afs_writeback_all(vnode
);
770 afs_put_writeback(wb
);
771 _leave(" = %d [wb]", ret
);
775 /* wait for the preceding writes to actually complete */
776 ret
= wait_event_interruptible(wb
->waitq
,
777 wb
->state
== AFS_WBACK_COMPLETE
||
778 vnode
->writebacks
.next
== &wb
->link
);
779 afs_put_writeback(wb
);
780 _leave(" = %d", ret
);