131143d5dSDavid Howells /* handling of writes to regular files and writing back to the server 231143d5dSDavid Howells * 331143d5dSDavid Howells * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. 431143d5dSDavid Howells * Written by David Howells (dhowells@redhat.com) 531143d5dSDavid Howells * 631143d5dSDavid Howells * This program is free software; you can redistribute it and/or 731143d5dSDavid Howells * modify it under the terms of the GNU General Public License 831143d5dSDavid Howells * as published by the Free Software Foundation; either version 931143d5dSDavid Howells * 2 of the License, or (at your option) any later version. 1031143d5dSDavid Howells */ 114af3c9ccSAlexey Dobriyan #include <linux/backing-dev.h> 1231143d5dSDavid Howells #include <linux/slab.h> 1331143d5dSDavid Howells #include <linux/fs.h> 1431143d5dSDavid Howells #include <linux/pagemap.h> 1531143d5dSDavid Howells #include <linux/writeback.h> 1631143d5dSDavid Howells #include <linux/pagevec.h> 1731143d5dSDavid Howells #include "internal.h" 1831143d5dSDavid Howells 1931143d5dSDavid Howells static int afs_write_back_from_locked_page(struct afs_writeback *wb, 2031143d5dSDavid Howells struct page *page); 2131143d5dSDavid Howells 2231143d5dSDavid Howells /* 2331143d5dSDavid Howells * mark a page as having been made dirty and thus needing writeback 2431143d5dSDavid Howells */ 2531143d5dSDavid Howells int afs_set_page_dirty(struct page *page) 2631143d5dSDavid Howells { 2731143d5dSDavid Howells _enter(""); 2831143d5dSDavid Howells return __set_page_dirty_nobuffers(page); 2931143d5dSDavid Howells } 3031143d5dSDavid Howells 3131143d5dSDavid Howells /* 3231143d5dSDavid Howells * unlink a writeback record because its usage has reached zero 3331143d5dSDavid Howells * - must be called with the wb->vnode->writeback_lock held 3431143d5dSDavid Howells */ 3531143d5dSDavid Howells static void afs_unlink_writeback(struct afs_writeback *wb) 3631143d5dSDavid Howells { 3731143d5dSDavid Howells struct afs_writeback *front; 3831143d5dSDavid Howells struct afs_vnode *vnode = wb->vnode; 3931143d5dSDavid Howells 4031143d5dSDavid Howells list_del_init(&wb->link); 4131143d5dSDavid Howells if (!list_empty(&vnode->writebacks)) { 4231143d5dSDavid Howells /* if an fsync rises to the front of the queue then wake it 4331143d5dSDavid Howells * up */ 4431143d5dSDavid Howells front = list_entry(vnode->writebacks.next, 4531143d5dSDavid Howells struct afs_writeback, link); 4631143d5dSDavid Howells if (front->state == AFS_WBACK_SYNCING) { 4731143d5dSDavid Howells _debug("wake up sync"); 4831143d5dSDavid Howells front->state = AFS_WBACK_COMPLETE; 4931143d5dSDavid Howells wake_up(&front->waitq); 5031143d5dSDavid Howells } 5131143d5dSDavid Howells } 5231143d5dSDavid Howells } 5331143d5dSDavid Howells 5431143d5dSDavid Howells /* 5531143d5dSDavid Howells * free a writeback record 5631143d5dSDavid Howells */ 5731143d5dSDavid Howells static void afs_free_writeback(struct afs_writeback *wb) 5831143d5dSDavid Howells { 5931143d5dSDavid Howells _enter(""); 6031143d5dSDavid Howells key_put(wb->key); 6131143d5dSDavid Howells kfree(wb); 6231143d5dSDavid Howells } 6331143d5dSDavid Howells 6431143d5dSDavid Howells /* 6531143d5dSDavid Howells * dispose of a reference to a writeback record 6631143d5dSDavid Howells */ 6731143d5dSDavid Howells void afs_put_writeback(struct afs_writeback *wb) 6831143d5dSDavid Howells { 6931143d5dSDavid Howells struct afs_vnode *vnode = wb->vnode; 7031143d5dSDavid Howells 7131143d5dSDavid Howells _enter("{%d}", wb->usage); 7231143d5dSDavid Howells 7331143d5dSDavid Howells spin_lock(&vnode->writeback_lock); 7431143d5dSDavid Howells if (--wb->usage == 0) 7531143d5dSDavid Howells afs_unlink_writeback(wb); 7631143d5dSDavid Howells else 7731143d5dSDavid Howells wb = NULL; 7831143d5dSDavid Howells spin_unlock(&vnode->writeback_lock); 7931143d5dSDavid Howells if (wb) 8031143d5dSDavid Howells afs_free_writeback(wb); 8131143d5dSDavid Howells } 8231143d5dSDavid Howells 8331143d5dSDavid Howells /* 8431143d5dSDavid Howells * partly or wholly fill a page that's under preparation for writing 8531143d5dSDavid Howells */ 8631143d5dSDavid Howells static int afs_fill_page(struct afs_vnode *vnode, struct key *key, 875e7f2337SAnton Blanchard loff_t pos, struct page *page) 8831143d5dSDavid Howells { 89196ee9cdSDavid Howells struct afs_read *req; 9015b4650eSNick Piggin loff_t i_size; 9131143d5dSDavid Howells int ret; 9231143d5dSDavid Howells 935e7f2337SAnton Blanchard _enter(",,%llu", (unsigned long long)pos); 9431143d5dSDavid Howells 95196ee9cdSDavid Howells req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *), 96196ee9cdSDavid Howells GFP_KERNEL); 97196ee9cdSDavid Howells if (!req) 98196ee9cdSDavid Howells return -ENOMEM; 99196ee9cdSDavid Howells 100196ee9cdSDavid Howells atomic_set(&req->usage, 1); 101196ee9cdSDavid Howells req->pos = pos; 102196ee9cdSDavid Howells req->nr_pages = 1; 103196ee9cdSDavid Howells req->pages[0] = page; 1045611ef28SDavid Howells get_page(page); 105196ee9cdSDavid Howells 10615b4650eSNick Piggin i_size = i_size_read(&vnode->vfs_inode); 10709cbfeafSKirill A. Shutemov if (pos + PAGE_SIZE > i_size) 108196ee9cdSDavid Howells req->len = i_size - pos; 10915b4650eSNick Piggin else 110196ee9cdSDavid Howells req->len = PAGE_SIZE; 11115b4650eSNick Piggin 112196ee9cdSDavid Howells ret = afs_vnode_fetch_data(vnode, key, req); 113196ee9cdSDavid Howells afs_put_read(req); 11431143d5dSDavid Howells if (ret < 0) { 11531143d5dSDavid Howells if (ret == -ENOENT) { 11631143d5dSDavid Howells _debug("got NOENT from server" 11731143d5dSDavid Howells " - marking file deleted and stale"); 11831143d5dSDavid Howells set_bit(AFS_VNODE_DELETED, &vnode->flags); 11931143d5dSDavid Howells ret = -ESTALE; 12031143d5dSDavid Howells } 12131143d5dSDavid Howells } 12231143d5dSDavid Howells 12331143d5dSDavid Howells _leave(" = %d", ret); 12431143d5dSDavid Howells return ret; 12531143d5dSDavid Howells } 12631143d5dSDavid Howells 12731143d5dSDavid Howells /* 12831143d5dSDavid Howells * prepare to perform part of a write to a page 12931143d5dSDavid Howells */ 13015b4650eSNick Piggin int afs_write_begin(struct file *file, struct address_space *mapping, 13115b4650eSNick Piggin loff_t pos, unsigned len, unsigned flags, 13215b4650eSNick Piggin struct page **pagep, void **fsdata) 13331143d5dSDavid Howells { 13431143d5dSDavid Howells struct afs_writeback *candidate, *wb; 135496ad9aaSAl Viro struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 13615b4650eSNick Piggin struct page *page; 13731143d5dSDavid Howells struct key *key = file->private_data; 13809cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 13915b4650eSNick Piggin unsigned to = from + len; 14009cbfeafSKirill A. Shutemov pgoff_t index = pos >> PAGE_SHIFT; 14131143d5dSDavid Howells int ret; 14231143d5dSDavid Howells 14331143d5dSDavid Howells _enter("{%x:%u},{%lx},%u,%u", 14415b4650eSNick Piggin vnode->fid.vid, vnode->fid.vnode, index, from, to); 14531143d5dSDavid Howells 14631143d5dSDavid Howells candidate = kzalloc(sizeof(*candidate), GFP_KERNEL); 14731143d5dSDavid Howells if (!candidate) 14831143d5dSDavid Howells return -ENOMEM; 14931143d5dSDavid Howells candidate->vnode = vnode; 15015b4650eSNick Piggin candidate->first = candidate->last = index; 15115b4650eSNick Piggin candidate->offset_first = from; 15231143d5dSDavid Howells candidate->to_last = to; 153f129ccc9SAnton Blanchard INIT_LIST_HEAD(&candidate->link); 15431143d5dSDavid Howells candidate->usage = 1; 15531143d5dSDavid Howells candidate->state = AFS_WBACK_PENDING; 15631143d5dSDavid Howells init_waitqueue_head(&candidate->waitq); 15731143d5dSDavid Howells 15854566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 15915b4650eSNick Piggin if (!page) { 16015b4650eSNick Piggin kfree(candidate); 16115b4650eSNick Piggin return -ENOMEM; 16215b4650eSNick Piggin } 16315b4650eSNick Piggin *pagep = page; 16415b4650eSNick Piggin /* page won't leak in error case: it eventually gets cleaned off LRU */ 16515b4650eSNick Piggin 16609cbfeafSKirill A. Shutemov if (!PageUptodate(page) && len != PAGE_SIZE) { 16709cbfeafSKirill A. Shutemov ret = afs_fill_page(vnode, key, index << PAGE_SHIFT, page); 16831143d5dSDavid Howells if (ret < 0) { 16931143d5dSDavid Howells kfree(candidate); 17031143d5dSDavid Howells _leave(" = %d [prep]", ret); 17131143d5dSDavid Howells return ret; 17231143d5dSDavid Howells } 17315b4650eSNick Piggin SetPageUptodate(page); 17431143d5dSDavid Howells } 17531143d5dSDavid Howells 17631143d5dSDavid Howells try_again: 17731143d5dSDavid Howells spin_lock(&vnode->writeback_lock); 17831143d5dSDavid Howells 17931143d5dSDavid Howells /* see if this page is already pending a writeback under a suitable key 18031143d5dSDavid Howells * - if so we can just join onto that one */ 18131143d5dSDavid Howells wb = (struct afs_writeback *) page_private(page); 18231143d5dSDavid Howells if (wb) { 18331143d5dSDavid Howells if (wb->key == key && wb->state == AFS_WBACK_PENDING) 18431143d5dSDavid Howells goto subsume_in_current_wb; 18531143d5dSDavid Howells goto flush_conflicting_wb; 18631143d5dSDavid Howells } 18731143d5dSDavid Howells 18831143d5dSDavid Howells if (index > 0) { 18931143d5dSDavid Howells /* see if we can find an already pending writeback that we can 19031143d5dSDavid Howells * append this page to */ 19131143d5dSDavid Howells list_for_each_entry(wb, &vnode->writebacks, link) { 19231143d5dSDavid Howells if (wb->last == index - 1 && wb->key == key && 19331143d5dSDavid Howells wb->state == AFS_WBACK_PENDING) 19431143d5dSDavid Howells goto append_to_previous_wb; 19531143d5dSDavid Howells } 19631143d5dSDavid Howells } 19731143d5dSDavid Howells 19831143d5dSDavid Howells list_add_tail(&candidate->link, &vnode->writebacks); 19931143d5dSDavid Howells candidate->key = key_get(key); 20031143d5dSDavid Howells spin_unlock(&vnode->writeback_lock); 20131143d5dSDavid Howells SetPagePrivate(page); 20231143d5dSDavid Howells set_page_private(page, (unsigned long) candidate); 20331143d5dSDavid Howells _leave(" = 0 [new]"); 20431143d5dSDavid Howells return 0; 20531143d5dSDavid Howells 20631143d5dSDavid Howells subsume_in_current_wb: 20731143d5dSDavid Howells _debug("subsume"); 20831143d5dSDavid Howells ASSERTRANGE(wb->first, <=, index, <=, wb->last); 20915b4650eSNick Piggin if (index == wb->first && from < wb->offset_first) 21015b4650eSNick Piggin wb->offset_first = from; 21131143d5dSDavid Howells if (index == wb->last && to > wb->to_last) 21231143d5dSDavid Howells wb->to_last = to; 21331143d5dSDavid Howells spin_unlock(&vnode->writeback_lock); 21431143d5dSDavid Howells kfree(candidate); 21531143d5dSDavid Howells _leave(" = 0 [sub]"); 21631143d5dSDavid Howells return 0; 21731143d5dSDavid Howells 21831143d5dSDavid Howells append_to_previous_wb: 21931143d5dSDavid Howells _debug("append into %lx-%lx", wb->first, wb->last); 22031143d5dSDavid Howells wb->usage++; 22131143d5dSDavid Howells wb->last++; 22231143d5dSDavid Howells wb->to_last = to; 22331143d5dSDavid Howells spin_unlock(&vnode->writeback_lock); 22431143d5dSDavid Howells SetPagePrivate(page); 22531143d5dSDavid Howells set_page_private(page, (unsigned long) wb); 22631143d5dSDavid Howells kfree(candidate); 22731143d5dSDavid Howells _leave(" = 0 [app]"); 22831143d5dSDavid Howells return 0; 22931143d5dSDavid Howells 23031143d5dSDavid Howells /* the page is currently bound to another context, so if it's dirty we 23131143d5dSDavid Howells * need to flush it before we can use the new context */ 23231143d5dSDavid Howells flush_conflicting_wb: 23331143d5dSDavid Howells _debug("flush conflict"); 23431143d5dSDavid Howells if (wb->state == AFS_WBACK_PENDING) 23531143d5dSDavid Howells wb->state = AFS_WBACK_CONFLICTING; 23631143d5dSDavid Howells spin_unlock(&vnode->writeback_lock); 23731143d5dSDavid Howells if (PageDirty(page)) { 23831143d5dSDavid Howells ret = afs_write_back_from_locked_page(wb, page); 23931143d5dSDavid Howells if (ret < 0) { 24031143d5dSDavid Howells afs_put_writeback(candidate); 24131143d5dSDavid Howells _leave(" = %d", ret); 24231143d5dSDavid Howells return ret; 24331143d5dSDavid Howells } 24431143d5dSDavid Howells } 24531143d5dSDavid Howells 24631143d5dSDavid Howells /* the page holds a ref on the writeback record */ 24731143d5dSDavid Howells afs_put_writeback(wb); 24831143d5dSDavid Howells set_page_private(page, 0); 24931143d5dSDavid Howells ClearPagePrivate(page); 25031143d5dSDavid Howells goto try_again; 25131143d5dSDavid Howells } 25231143d5dSDavid Howells 25331143d5dSDavid Howells /* 25431143d5dSDavid Howells * finalise part of a write to a page 25531143d5dSDavid Howells */ 25615b4650eSNick Piggin int afs_write_end(struct file *file, struct address_space *mapping, 25715b4650eSNick Piggin loff_t pos, unsigned len, unsigned copied, 25815b4650eSNick Piggin struct page *page, void *fsdata) 25931143d5dSDavid Howells { 260496ad9aaSAl Viro struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 26131143d5dSDavid Howells loff_t i_size, maybe_i_size; 26231143d5dSDavid Howells 26315b4650eSNick Piggin _enter("{%x:%u},{%lx}", 26415b4650eSNick Piggin vnode->fid.vid, vnode->fid.vnode, page->index); 26531143d5dSDavid Howells 26615b4650eSNick Piggin maybe_i_size = pos + copied; 26731143d5dSDavid Howells 26831143d5dSDavid Howells i_size = i_size_read(&vnode->vfs_inode); 26931143d5dSDavid Howells if (maybe_i_size > i_size) { 27031143d5dSDavid Howells spin_lock(&vnode->writeback_lock); 27131143d5dSDavid Howells i_size = i_size_read(&vnode->vfs_inode); 27231143d5dSDavid Howells if (maybe_i_size > i_size) 27331143d5dSDavid Howells i_size_write(&vnode->vfs_inode, maybe_i_size); 27431143d5dSDavid Howells spin_unlock(&vnode->writeback_lock); 27531143d5dSDavid Howells } 27631143d5dSDavid Howells 27731143d5dSDavid Howells set_page_dirty(page); 27831143d5dSDavid Howells if (PageDirty(page)) 27931143d5dSDavid Howells _debug("dirtied"); 28015b4650eSNick Piggin unlock_page(page); 28109cbfeafSKirill A. Shutemov put_page(page); 28231143d5dSDavid Howells 28315b4650eSNick Piggin return copied; 28431143d5dSDavid Howells } 28531143d5dSDavid Howells 28631143d5dSDavid Howells /* 28731143d5dSDavid Howells * kill all the pages in the given range 28831143d5dSDavid Howells */ 28931143d5dSDavid Howells static void afs_kill_pages(struct afs_vnode *vnode, bool error, 29031143d5dSDavid Howells pgoff_t first, pgoff_t last) 29131143d5dSDavid Howells { 29231143d5dSDavid Howells struct pagevec pv; 29331143d5dSDavid Howells unsigned count, loop; 29431143d5dSDavid Howells 29531143d5dSDavid Howells _enter("{%x:%u},%lx-%lx", 29631143d5dSDavid Howells vnode->fid.vid, vnode->fid.vnode, first, last); 29731143d5dSDavid Howells 29831143d5dSDavid Howells pagevec_init(&pv, 0); 29931143d5dSDavid Howells 30031143d5dSDavid Howells do { 30131143d5dSDavid Howells _debug("kill %lx-%lx", first, last); 30231143d5dSDavid Howells 30331143d5dSDavid Howells count = last - first + 1; 30431143d5dSDavid Howells if (count > PAGEVEC_SIZE) 30531143d5dSDavid Howells count = PAGEVEC_SIZE; 30631143d5dSDavid Howells pv.nr = find_get_pages_contig(vnode->vfs_inode.i_mapping, 30731143d5dSDavid Howells first, count, pv.pages); 30831143d5dSDavid Howells ASSERTCMP(pv.nr, ==, count); 30931143d5dSDavid Howells 31031143d5dSDavid Howells for (loop = 0; loop < count; loop++) { 31131143d5dSDavid Howells ClearPageUptodate(pv.pages[loop]); 31231143d5dSDavid Howells if (error) 31331143d5dSDavid Howells SetPageError(pv.pages[loop]); 31431143d5dSDavid Howells end_page_writeback(pv.pages[loop]); 31531143d5dSDavid Howells } 31631143d5dSDavid Howells 31731143d5dSDavid Howells __pagevec_release(&pv); 31831143d5dSDavid Howells } while (first < last); 31931143d5dSDavid Howells 32031143d5dSDavid Howells _leave(""); 32131143d5dSDavid Howells } 32231143d5dSDavid Howells 32331143d5dSDavid Howells /* 32431143d5dSDavid Howells * synchronously write back the locked page and any subsequent non-locked dirty 32531143d5dSDavid Howells * pages also covered by the same writeback record 32631143d5dSDavid Howells */ 32731143d5dSDavid Howells static int afs_write_back_from_locked_page(struct afs_writeback *wb, 32831143d5dSDavid Howells struct page *primary_page) 32931143d5dSDavid Howells { 33031143d5dSDavid Howells struct page *pages[8], *page; 33131143d5dSDavid Howells unsigned long count; 33231143d5dSDavid Howells unsigned n, offset, to; 33331143d5dSDavid Howells pgoff_t start, first, last; 33431143d5dSDavid Howells int loop, ret; 33531143d5dSDavid Howells 33631143d5dSDavid Howells _enter(",%lx", primary_page->index); 33731143d5dSDavid Howells 33831143d5dSDavid Howells count = 1; 33931143d5dSDavid Howells if (!clear_page_dirty_for_io(primary_page)) 34031143d5dSDavid Howells BUG(); 34131143d5dSDavid Howells if (test_set_page_writeback(primary_page)) 34231143d5dSDavid Howells BUG(); 34331143d5dSDavid Howells 34431143d5dSDavid Howells /* find all consecutive lockable dirty pages, stopping when we find a 34531143d5dSDavid Howells * page that is not immediately lockable, is not dirty or is missing, 34631143d5dSDavid Howells * or we reach the end of the range */ 34731143d5dSDavid Howells start = primary_page->index; 34831143d5dSDavid Howells if (start >= wb->last) 34931143d5dSDavid Howells goto no_more; 35031143d5dSDavid Howells start++; 35131143d5dSDavid Howells do { 35231143d5dSDavid Howells _debug("more %lx [%lx]", start, count); 35331143d5dSDavid Howells n = wb->last - start + 1; 35431143d5dSDavid Howells if (n > ARRAY_SIZE(pages)) 35531143d5dSDavid Howells n = ARRAY_SIZE(pages); 35631143d5dSDavid Howells n = find_get_pages_contig(wb->vnode->vfs_inode.i_mapping, 35731143d5dSDavid Howells start, n, pages); 35831143d5dSDavid Howells _debug("fgpc %u", n); 35931143d5dSDavid Howells if (n == 0) 36031143d5dSDavid Howells goto no_more; 36131143d5dSDavid Howells if (pages[0]->index != start) { 3629d577b6aSDavid Howells do { 3639d577b6aSDavid Howells put_page(pages[--n]); 3649d577b6aSDavid Howells } while (n > 0); 36531143d5dSDavid Howells goto no_more; 36631143d5dSDavid Howells } 36731143d5dSDavid Howells 36831143d5dSDavid Howells for (loop = 0; loop < n; loop++) { 36931143d5dSDavid Howells page = pages[loop]; 37031143d5dSDavid Howells if (page->index > wb->last) 37131143d5dSDavid Howells break; 372529ae9aaSNick Piggin if (!trylock_page(page)) 37331143d5dSDavid Howells break; 37431143d5dSDavid Howells if (!PageDirty(page) || 37531143d5dSDavid Howells page_private(page) != (unsigned long) wb) { 37631143d5dSDavid Howells unlock_page(page); 37731143d5dSDavid Howells break; 37831143d5dSDavid Howells } 37931143d5dSDavid Howells if (!clear_page_dirty_for_io(page)) 38031143d5dSDavid Howells BUG(); 38131143d5dSDavid Howells if (test_set_page_writeback(page)) 38231143d5dSDavid Howells BUG(); 38331143d5dSDavid Howells unlock_page(page); 38431143d5dSDavid Howells put_page(page); 38531143d5dSDavid Howells } 38631143d5dSDavid Howells count += loop; 38731143d5dSDavid Howells if (loop < n) { 38831143d5dSDavid Howells for (; loop < n; loop++) 38931143d5dSDavid Howells put_page(pages[loop]); 39031143d5dSDavid Howells goto no_more; 39131143d5dSDavid Howells } 39231143d5dSDavid Howells 39331143d5dSDavid Howells start += loop; 39431143d5dSDavid Howells } while (start <= wb->last && count < 65536); 39531143d5dSDavid Howells 39631143d5dSDavid Howells no_more: 39731143d5dSDavid Howells /* we now have a contiguous set of dirty pages, each with writeback set 39831143d5dSDavid Howells * and the dirty mark cleared; the first page is locked and must remain 39931143d5dSDavid Howells * so, all the rest are unlocked */ 40031143d5dSDavid Howells first = primary_page->index; 40131143d5dSDavid Howells last = first + count - 1; 40231143d5dSDavid Howells 40331143d5dSDavid Howells offset = (first == wb->first) ? wb->offset_first : 0; 40431143d5dSDavid Howells to = (last == wb->last) ? wb->to_last : PAGE_SIZE; 40531143d5dSDavid Howells 40631143d5dSDavid Howells _debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to); 40731143d5dSDavid Howells 40831143d5dSDavid Howells ret = afs_vnode_store_data(wb, first, last, offset, to); 40931143d5dSDavid Howells if (ret < 0) { 41031143d5dSDavid Howells switch (ret) { 41131143d5dSDavid Howells case -EDQUOT: 41231143d5dSDavid Howells case -ENOSPC: 4135114a97aSMichal Hocko mapping_set_error(wb->vnode->vfs_inode.i_mapping, -ENOSPC); 41431143d5dSDavid Howells break; 41531143d5dSDavid Howells case -EROFS: 41631143d5dSDavid Howells case -EIO: 41731143d5dSDavid Howells case -EREMOTEIO: 41831143d5dSDavid Howells case -EFBIG: 41931143d5dSDavid Howells case -ENOENT: 42031143d5dSDavid Howells case -ENOMEDIUM: 42131143d5dSDavid Howells case -ENXIO: 42231143d5dSDavid Howells afs_kill_pages(wb->vnode, true, first, last); 4235114a97aSMichal Hocko mapping_set_error(wb->vnode->vfs_inode.i_mapping, -EIO); 42431143d5dSDavid Howells break; 42531143d5dSDavid Howells case -EACCES: 42631143d5dSDavid Howells case -EPERM: 42731143d5dSDavid Howells case -ENOKEY: 42831143d5dSDavid Howells case -EKEYEXPIRED: 42931143d5dSDavid Howells case -EKEYREJECTED: 43031143d5dSDavid Howells case -EKEYREVOKED: 43131143d5dSDavid Howells afs_kill_pages(wb->vnode, false, first, last); 43231143d5dSDavid Howells break; 43331143d5dSDavid Howells default: 43431143d5dSDavid Howells break; 43531143d5dSDavid Howells } 43631143d5dSDavid Howells } else { 43731143d5dSDavid Howells ret = count; 43831143d5dSDavid Howells } 43931143d5dSDavid Howells 44031143d5dSDavid Howells _leave(" = %d", ret); 44131143d5dSDavid Howells return ret; 44231143d5dSDavid Howells } 44331143d5dSDavid Howells 44431143d5dSDavid Howells /* 44531143d5dSDavid Howells * write a page back to the server 44631143d5dSDavid Howells * - the caller locked the page for us 44731143d5dSDavid Howells */ 44831143d5dSDavid Howells int afs_writepage(struct page *page, struct writeback_control *wbc) 44931143d5dSDavid Howells { 45031143d5dSDavid Howells struct afs_writeback *wb; 45131143d5dSDavid Howells int ret; 45231143d5dSDavid Howells 45331143d5dSDavid Howells _enter("{%lx},", page->index); 45431143d5dSDavid Howells 45531143d5dSDavid Howells wb = (struct afs_writeback *) page_private(page); 45631143d5dSDavid Howells ASSERT(wb != NULL); 45731143d5dSDavid Howells 45831143d5dSDavid Howells ret = afs_write_back_from_locked_page(wb, page); 45931143d5dSDavid Howells unlock_page(page); 46031143d5dSDavid Howells if (ret < 0) { 46131143d5dSDavid Howells _leave(" = %d", ret); 46231143d5dSDavid Howells return 0; 46331143d5dSDavid Howells } 46431143d5dSDavid Howells 46531143d5dSDavid Howells wbc->nr_to_write -= ret; 46631143d5dSDavid Howells 46731143d5dSDavid Howells _leave(" = 0"); 46831143d5dSDavid Howells return 0; 46931143d5dSDavid Howells } 47031143d5dSDavid Howells 47131143d5dSDavid Howells /* 47231143d5dSDavid Howells * write a region of pages back to the server 47331143d5dSDavid Howells */ 474c1206a2cSAdrian Bunk static int afs_writepages_region(struct address_space *mapping, 47531143d5dSDavid Howells struct writeback_control *wbc, 47631143d5dSDavid Howells pgoff_t index, pgoff_t end, pgoff_t *_next) 47731143d5dSDavid Howells { 47831143d5dSDavid Howells struct afs_writeback *wb; 47931143d5dSDavid Howells struct page *page; 48031143d5dSDavid Howells int ret, n; 48131143d5dSDavid Howells 48231143d5dSDavid Howells _enter(",,%lx,%lx,", index, end); 48331143d5dSDavid Howells 48431143d5dSDavid Howells do { 48531143d5dSDavid Howells n = find_get_pages_tag(mapping, &index, PAGECACHE_TAG_DIRTY, 48631143d5dSDavid Howells 1, &page); 48731143d5dSDavid Howells if (!n) 48831143d5dSDavid Howells break; 48931143d5dSDavid Howells 49031143d5dSDavid Howells _debug("wback %lx", page->index); 49131143d5dSDavid Howells 49231143d5dSDavid Howells if (page->index > end) { 49331143d5dSDavid Howells *_next = index; 49409cbfeafSKirill A. Shutemov put_page(page); 49531143d5dSDavid Howells _leave(" = 0 [%lx]", *_next); 49631143d5dSDavid Howells return 0; 49731143d5dSDavid Howells } 49831143d5dSDavid Howells 49931143d5dSDavid Howells /* at this point we hold neither mapping->tree_lock nor lock on 50031143d5dSDavid Howells * the page itself: the page may be truncated or invalidated 50131143d5dSDavid Howells * (changing page->mapping to NULL), or even swizzled back from 50231143d5dSDavid Howells * swapper_space to tmpfs file mapping 50331143d5dSDavid Howells */ 50431143d5dSDavid Howells lock_page(page); 50531143d5dSDavid Howells 50631143d5dSDavid Howells if (page->mapping != mapping) { 50731143d5dSDavid Howells unlock_page(page); 50809cbfeafSKirill A. Shutemov put_page(page); 50931143d5dSDavid Howells continue; 51031143d5dSDavid Howells } 51131143d5dSDavid Howells 51231143d5dSDavid Howells if (wbc->sync_mode != WB_SYNC_NONE) 51331143d5dSDavid Howells wait_on_page_writeback(page); 51431143d5dSDavid Howells 51531143d5dSDavid Howells if (PageWriteback(page) || !PageDirty(page)) { 51631143d5dSDavid Howells unlock_page(page); 51729c8bbbdSDavid Howells put_page(page); 51831143d5dSDavid Howells continue; 51931143d5dSDavid Howells } 52031143d5dSDavid Howells 52131143d5dSDavid Howells wb = (struct afs_writeback *) page_private(page); 52231143d5dSDavid Howells ASSERT(wb != NULL); 52331143d5dSDavid Howells 52431143d5dSDavid Howells spin_lock(&wb->vnode->writeback_lock); 52531143d5dSDavid Howells wb->state = AFS_WBACK_WRITING; 52631143d5dSDavid Howells spin_unlock(&wb->vnode->writeback_lock); 52731143d5dSDavid Howells 52831143d5dSDavid Howells ret = afs_write_back_from_locked_page(wb, page); 52931143d5dSDavid Howells unlock_page(page); 53009cbfeafSKirill A. Shutemov put_page(page); 53131143d5dSDavid Howells if (ret < 0) { 53231143d5dSDavid Howells _leave(" = %d", ret); 53331143d5dSDavid Howells return ret; 53431143d5dSDavid Howells } 53531143d5dSDavid Howells 53631143d5dSDavid Howells wbc->nr_to_write -= ret; 53731143d5dSDavid Howells 53831143d5dSDavid Howells cond_resched(); 53931143d5dSDavid Howells } while (index < end && wbc->nr_to_write > 0); 54031143d5dSDavid Howells 54131143d5dSDavid Howells *_next = index; 54231143d5dSDavid Howells _leave(" = 0 [%lx]", *_next); 54331143d5dSDavid Howells return 0; 54431143d5dSDavid Howells } 54531143d5dSDavid Howells 54631143d5dSDavid Howells /* 54731143d5dSDavid Howells * write some of the pending data back to the server 54831143d5dSDavid Howells */ 54931143d5dSDavid Howells int afs_writepages(struct address_space *mapping, 55031143d5dSDavid Howells struct writeback_control *wbc) 55131143d5dSDavid Howells { 55231143d5dSDavid Howells pgoff_t start, end, next; 55331143d5dSDavid Howells int ret; 55431143d5dSDavid Howells 55531143d5dSDavid Howells _enter(""); 55631143d5dSDavid Howells 55731143d5dSDavid Howells if (wbc->range_cyclic) { 55831143d5dSDavid Howells start = mapping->writeback_index; 55931143d5dSDavid Howells end = -1; 56031143d5dSDavid Howells ret = afs_writepages_region(mapping, wbc, start, end, &next); 5611b430beeSWu Fengguang if (start > 0 && wbc->nr_to_write > 0 && ret == 0) 56231143d5dSDavid Howells ret = afs_writepages_region(mapping, wbc, 0, start, 56331143d5dSDavid Howells &next); 56431143d5dSDavid Howells mapping->writeback_index = next; 56531143d5dSDavid Howells } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) { 56609cbfeafSKirill A. Shutemov end = (pgoff_t)(LLONG_MAX >> PAGE_SHIFT); 56731143d5dSDavid Howells ret = afs_writepages_region(mapping, wbc, 0, end, &next); 56831143d5dSDavid Howells if (wbc->nr_to_write > 0) 56931143d5dSDavid Howells mapping->writeback_index = next; 57031143d5dSDavid Howells } else { 57109cbfeafSKirill A. Shutemov start = wbc->range_start >> PAGE_SHIFT; 57209cbfeafSKirill A. Shutemov end = wbc->range_end >> PAGE_SHIFT; 57331143d5dSDavid Howells ret = afs_writepages_region(mapping, wbc, start, end, &next); 57431143d5dSDavid Howells } 57531143d5dSDavid Howells 57631143d5dSDavid Howells _leave(" = %d", ret); 57731143d5dSDavid Howells return ret; 57831143d5dSDavid Howells } 57931143d5dSDavid Howells 58031143d5dSDavid Howells /* 58131143d5dSDavid Howells * completion of write to server 58231143d5dSDavid Howells */ 58331143d5dSDavid Howells void afs_pages_written_back(struct afs_vnode *vnode, struct afs_call *call) 58431143d5dSDavid Howells { 58531143d5dSDavid Howells struct afs_writeback *wb = call->wb; 58631143d5dSDavid Howells struct pagevec pv; 58731143d5dSDavid Howells unsigned count, loop; 58831143d5dSDavid Howells pgoff_t first = call->first, last = call->last; 58931143d5dSDavid Howells bool free_wb; 59031143d5dSDavid Howells 59131143d5dSDavid Howells _enter("{%x:%u},{%lx-%lx}", 59231143d5dSDavid Howells vnode->fid.vid, vnode->fid.vnode, first, last); 59331143d5dSDavid Howells 59431143d5dSDavid Howells ASSERT(wb != NULL); 59531143d5dSDavid Howells 59631143d5dSDavid Howells pagevec_init(&pv, 0); 59731143d5dSDavid Howells 59831143d5dSDavid Howells do { 5995bbf5d39SDavid Howells _debug("done %lx-%lx", first, last); 60031143d5dSDavid Howells 60131143d5dSDavid Howells count = last - first + 1; 60231143d5dSDavid Howells if (count > PAGEVEC_SIZE) 60331143d5dSDavid Howells count = PAGEVEC_SIZE; 60431143d5dSDavid Howells pv.nr = find_get_pages_contig(call->mapping, first, count, 60531143d5dSDavid Howells pv.pages); 60631143d5dSDavid Howells ASSERTCMP(pv.nr, ==, count); 60731143d5dSDavid Howells 60831143d5dSDavid Howells spin_lock(&vnode->writeback_lock); 60931143d5dSDavid Howells for (loop = 0; loop < count; loop++) { 61031143d5dSDavid Howells struct page *page = pv.pages[loop]; 61131143d5dSDavid Howells end_page_writeback(page); 61231143d5dSDavid Howells if (page_private(page) == (unsigned long) wb) { 61331143d5dSDavid Howells set_page_private(page, 0); 61431143d5dSDavid Howells ClearPagePrivate(page); 61531143d5dSDavid Howells wb->usage--; 61631143d5dSDavid Howells } 61731143d5dSDavid Howells } 61831143d5dSDavid Howells free_wb = false; 61931143d5dSDavid Howells if (wb->usage == 0) { 62031143d5dSDavid Howells afs_unlink_writeback(wb); 62131143d5dSDavid Howells free_wb = true; 62231143d5dSDavid Howells } 62331143d5dSDavid Howells spin_unlock(&vnode->writeback_lock); 62431143d5dSDavid Howells first += count; 62531143d5dSDavid Howells if (free_wb) { 62631143d5dSDavid Howells afs_free_writeback(wb); 62731143d5dSDavid Howells wb = NULL; 62831143d5dSDavid Howells } 62931143d5dSDavid Howells 63031143d5dSDavid Howells __pagevec_release(&pv); 6315bbf5d39SDavid Howells } while (first <= last); 63231143d5dSDavid Howells 63331143d5dSDavid Howells _leave(""); 63431143d5dSDavid Howells } 63531143d5dSDavid Howells 63631143d5dSDavid Howells /* 63731143d5dSDavid Howells * write to an AFS file 63831143d5dSDavid Howells */ 63950b5551dSAl Viro ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from) 64031143d5dSDavid Howells { 641496ad9aaSAl Viro struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp)); 64231143d5dSDavid Howells ssize_t result; 64350b5551dSAl Viro size_t count = iov_iter_count(from); 64431143d5dSDavid Howells 64550b5551dSAl Viro _enter("{%x.%u},{%zu},", 64650b5551dSAl Viro vnode->fid.vid, vnode->fid.vnode, count); 64731143d5dSDavid Howells 64831143d5dSDavid Howells if (IS_SWAPFILE(&vnode->vfs_inode)) { 64931143d5dSDavid Howells printk(KERN_INFO 65031143d5dSDavid Howells "AFS: Attempt to write to active swap file!\n"); 65131143d5dSDavid Howells return -EBUSY; 65231143d5dSDavid Howells } 65331143d5dSDavid Howells 65431143d5dSDavid Howells if (!count) 65531143d5dSDavid Howells return 0; 65631143d5dSDavid Howells 65750b5551dSAl Viro result = generic_file_write_iter(iocb, from); 65831143d5dSDavid Howells 65931143d5dSDavid Howells _leave(" = %zd", result); 66031143d5dSDavid Howells return result; 66131143d5dSDavid Howells } 66231143d5dSDavid Howells 66331143d5dSDavid Howells /* 66431143d5dSDavid Howells * flush the vnode to the fileserver 66531143d5dSDavid Howells */ 66631143d5dSDavid Howells int afs_writeback_all(struct afs_vnode *vnode) 66731143d5dSDavid Howells { 66831143d5dSDavid Howells struct address_space *mapping = vnode->vfs_inode.i_mapping; 66931143d5dSDavid Howells struct writeback_control wbc = { 67031143d5dSDavid Howells .sync_mode = WB_SYNC_ALL, 67131143d5dSDavid Howells .nr_to_write = LONG_MAX, 67231143d5dSDavid Howells .range_cyclic = 1, 67331143d5dSDavid Howells }; 67431143d5dSDavid Howells int ret; 67531143d5dSDavid Howells 67631143d5dSDavid Howells _enter(""); 67731143d5dSDavid Howells 67831143d5dSDavid Howells ret = mapping->a_ops->writepages(mapping, &wbc); 67931143d5dSDavid Howells __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); 68031143d5dSDavid Howells 68131143d5dSDavid Howells _leave(" = %d", ret); 68231143d5dSDavid Howells return ret; 68331143d5dSDavid Howells } 68431143d5dSDavid Howells 68531143d5dSDavid Howells /* 68631143d5dSDavid Howells * flush any dirty pages for this process, and check for write errors. 68731143d5dSDavid Howells * - the return status from this call provides a reliable indication of 68831143d5dSDavid Howells * whether any write errors occurred for this process. 68931143d5dSDavid Howells */ 69002c24a82SJosef Bacik int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync) 69131143d5dSDavid Howells { 6923c981bfcSAl Viro struct inode *inode = file_inode(file); 69331143d5dSDavid Howells struct afs_writeback *wb, *xwb; 6943c981bfcSAl Viro struct afs_vnode *vnode = AFS_FS_I(inode); 69531143d5dSDavid Howells int ret; 69631143d5dSDavid Howells 6973c981bfcSAl Viro _enter("{%x:%u},{n=%pD},%d", 6983c981bfcSAl Viro vnode->fid.vid, vnode->fid.vnode, file, 69931143d5dSDavid Howells datasync); 70031143d5dSDavid Howells 70102c24a82SJosef Bacik ret = filemap_write_and_wait_range(inode->i_mapping, start, end); 70202c24a82SJosef Bacik if (ret) 70302c24a82SJosef Bacik return ret; 7045955102cSAl Viro inode_lock(inode); 70502c24a82SJosef Bacik 70631143d5dSDavid Howells /* use a writeback record as a marker in the queue - when this reaches 70731143d5dSDavid Howells * the front of the queue, all the outstanding writes are either 70831143d5dSDavid Howells * completed or rejected */ 70931143d5dSDavid Howells wb = kzalloc(sizeof(*wb), GFP_KERNEL); 71002c24a82SJosef Bacik if (!wb) { 71102c24a82SJosef Bacik ret = -ENOMEM; 71202c24a82SJosef Bacik goto out; 71302c24a82SJosef Bacik } 71431143d5dSDavid Howells wb->vnode = vnode; 71531143d5dSDavid Howells wb->first = 0; 71631143d5dSDavid Howells wb->last = -1; 71731143d5dSDavid Howells wb->offset_first = 0; 71831143d5dSDavid Howells wb->to_last = PAGE_SIZE; 71931143d5dSDavid Howells wb->usage = 1; 72031143d5dSDavid Howells wb->state = AFS_WBACK_SYNCING; 72131143d5dSDavid Howells init_waitqueue_head(&wb->waitq); 72231143d5dSDavid Howells 72331143d5dSDavid Howells spin_lock(&vnode->writeback_lock); 72431143d5dSDavid Howells list_for_each_entry(xwb, &vnode->writebacks, link) { 72531143d5dSDavid Howells if (xwb->state == AFS_WBACK_PENDING) 72631143d5dSDavid Howells xwb->state = AFS_WBACK_CONFLICTING; 72731143d5dSDavid Howells } 72831143d5dSDavid Howells list_add_tail(&wb->link, &vnode->writebacks); 72931143d5dSDavid Howells spin_unlock(&vnode->writeback_lock); 73031143d5dSDavid Howells 73131143d5dSDavid Howells /* push all the outstanding writebacks to the server */ 73231143d5dSDavid Howells ret = afs_writeback_all(vnode); 73331143d5dSDavid Howells if (ret < 0) { 73431143d5dSDavid Howells afs_put_writeback(wb); 73531143d5dSDavid Howells _leave(" = %d [wb]", ret); 73602c24a82SJosef Bacik goto out; 73731143d5dSDavid Howells } 73831143d5dSDavid Howells 73931143d5dSDavid Howells /* wait for the preceding writes to actually complete */ 74031143d5dSDavid Howells ret = wait_event_interruptible(wb->waitq, 74131143d5dSDavid Howells wb->state == AFS_WBACK_COMPLETE || 74231143d5dSDavid Howells vnode->writebacks.next == &wb->link); 74331143d5dSDavid Howells afs_put_writeback(wb); 74431143d5dSDavid Howells _leave(" = %d", ret); 74502c24a82SJosef Bacik out: 7465955102cSAl Viro inode_unlock(inode); 74731143d5dSDavid Howells return ret; 74831143d5dSDavid Howells } 7499b3f26c9SDavid Howells 7509b3f26c9SDavid Howells /* 7519b3f26c9SDavid Howells * notification that a previously read-only page is about to become writable 7529b3f26c9SDavid Howells * - if it returns an error, the caller will deliver a bus error signal 7539b3f26c9SDavid Howells */ 7549b3f26c9SDavid Howells int afs_page_mkwrite(struct vm_area_struct *vma, struct page *page) 7559b3f26c9SDavid Howells { 7569b3f26c9SDavid Howells struct afs_vnode *vnode = AFS_FS_I(vma->vm_file->f_mapping->host); 7579b3f26c9SDavid Howells 7589b3f26c9SDavid Howells _enter("{{%x:%u}},{%lx}", 7599b3f26c9SDavid Howells vnode->fid.vid, vnode->fid.vnode, page->index); 7609b3f26c9SDavid Howells 7619b3f26c9SDavid Howells /* wait for the page to be written to the cache before we allow it to 7629b3f26c9SDavid Howells * be modified */ 7639b3f26c9SDavid Howells #ifdef CONFIG_AFS_FSCACHE 7649b3f26c9SDavid Howells fscache_wait_on_page_write(vnode->cache, page); 7659b3f26c9SDavid Howells #endif 7669b3f26c9SDavid Howells 7679b3f26c9SDavid Howells _leave(" = 0"); 7689b3f26c9SDavid Howells return 0; 7699b3f26c9SDavid Howells } 770