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 unsigned start
, unsigned len
, struct page
*page
)
91 _enter(",,%u,%u", start
, len
);
93 ASSERTCMP(start
+ len
, <=, PAGE_SIZE
);
95 ret
= afs_vnode_fetch_data(vnode
, key
, start
, len
, page
);
98 _debug("got NOENT from server"
99 " - marking file deleted and stale");
100 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
105 _leave(" = %d", ret
);
110 * prepare a page for being written to
112 static int afs_prepare_page(struct afs_vnode
*vnode
, struct page
*page
,
113 struct key
*key
, unsigned offset
, unsigned to
)
115 unsigned eof
, tail
, start
, stop
, len
;
122 if (offset
== 0 && to
== PAGE_SIZE
)
125 p
= kmap_atomic(page
, KM_USER0
);
127 i_size
= i_size_read(&vnode
->vfs_inode
);
128 pos
= (loff_t
) page
->index
<< PAGE_SHIFT
;
130 /* partial write, page beyond EOF */
133 memset(p
, 0, offset
);
135 memset(p
+ to
, 0, PAGE_SIZE
- to
);
136 kunmap_atomic(p
, KM_USER0
);
140 if (i_size
- pos
>= PAGE_SIZE
) {
141 /* partial write, page entirely before EOF */
143 tail
= eof
= PAGE_SIZE
;
145 /* partial write, page overlaps EOF */
147 _debug("overlap %u", eof
);
149 if (tail
< PAGE_SIZE
)
150 memset(p
+ tail
, 0, PAGE_SIZE
- tail
);
152 memset(p
+ eof
, 0, PAGE_SIZE
- eof
);
155 kunmap_atomic(p
, KM_USER0
);
158 if (offset
> 0 || eof
> to
) {
159 /* need to fill one or two bits that aren't going to be written
160 * (cover both fillers in one read if there are two) */
161 start
= (offset
> 0) ? 0 : to
;
162 stop
= (eof
> to
) ? eof
: offset
;
164 _debug("wr=%u-%u av=0-%u rd=%u@%u",
165 offset
, to
, eof
, start
, len
);
166 ret
= afs_fill_page(vnode
, key
, start
, len
, page
);
169 _leave(" = %d", ret
);
174 * prepare to perform part of a write to a page
175 * - the caller holds the page locked, preventing it from being written out or
176 * modified by anyone else
178 int afs_prepare_write(struct file
*file
, struct page
*page
,
179 unsigned offset
, unsigned to
)
181 struct afs_writeback
*candidate
, *wb
;
182 struct afs_vnode
*vnode
= AFS_FS_I(file
->f_dentry
->d_inode
);
183 struct key
*key
= file
->private_data
;
187 _enter("{%x:%u},{%lx},%u,%u",
188 vnode
->fid
.vid
, vnode
->fid
.vnode
, page
->index
, offset
, to
);
190 candidate
= kzalloc(sizeof(*candidate
), GFP_KERNEL
);
193 candidate
->vnode
= vnode
;
194 candidate
->first
= candidate
->last
= page
->index
;
195 candidate
->offset_first
= offset
;
196 candidate
->to_last
= to
;
197 candidate
->usage
= 1;
198 candidate
->state
= AFS_WBACK_PENDING
;
199 init_waitqueue_head(&candidate
->waitq
);
201 if (!PageUptodate(page
)) {
202 _debug("not up to date");
203 ret
= afs_prepare_page(vnode
, page
, key
, offset
, to
);
206 _leave(" = %d [prep]", ret
);
213 spin_lock(&vnode
->writeback_lock
);
215 /* see if this page is already pending a writeback under a suitable key
216 * - if so we can just join onto that one */
217 wb
= (struct afs_writeback
*) page_private(page
);
219 if (wb
->key
== key
&& wb
->state
== AFS_WBACK_PENDING
)
220 goto subsume_in_current_wb
;
221 goto flush_conflicting_wb
;
225 /* see if we can find an already pending writeback that we can
226 * append this page to */
227 list_for_each_entry(wb
, &vnode
->writebacks
, link
) {
228 if (wb
->last
== index
- 1 && wb
->key
== key
&&
229 wb
->state
== AFS_WBACK_PENDING
)
230 goto append_to_previous_wb
;
234 list_add_tail(&candidate
->link
, &vnode
->writebacks
);
235 candidate
->key
= key_get(key
);
236 spin_unlock(&vnode
->writeback_lock
);
237 SetPagePrivate(page
);
238 set_page_private(page
, (unsigned long) candidate
);
239 _leave(" = 0 [new]");
242 subsume_in_current_wb
:
244 ASSERTRANGE(wb
->first
, <=, index
, <=, wb
->last
);
245 if (index
== wb
->first
&& offset
< wb
->offset_first
)
246 wb
->offset_first
= offset
;
247 if (index
== wb
->last
&& to
> wb
->to_last
)
249 spin_unlock(&vnode
->writeback_lock
);
251 _leave(" = 0 [sub]");
254 append_to_previous_wb
:
255 _debug("append into %lx-%lx", wb
->first
, wb
->last
);
259 spin_unlock(&vnode
->writeback_lock
);
260 SetPagePrivate(page
);
261 set_page_private(page
, (unsigned long) wb
);
263 _leave(" = 0 [app]");
266 /* the page is currently bound to another context, so if it's dirty we
267 * need to flush it before we can use the new context */
268 flush_conflicting_wb
:
269 _debug("flush conflict");
270 if (wb
->state
== AFS_WBACK_PENDING
)
271 wb
->state
= AFS_WBACK_CONFLICTING
;
272 spin_unlock(&vnode
->writeback_lock
);
273 if (PageDirty(page
)) {
274 ret
= afs_write_back_from_locked_page(wb
, page
);
276 afs_put_writeback(candidate
);
277 _leave(" = %d", ret
);
282 /* the page holds a ref on the writeback record */
283 afs_put_writeback(wb
);
284 set_page_private(page
, 0);
285 ClearPagePrivate(page
);
290 * finalise part of a write to a page
292 int afs_commit_write(struct file
*file
, struct page
*page
,
293 unsigned offset
, unsigned to
)
295 struct afs_vnode
*vnode
= AFS_FS_I(file
->f_dentry
->d_inode
);
296 loff_t i_size
, maybe_i_size
;
298 _enter("{%x:%u},{%lx},%u,%u",
299 vnode
->fid
.vid
, vnode
->fid
.vnode
, page
->index
, offset
, to
);
301 maybe_i_size
= (loff_t
) page
->index
<< PAGE_SHIFT
;
304 i_size
= i_size_read(&vnode
->vfs_inode
);
305 if (maybe_i_size
> i_size
) {
306 spin_lock(&vnode
->writeback_lock
);
307 i_size
= i_size_read(&vnode
->vfs_inode
);
308 if (maybe_i_size
> i_size
)
309 i_size_write(&vnode
->vfs_inode
, maybe_i_size
);
310 spin_unlock(&vnode
->writeback_lock
);
313 SetPageUptodate(page
);
314 set_page_dirty(page
);
322 * kill all the pages in the given range
324 static void afs_kill_pages(struct afs_vnode
*vnode
, bool error
,
325 pgoff_t first
, pgoff_t last
)
328 unsigned count
, loop
;
330 _enter("{%x:%u},%lx-%lx",
331 vnode
->fid
.vid
, vnode
->fid
.vnode
, first
, last
);
333 pagevec_init(&pv
, 0);
336 _debug("kill %lx-%lx", first
, last
);
338 count
= last
- first
+ 1;
339 if (count
> PAGEVEC_SIZE
)
340 count
= PAGEVEC_SIZE
;
341 pv
.nr
= find_get_pages_contig(vnode
->vfs_inode
.i_mapping
,
342 first
, count
, pv
.pages
);
343 ASSERTCMP(pv
.nr
, ==, count
);
345 for (loop
= 0; loop
< count
; loop
++) {
346 ClearPageUptodate(pv
.pages
[loop
]);
348 SetPageError(pv
.pages
[loop
]);
349 end_page_writeback(pv
.pages
[loop
]);
352 __pagevec_release(&pv
);
353 } while (first
< last
);
359 * synchronously write back the locked page and any subsequent non-locked dirty
360 * pages also covered by the same writeback record
362 static int afs_write_back_from_locked_page(struct afs_writeback
*wb
,
363 struct page
*primary_page
)
365 struct page
*pages
[8], *page
;
367 unsigned n
, offset
, to
;
368 pgoff_t start
, first
, last
;
371 _enter(",%lx", primary_page
->index
);
374 if (!clear_page_dirty_for_io(primary_page
))
376 if (test_set_page_writeback(primary_page
))
379 /* find all consecutive lockable dirty pages, stopping when we find a
380 * page that is not immediately lockable, is not dirty or is missing,
381 * or we reach the end of the range */
382 start
= primary_page
->index
;
383 if (start
>= wb
->last
)
387 _debug("more %lx [%lx]", start
, count
);
388 n
= wb
->last
- start
+ 1;
389 if (n
> ARRAY_SIZE(pages
))
390 n
= ARRAY_SIZE(pages
);
391 n
= find_get_pages_contig(wb
->vnode
->vfs_inode
.i_mapping
,
393 _debug("fgpc %u", n
);
396 if (pages
[0]->index
!= start
) {
398 put_page(pages
[--n
]);
403 for (loop
= 0; loop
< n
; loop
++) {
405 if (page
->index
> wb
->last
)
407 if (TestSetPageLocked(page
))
409 if (!PageDirty(page
) ||
410 page_private(page
) != (unsigned long) wb
) {
414 if (!clear_page_dirty_for_io(page
))
416 if (test_set_page_writeback(page
))
423 for (; loop
< n
; loop
++)
424 put_page(pages
[loop
]);
429 } while (start
<= wb
->last
&& count
< 65536);
432 /* we now have a contiguous set of dirty pages, each with writeback set
433 * and the dirty mark cleared; the first page is locked and must remain
434 * so, all the rest are unlocked */
435 first
= primary_page
->index
;
436 last
= first
+ count
- 1;
438 offset
= (first
== wb
->first
) ? wb
->offset_first
: 0;
439 to
= (last
== wb
->last
) ? wb
->to_last
: PAGE_SIZE
;
441 _debug("write back %lx[%u..] to %lx[..%u]", first
, offset
, last
, to
);
443 ret
= afs_vnode_store_data(wb
, first
, last
, offset
, to
);
449 &wb
->vnode
->vfs_inode
.i_mapping
->flags
);
458 afs_kill_pages(wb
->vnode
, true, first
, last
);
459 set_bit(AS_EIO
, &wb
->vnode
->vfs_inode
.i_mapping
->flags
);
467 afs_kill_pages(wb
->vnode
, false, first
, last
);
476 _leave(" = %d", ret
);
481 * write a page back to the server
482 * - the caller locked the page for us
484 int afs_writepage(struct page
*page
, struct writeback_control
*wbc
)
486 struct backing_dev_info
*bdi
= page
->mapping
->backing_dev_info
;
487 struct afs_writeback
*wb
;
490 _enter("{%lx},", page
->index
);
492 wb
= (struct afs_writeback
*) page_private(page
);
495 ret
= afs_write_back_from_locked_page(wb
, page
);
498 _leave(" = %d", ret
);
502 wbc
->nr_to_write
-= ret
;
503 if (wbc
->nonblocking
&& bdi_write_congested(bdi
))
504 wbc
->encountered_congestion
= 1;
511 * write a region of pages back to the server
513 static int afs_writepages_region(struct address_space
*mapping
,
514 struct writeback_control
*wbc
,
515 pgoff_t index
, pgoff_t end
, pgoff_t
*_next
)
517 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
518 struct afs_writeback
*wb
;
522 _enter(",,%lx,%lx,", index
, end
);
525 n
= find_get_pages_tag(mapping
, &index
, PAGECACHE_TAG_DIRTY
,
530 _debug("wback %lx", page
->index
);
532 if (page
->index
> end
) {
534 page_cache_release(page
);
535 _leave(" = 0 [%lx]", *_next
);
539 /* at this point we hold neither mapping->tree_lock nor lock on
540 * the page itself: the page may be truncated or invalidated
541 * (changing page->mapping to NULL), or even swizzled back from
542 * swapper_space to tmpfs file mapping
546 if (page
->mapping
!= mapping
) {
548 page_cache_release(page
);
552 if (wbc
->sync_mode
!= WB_SYNC_NONE
)
553 wait_on_page_writeback(page
);
555 if (PageWriteback(page
) || !PageDirty(page
)) {
560 wb
= (struct afs_writeback
*) page_private(page
);
563 spin_lock(&wb
->vnode
->writeback_lock
);
564 wb
->state
= AFS_WBACK_WRITING
;
565 spin_unlock(&wb
->vnode
->writeback_lock
);
567 ret
= afs_write_back_from_locked_page(wb
, page
);
569 page_cache_release(page
);
571 _leave(" = %d", ret
);
575 wbc
->nr_to_write
-= ret
;
577 if (wbc
->nonblocking
&& bdi_write_congested(bdi
)) {
578 wbc
->encountered_congestion
= 1;
583 } while (index
< end
&& wbc
->nr_to_write
> 0);
586 _leave(" = 0 [%lx]", *_next
);
591 * write some of the pending data back to the server
593 int afs_writepages(struct address_space
*mapping
,
594 struct writeback_control
*wbc
)
596 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
597 pgoff_t start
, end
, next
;
602 if (wbc
->nonblocking
&& bdi_write_congested(bdi
)) {
603 wbc
->encountered_congestion
= 1;
604 _leave(" = 0 [congest]");
608 if (wbc
->range_cyclic
) {
609 start
= mapping
->writeback_index
;
611 ret
= afs_writepages_region(mapping
, wbc
, start
, end
, &next
);
612 if (start
> 0 && wbc
->nr_to_write
> 0 && ret
== 0 &&
613 !(wbc
->nonblocking
&& wbc
->encountered_congestion
))
614 ret
= afs_writepages_region(mapping
, wbc
, 0, start
,
616 mapping
->writeback_index
= next
;
617 } else if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
) {
618 end
= (pgoff_t
)(LLONG_MAX
>> PAGE_CACHE_SHIFT
);
619 ret
= afs_writepages_region(mapping
, wbc
, 0, end
, &next
);
620 if (wbc
->nr_to_write
> 0)
621 mapping
->writeback_index
= next
;
623 start
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
624 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
625 ret
= afs_writepages_region(mapping
, wbc
, start
, end
, &next
);
628 _leave(" = %d", ret
);
633 * write an inode back
635 int afs_write_inode(struct inode
*inode
, int sync
)
637 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
640 _enter("{%x:%u},", vnode
->fid
.vid
, vnode
->fid
.vnode
);
644 ret
= filemap_fdatawait(inode
->i_mapping
);
646 __mark_inode_dirty(inode
, I_DIRTY_DATASYNC
);
649 _leave(" = %d", ret
);
654 * completion of write to server
656 void afs_pages_written_back(struct afs_vnode
*vnode
, struct afs_call
*call
)
658 struct afs_writeback
*wb
= call
->wb
;
660 unsigned count
, loop
;
661 pgoff_t first
= call
->first
, last
= call
->last
;
664 _enter("{%x:%u},{%lx-%lx}",
665 vnode
->fid
.vid
, vnode
->fid
.vnode
, first
, last
);
669 pagevec_init(&pv
, 0);
672 _debug("done %lx-%lx", first
, last
);
674 count
= last
- first
+ 1;
675 if (count
> PAGEVEC_SIZE
)
676 count
= PAGEVEC_SIZE
;
677 pv
.nr
= find_get_pages_contig(call
->mapping
, first
, count
,
679 ASSERTCMP(pv
.nr
, ==, count
);
681 spin_lock(&vnode
->writeback_lock
);
682 for (loop
= 0; loop
< count
; loop
++) {
683 struct page
*page
= pv
.pages
[loop
];
684 end_page_writeback(page
);
685 if (page_private(page
) == (unsigned long) wb
) {
686 set_page_private(page
, 0);
687 ClearPagePrivate(page
);
692 if (wb
->usage
== 0) {
693 afs_unlink_writeback(wb
);
696 spin_unlock(&vnode
->writeback_lock
);
699 afs_free_writeback(wb
);
703 __pagevec_release(&pv
);
704 } while (first
<= last
);
710 * write to an AFS file
712 ssize_t
afs_file_write(struct kiocb
*iocb
, const struct iovec
*iov
,
713 unsigned long nr_segs
, loff_t pos
)
715 struct dentry
*dentry
= iocb
->ki_filp
->f_path
.dentry
;
716 struct afs_vnode
*vnode
= AFS_FS_I(dentry
->d_inode
);
718 size_t count
= iov_length(iov
, nr_segs
);
721 _enter("{%x.%u},{%zu},%lu,",
722 vnode
->fid
.vid
, vnode
->fid
.vnode
, count
, nr_segs
);
724 if (IS_SWAPFILE(&vnode
->vfs_inode
)) {
726 "AFS: Attempt to write to active swap file!\n");
733 result
= generic_file_aio_write(iocb
, iov
, nr_segs
, pos
);
734 if (IS_ERR_VALUE(result
)) {
735 _leave(" = %zd", result
);
739 /* return error values for O_SYNC and IS_SYNC() */
740 if (IS_SYNC(&vnode
->vfs_inode
) || iocb
->ki_filp
->f_flags
& O_SYNC
) {
741 ret
= afs_fsync(iocb
->ki_filp
, dentry
, 1);
746 _leave(" = %zd", result
);
751 * flush the vnode to the fileserver
753 int afs_writeback_all(struct afs_vnode
*vnode
)
755 struct address_space
*mapping
= vnode
->vfs_inode
.i_mapping
;
756 struct writeback_control wbc
= {
757 .bdi
= mapping
->backing_dev_info
,
758 .sync_mode
= WB_SYNC_ALL
,
759 .nr_to_write
= LONG_MAX
,
767 ret
= mapping
->a_ops
->writepages(mapping
, &wbc
);
768 __mark_inode_dirty(mapping
->host
, I_DIRTY_PAGES
);
770 _leave(" = %d", ret
);
775 * flush any dirty pages for this process, and check for write errors.
776 * - the return status from this call provides a reliable indication of
777 * whether any write errors occurred for this process.
779 int afs_fsync(struct file
*file
, struct dentry
*dentry
, int datasync
)
781 struct afs_writeback
*wb
, *xwb
;
782 struct afs_vnode
*vnode
= AFS_FS_I(dentry
->d_inode
);
785 _enter("{%x:%u},{n=%s},%d",
786 vnode
->fid
.vid
, vnode
->fid
.vnode
, dentry
->d_name
.name
,
789 /* use a writeback record as a marker in the queue - when this reaches
790 * the front of the queue, all the outstanding writes are either
791 * completed or rejected */
792 wb
= kzalloc(sizeof(*wb
), GFP_KERNEL
);
798 wb
->offset_first
= 0;
799 wb
->to_last
= PAGE_SIZE
;
801 wb
->state
= AFS_WBACK_SYNCING
;
802 init_waitqueue_head(&wb
->waitq
);
804 spin_lock(&vnode
->writeback_lock
);
805 list_for_each_entry(xwb
, &vnode
->writebacks
, link
) {
806 if (xwb
->state
== AFS_WBACK_PENDING
)
807 xwb
->state
= AFS_WBACK_CONFLICTING
;
809 list_add_tail(&wb
->link
, &vnode
->writebacks
);
810 spin_unlock(&vnode
->writeback_lock
);
812 /* push all the outstanding writebacks to the server */
813 ret
= afs_writeback_all(vnode
);
815 afs_put_writeback(wb
);
816 _leave(" = %d [wb]", ret
);
820 /* wait for the preceding writes to actually complete */
821 ret
= wait_event_interruptible(wb
->waitq
,
822 wb
->state
== AFS_WBACK_COMPLETE
||
823 vnode
->writebacks
.next
== &wb
->link
);
824 afs_put_writeback(wb
);
825 _leave(" = %d", ret
);