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 */ 2531143d5dSDavid Howells int afs_set_page_dirty(struct page *page) 2631143d5dSDavid Howells { 27c7f75ef3SDavid Howells return fscache_set_page_dirty(page, afs_vnode_cache(AFS_FS_I(page->mapping->host))); 2831143d5dSDavid Howells } 29c7f75ef3SDavid Howells static void afs_folio_start_fscache(bool caching, struct folio *folio) 30c7f75ef3SDavid Howells { 31c7f75ef3SDavid Howells if (caching) 32c7f75ef3SDavid Howells folio_start_fscache(folio); 33c7f75ef3SDavid Howells } 34c7f75ef3SDavid Howells #else 35c7f75ef3SDavid Howells static void afs_folio_start_fscache(bool caching, struct folio *folio) 36c7f75ef3SDavid Howells { 37c7f75ef3SDavid Howells } 38c7f75ef3SDavid Howells #endif 3931143d5dSDavid Howells 4031143d5dSDavid Howells /* 4131143d5dSDavid Howells * prepare to perform part of a write to a page 4231143d5dSDavid Howells */ 4315b4650eSNick Piggin int afs_write_begin(struct file *file, struct address_space *mapping, 4415b4650eSNick Piggin loff_t pos, unsigned len, unsigned flags, 4521db2cdcSDavid Howells struct page **_page, void **fsdata) 4631143d5dSDavid Howells { 47496ad9aaSAl Viro struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 4878525c74SDavid Howells struct folio *folio; 494343d008SDavid Howells unsigned long priv; 50e87b03f5SDavid Howells unsigned f, from; 51e87b03f5SDavid Howells unsigned t, to; 52e87b03f5SDavid Howells pgoff_t index; 5331143d5dSDavid Howells int ret; 5431143d5dSDavid Howells 55e87b03f5SDavid Howells _enter("{%llx:%llu},%llx,%x", 56e87b03f5SDavid Howells vnode->fid.vid, vnode->fid.vnode, pos, len); 5731143d5dSDavid Howells 583003bbd0SDavid Howells /* Prefetch area to be written into the cache if we're caching this 593003bbd0SDavid Howells * file. We need to do this before we get a lock on the page in case 603003bbd0SDavid Howells * there's more than one writer competing for the same cache block. 613003bbd0SDavid Howells */ 6278525c74SDavid Howells ret = netfs_write_begin(file, mapping, pos, len, flags, &folio, fsdata, 633003bbd0SDavid Howells &afs_req_ops, NULL); 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 { 357e49c7b2fSDavid Howells struct afs_operation *op; 3584343d008SDavid Howells struct afs_wb_key *wbk = NULL; 359bd80d8a8SDavid Howells loff_t size = iov_iter_count(iter), i_size; 360bd80d8a8SDavid Howells int ret = -ENOKEY; 361d2ddc776SDavid Howells 362bd80d8a8SDavid Howells _enter("%s{%llx:%llu.%u},%llx,%llx", 363d2ddc776SDavid Howells vnode->volume->name, 364d2ddc776SDavid Howells vnode->fid.vid, 365d2ddc776SDavid Howells vnode->fid.vnode, 366d2ddc776SDavid Howells vnode->fid.unique, 367bd80d8a8SDavid Howells size, pos); 368d2ddc776SDavid Howells 369e49c7b2fSDavid Howells ret = afs_get_writeback_key(vnode, &wbk); 370e49c7b2fSDavid Howells if (ret) { 3714343d008SDavid Howells _leave(" = %d [no keys]", ret); 3724343d008SDavid Howells return ret; 373d2ddc776SDavid Howells } 374d2ddc776SDavid Howells 375e49c7b2fSDavid Howells op = afs_alloc_operation(wbk->key, vnode->volume); 376e49c7b2fSDavid Howells if (IS_ERR(op)) { 377e49c7b2fSDavid Howells afs_put_wb_key(wbk); 378e49c7b2fSDavid Howells return -ENOMEM; 379d2ddc776SDavid Howells } 380d2ddc776SDavid Howells 381bd80d8a8SDavid Howells i_size = i_size_read(&vnode->vfs_inode); 382bd80d8a8SDavid Howells 383e49c7b2fSDavid Howells afs_op_set_vnode(op, 0, vnode); 384e49c7b2fSDavid Howells op->file[0].dv_delta = 1; 38522650f14SDavid Howells op->file[0].modification = true; 386bd80d8a8SDavid Howells op->store.write_iter = iter; 387bd80d8a8SDavid Howells op->store.pos = pos; 388bd80d8a8SDavid Howells op->store.size = size; 389bd80d8a8SDavid Howells op->store.i_size = max(pos + size, i_size); 390d383e346SDavid Howells op->store.laundering = laundering; 391b3597945SDavid Howells op->mtime = vnode->vfs_inode.i_mtime; 392811f04baSDavid Howells op->flags |= AFS_OPERATION_UNINTR; 393e49c7b2fSDavid Howells op->ops = &afs_store_data_operation; 394e49c7b2fSDavid Howells 395e49c7b2fSDavid Howells try_next_key: 396e49c7b2fSDavid Howells afs_begin_vnode_operation(op); 397e49c7b2fSDavid Howells afs_wait_for_operation(op); 398e49c7b2fSDavid Howells 399e49c7b2fSDavid Howells switch (op->error) { 4004343d008SDavid Howells case -EACCES: 4014343d008SDavid Howells case -EPERM: 4024343d008SDavid Howells case -ENOKEY: 4034343d008SDavid Howells case -EKEYEXPIRED: 4044343d008SDavid Howells case -EKEYREJECTED: 4054343d008SDavid Howells case -EKEYREVOKED: 4064343d008SDavid Howells _debug("next"); 407e49c7b2fSDavid Howells 408e49c7b2fSDavid Howells ret = afs_get_writeback_key(vnode, &wbk); 409e49c7b2fSDavid Howells if (ret == 0) { 410e49c7b2fSDavid Howells key_put(op->key); 411e49c7b2fSDavid Howells op->key = key_get(wbk->key); 4124343d008SDavid Howells goto try_next_key; 4134343d008SDavid Howells } 414e49c7b2fSDavid Howells break; 415e49c7b2fSDavid Howells } 4164343d008SDavid Howells 4174343d008SDavid Howells afs_put_wb_key(wbk); 418e49c7b2fSDavid Howells _leave(" = %d", op->error); 419e49c7b2fSDavid Howells return afs_put_operation(op); 420d2ddc776SDavid Howells } 421d2ddc776SDavid Howells 422d2ddc776SDavid Howells /* 423810caa3eSDavid Howells * Extend the region to be written back to include subsequent contiguously 424810caa3eSDavid Howells * dirty pages if possible, but don't sleep while doing so. 425810caa3eSDavid Howells * 426810caa3eSDavid Howells * If this page holds new content, then we can include filler zeros in the 427810caa3eSDavid Howells * writeback. 42831143d5dSDavid Howells */ 429810caa3eSDavid Howells static void afs_extend_writeback(struct address_space *mapping, 430810caa3eSDavid Howells struct afs_vnode *vnode, 431810caa3eSDavid Howells long *_count, 432e87b03f5SDavid Howells loff_t start, 433e87b03f5SDavid Howells loff_t max_len, 434e87b03f5SDavid Howells bool new_content, 435c7f75ef3SDavid Howells bool caching, 436e87b03f5SDavid Howells unsigned int *_len) 43731143d5dSDavid Howells { 438e87b03f5SDavid Howells struct pagevec pvec; 43978525c74SDavid Howells struct folio *folio; 440e87b03f5SDavid Howells unsigned long priv; 441e87b03f5SDavid Howells unsigned int psize, filler = 0; 442e87b03f5SDavid Howells unsigned int f, t; 443e87b03f5SDavid Howells loff_t len = *_len; 444e87b03f5SDavid Howells pgoff_t index = (start + len) / PAGE_SIZE; 445e87b03f5SDavid Howells bool stop = true; 446e87b03f5SDavid Howells unsigned int i; 4474343d008SDavid Howells 448e87b03f5SDavid Howells XA_STATE(xas, &mapping->i_pages, index); 449e87b03f5SDavid Howells pagevec_init(&pvec); 450e87b03f5SDavid Howells 45131143d5dSDavid Howells do { 452e87b03f5SDavid Howells /* Firstly, we gather up a batch of contiguous dirty pages 453e87b03f5SDavid Howells * under the RCU read lock - but we can't clear the dirty flags 454e87b03f5SDavid Howells * there if any of those pages are mapped. 455e87b03f5SDavid Howells */ 456e87b03f5SDavid Howells rcu_read_lock(); 457e87b03f5SDavid Howells 45878525c74SDavid Howells xas_for_each(&xas, folio, ULONG_MAX) { 459e87b03f5SDavid Howells stop = true; 46078525c74SDavid Howells if (xas_retry(&xas, folio)) 461e87b03f5SDavid Howells continue; 46278525c74SDavid Howells if (xa_is_value(folio)) 463e87b03f5SDavid Howells break; 46478525c74SDavid Howells if (folio_index(folio) != index) 465e87b03f5SDavid Howells break; 466e87b03f5SDavid Howells 46778525c74SDavid Howells if (!folio_try_get_rcu(folio)) { 468e87b03f5SDavid Howells xas_reset(&xas); 469e87b03f5SDavid Howells continue; 47031143d5dSDavid Howells } 47131143d5dSDavid Howells 472e87b03f5SDavid Howells /* Has the page moved or been split? */ 47378525c74SDavid Howells if (unlikely(folio != xas_reload(&xas))) { 47478525c74SDavid Howells folio_put(folio); 4755a813276SDavid Howells break; 476581b2027SDavid Howells } 477e87b03f5SDavid Howells 47878525c74SDavid Howells if (!folio_trylock(folio)) { 47978525c74SDavid Howells folio_put(folio); 48031143d5dSDavid Howells break; 481581b2027SDavid Howells } 482c7f75ef3SDavid Howells if (!folio_test_dirty(folio) || 483c7f75ef3SDavid Howells folio_test_writeback(folio) || 484c7f75ef3SDavid Howells folio_test_fscache(folio)) { 48578525c74SDavid Howells folio_unlock(folio); 48678525c74SDavid Howells folio_put(folio); 48731143d5dSDavid Howells break; 48831143d5dSDavid Howells } 4894343d008SDavid Howells 49078525c74SDavid Howells psize = folio_size(folio); 49178525c74SDavid Howells priv = (unsigned long)folio_get_private(folio); 49278525c74SDavid Howells f = afs_folio_dirty_from(folio, priv); 49378525c74SDavid Howells t = afs_folio_dirty_to(folio, priv); 494810caa3eSDavid Howells if (f != 0 && !new_content) { 49578525c74SDavid Howells folio_unlock(folio); 49678525c74SDavid Howells folio_put(folio); 4974343d008SDavid Howells break; 4984343d008SDavid Howells } 4994343d008SDavid Howells 500e87b03f5SDavid Howells len += filler + t; 501e87b03f5SDavid Howells filler = psize - t; 502e87b03f5SDavid Howells if (len >= max_len || *_count <= 0) 503e87b03f5SDavid Howells stop = true; 504e87b03f5SDavid Howells else if (t == psize || new_content) 505e87b03f5SDavid Howells stop = false; 506e87b03f5SDavid Howells 50778525c74SDavid Howells index += folio_nr_pages(folio); 50878525c74SDavid Howells if (!pagevec_add(&pvec, &folio->page)) 509e87b03f5SDavid Howells break; 510e87b03f5SDavid Howells if (stop) 511e87b03f5SDavid Howells break; 512e87b03f5SDavid Howells } 513e87b03f5SDavid Howells 514e87b03f5SDavid Howells if (!stop) 515e87b03f5SDavid Howells xas_pause(&xas); 516e87b03f5SDavid Howells rcu_read_unlock(); 517e87b03f5SDavid Howells 518e87b03f5SDavid Howells /* Now, if we obtained any pages, we can shift them to being 519e87b03f5SDavid Howells * writable and mark them for caching. 520e87b03f5SDavid Howells */ 521e87b03f5SDavid Howells if (!pagevec_count(&pvec)) 522e87b03f5SDavid Howells break; 523e87b03f5SDavid Howells 524e87b03f5SDavid Howells for (i = 0; i < pagevec_count(&pvec); i++) { 52578525c74SDavid Howells folio = page_folio(pvec.pages[i]); 52678525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("store+"), folio); 52713524ab3SDavid Howells 52878525c74SDavid Howells if (!folio_clear_dirty_for_io(folio)) 52931143d5dSDavid Howells BUG(); 53078525c74SDavid Howells if (folio_start_writeback(folio)) 53131143d5dSDavid Howells BUG(); 532c7f75ef3SDavid Howells afs_folio_start_fscache(caching, folio); 533e87b03f5SDavid Howells 53478525c74SDavid Howells *_count -= folio_nr_pages(folio); 53578525c74SDavid Howells folio_unlock(folio); 53631143d5dSDavid Howells } 53731143d5dSDavid Howells 538e87b03f5SDavid Howells pagevec_release(&pvec); 539e87b03f5SDavid Howells cond_resched(); 540e87b03f5SDavid Howells } while (!stop); 54131143d5dSDavid Howells 542e87b03f5SDavid Howells *_len = len; 543810caa3eSDavid Howells } 544810caa3eSDavid Howells 545810caa3eSDavid Howells /* 546810caa3eSDavid Howells * Synchronously write back the locked page and any subsequent non-locked dirty 547810caa3eSDavid Howells * pages. 548810caa3eSDavid Howells */ 54978525c74SDavid Howells static ssize_t afs_write_back_from_locked_folio(struct address_space *mapping, 550810caa3eSDavid Howells struct writeback_control *wbc, 55178525c74SDavid Howells struct folio *folio, 552e87b03f5SDavid Howells loff_t start, loff_t end) 553810caa3eSDavid Howells { 554810caa3eSDavid Howells struct afs_vnode *vnode = AFS_FS_I(mapping->host); 555810caa3eSDavid Howells struct iov_iter iter; 556e87b03f5SDavid Howells unsigned long priv; 557e87b03f5SDavid Howells unsigned int offset, to, len, max_len; 558e87b03f5SDavid Howells loff_t i_size = i_size_read(&vnode->vfs_inode); 559810caa3eSDavid Howells bool new_content = test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags); 560c7f75ef3SDavid Howells bool caching = fscache_cookie_enabled(afs_vnode_cache(vnode)); 561e87b03f5SDavid Howells long count = wbc->nr_to_write; 562810caa3eSDavid Howells int ret; 563810caa3eSDavid Howells 56478525c74SDavid Howells _enter(",%lx,%llx-%llx", folio_index(folio), start, end); 565810caa3eSDavid Howells 56678525c74SDavid Howells if (folio_start_writeback(folio)) 567810caa3eSDavid Howells BUG(); 568c7f75ef3SDavid Howells afs_folio_start_fscache(caching, folio); 569810caa3eSDavid Howells 57078525c74SDavid Howells count -= folio_nr_pages(folio); 571e87b03f5SDavid Howells 572810caa3eSDavid Howells /* Find all consecutive lockable dirty pages that have contiguous 573810caa3eSDavid Howells * written regions, stopping when we find a page that is not 574810caa3eSDavid Howells * immediately lockable, is not dirty or is missing, or we reach the 575810caa3eSDavid Howells * end of the range. 576810caa3eSDavid Howells */ 57778525c74SDavid Howells priv = (unsigned long)folio_get_private(folio); 57878525c74SDavid Howells offset = afs_folio_dirty_from(folio, priv); 57978525c74SDavid Howells to = afs_folio_dirty_to(folio, priv); 58078525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("store"), folio); 581810caa3eSDavid Howells 582e87b03f5SDavid Howells len = to - offset; 583e87b03f5SDavid Howells start += offset; 584e87b03f5SDavid Howells if (start < i_size) { 585e87b03f5SDavid Howells /* Trim the write to the EOF; the extra data is ignored. Also 586e87b03f5SDavid Howells * put an upper limit on the size of a single storedata op. 587e87b03f5SDavid Howells */ 588e87b03f5SDavid Howells max_len = 65536 * 4096; 589e87b03f5SDavid Howells max_len = min_t(unsigned long long, max_len, end - start + 1); 590e87b03f5SDavid Howells max_len = min_t(unsigned long long, max_len, i_size - start); 591810caa3eSDavid Howells 592e87b03f5SDavid Howells if (len < max_len && 59378525c74SDavid Howells (to == folio_size(folio) || new_content)) 594e87b03f5SDavid Howells afs_extend_writeback(mapping, vnode, &count, 595c7f75ef3SDavid Howells start, max_len, new_content, 596c7f75ef3SDavid Howells caching, &len); 597e87b03f5SDavid Howells len = min_t(loff_t, len, max_len); 598e87b03f5SDavid Howells } 599810caa3eSDavid Howells 6004343d008SDavid Howells /* We now have a contiguous set of dirty pages, each with writeback 6014343d008SDavid Howells * set; the first page is still locked at this point, but all the rest 6024343d008SDavid Howells * have been unlocked. 6034343d008SDavid Howells */ 60478525c74SDavid Howells folio_unlock(folio); 6054343d008SDavid Howells 606e87b03f5SDavid Howells if (start < i_size) { 607e87b03f5SDavid Howells _debug("write back %x @%llx [%llx]", len, start, i_size); 60831143d5dSDavid Howells 609c7f75ef3SDavid Howells /* Speculatively write to the cache. We have to fix this up 610c7f75ef3SDavid Howells * later if the store fails. 611c7f75ef3SDavid Howells */ 612c7f75ef3SDavid Howells afs_write_to_cache(vnode, start, len, i_size, caching); 613c7f75ef3SDavid Howells 614e87b03f5SDavid Howells iov_iter_xarray(&iter, WRITE, &mapping->i_pages, start, len); 615e87b03f5SDavid Howells ret = afs_store_data(vnode, &iter, start, false); 616bd80d8a8SDavid Howells } else { 617e87b03f5SDavid Howells _debug("write discard %x @%llx [%llx]", len, start, i_size); 618e87b03f5SDavid Howells 619bd80d8a8SDavid Howells /* The dirty region was entirely beyond the EOF. */ 620c7f75ef3SDavid Howells fscache_clear_page_bits(afs_vnode_cache(vnode), 621c7f75ef3SDavid Howells mapping, start, len, caching); 622e87b03f5SDavid Howells afs_pages_written_back(vnode, start, len); 623bd80d8a8SDavid Howells ret = 0; 624bd80d8a8SDavid Howells } 625bd80d8a8SDavid Howells 62631143d5dSDavid Howells switch (ret) { 6274343d008SDavid Howells case 0: 628e87b03f5SDavid Howells wbc->nr_to_write = count; 629e87b03f5SDavid Howells ret = len; 6304343d008SDavid Howells break; 6314343d008SDavid Howells 6324343d008SDavid Howells default: 6334343d008SDavid Howells pr_notice("kAFS: Unexpected error from FS.StoreData %d\n", ret); 634df561f66SGustavo A. R. Silva fallthrough; 6354343d008SDavid Howells case -EACCES: 6364343d008SDavid Howells case -EPERM: 6374343d008SDavid Howells case -ENOKEY: 6384343d008SDavid Howells case -EKEYEXPIRED: 6394343d008SDavid Howells case -EKEYREJECTED: 6404343d008SDavid Howells case -EKEYREVOKED: 641e87b03f5SDavid Howells afs_redirty_pages(wbc, mapping, start, len); 6424343d008SDavid Howells mapping_set_error(mapping, ret); 6434343d008SDavid Howells break; 6444343d008SDavid Howells 64531143d5dSDavid Howells case -EDQUOT: 64631143d5dSDavid Howells case -ENOSPC: 647e87b03f5SDavid Howells afs_redirty_pages(wbc, mapping, start, len); 6484343d008SDavid Howells mapping_set_error(mapping, -ENOSPC); 64931143d5dSDavid Howells break; 6504343d008SDavid Howells 65131143d5dSDavid Howells case -EROFS: 65231143d5dSDavid Howells case -EIO: 65331143d5dSDavid Howells case -EREMOTEIO: 65431143d5dSDavid Howells case -EFBIG: 65531143d5dSDavid Howells case -ENOENT: 65631143d5dSDavid Howells case -ENOMEDIUM: 65731143d5dSDavid Howells case -ENXIO: 658f51375cdSDavid Howells trace_afs_file_error(vnode, ret, afs_file_error_writeback_fail); 659e87b03f5SDavid Howells afs_kill_pages(mapping, start, len); 6604343d008SDavid Howells mapping_set_error(mapping, ret); 66131143d5dSDavid Howells break; 66231143d5dSDavid Howells } 66331143d5dSDavid Howells 66431143d5dSDavid Howells _leave(" = %d", ret); 66531143d5dSDavid Howells return ret; 66631143d5dSDavid Howells } 66731143d5dSDavid Howells 66831143d5dSDavid Howells /* 66931143d5dSDavid Howells * write a page back to the server 67031143d5dSDavid Howells * - the caller locked the page for us 67131143d5dSDavid Howells */ 67278525c74SDavid Howells int afs_writepage(struct page *subpage, struct writeback_control *wbc) 67331143d5dSDavid Howells { 67478525c74SDavid Howells struct folio *folio = page_folio(subpage); 675e87b03f5SDavid Howells ssize_t ret; 676e87b03f5SDavid Howells loff_t start; 67731143d5dSDavid Howells 67878525c74SDavid Howells _enter("{%lx},", folio_index(folio)); 67931143d5dSDavid Howells 680c7f75ef3SDavid Howells #ifdef CONFIG_AFS_FSCACHE 681c7f75ef3SDavid Howells folio_wait_fscache(folio); 682c7f75ef3SDavid Howells #endif 683c7f75ef3SDavid Howells 68478525c74SDavid Howells start = folio_index(folio) * PAGE_SIZE; 68578525c74SDavid Howells ret = afs_write_back_from_locked_folio(folio_mapping(folio), wbc, 68678525c74SDavid Howells folio, start, LLONG_MAX - start); 68731143d5dSDavid Howells if (ret < 0) { 688e87b03f5SDavid Howells _leave(" = %zd", ret); 689e87b03f5SDavid Howells return ret; 69031143d5dSDavid Howells } 69131143d5dSDavid Howells 69231143d5dSDavid Howells _leave(" = 0"); 69331143d5dSDavid Howells return 0; 69431143d5dSDavid Howells } 69531143d5dSDavid Howells 69631143d5dSDavid Howells /* 69731143d5dSDavid Howells * write a region of pages back to the server 69831143d5dSDavid Howells */ 699c1206a2cSAdrian Bunk static int afs_writepages_region(struct address_space *mapping, 70031143d5dSDavid Howells struct writeback_control *wbc, 701e87b03f5SDavid Howells loff_t start, loff_t end, loff_t *_next) 70231143d5dSDavid Howells { 70378525c74SDavid Howells struct folio *folio; 70478525c74SDavid Howells struct page *head_page; 705e87b03f5SDavid Howells ssize_t ret; 706*173ce1caSDavid Howells int n, skips = 0; 70731143d5dSDavid Howells 708e87b03f5SDavid Howells _enter("%llx,%llx,", start, end); 70931143d5dSDavid Howells 71031143d5dSDavid Howells do { 711e87b03f5SDavid Howells pgoff_t index = start / PAGE_SIZE; 712e87b03f5SDavid Howells 713e87b03f5SDavid Howells n = find_get_pages_range_tag(mapping, &index, end / PAGE_SIZE, 71478525c74SDavid Howells PAGECACHE_TAG_DIRTY, 1, &head_page); 71531143d5dSDavid Howells if (!n) 71631143d5dSDavid Howells break; 71731143d5dSDavid Howells 71878525c74SDavid Howells folio = page_folio(head_page); 71978525c74SDavid Howells start = folio_pos(folio); /* May regress with THPs */ 720e87b03f5SDavid Howells 72178525c74SDavid Howells _debug("wback %lx", folio_index(folio)); 72231143d5dSDavid Howells 723e87b03f5SDavid Howells /* At this point we hold neither the i_pages lock nor the 724b93b0163SMatthew Wilcox * page lock: the page may be truncated or invalidated 725b93b0163SMatthew Wilcox * (changing page->mapping to NULL), or even swizzled 726b93b0163SMatthew Wilcox * back from swapper_space to tmpfs file mapping 72731143d5dSDavid Howells */ 728e87b03f5SDavid Howells if (wbc->sync_mode != WB_SYNC_NONE) { 72978525c74SDavid Howells ret = folio_lock_killable(folio); 7304343d008SDavid Howells if (ret < 0) { 73178525c74SDavid Howells folio_put(folio); 7324343d008SDavid Howells return ret; 7334343d008SDavid Howells } 734e87b03f5SDavid Howells } else { 73578525c74SDavid Howells if (!folio_trylock(folio)) { 73678525c74SDavid Howells folio_put(folio); 737e87b03f5SDavid Howells return 0; 738e87b03f5SDavid Howells } 739e87b03f5SDavid Howells } 74031143d5dSDavid Howells 74178525c74SDavid Howells if (folio_mapping(folio) != mapping || 74278525c74SDavid Howells !folio_test_dirty(folio)) { 74378525c74SDavid Howells start += folio_size(folio); 74478525c74SDavid Howells folio_unlock(folio); 74578525c74SDavid Howells folio_put(folio); 74631143d5dSDavid Howells continue; 74731143d5dSDavid Howells } 74831143d5dSDavid Howells 749c7f75ef3SDavid Howells if (folio_test_writeback(folio) || 750c7f75ef3SDavid Howells folio_test_fscache(folio)) { 75178525c74SDavid Howells folio_unlock(folio); 752c7f75ef3SDavid Howells if (wbc->sync_mode != WB_SYNC_NONE) { 75378525c74SDavid Howells folio_wait_writeback(folio); 754c7f75ef3SDavid Howells #ifdef CONFIG_AFS_FSCACHE 755c7f75ef3SDavid Howells folio_wait_fscache(folio); 756c7f75ef3SDavid Howells #endif 757*173ce1caSDavid Howells } else { 758*173ce1caSDavid Howells start += folio_size(folio); 759c7f75ef3SDavid Howells } 76078525c74SDavid Howells folio_put(folio); 761*173ce1caSDavid Howells if (wbc->sync_mode == WB_SYNC_NONE) { 762*173ce1caSDavid Howells if (skips >= 5 || need_resched()) 763*173ce1caSDavid Howells break; 764*173ce1caSDavid Howells skips++; 765*173ce1caSDavid Howells } 76631143d5dSDavid Howells continue; 76731143d5dSDavid Howells } 76831143d5dSDavid Howells 76978525c74SDavid Howells if (!folio_clear_dirty_for_io(folio)) 77065a15109SDavid Howells BUG(); 77178525c74SDavid Howells ret = afs_write_back_from_locked_folio(mapping, wbc, folio, start, end); 77278525c74SDavid Howells folio_put(folio); 77331143d5dSDavid Howells if (ret < 0) { 774e87b03f5SDavid Howells _leave(" = %zd", ret); 77531143d5dSDavid Howells return ret; 77631143d5dSDavid Howells } 77731143d5dSDavid Howells 778dc255730SMarc Dionne start += ret; 77931143d5dSDavid Howells 78031143d5dSDavid Howells cond_resched(); 781e87b03f5SDavid Howells } while (wbc->nr_to_write > 0); 78231143d5dSDavid Howells 783e87b03f5SDavid Howells *_next = start; 784e87b03f5SDavid Howells _leave(" = 0 [%llx]", *_next); 78531143d5dSDavid Howells return 0; 78631143d5dSDavid Howells } 78731143d5dSDavid Howells 78831143d5dSDavid Howells /* 78931143d5dSDavid Howells * write some of the pending data back to the server 79031143d5dSDavid Howells */ 79131143d5dSDavid Howells int afs_writepages(struct address_space *mapping, 79231143d5dSDavid Howells struct writeback_control *wbc) 79331143d5dSDavid Howells { 794ec0fa0b6SDavid Howells struct afs_vnode *vnode = AFS_FS_I(mapping->host); 795e87b03f5SDavid Howells loff_t start, next; 79631143d5dSDavid Howells int ret; 79731143d5dSDavid Howells 79831143d5dSDavid Howells _enter(""); 79931143d5dSDavid Howells 800ec0fa0b6SDavid Howells /* We have to be careful as we can end up racing with setattr() 801ec0fa0b6SDavid Howells * truncating the pagecache since the caller doesn't take a lock here 802ec0fa0b6SDavid Howells * to prevent it. 803ec0fa0b6SDavid Howells */ 804ec0fa0b6SDavid Howells if (wbc->sync_mode == WB_SYNC_ALL) 805ec0fa0b6SDavid Howells down_read(&vnode->validate_lock); 806ec0fa0b6SDavid Howells else if (!down_read_trylock(&vnode->validate_lock)) 807ec0fa0b6SDavid Howells return 0; 808ec0fa0b6SDavid Howells 80931143d5dSDavid Howells if (wbc->range_cyclic) { 810e87b03f5SDavid Howells start = mapping->writeback_index * PAGE_SIZE; 811e87b03f5SDavid Howells ret = afs_writepages_region(mapping, wbc, start, LLONG_MAX, &next); 812afe69498STom Rix if (ret == 0) { 813e87b03f5SDavid Howells mapping->writeback_index = next / PAGE_SIZE; 814afe69498STom Rix if (start > 0 && wbc->nr_to_write > 0) { 815afe69498STom Rix ret = afs_writepages_region(mapping, wbc, 0, 816afe69498STom Rix start, &next); 817afe69498STom Rix if (ret == 0) 818afe69498STom Rix mapping->writeback_index = 819afe69498STom Rix next / PAGE_SIZE; 820afe69498STom Rix } 821afe69498STom Rix } 82231143d5dSDavid Howells } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) { 823e87b03f5SDavid Howells ret = afs_writepages_region(mapping, wbc, 0, LLONG_MAX, &next); 824afe69498STom Rix if (wbc->nr_to_write > 0 && ret == 0) 8255a972474SDavid Howells mapping->writeback_index = next / PAGE_SIZE; 82631143d5dSDavid Howells } else { 827e87b03f5SDavid Howells ret = afs_writepages_region(mapping, wbc, 828e87b03f5SDavid Howells wbc->range_start, wbc->range_end, &next); 82931143d5dSDavid Howells } 83031143d5dSDavid Howells 831ec0fa0b6SDavid Howells up_read(&vnode->validate_lock); 83231143d5dSDavid Howells _leave(" = %d", ret); 83331143d5dSDavid Howells return ret; 83431143d5dSDavid Howells } 83531143d5dSDavid Howells 83631143d5dSDavid Howells /* 83731143d5dSDavid Howells * write to an AFS file 83831143d5dSDavid Howells */ 83950b5551dSAl Viro ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from) 84031143d5dSDavid Howells { 841496ad9aaSAl Viro struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp)); 8423978d816SDavid Howells struct afs_file *af = iocb->ki_filp->private_data; 84331143d5dSDavid Howells ssize_t result; 84450b5551dSAl Viro size_t count = iov_iter_count(from); 84531143d5dSDavid Howells 8463b6492dfSDavid Howells _enter("{%llx:%llu},{%zu},", 84750b5551dSAl Viro vnode->fid.vid, vnode->fid.vnode, count); 84831143d5dSDavid Howells 84931143d5dSDavid Howells if (IS_SWAPFILE(&vnode->vfs_inode)) { 85031143d5dSDavid Howells printk(KERN_INFO 85131143d5dSDavid Howells "AFS: Attempt to write to active swap file!\n"); 85231143d5dSDavid Howells return -EBUSY; 85331143d5dSDavid Howells } 85431143d5dSDavid Howells 85531143d5dSDavid Howells if (!count) 85631143d5dSDavid Howells return 0; 85731143d5dSDavid Howells 8583978d816SDavid Howells result = afs_validate(vnode, af->key); 8593978d816SDavid Howells if (result < 0) 8603978d816SDavid Howells return result; 8613978d816SDavid Howells 86250b5551dSAl Viro result = generic_file_write_iter(iocb, from); 86331143d5dSDavid Howells 86431143d5dSDavid Howells _leave(" = %zd", result); 86531143d5dSDavid Howells return result; 86631143d5dSDavid Howells } 86731143d5dSDavid Howells 86831143d5dSDavid Howells /* 86931143d5dSDavid Howells * flush any dirty pages for this process, and check for write errors. 87031143d5dSDavid Howells * - the return status from this call provides a reliable indication of 87131143d5dSDavid Howells * whether any write errors occurred for this process. 87231143d5dSDavid Howells */ 87302c24a82SJosef Bacik int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync) 87431143d5dSDavid Howells { 8753978d816SDavid Howells struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 8763978d816SDavid Howells struct afs_file *af = file->private_data; 8773978d816SDavid Howells int ret; 87831143d5dSDavid Howells 8793b6492dfSDavid Howells _enter("{%llx:%llu},{n=%pD},%d", 8803c981bfcSAl Viro vnode->fid.vid, vnode->fid.vnode, file, 88131143d5dSDavid Howells datasync); 88231143d5dSDavid Howells 8833978d816SDavid Howells ret = afs_validate(vnode, af->key); 8843978d816SDavid Howells if (ret < 0) 8853978d816SDavid Howells return ret; 8863978d816SDavid Howells 8874343d008SDavid Howells return file_write_and_wait_range(file, start, end); 88831143d5dSDavid Howells } 8899b3f26c9SDavid Howells 8909b3f26c9SDavid Howells /* 8919b3f26c9SDavid Howells * notification that a previously read-only page is about to become writable 8929b3f26c9SDavid Howells * - if it returns an error, the caller will deliver a bus error signal 8939b3f26c9SDavid Howells */ 8940722f186SSouptick Joarder vm_fault_t afs_page_mkwrite(struct vm_fault *vmf) 8959b3f26c9SDavid Howells { 896490e016fSMatthew Wilcox (Oracle) struct folio *folio = page_folio(vmf->page); 8971cf7a151SDavid Howells struct file *file = vmf->vma->vm_file; 8981cf7a151SDavid Howells struct inode *inode = file_inode(file); 8991cf7a151SDavid Howells struct afs_vnode *vnode = AFS_FS_I(inode); 9003978d816SDavid Howells struct afs_file *af = file->private_data; 9011cf7a151SDavid Howells unsigned long priv; 9029620ad86SMatthew Wilcox (Oracle) vm_fault_t ret = VM_FAULT_RETRY; 9039b3f26c9SDavid Howells 90478525c74SDavid Howells _enter("{{%llx:%llu}},{%lx}", vnode->fid.vid, vnode->fid.vnode, folio_index(folio)); 9059b3f26c9SDavid Howells 9063978d816SDavid Howells afs_validate(vnode, af->key); 9073978d816SDavid Howells 9081cf7a151SDavid Howells sb_start_pagefault(inode->i_sb); 9091cf7a151SDavid Howells 9101cf7a151SDavid Howells /* Wait for the page to be written to the cache before we allow it to 9111cf7a151SDavid Howells * be modified. We then assume the entire page will need writing back. 9121cf7a151SDavid Howells */ 913630f5ddaSDavid Howells #ifdef CONFIG_AFS_FSCACHE 91478525c74SDavid Howells if (folio_test_fscache(folio) && 91578525c74SDavid Howells folio_wait_fscache_killable(folio) < 0) 9169620ad86SMatthew Wilcox (Oracle) goto out; 917630f5ddaSDavid Howells #endif 9189b3f26c9SDavid Howells 919490e016fSMatthew Wilcox (Oracle) if (folio_wait_writeback_killable(folio)) 9209620ad86SMatthew Wilcox (Oracle) goto out; 9211cf7a151SDavid Howells 92278525c74SDavid Howells if (folio_lock_killable(folio) < 0) 9239620ad86SMatthew Wilcox (Oracle) goto out; 9241cf7a151SDavid Howells 92578525c74SDavid Howells /* We mustn't change folio->private until writeback is complete as that 9261cf7a151SDavid Howells * details the portion of the page we need to write back and we might 9271cf7a151SDavid Howells * need to redirty the page if there's a problem. 9281cf7a151SDavid Howells */ 929490e016fSMatthew Wilcox (Oracle) if (folio_wait_writeback_killable(folio) < 0) { 930490e016fSMatthew Wilcox (Oracle) folio_unlock(folio); 9319620ad86SMatthew Wilcox (Oracle) goto out; 9325cbf0398SDavid Howells } 9331cf7a151SDavid Howells 93478525c74SDavid Howells priv = afs_folio_dirty(folio, 0, folio_size(folio)); 93578525c74SDavid Howells priv = afs_folio_dirty_mmapped(priv); 93678525c74SDavid Howells if (folio_test_private(folio)) { 93778525c74SDavid Howells folio_change_private(folio, (void *)priv); 93878525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("mkwrite+"), folio); 939e87b03f5SDavid Howells } else { 94078525c74SDavid Howells folio_attach_private(folio, (void *)priv); 94178525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("mkwrite"), folio); 942e87b03f5SDavid Howells } 943bb413489SDavid Howells file_update_time(file); 9441cf7a151SDavid Howells 9459620ad86SMatthew Wilcox (Oracle) ret = VM_FAULT_LOCKED; 9469620ad86SMatthew Wilcox (Oracle) out: 9471cf7a151SDavid Howells sb_end_pagefault(inode->i_sb); 9489620ad86SMatthew Wilcox (Oracle) return ret; 9499b3f26c9SDavid Howells } 9504343d008SDavid Howells 9514343d008SDavid Howells /* 9524343d008SDavid Howells * Prune the keys cached for writeback. The caller must hold vnode->wb_lock. 9534343d008SDavid Howells */ 9544343d008SDavid Howells void afs_prune_wb_keys(struct afs_vnode *vnode) 9554343d008SDavid Howells { 9564343d008SDavid Howells LIST_HEAD(graveyard); 9574343d008SDavid Howells struct afs_wb_key *wbk, *tmp; 9584343d008SDavid Howells 9594343d008SDavid Howells /* Discard unused keys */ 9604343d008SDavid Howells spin_lock(&vnode->wb_lock); 9614343d008SDavid Howells 9624343d008SDavid Howells if (!mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_WRITEBACK) && 9634343d008SDavid Howells !mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_DIRTY)) { 9644343d008SDavid Howells list_for_each_entry_safe(wbk, tmp, &vnode->wb_keys, vnode_link) { 9654343d008SDavid Howells if (refcount_read(&wbk->usage) == 1) 9664343d008SDavid Howells list_move(&wbk->vnode_link, &graveyard); 9674343d008SDavid Howells } 9684343d008SDavid Howells } 9694343d008SDavid Howells 9704343d008SDavid Howells spin_unlock(&vnode->wb_lock); 9714343d008SDavid Howells 9724343d008SDavid Howells while (!list_empty(&graveyard)) { 9734343d008SDavid Howells wbk = list_entry(graveyard.next, struct afs_wb_key, vnode_link); 9744343d008SDavid Howells list_del(&wbk->vnode_link); 9754343d008SDavid Howells afs_put_wb_key(wbk); 9764343d008SDavid Howells } 9774343d008SDavid Howells } 9784343d008SDavid Howells 9794343d008SDavid Howells /* 9804343d008SDavid Howells * Clean up a page during invalidation. 9814343d008SDavid Howells */ 98278525c74SDavid Howells int afs_launder_page(struct page *subpage) 9834343d008SDavid Howells { 98478525c74SDavid Howells struct folio *folio = page_folio(subpage); 98578525c74SDavid Howells struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio)); 986bd80d8a8SDavid Howells struct iov_iter iter; 987bd80d8a8SDavid Howells struct bio_vec bv[1]; 9884343d008SDavid Howells unsigned long priv; 9894343d008SDavid Howells unsigned int f, t; 9904343d008SDavid Howells int ret = 0; 9914343d008SDavid Howells 99278525c74SDavid Howells _enter("{%lx}", folio_index(folio)); 9934343d008SDavid Howells 99478525c74SDavid Howells priv = (unsigned long)folio_get_private(folio); 99578525c74SDavid Howells if (folio_clear_dirty_for_io(folio)) { 9964343d008SDavid Howells f = 0; 99778525c74SDavid Howells t = folio_size(folio); 99878525c74SDavid Howells if (folio_test_private(folio)) { 99978525c74SDavid Howells f = afs_folio_dirty_from(folio, priv); 100078525c74SDavid Howells t = afs_folio_dirty_to(folio, priv); 10014343d008SDavid Howells } 10024343d008SDavid Howells 100378525c74SDavid Howells bv[0].bv_page = &folio->page; 1004bd80d8a8SDavid Howells bv[0].bv_offset = f; 1005bd80d8a8SDavid Howells bv[0].bv_len = t - f; 1006bd80d8a8SDavid Howells iov_iter_bvec(&iter, WRITE, bv, 1, bv[0].bv_len); 1007bd80d8a8SDavid Howells 100878525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("launder"), folio); 100978525c74SDavid Howells ret = afs_store_data(vnode, &iter, folio_pos(folio) + f, true); 10104343d008SDavid Howells } 10114343d008SDavid Howells 101278525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("laundered"), folio); 101378525c74SDavid Howells folio_detach_private(folio); 101478525c74SDavid Howells folio_wait_fscache(folio); 10154343d008SDavid Howells return ret; 101631143d5dSDavid Howells } 1017c7f75ef3SDavid Howells 1018c7f75ef3SDavid Howells /* 1019c7f75ef3SDavid Howells * Deal with the completion of writing the data to the cache. 1020c7f75ef3SDavid Howells */ 1021c7f75ef3SDavid Howells static void afs_write_to_cache_done(void *priv, ssize_t transferred_or_error, 1022c7f75ef3SDavid Howells bool was_async) 1023c7f75ef3SDavid Howells { 1024c7f75ef3SDavid Howells struct afs_vnode *vnode = priv; 1025c7f75ef3SDavid Howells 1026c7f75ef3SDavid Howells if (IS_ERR_VALUE(transferred_or_error) && 1027c7f75ef3SDavid Howells transferred_or_error != -ENOBUFS) 1028c7f75ef3SDavid Howells afs_invalidate_cache(vnode, 0); 1029c7f75ef3SDavid Howells } 1030c7f75ef3SDavid Howells 1031c7f75ef3SDavid Howells /* 1032c7f75ef3SDavid Howells * Save the write to the cache also. 1033c7f75ef3SDavid Howells */ 1034c7f75ef3SDavid Howells static void afs_write_to_cache(struct afs_vnode *vnode, 1035c7f75ef3SDavid Howells loff_t start, size_t len, loff_t i_size, 1036c7f75ef3SDavid Howells bool caching) 1037c7f75ef3SDavid Howells { 1038c7f75ef3SDavid Howells fscache_write_to_cache(afs_vnode_cache(vnode), 1039c7f75ef3SDavid Howells vnode->vfs_inode.i_mapping, start, len, i_size, 1040c7f75ef3SDavid Howells afs_write_to_cache_done, vnode, caching); 1041c7f75ef3SDavid Howells } 1042