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 */ 62*bc899ee1SDavid Howells ret = netfs_write_begin(file, mapping, pos, len, flags, &folio, fsdata); 633003bbd0SDavid Howells if (ret < 0) 6431143d5dSDavid Howells return ret; 65630f5ddaSDavid Howells 6678525c74SDavid Howells index = folio_index(folio); 67e87b03f5SDavid Howells from = pos - index * PAGE_SIZE; 68e87b03f5SDavid Howells to = from + len; 69e87b03f5SDavid Howells 7031143d5dSDavid Howells try_again: 714343d008SDavid Howells /* See if this page is already partially written in a way that we can 724343d008SDavid Howells * merge the new write with. 734343d008SDavid Howells */ 7478525c74SDavid Howells if (folio_test_private(folio)) { 7578525c74SDavid Howells priv = (unsigned long)folio_get_private(folio); 7678525c74SDavid Howells f = afs_folio_dirty_from(folio, priv); 7778525c74SDavid Howells t = afs_folio_dirty_to(folio, priv); 784343d008SDavid Howells ASSERTCMP(f, <=, t); 7931143d5dSDavid Howells 8078525c74SDavid Howells if (folio_test_writeback(folio)) { 8178525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("alrdy"), folio); 825a039c32SDavid Howells goto flush_conflicting_write; 835a039c32SDavid Howells } 845a813276SDavid Howells /* If the file is being filled locally, allow inter-write 855a813276SDavid Howells * spaces to be merged into writes. If it's not, only write 865a813276SDavid Howells * back what the user gives us. 875a813276SDavid Howells */ 885a813276SDavid Howells if (!test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags) && 895a813276SDavid Howells (to < f || from > t)) 904343d008SDavid Howells goto flush_conflicting_write; 9131143d5dSDavid Howells } 9231143d5dSDavid Howells 9378525c74SDavid Howells *_page = &folio->page; 944343d008SDavid Howells _leave(" = 0"); 9531143d5dSDavid Howells return 0; 9631143d5dSDavid Howells 974343d008SDavid Howells /* The previous write and this write aren't adjacent or overlapping, so 984343d008SDavid Howells * flush the page out. 994343d008SDavid Howells */ 1004343d008SDavid Howells flush_conflicting_write: 10131143d5dSDavid Howells _debug("flush conflict"); 10278525c74SDavid Howells ret = folio_write_one(folio); 10321db2cdcSDavid Howells if (ret < 0) 10421db2cdcSDavid Howells goto error; 10531143d5dSDavid Howells 10678525c74SDavid Howells ret = folio_lock_killable(folio); 10721db2cdcSDavid Howells if (ret < 0) 10821db2cdcSDavid Howells goto error; 10921db2cdcSDavid Howells goto try_again; 11021db2cdcSDavid Howells 11121db2cdcSDavid Howells error: 11278525c74SDavid Howells folio_put(folio); 1134343d008SDavid Howells _leave(" = %d", ret); 1144343d008SDavid Howells return ret; 1154343d008SDavid Howells } 11631143d5dSDavid Howells 11731143d5dSDavid Howells /* 11831143d5dSDavid Howells * finalise part of a write to a page 11931143d5dSDavid Howells */ 12015b4650eSNick Piggin int afs_write_end(struct file *file, struct address_space *mapping, 12115b4650eSNick Piggin loff_t pos, unsigned len, unsigned copied, 12278525c74SDavid Howells struct page *subpage, void *fsdata) 12331143d5dSDavid Howells { 12478525c74SDavid Howells struct folio *folio = page_folio(subpage); 125496ad9aaSAl Viro struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 126f792e3acSDavid Howells unsigned long priv; 12778525c74SDavid Howells unsigned int f, from = offset_in_folio(folio, pos); 128f792e3acSDavid Howells unsigned int t, to = from + copied; 129c7f75ef3SDavid Howells loff_t i_size, write_end_pos; 13031143d5dSDavid Howells 1313b6492dfSDavid Howells _enter("{%llx:%llu},{%lx}", 13278525c74SDavid Howells vnode->fid.vid, vnode->fid.vnode, folio_index(folio)); 13331143d5dSDavid Howells 13478525c74SDavid Howells if (!folio_test_uptodate(folio)) { 13566e9c6a8SDavid Howells if (copied < len) { 13666e9c6a8SDavid Howells copied = 0; 13766e9c6a8SDavid Howells goto out; 13866e9c6a8SDavid Howells } 13966e9c6a8SDavid Howells 14078525c74SDavid Howells folio_mark_uptodate(folio); 14166e9c6a8SDavid Howells } 14266e9c6a8SDavid Howells 1433ad216eeSDavid Howells if (copied == 0) 1443ad216eeSDavid Howells goto out; 1453ad216eeSDavid Howells 146c7f75ef3SDavid Howells write_end_pos = pos + copied; 14731143d5dSDavid Howells 14831143d5dSDavid Howells i_size = i_size_read(&vnode->vfs_inode); 149c7f75ef3SDavid Howells if (write_end_pos > i_size) { 1501f32ef79SDavid Howells write_seqlock(&vnode->cb_lock); 15131143d5dSDavid Howells i_size = i_size_read(&vnode->vfs_inode); 152c7f75ef3SDavid Howells if (write_end_pos > i_size) 153c7f75ef3SDavid Howells afs_set_i_size(vnode, write_end_pos); 1541f32ef79SDavid Howells write_sequnlock(&vnode->cb_lock); 155c7f75ef3SDavid Howells fscache_update_cookie(afs_vnode_cache(vnode), NULL, &write_end_pos); 15631143d5dSDavid Howells } 15731143d5dSDavid Howells 15878525c74SDavid Howells if (folio_test_private(folio)) { 15978525c74SDavid Howells priv = (unsigned long)folio_get_private(folio); 16078525c74SDavid Howells f = afs_folio_dirty_from(folio, priv); 16178525c74SDavid Howells t = afs_folio_dirty_to(folio, priv); 162f792e3acSDavid Howells if (from < f) 163f792e3acSDavid Howells f = from; 164f792e3acSDavid Howells if (to > t) 165f792e3acSDavid Howells t = to; 16678525c74SDavid Howells priv = afs_folio_dirty(folio, f, t); 16778525c74SDavid Howells folio_change_private(folio, (void *)priv); 16878525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("dirty+"), folio); 169f792e3acSDavid Howells } else { 17078525c74SDavid Howells priv = afs_folio_dirty(folio, from, to); 17178525c74SDavid Howells folio_attach_private(folio, (void *)priv); 17278525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("dirty"), folio); 173f792e3acSDavid Howells } 174f792e3acSDavid Howells 17578525c74SDavid Howells if (folio_mark_dirty(folio)) 17678525c74SDavid Howells _debug("dirtied %lx", folio_index(folio)); 177afae457dSDavid Howells 178afae457dSDavid Howells out: 17978525c74SDavid Howells folio_unlock(folio); 18078525c74SDavid Howells folio_put(folio); 1813003bbd0SDavid Howells return copied; 18231143d5dSDavid Howells } 18331143d5dSDavid Howells 18431143d5dSDavid Howells /* 18531143d5dSDavid Howells * kill all the pages in the given range 18631143d5dSDavid Howells */ 1874343d008SDavid Howells static void afs_kill_pages(struct address_space *mapping, 188e87b03f5SDavid Howells loff_t start, loff_t len) 18931143d5dSDavid Howells { 1904343d008SDavid Howells struct afs_vnode *vnode = AFS_FS_I(mapping->host); 19178525c74SDavid Howells struct folio *folio; 19278525c74SDavid Howells pgoff_t index = start / PAGE_SIZE; 19378525c74SDavid Howells pgoff_t last = (start + len - 1) / PAGE_SIZE, next; 19431143d5dSDavid Howells 195e87b03f5SDavid Howells _enter("{%llx:%llu},%llx @%llx", 196e87b03f5SDavid Howells vnode->fid.vid, vnode->fid.vnode, len, start); 19731143d5dSDavid Howells 19831143d5dSDavid Howells do { 19978525c74SDavid Howells _debug("kill %lx (to %lx)", index, last); 20031143d5dSDavid Howells 20178525c74SDavid Howells folio = filemap_get_folio(mapping, index); 20278525c74SDavid Howells if (!folio) { 20378525c74SDavid Howells next = index + 1; 20478525c74SDavid Howells continue; 2054343d008SDavid Howells } 2064343d008SDavid Howells 20778525c74SDavid Howells next = folio_next_index(folio); 20878525c74SDavid Howells 20978525c74SDavid Howells folio_clear_uptodate(folio); 21078525c74SDavid Howells folio_end_writeback(folio); 21178525c74SDavid Howells folio_lock(folio); 21278525c74SDavid Howells generic_error_remove_page(mapping, &folio->page); 21378525c74SDavid Howells folio_unlock(folio); 21478525c74SDavid Howells folio_put(folio); 21578525c74SDavid Howells 21678525c74SDavid Howells } while (index = next, index <= last); 2174343d008SDavid Howells 2184343d008SDavid Howells _leave(""); 2194343d008SDavid Howells } 2204343d008SDavid Howells 2214343d008SDavid Howells /* 2224343d008SDavid Howells * Redirty all the pages in a given range. 2234343d008SDavid Howells */ 2244343d008SDavid Howells static void afs_redirty_pages(struct writeback_control *wbc, 2254343d008SDavid Howells struct address_space *mapping, 226e87b03f5SDavid Howells loff_t start, loff_t len) 2274343d008SDavid Howells { 2284343d008SDavid Howells struct afs_vnode *vnode = AFS_FS_I(mapping->host); 22978525c74SDavid Howells struct folio *folio; 23078525c74SDavid Howells pgoff_t index = start / PAGE_SIZE; 23178525c74SDavid Howells pgoff_t last = (start + len - 1) / PAGE_SIZE, next; 2324343d008SDavid Howells 233e87b03f5SDavid Howells _enter("{%llx:%llu},%llx @%llx", 234e87b03f5SDavid Howells vnode->fid.vid, vnode->fid.vnode, len, start); 2354343d008SDavid Howells 2364343d008SDavid Howells do { 237e87b03f5SDavid Howells _debug("redirty %llx @%llx", len, start); 2384343d008SDavid Howells 23978525c74SDavid Howells folio = filemap_get_folio(mapping, index); 24078525c74SDavid Howells if (!folio) { 24178525c74SDavid Howells next = index + 1; 24278525c74SDavid Howells continue; 24331143d5dSDavid Howells } 24431143d5dSDavid Howells 24578525c74SDavid Howells next = index + folio_nr_pages(folio); 24678525c74SDavid Howells folio_redirty_for_writepage(wbc, folio); 24778525c74SDavid Howells folio_end_writeback(folio); 24878525c74SDavid Howells folio_put(folio); 24978525c74SDavid Howells } while (index = next, index <= last); 25031143d5dSDavid Howells 25131143d5dSDavid Howells _leave(""); 25231143d5dSDavid Howells } 25331143d5dSDavid Howells 25431143d5dSDavid Howells /* 255a58823acSDavid Howells * completion of write to server 256a58823acSDavid Howells */ 257e87b03f5SDavid Howells static void afs_pages_written_back(struct afs_vnode *vnode, loff_t start, unsigned int len) 258a58823acSDavid Howells { 259bd80d8a8SDavid Howells struct address_space *mapping = vnode->vfs_inode.i_mapping; 26078525c74SDavid Howells struct folio *folio; 261e87b03f5SDavid Howells pgoff_t end; 262bd80d8a8SDavid Howells 263e87b03f5SDavid Howells XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE); 264a58823acSDavid Howells 265e87b03f5SDavid Howells _enter("{%llx:%llu},{%x @%llx}", 266e87b03f5SDavid Howells vnode->fid.vid, vnode->fid.vnode, len, start); 267a58823acSDavid Howells 268bd80d8a8SDavid Howells rcu_read_lock(); 269a58823acSDavid Howells 270e87b03f5SDavid Howells end = (start + len - 1) / PAGE_SIZE; 27178525c74SDavid Howells xas_for_each(&xas, folio, end) { 27278525c74SDavid Howells if (!folio_test_writeback(folio)) { 27378525c74SDavid Howells kdebug("bad %x @%llx page %lx %lx", 27478525c74SDavid Howells len, start, folio_index(folio), end); 27578525c74SDavid Howells ASSERT(folio_test_writeback(folio)); 276e87b03f5SDavid Howells } 277a58823acSDavid Howells 27878525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("clear"), folio); 27978525c74SDavid Howells folio_detach_private(folio); 28078525c74SDavid Howells folio_end_writeback(folio); 281a58823acSDavid Howells } 282bd80d8a8SDavid Howells 283bd80d8a8SDavid Howells rcu_read_unlock(); 284a58823acSDavid Howells 285a58823acSDavid Howells afs_prune_wb_keys(vnode); 286a58823acSDavid Howells _leave(""); 287a58823acSDavid Howells } 288a58823acSDavid Howells 289a58823acSDavid Howells /* 290e49c7b2fSDavid Howells * Find a key to use for the writeback. We cached the keys used to author the 291e49c7b2fSDavid Howells * writes on the vnode. *_wbk will contain the last writeback key used or NULL 292e49c7b2fSDavid Howells * and we need to start from there if it's set. 293e49c7b2fSDavid Howells */ 294e49c7b2fSDavid Howells static int afs_get_writeback_key(struct afs_vnode *vnode, 295e49c7b2fSDavid Howells struct afs_wb_key **_wbk) 296e49c7b2fSDavid Howells { 297e49c7b2fSDavid Howells struct afs_wb_key *wbk = NULL; 298e49c7b2fSDavid Howells struct list_head *p; 299e49c7b2fSDavid Howells int ret = -ENOKEY, ret2; 300e49c7b2fSDavid Howells 301e49c7b2fSDavid Howells spin_lock(&vnode->wb_lock); 302e49c7b2fSDavid Howells if (*_wbk) 303e49c7b2fSDavid Howells p = (*_wbk)->vnode_link.next; 304e49c7b2fSDavid Howells else 305e49c7b2fSDavid Howells p = vnode->wb_keys.next; 306e49c7b2fSDavid Howells 307e49c7b2fSDavid Howells while (p != &vnode->wb_keys) { 308e49c7b2fSDavid Howells wbk = list_entry(p, struct afs_wb_key, vnode_link); 309e49c7b2fSDavid Howells _debug("wbk %u", key_serial(wbk->key)); 310e49c7b2fSDavid Howells ret2 = key_validate(wbk->key); 311e49c7b2fSDavid Howells if (ret2 == 0) { 312e49c7b2fSDavid Howells refcount_inc(&wbk->usage); 313e49c7b2fSDavid Howells _debug("USE WB KEY %u", key_serial(wbk->key)); 314e49c7b2fSDavid Howells break; 315e49c7b2fSDavid Howells } 316e49c7b2fSDavid Howells 317e49c7b2fSDavid Howells wbk = NULL; 318e49c7b2fSDavid Howells if (ret == -ENOKEY) 319e49c7b2fSDavid Howells ret = ret2; 320e49c7b2fSDavid Howells p = p->next; 321e49c7b2fSDavid Howells } 322e49c7b2fSDavid Howells 323e49c7b2fSDavid Howells spin_unlock(&vnode->wb_lock); 324e49c7b2fSDavid Howells if (*_wbk) 325e49c7b2fSDavid Howells afs_put_wb_key(*_wbk); 326e49c7b2fSDavid Howells *_wbk = wbk; 327e49c7b2fSDavid Howells return 0; 328e49c7b2fSDavid Howells } 329e49c7b2fSDavid Howells 330e49c7b2fSDavid Howells static void afs_store_data_success(struct afs_operation *op) 331e49c7b2fSDavid Howells { 332e49c7b2fSDavid Howells struct afs_vnode *vnode = op->file[0].vnode; 333e49c7b2fSDavid Howells 334da8d0755SDavid Howells op->ctime = op->file[0].scb.status.mtime_client; 335e49c7b2fSDavid Howells afs_vnode_commit_status(op, &op->file[0]); 336e49c7b2fSDavid Howells if (op->error == 0) { 337d383e346SDavid Howells if (!op->store.laundering) 338e87b03f5SDavid Howells afs_pages_written_back(vnode, op->store.pos, op->store.size); 339e49c7b2fSDavid Howells afs_stat_v(vnode, n_stores); 340bd80d8a8SDavid Howells atomic_long_add(op->store.size, &afs_v2net(vnode)->n_store_bytes); 341e49c7b2fSDavid Howells } 342e49c7b2fSDavid Howells } 343e49c7b2fSDavid Howells 344e49c7b2fSDavid Howells static const struct afs_operation_ops afs_store_data_operation = { 345e49c7b2fSDavid Howells .issue_afs_rpc = afs_fs_store_data, 346e49c7b2fSDavid Howells .issue_yfs_rpc = yfs_fs_store_data, 347e49c7b2fSDavid Howells .success = afs_store_data_success, 348e49c7b2fSDavid Howells }; 349e49c7b2fSDavid Howells 350e49c7b2fSDavid Howells /* 351d2ddc776SDavid Howells * write to a file 35231143d5dSDavid Howells */ 353e87b03f5SDavid Howells static int afs_store_data(struct afs_vnode *vnode, struct iov_iter *iter, loff_t pos, 354bd80d8a8SDavid Howells bool laundering) 35531143d5dSDavid Howells { 356e49c7b2fSDavid Howells struct afs_operation *op; 3574343d008SDavid Howells struct afs_wb_key *wbk = NULL; 358bd80d8a8SDavid Howells loff_t size = iov_iter_count(iter), i_size; 359bd80d8a8SDavid Howells int ret = -ENOKEY; 360d2ddc776SDavid Howells 361bd80d8a8SDavid Howells _enter("%s{%llx:%llu.%u},%llx,%llx", 362d2ddc776SDavid Howells vnode->volume->name, 363d2ddc776SDavid Howells vnode->fid.vid, 364d2ddc776SDavid Howells vnode->fid.vnode, 365d2ddc776SDavid Howells vnode->fid.unique, 366bd80d8a8SDavid Howells size, pos); 367d2ddc776SDavid Howells 368e49c7b2fSDavid Howells ret = afs_get_writeback_key(vnode, &wbk); 369e49c7b2fSDavid Howells if (ret) { 3704343d008SDavid Howells _leave(" = %d [no keys]", ret); 3714343d008SDavid Howells return ret; 372d2ddc776SDavid Howells } 373d2ddc776SDavid Howells 374e49c7b2fSDavid Howells op = afs_alloc_operation(wbk->key, vnode->volume); 375e49c7b2fSDavid Howells if (IS_ERR(op)) { 376e49c7b2fSDavid Howells afs_put_wb_key(wbk); 377e49c7b2fSDavid Howells return -ENOMEM; 378d2ddc776SDavid Howells } 379d2ddc776SDavid Howells 380bd80d8a8SDavid Howells i_size = i_size_read(&vnode->vfs_inode); 381bd80d8a8SDavid 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; 388bd80d8a8SDavid Howells op->store.i_size = max(pos + size, 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. */ 619c7f75ef3SDavid Howells fscache_clear_page_bits(afs_vnode_cache(vnode), 620c7f75ef3SDavid Howells mapping, start, len, caching); 621e87b03f5SDavid Howells afs_pages_written_back(vnode, start, len); 622bd80d8a8SDavid Howells ret = 0; 623bd80d8a8SDavid Howells } 624bd80d8a8SDavid Howells 62531143d5dSDavid Howells switch (ret) { 6264343d008SDavid Howells case 0: 627e87b03f5SDavid Howells wbc->nr_to_write = count; 628e87b03f5SDavid Howells ret = len; 6294343d008SDavid Howells break; 6304343d008SDavid Howells 6314343d008SDavid Howells default: 6324343d008SDavid Howells pr_notice("kAFS: Unexpected error from FS.StoreData %d\n", ret); 633df561f66SGustavo A. R. Silva fallthrough; 6344343d008SDavid Howells case -EACCES: 6354343d008SDavid Howells case -EPERM: 6364343d008SDavid Howells case -ENOKEY: 6374343d008SDavid Howells case -EKEYEXPIRED: 6384343d008SDavid Howells case -EKEYREJECTED: 6394343d008SDavid Howells case -EKEYREVOKED: 640e87b03f5SDavid Howells afs_redirty_pages(wbc, mapping, start, len); 6414343d008SDavid Howells mapping_set_error(mapping, ret); 6424343d008SDavid Howells break; 6434343d008SDavid Howells 64431143d5dSDavid Howells case -EDQUOT: 64531143d5dSDavid Howells case -ENOSPC: 646e87b03f5SDavid Howells afs_redirty_pages(wbc, mapping, start, len); 6474343d008SDavid Howells mapping_set_error(mapping, -ENOSPC); 64831143d5dSDavid Howells break; 6494343d008SDavid Howells 65031143d5dSDavid Howells case -EROFS: 65131143d5dSDavid Howells case -EIO: 65231143d5dSDavid Howells case -EREMOTEIO: 65331143d5dSDavid Howells case -EFBIG: 65431143d5dSDavid Howells case -ENOENT: 65531143d5dSDavid Howells case -ENOMEDIUM: 65631143d5dSDavid Howells case -ENXIO: 657f51375cdSDavid Howells trace_afs_file_error(vnode, ret, afs_file_error_writeback_fail); 658e87b03f5SDavid Howells afs_kill_pages(mapping, start, len); 6594343d008SDavid Howells mapping_set_error(mapping, ret); 66031143d5dSDavid Howells break; 66131143d5dSDavid Howells } 66231143d5dSDavid Howells 66331143d5dSDavid Howells _leave(" = %d", ret); 66431143d5dSDavid Howells return ret; 66531143d5dSDavid Howells } 66631143d5dSDavid Howells 66731143d5dSDavid Howells /* 66831143d5dSDavid Howells * write a page back to the server 66931143d5dSDavid Howells * - the caller locked the page for us 67031143d5dSDavid Howells */ 67178525c74SDavid Howells int afs_writepage(struct page *subpage, struct writeback_control *wbc) 67231143d5dSDavid Howells { 67378525c74SDavid Howells struct folio *folio = page_folio(subpage); 674e87b03f5SDavid Howells ssize_t ret; 675e87b03f5SDavid Howells loff_t start; 67631143d5dSDavid Howells 67778525c74SDavid Howells _enter("{%lx},", folio_index(folio)); 67831143d5dSDavid Howells 679c7f75ef3SDavid Howells #ifdef CONFIG_AFS_FSCACHE 680c7f75ef3SDavid Howells folio_wait_fscache(folio); 681c7f75ef3SDavid Howells #endif 682c7f75ef3SDavid Howells 68378525c74SDavid Howells start = folio_index(folio) * PAGE_SIZE; 68478525c74SDavid Howells ret = afs_write_back_from_locked_folio(folio_mapping(folio), wbc, 68578525c74SDavid Howells folio, start, LLONG_MAX - start); 68631143d5dSDavid Howells if (ret < 0) { 687e87b03f5SDavid Howells _leave(" = %zd", ret); 688e87b03f5SDavid Howells return ret; 68931143d5dSDavid Howells } 69031143d5dSDavid Howells 69131143d5dSDavid Howells _leave(" = 0"); 69231143d5dSDavid Howells return 0; 69331143d5dSDavid Howells } 69431143d5dSDavid Howells 69531143d5dSDavid Howells /* 69631143d5dSDavid Howells * write a region of pages back to the server 69731143d5dSDavid Howells */ 698c1206a2cSAdrian Bunk static int afs_writepages_region(struct address_space *mapping, 69931143d5dSDavid Howells struct writeback_control *wbc, 700e87b03f5SDavid Howells loff_t start, loff_t end, loff_t *_next) 70131143d5dSDavid Howells { 70278525c74SDavid Howells struct folio *folio; 70378525c74SDavid Howells struct page *head_page; 704e87b03f5SDavid Howells ssize_t ret; 705e87b03f5SDavid Howells int n; 70631143d5dSDavid Howells 707e87b03f5SDavid Howells _enter("%llx,%llx,", start, end); 70831143d5dSDavid Howells 70931143d5dSDavid Howells do { 710e87b03f5SDavid Howells pgoff_t index = start / PAGE_SIZE; 711e87b03f5SDavid Howells 712e87b03f5SDavid Howells n = find_get_pages_range_tag(mapping, &index, end / PAGE_SIZE, 71378525c74SDavid Howells PAGECACHE_TAG_DIRTY, 1, &head_page); 71431143d5dSDavid Howells if (!n) 71531143d5dSDavid Howells break; 71631143d5dSDavid Howells 71778525c74SDavid Howells folio = page_folio(head_page); 71878525c74SDavid Howells start = folio_pos(folio); /* May regress with THPs */ 719e87b03f5SDavid Howells 72078525c74SDavid Howells _debug("wback %lx", folio_index(folio)); 72131143d5dSDavid Howells 722e87b03f5SDavid Howells /* At this point we hold neither the i_pages lock nor the 723b93b0163SMatthew Wilcox * page lock: the page may be truncated or invalidated 724b93b0163SMatthew Wilcox * (changing page->mapping to NULL), or even swizzled 725b93b0163SMatthew Wilcox * back from swapper_space to tmpfs file mapping 72631143d5dSDavid Howells */ 727e87b03f5SDavid Howells if (wbc->sync_mode != WB_SYNC_NONE) { 72878525c74SDavid Howells ret = folio_lock_killable(folio); 7294343d008SDavid Howells if (ret < 0) { 73078525c74SDavid Howells folio_put(folio); 7314343d008SDavid Howells return ret; 7324343d008SDavid Howells } 733e87b03f5SDavid Howells } else { 73478525c74SDavid Howells if (!folio_trylock(folio)) { 73578525c74SDavid Howells folio_put(folio); 736e87b03f5SDavid Howells return 0; 737e87b03f5SDavid Howells } 738e87b03f5SDavid Howells } 73931143d5dSDavid Howells 74078525c74SDavid Howells if (folio_mapping(folio) != mapping || 74178525c74SDavid Howells !folio_test_dirty(folio)) { 74278525c74SDavid Howells start += folio_size(folio); 74378525c74SDavid Howells folio_unlock(folio); 74478525c74SDavid Howells folio_put(folio); 74531143d5dSDavid Howells continue; 74631143d5dSDavid Howells } 74731143d5dSDavid Howells 748c7f75ef3SDavid Howells if (folio_test_writeback(folio) || 749c7f75ef3SDavid Howells folio_test_fscache(folio)) { 75078525c74SDavid Howells folio_unlock(folio); 751c7f75ef3SDavid Howells if (wbc->sync_mode != WB_SYNC_NONE) { 75278525c74SDavid Howells folio_wait_writeback(folio); 753c7f75ef3SDavid Howells #ifdef CONFIG_AFS_FSCACHE 754c7f75ef3SDavid Howells folio_wait_fscache(folio); 755c7f75ef3SDavid Howells #endif 756c7f75ef3SDavid Howells } 75778525c74SDavid Howells folio_put(folio); 75831143d5dSDavid Howells continue; 75931143d5dSDavid Howells } 76031143d5dSDavid Howells 76178525c74SDavid Howells if (!folio_clear_dirty_for_io(folio)) 76265a15109SDavid Howells BUG(); 76378525c74SDavid Howells ret = afs_write_back_from_locked_folio(mapping, wbc, folio, start, end); 76478525c74SDavid Howells folio_put(folio); 76531143d5dSDavid Howells if (ret < 0) { 766e87b03f5SDavid Howells _leave(" = %zd", ret); 76731143d5dSDavid Howells return ret; 76831143d5dSDavid Howells } 76931143d5dSDavid Howells 770dc255730SMarc Dionne start += ret; 77131143d5dSDavid Howells 77231143d5dSDavid Howells cond_resched(); 773e87b03f5SDavid Howells } while (wbc->nr_to_write > 0); 77431143d5dSDavid Howells 775e87b03f5SDavid Howells *_next = start; 776e87b03f5SDavid Howells _leave(" = 0 [%llx]", *_next); 77731143d5dSDavid Howells return 0; 77831143d5dSDavid Howells } 77931143d5dSDavid Howells 78031143d5dSDavid Howells /* 78131143d5dSDavid Howells * write some of the pending data back to the server 78231143d5dSDavid Howells */ 78331143d5dSDavid Howells int afs_writepages(struct address_space *mapping, 78431143d5dSDavid Howells struct writeback_control *wbc) 78531143d5dSDavid Howells { 786ec0fa0b6SDavid Howells struct afs_vnode *vnode = AFS_FS_I(mapping->host); 787e87b03f5SDavid Howells loff_t start, next; 78831143d5dSDavid Howells int ret; 78931143d5dSDavid Howells 79031143d5dSDavid Howells _enter(""); 79131143d5dSDavid Howells 792ec0fa0b6SDavid Howells /* We have to be careful as we can end up racing with setattr() 793ec0fa0b6SDavid Howells * truncating the pagecache since the caller doesn't take a lock here 794ec0fa0b6SDavid Howells * to prevent it. 795ec0fa0b6SDavid Howells */ 796ec0fa0b6SDavid Howells if (wbc->sync_mode == WB_SYNC_ALL) 797ec0fa0b6SDavid Howells down_read(&vnode->validate_lock); 798ec0fa0b6SDavid Howells else if (!down_read_trylock(&vnode->validate_lock)) 799ec0fa0b6SDavid Howells return 0; 800ec0fa0b6SDavid Howells 80131143d5dSDavid Howells if (wbc->range_cyclic) { 802e87b03f5SDavid Howells start = mapping->writeback_index * PAGE_SIZE; 803e87b03f5SDavid Howells ret = afs_writepages_region(mapping, wbc, start, LLONG_MAX, &next); 804afe69498STom Rix if (ret == 0) { 805e87b03f5SDavid Howells mapping->writeback_index = next / PAGE_SIZE; 806afe69498STom Rix if (start > 0 && wbc->nr_to_write > 0) { 807afe69498STom Rix ret = afs_writepages_region(mapping, wbc, 0, 808afe69498STom Rix start, &next); 809afe69498STom Rix if (ret == 0) 810afe69498STom Rix mapping->writeback_index = 811afe69498STom Rix next / PAGE_SIZE; 812afe69498STom Rix } 813afe69498STom Rix } 81431143d5dSDavid Howells } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) { 815e87b03f5SDavid Howells ret = afs_writepages_region(mapping, wbc, 0, LLONG_MAX, &next); 816afe69498STom Rix if (wbc->nr_to_write > 0 && ret == 0) 8175a972474SDavid Howells mapping->writeback_index = next / PAGE_SIZE; 81831143d5dSDavid Howells } else { 819e87b03f5SDavid Howells ret = afs_writepages_region(mapping, wbc, 820e87b03f5SDavid Howells wbc->range_start, wbc->range_end, &next); 82131143d5dSDavid Howells } 82231143d5dSDavid Howells 823ec0fa0b6SDavid Howells up_read(&vnode->validate_lock); 82431143d5dSDavid Howells _leave(" = %d", ret); 82531143d5dSDavid Howells return ret; 82631143d5dSDavid Howells } 82731143d5dSDavid Howells 82831143d5dSDavid Howells /* 82931143d5dSDavid Howells * write to an AFS file 83031143d5dSDavid Howells */ 83150b5551dSAl Viro ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from) 83231143d5dSDavid Howells { 833496ad9aaSAl Viro struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp)); 8343978d816SDavid Howells struct afs_file *af = iocb->ki_filp->private_data; 83531143d5dSDavid Howells ssize_t result; 83650b5551dSAl Viro size_t count = iov_iter_count(from); 83731143d5dSDavid Howells 8383b6492dfSDavid Howells _enter("{%llx:%llu},{%zu},", 83950b5551dSAl Viro vnode->fid.vid, vnode->fid.vnode, count); 84031143d5dSDavid Howells 84131143d5dSDavid Howells if (IS_SWAPFILE(&vnode->vfs_inode)) { 84231143d5dSDavid Howells printk(KERN_INFO 84331143d5dSDavid Howells "AFS: Attempt to write to active swap file!\n"); 84431143d5dSDavid Howells return -EBUSY; 84531143d5dSDavid Howells } 84631143d5dSDavid Howells 84731143d5dSDavid Howells if (!count) 84831143d5dSDavid Howells return 0; 84931143d5dSDavid Howells 8503978d816SDavid Howells result = afs_validate(vnode, af->key); 8513978d816SDavid Howells if (result < 0) 8523978d816SDavid Howells return result; 8533978d816SDavid Howells 85450b5551dSAl Viro result = generic_file_write_iter(iocb, from); 85531143d5dSDavid Howells 85631143d5dSDavid Howells _leave(" = %zd", result); 85731143d5dSDavid Howells return result; 85831143d5dSDavid Howells } 85931143d5dSDavid Howells 86031143d5dSDavid Howells /* 86131143d5dSDavid Howells * flush any dirty pages for this process, and check for write errors. 86231143d5dSDavid Howells * - the return status from this call provides a reliable indication of 86331143d5dSDavid Howells * whether any write errors occurred for this process. 86431143d5dSDavid Howells */ 86502c24a82SJosef Bacik int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync) 86631143d5dSDavid Howells { 8673978d816SDavid Howells struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 8683978d816SDavid Howells struct afs_file *af = file->private_data; 8693978d816SDavid Howells int ret; 87031143d5dSDavid Howells 8713b6492dfSDavid Howells _enter("{%llx:%llu},{n=%pD},%d", 8723c981bfcSAl Viro vnode->fid.vid, vnode->fid.vnode, file, 87331143d5dSDavid Howells datasync); 87431143d5dSDavid Howells 8753978d816SDavid Howells ret = afs_validate(vnode, af->key); 8763978d816SDavid Howells if (ret < 0) 8773978d816SDavid Howells return ret; 8783978d816SDavid Howells 8794343d008SDavid Howells return file_write_and_wait_range(file, start, end); 88031143d5dSDavid Howells } 8819b3f26c9SDavid Howells 8829b3f26c9SDavid Howells /* 8839b3f26c9SDavid Howells * notification that a previously read-only page is about to become writable 8849b3f26c9SDavid Howells * - if it returns an error, the caller will deliver a bus error signal 8859b3f26c9SDavid Howells */ 8860722f186SSouptick Joarder vm_fault_t afs_page_mkwrite(struct vm_fault *vmf) 8879b3f26c9SDavid Howells { 888490e016fSMatthew Wilcox (Oracle) struct folio *folio = page_folio(vmf->page); 8891cf7a151SDavid Howells struct file *file = vmf->vma->vm_file; 8901cf7a151SDavid Howells struct inode *inode = file_inode(file); 8911cf7a151SDavid Howells struct afs_vnode *vnode = AFS_FS_I(inode); 8923978d816SDavid Howells struct afs_file *af = file->private_data; 8931cf7a151SDavid Howells unsigned long priv; 8949620ad86SMatthew Wilcox (Oracle) vm_fault_t ret = VM_FAULT_RETRY; 8959b3f26c9SDavid Howells 89678525c74SDavid Howells _enter("{{%llx:%llu}},{%lx}", vnode->fid.vid, vnode->fid.vnode, folio_index(folio)); 8979b3f26c9SDavid Howells 8983978d816SDavid Howells afs_validate(vnode, af->key); 8993978d816SDavid Howells 9001cf7a151SDavid Howells sb_start_pagefault(inode->i_sb); 9011cf7a151SDavid Howells 9021cf7a151SDavid Howells /* Wait for the page to be written to the cache before we allow it to 9031cf7a151SDavid Howells * be modified. We then assume the entire page will need writing back. 9041cf7a151SDavid Howells */ 905630f5ddaSDavid Howells #ifdef CONFIG_AFS_FSCACHE 90678525c74SDavid Howells if (folio_test_fscache(folio) && 90778525c74SDavid Howells folio_wait_fscache_killable(folio) < 0) 9089620ad86SMatthew Wilcox (Oracle) goto out; 909630f5ddaSDavid Howells #endif 9109b3f26c9SDavid Howells 911490e016fSMatthew Wilcox (Oracle) if (folio_wait_writeback_killable(folio)) 9129620ad86SMatthew Wilcox (Oracle) goto out; 9131cf7a151SDavid Howells 91478525c74SDavid Howells if (folio_lock_killable(folio) < 0) 9159620ad86SMatthew Wilcox (Oracle) goto out; 9161cf7a151SDavid Howells 91778525c74SDavid Howells /* We mustn't change folio->private until writeback is complete as that 9181cf7a151SDavid Howells * details the portion of the page we need to write back and we might 9191cf7a151SDavid Howells * need to redirty the page if there's a problem. 9201cf7a151SDavid Howells */ 921490e016fSMatthew Wilcox (Oracle) if (folio_wait_writeback_killable(folio) < 0) { 922490e016fSMatthew Wilcox (Oracle) folio_unlock(folio); 9239620ad86SMatthew Wilcox (Oracle) goto out; 9245cbf0398SDavid Howells } 9251cf7a151SDavid Howells 92678525c74SDavid Howells priv = afs_folio_dirty(folio, 0, folio_size(folio)); 92778525c74SDavid Howells priv = afs_folio_dirty_mmapped(priv); 92878525c74SDavid Howells if (folio_test_private(folio)) { 92978525c74SDavid Howells folio_change_private(folio, (void *)priv); 93078525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("mkwrite+"), folio); 931e87b03f5SDavid Howells } else { 93278525c74SDavid Howells folio_attach_private(folio, (void *)priv); 93378525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("mkwrite"), folio); 934e87b03f5SDavid Howells } 935bb413489SDavid Howells file_update_time(file); 9361cf7a151SDavid Howells 9379620ad86SMatthew Wilcox (Oracle) ret = VM_FAULT_LOCKED; 9389620ad86SMatthew Wilcox (Oracle) out: 9391cf7a151SDavid Howells sb_end_pagefault(inode->i_sb); 9409620ad86SMatthew Wilcox (Oracle) return ret; 9419b3f26c9SDavid Howells } 9424343d008SDavid Howells 9434343d008SDavid Howells /* 9444343d008SDavid Howells * Prune the keys cached for writeback. The caller must hold vnode->wb_lock. 9454343d008SDavid Howells */ 9464343d008SDavid Howells void afs_prune_wb_keys(struct afs_vnode *vnode) 9474343d008SDavid Howells { 9484343d008SDavid Howells LIST_HEAD(graveyard); 9494343d008SDavid Howells struct afs_wb_key *wbk, *tmp; 9504343d008SDavid Howells 9514343d008SDavid Howells /* Discard unused keys */ 9524343d008SDavid Howells spin_lock(&vnode->wb_lock); 9534343d008SDavid Howells 9544343d008SDavid Howells if (!mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_WRITEBACK) && 9554343d008SDavid Howells !mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_DIRTY)) { 9564343d008SDavid Howells list_for_each_entry_safe(wbk, tmp, &vnode->wb_keys, vnode_link) { 9574343d008SDavid Howells if (refcount_read(&wbk->usage) == 1) 9584343d008SDavid Howells list_move(&wbk->vnode_link, &graveyard); 9594343d008SDavid Howells } 9604343d008SDavid Howells } 9614343d008SDavid Howells 9624343d008SDavid Howells spin_unlock(&vnode->wb_lock); 9634343d008SDavid Howells 9644343d008SDavid Howells while (!list_empty(&graveyard)) { 9654343d008SDavid Howells wbk = list_entry(graveyard.next, struct afs_wb_key, vnode_link); 9664343d008SDavid Howells list_del(&wbk->vnode_link); 9674343d008SDavid Howells afs_put_wb_key(wbk); 9684343d008SDavid Howells } 9694343d008SDavid Howells } 9704343d008SDavid Howells 9714343d008SDavid Howells /* 9724343d008SDavid Howells * Clean up a page during invalidation. 9734343d008SDavid Howells */ 97478525c74SDavid Howells int afs_launder_page(struct page *subpage) 9754343d008SDavid Howells { 97678525c74SDavid Howells struct folio *folio = page_folio(subpage); 97778525c74SDavid Howells struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio)); 978bd80d8a8SDavid Howells struct iov_iter iter; 979bd80d8a8SDavid Howells struct bio_vec bv[1]; 9804343d008SDavid Howells unsigned long priv; 9814343d008SDavid Howells unsigned int f, t; 9824343d008SDavid Howells int ret = 0; 9834343d008SDavid Howells 98478525c74SDavid Howells _enter("{%lx}", folio_index(folio)); 9854343d008SDavid Howells 98678525c74SDavid Howells priv = (unsigned long)folio_get_private(folio); 98778525c74SDavid Howells if (folio_clear_dirty_for_io(folio)) { 9884343d008SDavid Howells f = 0; 98978525c74SDavid Howells t = folio_size(folio); 99078525c74SDavid Howells if (folio_test_private(folio)) { 99178525c74SDavid Howells f = afs_folio_dirty_from(folio, priv); 99278525c74SDavid Howells t = afs_folio_dirty_to(folio, priv); 9934343d008SDavid Howells } 9944343d008SDavid Howells 99578525c74SDavid Howells bv[0].bv_page = &folio->page; 996bd80d8a8SDavid Howells bv[0].bv_offset = f; 997bd80d8a8SDavid Howells bv[0].bv_len = t - f; 998bd80d8a8SDavid Howells iov_iter_bvec(&iter, WRITE, bv, 1, bv[0].bv_len); 999bd80d8a8SDavid Howells 100078525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("launder"), folio); 100178525c74SDavid Howells ret = afs_store_data(vnode, &iter, folio_pos(folio) + f, true); 10024343d008SDavid Howells } 10034343d008SDavid Howells 100478525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("laundered"), folio); 100578525c74SDavid Howells folio_detach_private(folio); 100678525c74SDavid Howells folio_wait_fscache(folio); 10074343d008SDavid Howells return ret; 100831143d5dSDavid Howells } 1009c7f75ef3SDavid Howells 1010c7f75ef3SDavid Howells /* 1011c7f75ef3SDavid Howells * Deal with the completion of writing the data to the cache. 1012c7f75ef3SDavid Howells */ 1013c7f75ef3SDavid Howells static void afs_write_to_cache_done(void *priv, ssize_t transferred_or_error, 1014c7f75ef3SDavid Howells bool was_async) 1015c7f75ef3SDavid Howells { 1016c7f75ef3SDavid Howells struct afs_vnode *vnode = priv; 1017c7f75ef3SDavid Howells 1018c7f75ef3SDavid Howells if (IS_ERR_VALUE(transferred_or_error) && 1019c7f75ef3SDavid Howells transferred_or_error != -ENOBUFS) 1020c7f75ef3SDavid Howells afs_invalidate_cache(vnode, 0); 1021c7f75ef3SDavid Howells } 1022c7f75ef3SDavid Howells 1023c7f75ef3SDavid Howells /* 1024c7f75ef3SDavid Howells * Save the write to the cache also. 1025c7f75ef3SDavid Howells */ 1026c7f75ef3SDavid Howells static void afs_write_to_cache(struct afs_vnode *vnode, 1027c7f75ef3SDavid Howells loff_t start, size_t len, loff_t i_size, 1028c7f75ef3SDavid Howells bool caching) 1029c7f75ef3SDavid Howells { 1030c7f75ef3SDavid Howells fscache_write_to_cache(afs_vnode_cache(vnode), 1031c7f75ef3SDavid Howells vnode->vfs_inode.i_mapping, start, len, i_size, 1032c7f75ef3SDavid Howells afs_write_to_cache_done, vnode, caching); 1033c7f75ef3SDavid Howells } 1034