1 /* handling of writes to regular files and writing back to the server
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
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.
12 #include <linux/backing-dev.h>
13 #include <linux/slab.h>
15 #include <linux/pagemap.h>
16 #include <linux/writeback.h>
17 #include <linux/pagevec.h>
21 * mark a page as having been made dirty and thus needing writeback
23 int afs_set_page_dirty(struct page *page)
26 return __set_page_dirty_nobuffers(page);
30 * partly or wholly fill a page that's under preparation for writing
32 static int afs_fill_page(struct afs_vnode *vnode, struct key *key,
33 loff_t pos, unsigned int len, struct page *page)
38 _enter(",,%llu", (unsigned long long)pos);
40 req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *),
45 atomic_set(&req->usage, 1);
52 ret = afs_fetch_data(vnode, key, req);
56 _debug("got NOENT from server"
57 " - marking file deleted and stale");
58 set_bit(AFS_VNODE_DELETED, &vnode->flags);
68 * prepare to perform part of a write to a page
70 int afs_write_begin(struct file *file, struct address_space *mapping,
71 loff_t pos, unsigned len, unsigned flags,
72 struct page **pagep, void **fsdata)
74 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
76 struct key *key = afs_file_key(file);
78 unsigned f, from = pos & (PAGE_SIZE - 1);
79 unsigned t, to = from + len;
80 pgoff_t index = pos >> PAGE_SHIFT;
83 _enter("{%x:%u},{%lx},%u,%u",
84 vnode->fid.vid, vnode->fid.vnode, index, from, to);
86 /* We want to store information about how much of a page is altered in
89 BUILD_BUG_ON(PAGE_SIZE > 32768 && sizeof(page->private) < 8);
91 page = grab_cache_page_write_begin(mapping, index, flags);
95 if (!PageUptodate(page) && len != PAGE_SIZE) {
96 ret = afs_fill_page(vnode, key, pos & PAGE_MASK, PAGE_SIZE, page);
100 _leave(" = %d [prep]", ret);
103 SetPageUptodate(page);
106 /* page won't leak in error case: it eventually gets cleaned off LRU */
110 /* See if this page is already partially written in a way that we can
111 * merge the new write with.
114 if (PagePrivate(page)) {
115 priv = page_private(page);
116 f = priv & AFS_PRIV_MAX;
117 t = priv >> AFS_PRIV_SHIFT;
122 if (PageWriteback(page)) {
123 trace_afs_page_dirty(vnode, tracepoint_string("alrdy"),
125 goto flush_conflicting_write;
127 if (to < f || from > t)
128 goto flush_conflicting_write;
138 priv = (unsigned long)t << AFS_PRIV_SHIFT;
140 trace_afs_page_dirty(vnode, tracepoint_string("begin"),
142 SetPagePrivate(page);
143 set_page_private(page, priv);
147 /* The previous write and this write aren't adjacent or overlapping, so
148 * flush the page out.
150 flush_conflicting_write:
151 _debug("flush conflict");
152 ret = write_one_page(page);
154 _leave(" = %d", ret);
158 ret = lock_page_killable(page);
160 _leave(" = %d", ret);
167 * finalise part of a write to a page
169 int afs_write_end(struct file *file, struct address_space *mapping,
170 loff_t pos, unsigned len, unsigned copied,
171 struct page *page, void *fsdata)
173 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
174 struct key *key = afs_file_key(file);
175 loff_t i_size, maybe_i_size;
178 _enter("{%x:%u},{%lx}",
179 vnode->fid.vid, vnode->fid.vnode, page->index);
181 maybe_i_size = pos + copied;
183 i_size = i_size_read(&vnode->vfs_inode);
184 if (maybe_i_size > i_size) {
185 spin_lock(&vnode->wb_lock);
186 i_size = i_size_read(&vnode->vfs_inode);
187 if (maybe_i_size > i_size)
188 i_size_write(&vnode->vfs_inode, maybe_i_size);
189 spin_unlock(&vnode->wb_lock);
192 if (!PageUptodate(page)) {
194 /* Try and load any missing data from the server. The
195 * unmarshalling routine will take care of clearing any
196 * bits that are beyond the EOF.
198 ret = afs_fill_page(vnode, key, pos + copied,
203 SetPageUptodate(page);
206 set_page_dirty(page);
216 * kill all the pages in the given range
218 static void afs_kill_pages(struct address_space *mapping,
219 pgoff_t first, pgoff_t last)
221 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
223 unsigned count, loop;
225 _enter("{%x:%u},%lx-%lx",
226 vnode->fid.vid, vnode->fid.vnode, first, last);
231 _debug("kill %lx-%lx", first, last);
233 count = last - first + 1;
234 if (count > PAGEVEC_SIZE)
235 count = PAGEVEC_SIZE;
236 pv.nr = find_get_pages_contig(mapping, first, count, pv.pages);
237 ASSERTCMP(pv.nr, ==, count);
239 for (loop = 0; loop < count; loop++) {
240 struct page *page = pv.pages[loop];
241 ClearPageUptodate(page);
243 end_page_writeback(page);
244 if (page->index >= first)
245 first = page->index + 1;
247 generic_error_remove_page(mapping, page);
250 __pagevec_release(&pv);
251 } while (first <= last);
257 * Redirty all the pages in a given range.
259 static void afs_redirty_pages(struct writeback_control *wbc,
260 struct address_space *mapping,
261 pgoff_t first, pgoff_t last)
263 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
265 unsigned count, loop;
267 _enter("{%x:%u},%lx-%lx",
268 vnode->fid.vid, vnode->fid.vnode, first, last);
273 _debug("redirty %lx-%lx", first, last);
275 count = last - first + 1;
276 if (count > PAGEVEC_SIZE)
277 count = PAGEVEC_SIZE;
278 pv.nr = find_get_pages_contig(mapping, first, count, pv.pages);
279 ASSERTCMP(pv.nr, ==, count);
281 for (loop = 0; loop < count; loop++) {
282 struct page *page = pv.pages[loop];
284 redirty_page_for_writepage(wbc, page);
285 end_page_writeback(page);
286 if (page->index >= first)
287 first = page->index + 1;
290 __pagevec_release(&pv);
291 } while (first <= last);
299 static int afs_store_data(struct address_space *mapping,
300 pgoff_t first, pgoff_t last,
301 unsigned offset, unsigned to)
303 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
304 struct afs_fs_cursor fc;
305 struct afs_wb_key *wbk = NULL;
307 int ret = -ENOKEY, ret2;
309 _enter("%s{%x:%u.%u},%lx,%lx,%x,%x",
314 first, last, offset, to);
316 spin_lock(&vnode->wb_lock);
317 p = vnode->wb_keys.next;
319 /* Iterate through the list looking for a valid key to use. */
321 while (p != &vnode->wb_keys) {
322 wbk = list_entry(p, struct afs_wb_key, vnode_link);
323 _debug("wbk %u", key_serial(wbk->key));
324 ret2 = key_validate(wbk->key);
332 spin_unlock(&vnode->wb_lock);
334 _leave(" = %d [no keys]", ret);
338 refcount_inc(&wbk->usage);
339 spin_unlock(&vnode->wb_lock);
341 _debug("USE WB KEY %u", key_serial(wbk->key));
344 if (afs_begin_vnode_operation(&fc, vnode, wbk->key)) {
345 while (afs_select_fileserver(&fc)) {
346 fc.cb_break = vnode->cb_break + vnode->cb_s_break;
347 afs_fs_store_data(&fc, mapping, first, last, offset, to);
350 afs_check_for_remote_deletion(&fc, fc.vnode);
351 afs_vnode_commit_status(&fc, vnode, fc.cb_break);
352 ret = afs_end_vnode_operation(&fc);
363 spin_lock(&vnode->wb_lock);
364 p = wbk->vnode_link.next;
370 _leave(" = %d", ret);
375 * Synchronously write back the locked page and any subsequent non-locked dirty
378 static int afs_write_back_from_locked_page(struct address_space *mapping,
379 struct writeback_control *wbc,
380 struct page *primary_page,
383 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
384 struct page *pages[8], *page;
385 unsigned long count, priv;
386 unsigned n, offset, to, f, t;
387 pgoff_t start, first, last;
390 _enter(",%lx", primary_page->index);
393 if (test_set_page_writeback(primary_page))
396 /* Find all consecutive lockable dirty pages that have contiguous
397 * written regions, stopping when we find a page that is not
398 * immediately lockable, is not dirty or is missing, or we reach the
401 start = primary_page->index;
402 priv = page_private(primary_page);
403 offset = priv & AFS_PRIV_MAX;
404 to = priv >> AFS_PRIV_SHIFT;
405 trace_afs_page_dirty(vnode, tracepoint_string("store"),
406 primary_page->index, priv);
408 WARN_ON(offset == to);
410 trace_afs_page_dirty(vnode, tracepoint_string("WARN"),
411 primary_page->index, priv);
413 if (start >= final_page || to < PAGE_SIZE)
418 _debug("more %lx [%lx]", start, count);
419 n = final_page - start + 1;
420 if (n > ARRAY_SIZE(pages))
421 n = ARRAY_SIZE(pages);
422 n = find_get_pages_contig(mapping, start, ARRAY_SIZE(pages), pages);
423 _debug("fgpc %u", n);
426 if (pages[0]->index != start) {
428 put_page(pages[--n]);
433 for (loop = 0; loop < n; loop++) {
437 if (page->index > final_page)
439 if (!trylock_page(page))
441 if (!PageDirty(page) || PageWriteback(page)) {
446 priv = page_private(page);
447 f = priv & AFS_PRIV_MAX;
448 t = priv >> AFS_PRIV_SHIFT;
455 trace_afs_page_dirty(vnode, tracepoint_string("store+"),
458 if (!clear_page_dirty_for_io(page))
460 if (test_set_page_writeback(page))
467 for (; loop < n; loop++)
468 put_page(pages[loop]);
473 } while (start <= final_page && count < 65536);
476 /* We now have a contiguous set of dirty pages, each with writeback
477 * set; the first page is still locked at this point, but all the rest
478 * have been unlocked.
480 unlock_page(primary_page);
482 first = primary_page->index;
483 last = first + count - 1;
485 _debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to);
487 ret = afs_store_data(mapping, first, last, offset, to);
494 pr_notice("kAFS: Unexpected error from FS.StoreData %d\n", ret);
502 afs_redirty_pages(wbc, mapping, first, last);
503 mapping_set_error(mapping, ret);
508 afs_redirty_pages(wbc, mapping, first, last);
509 mapping_set_error(mapping, -ENOSPC);
519 afs_kill_pages(mapping, first, last);
520 mapping_set_error(mapping, ret);
524 _leave(" = %d", ret);
529 * write a page back to the server
530 * - the caller locked the page for us
532 int afs_writepage(struct page *page, struct writeback_control *wbc)
536 _enter("{%lx},", page->index);
538 ret = afs_write_back_from_locked_page(page->mapping, wbc, page,
539 wbc->range_end >> PAGE_SHIFT);
541 _leave(" = %d", ret);
545 wbc->nr_to_write -= ret;
552 * write a region of pages back to the server
554 static int afs_writepages_region(struct address_space *mapping,
555 struct writeback_control *wbc,
556 pgoff_t index, pgoff_t end, pgoff_t *_next)
561 _enter(",,%lx,%lx,", index, end);
564 n = find_get_pages_range_tag(mapping, &index, end,
565 PAGECACHE_TAG_DIRTY, 1, &page);
569 _debug("wback %lx", page->index);
571 /* at this point we hold neither mapping->tree_lock nor lock on
572 * the page itself: the page may be truncated or invalidated
573 * (changing page->mapping to NULL), or even swizzled back from
574 * swapper_space to tmpfs file mapping
576 ret = lock_page_killable(page);
579 _leave(" = %d", ret);
583 if (page->mapping != mapping || !PageDirty(page)) {
589 if (PageWriteback(page)) {
591 if (wbc->sync_mode != WB_SYNC_NONE)
592 wait_on_page_writeback(page);
597 if (!clear_page_dirty_for_io(page))
599 ret = afs_write_back_from_locked_page(mapping, wbc, page, end);
602 _leave(" = %d", ret);
606 wbc->nr_to_write -= ret;
609 } while (index < end && wbc->nr_to_write > 0);
612 _leave(" = 0 [%lx]", *_next);
617 * write some of the pending data back to the server
619 int afs_writepages(struct address_space *mapping,
620 struct writeback_control *wbc)
622 pgoff_t start, end, next;
627 if (wbc->range_cyclic) {
628 start = mapping->writeback_index;
630 ret = afs_writepages_region(mapping, wbc, start, end, &next);
631 if (start > 0 && wbc->nr_to_write > 0 && ret == 0)
632 ret = afs_writepages_region(mapping, wbc, 0, start,
634 mapping->writeback_index = next;
635 } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
636 end = (pgoff_t)(LLONG_MAX >> PAGE_SHIFT);
637 ret = afs_writepages_region(mapping, wbc, 0, end, &next);
638 if (wbc->nr_to_write > 0)
639 mapping->writeback_index = next;
641 start = wbc->range_start >> PAGE_SHIFT;
642 end = wbc->range_end >> PAGE_SHIFT;
643 ret = afs_writepages_region(mapping, wbc, start, end, &next);
646 _leave(" = %d", ret);
651 * completion of write to server
653 void afs_pages_written_back(struct afs_vnode *vnode, struct afs_call *call)
657 unsigned count, loop;
658 pgoff_t first = call->first, last = call->last;
660 _enter("{%x:%u},{%lx-%lx}",
661 vnode->fid.vid, vnode->fid.vnode, first, last);
666 _debug("done %lx-%lx", first, last);
668 count = last - first + 1;
669 if (count > PAGEVEC_SIZE)
670 count = PAGEVEC_SIZE;
671 pv.nr = find_get_pages_contig(vnode->vfs_inode.i_mapping,
672 first, count, pv.pages);
673 ASSERTCMP(pv.nr, ==, count);
675 for (loop = 0; loop < count; loop++) {
676 priv = page_private(pv.pages[loop]);
677 trace_afs_page_dirty(vnode, tracepoint_string("clear"),
678 pv.pages[loop]->index, priv);
679 set_page_private(pv.pages[loop], 0);
680 end_page_writeback(pv.pages[loop]);
683 __pagevec_release(&pv);
684 } while (first <= last);
686 afs_prune_wb_keys(vnode);
691 * write to an AFS file
693 ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from)
695 struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
697 size_t count = iov_iter_count(from);
699 _enter("{%x.%u},{%zu},",
700 vnode->fid.vid, vnode->fid.vnode, count);
702 if (IS_SWAPFILE(&vnode->vfs_inode)) {
704 "AFS: Attempt to write to active swap file!\n");
711 result = generic_file_write_iter(iocb, from);
713 _leave(" = %zd", result);
718 * flush any dirty pages for this process, and check for write errors.
719 * - the return status from this call provides a reliable indication of
720 * whether any write errors occurred for this process.
722 int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
724 struct inode *inode = file_inode(file);
725 struct afs_vnode *vnode = AFS_FS_I(inode);
727 _enter("{%x:%u},{n=%pD},%d",
728 vnode->fid.vid, vnode->fid.vnode, file,
731 return file_write_and_wait_range(file, start, end);
735 * Flush out all outstanding writes on a file opened for writing when it is
738 int afs_flush(struct file *file, fl_owner_t id)
742 if ((file->f_mode & FMODE_WRITE) == 0)
745 return vfs_fsync(file, 0);
749 * notification that a previously read-only page is about to become writable
750 * - if it returns an error, the caller will deliver a bus error signal
752 int afs_page_mkwrite(struct vm_fault *vmf)
754 struct file *file = vmf->vma->vm_file;
755 struct inode *inode = file_inode(file);
756 struct afs_vnode *vnode = AFS_FS_I(inode);
759 _enter("{{%x:%u}},{%lx}",
760 vnode->fid.vid, vnode->fid.vnode, vmf->page->index);
762 sb_start_pagefault(inode->i_sb);
764 /* Wait for the page to be written to the cache before we allow it to
765 * be modified. We then assume the entire page will need writing back.
767 #ifdef CONFIG_AFS_FSCACHE
768 fscache_wait_on_page_write(vnode->cache, vmf->page);
771 if (PageWriteback(vmf->page) &&
772 wait_on_page_bit_killable(vmf->page, PG_writeback) < 0)
773 return VM_FAULT_RETRY;
775 if (lock_page_killable(vmf->page) < 0)
776 return VM_FAULT_RETRY;
778 /* We mustn't change page->private until writeback is complete as that
779 * details the portion of the page we need to write back and we might
780 * need to redirty the page if there's a problem.
782 wait_on_page_writeback(vmf->page);
784 priv = (unsigned long)PAGE_SIZE << AFS_PRIV_SHIFT; /* To */
785 priv |= 0; /* From */
786 trace_afs_page_dirty(vnode, tracepoint_string("mkwrite"),
787 vmf->page->index, priv);
788 SetPagePrivate(vmf->page);
789 set_page_private(vmf->page, priv);
791 sb_end_pagefault(inode->i_sb);
792 return VM_FAULT_LOCKED;
796 * Prune the keys cached for writeback. The caller must hold vnode->wb_lock.
798 void afs_prune_wb_keys(struct afs_vnode *vnode)
800 LIST_HEAD(graveyard);
801 struct afs_wb_key *wbk, *tmp;
803 /* Discard unused keys */
804 spin_lock(&vnode->wb_lock);
806 if (!mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_WRITEBACK) &&
807 !mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_DIRTY)) {
808 list_for_each_entry_safe(wbk, tmp, &vnode->wb_keys, vnode_link) {
809 if (refcount_read(&wbk->usage) == 1)
810 list_move(&wbk->vnode_link, &graveyard);
814 spin_unlock(&vnode->wb_lock);
816 while (!list_empty(&graveyard)) {
817 wbk = list_entry(graveyard.next, struct afs_wb_key, vnode_link);
818 list_del(&wbk->vnode_link);
824 * Clean up a page during invalidation.
826 int afs_launder_page(struct page *page)
828 struct address_space *mapping = page->mapping;
829 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
834 _enter("{%lx}", page->index);
836 priv = page_private(page);
837 if (clear_page_dirty_for_io(page)) {
840 if (PagePrivate(page)) {
841 f = priv & AFS_PRIV_MAX;
842 t = priv >> AFS_PRIV_SHIFT;
845 trace_afs_page_dirty(vnode, tracepoint_string("launder"),
847 ret = afs_store_data(mapping, page->index, page->index, t, f);
850 trace_afs_page_dirty(vnode, tracepoint_string("laundered"),
852 set_page_private(page, 0);
853 ClearPagePrivate(page);
855 #ifdef CONFIG_AFS_FSCACHE
856 if (PageFsCache(page)) {
857 fscache_wait_on_page_write(vnode->cache, page);
858 fscache_uncache_page(vnode->cache, page);