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 int len, struct page *page)
92 _enter(",,%llu", (unsigned long long)pos);
94 req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *),
99 atomic_set(&req->usage, 1);
103 req->pages[0] = page;
106 ret = afs_vnode_fetch_data(vnode, key, req);
109 if (ret == -ENOENT) {
110 _debug("got NOENT from server"
111 " - marking file deleted and stale");
112 set_bit(AFS_VNODE_DELETED, &vnode->flags);
117 _leave(" = %d", ret);
122 * prepare to perform part of a write to a page
124 int afs_write_begin(struct file *file, struct address_space *mapping,
125 loff_t pos, unsigned len, unsigned flags,
126 struct page **pagep, void **fsdata)
128 struct afs_writeback *candidate, *wb;
129 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
131 struct key *key = file->private_data;
132 unsigned from = pos & (PAGE_SIZE - 1);
133 unsigned to = from + len;
134 pgoff_t index = pos >> PAGE_SHIFT;
137 _enter("{%x:%u},{%lx},%u,%u",
138 vnode->fid.vid, vnode->fid.vnode, index, from, to);
140 candidate = kzalloc(sizeof(*candidate), GFP_KERNEL);
143 candidate->vnode = vnode;
144 candidate->first = candidate->last = index;
145 candidate->offset_first = from;
146 candidate->to_last = to;
147 INIT_LIST_HEAD(&candidate->link);
148 candidate->usage = 1;
149 candidate->state = AFS_WBACK_PENDING;
150 init_waitqueue_head(&candidate->waitq);
152 page = grab_cache_page_write_begin(mapping, index, flags);
158 if (!PageUptodate(page) && len != PAGE_SIZE) {
159 ret = afs_fill_page(vnode, key, pos & PAGE_MASK, PAGE_SIZE, page);
164 _leave(" = %d [prep]", ret);
167 SetPageUptodate(page);
170 /* page won't leak in error case: it eventually gets cleaned off LRU */
174 spin_lock(&vnode->writeback_lock);
176 /* see if this page is already pending a writeback under a suitable key
177 * - if so we can just join onto that one */
178 wb = (struct afs_writeback *) page_private(page);
180 if (wb->key == key && wb->state == AFS_WBACK_PENDING)
181 goto subsume_in_current_wb;
182 goto flush_conflicting_wb;
186 /* see if we can find an already pending writeback that we can
187 * append this page to */
188 list_for_each_entry(wb, &vnode->writebacks, link) {
189 if (wb->last == index - 1 && wb->key == key &&
190 wb->state == AFS_WBACK_PENDING)
191 goto append_to_previous_wb;
195 list_add_tail(&candidate->link, &vnode->writebacks);
196 candidate->key = key_get(key);
197 spin_unlock(&vnode->writeback_lock);
198 SetPagePrivate(page);
199 set_page_private(page, (unsigned long) candidate);
200 _leave(" = 0 [new]");
203 subsume_in_current_wb:
205 ASSERTRANGE(wb->first, <=, index, <=, wb->last);
206 if (index == wb->first && from < wb->offset_first)
207 wb->offset_first = from;
208 if (index == wb->last && to > wb->to_last)
210 spin_unlock(&vnode->writeback_lock);
212 _leave(" = 0 [sub]");
215 append_to_previous_wb:
216 _debug("append into %lx-%lx", wb->first, wb->last);
220 spin_unlock(&vnode->writeback_lock);
221 SetPagePrivate(page);
222 set_page_private(page, (unsigned long) wb);
224 _leave(" = 0 [app]");
227 /* the page is currently bound to another context, so if it's dirty we
228 * need to flush it before we can use the new context */
229 flush_conflicting_wb:
230 _debug("flush conflict");
231 if (wb->state == AFS_WBACK_PENDING)
232 wb->state = AFS_WBACK_CONFLICTING;
233 spin_unlock(&vnode->writeback_lock);
234 if (clear_page_dirty_for_io(page)) {
235 ret = afs_write_back_from_locked_page(wb, page);
237 afs_put_writeback(candidate);
238 _leave(" = %d", ret);
243 /* the page holds a ref on the writeback record */
244 afs_put_writeback(wb);
245 set_page_private(page, 0);
246 ClearPagePrivate(page);
251 * finalise part of a write to a page
253 int afs_write_end(struct file *file, struct address_space *mapping,
254 loff_t pos, unsigned len, unsigned copied,
255 struct page *page, void *fsdata)
257 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
258 struct key *key = file->private_data;
259 loff_t i_size, maybe_i_size;
262 _enter("{%x:%u},{%lx}",
263 vnode->fid.vid, vnode->fid.vnode, page->index);
265 maybe_i_size = pos + copied;
267 i_size = i_size_read(&vnode->vfs_inode);
268 if (maybe_i_size > i_size) {
269 spin_lock(&vnode->writeback_lock);
270 i_size = i_size_read(&vnode->vfs_inode);
271 if (maybe_i_size > i_size)
272 i_size_write(&vnode->vfs_inode, maybe_i_size);
273 spin_unlock(&vnode->writeback_lock);
276 if (!PageUptodate(page)) {
278 /* Try and load any missing data from the server. The
279 * unmarshalling routine will take care of clearing any
280 * bits that are beyond the EOF.
282 ret = afs_fill_page(vnode, key, pos + copied,
287 SetPageUptodate(page);
290 set_page_dirty(page);
302 * kill all the pages in the given range
304 static void afs_kill_pages(struct afs_vnode *vnode, bool error,
305 pgoff_t first, pgoff_t last)
308 unsigned count, loop;
310 _enter("{%x:%u},%lx-%lx",
311 vnode->fid.vid, vnode->fid.vnode, first, last);
313 pagevec_init(&pv, 0);
316 _debug("kill %lx-%lx", first, last);
318 count = last - first + 1;
319 if (count > PAGEVEC_SIZE)
320 count = PAGEVEC_SIZE;
321 pv.nr = find_get_pages_contig(vnode->vfs_inode.i_mapping,
322 first, count, pv.pages);
323 ASSERTCMP(pv.nr, ==, count);
325 for (loop = 0; loop < count; loop++) {
326 struct page *page = pv.pages[loop];
327 ClearPageUptodate(page);
330 if (PageWriteback(page))
331 end_page_writeback(page);
332 if (page->index >= first)
333 first = page->index + 1;
336 __pagevec_release(&pv);
337 } while (first < last);
343 * synchronously write back the locked page and any subsequent non-locked dirty
344 * pages also covered by the same writeback record
346 static int afs_write_back_from_locked_page(struct afs_writeback *wb,
347 struct page *primary_page)
349 struct page *pages[8], *page;
351 unsigned n, offset, to;
352 pgoff_t start, first, last;
355 _enter(",%lx", primary_page->index);
358 if (test_set_page_writeback(primary_page))
361 /* find all consecutive lockable dirty pages, stopping when we find a
362 * page that is not immediately lockable, is not dirty or is missing,
363 * or we reach the end of the range */
364 start = primary_page->index;
365 if (start >= wb->last)
369 _debug("more %lx [%lx]", start, count);
370 n = wb->last - start + 1;
371 if (n > ARRAY_SIZE(pages))
372 n = ARRAY_SIZE(pages);
373 n = find_get_pages_contig(wb->vnode->vfs_inode.i_mapping,
375 _debug("fgpc %u", n);
378 if (pages[0]->index != start) {
380 put_page(pages[--n]);
385 for (loop = 0; loop < n; loop++) {
387 if (page->index > wb->last)
389 if (!trylock_page(page))
391 if (!PageDirty(page) ||
392 page_private(page) != (unsigned long) wb) {
396 if (!clear_page_dirty_for_io(page))
398 if (test_set_page_writeback(page))
405 for (; loop < n; loop++)
406 put_page(pages[loop]);
411 } while (start <= wb->last && count < 65536);
414 /* we now have a contiguous set of dirty pages, each with writeback set
415 * and the dirty mark cleared; the first page is locked and must remain
416 * so, all the rest are unlocked */
417 first = primary_page->index;
418 last = first + count - 1;
420 offset = (first == wb->first) ? wb->offset_first : 0;
421 to = (last == wb->last) ? wb->to_last : PAGE_SIZE;
423 _debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to);
425 ret = afs_vnode_store_data(wb, first, last, offset, to);
430 mapping_set_error(wb->vnode->vfs_inode.i_mapping, -ENOSPC);
439 afs_kill_pages(wb->vnode, true, first, last);
440 mapping_set_error(wb->vnode->vfs_inode.i_mapping, -EIO);
448 afs_kill_pages(wb->vnode, false, first, last);
457 _leave(" = %d", ret);
462 * write a page back to the server
463 * - the caller locked the page for us
465 int afs_writepage(struct page *page, struct writeback_control *wbc)
467 struct afs_writeback *wb;
470 _enter("{%lx},", page->index);
472 wb = (struct afs_writeback *) page_private(page);
475 ret = afs_write_back_from_locked_page(wb, page);
478 _leave(" = %d", ret);
482 wbc->nr_to_write -= ret;
489 * write a region of pages back to the server
491 static int afs_writepages_region(struct address_space *mapping,
492 struct writeback_control *wbc,
493 pgoff_t index, pgoff_t end, pgoff_t *_next)
495 struct afs_writeback *wb;
499 _enter(",,%lx,%lx,", index, end);
502 n = find_get_pages_tag(mapping, &index, PAGECACHE_TAG_DIRTY,
507 _debug("wback %lx", page->index);
509 if (page->index > end) {
512 _leave(" = 0 [%lx]", *_next);
516 /* at this point we hold neither mapping->tree_lock nor lock on
517 * the page itself: the page may be truncated or invalidated
518 * (changing page->mapping to NULL), or even swizzled back from
519 * swapper_space to tmpfs file mapping
523 if (page->mapping != mapping || !PageDirty(page)) {
529 if (PageWriteback(page)) {
531 if (wbc->sync_mode != WB_SYNC_NONE)
532 wait_on_page_writeback(page);
537 wb = (struct afs_writeback *) page_private(page);
540 spin_lock(&wb->vnode->writeback_lock);
541 wb->state = AFS_WBACK_WRITING;
542 spin_unlock(&wb->vnode->writeback_lock);
544 if (!clear_page_dirty_for_io(page))
546 ret = afs_write_back_from_locked_page(wb, page);
550 _leave(" = %d", ret);
554 wbc->nr_to_write -= ret;
557 } while (index < end && wbc->nr_to_write > 0);
560 _leave(" = 0 [%lx]", *_next);
565 * write some of the pending data back to the server
567 int afs_writepages(struct address_space *mapping,
568 struct writeback_control *wbc)
570 pgoff_t start, end, next;
575 if (wbc->range_cyclic) {
576 start = mapping->writeback_index;
578 ret = afs_writepages_region(mapping, wbc, start, end, &next);
579 if (start > 0 && wbc->nr_to_write > 0 && ret == 0)
580 ret = afs_writepages_region(mapping, wbc, 0, start,
582 mapping->writeback_index = next;
583 } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
584 end = (pgoff_t)(LLONG_MAX >> PAGE_SHIFT);
585 ret = afs_writepages_region(mapping, wbc, 0, end, &next);
586 if (wbc->nr_to_write > 0)
587 mapping->writeback_index = next;
589 start = wbc->range_start >> PAGE_SHIFT;
590 end = wbc->range_end >> PAGE_SHIFT;
591 ret = afs_writepages_region(mapping, wbc, start, end, &next);
594 _leave(" = %d", ret);
599 * completion of write to server
601 void afs_pages_written_back(struct afs_vnode *vnode, struct afs_call *call)
603 struct afs_writeback *wb = call->wb;
605 unsigned count, loop;
606 pgoff_t first = call->first, last = call->last;
609 _enter("{%x:%u},{%lx-%lx}",
610 vnode->fid.vid, vnode->fid.vnode, first, last);
614 pagevec_init(&pv, 0);
617 _debug("done %lx-%lx", first, last);
619 count = last - first + 1;
620 if (count > PAGEVEC_SIZE)
621 count = PAGEVEC_SIZE;
622 pv.nr = find_get_pages_contig(call->mapping, first, count,
624 ASSERTCMP(pv.nr, ==, count);
626 spin_lock(&vnode->writeback_lock);
627 for (loop = 0; loop < count; loop++) {
628 struct page *page = pv.pages[loop];
629 end_page_writeback(page);
630 if (page_private(page) == (unsigned long) wb) {
631 set_page_private(page, 0);
632 ClearPagePrivate(page);
637 if (wb->usage == 0) {
638 afs_unlink_writeback(wb);
641 spin_unlock(&vnode->writeback_lock);
644 afs_free_writeback(wb);
648 __pagevec_release(&pv);
649 } while (first <= last);
655 * write to an AFS file
657 ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from)
659 struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
661 size_t count = iov_iter_count(from);
663 _enter("{%x.%u},{%zu},",
664 vnode->fid.vid, vnode->fid.vnode, count);
666 if (IS_SWAPFILE(&vnode->vfs_inode)) {
668 "AFS: Attempt to write to active swap file!\n");
675 result = generic_file_write_iter(iocb, from);
677 _leave(" = %zd", result);
682 * flush the vnode to the fileserver
684 int afs_writeback_all(struct afs_vnode *vnode)
686 struct address_space *mapping = vnode->vfs_inode.i_mapping;
687 struct writeback_control wbc = {
688 .sync_mode = WB_SYNC_ALL,
689 .nr_to_write = LONG_MAX,
696 ret = mapping->a_ops->writepages(mapping, &wbc);
697 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
699 _leave(" = %d", ret);
704 * flush any dirty pages for this process, and check for write errors.
705 * - the return status from this call provides a reliable indication of
706 * whether any write errors occurred for this process.
708 int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
710 struct inode *inode = file_inode(file);
711 struct afs_writeback *wb, *xwb;
712 struct afs_vnode *vnode = AFS_FS_I(inode);
715 _enter("{%x:%u},{n=%pD},%d",
716 vnode->fid.vid, vnode->fid.vnode, file,
719 ret = file_write_and_wait_range(file, start, end);
724 /* use a writeback record as a marker in the queue - when this reaches
725 * the front of the queue, all the outstanding writes are either
726 * completed or rejected */
727 wb = kzalloc(sizeof(*wb), GFP_KERNEL);
735 wb->offset_first = 0;
736 wb->to_last = PAGE_SIZE;
738 wb->state = AFS_WBACK_SYNCING;
739 init_waitqueue_head(&wb->waitq);
741 spin_lock(&vnode->writeback_lock);
742 list_for_each_entry(xwb, &vnode->writebacks, link) {
743 if (xwb->state == AFS_WBACK_PENDING)
744 xwb->state = AFS_WBACK_CONFLICTING;
746 list_add_tail(&wb->link, &vnode->writebacks);
747 spin_unlock(&vnode->writeback_lock);
749 /* push all the outstanding writebacks to the server */
750 ret = afs_writeback_all(vnode);
752 afs_put_writeback(wb);
753 _leave(" = %d [wb]", ret);
757 /* wait for the preceding writes to actually complete */
758 ret = wait_event_interruptible(wb->waitq,
759 wb->state == AFS_WBACK_COMPLETE ||
760 vnode->writebacks.next == &wb->link);
761 afs_put_writeback(wb);
762 _leave(" = %d", ret);
769 * Flush out all outstanding writes on a file opened for writing when it is
772 int afs_flush(struct file *file, fl_owner_t id)
776 if ((file->f_mode & FMODE_WRITE) == 0)
779 return vfs_fsync(file, 0);
783 * notification that a previously read-only page is about to become writable
784 * - if it returns an error, the caller will deliver a bus error signal
786 int afs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
788 struct afs_vnode *vnode = AFS_FS_I(vma->vm_file->f_mapping->host);
790 _enter("{{%x:%u}},{%lx}",
791 vnode->fid.vid, vnode->fid.vnode, page->index);
793 /* wait for the page to be written to the cache before we allow it to
795 #ifdef CONFIG_AFS_FSCACHE
796 fscache_wait_on_page_write(vnode->cache, page);