12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 231143d5dSDavid Howells /* handling of writes to regular files and writing back to the server 331143d5dSDavid Howells * 431143d5dSDavid Howells * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. 531143d5dSDavid Howells * Written by David Howells (dhowells@redhat.com) 631143d5dSDavid Howells */ 74343d008SDavid Howells 84af3c9ccSAlexey Dobriyan #include <linux/backing-dev.h> 931143d5dSDavid Howells #include <linux/slab.h> 1031143d5dSDavid Howells #include <linux/fs.h> 1131143d5dSDavid Howells #include <linux/pagemap.h> 1231143d5dSDavid Howells #include <linux/writeback.h> 1331143d5dSDavid Howells #include <linux/pagevec.h> 143003bbd0SDavid Howells #include <linux/netfs.h> 1531143d5dSDavid Howells #include "internal.h" 1631143d5dSDavid Howells 17a9eb558aSDavid Howells static int afs_writepages_region(struct address_space *mapping, 18a9eb558aSDavid Howells struct writeback_control *wbc, 19a9eb558aSDavid Howells loff_t start, loff_t end, loff_t *_next, 20a9eb558aSDavid Howells bool max_one_loop); 21a9eb558aSDavid Howells 22c7f75ef3SDavid Howells static void afs_write_to_cache(struct afs_vnode *vnode, loff_t start, size_t len, 23c7f75ef3SDavid Howells loff_t i_size, bool caching); 24c7f75ef3SDavid Howells 25c7f75ef3SDavid Howells #ifdef CONFIG_AFS_FSCACHE 2631143d5dSDavid Howells /* 27c7f75ef3SDavid Howells * Mark a page as having been made dirty and thus needing writeback. We also 28c7f75ef3SDavid Howells * need to pin the cache object to write back to. 2931143d5dSDavid Howells */ 308fb72b4aSMatthew Wilcox (Oracle) bool afs_dirty_folio(struct address_space *mapping, struct folio *folio) 3131143d5dSDavid Howells { 328fb72b4aSMatthew Wilcox (Oracle) return fscache_dirty_folio(mapping, folio, 338fb72b4aSMatthew Wilcox (Oracle) afs_vnode_cache(AFS_FS_I(mapping->host))); 3431143d5dSDavid Howells } 35c7f75ef3SDavid Howells static void afs_folio_start_fscache(bool caching, struct folio *folio) 36c7f75ef3SDavid Howells { 37c7f75ef3SDavid Howells if (caching) 38c7f75ef3SDavid Howells folio_start_fscache(folio); 39c7f75ef3SDavid Howells } 40c7f75ef3SDavid Howells #else 41c7f75ef3SDavid Howells static void afs_folio_start_fscache(bool caching, struct folio *folio) 42c7f75ef3SDavid Howells { 43c7f75ef3SDavid Howells } 44c7f75ef3SDavid Howells #endif 4531143d5dSDavid Howells 4631143d5dSDavid Howells /* 47a9eb558aSDavid Howells * Flush out a conflicting write. This may extend the write to the surrounding 48a9eb558aSDavid Howells * pages if also dirty and contiguous to the conflicting region.. 49a9eb558aSDavid Howells */ 50a9eb558aSDavid Howells static int afs_flush_conflicting_write(struct address_space *mapping, 51a9eb558aSDavid Howells struct folio *folio) 52a9eb558aSDavid Howells { 53a9eb558aSDavid Howells struct writeback_control wbc = { 54a9eb558aSDavid Howells .sync_mode = WB_SYNC_ALL, 55a9eb558aSDavid Howells .nr_to_write = LONG_MAX, 56a9eb558aSDavid Howells .range_start = folio_pos(folio), 57a9eb558aSDavid Howells .range_end = LLONG_MAX, 58a9eb558aSDavid Howells }; 59a9eb558aSDavid Howells loff_t next; 60a9eb558aSDavid Howells 61a9eb558aSDavid Howells return afs_writepages_region(mapping, &wbc, folio_pos(folio), LLONG_MAX, 62a9eb558aSDavid Howells &next, true); 63a9eb558aSDavid Howells } 64a9eb558aSDavid Howells 65a9eb558aSDavid Howells /* 6631143d5dSDavid Howells * prepare to perform part of a write to a page 6731143d5dSDavid Howells */ 6815b4650eSNick Piggin int afs_write_begin(struct file *file, struct address_space *mapping, 699d6b0cd7SMatthew Wilcox (Oracle) loff_t pos, unsigned len, 7021db2cdcSDavid Howells struct page **_page, void **fsdata) 7131143d5dSDavid Howells { 72496ad9aaSAl Viro struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 7378525c74SDavid Howells struct folio *folio; 744343d008SDavid Howells unsigned long priv; 75e87b03f5SDavid Howells unsigned f, from; 76e87b03f5SDavid Howells unsigned t, to; 77e87b03f5SDavid Howells pgoff_t index; 7831143d5dSDavid Howells int ret; 7931143d5dSDavid Howells 80e87b03f5SDavid Howells _enter("{%llx:%llu},%llx,%x", 81e87b03f5SDavid Howells vnode->fid.vid, vnode->fid.vnode, pos, len); 8231143d5dSDavid Howells 833003bbd0SDavid Howells /* Prefetch area to be written into the cache if we're caching this 843003bbd0SDavid Howells * file. We need to do this before we get a lock on the page in case 853003bbd0SDavid Howells * there's more than one writer competing for the same cache block. 863003bbd0SDavid Howells */ 87e81fb419SLinus Torvalds ret = netfs_write_begin(&vnode->netfs, file, mapping, pos, len, &folio, fsdata); 883003bbd0SDavid Howells if (ret < 0) 8931143d5dSDavid Howells return ret; 90630f5ddaSDavid Howells 9178525c74SDavid Howells index = folio_index(folio); 92e87b03f5SDavid Howells from = pos - index * PAGE_SIZE; 93e87b03f5SDavid Howells to = from + len; 94e87b03f5SDavid Howells 9531143d5dSDavid Howells try_again: 964343d008SDavid Howells /* See if this page is already partially written in a way that we can 974343d008SDavid Howells * merge the new write with. 984343d008SDavid Howells */ 9978525c74SDavid Howells if (folio_test_private(folio)) { 10078525c74SDavid Howells priv = (unsigned long)folio_get_private(folio); 10178525c74SDavid Howells f = afs_folio_dirty_from(folio, priv); 10278525c74SDavid Howells t = afs_folio_dirty_to(folio, priv); 1034343d008SDavid Howells ASSERTCMP(f, <=, t); 10431143d5dSDavid Howells 10578525c74SDavid Howells if (folio_test_writeback(folio)) { 10678525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("alrdy"), folio); 107a9eb558aSDavid Howells folio_unlock(folio); 108a9eb558aSDavid Howells goto wait_for_writeback; 1095a039c32SDavid Howells } 1105a813276SDavid Howells /* If the file is being filled locally, allow inter-write 1115a813276SDavid Howells * spaces to be merged into writes. If it's not, only write 1125a813276SDavid Howells * back what the user gives us. 1135a813276SDavid Howells */ 1145a813276SDavid Howells if (!test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags) && 1155a813276SDavid Howells (to < f || from > t)) 1164343d008SDavid Howells goto flush_conflicting_write; 11731143d5dSDavid Howells } 11831143d5dSDavid Howells 1198549a263SDavid Howells *_page = folio_file_page(folio, pos / PAGE_SIZE); 1204343d008SDavid Howells _leave(" = 0"); 12131143d5dSDavid Howells return 0; 12231143d5dSDavid Howells 1234343d008SDavid Howells /* The previous write and this write aren't adjacent or overlapping, so 1244343d008SDavid Howells * flush the page out. 1254343d008SDavid Howells */ 1264343d008SDavid Howells flush_conflicting_write: 127a9eb558aSDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("confl"), folio); 128a9eb558aSDavid Howells folio_unlock(folio); 129a9eb558aSDavid Howells 130a9eb558aSDavid Howells ret = afs_flush_conflicting_write(mapping, folio); 131a9eb558aSDavid Howells if (ret < 0) 132a9eb558aSDavid Howells goto error; 133a9eb558aSDavid Howells 134a9eb558aSDavid Howells wait_for_writeback: 135a9eb558aSDavid Howells ret = folio_wait_writeback_killable(folio); 13621db2cdcSDavid Howells if (ret < 0) 13721db2cdcSDavid Howells goto error; 13831143d5dSDavid Howells 13978525c74SDavid Howells ret = folio_lock_killable(folio); 14021db2cdcSDavid Howells if (ret < 0) 14121db2cdcSDavid Howells goto error; 14221db2cdcSDavid Howells goto try_again; 14321db2cdcSDavid Howells 14421db2cdcSDavid Howells error: 14578525c74SDavid Howells folio_put(folio); 1464343d008SDavid Howells _leave(" = %d", ret); 1474343d008SDavid Howells return ret; 1484343d008SDavid Howells } 14931143d5dSDavid Howells 15031143d5dSDavid Howells /* 15131143d5dSDavid Howells * finalise part of a write to a page 15231143d5dSDavid Howells */ 15315b4650eSNick Piggin int afs_write_end(struct file *file, struct address_space *mapping, 15415b4650eSNick Piggin loff_t pos, unsigned len, unsigned copied, 15578525c74SDavid Howells struct page *subpage, void *fsdata) 15631143d5dSDavid Howells { 15778525c74SDavid Howells struct folio *folio = page_folio(subpage); 158496ad9aaSAl Viro struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 159f792e3acSDavid Howells unsigned long priv; 16078525c74SDavid Howells unsigned int f, from = offset_in_folio(folio, pos); 161f792e3acSDavid Howells unsigned int t, to = from + copied; 162c7f75ef3SDavid Howells loff_t i_size, write_end_pos; 16331143d5dSDavid Howells 1643b6492dfSDavid Howells _enter("{%llx:%llu},{%lx}", 16578525c74SDavid Howells vnode->fid.vid, vnode->fid.vnode, folio_index(folio)); 16631143d5dSDavid Howells 16778525c74SDavid Howells if (!folio_test_uptodate(folio)) { 16866e9c6a8SDavid Howells if (copied < len) { 16966e9c6a8SDavid Howells copied = 0; 17066e9c6a8SDavid Howells goto out; 17166e9c6a8SDavid Howells } 17266e9c6a8SDavid Howells 17378525c74SDavid Howells folio_mark_uptodate(folio); 17466e9c6a8SDavid Howells } 17566e9c6a8SDavid Howells 1763ad216eeSDavid Howells if (copied == 0) 1773ad216eeSDavid Howells goto out; 1783ad216eeSDavid Howells 179c7f75ef3SDavid Howells write_end_pos = pos + copied; 18031143d5dSDavid Howells 181874c8ca1SDavid Howells i_size = i_size_read(&vnode->netfs.inode); 182c7f75ef3SDavid Howells if (write_end_pos > i_size) { 1831f32ef79SDavid Howells write_seqlock(&vnode->cb_lock); 184874c8ca1SDavid Howells i_size = i_size_read(&vnode->netfs.inode); 185c7f75ef3SDavid Howells if (write_end_pos > i_size) 186c7f75ef3SDavid Howells afs_set_i_size(vnode, write_end_pos); 1871f32ef79SDavid Howells write_sequnlock(&vnode->cb_lock); 188c7f75ef3SDavid Howells fscache_update_cookie(afs_vnode_cache(vnode), NULL, &write_end_pos); 18931143d5dSDavid Howells } 19031143d5dSDavid Howells 19178525c74SDavid Howells if (folio_test_private(folio)) { 19278525c74SDavid Howells priv = (unsigned long)folio_get_private(folio); 19378525c74SDavid Howells f = afs_folio_dirty_from(folio, priv); 19478525c74SDavid Howells t = afs_folio_dirty_to(folio, priv); 195f792e3acSDavid Howells if (from < f) 196f792e3acSDavid Howells f = from; 197f792e3acSDavid Howells if (to > t) 198f792e3acSDavid Howells t = to; 19978525c74SDavid Howells priv = afs_folio_dirty(folio, f, t); 20078525c74SDavid Howells folio_change_private(folio, (void *)priv); 20178525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("dirty+"), folio); 202f792e3acSDavid Howells } else { 20378525c74SDavid Howells priv = afs_folio_dirty(folio, from, to); 20478525c74SDavid Howells folio_attach_private(folio, (void *)priv); 20578525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("dirty"), folio); 206f792e3acSDavid Howells } 207f792e3acSDavid Howells 20878525c74SDavid Howells if (folio_mark_dirty(folio)) 20978525c74SDavid Howells _debug("dirtied %lx", folio_index(folio)); 210afae457dSDavid Howells 211afae457dSDavid Howells out: 21278525c74SDavid Howells folio_unlock(folio); 21378525c74SDavid Howells folio_put(folio); 2143003bbd0SDavid Howells return copied; 21531143d5dSDavid Howells } 21631143d5dSDavid Howells 21731143d5dSDavid Howells /* 21831143d5dSDavid Howells * kill all the pages in the given range 21931143d5dSDavid Howells */ 2204343d008SDavid Howells static void afs_kill_pages(struct address_space *mapping, 221e87b03f5SDavid Howells loff_t start, loff_t len) 22231143d5dSDavid Howells { 2234343d008SDavid Howells struct afs_vnode *vnode = AFS_FS_I(mapping->host); 22478525c74SDavid Howells struct folio *folio; 22578525c74SDavid Howells pgoff_t index = start / PAGE_SIZE; 22678525c74SDavid Howells pgoff_t last = (start + len - 1) / PAGE_SIZE, next; 22731143d5dSDavid Howells 228e87b03f5SDavid Howells _enter("{%llx:%llu},%llx @%llx", 229e87b03f5SDavid Howells vnode->fid.vid, vnode->fid.vnode, len, start); 23031143d5dSDavid Howells 23131143d5dSDavid Howells do { 23278525c74SDavid Howells _debug("kill %lx (to %lx)", index, last); 23331143d5dSDavid Howells 23478525c74SDavid Howells folio = filemap_get_folio(mapping, index); 23578525c74SDavid Howells if (!folio) { 23678525c74SDavid Howells next = index + 1; 23778525c74SDavid Howells continue; 2384343d008SDavid Howells } 2394343d008SDavid Howells 24078525c74SDavid Howells next = folio_next_index(folio); 24178525c74SDavid Howells 24278525c74SDavid Howells folio_clear_uptodate(folio); 24378525c74SDavid Howells folio_end_writeback(folio); 24478525c74SDavid Howells folio_lock(folio); 24578525c74SDavid Howells generic_error_remove_page(mapping, &folio->page); 24678525c74SDavid Howells folio_unlock(folio); 24778525c74SDavid Howells folio_put(folio); 24878525c74SDavid Howells 24978525c74SDavid Howells } while (index = next, index <= last); 2504343d008SDavid Howells 2514343d008SDavid Howells _leave(""); 2524343d008SDavid Howells } 2534343d008SDavid Howells 2544343d008SDavid Howells /* 2554343d008SDavid Howells * Redirty all the pages in a given range. 2564343d008SDavid Howells */ 2574343d008SDavid Howells static void afs_redirty_pages(struct writeback_control *wbc, 2584343d008SDavid Howells struct address_space *mapping, 259e87b03f5SDavid Howells loff_t start, loff_t len) 2604343d008SDavid Howells { 2614343d008SDavid Howells struct afs_vnode *vnode = AFS_FS_I(mapping->host); 26278525c74SDavid Howells struct folio *folio; 26378525c74SDavid Howells pgoff_t index = start / PAGE_SIZE; 26478525c74SDavid Howells pgoff_t last = (start + len - 1) / PAGE_SIZE, next; 2654343d008SDavid Howells 266e87b03f5SDavid Howells _enter("{%llx:%llu},%llx @%llx", 267e87b03f5SDavid Howells vnode->fid.vid, vnode->fid.vnode, len, start); 2684343d008SDavid Howells 2694343d008SDavid Howells do { 270e87b03f5SDavid Howells _debug("redirty %llx @%llx", len, start); 2714343d008SDavid Howells 27278525c74SDavid Howells folio = filemap_get_folio(mapping, index); 27378525c74SDavid Howells if (!folio) { 27478525c74SDavid Howells next = index + 1; 27578525c74SDavid Howells continue; 27631143d5dSDavid Howells } 27731143d5dSDavid Howells 27878525c74SDavid Howells next = index + folio_nr_pages(folio); 27978525c74SDavid Howells folio_redirty_for_writepage(wbc, folio); 28078525c74SDavid Howells folio_end_writeback(folio); 28178525c74SDavid Howells folio_put(folio); 28278525c74SDavid Howells } while (index = next, index <= last); 28331143d5dSDavid Howells 28431143d5dSDavid Howells _leave(""); 28531143d5dSDavid Howells } 28631143d5dSDavid Howells 28731143d5dSDavid Howells /* 288a58823acSDavid Howells * completion of write to server 289a58823acSDavid Howells */ 290e87b03f5SDavid Howells static void afs_pages_written_back(struct afs_vnode *vnode, loff_t start, unsigned int len) 291a58823acSDavid Howells { 292874c8ca1SDavid Howells struct address_space *mapping = vnode->netfs.inode.i_mapping; 29378525c74SDavid Howells struct folio *folio; 294e87b03f5SDavid Howells pgoff_t end; 295bd80d8a8SDavid Howells 296e87b03f5SDavid Howells XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE); 297a58823acSDavid Howells 298e87b03f5SDavid Howells _enter("{%llx:%llu},{%x @%llx}", 299e87b03f5SDavid Howells vnode->fid.vid, vnode->fid.vnode, len, start); 300a58823acSDavid Howells 301bd80d8a8SDavid Howells rcu_read_lock(); 302a58823acSDavid Howells 303e87b03f5SDavid Howells end = (start + len - 1) / PAGE_SIZE; 30478525c74SDavid Howells xas_for_each(&xas, folio, end) { 30578525c74SDavid Howells if (!folio_test_writeback(folio)) { 30678525c74SDavid Howells kdebug("bad %x @%llx page %lx %lx", 30778525c74SDavid Howells len, start, folio_index(folio), end); 30878525c74SDavid Howells ASSERT(folio_test_writeback(folio)); 309e87b03f5SDavid Howells } 310a58823acSDavid Howells 31178525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("clear"), folio); 31278525c74SDavid Howells folio_detach_private(folio); 31378525c74SDavid Howells folio_end_writeback(folio); 314a58823acSDavid Howells } 315bd80d8a8SDavid Howells 316bd80d8a8SDavid Howells rcu_read_unlock(); 317a58823acSDavid Howells 318a58823acSDavid Howells afs_prune_wb_keys(vnode); 319a58823acSDavid Howells _leave(""); 320a58823acSDavid Howells } 321a58823acSDavid Howells 322a58823acSDavid Howells /* 323e49c7b2fSDavid Howells * Find a key to use for the writeback. We cached the keys used to author the 324e49c7b2fSDavid Howells * writes on the vnode. *_wbk will contain the last writeback key used or NULL 325e49c7b2fSDavid Howells * and we need to start from there if it's set. 326e49c7b2fSDavid Howells */ 327e49c7b2fSDavid Howells static int afs_get_writeback_key(struct afs_vnode *vnode, 328e49c7b2fSDavid Howells struct afs_wb_key **_wbk) 329e49c7b2fSDavid Howells { 330e49c7b2fSDavid Howells struct afs_wb_key *wbk = NULL; 331e49c7b2fSDavid Howells struct list_head *p; 332e49c7b2fSDavid Howells int ret = -ENOKEY, ret2; 333e49c7b2fSDavid Howells 334e49c7b2fSDavid Howells spin_lock(&vnode->wb_lock); 335e49c7b2fSDavid Howells if (*_wbk) 336e49c7b2fSDavid Howells p = (*_wbk)->vnode_link.next; 337e49c7b2fSDavid Howells else 338e49c7b2fSDavid Howells p = vnode->wb_keys.next; 339e49c7b2fSDavid Howells 340e49c7b2fSDavid Howells while (p != &vnode->wb_keys) { 341e49c7b2fSDavid Howells wbk = list_entry(p, struct afs_wb_key, vnode_link); 342e49c7b2fSDavid Howells _debug("wbk %u", key_serial(wbk->key)); 343e49c7b2fSDavid Howells ret2 = key_validate(wbk->key); 344e49c7b2fSDavid Howells if (ret2 == 0) { 345e49c7b2fSDavid Howells refcount_inc(&wbk->usage); 346e49c7b2fSDavid Howells _debug("USE WB KEY %u", key_serial(wbk->key)); 347e49c7b2fSDavid Howells break; 348e49c7b2fSDavid Howells } 349e49c7b2fSDavid Howells 350e49c7b2fSDavid Howells wbk = NULL; 351e49c7b2fSDavid Howells if (ret == -ENOKEY) 352e49c7b2fSDavid Howells ret = ret2; 353e49c7b2fSDavid Howells p = p->next; 354e49c7b2fSDavid Howells } 355e49c7b2fSDavid Howells 356e49c7b2fSDavid Howells spin_unlock(&vnode->wb_lock); 357e49c7b2fSDavid Howells if (*_wbk) 358e49c7b2fSDavid Howells afs_put_wb_key(*_wbk); 359e49c7b2fSDavid Howells *_wbk = wbk; 360e49c7b2fSDavid Howells return 0; 361e49c7b2fSDavid Howells } 362e49c7b2fSDavid Howells 363e49c7b2fSDavid Howells static void afs_store_data_success(struct afs_operation *op) 364e49c7b2fSDavid Howells { 365e49c7b2fSDavid Howells struct afs_vnode *vnode = op->file[0].vnode; 366e49c7b2fSDavid Howells 367da8d0755SDavid Howells op->ctime = op->file[0].scb.status.mtime_client; 368e49c7b2fSDavid Howells afs_vnode_commit_status(op, &op->file[0]); 369e49c7b2fSDavid Howells if (op->error == 0) { 370d383e346SDavid Howells if (!op->store.laundering) 371e87b03f5SDavid Howells afs_pages_written_back(vnode, op->store.pos, op->store.size); 372e49c7b2fSDavid Howells afs_stat_v(vnode, n_stores); 373bd80d8a8SDavid Howells atomic_long_add(op->store.size, &afs_v2net(vnode)->n_store_bytes); 374e49c7b2fSDavid Howells } 375e49c7b2fSDavid Howells } 376e49c7b2fSDavid Howells 377e49c7b2fSDavid Howells static const struct afs_operation_ops afs_store_data_operation = { 378e49c7b2fSDavid Howells .issue_afs_rpc = afs_fs_store_data, 379e49c7b2fSDavid Howells .issue_yfs_rpc = yfs_fs_store_data, 380e49c7b2fSDavid Howells .success = afs_store_data_success, 381e49c7b2fSDavid Howells }; 382e49c7b2fSDavid Howells 383e49c7b2fSDavid Howells /* 384d2ddc776SDavid Howells * write to a file 38531143d5dSDavid Howells */ 386e87b03f5SDavid Howells static int afs_store_data(struct afs_vnode *vnode, struct iov_iter *iter, loff_t pos, 387bd80d8a8SDavid Howells bool laundering) 38831143d5dSDavid Howells { 389e49c7b2fSDavid Howells struct afs_operation *op; 3904343d008SDavid Howells struct afs_wb_key *wbk = NULL; 391ab487a4cSDavid Howells loff_t size = iov_iter_count(iter); 392bd80d8a8SDavid Howells int ret = -ENOKEY; 393d2ddc776SDavid Howells 394bd80d8a8SDavid Howells _enter("%s{%llx:%llu.%u},%llx,%llx", 395d2ddc776SDavid Howells vnode->volume->name, 396d2ddc776SDavid Howells vnode->fid.vid, 397d2ddc776SDavid Howells vnode->fid.vnode, 398d2ddc776SDavid Howells vnode->fid.unique, 399bd80d8a8SDavid Howells size, pos); 400d2ddc776SDavid Howells 401e49c7b2fSDavid Howells ret = afs_get_writeback_key(vnode, &wbk); 402e49c7b2fSDavid Howells if (ret) { 4034343d008SDavid Howells _leave(" = %d [no keys]", ret); 4044343d008SDavid Howells return ret; 405d2ddc776SDavid Howells } 406d2ddc776SDavid Howells 407e49c7b2fSDavid Howells op = afs_alloc_operation(wbk->key, vnode->volume); 408e49c7b2fSDavid Howells if (IS_ERR(op)) { 409e49c7b2fSDavid Howells afs_put_wb_key(wbk); 410e49c7b2fSDavid Howells return -ENOMEM; 411d2ddc776SDavid Howells } 412d2ddc776SDavid Howells 413e49c7b2fSDavid Howells afs_op_set_vnode(op, 0, vnode); 414e49c7b2fSDavid Howells op->file[0].dv_delta = 1; 41522650f14SDavid Howells op->file[0].modification = true; 416bd80d8a8SDavid Howells op->store.write_iter = iter; 417bd80d8a8SDavid Howells op->store.pos = pos; 418bd80d8a8SDavid Howells op->store.size = size; 419874c8ca1SDavid Howells op->store.i_size = max(pos + size, vnode->netfs.remote_i_size); 420d383e346SDavid Howells op->store.laundering = laundering; 421874c8ca1SDavid Howells op->mtime = vnode->netfs.inode.i_mtime; 422811f04baSDavid Howells op->flags |= AFS_OPERATION_UNINTR; 423e49c7b2fSDavid Howells op->ops = &afs_store_data_operation; 424e49c7b2fSDavid Howells 425e49c7b2fSDavid Howells try_next_key: 426e49c7b2fSDavid Howells afs_begin_vnode_operation(op); 427e49c7b2fSDavid Howells afs_wait_for_operation(op); 428e49c7b2fSDavid Howells 429e49c7b2fSDavid Howells switch (op->error) { 4304343d008SDavid Howells case -EACCES: 4314343d008SDavid Howells case -EPERM: 4324343d008SDavid Howells case -ENOKEY: 4334343d008SDavid Howells case -EKEYEXPIRED: 4344343d008SDavid Howells case -EKEYREJECTED: 4354343d008SDavid Howells case -EKEYREVOKED: 4364343d008SDavid Howells _debug("next"); 437e49c7b2fSDavid Howells 438e49c7b2fSDavid Howells ret = afs_get_writeback_key(vnode, &wbk); 439e49c7b2fSDavid Howells if (ret == 0) { 440e49c7b2fSDavid Howells key_put(op->key); 441e49c7b2fSDavid Howells op->key = key_get(wbk->key); 4424343d008SDavid Howells goto try_next_key; 4434343d008SDavid Howells } 444e49c7b2fSDavid Howells break; 445e49c7b2fSDavid Howells } 4464343d008SDavid Howells 4474343d008SDavid Howells afs_put_wb_key(wbk); 448e49c7b2fSDavid Howells _leave(" = %d", op->error); 449e49c7b2fSDavid Howells return afs_put_operation(op); 450d2ddc776SDavid Howells } 451d2ddc776SDavid Howells 452d2ddc776SDavid Howells /* 453810caa3eSDavid Howells * Extend the region to be written back to include subsequent contiguously 454810caa3eSDavid Howells * dirty pages if possible, but don't sleep while doing so. 455810caa3eSDavid Howells * 456810caa3eSDavid Howells * If this page holds new content, then we can include filler zeros in the 457810caa3eSDavid Howells * writeback. 45831143d5dSDavid Howells */ 459810caa3eSDavid Howells static void afs_extend_writeback(struct address_space *mapping, 460810caa3eSDavid Howells struct afs_vnode *vnode, 461810caa3eSDavid Howells long *_count, 462e87b03f5SDavid Howells loff_t start, 463e87b03f5SDavid Howells loff_t max_len, 464e87b03f5SDavid Howells bool new_content, 465c7f75ef3SDavid Howells bool caching, 466e87b03f5SDavid Howells unsigned int *_len) 46731143d5dSDavid Howells { 468e87b03f5SDavid Howells struct pagevec pvec; 46978525c74SDavid Howells struct folio *folio; 470e87b03f5SDavid Howells unsigned long priv; 471e87b03f5SDavid Howells unsigned int psize, filler = 0; 472e87b03f5SDavid Howells unsigned int f, t; 473e87b03f5SDavid Howells loff_t len = *_len; 474e87b03f5SDavid Howells pgoff_t index = (start + len) / PAGE_SIZE; 475e87b03f5SDavid Howells bool stop = true; 476e87b03f5SDavid Howells unsigned int i; 4774343d008SDavid Howells 478e87b03f5SDavid Howells XA_STATE(xas, &mapping->i_pages, index); 479e87b03f5SDavid Howells pagevec_init(&pvec); 480e87b03f5SDavid Howells 48131143d5dSDavid Howells do { 482e87b03f5SDavid Howells /* Firstly, we gather up a batch of contiguous dirty pages 483e87b03f5SDavid Howells * under the RCU read lock - but we can't clear the dirty flags 484e87b03f5SDavid Howells * there if any of those pages are mapped. 485e87b03f5SDavid Howells */ 486e87b03f5SDavid Howells rcu_read_lock(); 487e87b03f5SDavid Howells 48878525c74SDavid Howells xas_for_each(&xas, folio, ULONG_MAX) { 489e87b03f5SDavid Howells stop = true; 49078525c74SDavid Howells if (xas_retry(&xas, folio)) 491e87b03f5SDavid Howells continue; 49278525c74SDavid Howells if (xa_is_value(folio)) 493e87b03f5SDavid Howells break; 49478525c74SDavid Howells if (folio_index(folio) != index) 495e87b03f5SDavid Howells break; 496e87b03f5SDavid Howells 49778525c74SDavid Howells if (!folio_try_get_rcu(folio)) { 498e87b03f5SDavid Howells xas_reset(&xas); 499e87b03f5SDavid Howells continue; 50031143d5dSDavid Howells } 50131143d5dSDavid Howells 502e87b03f5SDavid Howells /* Has the page moved or been split? */ 50378525c74SDavid Howells if (unlikely(folio != xas_reload(&xas))) { 50478525c74SDavid Howells folio_put(folio); 5055a813276SDavid Howells break; 506581b2027SDavid Howells } 507e87b03f5SDavid Howells 50878525c74SDavid Howells if (!folio_trylock(folio)) { 50978525c74SDavid Howells folio_put(folio); 51031143d5dSDavid Howells break; 511581b2027SDavid Howells } 512c7f75ef3SDavid Howells if (!folio_test_dirty(folio) || 513c7f75ef3SDavid Howells folio_test_writeback(folio) || 514c7f75ef3SDavid Howells folio_test_fscache(folio)) { 51578525c74SDavid Howells folio_unlock(folio); 51678525c74SDavid Howells folio_put(folio); 51731143d5dSDavid Howells break; 51831143d5dSDavid Howells } 5194343d008SDavid Howells 52078525c74SDavid Howells psize = folio_size(folio); 52178525c74SDavid Howells priv = (unsigned long)folio_get_private(folio); 52278525c74SDavid Howells f = afs_folio_dirty_from(folio, priv); 52378525c74SDavid Howells t = afs_folio_dirty_to(folio, priv); 524810caa3eSDavid Howells if (f != 0 && !new_content) { 52578525c74SDavid Howells folio_unlock(folio); 52678525c74SDavid Howells folio_put(folio); 5274343d008SDavid Howells break; 5284343d008SDavid Howells } 5294343d008SDavid Howells 530e87b03f5SDavid Howells len += filler + t; 531e87b03f5SDavid Howells filler = psize - t; 532e87b03f5SDavid Howells if (len >= max_len || *_count <= 0) 533e87b03f5SDavid Howells stop = true; 534e87b03f5SDavid Howells else if (t == psize || new_content) 535e87b03f5SDavid Howells stop = false; 536e87b03f5SDavid Howells 53778525c74SDavid Howells index += folio_nr_pages(folio); 53878525c74SDavid Howells if (!pagevec_add(&pvec, &folio->page)) 539e87b03f5SDavid Howells break; 540e87b03f5SDavid Howells if (stop) 541e87b03f5SDavid Howells break; 542e87b03f5SDavid Howells } 543e87b03f5SDavid Howells 544e87b03f5SDavid Howells if (!stop) 545e87b03f5SDavid Howells xas_pause(&xas); 546e87b03f5SDavid Howells rcu_read_unlock(); 547e87b03f5SDavid Howells 548e87b03f5SDavid Howells /* Now, if we obtained any pages, we can shift them to being 549e87b03f5SDavid Howells * writable and mark them for caching. 550e87b03f5SDavid Howells */ 551e87b03f5SDavid Howells if (!pagevec_count(&pvec)) 552e87b03f5SDavid Howells break; 553e87b03f5SDavid Howells 554e87b03f5SDavid Howells for (i = 0; i < pagevec_count(&pvec); i++) { 55578525c74SDavid Howells folio = page_folio(pvec.pages[i]); 55678525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("store+"), folio); 55713524ab3SDavid Howells 55878525c74SDavid Howells if (!folio_clear_dirty_for_io(folio)) 55931143d5dSDavid Howells BUG(); 56078525c74SDavid Howells if (folio_start_writeback(folio)) 56131143d5dSDavid Howells BUG(); 562c7f75ef3SDavid Howells afs_folio_start_fscache(caching, folio); 563e87b03f5SDavid Howells 56478525c74SDavid Howells *_count -= folio_nr_pages(folio); 56578525c74SDavid Howells folio_unlock(folio); 56631143d5dSDavid Howells } 56731143d5dSDavid Howells 568e87b03f5SDavid Howells pagevec_release(&pvec); 569e87b03f5SDavid Howells cond_resched(); 570e87b03f5SDavid Howells } while (!stop); 57131143d5dSDavid Howells 572e87b03f5SDavid Howells *_len = len; 573810caa3eSDavid Howells } 574810caa3eSDavid Howells 575810caa3eSDavid Howells /* 576810caa3eSDavid Howells * Synchronously write back the locked page and any subsequent non-locked dirty 577810caa3eSDavid Howells * pages. 578810caa3eSDavid Howells */ 57978525c74SDavid Howells static ssize_t afs_write_back_from_locked_folio(struct address_space *mapping, 580810caa3eSDavid Howells struct writeback_control *wbc, 58178525c74SDavid Howells struct folio *folio, 582e87b03f5SDavid Howells loff_t start, loff_t end) 583810caa3eSDavid Howells { 584810caa3eSDavid Howells struct afs_vnode *vnode = AFS_FS_I(mapping->host); 585810caa3eSDavid Howells struct iov_iter iter; 586e87b03f5SDavid Howells unsigned long priv; 587e87b03f5SDavid Howells unsigned int offset, to, len, max_len; 588874c8ca1SDavid Howells loff_t i_size = i_size_read(&vnode->netfs.inode); 589810caa3eSDavid Howells bool new_content = test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags); 590c7f75ef3SDavid Howells bool caching = fscache_cookie_enabled(afs_vnode_cache(vnode)); 591e87b03f5SDavid Howells long count = wbc->nr_to_write; 592810caa3eSDavid Howells int ret; 593810caa3eSDavid Howells 59478525c74SDavid Howells _enter(",%lx,%llx-%llx", folio_index(folio), start, end); 595810caa3eSDavid Howells 59678525c74SDavid Howells if (folio_start_writeback(folio)) 597810caa3eSDavid Howells BUG(); 598c7f75ef3SDavid Howells afs_folio_start_fscache(caching, folio); 599810caa3eSDavid Howells 60078525c74SDavid Howells count -= folio_nr_pages(folio); 601e87b03f5SDavid Howells 602810caa3eSDavid Howells /* Find all consecutive lockable dirty pages that have contiguous 603810caa3eSDavid Howells * written regions, stopping when we find a page that is not 604810caa3eSDavid Howells * immediately lockable, is not dirty or is missing, or we reach the 605810caa3eSDavid Howells * end of the range. 606810caa3eSDavid Howells */ 60778525c74SDavid Howells priv = (unsigned long)folio_get_private(folio); 60878525c74SDavid Howells offset = afs_folio_dirty_from(folio, priv); 60978525c74SDavid Howells to = afs_folio_dirty_to(folio, priv); 61078525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("store"), folio); 611810caa3eSDavid Howells 612e87b03f5SDavid Howells len = to - offset; 613e87b03f5SDavid Howells start += offset; 614e87b03f5SDavid Howells if (start < i_size) { 615e87b03f5SDavid Howells /* Trim the write to the EOF; the extra data is ignored. Also 616e87b03f5SDavid Howells * put an upper limit on the size of a single storedata op. 617e87b03f5SDavid Howells */ 618e87b03f5SDavid Howells max_len = 65536 * 4096; 619e87b03f5SDavid Howells max_len = min_t(unsigned long long, max_len, end - start + 1); 620e87b03f5SDavid Howells max_len = min_t(unsigned long long, max_len, i_size - start); 621810caa3eSDavid Howells 622e87b03f5SDavid Howells if (len < max_len && 62378525c74SDavid Howells (to == folio_size(folio) || new_content)) 624e87b03f5SDavid Howells afs_extend_writeback(mapping, vnode, &count, 625c7f75ef3SDavid Howells start, max_len, new_content, 626c7f75ef3SDavid Howells caching, &len); 627e87b03f5SDavid Howells len = min_t(loff_t, len, max_len); 628e87b03f5SDavid Howells } 629810caa3eSDavid Howells 6304343d008SDavid Howells /* We now have a contiguous set of dirty pages, each with writeback 6314343d008SDavid Howells * set; the first page is still locked at this point, but all the rest 6324343d008SDavid Howells * have been unlocked. 6334343d008SDavid Howells */ 63478525c74SDavid Howells folio_unlock(folio); 6354343d008SDavid Howells 636e87b03f5SDavid Howells if (start < i_size) { 637e87b03f5SDavid Howells _debug("write back %x @%llx [%llx]", len, start, i_size); 63831143d5dSDavid Howells 639c7f75ef3SDavid Howells /* Speculatively write to the cache. We have to fix this up 640c7f75ef3SDavid Howells * later if the store fails. 641c7f75ef3SDavid Howells */ 642c7f75ef3SDavid Howells afs_write_to_cache(vnode, start, len, i_size, caching); 643c7f75ef3SDavid Howells 644de4eda9dSAl Viro iov_iter_xarray(&iter, ITER_SOURCE, &mapping->i_pages, start, len); 645e87b03f5SDavid Howells ret = afs_store_data(vnode, &iter, start, false); 646bd80d8a8SDavid Howells } else { 647e87b03f5SDavid Howells _debug("write discard %x @%llx [%llx]", len, start, i_size); 648e87b03f5SDavid Howells 649bd80d8a8SDavid Howells /* The dirty region was entirely beyond the EOF. */ 6502c547f29SYue Hu fscache_clear_page_bits(mapping, start, len, caching); 651e87b03f5SDavid Howells afs_pages_written_back(vnode, start, len); 652bd80d8a8SDavid Howells ret = 0; 653bd80d8a8SDavid Howells } 654bd80d8a8SDavid Howells 65531143d5dSDavid Howells switch (ret) { 6564343d008SDavid Howells case 0: 657e87b03f5SDavid Howells wbc->nr_to_write = count; 658e87b03f5SDavid Howells ret = len; 6594343d008SDavid Howells break; 6604343d008SDavid Howells 6614343d008SDavid Howells default: 6624343d008SDavid Howells pr_notice("kAFS: Unexpected error from FS.StoreData %d\n", ret); 663df561f66SGustavo A. R. Silva fallthrough; 6644343d008SDavid Howells case -EACCES: 6654343d008SDavid Howells case -EPERM: 6664343d008SDavid Howells case -ENOKEY: 6674343d008SDavid Howells case -EKEYEXPIRED: 6684343d008SDavid Howells case -EKEYREJECTED: 6694343d008SDavid Howells case -EKEYREVOKED: 670adc9613fSDavid Howells case -ENETRESET: 671e87b03f5SDavid Howells afs_redirty_pages(wbc, mapping, start, len); 6724343d008SDavid Howells mapping_set_error(mapping, ret); 6734343d008SDavid Howells break; 6744343d008SDavid Howells 67531143d5dSDavid Howells case -EDQUOT: 67631143d5dSDavid Howells case -ENOSPC: 677e87b03f5SDavid Howells afs_redirty_pages(wbc, mapping, start, len); 6784343d008SDavid Howells mapping_set_error(mapping, -ENOSPC); 67931143d5dSDavid Howells break; 6804343d008SDavid Howells 68131143d5dSDavid Howells case -EROFS: 68231143d5dSDavid Howells case -EIO: 68331143d5dSDavid Howells case -EREMOTEIO: 68431143d5dSDavid Howells case -EFBIG: 68531143d5dSDavid Howells case -ENOENT: 68631143d5dSDavid Howells case -ENOMEDIUM: 68731143d5dSDavid Howells case -ENXIO: 688f51375cdSDavid Howells trace_afs_file_error(vnode, ret, afs_file_error_writeback_fail); 689e87b03f5SDavid Howells afs_kill_pages(mapping, start, len); 6904343d008SDavid Howells mapping_set_error(mapping, ret); 69131143d5dSDavid Howells break; 69231143d5dSDavid Howells } 69331143d5dSDavid Howells 69431143d5dSDavid Howells _leave(" = %d", ret); 69531143d5dSDavid Howells return ret; 69631143d5dSDavid Howells } 69731143d5dSDavid Howells 69831143d5dSDavid Howells /* 69931143d5dSDavid Howells * write a region of pages back to the server 70031143d5dSDavid Howells */ 701c1206a2cSAdrian Bunk static int afs_writepages_region(struct address_space *mapping, 70231143d5dSDavid Howells struct writeback_control *wbc, 703a9eb558aSDavid Howells loff_t start, loff_t end, loff_t *_next, 704a9eb558aSDavid Howells bool max_one_loop) 70531143d5dSDavid Howells { 70678525c74SDavid Howells struct folio *folio; 70778525c74SDavid Howells struct page *head_page; 708e87b03f5SDavid Howells ssize_t ret; 709173ce1caSDavid Howells int n, skips = 0; 71031143d5dSDavid Howells 711e87b03f5SDavid Howells _enter("%llx,%llx,", start, end); 71231143d5dSDavid Howells 71331143d5dSDavid Howells do { 714e87b03f5SDavid Howells pgoff_t index = start / PAGE_SIZE; 715e87b03f5SDavid Howells 716e87b03f5SDavid Howells n = find_get_pages_range_tag(mapping, &index, end / PAGE_SIZE, 71778525c74SDavid Howells PAGECACHE_TAG_DIRTY, 1, &head_page); 71831143d5dSDavid Howells if (!n) 71931143d5dSDavid Howells break; 72031143d5dSDavid Howells 72178525c74SDavid Howells folio = page_folio(head_page); 72278525c74SDavid Howells start = folio_pos(folio); /* May regress with THPs */ 723e87b03f5SDavid Howells 72478525c74SDavid Howells _debug("wback %lx", folio_index(folio)); 72531143d5dSDavid Howells 726e87b03f5SDavid Howells /* At this point we hold neither the i_pages lock nor the 727b93b0163SMatthew Wilcox * page lock: the page may be truncated or invalidated 728b93b0163SMatthew Wilcox * (changing page->mapping to NULL), or even swizzled 729b93b0163SMatthew Wilcox * back from swapper_space to tmpfs file mapping 73031143d5dSDavid Howells */ 731e87b03f5SDavid Howells if (wbc->sync_mode != WB_SYNC_NONE) { 73278525c74SDavid Howells ret = folio_lock_killable(folio); 7334343d008SDavid Howells if (ret < 0) { 73478525c74SDavid Howells folio_put(folio); 7354343d008SDavid Howells return ret; 7364343d008SDavid Howells } 737e87b03f5SDavid Howells } else { 73878525c74SDavid Howells if (!folio_trylock(folio)) { 73978525c74SDavid Howells folio_put(folio); 740e87b03f5SDavid Howells return 0; 741e87b03f5SDavid Howells } 742e87b03f5SDavid Howells } 74331143d5dSDavid Howells 74478525c74SDavid Howells if (folio_mapping(folio) != mapping || 74578525c74SDavid Howells !folio_test_dirty(folio)) { 74678525c74SDavid Howells start += folio_size(folio); 74778525c74SDavid Howells folio_unlock(folio); 74878525c74SDavid Howells folio_put(folio); 74931143d5dSDavid Howells continue; 75031143d5dSDavid Howells } 75131143d5dSDavid Howells 752c7f75ef3SDavid Howells if (folio_test_writeback(folio) || 753c7f75ef3SDavid Howells folio_test_fscache(folio)) { 75478525c74SDavid Howells folio_unlock(folio); 755c7f75ef3SDavid Howells if (wbc->sync_mode != WB_SYNC_NONE) { 75678525c74SDavid Howells folio_wait_writeback(folio); 757c7f75ef3SDavid Howells #ifdef CONFIG_AFS_FSCACHE 758c7f75ef3SDavid Howells folio_wait_fscache(folio); 759c7f75ef3SDavid Howells #endif 760173ce1caSDavid Howells } else { 761173ce1caSDavid Howells start += folio_size(folio); 762c7f75ef3SDavid Howells } 76378525c74SDavid Howells folio_put(folio); 764173ce1caSDavid Howells if (wbc->sync_mode == WB_SYNC_NONE) { 765173ce1caSDavid Howells if (skips >= 5 || need_resched()) 766173ce1caSDavid Howells break; 767173ce1caSDavid Howells skips++; 768173ce1caSDavid Howells } 76931143d5dSDavid Howells continue; 77031143d5dSDavid Howells } 77131143d5dSDavid Howells 77278525c74SDavid Howells if (!folio_clear_dirty_for_io(folio)) 77365a15109SDavid Howells BUG(); 77478525c74SDavid Howells ret = afs_write_back_from_locked_folio(mapping, wbc, folio, start, end); 77578525c74SDavid Howells folio_put(folio); 77631143d5dSDavid Howells if (ret < 0) { 777e87b03f5SDavid Howells _leave(" = %zd", ret); 77831143d5dSDavid Howells return ret; 77931143d5dSDavid Howells } 78031143d5dSDavid Howells 781dc255730SMarc Dionne start += ret; 78231143d5dSDavid Howells 783a9eb558aSDavid Howells if (max_one_loop) 784a9eb558aSDavid Howells break; 785a9eb558aSDavid Howells 78631143d5dSDavid Howells cond_resched(); 787e87b03f5SDavid Howells } while (wbc->nr_to_write > 0); 78831143d5dSDavid Howells 789e87b03f5SDavid Howells *_next = start; 790e87b03f5SDavid Howells _leave(" = 0 [%llx]", *_next); 79131143d5dSDavid Howells return 0; 79231143d5dSDavid Howells } 79331143d5dSDavid Howells 79431143d5dSDavid Howells /* 79531143d5dSDavid Howells * write some of the pending data back to the server 79631143d5dSDavid Howells */ 79731143d5dSDavid Howells int afs_writepages(struct address_space *mapping, 79831143d5dSDavid Howells struct writeback_control *wbc) 79931143d5dSDavid Howells { 800ec0fa0b6SDavid Howells struct afs_vnode *vnode = AFS_FS_I(mapping->host); 801e87b03f5SDavid Howells loff_t start, next; 80231143d5dSDavid Howells int ret; 80331143d5dSDavid Howells 80431143d5dSDavid Howells _enter(""); 80531143d5dSDavid Howells 806ec0fa0b6SDavid Howells /* We have to be careful as we can end up racing with setattr() 807ec0fa0b6SDavid Howells * truncating the pagecache since the caller doesn't take a lock here 808ec0fa0b6SDavid Howells * to prevent it. 809ec0fa0b6SDavid Howells */ 810ec0fa0b6SDavid Howells if (wbc->sync_mode == WB_SYNC_ALL) 811ec0fa0b6SDavid Howells down_read(&vnode->validate_lock); 812ec0fa0b6SDavid Howells else if (!down_read_trylock(&vnode->validate_lock)) 813ec0fa0b6SDavid Howells return 0; 814ec0fa0b6SDavid Howells 81531143d5dSDavid Howells if (wbc->range_cyclic) { 816e87b03f5SDavid Howells start = mapping->writeback_index * PAGE_SIZE; 817a9eb558aSDavid Howells ret = afs_writepages_region(mapping, wbc, start, LLONG_MAX, 818a9eb558aSDavid Howells &next, false); 819afe69498STom Rix if (ret == 0) { 820e87b03f5SDavid Howells mapping->writeback_index = next / PAGE_SIZE; 821afe69498STom Rix if (start > 0 && wbc->nr_to_write > 0) { 822afe69498STom Rix ret = afs_writepages_region(mapping, wbc, 0, 823a9eb558aSDavid Howells start, &next, false); 824afe69498STom Rix if (ret == 0) 825afe69498STom Rix mapping->writeback_index = 826afe69498STom Rix next / PAGE_SIZE; 827afe69498STom Rix } 828afe69498STom Rix } 82931143d5dSDavid Howells } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) { 830a9eb558aSDavid Howells ret = afs_writepages_region(mapping, wbc, 0, LLONG_MAX, 831a9eb558aSDavid Howells &next, false); 832afe69498STom Rix if (wbc->nr_to_write > 0 && ret == 0) 8335a972474SDavid Howells mapping->writeback_index = next / PAGE_SIZE; 83431143d5dSDavid Howells } else { 835e87b03f5SDavid Howells ret = afs_writepages_region(mapping, wbc, 836a9eb558aSDavid Howells wbc->range_start, wbc->range_end, 837a9eb558aSDavid Howells &next, false); 83831143d5dSDavid Howells } 83931143d5dSDavid Howells 840ec0fa0b6SDavid Howells up_read(&vnode->validate_lock); 84131143d5dSDavid Howells _leave(" = %d", ret); 84231143d5dSDavid Howells return ret; 84331143d5dSDavid Howells } 84431143d5dSDavid Howells 84531143d5dSDavid Howells /* 84631143d5dSDavid Howells * write to an AFS file 84731143d5dSDavid Howells */ 84850b5551dSAl Viro ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from) 84931143d5dSDavid Howells { 850496ad9aaSAl Viro struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp)); 8513978d816SDavid Howells struct afs_file *af = iocb->ki_filp->private_data; 85231143d5dSDavid Howells ssize_t result; 85350b5551dSAl Viro size_t count = iov_iter_count(from); 85431143d5dSDavid Howells 8553b6492dfSDavid Howells _enter("{%llx:%llu},{%zu},", 85650b5551dSAl Viro vnode->fid.vid, vnode->fid.vnode, count); 85731143d5dSDavid Howells 858874c8ca1SDavid Howells if (IS_SWAPFILE(&vnode->netfs.inode)) { 85931143d5dSDavid Howells printk(KERN_INFO 86031143d5dSDavid Howells "AFS: Attempt to write to active swap file!\n"); 86131143d5dSDavid Howells return -EBUSY; 86231143d5dSDavid Howells } 86331143d5dSDavid Howells 86431143d5dSDavid Howells if (!count) 86531143d5dSDavid Howells return 0; 86631143d5dSDavid Howells 8673978d816SDavid Howells result = afs_validate(vnode, af->key); 8683978d816SDavid Howells if (result < 0) 8693978d816SDavid Howells return result; 8703978d816SDavid Howells 87150b5551dSAl Viro result = generic_file_write_iter(iocb, from); 87231143d5dSDavid Howells 87331143d5dSDavid Howells _leave(" = %zd", result); 87431143d5dSDavid Howells return result; 87531143d5dSDavid Howells } 87631143d5dSDavid Howells 87731143d5dSDavid Howells /* 87831143d5dSDavid Howells * flush any dirty pages for this process, and check for write errors. 87931143d5dSDavid Howells * - the return status from this call provides a reliable indication of 88031143d5dSDavid Howells * whether any write errors occurred for this process. 88131143d5dSDavid Howells */ 88202c24a82SJosef Bacik int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync) 88331143d5dSDavid Howells { 8843978d816SDavid Howells struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 8853978d816SDavid Howells struct afs_file *af = file->private_data; 8863978d816SDavid Howells int ret; 88731143d5dSDavid Howells 8883b6492dfSDavid Howells _enter("{%llx:%llu},{n=%pD},%d", 8893c981bfcSAl Viro vnode->fid.vid, vnode->fid.vnode, file, 89031143d5dSDavid Howells datasync); 89131143d5dSDavid Howells 8923978d816SDavid Howells ret = afs_validate(vnode, af->key); 8933978d816SDavid Howells if (ret < 0) 8943978d816SDavid Howells return ret; 8953978d816SDavid Howells 8964343d008SDavid Howells return file_write_and_wait_range(file, start, end); 89731143d5dSDavid Howells } 8989b3f26c9SDavid Howells 8999b3f26c9SDavid Howells /* 9009b3f26c9SDavid Howells * notification that a previously read-only page is about to become writable 9019b3f26c9SDavid Howells * - if it returns an error, the caller will deliver a bus error signal 9029b3f26c9SDavid Howells */ 9030722f186SSouptick Joarder vm_fault_t afs_page_mkwrite(struct vm_fault *vmf) 9049b3f26c9SDavid Howells { 905490e016fSMatthew Wilcox (Oracle) struct folio *folio = page_folio(vmf->page); 9061cf7a151SDavid Howells struct file *file = vmf->vma->vm_file; 9071cf7a151SDavid Howells struct inode *inode = file_inode(file); 9081cf7a151SDavid Howells struct afs_vnode *vnode = AFS_FS_I(inode); 9093978d816SDavid Howells struct afs_file *af = file->private_data; 9101cf7a151SDavid Howells unsigned long priv; 9119620ad86SMatthew Wilcox (Oracle) vm_fault_t ret = VM_FAULT_RETRY; 9129b3f26c9SDavid Howells 91378525c74SDavid Howells _enter("{{%llx:%llu}},{%lx}", vnode->fid.vid, vnode->fid.vnode, folio_index(folio)); 9149b3f26c9SDavid Howells 9153978d816SDavid Howells afs_validate(vnode, af->key); 9163978d816SDavid Howells 9171cf7a151SDavid Howells sb_start_pagefault(inode->i_sb); 9181cf7a151SDavid Howells 9191cf7a151SDavid Howells /* Wait for the page to be written to the cache before we allow it to 9201cf7a151SDavid Howells * be modified. We then assume the entire page will need writing back. 9211cf7a151SDavid Howells */ 922630f5ddaSDavid Howells #ifdef CONFIG_AFS_FSCACHE 92378525c74SDavid Howells if (folio_test_fscache(folio) && 92478525c74SDavid Howells folio_wait_fscache_killable(folio) < 0) 9259620ad86SMatthew Wilcox (Oracle) goto out; 926630f5ddaSDavid Howells #endif 9279b3f26c9SDavid Howells 928490e016fSMatthew Wilcox (Oracle) if (folio_wait_writeback_killable(folio)) 9299620ad86SMatthew Wilcox (Oracle) goto out; 9301cf7a151SDavid Howells 93178525c74SDavid Howells if (folio_lock_killable(folio) < 0) 9329620ad86SMatthew Wilcox (Oracle) goto out; 9331cf7a151SDavid Howells 93478525c74SDavid Howells /* We mustn't change folio->private until writeback is complete as that 9351cf7a151SDavid Howells * details the portion of the page we need to write back and we might 9361cf7a151SDavid Howells * need to redirty the page if there's a problem. 9371cf7a151SDavid Howells */ 938490e016fSMatthew Wilcox (Oracle) if (folio_wait_writeback_killable(folio) < 0) { 939490e016fSMatthew Wilcox (Oracle) folio_unlock(folio); 9409620ad86SMatthew Wilcox (Oracle) goto out; 9415cbf0398SDavid Howells } 9421cf7a151SDavid Howells 94378525c74SDavid Howells priv = afs_folio_dirty(folio, 0, folio_size(folio)); 94478525c74SDavid Howells priv = afs_folio_dirty_mmapped(priv); 94578525c74SDavid Howells if (folio_test_private(folio)) { 94678525c74SDavid Howells folio_change_private(folio, (void *)priv); 94778525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("mkwrite+"), folio); 948e87b03f5SDavid Howells } else { 94978525c74SDavid Howells folio_attach_private(folio, (void *)priv); 95078525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("mkwrite"), folio); 951e87b03f5SDavid Howells } 952bb413489SDavid Howells file_update_time(file); 9531cf7a151SDavid Howells 9549620ad86SMatthew Wilcox (Oracle) ret = VM_FAULT_LOCKED; 9559620ad86SMatthew Wilcox (Oracle) out: 9561cf7a151SDavid Howells sb_end_pagefault(inode->i_sb); 9579620ad86SMatthew Wilcox (Oracle) return ret; 9589b3f26c9SDavid Howells } 9594343d008SDavid Howells 9604343d008SDavid Howells /* 9614343d008SDavid Howells * Prune the keys cached for writeback. The caller must hold vnode->wb_lock. 9624343d008SDavid Howells */ 9634343d008SDavid Howells void afs_prune_wb_keys(struct afs_vnode *vnode) 9644343d008SDavid Howells { 9654343d008SDavid Howells LIST_HEAD(graveyard); 9664343d008SDavid Howells struct afs_wb_key *wbk, *tmp; 9674343d008SDavid Howells 9684343d008SDavid Howells /* Discard unused keys */ 9694343d008SDavid Howells spin_lock(&vnode->wb_lock); 9704343d008SDavid Howells 971874c8ca1SDavid Howells if (!mapping_tagged(&vnode->netfs.inode.i_data, PAGECACHE_TAG_WRITEBACK) && 972874c8ca1SDavid Howells !mapping_tagged(&vnode->netfs.inode.i_data, PAGECACHE_TAG_DIRTY)) { 9734343d008SDavid Howells list_for_each_entry_safe(wbk, tmp, &vnode->wb_keys, vnode_link) { 9744343d008SDavid Howells if (refcount_read(&wbk->usage) == 1) 9754343d008SDavid Howells list_move(&wbk->vnode_link, &graveyard); 9764343d008SDavid Howells } 9774343d008SDavid Howells } 9784343d008SDavid Howells 9794343d008SDavid Howells spin_unlock(&vnode->wb_lock); 9804343d008SDavid Howells 9814343d008SDavid Howells while (!list_empty(&graveyard)) { 9824343d008SDavid Howells wbk = list_entry(graveyard.next, struct afs_wb_key, vnode_link); 9834343d008SDavid Howells list_del(&wbk->vnode_link); 9844343d008SDavid Howells afs_put_wb_key(wbk); 9854343d008SDavid Howells } 9864343d008SDavid Howells } 9874343d008SDavid Howells 9884343d008SDavid Howells /* 9894343d008SDavid Howells * Clean up a page during invalidation. 9904343d008SDavid Howells */ 991a42442ddSMatthew Wilcox (Oracle) int afs_launder_folio(struct folio *folio) 9924343d008SDavid Howells { 99378525c74SDavid Howells struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio)); 994bd80d8a8SDavid Howells struct iov_iter iter; 995*a8173be1SChristoph Hellwig struct bio_vec bv; 9964343d008SDavid Howells unsigned long priv; 9974343d008SDavid Howells unsigned int f, t; 9984343d008SDavid Howells int ret = 0; 9994343d008SDavid Howells 1000a42442ddSMatthew Wilcox (Oracle) _enter("{%lx}", folio->index); 10014343d008SDavid Howells 100278525c74SDavid Howells priv = (unsigned long)folio_get_private(folio); 100378525c74SDavid Howells if (folio_clear_dirty_for_io(folio)) { 10044343d008SDavid Howells f = 0; 100578525c74SDavid Howells t = folio_size(folio); 100678525c74SDavid Howells if (folio_test_private(folio)) { 100778525c74SDavid Howells f = afs_folio_dirty_from(folio, priv); 100878525c74SDavid Howells t = afs_folio_dirty_to(folio, priv); 10094343d008SDavid Howells } 10104343d008SDavid Howells 1011*a8173be1SChristoph Hellwig bvec_set_folio(&bv, folio, t - f, f); 1012*a8173be1SChristoph Hellwig iov_iter_bvec(&iter, ITER_SOURCE, &bv, 1, bv.bv_len); 1013bd80d8a8SDavid Howells 101478525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("launder"), folio); 101578525c74SDavid Howells ret = afs_store_data(vnode, &iter, folio_pos(folio) + f, true); 10164343d008SDavid Howells } 10174343d008SDavid Howells 101878525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("laundered"), folio); 101978525c74SDavid Howells folio_detach_private(folio); 102078525c74SDavid Howells folio_wait_fscache(folio); 10214343d008SDavid Howells return ret; 102231143d5dSDavid Howells } 1023c7f75ef3SDavid Howells 1024c7f75ef3SDavid Howells /* 1025c7f75ef3SDavid Howells * Deal with the completion of writing the data to the cache. 1026c7f75ef3SDavid Howells */ 1027c7f75ef3SDavid Howells static void afs_write_to_cache_done(void *priv, ssize_t transferred_or_error, 1028c7f75ef3SDavid Howells bool was_async) 1029c7f75ef3SDavid Howells { 1030c7f75ef3SDavid Howells struct afs_vnode *vnode = priv; 1031c7f75ef3SDavid Howells 1032c7f75ef3SDavid Howells if (IS_ERR_VALUE(transferred_or_error) && 1033c7f75ef3SDavid Howells transferred_or_error != -ENOBUFS) 1034c7f75ef3SDavid Howells afs_invalidate_cache(vnode, 0); 1035c7f75ef3SDavid Howells } 1036c7f75ef3SDavid Howells 1037c7f75ef3SDavid Howells /* 1038c7f75ef3SDavid Howells * Save the write to the cache also. 1039c7f75ef3SDavid Howells */ 1040c7f75ef3SDavid Howells static void afs_write_to_cache(struct afs_vnode *vnode, 1041c7f75ef3SDavid Howells loff_t start, size_t len, loff_t i_size, 1042c7f75ef3SDavid Howells bool caching) 1043c7f75ef3SDavid Howells { 1044c7f75ef3SDavid Howells fscache_write_to_cache(afs_vnode_cache(vnode), 1045874c8ca1SDavid Howells vnode->netfs.inode.i_mapping, start, len, i_size, 1046c7f75ef3SDavid Howells afs_write_to_cache_done, vnode, caching); 1047c7f75ef3SDavid Howells } 1048