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 17c7f75ef3SDavid Howells static void afs_write_to_cache(struct afs_vnode *vnode, loff_t start, size_t len, 18c7f75ef3SDavid Howells loff_t i_size, bool caching); 19c7f75ef3SDavid Howells 20c7f75ef3SDavid Howells #ifdef CONFIG_AFS_FSCACHE 2131143d5dSDavid Howells /* 22c7f75ef3SDavid Howells * Mark a page as having been made dirty and thus needing writeback. We also 23c7f75ef3SDavid Howells * need to pin the cache object to write back to. 2431143d5dSDavid Howells */ 258fb72b4aSMatthew Wilcox (Oracle) bool afs_dirty_folio(struct address_space *mapping, struct folio *folio) 2631143d5dSDavid Howells { 278fb72b4aSMatthew Wilcox (Oracle) return fscache_dirty_folio(mapping, folio, 288fb72b4aSMatthew Wilcox (Oracle) afs_vnode_cache(AFS_FS_I(mapping->host))); 2931143d5dSDavid Howells } 30c7f75ef3SDavid Howells static void afs_folio_start_fscache(bool caching, struct folio *folio) 31c7f75ef3SDavid Howells { 32c7f75ef3SDavid Howells if (caching) 33c7f75ef3SDavid Howells folio_start_fscache(folio); 34c7f75ef3SDavid Howells } 35c7f75ef3SDavid Howells #else 36c7f75ef3SDavid Howells static void afs_folio_start_fscache(bool caching, struct folio *folio) 37c7f75ef3SDavid Howells { 38c7f75ef3SDavid Howells } 39c7f75ef3SDavid Howells #endif 4031143d5dSDavid Howells 4131143d5dSDavid Howells /* 4231143d5dSDavid Howells * prepare to perform part of a write to a page 4331143d5dSDavid Howells */ 4415b4650eSNick Piggin int afs_write_begin(struct file *file, struct address_space *mapping, 45*9d6b0cd7SMatthew Wilcox (Oracle) loff_t pos, unsigned len, 4621db2cdcSDavid Howells struct page **_page, void **fsdata) 4731143d5dSDavid Howells { 48496ad9aaSAl Viro struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 4978525c74SDavid Howells struct folio *folio; 504343d008SDavid Howells unsigned long priv; 51e87b03f5SDavid Howells unsigned f, from; 52e87b03f5SDavid Howells unsigned t, to; 53e87b03f5SDavid Howells pgoff_t index; 5431143d5dSDavid Howells int ret; 5531143d5dSDavid Howells 56e87b03f5SDavid Howells _enter("{%llx:%llu},%llx,%x", 57e87b03f5SDavid Howells vnode->fid.vid, vnode->fid.vnode, pos, len); 5831143d5dSDavid Howells 593003bbd0SDavid Howells /* Prefetch area to be written into the cache if we're caching this 603003bbd0SDavid Howells * file. We need to do this before we get a lock on the page in case 613003bbd0SDavid Howells * there's more than one writer competing for the same cache block. 623003bbd0SDavid Howells */ 63de2a9311SMatthew Wilcox (Oracle) ret = netfs_write_begin(file, mapping, pos, len, &folio, fsdata); 643003bbd0SDavid Howells if (ret < 0) 6531143d5dSDavid Howells return ret; 66630f5ddaSDavid Howells 6778525c74SDavid Howells index = folio_index(folio); 68e87b03f5SDavid Howells from = pos - index * PAGE_SIZE; 69e87b03f5SDavid Howells to = from + len; 70e87b03f5SDavid Howells 7131143d5dSDavid Howells try_again: 724343d008SDavid Howells /* See if this page is already partially written in a way that we can 734343d008SDavid Howells * merge the new write with. 744343d008SDavid Howells */ 7578525c74SDavid Howells if (folio_test_private(folio)) { 7678525c74SDavid Howells priv = (unsigned long)folio_get_private(folio); 7778525c74SDavid Howells f = afs_folio_dirty_from(folio, priv); 7878525c74SDavid Howells t = afs_folio_dirty_to(folio, priv); 794343d008SDavid Howells ASSERTCMP(f, <=, t); 8031143d5dSDavid Howells 8178525c74SDavid Howells if (folio_test_writeback(folio)) { 8278525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("alrdy"), folio); 835a039c32SDavid Howells goto flush_conflicting_write; 845a039c32SDavid Howells } 855a813276SDavid Howells /* If the file is being filled locally, allow inter-write 865a813276SDavid Howells * spaces to be merged into writes. If it's not, only write 875a813276SDavid Howells * back what the user gives us. 885a813276SDavid Howells */ 895a813276SDavid Howells if (!test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags) && 905a813276SDavid Howells (to < f || from > t)) 914343d008SDavid Howells goto flush_conflicting_write; 9231143d5dSDavid Howells } 9331143d5dSDavid Howells 9478525c74SDavid Howells *_page = &folio->page; 954343d008SDavid Howells _leave(" = 0"); 9631143d5dSDavid Howells return 0; 9731143d5dSDavid Howells 984343d008SDavid Howells /* The previous write and this write aren't adjacent or overlapping, so 994343d008SDavid Howells * flush the page out. 1004343d008SDavid Howells */ 1014343d008SDavid Howells flush_conflicting_write: 10231143d5dSDavid Howells _debug("flush conflict"); 10378525c74SDavid Howells ret = folio_write_one(folio); 10421db2cdcSDavid Howells if (ret < 0) 10521db2cdcSDavid Howells goto error; 10631143d5dSDavid Howells 10778525c74SDavid Howells ret = folio_lock_killable(folio); 10821db2cdcSDavid Howells if (ret < 0) 10921db2cdcSDavid Howells goto error; 11021db2cdcSDavid Howells goto try_again; 11121db2cdcSDavid Howells 11221db2cdcSDavid Howells error: 11378525c74SDavid Howells folio_put(folio); 1144343d008SDavid Howells _leave(" = %d", ret); 1154343d008SDavid Howells return ret; 1164343d008SDavid Howells } 11731143d5dSDavid Howells 11831143d5dSDavid Howells /* 11931143d5dSDavid Howells * finalise part of a write to a page 12031143d5dSDavid Howells */ 12115b4650eSNick Piggin int afs_write_end(struct file *file, struct address_space *mapping, 12215b4650eSNick Piggin loff_t pos, unsigned len, unsigned copied, 12378525c74SDavid Howells struct page *subpage, void *fsdata) 12431143d5dSDavid Howells { 12578525c74SDavid Howells struct folio *folio = page_folio(subpage); 126496ad9aaSAl Viro struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 127f792e3acSDavid Howells unsigned long priv; 12878525c74SDavid Howells unsigned int f, from = offset_in_folio(folio, pos); 129f792e3acSDavid Howells unsigned int t, to = from + copied; 130c7f75ef3SDavid Howells loff_t i_size, write_end_pos; 13131143d5dSDavid Howells 1323b6492dfSDavid Howells _enter("{%llx:%llu},{%lx}", 13378525c74SDavid Howells vnode->fid.vid, vnode->fid.vnode, folio_index(folio)); 13431143d5dSDavid Howells 13578525c74SDavid Howells if (!folio_test_uptodate(folio)) { 13666e9c6a8SDavid Howells if (copied < len) { 13766e9c6a8SDavid Howells copied = 0; 13866e9c6a8SDavid Howells goto out; 13966e9c6a8SDavid Howells } 14066e9c6a8SDavid Howells 14178525c74SDavid Howells folio_mark_uptodate(folio); 14266e9c6a8SDavid Howells } 14366e9c6a8SDavid Howells 1443ad216eeSDavid Howells if (copied == 0) 1453ad216eeSDavid Howells goto out; 1463ad216eeSDavid Howells 147c7f75ef3SDavid Howells write_end_pos = pos + copied; 14831143d5dSDavid Howells 14931143d5dSDavid Howells i_size = i_size_read(&vnode->vfs_inode); 150c7f75ef3SDavid Howells if (write_end_pos > i_size) { 1511f32ef79SDavid Howells write_seqlock(&vnode->cb_lock); 15231143d5dSDavid Howells i_size = i_size_read(&vnode->vfs_inode); 153c7f75ef3SDavid Howells if (write_end_pos > i_size) 154c7f75ef3SDavid Howells afs_set_i_size(vnode, write_end_pos); 1551f32ef79SDavid Howells write_sequnlock(&vnode->cb_lock); 156c7f75ef3SDavid Howells fscache_update_cookie(afs_vnode_cache(vnode), NULL, &write_end_pos); 15731143d5dSDavid Howells } 15831143d5dSDavid Howells 15978525c74SDavid Howells if (folio_test_private(folio)) { 16078525c74SDavid Howells priv = (unsigned long)folio_get_private(folio); 16178525c74SDavid Howells f = afs_folio_dirty_from(folio, priv); 16278525c74SDavid Howells t = afs_folio_dirty_to(folio, priv); 163f792e3acSDavid Howells if (from < f) 164f792e3acSDavid Howells f = from; 165f792e3acSDavid Howells if (to > t) 166f792e3acSDavid Howells t = to; 16778525c74SDavid Howells priv = afs_folio_dirty(folio, f, t); 16878525c74SDavid Howells folio_change_private(folio, (void *)priv); 16978525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("dirty+"), folio); 170f792e3acSDavid Howells } else { 17178525c74SDavid Howells priv = afs_folio_dirty(folio, from, to); 17278525c74SDavid Howells folio_attach_private(folio, (void *)priv); 17378525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("dirty"), folio); 174f792e3acSDavid Howells } 175f792e3acSDavid Howells 17678525c74SDavid Howells if (folio_mark_dirty(folio)) 17778525c74SDavid Howells _debug("dirtied %lx", folio_index(folio)); 178afae457dSDavid Howells 179afae457dSDavid Howells out: 18078525c74SDavid Howells folio_unlock(folio); 18178525c74SDavid Howells folio_put(folio); 1823003bbd0SDavid Howells return copied; 18331143d5dSDavid Howells } 18431143d5dSDavid Howells 18531143d5dSDavid Howells /* 18631143d5dSDavid Howells * kill all the pages in the given range 18731143d5dSDavid Howells */ 1884343d008SDavid Howells static void afs_kill_pages(struct address_space *mapping, 189e87b03f5SDavid Howells loff_t start, loff_t len) 19031143d5dSDavid Howells { 1914343d008SDavid Howells struct afs_vnode *vnode = AFS_FS_I(mapping->host); 19278525c74SDavid Howells struct folio *folio; 19378525c74SDavid Howells pgoff_t index = start / PAGE_SIZE; 19478525c74SDavid Howells pgoff_t last = (start + len - 1) / PAGE_SIZE, next; 19531143d5dSDavid Howells 196e87b03f5SDavid Howells _enter("{%llx:%llu},%llx @%llx", 197e87b03f5SDavid Howells vnode->fid.vid, vnode->fid.vnode, len, start); 19831143d5dSDavid Howells 19931143d5dSDavid Howells do { 20078525c74SDavid Howells _debug("kill %lx (to %lx)", index, last); 20131143d5dSDavid Howells 20278525c74SDavid Howells folio = filemap_get_folio(mapping, index); 20378525c74SDavid Howells if (!folio) { 20478525c74SDavid Howells next = index + 1; 20578525c74SDavid Howells continue; 2064343d008SDavid Howells } 2074343d008SDavid Howells 20878525c74SDavid Howells next = folio_next_index(folio); 20978525c74SDavid Howells 21078525c74SDavid Howells folio_clear_uptodate(folio); 21178525c74SDavid Howells folio_end_writeback(folio); 21278525c74SDavid Howells folio_lock(folio); 21378525c74SDavid Howells generic_error_remove_page(mapping, &folio->page); 21478525c74SDavid Howells folio_unlock(folio); 21578525c74SDavid Howells folio_put(folio); 21678525c74SDavid Howells 21778525c74SDavid Howells } while (index = next, index <= last); 2184343d008SDavid Howells 2194343d008SDavid Howells _leave(""); 2204343d008SDavid Howells } 2214343d008SDavid Howells 2224343d008SDavid Howells /* 2234343d008SDavid Howells * Redirty all the pages in a given range. 2244343d008SDavid Howells */ 2254343d008SDavid Howells static void afs_redirty_pages(struct writeback_control *wbc, 2264343d008SDavid Howells struct address_space *mapping, 227e87b03f5SDavid Howells loff_t start, loff_t len) 2284343d008SDavid Howells { 2294343d008SDavid Howells struct afs_vnode *vnode = AFS_FS_I(mapping->host); 23078525c74SDavid Howells struct folio *folio; 23178525c74SDavid Howells pgoff_t index = start / PAGE_SIZE; 23278525c74SDavid Howells pgoff_t last = (start + len - 1) / PAGE_SIZE, next; 2334343d008SDavid Howells 234e87b03f5SDavid Howells _enter("{%llx:%llu},%llx @%llx", 235e87b03f5SDavid Howells vnode->fid.vid, vnode->fid.vnode, len, start); 2364343d008SDavid Howells 2374343d008SDavid Howells do { 238e87b03f5SDavid Howells _debug("redirty %llx @%llx", len, start); 2394343d008SDavid Howells 24078525c74SDavid Howells folio = filemap_get_folio(mapping, index); 24178525c74SDavid Howells if (!folio) { 24278525c74SDavid Howells next = index + 1; 24378525c74SDavid Howells continue; 24431143d5dSDavid Howells } 24531143d5dSDavid Howells 24678525c74SDavid Howells next = index + folio_nr_pages(folio); 24778525c74SDavid Howells folio_redirty_for_writepage(wbc, folio); 24878525c74SDavid Howells folio_end_writeback(folio); 24978525c74SDavid Howells folio_put(folio); 25078525c74SDavid Howells } while (index = next, index <= last); 25131143d5dSDavid Howells 25231143d5dSDavid Howells _leave(""); 25331143d5dSDavid Howells } 25431143d5dSDavid Howells 25531143d5dSDavid Howells /* 256a58823acSDavid Howells * completion of write to server 257a58823acSDavid Howells */ 258e87b03f5SDavid Howells static void afs_pages_written_back(struct afs_vnode *vnode, loff_t start, unsigned int len) 259a58823acSDavid Howells { 260bd80d8a8SDavid Howells struct address_space *mapping = vnode->vfs_inode.i_mapping; 26178525c74SDavid Howells struct folio *folio; 262e87b03f5SDavid Howells pgoff_t end; 263bd80d8a8SDavid Howells 264e87b03f5SDavid Howells XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE); 265a58823acSDavid Howells 266e87b03f5SDavid Howells _enter("{%llx:%llu},{%x @%llx}", 267e87b03f5SDavid Howells vnode->fid.vid, vnode->fid.vnode, len, start); 268a58823acSDavid Howells 269bd80d8a8SDavid Howells rcu_read_lock(); 270a58823acSDavid Howells 271e87b03f5SDavid Howells end = (start + len - 1) / PAGE_SIZE; 27278525c74SDavid Howells xas_for_each(&xas, folio, end) { 27378525c74SDavid Howells if (!folio_test_writeback(folio)) { 27478525c74SDavid Howells kdebug("bad %x @%llx page %lx %lx", 27578525c74SDavid Howells len, start, folio_index(folio), end); 27678525c74SDavid Howells ASSERT(folio_test_writeback(folio)); 277e87b03f5SDavid Howells } 278a58823acSDavid Howells 27978525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("clear"), folio); 28078525c74SDavid Howells folio_detach_private(folio); 28178525c74SDavid Howells folio_end_writeback(folio); 282a58823acSDavid Howells } 283bd80d8a8SDavid Howells 284bd80d8a8SDavid Howells rcu_read_unlock(); 285a58823acSDavid Howells 286a58823acSDavid Howells afs_prune_wb_keys(vnode); 287a58823acSDavid Howells _leave(""); 288a58823acSDavid Howells } 289a58823acSDavid Howells 290a58823acSDavid Howells /* 291e49c7b2fSDavid Howells * Find a key to use for the writeback. We cached the keys used to author the 292e49c7b2fSDavid Howells * writes on the vnode. *_wbk will contain the last writeback key used or NULL 293e49c7b2fSDavid Howells * and we need to start from there if it's set. 294e49c7b2fSDavid Howells */ 295e49c7b2fSDavid Howells static int afs_get_writeback_key(struct afs_vnode *vnode, 296e49c7b2fSDavid Howells struct afs_wb_key **_wbk) 297e49c7b2fSDavid Howells { 298e49c7b2fSDavid Howells struct afs_wb_key *wbk = NULL; 299e49c7b2fSDavid Howells struct list_head *p; 300e49c7b2fSDavid Howells int ret = -ENOKEY, ret2; 301e49c7b2fSDavid Howells 302e49c7b2fSDavid Howells spin_lock(&vnode->wb_lock); 303e49c7b2fSDavid Howells if (*_wbk) 304e49c7b2fSDavid Howells p = (*_wbk)->vnode_link.next; 305e49c7b2fSDavid Howells else 306e49c7b2fSDavid Howells p = vnode->wb_keys.next; 307e49c7b2fSDavid Howells 308e49c7b2fSDavid Howells while (p != &vnode->wb_keys) { 309e49c7b2fSDavid Howells wbk = list_entry(p, struct afs_wb_key, vnode_link); 310e49c7b2fSDavid Howells _debug("wbk %u", key_serial(wbk->key)); 311e49c7b2fSDavid Howells ret2 = key_validate(wbk->key); 312e49c7b2fSDavid Howells if (ret2 == 0) { 313e49c7b2fSDavid Howells refcount_inc(&wbk->usage); 314e49c7b2fSDavid Howells _debug("USE WB KEY %u", key_serial(wbk->key)); 315e49c7b2fSDavid Howells break; 316e49c7b2fSDavid Howells } 317e49c7b2fSDavid Howells 318e49c7b2fSDavid Howells wbk = NULL; 319e49c7b2fSDavid Howells if (ret == -ENOKEY) 320e49c7b2fSDavid Howells ret = ret2; 321e49c7b2fSDavid Howells p = p->next; 322e49c7b2fSDavid Howells } 323e49c7b2fSDavid Howells 324e49c7b2fSDavid Howells spin_unlock(&vnode->wb_lock); 325e49c7b2fSDavid Howells if (*_wbk) 326e49c7b2fSDavid Howells afs_put_wb_key(*_wbk); 327e49c7b2fSDavid Howells *_wbk = wbk; 328e49c7b2fSDavid Howells return 0; 329e49c7b2fSDavid Howells } 330e49c7b2fSDavid Howells 331e49c7b2fSDavid Howells static void afs_store_data_success(struct afs_operation *op) 332e49c7b2fSDavid Howells { 333e49c7b2fSDavid Howells struct afs_vnode *vnode = op->file[0].vnode; 334e49c7b2fSDavid Howells 335da8d0755SDavid Howells op->ctime = op->file[0].scb.status.mtime_client; 336e49c7b2fSDavid Howells afs_vnode_commit_status(op, &op->file[0]); 337e49c7b2fSDavid Howells if (op->error == 0) { 338d383e346SDavid Howells if (!op->store.laundering) 339e87b03f5SDavid Howells afs_pages_written_back(vnode, op->store.pos, op->store.size); 340e49c7b2fSDavid Howells afs_stat_v(vnode, n_stores); 341bd80d8a8SDavid Howells atomic_long_add(op->store.size, &afs_v2net(vnode)->n_store_bytes); 342e49c7b2fSDavid Howells } 343e49c7b2fSDavid Howells } 344e49c7b2fSDavid Howells 345e49c7b2fSDavid Howells static const struct afs_operation_ops afs_store_data_operation = { 346e49c7b2fSDavid Howells .issue_afs_rpc = afs_fs_store_data, 347e49c7b2fSDavid Howells .issue_yfs_rpc = yfs_fs_store_data, 348e49c7b2fSDavid Howells .success = afs_store_data_success, 349e49c7b2fSDavid Howells }; 350e49c7b2fSDavid Howells 351e49c7b2fSDavid Howells /* 352d2ddc776SDavid Howells * write to a file 35331143d5dSDavid Howells */ 354e87b03f5SDavid Howells static int afs_store_data(struct afs_vnode *vnode, struct iov_iter *iter, loff_t pos, 355bd80d8a8SDavid Howells bool laundering) 35631143d5dSDavid Howells { 357ab487a4cSDavid Howells struct netfs_i_context *ictx = &vnode->netfs_ctx; 358e49c7b2fSDavid Howells struct afs_operation *op; 3594343d008SDavid Howells struct afs_wb_key *wbk = NULL; 360ab487a4cSDavid Howells loff_t size = iov_iter_count(iter); 361bd80d8a8SDavid Howells int ret = -ENOKEY; 362d2ddc776SDavid Howells 363bd80d8a8SDavid Howells _enter("%s{%llx:%llu.%u},%llx,%llx", 364d2ddc776SDavid Howells vnode->volume->name, 365d2ddc776SDavid Howells vnode->fid.vid, 366d2ddc776SDavid Howells vnode->fid.vnode, 367d2ddc776SDavid Howells vnode->fid.unique, 368bd80d8a8SDavid Howells size, pos); 369d2ddc776SDavid Howells 370e49c7b2fSDavid Howells ret = afs_get_writeback_key(vnode, &wbk); 371e49c7b2fSDavid Howells if (ret) { 3724343d008SDavid Howells _leave(" = %d [no keys]", ret); 3734343d008SDavid Howells return ret; 374d2ddc776SDavid Howells } 375d2ddc776SDavid Howells 376e49c7b2fSDavid Howells op = afs_alloc_operation(wbk->key, vnode->volume); 377e49c7b2fSDavid Howells if (IS_ERR(op)) { 378e49c7b2fSDavid Howells afs_put_wb_key(wbk); 379e49c7b2fSDavid Howells return -ENOMEM; 380d2ddc776SDavid Howells } 381d2ddc776SDavid Howells 382e49c7b2fSDavid Howells afs_op_set_vnode(op, 0, vnode); 383e49c7b2fSDavid Howells op->file[0].dv_delta = 1; 38422650f14SDavid Howells op->file[0].modification = true; 385bd80d8a8SDavid Howells op->store.write_iter = iter; 386bd80d8a8SDavid Howells op->store.pos = pos; 387bd80d8a8SDavid Howells op->store.size = size; 388ab487a4cSDavid Howells op->store.i_size = max(pos + size, ictx->remote_i_size); 389d383e346SDavid Howells op->store.laundering = laundering; 390b3597945SDavid Howells op->mtime = vnode->vfs_inode.i_mtime; 391811f04baSDavid Howells op->flags |= AFS_OPERATION_UNINTR; 392e49c7b2fSDavid Howells op->ops = &afs_store_data_operation; 393e49c7b2fSDavid Howells 394e49c7b2fSDavid Howells try_next_key: 395e49c7b2fSDavid Howells afs_begin_vnode_operation(op); 396e49c7b2fSDavid Howells afs_wait_for_operation(op); 397e49c7b2fSDavid Howells 398e49c7b2fSDavid Howells switch (op->error) { 3994343d008SDavid Howells case -EACCES: 4004343d008SDavid Howells case -EPERM: 4014343d008SDavid Howells case -ENOKEY: 4024343d008SDavid Howells case -EKEYEXPIRED: 4034343d008SDavid Howells case -EKEYREJECTED: 4044343d008SDavid Howells case -EKEYREVOKED: 4054343d008SDavid Howells _debug("next"); 406e49c7b2fSDavid Howells 407e49c7b2fSDavid Howells ret = afs_get_writeback_key(vnode, &wbk); 408e49c7b2fSDavid Howells if (ret == 0) { 409e49c7b2fSDavid Howells key_put(op->key); 410e49c7b2fSDavid Howells op->key = key_get(wbk->key); 4114343d008SDavid Howells goto try_next_key; 4124343d008SDavid Howells } 413e49c7b2fSDavid Howells break; 414e49c7b2fSDavid Howells } 4154343d008SDavid Howells 4164343d008SDavid Howells afs_put_wb_key(wbk); 417e49c7b2fSDavid Howells _leave(" = %d", op->error); 418e49c7b2fSDavid Howells return afs_put_operation(op); 419d2ddc776SDavid Howells } 420d2ddc776SDavid Howells 421d2ddc776SDavid Howells /* 422810caa3eSDavid Howells * Extend the region to be written back to include subsequent contiguously 423810caa3eSDavid Howells * dirty pages if possible, but don't sleep while doing so. 424810caa3eSDavid Howells * 425810caa3eSDavid Howells * If this page holds new content, then we can include filler zeros in the 426810caa3eSDavid Howells * writeback. 42731143d5dSDavid Howells */ 428810caa3eSDavid Howells static void afs_extend_writeback(struct address_space *mapping, 429810caa3eSDavid Howells struct afs_vnode *vnode, 430810caa3eSDavid Howells long *_count, 431e87b03f5SDavid Howells loff_t start, 432e87b03f5SDavid Howells loff_t max_len, 433e87b03f5SDavid Howells bool new_content, 434c7f75ef3SDavid Howells bool caching, 435e87b03f5SDavid Howells unsigned int *_len) 43631143d5dSDavid Howells { 437e87b03f5SDavid Howells struct pagevec pvec; 43878525c74SDavid Howells struct folio *folio; 439e87b03f5SDavid Howells unsigned long priv; 440e87b03f5SDavid Howells unsigned int psize, filler = 0; 441e87b03f5SDavid Howells unsigned int f, t; 442e87b03f5SDavid Howells loff_t len = *_len; 443e87b03f5SDavid Howells pgoff_t index = (start + len) / PAGE_SIZE; 444e87b03f5SDavid Howells bool stop = true; 445e87b03f5SDavid Howells unsigned int i; 4464343d008SDavid Howells 447e87b03f5SDavid Howells XA_STATE(xas, &mapping->i_pages, index); 448e87b03f5SDavid Howells pagevec_init(&pvec); 449e87b03f5SDavid Howells 45031143d5dSDavid Howells do { 451e87b03f5SDavid Howells /* Firstly, we gather up a batch of contiguous dirty pages 452e87b03f5SDavid Howells * under the RCU read lock - but we can't clear the dirty flags 453e87b03f5SDavid Howells * there if any of those pages are mapped. 454e87b03f5SDavid Howells */ 455e87b03f5SDavid Howells rcu_read_lock(); 456e87b03f5SDavid Howells 45778525c74SDavid Howells xas_for_each(&xas, folio, ULONG_MAX) { 458e87b03f5SDavid Howells stop = true; 45978525c74SDavid Howells if (xas_retry(&xas, folio)) 460e87b03f5SDavid Howells continue; 46178525c74SDavid Howells if (xa_is_value(folio)) 462e87b03f5SDavid Howells break; 46378525c74SDavid Howells if (folio_index(folio) != index) 464e87b03f5SDavid Howells break; 465e87b03f5SDavid Howells 46678525c74SDavid Howells if (!folio_try_get_rcu(folio)) { 467e87b03f5SDavid Howells xas_reset(&xas); 468e87b03f5SDavid Howells continue; 46931143d5dSDavid Howells } 47031143d5dSDavid Howells 471e87b03f5SDavid Howells /* Has the page moved or been split? */ 47278525c74SDavid Howells if (unlikely(folio != xas_reload(&xas))) { 47378525c74SDavid Howells folio_put(folio); 4745a813276SDavid Howells break; 475581b2027SDavid Howells } 476e87b03f5SDavid Howells 47778525c74SDavid Howells if (!folio_trylock(folio)) { 47878525c74SDavid Howells folio_put(folio); 47931143d5dSDavid Howells break; 480581b2027SDavid Howells } 481c7f75ef3SDavid Howells if (!folio_test_dirty(folio) || 482c7f75ef3SDavid Howells folio_test_writeback(folio) || 483c7f75ef3SDavid Howells folio_test_fscache(folio)) { 48478525c74SDavid Howells folio_unlock(folio); 48578525c74SDavid Howells folio_put(folio); 48631143d5dSDavid Howells break; 48731143d5dSDavid Howells } 4884343d008SDavid Howells 48978525c74SDavid Howells psize = folio_size(folio); 49078525c74SDavid Howells priv = (unsigned long)folio_get_private(folio); 49178525c74SDavid Howells f = afs_folio_dirty_from(folio, priv); 49278525c74SDavid Howells t = afs_folio_dirty_to(folio, priv); 493810caa3eSDavid Howells if (f != 0 && !new_content) { 49478525c74SDavid Howells folio_unlock(folio); 49578525c74SDavid Howells folio_put(folio); 4964343d008SDavid Howells break; 4974343d008SDavid Howells } 4984343d008SDavid Howells 499e87b03f5SDavid Howells len += filler + t; 500e87b03f5SDavid Howells filler = psize - t; 501e87b03f5SDavid Howells if (len >= max_len || *_count <= 0) 502e87b03f5SDavid Howells stop = true; 503e87b03f5SDavid Howells else if (t == psize || new_content) 504e87b03f5SDavid Howells stop = false; 505e87b03f5SDavid Howells 50678525c74SDavid Howells index += folio_nr_pages(folio); 50778525c74SDavid Howells if (!pagevec_add(&pvec, &folio->page)) 508e87b03f5SDavid Howells break; 509e87b03f5SDavid Howells if (stop) 510e87b03f5SDavid Howells break; 511e87b03f5SDavid Howells } 512e87b03f5SDavid Howells 513e87b03f5SDavid Howells if (!stop) 514e87b03f5SDavid Howells xas_pause(&xas); 515e87b03f5SDavid Howells rcu_read_unlock(); 516e87b03f5SDavid Howells 517e87b03f5SDavid Howells /* Now, if we obtained any pages, we can shift them to being 518e87b03f5SDavid Howells * writable and mark them for caching. 519e87b03f5SDavid Howells */ 520e87b03f5SDavid Howells if (!pagevec_count(&pvec)) 521e87b03f5SDavid Howells break; 522e87b03f5SDavid Howells 523e87b03f5SDavid Howells for (i = 0; i < pagevec_count(&pvec); i++) { 52478525c74SDavid Howells folio = page_folio(pvec.pages[i]); 52578525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("store+"), folio); 52613524ab3SDavid Howells 52778525c74SDavid Howells if (!folio_clear_dirty_for_io(folio)) 52831143d5dSDavid Howells BUG(); 52978525c74SDavid Howells if (folio_start_writeback(folio)) 53031143d5dSDavid Howells BUG(); 531c7f75ef3SDavid Howells afs_folio_start_fscache(caching, folio); 532e87b03f5SDavid Howells 53378525c74SDavid Howells *_count -= folio_nr_pages(folio); 53478525c74SDavid Howells folio_unlock(folio); 53531143d5dSDavid Howells } 53631143d5dSDavid Howells 537e87b03f5SDavid Howells pagevec_release(&pvec); 538e87b03f5SDavid Howells cond_resched(); 539e87b03f5SDavid Howells } while (!stop); 54031143d5dSDavid Howells 541e87b03f5SDavid Howells *_len = len; 542810caa3eSDavid Howells } 543810caa3eSDavid Howells 544810caa3eSDavid Howells /* 545810caa3eSDavid Howells * Synchronously write back the locked page and any subsequent non-locked dirty 546810caa3eSDavid Howells * pages. 547810caa3eSDavid Howells */ 54878525c74SDavid Howells static ssize_t afs_write_back_from_locked_folio(struct address_space *mapping, 549810caa3eSDavid Howells struct writeback_control *wbc, 55078525c74SDavid Howells struct folio *folio, 551e87b03f5SDavid Howells loff_t start, loff_t end) 552810caa3eSDavid Howells { 553810caa3eSDavid Howells struct afs_vnode *vnode = AFS_FS_I(mapping->host); 554810caa3eSDavid Howells struct iov_iter iter; 555e87b03f5SDavid Howells unsigned long priv; 556e87b03f5SDavid Howells unsigned int offset, to, len, max_len; 557e87b03f5SDavid Howells loff_t i_size = i_size_read(&vnode->vfs_inode); 558810caa3eSDavid Howells bool new_content = test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags); 559c7f75ef3SDavid Howells bool caching = fscache_cookie_enabled(afs_vnode_cache(vnode)); 560e87b03f5SDavid Howells long count = wbc->nr_to_write; 561810caa3eSDavid Howells int ret; 562810caa3eSDavid Howells 56378525c74SDavid Howells _enter(",%lx,%llx-%llx", folio_index(folio), start, end); 564810caa3eSDavid Howells 56578525c74SDavid Howells if (folio_start_writeback(folio)) 566810caa3eSDavid Howells BUG(); 567c7f75ef3SDavid Howells afs_folio_start_fscache(caching, folio); 568810caa3eSDavid Howells 56978525c74SDavid Howells count -= folio_nr_pages(folio); 570e87b03f5SDavid Howells 571810caa3eSDavid Howells /* Find all consecutive lockable dirty pages that have contiguous 572810caa3eSDavid Howells * written regions, stopping when we find a page that is not 573810caa3eSDavid Howells * immediately lockable, is not dirty or is missing, or we reach the 574810caa3eSDavid Howells * end of the range. 575810caa3eSDavid Howells */ 57678525c74SDavid Howells priv = (unsigned long)folio_get_private(folio); 57778525c74SDavid Howells offset = afs_folio_dirty_from(folio, priv); 57878525c74SDavid Howells to = afs_folio_dirty_to(folio, priv); 57978525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("store"), folio); 580810caa3eSDavid Howells 581e87b03f5SDavid Howells len = to - offset; 582e87b03f5SDavid Howells start += offset; 583e87b03f5SDavid Howells if (start < i_size) { 584e87b03f5SDavid Howells /* Trim the write to the EOF; the extra data is ignored. Also 585e87b03f5SDavid Howells * put an upper limit on the size of a single storedata op. 586e87b03f5SDavid Howells */ 587e87b03f5SDavid Howells max_len = 65536 * 4096; 588e87b03f5SDavid Howells max_len = min_t(unsigned long long, max_len, end - start + 1); 589e87b03f5SDavid Howells max_len = min_t(unsigned long long, max_len, i_size - start); 590810caa3eSDavid Howells 591e87b03f5SDavid Howells if (len < max_len && 59278525c74SDavid Howells (to == folio_size(folio) || new_content)) 593e87b03f5SDavid Howells afs_extend_writeback(mapping, vnode, &count, 594c7f75ef3SDavid Howells start, max_len, new_content, 595c7f75ef3SDavid Howells caching, &len); 596e87b03f5SDavid Howells len = min_t(loff_t, len, max_len); 597e87b03f5SDavid Howells } 598810caa3eSDavid Howells 5994343d008SDavid Howells /* We now have a contiguous set of dirty pages, each with writeback 6004343d008SDavid Howells * set; the first page is still locked at this point, but all the rest 6014343d008SDavid Howells * have been unlocked. 6024343d008SDavid Howells */ 60378525c74SDavid Howells folio_unlock(folio); 6044343d008SDavid Howells 605e87b03f5SDavid Howells if (start < i_size) { 606e87b03f5SDavid Howells _debug("write back %x @%llx [%llx]", len, start, i_size); 60731143d5dSDavid Howells 608c7f75ef3SDavid Howells /* Speculatively write to the cache. We have to fix this up 609c7f75ef3SDavid Howells * later if the store fails. 610c7f75ef3SDavid Howells */ 611c7f75ef3SDavid Howells afs_write_to_cache(vnode, start, len, i_size, caching); 612c7f75ef3SDavid Howells 613e87b03f5SDavid Howells iov_iter_xarray(&iter, WRITE, &mapping->i_pages, start, len); 614e87b03f5SDavid Howells ret = afs_store_data(vnode, &iter, start, false); 615bd80d8a8SDavid Howells } else { 616e87b03f5SDavid Howells _debug("write discard %x @%llx [%llx]", len, start, i_size); 617e87b03f5SDavid Howells 618bd80d8a8SDavid Howells /* The dirty region was entirely beyond the EOF. */ 6192c547f29SYue Hu fscache_clear_page_bits(mapping, start, len, caching); 620e87b03f5SDavid Howells afs_pages_written_back(vnode, start, len); 621bd80d8a8SDavid Howells ret = 0; 622bd80d8a8SDavid Howells } 623bd80d8a8SDavid Howells 62431143d5dSDavid Howells switch (ret) { 6254343d008SDavid Howells case 0: 626e87b03f5SDavid Howells wbc->nr_to_write = count; 627e87b03f5SDavid Howells ret = len; 6284343d008SDavid Howells break; 6294343d008SDavid Howells 6304343d008SDavid Howells default: 6314343d008SDavid Howells pr_notice("kAFS: Unexpected error from FS.StoreData %d\n", ret); 632df561f66SGustavo A. R. Silva fallthrough; 6334343d008SDavid Howells case -EACCES: 6344343d008SDavid Howells case -EPERM: 6354343d008SDavid Howells case -ENOKEY: 6364343d008SDavid Howells case -EKEYEXPIRED: 6374343d008SDavid Howells case -EKEYREJECTED: 6384343d008SDavid Howells case -EKEYREVOKED: 639e87b03f5SDavid Howells afs_redirty_pages(wbc, mapping, start, len); 6404343d008SDavid Howells mapping_set_error(mapping, ret); 6414343d008SDavid Howells break; 6424343d008SDavid Howells 64331143d5dSDavid Howells case -EDQUOT: 64431143d5dSDavid Howells case -ENOSPC: 645e87b03f5SDavid Howells afs_redirty_pages(wbc, mapping, start, len); 6464343d008SDavid Howells mapping_set_error(mapping, -ENOSPC); 64731143d5dSDavid Howells break; 6484343d008SDavid Howells 64931143d5dSDavid Howells case -EROFS: 65031143d5dSDavid Howells case -EIO: 65131143d5dSDavid Howells case -EREMOTEIO: 65231143d5dSDavid Howells case -EFBIG: 65331143d5dSDavid Howells case -ENOENT: 65431143d5dSDavid Howells case -ENOMEDIUM: 65531143d5dSDavid Howells case -ENXIO: 656f51375cdSDavid Howells trace_afs_file_error(vnode, ret, afs_file_error_writeback_fail); 657e87b03f5SDavid Howells afs_kill_pages(mapping, start, len); 6584343d008SDavid Howells mapping_set_error(mapping, ret); 65931143d5dSDavid Howells break; 66031143d5dSDavid Howells } 66131143d5dSDavid Howells 66231143d5dSDavid Howells _leave(" = %d", ret); 66331143d5dSDavid Howells return ret; 66431143d5dSDavid Howells } 66531143d5dSDavid Howells 66631143d5dSDavid Howells /* 66731143d5dSDavid Howells * write a page back to the server 66831143d5dSDavid Howells * - the caller locked the page for us 66931143d5dSDavid Howells */ 67078525c74SDavid Howells int afs_writepage(struct page *subpage, struct writeback_control *wbc) 67131143d5dSDavid Howells { 67278525c74SDavid Howells struct folio *folio = page_folio(subpage); 673e87b03f5SDavid Howells ssize_t ret; 674e87b03f5SDavid Howells loff_t start; 67531143d5dSDavid Howells 67678525c74SDavid Howells _enter("{%lx},", folio_index(folio)); 67731143d5dSDavid Howells 678c7f75ef3SDavid Howells #ifdef CONFIG_AFS_FSCACHE 679c7f75ef3SDavid Howells folio_wait_fscache(folio); 680c7f75ef3SDavid Howells #endif 681c7f75ef3SDavid Howells 68278525c74SDavid Howells start = folio_index(folio) * PAGE_SIZE; 68378525c74SDavid Howells ret = afs_write_back_from_locked_folio(folio_mapping(folio), wbc, 68478525c74SDavid Howells folio, start, LLONG_MAX - start); 68531143d5dSDavid Howells if (ret < 0) { 686e87b03f5SDavid Howells _leave(" = %zd", ret); 687e87b03f5SDavid Howells return ret; 68831143d5dSDavid Howells } 68931143d5dSDavid Howells 69031143d5dSDavid Howells _leave(" = 0"); 69131143d5dSDavid Howells return 0; 69231143d5dSDavid Howells } 69331143d5dSDavid Howells 69431143d5dSDavid Howells /* 69531143d5dSDavid Howells * write a region of pages back to the server 69631143d5dSDavid Howells */ 697c1206a2cSAdrian Bunk static int afs_writepages_region(struct address_space *mapping, 69831143d5dSDavid Howells struct writeback_control *wbc, 699e87b03f5SDavid Howells loff_t start, loff_t end, loff_t *_next) 70031143d5dSDavid Howells { 70178525c74SDavid Howells struct folio *folio; 70278525c74SDavid Howells struct page *head_page; 703e87b03f5SDavid Howells ssize_t ret; 704173ce1caSDavid Howells int n, skips = 0; 70531143d5dSDavid Howells 706e87b03f5SDavid Howells _enter("%llx,%llx,", start, end); 70731143d5dSDavid Howells 70831143d5dSDavid Howells do { 709e87b03f5SDavid Howells pgoff_t index = start / PAGE_SIZE; 710e87b03f5SDavid Howells 711e87b03f5SDavid Howells n = find_get_pages_range_tag(mapping, &index, end / PAGE_SIZE, 71278525c74SDavid Howells PAGECACHE_TAG_DIRTY, 1, &head_page); 71331143d5dSDavid Howells if (!n) 71431143d5dSDavid Howells break; 71531143d5dSDavid Howells 71678525c74SDavid Howells folio = page_folio(head_page); 71778525c74SDavid Howells start = folio_pos(folio); /* May regress with THPs */ 718e87b03f5SDavid Howells 71978525c74SDavid Howells _debug("wback %lx", folio_index(folio)); 72031143d5dSDavid Howells 721e87b03f5SDavid Howells /* At this point we hold neither the i_pages lock nor the 722b93b0163SMatthew Wilcox * page lock: the page may be truncated or invalidated 723b93b0163SMatthew Wilcox * (changing page->mapping to NULL), or even swizzled 724b93b0163SMatthew Wilcox * back from swapper_space to tmpfs file mapping 72531143d5dSDavid Howells */ 726e87b03f5SDavid Howells if (wbc->sync_mode != WB_SYNC_NONE) { 72778525c74SDavid Howells ret = folio_lock_killable(folio); 7284343d008SDavid Howells if (ret < 0) { 72978525c74SDavid Howells folio_put(folio); 7304343d008SDavid Howells return ret; 7314343d008SDavid Howells } 732e87b03f5SDavid Howells } else { 73378525c74SDavid Howells if (!folio_trylock(folio)) { 73478525c74SDavid Howells folio_put(folio); 735e87b03f5SDavid Howells return 0; 736e87b03f5SDavid Howells } 737e87b03f5SDavid Howells } 73831143d5dSDavid Howells 73978525c74SDavid Howells if (folio_mapping(folio) != mapping || 74078525c74SDavid Howells !folio_test_dirty(folio)) { 74178525c74SDavid Howells start += folio_size(folio); 74278525c74SDavid Howells folio_unlock(folio); 74378525c74SDavid Howells folio_put(folio); 74431143d5dSDavid Howells continue; 74531143d5dSDavid Howells } 74631143d5dSDavid Howells 747c7f75ef3SDavid Howells if (folio_test_writeback(folio) || 748c7f75ef3SDavid Howells folio_test_fscache(folio)) { 74978525c74SDavid Howells folio_unlock(folio); 750c7f75ef3SDavid Howells if (wbc->sync_mode != WB_SYNC_NONE) { 75178525c74SDavid Howells folio_wait_writeback(folio); 752c7f75ef3SDavid Howells #ifdef CONFIG_AFS_FSCACHE 753c7f75ef3SDavid Howells folio_wait_fscache(folio); 754c7f75ef3SDavid Howells #endif 755173ce1caSDavid Howells } else { 756173ce1caSDavid Howells start += folio_size(folio); 757c7f75ef3SDavid Howells } 75878525c74SDavid Howells folio_put(folio); 759173ce1caSDavid Howells if (wbc->sync_mode == WB_SYNC_NONE) { 760173ce1caSDavid Howells if (skips >= 5 || need_resched()) 761173ce1caSDavid Howells break; 762173ce1caSDavid Howells skips++; 763173ce1caSDavid Howells } 76431143d5dSDavid Howells continue; 76531143d5dSDavid Howells } 76631143d5dSDavid Howells 76778525c74SDavid Howells if (!folio_clear_dirty_for_io(folio)) 76865a15109SDavid Howells BUG(); 76978525c74SDavid Howells ret = afs_write_back_from_locked_folio(mapping, wbc, folio, start, end); 77078525c74SDavid Howells folio_put(folio); 77131143d5dSDavid Howells if (ret < 0) { 772e87b03f5SDavid Howells _leave(" = %zd", ret); 77331143d5dSDavid Howells return ret; 77431143d5dSDavid Howells } 77531143d5dSDavid Howells 776dc255730SMarc Dionne start += ret; 77731143d5dSDavid Howells 77831143d5dSDavid Howells cond_resched(); 779e87b03f5SDavid Howells } while (wbc->nr_to_write > 0); 78031143d5dSDavid Howells 781e87b03f5SDavid Howells *_next = start; 782e87b03f5SDavid Howells _leave(" = 0 [%llx]", *_next); 78331143d5dSDavid Howells return 0; 78431143d5dSDavid Howells } 78531143d5dSDavid Howells 78631143d5dSDavid Howells /* 78731143d5dSDavid Howells * write some of the pending data back to the server 78831143d5dSDavid Howells */ 78931143d5dSDavid Howells int afs_writepages(struct address_space *mapping, 79031143d5dSDavid Howells struct writeback_control *wbc) 79131143d5dSDavid Howells { 792ec0fa0b6SDavid Howells struct afs_vnode *vnode = AFS_FS_I(mapping->host); 793e87b03f5SDavid Howells loff_t start, next; 79431143d5dSDavid Howells int ret; 79531143d5dSDavid Howells 79631143d5dSDavid Howells _enter(""); 79731143d5dSDavid Howells 798ec0fa0b6SDavid Howells /* We have to be careful as we can end up racing with setattr() 799ec0fa0b6SDavid Howells * truncating the pagecache since the caller doesn't take a lock here 800ec0fa0b6SDavid Howells * to prevent it. 801ec0fa0b6SDavid Howells */ 802ec0fa0b6SDavid Howells if (wbc->sync_mode == WB_SYNC_ALL) 803ec0fa0b6SDavid Howells down_read(&vnode->validate_lock); 804ec0fa0b6SDavid Howells else if (!down_read_trylock(&vnode->validate_lock)) 805ec0fa0b6SDavid Howells return 0; 806ec0fa0b6SDavid Howells 80731143d5dSDavid Howells if (wbc->range_cyclic) { 808e87b03f5SDavid Howells start = mapping->writeback_index * PAGE_SIZE; 809e87b03f5SDavid Howells ret = afs_writepages_region(mapping, wbc, start, LLONG_MAX, &next); 810afe69498STom Rix if (ret == 0) { 811e87b03f5SDavid Howells mapping->writeback_index = next / PAGE_SIZE; 812afe69498STom Rix if (start > 0 && wbc->nr_to_write > 0) { 813afe69498STom Rix ret = afs_writepages_region(mapping, wbc, 0, 814afe69498STom Rix start, &next); 815afe69498STom Rix if (ret == 0) 816afe69498STom Rix mapping->writeback_index = 817afe69498STom Rix next / PAGE_SIZE; 818afe69498STom Rix } 819afe69498STom Rix } 82031143d5dSDavid Howells } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) { 821e87b03f5SDavid Howells ret = afs_writepages_region(mapping, wbc, 0, LLONG_MAX, &next); 822afe69498STom Rix if (wbc->nr_to_write > 0 && ret == 0) 8235a972474SDavid Howells mapping->writeback_index = next / PAGE_SIZE; 82431143d5dSDavid Howells } else { 825e87b03f5SDavid Howells ret = afs_writepages_region(mapping, wbc, 826e87b03f5SDavid Howells wbc->range_start, wbc->range_end, &next); 82731143d5dSDavid Howells } 82831143d5dSDavid Howells 829ec0fa0b6SDavid Howells up_read(&vnode->validate_lock); 83031143d5dSDavid Howells _leave(" = %d", ret); 83131143d5dSDavid Howells return ret; 83231143d5dSDavid Howells } 83331143d5dSDavid Howells 83431143d5dSDavid Howells /* 83531143d5dSDavid Howells * write to an AFS file 83631143d5dSDavid Howells */ 83750b5551dSAl Viro ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from) 83831143d5dSDavid Howells { 839496ad9aaSAl Viro struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp)); 8403978d816SDavid Howells struct afs_file *af = iocb->ki_filp->private_data; 84131143d5dSDavid Howells ssize_t result; 84250b5551dSAl Viro size_t count = iov_iter_count(from); 84331143d5dSDavid Howells 8443b6492dfSDavid Howells _enter("{%llx:%llu},{%zu},", 84550b5551dSAl Viro vnode->fid.vid, vnode->fid.vnode, count); 84631143d5dSDavid Howells 84731143d5dSDavid Howells if (IS_SWAPFILE(&vnode->vfs_inode)) { 84831143d5dSDavid Howells printk(KERN_INFO 84931143d5dSDavid Howells "AFS: Attempt to write to active swap file!\n"); 85031143d5dSDavid Howells return -EBUSY; 85131143d5dSDavid Howells } 85231143d5dSDavid Howells 85331143d5dSDavid Howells if (!count) 85431143d5dSDavid Howells return 0; 85531143d5dSDavid Howells 8563978d816SDavid Howells result = afs_validate(vnode, af->key); 8573978d816SDavid Howells if (result < 0) 8583978d816SDavid Howells return result; 8593978d816SDavid Howells 86050b5551dSAl Viro result = generic_file_write_iter(iocb, from); 86131143d5dSDavid Howells 86231143d5dSDavid Howells _leave(" = %zd", result); 86331143d5dSDavid Howells return result; 86431143d5dSDavid Howells } 86531143d5dSDavid Howells 86631143d5dSDavid Howells /* 86731143d5dSDavid Howells * flush any dirty pages for this process, and check for write errors. 86831143d5dSDavid Howells * - the return status from this call provides a reliable indication of 86931143d5dSDavid Howells * whether any write errors occurred for this process. 87031143d5dSDavid Howells */ 87102c24a82SJosef Bacik int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync) 87231143d5dSDavid Howells { 8733978d816SDavid Howells struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 8743978d816SDavid Howells struct afs_file *af = file->private_data; 8753978d816SDavid Howells int ret; 87631143d5dSDavid Howells 8773b6492dfSDavid Howells _enter("{%llx:%llu},{n=%pD},%d", 8783c981bfcSAl Viro vnode->fid.vid, vnode->fid.vnode, file, 87931143d5dSDavid Howells datasync); 88031143d5dSDavid Howells 8813978d816SDavid Howells ret = afs_validate(vnode, af->key); 8823978d816SDavid Howells if (ret < 0) 8833978d816SDavid Howells return ret; 8843978d816SDavid Howells 8854343d008SDavid Howells return file_write_and_wait_range(file, start, end); 88631143d5dSDavid Howells } 8879b3f26c9SDavid Howells 8889b3f26c9SDavid Howells /* 8899b3f26c9SDavid Howells * notification that a previously read-only page is about to become writable 8909b3f26c9SDavid Howells * - if it returns an error, the caller will deliver a bus error signal 8919b3f26c9SDavid Howells */ 8920722f186SSouptick Joarder vm_fault_t afs_page_mkwrite(struct vm_fault *vmf) 8939b3f26c9SDavid Howells { 894490e016fSMatthew Wilcox (Oracle) struct folio *folio = page_folio(vmf->page); 8951cf7a151SDavid Howells struct file *file = vmf->vma->vm_file; 8961cf7a151SDavid Howells struct inode *inode = file_inode(file); 8971cf7a151SDavid Howells struct afs_vnode *vnode = AFS_FS_I(inode); 8983978d816SDavid Howells struct afs_file *af = file->private_data; 8991cf7a151SDavid Howells unsigned long priv; 9009620ad86SMatthew Wilcox (Oracle) vm_fault_t ret = VM_FAULT_RETRY; 9019b3f26c9SDavid Howells 90278525c74SDavid Howells _enter("{{%llx:%llu}},{%lx}", vnode->fid.vid, vnode->fid.vnode, folio_index(folio)); 9039b3f26c9SDavid Howells 9043978d816SDavid Howells afs_validate(vnode, af->key); 9053978d816SDavid Howells 9061cf7a151SDavid Howells sb_start_pagefault(inode->i_sb); 9071cf7a151SDavid Howells 9081cf7a151SDavid Howells /* Wait for the page to be written to the cache before we allow it to 9091cf7a151SDavid Howells * be modified. We then assume the entire page will need writing back. 9101cf7a151SDavid Howells */ 911630f5ddaSDavid Howells #ifdef CONFIG_AFS_FSCACHE 91278525c74SDavid Howells if (folio_test_fscache(folio) && 91378525c74SDavid Howells folio_wait_fscache_killable(folio) < 0) 9149620ad86SMatthew Wilcox (Oracle) goto out; 915630f5ddaSDavid Howells #endif 9169b3f26c9SDavid Howells 917490e016fSMatthew Wilcox (Oracle) if (folio_wait_writeback_killable(folio)) 9189620ad86SMatthew Wilcox (Oracle) goto out; 9191cf7a151SDavid Howells 92078525c74SDavid Howells if (folio_lock_killable(folio) < 0) 9219620ad86SMatthew Wilcox (Oracle) goto out; 9221cf7a151SDavid Howells 92378525c74SDavid Howells /* We mustn't change folio->private until writeback is complete as that 9241cf7a151SDavid Howells * details the portion of the page we need to write back and we might 9251cf7a151SDavid Howells * need to redirty the page if there's a problem. 9261cf7a151SDavid Howells */ 927490e016fSMatthew Wilcox (Oracle) if (folio_wait_writeback_killable(folio) < 0) { 928490e016fSMatthew Wilcox (Oracle) folio_unlock(folio); 9299620ad86SMatthew Wilcox (Oracle) goto out; 9305cbf0398SDavid Howells } 9311cf7a151SDavid Howells 93278525c74SDavid Howells priv = afs_folio_dirty(folio, 0, folio_size(folio)); 93378525c74SDavid Howells priv = afs_folio_dirty_mmapped(priv); 93478525c74SDavid Howells if (folio_test_private(folio)) { 93578525c74SDavid Howells folio_change_private(folio, (void *)priv); 93678525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("mkwrite+"), folio); 937e87b03f5SDavid Howells } else { 93878525c74SDavid Howells folio_attach_private(folio, (void *)priv); 93978525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("mkwrite"), folio); 940e87b03f5SDavid Howells } 941bb413489SDavid Howells file_update_time(file); 9421cf7a151SDavid Howells 9439620ad86SMatthew Wilcox (Oracle) ret = VM_FAULT_LOCKED; 9449620ad86SMatthew Wilcox (Oracle) out: 9451cf7a151SDavid Howells sb_end_pagefault(inode->i_sb); 9469620ad86SMatthew Wilcox (Oracle) return ret; 9479b3f26c9SDavid Howells } 9484343d008SDavid Howells 9494343d008SDavid Howells /* 9504343d008SDavid Howells * Prune the keys cached for writeback. The caller must hold vnode->wb_lock. 9514343d008SDavid Howells */ 9524343d008SDavid Howells void afs_prune_wb_keys(struct afs_vnode *vnode) 9534343d008SDavid Howells { 9544343d008SDavid Howells LIST_HEAD(graveyard); 9554343d008SDavid Howells struct afs_wb_key *wbk, *tmp; 9564343d008SDavid Howells 9574343d008SDavid Howells /* Discard unused keys */ 9584343d008SDavid Howells spin_lock(&vnode->wb_lock); 9594343d008SDavid Howells 9604343d008SDavid Howells if (!mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_WRITEBACK) && 9614343d008SDavid Howells !mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_DIRTY)) { 9624343d008SDavid Howells list_for_each_entry_safe(wbk, tmp, &vnode->wb_keys, vnode_link) { 9634343d008SDavid Howells if (refcount_read(&wbk->usage) == 1) 9644343d008SDavid Howells list_move(&wbk->vnode_link, &graveyard); 9654343d008SDavid Howells } 9664343d008SDavid Howells } 9674343d008SDavid Howells 9684343d008SDavid Howells spin_unlock(&vnode->wb_lock); 9694343d008SDavid Howells 9704343d008SDavid Howells while (!list_empty(&graveyard)) { 9714343d008SDavid Howells wbk = list_entry(graveyard.next, struct afs_wb_key, vnode_link); 9724343d008SDavid Howells list_del(&wbk->vnode_link); 9734343d008SDavid Howells afs_put_wb_key(wbk); 9744343d008SDavid Howells } 9754343d008SDavid Howells } 9764343d008SDavid Howells 9774343d008SDavid Howells /* 9784343d008SDavid Howells * Clean up a page during invalidation. 9794343d008SDavid Howells */ 980a42442ddSMatthew Wilcox (Oracle) int afs_launder_folio(struct folio *folio) 9814343d008SDavid Howells { 98278525c74SDavid Howells struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio)); 983bd80d8a8SDavid Howells struct iov_iter iter; 984bd80d8a8SDavid Howells struct bio_vec bv[1]; 9854343d008SDavid Howells unsigned long priv; 9864343d008SDavid Howells unsigned int f, t; 9874343d008SDavid Howells int ret = 0; 9884343d008SDavid Howells 989a42442ddSMatthew Wilcox (Oracle) _enter("{%lx}", folio->index); 9904343d008SDavid Howells 99178525c74SDavid Howells priv = (unsigned long)folio_get_private(folio); 99278525c74SDavid Howells if (folio_clear_dirty_for_io(folio)) { 9934343d008SDavid Howells f = 0; 99478525c74SDavid Howells t = folio_size(folio); 99578525c74SDavid Howells if (folio_test_private(folio)) { 99678525c74SDavid Howells f = afs_folio_dirty_from(folio, priv); 99778525c74SDavid Howells t = afs_folio_dirty_to(folio, priv); 9984343d008SDavid Howells } 9994343d008SDavid Howells 100078525c74SDavid Howells bv[0].bv_page = &folio->page; 1001bd80d8a8SDavid Howells bv[0].bv_offset = f; 1002bd80d8a8SDavid Howells bv[0].bv_len = t - f; 1003bd80d8a8SDavid Howells iov_iter_bvec(&iter, WRITE, bv, 1, bv[0].bv_len); 1004bd80d8a8SDavid Howells 100578525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("launder"), folio); 100678525c74SDavid Howells ret = afs_store_data(vnode, &iter, folio_pos(folio) + f, true); 10074343d008SDavid Howells } 10084343d008SDavid Howells 100978525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("laundered"), folio); 101078525c74SDavid Howells folio_detach_private(folio); 101178525c74SDavid Howells folio_wait_fscache(folio); 10124343d008SDavid Howells return ret; 101331143d5dSDavid Howells } 1014c7f75ef3SDavid Howells 1015c7f75ef3SDavid Howells /* 1016c7f75ef3SDavid Howells * Deal with the completion of writing the data to the cache. 1017c7f75ef3SDavid Howells */ 1018c7f75ef3SDavid Howells static void afs_write_to_cache_done(void *priv, ssize_t transferred_or_error, 1019c7f75ef3SDavid Howells bool was_async) 1020c7f75ef3SDavid Howells { 1021c7f75ef3SDavid Howells struct afs_vnode *vnode = priv; 1022c7f75ef3SDavid Howells 1023c7f75ef3SDavid Howells if (IS_ERR_VALUE(transferred_or_error) && 1024c7f75ef3SDavid Howells transferred_or_error != -ENOBUFS) 1025c7f75ef3SDavid Howells afs_invalidate_cache(vnode, 0); 1026c7f75ef3SDavid Howells } 1027c7f75ef3SDavid Howells 1028c7f75ef3SDavid Howells /* 1029c7f75ef3SDavid Howells * Save the write to the cache also. 1030c7f75ef3SDavid Howells */ 1031c7f75ef3SDavid Howells static void afs_write_to_cache(struct afs_vnode *vnode, 1032c7f75ef3SDavid Howells loff_t start, size_t len, loff_t i_size, 1033c7f75ef3SDavid Howells bool caching) 1034c7f75ef3SDavid Howells { 1035c7f75ef3SDavid Howells fscache_write_to_cache(afs_vnode_cache(vnode), 1036c7f75ef3SDavid Howells vnode->vfs_inode.i_mapping, start, len, i_size, 1037c7f75ef3SDavid Howells afs_write_to_cache_done, vnode, caching); 1038c7f75ef3SDavid Howells } 1039