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 { 8915b4650eSNick Piggin loff_t i_size; 9031143d5dSDavid Howells int ret; 915e7f2337SAnton Blanchard int len; 9231143d5dSDavid Howells 935e7f2337SAnton Blanchard _enter(",,%llu", (unsigned long long)pos); 9431143d5dSDavid Howells 9515b4650eSNick Piggin i_size = i_size_read(&vnode->vfs_inode); 9609cbfeafSKirill A. Shutemov if (pos + PAGE_SIZE > i_size) 975e7f2337SAnton Blanchard len = i_size - pos; 9815b4650eSNick Piggin else 9909cbfeafSKirill A. Shutemov len = PAGE_SIZE; 10015b4650eSNick Piggin 1015e7f2337SAnton Blanchard ret = afs_vnode_fetch_data(vnode, key, pos, len, page); 10231143d5dSDavid Howells if (ret < 0) { 10331143d5dSDavid Howells if (ret == -ENOENT) { 10431143d5dSDavid Howells _debug("got NOENT from server" 10531143d5dSDavid Howells " - marking file deleted and stale"); 10631143d5dSDavid Howells set_bit(AFS_VNODE_DELETED, &vnode->flags); 10731143d5dSDavid Howells ret = -ESTALE; 10831143d5dSDavid Howells } 10931143d5dSDavid Howells } 11031143d5dSDavid Howells 11131143d5dSDavid Howells _leave(" = %d", ret); 11231143d5dSDavid Howells return ret; 11331143d5dSDavid Howells } 11431143d5dSDavid Howells 11531143d5dSDavid Howells /* 11631143d5dSDavid Howells * prepare to perform part of a write to a page 11731143d5dSDavid Howells */ 11815b4650eSNick Piggin int afs_write_begin(struct file *file, struct address_space *mapping, 11915b4650eSNick Piggin loff_t pos, unsigned len, unsigned flags, 12015b4650eSNick Piggin struct page **pagep, void **fsdata) 12131143d5dSDavid Howells { 12231143d5dSDavid Howells struct afs_writeback *candidate, *wb; 123496ad9aaSAl Viro struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 12415b4650eSNick Piggin struct page *page; 12531143d5dSDavid Howells struct key *key = file->private_data; 12609cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 12715b4650eSNick Piggin unsigned to = from + len; 12809cbfeafSKirill A. Shutemov pgoff_t index = pos >> PAGE_SHIFT; 12931143d5dSDavid Howells int ret; 13031143d5dSDavid Howells 13131143d5dSDavid Howells _enter("{%x:%u},{%lx},%u,%u", 13215b4650eSNick Piggin vnode->fid.vid, vnode->fid.vnode, index, from, to); 13331143d5dSDavid Howells 13431143d5dSDavid Howells candidate = kzalloc(sizeof(*candidate), GFP_KERNEL); 13531143d5dSDavid Howells if (!candidate) 13631143d5dSDavid Howells return -ENOMEM; 13731143d5dSDavid Howells candidate->vnode = vnode; 13815b4650eSNick Piggin candidate->first = candidate->last = index; 13915b4650eSNick Piggin candidate->offset_first = from; 14031143d5dSDavid Howells candidate->to_last = to; 141f129ccc9SAnton Blanchard INIT_LIST_HEAD(&candidate->link); 14231143d5dSDavid Howells candidate->usage = 1; 14331143d5dSDavid Howells candidate->state = AFS_WBACK_PENDING; 14431143d5dSDavid Howells init_waitqueue_head(&candidate->waitq); 14531143d5dSDavid Howells 14654566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 14715b4650eSNick Piggin if (!page) { 14815b4650eSNick Piggin kfree(candidate); 14915b4650eSNick Piggin return -ENOMEM; 15015b4650eSNick Piggin } 15115b4650eSNick Piggin *pagep = page; 15215b4650eSNick Piggin /* page won't leak in error case: it eventually gets cleaned off LRU */ 15315b4650eSNick Piggin 15409cbfeafSKirill A. Shutemov if (!PageUptodate(page) && len != PAGE_SIZE) { 15509cbfeafSKirill A. Shutemov ret = afs_fill_page(vnode, key, index << PAGE_SHIFT, page); 15631143d5dSDavid Howells if (ret < 0) { 15731143d5dSDavid Howells kfree(candidate); 15831143d5dSDavid Howells _leave(" = %d [prep]", ret); 15931143d5dSDavid Howells return ret; 16031143d5dSDavid Howells } 16115b4650eSNick Piggin SetPageUptodate(page); 16231143d5dSDavid Howells } 16331143d5dSDavid Howells 16431143d5dSDavid Howells try_again: 16531143d5dSDavid Howells spin_lock(&vnode->writeback_lock); 16631143d5dSDavid Howells 16731143d5dSDavid Howells /* see if this page is already pending a writeback under a suitable key 16831143d5dSDavid Howells * - if so we can just join onto that one */ 16931143d5dSDavid Howells wb = (struct afs_writeback *) page_private(page); 17031143d5dSDavid Howells if (wb) { 17131143d5dSDavid Howells if (wb->key == key && wb->state == AFS_WBACK_PENDING) 17231143d5dSDavid Howells goto subsume_in_current_wb; 17331143d5dSDavid Howells goto flush_conflicting_wb; 17431143d5dSDavid Howells } 17531143d5dSDavid Howells 17631143d5dSDavid Howells if (index > 0) { 17731143d5dSDavid Howells /* see if we can find an already pending writeback that we can 17831143d5dSDavid Howells * append this page to */ 17931143d5dSDavid Howells list_for_each_entry(wb, &vnode->writebacks, link) { 18031143d5dSDavid Howells if (wb->last == index - 1 && wb->key == key && 18131143d5dSDavid Howells wb->state == AFS_WBACK_PENDING) 18231143d5dSDavid Howells goto append_to_previous_wb; 18331143d5dSDavid Howells } 18431143d5dSDavid Howells } 18531143d5dSDavid Howells 18631143d5dSDavid Howells list_add_tail(&candidate->link, &vnode->writebacks); 18731143d5dSDavid Howells candidate->key = key_get(key); 18831143d5dSDavid Howells spin_unlock(&vnode->writeback_lock); 18931143d5dSDavid Howells SetPagePrivate(page); 19031143d5dSDavid Howells set_page_private(page, (unsigned long) candidate); 19131143d5dSDavid Howells _leave(" = 0 [new]"); 19231143d5dSDavid Howells return 0; 19331143d5dSDavid Howells 19431143d5dSDavid Howells subsume_in_current_wb: 19531143d5dSDavid Howells _debug("subsume"); 19631143d5dSDavid Howells ASSERTRANGE(wb->first, <=, index, <=, wb->last); 19715b4650eSNick Piggin if (index == wb->first && from < wb->offset_first) 19815b4650eSNick Piggin wb->offset_first = from; 19931143d5dSDavid Howells if (index == wb->last && to > wb->to_last) 20031143d5dSDavid Howells wb->to_last = to; 20131143d5dSDavid Howells spin_unlock(&vnode->writeback_lock); 20231143d5dSDavid Howells kfree(candidate); 20331143d5dSDavid Howells _leave(" = 0 [sub]"); 20431143d5dSDavid Howells return 0; 20531143d5dSDavid Howells 20631143d5dSDavid Howells append_to_previous_wb: 20731143d5dSDavid Howells _debug("append into %lx-%lx", wb->first, wb->last); 20831143d5dSDavid Howells wb->usage++; 20931143d5dSDavid Howells wb->last++; 21031143d5dSDavid Howells wb->to_last = to; 21131143d5dSDavid Howells spin_unlock(&vnode->writeback_lock); 21231143d5dSDavid Howells SetPagePrivate(page); 21331143d5dSDavid Howells set_page_private(page, (unsigned long) wb); 21431143d5dSDavid Howells kfree(candidate); 21531143d5dSDavid Howells _leave(" = 0 [app]"); 21631143d5dSDavid Howells return 0; 21731143d5dSDavid Howells 21831143d5dSDavid Howells /* the page is currently bound to another context, so if it's dirty we 21931143d5dSDavid Howells * need to flush it before we can use the new context */ 22031143d5dSDavid Howells flush_conflicting_wb: 22131143d5dSDavid Howells _debug("flush conflict"); 22231143d5dSDavid Howells if (wb->state == AFS_WBACK_PENDING) 22331143d5dSDavid Howells wb->state = AFS_WBACK_CONFLICTING; 22431143d5dSDavid Howells spin_unlock(&vnode->writeback_lock); 22531143d5dSDavid Howells if (PageDirty(page)) { 22631143d5dSDavid Howells ret = afs_write_back_from_locked_page(wb, page); 22731143d5dSDavid Howells if (ret < 0) { 22831143d5dSDavid Howells afs_put_writeback(candidate); 22931143d5dSDavid Howells _leave(" = %d", ret); 23031143d5dSDavid Howells return ret; 23131143d5dSDavid Howells } 23231143d5dSDavid Howells } 23331143d5dSDavid Howells 23431143d5dSDavid Howells /* the page holds a ref on the writeback record */ 23531143d5dSDavid Howells afs_put_writeback(wb); 23631143d5dSDavid Howells set_page_private(page, 0); 23731143d5dSDavid Howells ClearPagePrivate(page); 23831143d5dSDavid Howells goto try_again; 23931143d5dSDavid Howells } 24031143d5dSDavid Howells 24131143d5dSDavid Howells /* 24231143d5dSDavid Howells * finalise part of a write to a page 24331143d5dSDavid Howells */ 24415b4650eSNick Piggin int afs_write_end(struct file *file, struct address_space *mapping, 24515b4650eSNick Piggin loff_t pos, unsigned len, unsigned copied, 24615b4650eSNick Piggin struct page *page, void *fsdata) 24731143d5dSDavid Howells { 248496ad9aaSAl Viro struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 24931143d5dSDavid Howells loff_t i_size, maybe_i_size; 25031143d5dSDavid Howells 25115b4650eSNick Piggin _enter("{%x:%u},{%lx}", 25215b4650eSNick Piggin vnode->fid.vid, vnode->fid.vnode, page->index); 25331143d5dSDavid Howells 25415b4650eSNick Piggin maybe_i_size = pos + copied; 25531143d5dSDavid Howells 25631143d5dSDavid Howells i_size = i_size_read(&vnode->vfs_inode); 25731143d5dSDavid Howells if (maybe_i_size > i_size) { 25831143d5dSDavid Howells spin_lock(&vnode->writeback_lock); 25931143d5dSDavid Howells i_size = i_size_read(&vnode->vfs_inode); 26031143d5dSDavid Howells if (maybe_i_size > i_size) 26131143d5dSDavid Howells i_size_write(&vnode->vfs_inode, maybe_i_size); 26231143d5dSDavid Howells spin_unlock(&vnode->writeback_lock); 26331143d5dSDavid Howells } 26431143d5dSDavid Howells 26531143d5dSDavid Howells set_page_dirty(page); 26631143d5dSDavid Howells if (PageDirty(page)) 26731143d5dSDavid Howells _debug("dirtied"); 26815b4650eSNick Piggin unlock_page(page); 26909cbfeafSKirill A. Shutemov put_page(page); 27031143d5dSDavid Howells 27115b4650eSNick Piggin return copied; 27231143d5dSDavid Howells } 27331143d5dSDavid Howells 27431143d5dSDavid Howells /* 27531143d5dSDavid Howells * kill all the pages in the given range 27631143d5dSDavid Howells */ 27731143d5dSDavid Howells static void afs_kill_pages(struct afs_vnode *vnode, bool error, 27831143d5dSDavid Howells pgoff_t first, pgoff_t last) 27931143d5dSDavid Howells { 28031143d5dSDavid Howells struct pagevec pv; 28131143d5dSDavid Howells unsigned count, loop; 28231143d5dSDavid Howells 28331143d5dSDavid Howells _enter("{%x:%u},%lx-%lx", 28431143d5dSDavid Howells vnode->fid.vid, vnode->fid.vnode, first, last); 28531143d5dSDavid Howells 28631143d5dSDavid Howells pagevec_init(&pv, 0); 28731143d5dSDavid Howells 28831143d5dSDavid Howells do { 28931143d5dSDavid Howells _debug("kill %lx-%lx", first, last); 29031143d5dSDavid Howells 29131143d5dSDavid Howells count = last - first + 1; 29231143d5dSDavid Howells if (count > PAGEVEC_SIZE) 29331143d5dSDavid Howells count = PAGEVEC_SIZE; 29431143d5dSDavid Howells pv.nr = find_get_pages_contig(vnode->vfs_inode.i_mapping, 29531143d5dSDavid Howells first, count, pv.pages); 29631143d5dSDavid Howells ASSERTCMP(pv.nr, ==, count); 29731143d5dSDavid Howells 29831143d5dSDavid Howells for (loop = 0; loop < count; loop++) { 29931143d5dSDavid Howells ClearPageUptodate(pv.pages[loop]); 30031143d5dSDavid Howells if (error) 30131143d5dSDavid Howells SetPageError(pv.pages[loop]); 30231143d5dSDavid Howells end_page_writeback(pv.pages[loop]); 30331143d5dSDavid Howells } 30431143d5dSDavid Howells 30531143d5dSDavid Howells __pagevec_release(&pv); 30631143d5dSDavid Howells } while (first < last); 30731143d5dSDavid Howells 30831143d5dSDavid Howells _leave(""); 30931143d5dSDavid Howells } 31031143d5dSDavid Howells 31131143d5dSDavid Howells /* 31231143d5dSDavid Howells * synchronously write back the locked page and any subsequent non-locked dirty 31331143d5dSDavid Howells * pages also covered by the same writeback record 31431143d5dSDavid Howells */ 31531143d5dSDavid Howells static int afs_write_back_from_locked_page(struct afs_writeback *wb, 31631143d5dSDavid Howells struct page *primary_page) 31731143d5dSDavid Howells { 31831143d5dSDavid Howells struct page *pages[8], *page; 31931143d5dSDavid Howells unsigned long count; 32031143d5dSDavid Howells unsigned n, offset, to; 32131143d5dSDavid Howells pgoff_t start, first, last; 32231143d5dSDavid Howells int loop, ret; 32331143d5dSDavid Howells 32431143d5dSDavid Howells _enter(",%lx", primary_page->index); 32531143d5dSDavid Howells 32631143d5dSDavid Howells count = 1; 32731143d5dSDavid Howells if (!clear_page_dirty_for_io(primary_page)) 32831143d5dSDavid Howells BUG(); 32931143d5dSDavid Howells if (test_set_page_writeback(primary_page)) 33031143d5dSDavid Howells BUG(); 33131143d5dSDavid Howells 33231143d5dSDavid Howells /* find all consecutive lockable dirty pages, stopping when we find a 33331143d5dSDavid Howells * page that is not immediately lockable, is not dirty or is missing, 33431143d5dSDavid Howells * or we reach the end of the range */ 33531143d5dSDavid Howells start = primary_page->index; 33631143d5dSDavid Howells if (start >= wb->last) 33731143d5dSDavid Howells goto no_more; 33831143d5dSDavid Howells start++; 33931143d5dSDavid Howells do { 34031143d5dSDavid Howells _debug("more %lx [%lx]", start, count); 34131143d5dSDavid Howells n = wb->last - start + 1; 34231143d5dSDavid Howells if (n > ARRAY_SIZE(pages)) 34331143d5dSDavid Howells n = ARRAY_SIZE(pages); 34431143d5dSDavid Howells n = find_get_pages_contig(wb->vnode->vfs_inode.i_mapping, 34531143d5dSDavid Howells start, n, pages); 34631143d5dSDavid Howells _debug("fgpc %u", n); 34731143d5dSDavid Howells if (n == 0) 34831143d5dSDavid Howells goto no_more; 34931143d5dSDavid Howells if (pages[0]->index != start) { 3509d577b6aSDavid Howells do { 3519d577b6aSDavid Howells put_page(pages[--n]); 3529d577b6aSDavid Howells } while (n > 0); 35331143d5dSDavid Howells goto no_more; 35431143d5dSDavid Howells } 35531143d5dSDavid Howells 35631143d5dSDavid Howells for (loop = 0; loop < n; loop++) { 35731143d5dSDavid Howells page = pages[loop]; 35831143d5dSDavid Howells if (page->index > wb->last) 35931143d5dSDavid Howells break; 360529ae9aaSNick Piggin if (!trylock_page(page)) 36131143d5dSDavid Howells break; 36231143d5dSDavid Howells if (!PageDirty(page) || 36331143d5dSDavid Howells page_private(page) != (unsigned long) wb) { 36431143d5dSDavid Howells unlock_page(page); 36531143d5dSDavid Howells break; 36631143d5dSDavid Howells } 36731143d5dSDavid Howells if (!clear_page_dirty_for_io(page)) 36831143d5dSDavid Howells BUG(); 36931143d5dSDavid Howells if (test_set_page_writeback(page)) 37031143d5dSDavid Howells BUG(); 37131143d5dSDavid Howells unlock_page(page); 37231143d5dSDavid Howells put_page(page); 37331143d5dSDavid Howells } 37431143d5dSDavid Howells count += loop; 37531143d5dSDavid Howells if (loop < n) { 37631143d5dSDavid Howells for (; loop < n; loop++) 37731143d5dSDavid Howells put_page(pages[loop]); 37831143d5dSDavid Howells goto no_more; 37931143d5dSDavid Howells } 38031143d5dSDavid Howells 38131143d5dSDavid Howells start += loop; 38231143d5dSDavid Howells } while (start <= wb->last && count < 65536); 38331143d5dSDavid Howells 38431143d5dSDavid Howells no_more: 38531143d5dSDavid Howells /* we now have a contiguous set of dirty pages, each with writeback set 38631143d5dSDavid Howells * and the dirty mark cleared; the first page is locked and must remain 38731143d5dSDavid Howells * so, all the rest are unlocked */ 38831143d5dSDavid Howells first = primary_page->index; 38931143d5dSDavid Howells last = first + count - 1; 39031143d5dSDavid Howells 39131143d5dSDavid Howells offset = (first == wb->first) ? wb->offset_first : 0; 39231143d5dSDavid Howells to = (last == wb->last) ? wb->to_last : PAGE_SIZE; 39331143d5dSDavid Howells 39431143d5dSDavid Howells _debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to); 39531143d5dSDavid Howells 39631143d5dSDavid Howells ret = afs_vnode_store_data(wb, first, last, offset, to); 39731143d5dSDavid Howells if (ret < 0) { 39831143d5dSDavid Howells switch (ret) { 39931143d5dSDavid Howells case -EDQUOT: 40031143d5dSDavid Howells case -ENOSPC: 4015114a97aSMichal Hocko mapping_set_error(wb->vnode->vfs_inode.i_mapping, -ENOSPC); 40231143d5dSDavid Howells break; 40331143d5dSDavid Howells case -EROFS: 40431143d5dSDavid Howells case -EIO: 40531143d5dSDavid Howells case -EREMOTEIO: 40631143d5dSDavid Howells case -EFBIG: 40731143d5dSDavid Howells case -ENOENT: 40831143d5dSDavid Howells case -ENOMEDIUM: 40931143d5dSDavid Howells case -ENXIO: 41031143d5dSDavid Howells afs_kill_pages(wb->vnode, true, first, last); 4115114a97aSMichal Hocko mapping_set_error(wb->vnode->vfs_inode.i_mapping, -EIO); 41231143d5dSDavid Howells break; 41331143d5dSDavid Howells case -EACCES: 41431143d5dSDavid Howells case -EPERM: 41531143d5dSDavid Howells case -ENOKEY: 41631143d5dSDavid Howells case -EKEYEXPIRED: 41731143d5dSDavid Howells case -EKEYREJECTED: 41831143d5dSDavid Howells case -EKEYREVOKED: 41931143d5dSDavid Howells afs_kill_pages(wb->vnode, false, first, last); 42031143d5dSDavid Howells break; 42131143d5dSDavid Howells default: 42231143d5dSDavid Howells break; 42331143d5dSDavid Howells } 42431143d5dSDavid Howells } else { 42531143d5dSDavid Howells ret = count; 42631143d5dSDavid Howells } 42731143d5dSDavid Howells 42831143d5dSDavid Howells _leave(" = %d", ret); 42931143d5dSDavid Howells return ret; 43031143d5dSDavid Howells } 43131143d5dSDavid Howells 43231143d5dSDavid Howells /* 43331143d5dSDavid Howells * write a page back to the server 43431143d5dSDavid Howells * - the caller locked the page for us 43531143d5dSDavid Howells */ 43631143d5dSDavid Howells int afs_writepage(struct page *page, struct writeback_control *wbc) 43731143d5dSDavid Howells { 43831143d5dSDavid Howells struct afs_writeback *wb; 43931143d5dSDavid Howells int ret; 44031143d5dSDavid Howells 44131143d5dSDavid Howells _enter("{%lx},", page->index); 44231143d5dSDavid Howells 44331143d5dSDavid Howells wb = (struct afs_writeback *) page_private(page); 44431143d5dSDavid Howells ASSERT(wb != NULL); 44531143d5dSDavid Howells 44631143d5dSDavid Howells ret = afs_write_back_from_locked_page(wb, page); 44731143d5dSDavid Howells unlock_page(page); 44831143d5dSDavid Howells if (ret < 0) { 44931143d5dSDavid Howells _leave(" = %d", ret); 45031143d5dSDavid Howells return 0; 45131143d5dSDavid Howells } 45231143d5dSDavid Howells 45331143d5dSDavid Howells wbc->nr_to_write -= ret; 45431143d5dSDavid Howells 45531143d5dSDavid Howells _leave(" = 0"); 45631143d5dSDavid Howells return 0; 45731143d5dSDavid Howells } 45831143d5dSDavid Howells 45931143d5dSDavid Howells /* 46031143d5dSDavid Howells * write a region of pages back to the server 46131143d5dSDavid Howells */ 462c1206a2cSAdrian Bunk static int afs_writepages_region(struct address_space *mapping, 46331143d5dSDavid Howells struct writeback_control *wbc, 46431143d5dSDavid Howells pgoff_t index, pgoff_t end, pgoff_t *_next) 46531143d5dSDavid Howells { 46631143d5dSDavid Howells struct afs_writeback *wb; 46731143d5dSDavid Howells struct page *page; 46831143d5dSDavid Howells int ret, n; 46931143d5dSDavid Howells 47031143d5dSDavid Howells _enter(",,%lx,%lx,", index, end); 47131143d5dSDavid Howells 47231143d5dSDavid Howells do { 47331143d5dSDavid Howells n = find_get_pages_tag(mapping, &index, PAGECACHE_TAG_DIRTY, 47431143d5dSDavid Howells 1, &page); 47531143d5dSDavid Howells if (!n) 47631143d5dSDavid Howells break; 47731143d5dSDavid Howells 47831143d5dSDavid Howells _debug("wback %lx", page->index); 47931143d5dSDavid Howells 48031143d5dSDavid Howells if (page->index > end) { 48131143d5dSDavid Howells *_next = index; 48209cbfeafSKirill A. Shutemov put_page(page); 48331143d5dSDavid Howells _leave(" = 0 [%lx]", *_next); 48431143d5dSDavid Howells return 0; 48531143d5dSDavid Howells } 48631143d5dSDavid Howells 48731143d5dSDavid Howells /* at this point we hold neither mapping->tree_lock nor lock on 48831143d5dSDavid Howells * the page itself: the page may be truncated or invalidated 48931143d5dSDavid Howells * (changing page->mapping to NULL), or even swizzled back from 49031143d5dSDavid Howells * swapper_space to tmpfs file mapping 49131143d5dSDavid Howells */ 49231143d5dSDavid Howells lock_page(page); 49331143d5dSDavid Howells 49431143d5dSDavid Howells if (page->mapping != mapping) { 49531143d5dSDavid Howells unlock_page(page); 49609cbfeafSKirill A. Shutemov put_page(page); 49731143d5dSDavid Howells continue; 49831143d5dSDavid Howells } 49931143d5dSDavid Howells 50031143d5dSDavid Howells if (wbc->sync_mode != WB_SYNC_NONE) 50131143d5dSDavid Howells wait_on_page_writeback(page); 50231143d5dSDavid Howells 50331143d5dSDavid Howells if (PageWriteback(page) || !PageDirty(page)) { 50431143d5dSDavid Howells unlock_page(page); 50531143d5dSDavid Howells continue; 50631143d5dSDavid Howells } 50731143d5dSDavid Howells 50831143d5dSDavid Howells wb = (struct afs_writeback *) page_private(page); 50931143d5dSDavid Howells ASSERT(wb != NULL); 51031143d5dSDavid Howells 51131143d5dSDavid Howells spin_lock(&wb->vnode->writeback_lock); 51231143d5dSDavid Howells wb->state = AFS_WBACK_WRITING; 51331143d5dSDavid Howells spin_unlock(&wb->vnode->writeback_lock); 51431143d5dSDavid Howells 51531143d5dSDavid Howells ret = afs_write_back_from_locked_page(wb, page); 51631143d5dSDavid Howells unlock_page(page); 51709cbfeafSKirill A. Shutemov put_page(page); 51831143d5dSDavid Howells if (ret < 0) { 51931143d5dSDavid Howells _leave(" = %d", ret); 52031143d5dSDavid Howells return ret; 52131143d5dSDavid Howells } 52231143d5dSDavid Howells 52331143d5dSDavid Howells wbc->nr_to_write -= ret; 52431143d5dSDavid Howells 52531143d5dSDavid Howells cond_resched(); 52631143d5dSDavid Howells } while (index < end && wbc->nr_to_write > 0); 52731143d5dSDavid Howells 52831143d5dSDavid Howells *_next = index; 52931143d5dSDavid Howells _leave(" = 0 [%lx]", *_next); 53031143d5dSDavid Howells return 0; 53131143d5dSDavid Howells } 53231143d5dSDavid Howells 53331143d5dSDavid Howells /* 53431143d5dSDavid Howells * write some of the pending data back to the server 53531143d5dSDavid Howells */ 53631143d5dSDavid Howells int afs_writepages(struct address_space *mapping, 53731143d5dSDavid Howells struct writeback_control *wbc) 53831143d5dSDavid Howells { 53931143d5dSDavid Howells pgoff_t start, end, next; 54031143d5dSDavid Howells int ret; 54131143d5dSDavid Howells 54231143d5dSDavid Howells _enter(""); 54331143d5dSDavid Howells 54431143d5dSDavid Howells if (wbc->range_cyclic) { 54531143d5dSDavid Howells start = mapping->writeback_index; 54631143d5dSDavid Howells end = -1; 54731143d5dSDavid Howells ret = afs_writepages_region(mapping, wbc, start, end, &next); 5481b430beeSWu Fengguang if (start > 0 && wbc->nr_to_write > 0 && ret == 0) 54931143d5dSDavid Howells ret = afs_writepages_region(mapping, wbc, 0, start, 55031143d5dSDavid Howells &next); 55131143d5dSDavid Howells mapping->writeback_index = next; 55231143d5dSDavid Howells } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) { 55309cbfeafSKirill A. Shutemov end = (pgoff_t)(LLONG_MAX >> PAGE_SHIFT); 55431143d5dSDavid Howells ret = afs_writepages_region(mapping, wbc, 0, end, &next); 55531143d5dSDavid Howells if (wbc->nr_to_write > 0) 55631143d5dSDavid Howells mapping->writeback_index = next; 55731143d5dSDavid Howells } else { 55809cbfeafSKirill A. Shutemov start = wbc->range_start >> PAGE_SHIFT; 55909cbfeafSKirill A. Shutemov end = wbc->range_end >> PAGE_SHIFT; 56031143d5dSDavid Howells ret = afs_writepages_region(mapping, wbc, start, end, &next); 56131143d5dSDavid Howells } 56231143d5dSDavid Howells 56331143d5dSDavid Howells _leave(" = %d", ret); 56431143d5dSDavid Howells return ret; 56531143d5dSDavid Howells } 56631143d5dSDavid Howells 56731143d5dSDavid Howells /* 56831143d5dSDavid Howells * completion of write to server 56931143d5dSDavid Howells */ 57031143d5dSDavid Howells void afs_pages_written_back(struct afs_vnode *vnode, struct afs_call *call) 57131143d5dSDavid Howells { 57231143d5dSDavid Howells struct afs_writeback *wb = call->wb; 57331143d5dSDavid Howells struct pagevec pv; 57431143d5dSDavid Howells unsigned count, loop; 57531143d5dSDavid Howells pgoff_t first = call->first, last = call->last; 57631143d5dSDavid Howells bool free_wb; 57731143d5dSDavid Howells 57831143d5dSDavid Howells _enter("{%x:%u},{%lx-%lx}", 57931143d5dSDavid Howells vnode->fid.vid, vnode->fid.vnode, first, last); 58031143d5dSDavid Howells 58131143d5dSDavid Howells ASSERT(wb != NULL); 58231143d5dSDavid Howells 58331143d5dSDavid Howells pagevec_init(&pv, 0); 58431143d5dSDavid Howells 58531143d5dSDavid Howells do { 5865bbf5d39SDavid Howells _debug("done %lx-%lx", first, last); 58731143d5dSDavid Howells 58831143d5dSDavid Howells count = last - first + 1; 58931143d5dSDavid Howells if (count > PAGEVEC_SIZE) 59031143d5dSDavid Howells count = PAGEVEC_SIZE; 59131143d5dSDavid Howells pv.nr = find_get_pages_contig(call->mapping, first, count, 59231143d5dSDavid Howells pv.pages); 59331143d5dSDavid Howells ASSERTCMP(pv.nr, ==, count); 59431143d5dSDavid Howells 59531143d5dSDavid Howells spin_lock(&vnode->writeback_lock); 59631143d5dSDavid Howells for (loop = 0; loop < count; loop++) { 59731143d5dSDavid Howells struct page *page = pv.pages[loop]; 59831143d5dSDavid Howells end_page_writeback(page); 59931143d5dSDavid Howells if (page_private(page) == (unsigned long) wb) { 60031143d5dSDavid Howells set_page_private(page, 0); 60131143d5dSDavid Howells ClearPagePrivate(page); 60231143d5dSDavid Howells wb->usage--; 60331143d5dSDavid Howells } 60431143d5dSDavid Howells } 60531143d5dSDavid Howells free_wb = false; 60631143d5dSDavid Howells if (wb->usage == 0) { 60731143d5dSDavid Howells afs_unlink_writeback(wb); 60831143d5dSDavid Howells free_wb = true; 60931143d5dSDavid Howells } 61031143d5dSDavid Howells spin_unlock(&vnode->writeback_lock); 61131143d5dSDavid Howells first += count; 61231143d5dSDavid Howells if (free_wb) { 61331143d5dSDavid Howells afs_free_writeback(wb); 61431143d5dSDavid Howells wb = NULL; 61531143d5dSDavid Howells } 61631143d5dSDavid Howells 61731143d5dSDavid Howells __pagevec_release(&pv); 6185bbf5d39SDavid Howells } while (first <= last); 61931143d5dSDavid Howells 62031143d5dSDavid Howells _leave(""); 62131143d5dSDavid Howells } 62231143d5dSDavid Howells 62331143d5dSDavid Howells /* 62431143d5dSDavid Howells * write to an AFS file 62531143d5dSDavid Howells */ 62650b5551dSAl Viro ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from) 62731143d5dSDavid Howells { 628496ad9aaSAl Viro struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp)); 62931143d5dSDavid Howells ssize_t result; 63050b5551dSAl Viro size_t count = iov_iter_count(from); 63131143d5dSDavid Howells 63250b5551dSAl Viro _enter("{%x.%u},{%zu},", 63350b5551dSAl Viro vnode->fid.vid, vnode->fid.vnode, count); 63431143d5dSDavid Howells 63531143d5dSDavid Howells if (IS_SWAPFILE(&vnode->vfs_inode)) { 63631143d5dSDavid Howells printk(KERN_INFO 63731143d5dSDavid Howells "AFS: Attempt to write to active swap file!\n"); 63831143d5dSDavid Howells return -EBUSY; 63931143d5dSDavid Howells } 64031143d5dSDavid Howells 64131143d5dSDavid Howells if (!count) 64231143d5dSDavid Howells return 0; 64331143d5dSDavid Howells 64450b5551dSAl Viro result = generic_file_write_iter(iocb, from); 64531143d5dSDavid Howells 64631143d5dSDavid Howells _leave(" = %zd", result); 64731143d5dSDavid Howells return result; 64831143d5dSDavid Howells } 64931143d5dSDavid Howells 65031143d5dSDavid Howells /* 65131143d5dSDavid Howells * flush the vnode to the fileserver 65231143d5dSDavid Howells */ 65331143d5dSDavid Howells int afs_writeback_all(struct afs_vnode *vnode) 65431143d5dSDavid Howells { 65531143d5dSDavid Howells struct address_space *mapping = vnode->vfs_inode.i_mapping; 65631143d5dSDavid Howells struct writeback_control wbc = { 65731143d5dSDavid Howells .sync_mode = WB_SYNC_ALL, 65831143d5dSDavid Howells .nr_to_write = LONG_MAX, 65931143d5dSDavid Howells .range_cyclic = 1, 66031143d5dSDavid Howells }; 66131143d5dSDavid Howells int ret; 66231143d5dSDavid Howells 66331143d5dSDavid Howells _enter(""); 66431143d5dSDavid Howells 66531143d5dSDavid Howells ret = mapping->a_ops->writepages(mapping, &wbc); 66631143d5dSDavid Howells __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); 66731143d5dSDavid Howells 66831143d5dSDavid Howells _leave(" = %d", ret); 66931143d5dSDavid Howells return ret; 67031143d5dSDavid Howells } 67131143d5dSDavid Howells 67231143d5dSDavid Howells /* 67331143d5dSDavid Howells * flush any dirty pages for this process, and check for write errors. 67431143d5dSDavid Howells * - the return status from this call provides a reliable indication of 67531143d5dSDavid Howells * whether any write errors occurred for this process. 67631143d5dSDavid Howells */ 67702c24a82SJosef Bacik int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync) 67831143d5dSDavid Howells { 6793c981bfcSAl Viro struct inode *inode = file_inode(file); 68031143d5dSDavid Howells struct afs_writeback *wb, *xwb; 6813c981bfcSAl Viro struct afs_vnode *vnode = AFS_FS_I(inode); 68231143d5dSDavid Howells int ret; 68331143d5dSDavid Howells 6843c981bfcSAl Viro _enter("{%x:%u},{n=%pD},%d", 6853c981bfcSAl Viro vnode->fid.vid, vnode->fid.vnode, file, 68631143d5dSDavid Howells datasync); 68731143d5dSDavid Howells 68802c24a82SJosef Bacik ret = filemap_write_and_wait_range(inode->i_mapping, start, end); 68902c24a82SJosef Bacik if (ret) 69002c24a82SJosef Bacik return ret; 6915955102cSAl Viro inode_lock(inode); 69202c24a82SJosef Bacik 69331143d5dSDavid Howells /* use a writeback record as a marker in the queue - when this reaches 69431143d5dSDavid Howells * the front of the queue, all the outstanding writes are either 69531143d5dSDavid Howells * completed or rejected */ 69631143d5dSDavid Howells wb = kzalloc(sizeof(*wb), GFP_KERNEL); 69702c24a82SJosef Bacik if (!wb) { 69802c24a82SJosef Bacik ret = -ENOMEM; 69902c24a82SJosef Bacik goto out; 70002c24a82SJosef Bacik } 70131143d5dSDavid Howells wb->vnode = vnode; 70231143d5dSDavid Howells wb->first = 0; 70331143d5dSDavid Howells wb->last = -1; 70431143d5dSDavid Howells wb->offset_first = 0; 70531143d5dSDavid Howells wb->to_last = PAGE_SIZE; 70631143d5dSDavid Howells wb->usage = 1; 70731143d5dSDavid Howells wb->state = AFS_WBACK_SYNCING; 70831143d5dSDavid Howells init_waitqueue_head(&wb->waitq); 70931143d5dSDavid Howells 71031143d5dSDavid Howells spin_lock(&vnode->writeback_lock); 71131143d5dSDavid Howells list_for_each_entry(xwb, &vnode->writebacks, link) { 71231143d5dSDavid Howells if (xwb->state == AFS_WBACK_PENDING) 71331143d5dSDavid Howells xwb->state = AFS_WBACK_CONFLICTING; 71431143d5dSDavid Howells } 71531143d5dSDavid Howells list_add_tail(&wb->link, &vnode->writebacks); 71631143d5dSDavid Howells spin_unlock(&vnode->writeback_lock); 71731143d5dSDavid Howells 71831143d5dSDavid Howells /* push all the outstanding writebacks to the server */ 71931143d5dSDavid Howells ret = afs_writeback_all(vnode); 72031143d5dSDavid Howells if (ret < 0) { 72131143d5dSDavid Howells afs_put_writeback(wb); 72231143d5dSDavid Howells _leave(" = %d [wb]", ret); 72302c24a82SJosef Bacik goto out; 72431143d5dSDavid Howells } 72531143d5dSDavid Howells 72631143d5dSDavid Howells /* wait for the preceding writes to actually complete */ 72731143d5dSDavid Howells ret = wait_event_interruptible(wb->waitq, 72831143d5dSDavid Howells wb->state == AFS_WBACK_COMPLETE || 72931143d5dSDavid Howells vnode->writebacks.next == &wb->link); 73031143d5dSDavid Howells afs_put_writeback(wb); 73131143d5dSDavid Howells _leave(" = %d", ret); 73202c24a82SJosef Bacik out: 7335955102cSAl Viro inode_unlock(inode); 73431143d5dSDavid Howells return ret; 73531143d5dSDavid Howells } 7369b3f26c9SDavid Howells 7379b3f26c9SDavid Howells /* 7389b3f26c9SDavid Howells * notification that a previously read-only page is about to become writable 7399b3f26c9SDavid Howells * - if it returns an error, the caller will deliver a bus error signal 7409b3f26c9SDavid Howells */ 7419b3f26c9SDavid Howells int afs_page_mkwrite(struct vm_area_struct *vma, struct page *page) 7429b3f26c9SDavid Howells { 7439b3f26c9SDavid Howells struct afs_vnode *vnode = AFS_FS_I(vma->vm_file->f_mapping->host); 7449b3f26c9SDavid Howells 7459b3f26c9SDavid Howells _enter("{{%x:%u}},{%lx}", 7469b3f26c9SDavid Howells vnode->fid.vid, vnode->fid.vnode, page->index); 7479b3f26c9SDavid Howells 7489b3f26c9SDavid Howells /* wait for the page to be written to the cache before we allow it to 7499b3f26c9SDavid Howells * be modified */ 7509b3f26c9SDavid Howells #ifdef CONFIG_AFS_FSCACHE 7519b3f26c9SDavid Howells fscache_wait_on_page_write(vnode->cache, page); 7529b3f26c9SDavid Howells #endif 7539b3f26c9SDavid Howells 7549b3f26c9SDavid Howells _leave(" = 0"); 7559b3f26c9SDavid Howells return 0; 7569b3f26c9SDavid Howells } 757