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 */ 25*8fb72b4aSMatthew Wilcox (Oracle) bool afs_dirty_folio(struct address_space *mapping, struct folio *folio) 2631143d5dSDavid Howells { 27*8fb72b4aSMatthew Wilcox (Oracle) return fscache_dirty_folio(mapping, folio, 28*8fb72b4aSMatthew Wilcox (Oracle) afs_vnode_cache(AFS_FS_I(mapping->host))); 2931143d5dSDavid Howells } 30c7f75ef3SDavid Howells static void afs_folio_start_fscache(bool caching, struct folio *folio) 31c7f75ef3SDavid Howells { 32c7f75ef3SDavid Howells if (caching) 33c7f75ef3SDavid Howells folio_start_fscache(folio); 34c7f75ef3SDavid Howells } 35c7f75ef3SDavid Howells #else 36c7f75ef3SDavid Howells static void afs_folio_start_fscache(bool caching, struct folio *folio) 37c7f75ef3SDavid Howells { 38c7f75ef3SDavid Howells } 39c7f75ef3SDavid Howells #endif 4031143d5dSDavid Howells 4131143d5dSDavid Howells /* 4231143d5dSDavid Howells * prepare to perform part of a write to a page 4331143d5dSDavid Howells */ 4415b4650eSNick Piggin int afs_write_begin(struct file *file, struct address_space *mapping, 4515b4650eSNick Piggin loff_t pos, unsigned len, unsigned flags, 4621db2cdcSDavid Howells struct page **_page, void **fsdata) 4731143d5dSDavid Howells { 48496ad9aaSAl Viro struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 4978525c74SDavid Howells struct folio *folio; 504343d008SDavid Howells unsigned long priv; 51e87b03f5SDavid Howells unsigned f, from; 52e87b03f5SDavid Howells unsigned t, to; 53e87b03f5SDavid Howells pgoff_t index; 5431143d5dSDavid Howells int ret; 5531143d5dSDavid Howells 56e87b03f5SDavid Howells _enter("{%llx:%llu},%llx,%x", 57e87b03f5SDavid Howells vnode->fid.vid, vnode->fid.vnode, pos, len); 5831143d5dSDavid Howells 593003bbd0SDavid Howells /* Prefetch area to be written into the cache if we're caching this 603003bbd0SDavid Howells * file. We need to do this before we get a lock on the page in case 613003bbd0SDavid Howells * there's more than one writer competing for the same cache block. 623003bbd0SDavid Howells */ 6378525c74SDavid Howells ret = netfs_write_begin(file, mapping, pos, len, flags, &folio, fsdata, 643003bbd0SDavid Howells &afs_req_ops, NULL); 653003bbd0SDavid Howells if (ret < 0) 6631143d5dSDavid Howells return ret; 67630f5ddaSDavid Howells 6878525c74SDavid Howells index = folio_index(folio); 69e87b03f5SDavid Howells from = pos - index * PAGE_SIZE; 70e87b03f5SDavid Howells to = from + len; 71e87b03f5SDavid Howells 7231143d5dSDavid Howells try_again: 734343d008SDavid Howells /* See if this page is already partially written in a way that we can 744343d008SDavid Howells * merge the new write with. 754343d008SDavid Howells */ 7678525c74SDavid Howells if (folio_test_private(folio)) { 7778525c74SDavid Howells priv = (unsigned long)folio_get_private(folio); 7878525c74SDavid Howells f = afs_folio_dirty_from(folio, priv); 7978525c74SDavid Howells t = afs_folio_dirty_to(folio, priv); 804343d008SDavid Howells ASSERTCMP(f, <=, t); 8131143d5dSDavid Howells 8278525c74SDavid Howells if (folio_test_writeback(folio)) { 8378525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("alrdy"), folio); 845a039c32SDavid Howells goto flush_conflicting_write; 855a039c32SDavid Howells } 865a813276SDavid Howells /* If the file is being filled locally, allow inter-write 875a813276SDavid Howells * spaces to be merged into writes. If it's not, only write 885a813276SDavid Howells * back what the user gives us. 895a813276SDavid Howells */ 905a813276SDavid Howells if (!test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags) && 915a813276SDavid Howells (to < f || from > t)) 924343d008SDavid Howells goto flush_conflicting_write; 9331143d5dSDavid Howells } 9431143d5dSDavid Howells 9578525c74SDavid Howells *_page = &folio->page; 964343d008SDavid Howells _leave(" = 0"); 9731143d5dSDavid Howells return 0; 9831143d5dSDavid Howells 994343d008SDavid Howells /* The previous write and this write aren't adjacent or overlapping, so 1004343d008SDavid Howells * flush the page out. 1014343d008SDavid Howells */ 1024343d008SDavid Howells flush_conflicting_write: 10331143d5dSDavid Howells _debug("flush conflict"); 10478525c74SDavid Howells ret = folio_write_one(folio); 10521db2cdcSDavid Howells if (ret < 0) 10621db2cdcSDavid Howells goto error; 10731143d5dSDavid Howells 10878525c74SDavid Howells ret = folio_lock_killable(folio); 10921db2cdcSDavid Howells if (ret < 0) 11021db2cdcSDavid Howells goto error; 11121db2cdcSDavid Howells goto try_again; 11221db2cdcSDavid Howells 11321db2cdcSDavid Howells error: 11478525c74SDavid Howells folio_put(folio); 1154343d008SDavid Howells _leave(" = %d", ret); 1164343d008SDavid Howells return ret; 1174343d008SDavid Howells } 11831143d5dSDavid Howells 11931143d5dSDavid Howells /* 12031143d5dSDavid Howells * finalise part of a write to a page 12131143d5dSDavid Howells */ 12215b4650eSNick Piggin int afs_write_end(struct file *file, struct address_space *mapping, 12315b4650eSNick Piggin loff_t pos, unsigned len, unsigned copied, 12478525c74SDavid Howells struct page *subpage, void *fsdata) 12531143d5dSDavid Howells { 12678525c74SDavid Howells struct folio *folio = page_folio(subpage); 127496ad9aaSAl Viro struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 128f792e3acSDavid Howells unsigned long priv; 12978525c74SDavid Howells unsigned int f, from = offset_in_folio(folio, pos); 130f792e3acSDavid Howells unsigned int t, to = from + copied; 131c7f75ef3SDavid Howells loff_t i_size, write_end_pos; 13231143d5dSDavid Howells 1333b6492dfSDavid Howells _enter("{%llx:%llu},{%lx}", 13478525c74SDavid Howells vnode->fid.vid, vnode->fid.vnode, folio_index(folio)); 13531143d5dSDavid Howells 13678525c74SDavid Howells if (!folio_test_uptodate(folio)) { 13766e9c6a8SDavid Howells if (copied < len) { 13866e9c6a8SDavid Howells copied = 0; 13966e9c6a8SDavid Howells goto out; 14066e9c6a8SDavid Howells } 14166e9c6a8SDavid Howells 14278525c74SDavid Howells folio_mark_uptodate(folio); 14366e9c6a8SDavid Howells } 14466e9c6a8SDavid Howells 1453ad216eeSDavid Howells if (copied == 0) 1463ad216eeSDavid Howells goto out; 1473ad216eeSDavid Howells 148c7f75ef3SDavid Howells write_end_pos = pos + copied; 14931143d5dSDavid Howells 15031143d5dSDavid Howells i_size = i_size_read(&vnode->vfs_inode); 151c7f75ef3SDavid Howells if (write_end_pos > i_size) { 1521f32ef79SDavid Howells write_seqlock(&vnode->cb_lock); 15331143d5dSDavid Howells i_size = i_size_read(&vnode->vfs_inode); 154c7f75ef3SDavid Howells if (write_end_pos > i_size) 155c7f75ef3SDavid Howells afs_set_i_size(vnode, write_end_pos); 1561f32ef79SDavid Howells write_sequnlock(&vnode->cb_lock); 157c7f75ef3SDavid Howells fscache_update_cookie(afs_vnode_cache(vnode), NULL, &write_end_pos); 15831143d5dSDavid Howells } 15931143d5dSDavid Howells 16078525c74SDavid Howells if (folio_test_private(folio)) { 16178525c74SDavid Howells priv = (unsigned long)folio_get_private(folio); 16278525c74SDavid Howells f = afs_folio_dirty_from(folio, priv); 16378525c74SDavid Howells t = afs_folio_dirty_to(folio, priv); 164f792e3acSDavid Howells if (from < f) 165f792e3acSDavid Howells f = from; 166f792e3acSDavid Howells if (to > t) 167f792e3acSDavid Howells t = to; 16878525c74SDavid Howells priv = afs_folio_dirty(folio, f, t); 16978525c74SDavid Howells folio_change_private(folio, (void *)priv); 17078525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("dirty+"), folio); 171f792e3acSDavid Howells } else { 17278525c74SDavid Howells priv = afs_folio_dirty(folio, from, to); 17378525c74SDavid Howells folio_attach_private(folio, (void *)priv); 17478525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("dirty"), folio); 175f792e3acSDavid Howells } 176f792e3acSDavid Howells 17778525c74SDavid Howells if (folio_mark_dirty(folio)) 17878525c74SDavid Howells _debug("dirtied %lx", folio_index(folio)); 179afae457dSDavid Howells 180afae457dSDavid Howells out: 18178525c74SDavid Howells folio_unlock(folio); 18278525c74SDavid Howells folio_put(folio); 1833003bbd0SDavid Howells return copied; 18431143d5dSDavid Howells } 18531143d5dSDavid Howells 18631143d5dSDavid Howells /* 18731143d5dSDavid Howells * kill all the pages in the given range 18831143d5dSDavid Howells */ 1894343d008SDavid Howells static void afs_kill_pages(struct address_space *mapping, 190e87b03f5SDavid Howells loff_t start, loff_t len) 19131143d5dSDavid Howells { 1924343d008SDavid Howells struct afs_vnode *vnode = AFS_FS_I(mapping->host); 19378525c74SDavid Howells struct folio *folio; 19478525c74SDavid Howells pgoff_t index = start / PAGE_SIZE; 19578525c74SDavid Howells pgoff_t last = (start + len - 1) / PAGE_SIZE, next; 19631143d5dSDavid Howells 197e87b03f5SDavid Howells _enter("{%llx:%llu},%llx @%llx", 198e87b03f5SDavid Howells vnode->fid.vid, vnode->fid.vnode, len, start); 19931143d5dSDavid Howells 20031143d5dSDavid Howells do { 20178525c74SDavid Howells _debug("kill %lx (to %lx)", index, last); 20231143d5dSDavid Howells 20378525c74SDavid Howells folio = filemap_get_folio(mapping, index); 20478525c74SDavid Howells if (!folio) { 20578525c74SDavid Howells next = index + 1; 20678525c74SDavid Howells continue; 2074343d008SDavid Howells } 2084343d008SDavid Howells 20978525c74SDavid Howells next = folio_next_index(folio); 21078525c74SDavid Howells 21178525c74SDavid Howells folio_clear_uptodate(folio); 21278525c74SDavid Howells folio_end_writeback(folio); 21378525c74SDavid Howells folio_lock(folio); 21478525c74SDavid Howells generic_error_remove_page(mapping, &folio->page); 21578525c74SDavid Howells folio_unlock(folio); 21678525c74SDavid Howells folio_put(folio); 21778525c74SDavid Howells 21878525c74SDavid Howells } while (index = next, index <= last); 2194343d008SDavid Howells 2204343d008SDavid Howells _leave(""); 2214343d008SDavid Howells } 2224343d008SDavid Howells 2234343d008SDavid Howells /* 2244343d008SDavid Howells * Redirty all the pages in a given range. 2254343d008SDavid Howells */ 2264343d008SDavid Howells static void afs_redirty_pages(struct writeback_control *wbc, 2274343d008SDavid Howells struct address_space *mapping, 228e87b03f5SDavid Howells loff_t start, loff_t len) 2294343d008SDavid Howells { 2304343d008SDavid Howells struct afs_vnode *vnode = AFS_FS_I(mapping->host); 23178525c74SDavid Howells struct folio *folio; 23278525c74SDavid Howells pgoff_t index = start / PAGE_SIZE; 23378525c74SDavid Howells pgoff_t last = (start + len - 1) / PAGE_SIZE, next; 2344343d008SDavid Howells 235e87b03f5SDavid Howells _enter("{%llx:%llu},%llx @%llx", 236e87b03f5SDavid Howells vnode->fid.vid, vnode->fid.vnode, len, start); 2374343d008SDavid Howells 2384343d008SDavid Howells do { 239e87b03f5SDavid Howells _debug("redirty %llx @%llx", len, start); 2404343d008SDavid Howells 24178525c74SDavid Howells folio = filemap_get_folio(mapping, index); 24278525c74SDavid Howells if (!folio) { 24378525c74SDavid Howells next = index + 1; 24478525c74SDavid Howells continue; 24531143d5dSDavid Howells } 24631143d5dSDavid Howells 24778525c74SDavid Howells next = index + folio_nr_pages(folio); 24878525c74SDavid Howells folio_redirty_for_writepage(wbc, folio); 24978525c74SDavid Howells folio_end_writeback(folio); 25078525c74SDavid Howells folio_put(folio); 25178525c74SDavid Howells } while (index = next, index <= last); 25231143d5dSDavid Howells 25331143d5dSDavid Howells _leave(""); 25431143d5dSDavid Howells } 25531143d5dSDavid Howells 25631143d5dSDavid Howells /* 257a58823acSDavid Howells * completion of write to server 258a58823acSDavid Howells */ 259e87b03f5SDavid Howells static void afs_pages_written_back(struct afs_vnode *vnode, loff_t start, unsigned int len) 260a58823acSDavid Howells { 261bd80d8a8SDavid Howells struct address_space *mapping = vnode->vfs_inode.i_mapping; 26278525c74SDavid Howells struct folio *folio; 263e87b03f5SDavid Howells pgoff_t end; 264bd80d8a8SDavid Howells 265e87b03f5SDavid Howells XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE); 266a58823acSDavid Howells 267e87b03f5SDavid Howells _enter("{%llx:%llu},{%x @%llx}", 268e87b03f5SDavid Howells vnode->fid.vid, vnode->fid.vnode, len, start); 269a58823acSDavid Howells 270bd80d8a8SDavid Howells rcu_read_lock(); 271a58823acSDavid Howells 272e87b03f5SDavid Howells end = (start + len - 1) / PAGE_SIZE; 27378525c74SDavid Howells xas_for_each(&xas, folio, end) { 27478525c74SDavid Howells if (!folio_test_writeback(folio)) { 27578525c74SDavid Howells kdebug("bad %x @%llx page %lx %lx", 27678525c74SDavid Howells len, start, folio_index(folio), end); 27778525c74SDavid Howells ASSERT(folio_test_writeback(folio)); 278e87b03f5SDavid Howells } 279a58823acSDavid Howells 28078525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("clear"), folio); 28178525c74SDavid Howells folio_detach_private(folio); 28278525c74SDavid Howells folio_end_writeback(folio); 283a58823acSDavid Howells } 284bd80d8a8SDavid Howells 285bd80d8a8SDavid Howells rcu_read_unlock(); 286a58823acSDavid Howells 287a58823acSDavid Howells afs_prune_wb_keys(vnode); 288a58823acSDavid Howells _leave(""); 289a58823acSDavid Howells } 290a58823acSDavid Howells 291a58823acSDavid Howells /* 292e49c7b2fSDavid Howells * Find a key to use for the writeback. We cached the keys used to author the 293e49c7b2fSDavid Howells * writes on the vnode. *_wbk will contain the last writeback key used or NULL 294e49c7b2fSDavid Howells * and we need to start from there if it's set. 295e49c7b2fSDavid Howells */ 296e49c7b2fSDavid Howells static int afs_get_writeback_key(struct afs_vnode *vnode, 297e49c7b2fSDavid Howells struct afs_wb_key **_wbk) 298e49c7b2fSDavid Howells { 299e49c7b2fSDavid Howells struct afs_wb_key *wbk = NULL; 300e49c7b2fSDavid Howells struct list_head *p; 301e49c7b2fSDavid Howells int ret = -ENOKEY, ret2; 302e49c7b2fSDavid Howells 303e49c7b2fSDavid Howells spin_lock(&vnode->wb_lock); 304e49c7b2fSDavid Howells if (*_wbk) 305e49c7b2fSDavid Howells p = (*_wbk)->vnode_link.next; 306e49c7b2fSDavid Howells else 307e49c7b2fSDavid Howells p = vnode->wb_keys.next; 308e49c7b2fSDavid Howells 309e49c7b2fSDavid Howells while (p != &vnode->wb_keys) { 310e49c7b2fSDavid Howells wbk = list_entry(p, struct afs_wb_key, vnode_link); 311e49c7b2fSDavid Howells _debug("wbk %u", key_serial(wbk->key)); 312e49c7b2fSDavid Howells ret2 = key_validate(wbk->key); 313e49c7b2fSDavid Howells if (ret2 == 0) { 314e49c7b2fSDavid Howells refcount_inc(&wbk->usage); 315e49c7b2fSDavid Howells _debug("USE WB KEY %u", key_serial(wbk->key)); 316e49c7b2fSDavid Howells break; 317e49c7b2fSDavid Howells } 318e49c7b2fSDavid Howells 319e49c7b2fSDavid Howells wbk = NULL; 320e49c7b2fSDavid Howells if (ret == -ENOKEY) 321e49c7b2fSDavid Howells ret = ret2; 322e49c7b2fSDavid Howells p = p->next; 323e49c7b2fSDavid Howells } 324e49c7b2fSDavid Howells 325e49c7b2fSDavid Howells spin_unlock(&vnode->wb_lock); 326e49c7b2fSDavid Howells if (*_wbk) 327e49c7b2fSDavid Howells afs_put_wb_key(*_wbk); 328e49c7b2fSDavid Howells *_wbk = wbk; 329e49c7b2fSDavid Howells return 0; 330e49c7b2fSDavid Howells } 331e49c7b2fSDavid Howells 332e49c7b2fSDavid Howells static void afs_store_data_success(struct afs_operation *op) 333e49c7b2fSDavid Howells { 334e49c7b2fSDavid Howells struct afs_vnode *vnode = op->file[0].vnode; 335e49c7b2fSDavid Howells 336da8d0755SDavid Howells op->ctime = op->file[0].scb.status.mtime_client; 337e49c7b2fSDavid Howells afs_vnode_commit_status(op, &op->file[0]); 338e49c7b2fSDavid Howells if (op->error == 0) { 339d383e346SDavid Howells if (!op->store.laundering) 340e87b03f5SDavid Howells afs_pages_written_back(vnode, op->store.pos, op->store.size); 341e49c7b2fSDavid Howells afs_stat_v(vnode, n_stores); 342bd80d8a8SDavid Howells atomic_long_add(op->store.size, &afs_v2net(vnode)->n_store_bytes); 343e49c7b2fSDavid Howells } 344e49c7b2fSDavid Howells } 345e49c7b2fSDavid Howells 346e49c7b2fSDavid Howells static const struct afs_operation_ops afs_store_data_operation = { 347e49c7b2fSDavid Howells .issue_afs_rpc = afs_fs_store_data, 348e49c7b2fSDavid Howells .issue_yfs_rpc = yfs_fs_store_data, 349e49c7b2fSDavid Howells .success = afs_store_data_success, 350e49c7b2fSDavid Howells }; 351e49c7b2fSDavid Howells 352e49c7b2fSDavid Howells /* 353d2ddc776SDavid Howells * write to a file 35431143d5dSDavid Howells */ 355e87b03f5SDavid Howells static int afs_store_data(struct afs_vnode *vnode, struct iov_iter *iter, loff_t pos, 356bd80d8a8SDavid Howells bool laundering) 35731143d5dSDavid Howells { 358e49c7b2fSDavid Howells struct afs_operation *op; 3594343d008SDavid Howells struct afs_wb_key *wbk = NULL; 360bd80d8a8SDavid Howells loff_t size = iov_iter_count(iter), i_size; 361bd80d8a8SDavid Howells int ret = -ENOKEY; 362d2ddc776SDavid Howells 363bd80d8a8SDavid Howells _enter("%s{%llx:%llu.%u},%llx,%llx", 364d2ddc776SDavid Howells vnode->volume->name, 365d2ddc776SDavid Howells vnode->fid.vid, 366d2ddc776SDavid Howells vnode->fid.vnode, 367d2ddc776SDavid Howells vnode->fid.unique, 368bd80d8a8SDavid Howells size, pos); 369d2ddc776SDavid Howells 370e49c7b2fSDavid Howells ret = afs_get_writeback_key(vnode, &wbk); 371e49c7b2fSDavid Howells if (ret) { 3724343d008SDavid Howells _leave(" = %d [no keys]", ret); 3734343d008SDavid Howells return ret; 374d2ddc776SDavid Howells } 375d2ddc776SDavid Howells 376e49c7b2fSDavid Howells op = afs_alloc_operation(wbk->key, vnode->volume); 377e49c7b2fSDavid Howells if (IS_ERR(op)) { 378e49c7b2fSDavid Howells afs_put_wb_key(wbk); 379e49c7b2fSDavid Howells return -ENOMEM; 380d2ddc776SDavid Howells } 381d2ddc776SDavid Howells 382bd80d8a8SDavid Howells i_size = i_size_read(&vnode->vfs_inode); 383bd80d8a8SDavid Howells 384e49c7b2fSDavid Howells afs_op_set_vnode(op, 0, vnode); 385e49c7b2fSDavid Howells op->file[0].dv_delta = 1; 38622650f14SDavid Howells op->file[0].modification = true; 387bd80d8a8SDavid Howells op->store.write_iter = iter; 388bd80d8a8SDavid Howells op->store.pos = pos; 389bd80d8a8SDavid Howells op->store.size = size; 390bd80d8a8SDavid Howells op->store.i_size = max(pos + size, i_size); 391d383e346SDavid Howells op->store.laundering = laundering; 392b3597945SDavid Howells op->mtime = vnode->vfs_inode.i_mtime; 393811f04baSDavid Howells op->flags |= AFS_OPERATION_UNINTR; 394e49c7b2fSDavid Howells op->ops = &afs_store_data_operation; 395e49c7b2fSDavid Howells 396e49c7b2fSDavid Howells try_next_key: 397e49c7b2fSDavid Howells afs_begin_vnode_operation(op); 398e49c7b2fSDavid Howells afs_wait_for_operation(op); 399e49c7b2fSDavid Howells 400e49c7b2fSDavid Howells switch (op->error) { 4014343d008SDavid Howells case -EACCES: 4024343d008SDavid Howells case -EPERM: 4034343d008SDavid Howells case -ENOKEY: 4044343d008SDavid Howells case -EKEYEXPIRED: 4054343d008SDavid Howells case -EKEYREJECTED: 4064343d008SDavid Howells case -EKEYREVOKED: 4074343d008SDavid Howells _debug("next"); 408e49c7b2fSDavid Howells 409e49c7b2fSDavid Howells ret = afs_get_writeback_key(vnode, &wbk); 410e49c7b2fSDavid Howells if (ret == 0) { 411e49c7b2fSDavid Howells key_put(op->key); 412e49c7b2fSDavid Howells op->key = key_get(wbk->key); 4134343d008SDavid Howells goto try_next_key; 4144343d008SDavid Howells } 415e49c7b2fSDavid Howells break; 416e49c7b2fSDavid Howells } 4174343d008SDavid Howells 4184343d008SDavid Howells afs_put_wb_key(wbk); 419e49c7b2fSDavid Howells _leave(" = %d", op->error); 420e49c7b2fSDavid Howells return afs_put_operation(op); 421d2ddc776SDavid Howells } 422d2ddc776SDavid Howells 423d2ddc776SDavid Howells /* 424810caa3eSDavid Howells * Extend the region to be written back to include subsequent contiguously 425810caa3eSDavid Howells * dirty pages if possible, but don't sleep while doing so. 426810caa3eSDavid Howells * 427810caa3eSDavid Howells * If this page holds new content, then we can include filler zeros in the 428810caa3eSDavid Howells * writeback. 42931143d5dSDavid Howells */ 430810caa3eSDavid Howells static void afs_extend_writeback(struct address_space *mapping, 431810caa3eSDavid Howells struct afs_vnode *vnode, 432810caa3eSDavid Howells long *_count, 433e87b03f5SDavid Howells loff_t start, 434e87b03f5SDavid Howells loff_t max_len, 435e87b03f5SDavid Howells bool new_content, 436c7f75ef3SDavid Howells bool caching, 437e87b03f5SDavid Howells unsigned int *_len) 43831143d5dSDavid Howells { 439e87b03f5SDavid Howells struct pagevec pvec; 44078525c74SDavid Howells struct folio *folio; 441e87b03f5SDavid Howells unsigned long priv; 442e87b03f5SDavid Howells unsigned int psize, filler = 0; 443e87b03f5SDavid Howells unsigned int f, t; 444e87b03f5SDavid Howells loff_t len = *_len; 445e87b03f5SDavid Howells pgoff_t index = (start + len) / PAGE_SIZE; 446e87b03f5SDavid Howells bool stop = true; 447e87b03f5SDavid Howells unsigned int i; 4484343d008SDavid Howells 449e87b03f5SDavid Howells XA_STATE(xas, &mapping->i_pages, index); 450e87b03f5SDavid Howells pagevec_init(&pvec); 451e87b03f5SDavid Howells 45231143d5dSDavid Howells do { 453e87b03f5SDavid Howells /* Firstly, we gather up a batch of contiguous dirty pages 454e87b03f5SDavid Howells * under the RCU read lock - but we can't clear the dirty flags 455e87b03f5SDavid Howells * there if any of those pages are mapped. 456e87b03f5SDavid Howells */ 457e87b03f5SDavid Howells rcu_read_lock(); 458e87b03f5SDavid Howells 45978525c74SDavid Howells xas_for_each(&xas, folio, ULONG_MAX) { 460e87b03f5SDavid Howells stop = true; 46178525c74SDavid Howells if (xas_retry(&xas, folio)) 462e87b03f5SDavid Howells continue; 46378525c74SDavid Howells if (xa_is_value(folio)) 464e87b03f5SDavid Howells break; 46578525c74SDavid Howells if (folio_index(folio) != index) 466e87b03f5SDavid Howells break; 467e87b03f5SDavid Howells 46878525c74SDavid Howells if (!folio_try_get_rcu(folio)) { 469e87b03f5SDavid Howells xas_reset(&xas); 470e87b03f5SDavid Howells continue; 47131143d5dSDavid Howells } 47231143d5dSDavid Howells 473e87b03f5SDavid Howells /* Has the page moved or been split? */ 47478525c74SDavid Howells if (unlikely(folio != xas_reload(&xas))) { 47578525c74SDavid Howells folio_put(folio); 4765a813276SDavid Howells break; 477581b2027SDavid Howells } 478e87b03f5SDavid Howells 47978525c74SDavid Howells if (!folio_trylock(folio)) { 48078525c74SDavid Howells folio_put(folio); 48131143d5dSDavid Howells break; 482581b2027SDavid Howells } 483c7f75ef3SDavid Howells if (!folio_test_dirty(folio) || 484c7f75ef3SDavid Howells folio_test_writeback(folio) || 485c7f75ef3SDavid Howells folio_test_fscache(folio)) { 48678525c74SDavid Howells folio_unlock(folio); 48778525c74SDavid Howells folio_put(folio); 48831143d5dSDavid Howells break; 48931143d5dSDavid Howells } 4904343d008SDavid Howells 49178525c74SDavid Howells psize = folio_size(folio); 49278525c74SDavid Howells priv = (unsigned long)folio_get_private(folio); 49378525c74SDavid Howells f = afs_folio_dirty_from(folio, priv); 49478525c74SDavid Howells t = afs_folio_dirty_to(folio, priv); 495810caa3eSDavid Howells if (f != 0 && !new_content) { 49678525c74SDavid Howells folio_unlock(folio); 49778525c74SDavid Howells folio_put(folio); 4984343d008SDavid Howells break; 4994343d008SDavid Howells } 5004343d008SDavid Howells 501e87b03f5SDavid Howells len += filler + t; 502e87b03f5SDavid Howells filler = psize - t; 503e87b03f5SDavid Howells if (len >= max_len || *_count <= 0) 504e87b03f5SDavid Howells stop = true; 505e87b03f5SDavid Howells else if (t == psize || new_content) 506e87b03f5SDavid Howells stop = false; 507e87b03f5SDavid Howells 50878525c74SDavid Howells index += folio_nr_pages(folio); 50978525c74SDavid Howells if (!pagevec_add(&pvec, &folio->page)) 510e87b03f5SDavid Howells break; 511e87b03f5SDavid Howells if (stop) 512e87b03f5SDavid Howells break; 513e87b03f5SDavid Howells } 514e87b03f5SDavid Howells 515e87b03f5SDavid Howells if (!stop) 516e87b03f5SDavid Howells xas_pause(&xas); 517e87b03f5SDavid Howells rcu_read_unlock(); 518e87b03f5SDavid Howells 519e87b03f5SDavid Howells /* Now, if we obtained any pages, we can shift them to being 520e87b03f5SDavid Howells * writable and mark them for caching. 521e87b03f5SDavid Howells */ 522e87b03f5SDavid Howells if (!pagevec_count(&pvec)) 523e87b03f5SDavid Howells break; 524e87b03f5SDavid Howells 525e87b03f5SDavid Howells for (i = 0; i < pagevec_count(&pvec); i++) { 52678525c74SDavid Howells folio = page_folio(pvec.pages[i]); 52778525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("store+"), folio); 52813524ab3SDavid Howells 52978525c74SDavid Howells if (!folio_clear_dirty_for_io(folio)) 53031143d5dSDavid Howells BUG(); 53178525c74SDavid Howells if (folio_start_writeback(folio)) 53231143d5dSDavid Howells BUG(); 533c7f75ef3SDavid Howells afs_folio_start_fscache(caching, folio); 534e87b03f5SDavid Howells 53578525c74SDavid Howells *_count -= folio_nr_pages(folio); 53678525c74SDavid Howells folio_unlock(folio); 53731143d5dSDavid Howells } 53831143d5dSDavid Howells 539e87b03f5SDavid Howells pagevec_release(&pvec); 540e87b03f5SDavid Howells cond_resched(); 541e87b03f5SDavid Howells } while (!stop); 54231143d5dSDavid Howells 543e87b03f5SDavid Howells *_len = len; 544810caa3eSDavid Howells } 545810caa3eSDavid Howells 546810caa3eSDavid Howells /* 547810caa3eSDavid Howells * Synchronously write back the locked page and any subsequent non-locked dirty 548810caa3eSDavid Howells * pages. 549810caa3eSDavid Howells */ 55078525c74SDavid Howells static ssize_t afs_write_back_from_locked_folio(struct address_space *mapping, 551810caa3eSDavid Howells struct writeback_control *wbc, 55278525c74SDavid Howells struct folio *folio, 553e87b03f5SDavid Howells loff_t start, loff_t end) 554810caa3eSDavid Howells { 555810caa3eSDavid Howells struct afs_vnode *vnode = AFS_FS_I(mapping->host); 556810caa3eSDavid Howells struct iov_iter iter; 557e87b03f5SDavid Howells unsigned long priv; 558e87b03f5SDavid Howells unsigned int offset, to, len, max_len; 559e87b03f5SDavid Howells loff_t i_size = i_size_read(&vnode->vfs_inode); 560810caa3eSDavid Howells bool new_content = test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags); 561c7f75ef3SDavid Howells bool caching = fscache_cookie_enabled(afs_vnode_cache(vnode)); 562e87b03f5SDavid Howells long count = wbc->nr_to_write; 563810caa3eSDavid Howells int ret; 564810caa3eSDavid Howells 56578525c74SDavid Howells _enter(",%lx,%llx-%llx", folio_index(folio), start, end); 566810caa3eSDavid Howells 56778525c74SDavid Howells if (folio_start_writeback(folio)) 568810caa3eSDavid Howells BUG(); 569c7f75ef3SDavid Howells afs_folio_start_fscache(caching, folio); 570810caa3eSDavid Howells 57178525c74SDavid Howells count -= folio_nr_pages(folio); 572e87b03f5SDavid Howells 573810caa3eSDavid Howells /* Find all consecutive lockable dirty pages that have contiguous 574810caa3eSDavid Howells * written regions, stopping when we find a page that is not 575810caa3eSDavid Howells * immediately lockable, is not dirty or is missing, or we reach the 576810caa3eSDavid Howells * end of the range. 577810caa3eSDavid Howells */ 57878525c74SDavid Howells priv = (unsigned long)folio_get_private(folio); 57978525c74SDavid Howells offset = afs_folio_dirty_from(folio, priv); 58078525c74SDavid Howells to = afs_folio_dirty_to(folio, priv); 58178525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("store"), folio); 582810caa3eSDavid Howells 583e87b03f5SDavid Howells len = to - offset; 584e87b03f5SDavid Howells start += offset; 585e87b03f5SDavid Howells if (start < i_size) { 586e87b03f5SDavid Howells /* Trim the write to the EOF; the extra data is ignored. Also 587e87b03f5SDavid Howells * put an upper limit on the size of a single storedata op. 588e87b03f5SDavid Howells */ 589e87b03f5SDavid Howells max_len = 65536 * 4096; 590e87b03f5SDavid Howells max_len = min_t(unsigned long long, max_len, end - start + 1); 591e87b03f5SDavid Howells max_len = min_t(unsigned long long, max_len, i_size - start); 592810caa3eSDavid Howells 593e87b03f5SDavid Howells if (len < max_len && 59478525c74SDavid Howells (to == folio_size(folio) || new_content)) 595e87b03f5SDavid Howells afs_extend_writeback(mapping, vnode, &count, 596c7f75ef3SDavid Howells start, max_len, new_content, 597c7f75ef3SDavid Howells caching, &len); 598e87b03f5SDavid Howells len = min_t(loff_t, len, max_len); 599e87b03f5SDavid Howells } 600810caa3eSDavid Howells 6014343d008SDavid Howells /* We now have a contiguous set of dirty pages, each with writeback 6024343d008SDavid Howells * set; the first page is still locked at this point, but all the rest 6034343d008SDavid Howells * have been unlocked. 6044343d008SDavid Howells */ 60578525c74SDavid Howells folio_unlock(folio); 6064343d008SDavid Howells 607e87b03f5SDavid Howells if (start < i_size) { 608e87b03f5SDavid Howells _debug("write back %x @%llx [%llx]", len, start, i_size); 60931143d5dSDavid Howells 610c7f75ef3SDavid Howells /* Speculatively write to the cache. We have to fix this up 611c7f75ef3SDavid Howells * later if the store fails. 612c7f75ef3SDavid Howells */ 613c7f75ef3SDavid Howells afs_write_to_cache(vnode, start, len, i_size, caching); 614c7f75ef3SDavid Howells 615e87b03f5SDavid Howells iov_iter_xarray(&iter, WRITE, &mapping->i_pages, start, len); 616e87b03f5SDavid Howells ret = afs_store_data(vnode, &iter, start, false); 617bd80d8a8SDavid Howells } else { 618e87b03f5SDavid Howells _debug("write discard %x @%llx [%llx]", len, start, i_size); 619e87b03f5SDavid Howells 620bd80d8a8SDavid Howells /* The dirty region was entirely beyond the EOF. */ 621c7f75ef3SDavid Howells fscache_clear_page_bits(afs_vnode_cache(vnode), 622c7f75ef3SDavid Howells mapping, start, len, caching); 623e87b03f5SDavid Howells afs_pages_written_back(vnode, start, len); 624bd80d8a8SDavid Howells ret = 0; 625bd80d8a8SDavid Howells } 626bd80d8a8SDavid Howells 62731143d5dSDavid Howells switch (ret) { 6284343d008SDavid Howells case 0: 629e87b03f5SDavid Howells wbc->nr_to_write = count; 630e87b03f5SDavid Howells ret = len; 6314343d008SDavid Howells break; 6324343d008SDavid Howells 6334343d008SDavid Howells default: 6344343d008SDavid Howells pr_notice("kAFS: Unexpected error from FS.StoreData %d\n", ret); 635df561f66SGustavo A. R. Silva fallthrough; 6364343d008SDavid Howells case -EACCES: 6374343d008SDavid Howells case -EPERM: 6384343d008SDavid Howells case -ENOKEY: 6394343d008SDavid Howells case -EKEYEXPIRED: 6404343d008SDavid Howells case -EKEYREJECTED: 6414343d008SDavid Howells case -EKEYREVOKED: 642e87b03f5SDavid Howells afs_redirty_pages(wbc, mapping, start, len); 6434343d008SDavid Howells mapping_set_error(mapping, ret); 6444343d008SDavid Howells break; 6454343d008SDavid Howells 64631143d5dSDavid Howells case -EDQUOT: 64731143d5dSDavid Howells case -ENOSPC: 648e87b03f5SDavid Howells afs_redirty_pages(wbc, mapping, start, len); 6494343d008SDavid Howells mapping_set_error(mapping, -ENOSPC); 65031143d5dSDavid Howells break; 6514343d008SDavid Howells 65231143d5dSDavid Howells case -EROFS: 65331143d5dSDavid Howells case -EIO: 65431143d5dSDavid Howells case -EREMOTEIO: 65531143d5dSDavid Howells case -EFBIG: 65631143d5dSDavid Howells case -ENOENT: 65731143d5dSDavid Howells case -ENOMEDIUM: 65831143d5dSDavid Howells case -ENXIO: 659f51375cdSDavid Howells trace_afs_file_error(vnode, ret, afs_file_error_writeback_fail); 660e87b03f5SDavid Howells afs_kill_pages(mapping, start, len); 6614343d008SDavid Howells mapping_set_error(mapping, ret); 66231143d5dSDavid Howells break; 66331143d5dSDavid Howells } 66431143d5dSDavid Howells 66531143d5dSDavid Howells _leave(" = %d", ret); 66631143d5dSDavid Howells return ret; 66731143d5dSDavid Howells } 66831143d5dSDavid Howells 66931143d5dSDavid Howells /* 67031143d5dSDavid Howells * write a page back to the server 67131143d5dSDavid Howells * - the caller locked the page for us 67231143d5dSDavid Howells */ 67378525c74SDavid Howells int afs_writepage(struct page *subpage, struct writeback_control *wbc) 67431143d5dSDavid Howells { 67578525c74SDavid Howells struct folio *folio = page_folio(subpage); 676e87b03f5SDavid Howells ssize_t ret; 677e87b03f5SDavid Howells loff_t start; 67831143d5dSDavid Howells 67978525c74SDavid Howells _enter("{%lx},", folio_index(folio)); 68031143d5dSDavid Howells 681c7f75ef3SDavid Howells #ifdef CONFIG_AFS_FSCACHE 682c7f75ef3SDavid Howells folio_wait_fscache(folio); 683c7f75ef3SDavid Howells #endif 684c7f75ef3SDavid Howells 68578525c74SDavid Howells start = folio_index(folio) * PAGE_SIZE; 68678525c74SDavid Howells ret = afs_write_back_from_locked_folio(folio_mapping(folio), wbc, 68778525c74SDavid Howells folio, start, LLONG_MAX - start); 68831143d5dSDavid Howells if (ret < 0) { 689e87b03f5SDavid Howells _leave(" = %zd", ret); 690e87b03f5SDavid Howells return ret; 69131143d5dSDavid Howells } 69231143d5dSDavid Howells 69331143d5dSDavid Howells _leave(" = 0"); 69431143d5dSDavid Howells return 0; 69531143d5dSDavid Howells } 69631143d5dSDavid Howells 69731143d5dSDavid Howells /* 69831143d5dSDavid Howells * write a region of pages back to the server 69931143d5dSDavid Howells */ 700c1206a2cSAdrian Bunk static int afs_writepages_region(struct address_space *mapping, 70131143d5dSDavid Howells struct writeback_control *wbc, 702e87b03f5SDavid Howells loff_t start, loff_t end, loff_t *_next) 70331143d5dSDavid Howells { 70478525c74SDavid Howells struct folio *folio; 70578525c74SDavid Howells struct page *head_page; 706e87b03f5SDavid Howells ssize_t ret; 707e87b03f5SDavid Howells int n; 70831143d5dSDavid Howells 709e87b03f5SDavid Howells _enter("%llx,%llx,", start, end); 71031143d5dSDavid Howells 71131143d5dSDavid Howells do { 712e87b03f5SDavid Howells pgoff_t index = start / PAGE_SIZE; 713e87b03f5SDavid Howells 714e87b03f5SDavid Howells n = find_get_pages_range_tag(mapping, &index, end / PAGE_SIZE, 71578525c74SDavid Howells PAGECACHE_TAG_DIRTY, 1, &head_page); 71631143d5dSDavid Howells if (!n) 71731143d5dSDavid Howells break; 71831143d5dSDavid Howells 71978525c74SDavid Howells folio = page_folio(head_page); 72078525c74SDavid Howells start = folio_pos(folio); /* May regress with THPs */ 721e87b03f5SDavid Howells 72278525c74SDavid Howells _debug("wback %lx", folio_index(folio)); 72331143d5dSDavid Howells 724e87b03f5SDavid Howells /* At this point we hold neither the i_pages lock nor the 725b93b0163SMatthew Wilcox * page lock: the page may be truncated or invalidated 726b93b0163SMatthew Wilcox * (changing page->mapping to NULL), or even swizzled 727b93b0163SMatthew Wilcox * back from swapper_space to tmpfs file mapping 72831143d5dSDavid Howells */ 729e87b03f5SDavid Howells if (wbc->sync_mode != WB_SYNC_NONE) { 73078525c74SDavid Howells ret = folio_lock_killable(folio); 7314343d008SDavid Howells if (ret < 0) { 73278525c74SDavid Howells folio_put(folio); 7334343d008SDavid Howells return ret; 7344343d008SDavid Howells } 735e87b03f5SDavid Howells } else { 73678525c74SDavid Howells if (!folio_trylock(folio)) { 73778525c74SDavid Howells folio_put(folio); 738e87b03f5SDavid Howells return 0; 739e87b03f5SDavid Howells } 740e87b03f5SDavid Howells } 74131143d5dSDavid Howells 74278525c74SDavid Howells if (folio_mapping(folio) != mapping || 74378525c74SDavid Howells !folio_test_dirty(folio)) { 74478525c74SDavid Howells start += folio_size(folio); 74578525c74SDavid Howells folio_unlock(folio); 74678525c74SDavid Howells folio_put(folio); 74731143d5dSDavid Howells continue; 74831143d5dSDavid Howells } 74931143d5dSDavid Howells 750c7f75ef3SDavid Howells if (folio_test_writeback(folio) || 751c7f75ef3SDavid Howells folio_test_fscache(folio)) { 75278525c74SDavid Howells folio_unlock(folio); 753c7f75ef3SDavid Howells if (wbc->sync_mode != WB_SYNC_NONE) { 75478525c74SDavid Howells folio_wait_writeback(folio); 755c7f75ef3SDavid Howells #ifdef CONFIG_AFS_FSCACHE 756c7f75ef3SDavid Howells folio_wait_fscache(folio); 757c7f75ef3SDavid Howells #endif 758c7f75ef3SDavid Howells } 75978525c74SDavid Howells folio_put(folio); 76031143d5dSDavid Howells continue; 76131143d5dSDavid Howells } 76231143d5dSDavid Howells 76378525c74SDavid Howells if (!folio_clear_dirty_for_io(folio)) 76465a15109SDavid Howells BUG(); 76578525c74SDavid Howells ret = afs_write_back_from_locked_folio(mapping, wbc, folio, start, end); 76678525c74SDavid Howells folio_put(folio); 76731143d5dSDavid Howells if (ret < 0) { 768e87b03f5SDavid Howells _leave(" = %zd", ret); 76931143d5dSDavid Howells return ret; 77031143d5dSDavid Howells } 77131143d5dSDavid Howells 772dc255730SMarc Dionne start += ret; 77331143d5dSDavid Howells 77431143d5dSDavid Howells cond_resched(); 775e87b03f5SDavid Howells } while (wbc->nr_to_write > 0); 77631143d5dSDavid Howells 777e87b03f5SDavid Howells *_next = start; 778e87b03f5SDavid Howells _leave(" = 0 [%llx]", *_next); 77931143d5dSDavid Howells return 0; 78031143d5dSDavid Howells } 78131143d5dSDavid Howells 78231143d5dSDavid Howells /* 78331143d5dSDavid Howells * write some of the pending data back to the server 78431143d5dSDavid Howells */ 78531143d5dSDavid Howells int afs_writepages(struct address_space *mapping, 78631143d5dSDavid Howells struct writeback_control *wbc) 78731143d5dSDavid Howells { 788ec0fa0b6SDavid Howells struct afs_vnode *vnode = AFS_FS_I(mapping->host); 789e87b03f5SDavid Howells loff_t start, next; 79031143d5dSDavid Howells int ret; 79131143d5dSDavid Howells 79231143d5dSDavid Howells _enter(""); 79331143d5dSDavid Howells 794ec0fa0b6SDavid Howells /* We have to be careful as we can end up racing with setattr() 795ec0fa0b6SDavid Howells * truncating the pagecache since the caller doesn't take a lock here 796ec0fa0b6SDavid Howells * to prevent it. 797ec0fa0b6SDavid Howells */ 798ec0fa0b6SDavid Howells if (wbc->sync_mode == WB_SYNC_ALL) 799ec0fa0b6SDavid Howells down_read(&vnode->validate_lock); 800ec0fa0b6SDavid Howells else if (!down_read_trylock(&vnode->validate_lock)) 801ec0fa0b6SDavid Howells return 0; 802ec0fa0b6SDavid Howells 80331143d5dSDavid Howells if (wbc->range_cyclic) { 804e87b03f5SDavid Howells start = mapping->writeback_index * PAGE_SIZE; 805e87b03f5SDavid Howells ret = afs_writepages_region(mapping, wbc, start, LLONG_MAX, &next); 806afe69498STom Rix if (ret == 0) { 807e87b03f5SDavid Howells mapping->writeback_index = next / PAGE_SIZE; 808afe69498STom Rix if (start > 0 && wbc->nr_to_write > 0) { 809afe69498STom Rix ret = afs_writepages_region(mapping, wbc, 0, 810afe69498STom Rix start, &next); 811afe69498STom Rix if (ret == 0) 812afe69498STom Rix mapping->writeback_index = 813afe69498STom Rix next / PAGE_SIZE; 814afe69498STom Rix } 815afe69498STom Rix } 81631143d5dSDavid Howells } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) { 817e87b03f5SDavid Howells ret = afs_writepages_region(mapping, wbc, 0, LLONG_MAX, &next); 818afe69498STom Rix if (wbc->nr_to_write > 0 && ret == 0) 8195a972474SDavid Howells mapping->writeback_index = next / PAGE_SIZE; 82031143d5dSDavid Howells } else { 821e87b03f5SDavid Howells ret = afs_writepages_region(mapping, wbc, 822e87b03f5SDavid Howells wbc->range_start, wbc->range_end, &next); 82331143d5dSDavid Howells } 82431143d5dSDavid Howells 825ec0fa0b6SDavid Howells up_read(&vnode->validate_lock); 82631143d5dSDavid Howells _leave(" = %d", ret); 82731143d5dSDavid Howells return ret; 82831143d5dSDavid Howells } 82931143d5dSDavid Howells 83031143d5dSDavid Howells /* 83131143d5dSDavid Howells * write to an AFS file 83231143d5dSDavid Howells */ 83350b5551dSAl Viro ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from) 83431143d5dSDavid Howells { 835496ad9aaSAl Viro struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp)); 8363978d816SDavid Howells struct afs_file *af = iocb->ki_filp->private_data; 83731143d5dSDavid Howells ssize_t result; 83850b5551dSAl Viro size_t count = iov_iter_count(from); 83931143d5dSDavid Howells 8403b6492dfSDavid Howells _enter("{%llx:%llu},{%zu},", 84150b5551dSAl Viro vnode->fid.vid, vnode->fid.vnode, count); 84231143d5dSDavid Howells 84331143d5dSDavid Howells if (IS_SWAPFILE(&vnode->vfs_inode)) { 84431143d5dSDavid Howells printk(KERN_INFO 84531143d5dSDavid Howells "AFS: Attempt to write to active swap file!\n"); 84631143d5dSDavid Howells return -EBUSY; 84731143d5dSDavid Howells } 84831143d5dSDavid Howells 84931143d5dSDavid Howells if (!count) 85031143d5dSDavid Howells return 0; 85131143d5dSDavid Howells 8523978d816SDavid Howells result = afs_validate(vnode, af->key); 8533978d816SDavid Howells if (result < 0) 8543978d816SDavid Howells return result; 8553978d816SDavid Howells 85650b5551dSAl Viro result = generic_file_write_iter(iocb, from); 85731143d5dSDavid Howells 85831143d5dSDavid Howells _leave(" = %zd", result); 85931143d5dSDavid Howells return result; 86031143d5dSDavid Howells } 86131143d5dSDavid Howells 86231143d5dSDavid Howells /* 86331143d5dSDavid Howells * flush any dirty pages for this process, and check for write errors. 86431143d5dSDavid Howells * - the return status from this call provides a reliable indication of 86531143d5dSDavid Howells * whether any write errors occurred for this process. 86631143d5dSDavid Howells */ 86702c24a82SJosef Bacik int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync) 86831143d5dSDavid Howells { 8693978d816SDavid Howells struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 8703978d816SDavid Howells struct afs_file *af = file->private_data; 8713978d816SDavid Howells int ret; 87231143d5dSDavid Howells 8733b6492dfSDavid Howells _enter("{%llx:%llu},{n=%pD},%d", 8743c981bfcSAl Viro vnode->fid.vid, vnode->fid.vnode, file, 87531143d5dSDavid Howells datasync); 87631143d5dSDavid Howells 8773978d816SDavid Howells ret = afs_validate(vnode, af->key); 8783978d816SDavid Howells if (ret < 0) 8793978d816SDavid Howells return ret; 8803978d816SDavid Howells 8814343d008SDavid Howells return file_write_and_wait_range(file, start, end); 88231143d5dSDavid Howells } 8839b3f26c9SDavid Howells 8849b3f26c9SDavid Howells /* 8859b3f26c9SDavid Howells * notification that a previously read-only page is about to become writable 8869b3f26c9SDavid Howells * - if it returns an error, the caller will deliver a bus error signal 8879b3f26c9SDavid Howells */ 8880722f186SSouptick Joarder vm_fault_t afs_page_mkwrite(struct vm_fault *vmf) 8899b3f26c9SDavid Howells { 890490e016fSMatthew Wilcox (Oracle) struct folio *folio = page_folio(vmf->page); 8911cf7a151SDavid Howells struct file *file = vmf->vma->vm_file; 8921cf7a151SDavid Howells struct inode *inode = file_inode(file); 8931cf7a151SDavid Howells struct afs_vnode *vnode = AFS_FS_I(inode); 8943978d816SDavid Howells struct afs_file *af = file->private_data; 8951cf7a151SDavid Howells unsigned long priv; 8969620ad86SMatthew Wilcox (Oracle) vm_fault_t ret = VM_FAULT_RETRY; 8979b3f26c9SDavid Howells 89878525c74SDavid Howells _enter("{{%llx:%llu}},{%lx}", vnode->fid.vid, vnode->fid.vnode, folio_index(folio)); 8999b3f26c9SDavid Howells 9003978d816SDavid Howells afs_validate(vnode, af->key); 9013978d816SDavid Howells 9021cf7a151SDavid Howells sb_start_pagefault(inode->i_sb); 9031cf7a151SDavid Howells 9041cf7a151SDavid Howells /* Wait for the page to be written to the cache before we allow it to 9051cf7a151SDavid Howells * be modified. We then assume the entire page will need writing back. 9061cf7a151SDavid Howells */ 907630f5ddaSDavid Howells #ifdef CONFIG_AFS_FSCACHE 90878525c74SDavid Howells if (folio_test_fscache(folio) && 90978525c74SDavid Howells folio_wait_fscache_killable(folio) < 0) 9109620ad86SMatthew Wilcox (Oracle) goto out; 911630f5ddaSDavid Howells #endif 9129b3f26c9SDavid Howells 913490e016fSMatthew Wilcox (Oracle) if (folio_wait_writeback_killable(folio)) 9149620ad86SMatthew Wilcox (Oracle) goto out; 9151cf7a151SDavid Howells 91678525c74SDavid Howells if (folio_lock_killable(folio) < 0) 9179620ad86SMatthew Wilcox (Oracle) goto out; 9181cf7a151SDavid Howells 91978525c74SDavid Howells /* We mustn't change folio->private until writeback is complete as that 9201cf7a151SDavid Howells * details the portion of the page we need to write back and we might 9211cf7a151SDavid Howells * need to redirty the page if there's a problem. 9221cf7a151SDavid Howells */ 923490e016fSMatthew Wilcox (Oracle) if (folio_wait_writeback_killable(folio) < 0) { 924490e016fSMatthew Wilcox (Oracle) folio_unlock(folio); 9259620ad86SMatthew Wilcox (Oracle) goto out; 9265cbf0398SDavid Howells } 9271cf7a151SDavid Howells 92878525c74SDavid Howells priv = afs_folio_dirty(folio, 0, folio_size(folio)); 92978525c74SDavid Howells priv = afs_folio_dirty_mmapped(priv); 93078525c74SDavid Howells if (folio_test_private(folio)) { 93178525c74SDavid Howells folio_change_private(folio, (void *)priv); 93278525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("mkwrite+"), folio); 933e87b03f5SDavid Howells } else { 93478525c74SDavid Howells folio_attach_private(folio, (void *)priv); 93578525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("mkwrite"), folio); 936e87b03f5SDavid Howells } 937bb413489SDavid Howells file_update_time(file); 9381cf7a151SDavid Howells 9399620ad86SMatthew Wilcox (Oracle) ret = VM_FAULT_LOCKED; 9409620ad86SMatthew Wilcox (Oracle) out: 9411cf7a151SDavid Howells sb_end_pagefault(inode->i_sb); 9429620ad86SMatthew Wilcox (Oracle) return ret; 9439b3f26c9SDavid Howells } 9444343d008SDavid Howells 9454343d008SDavid Howells /* 9464343d008SDavid Howells * Prune the keys cached for writeback. The caller must hold vnode->wb_lock. 9474343d008SDavid Howells */ 9484343d008SDavid Howells void afs_prune_wb_keys(struct afs_vnode *vnode) 9494343d008SDavid Howells { 9504343d008SDavid Howells LIST_HEAD(graveyard); 9514343d008SDavid Howells struct afs_wb_key *wbk, *tmp; 9524343d008SDavid Howells 9534343d008SDavid Howells /* Discard unused keys */ 9544343d008SDavid Howells spin_lock(&vnode->wb_lock); 9554343d008SDavid Howells 9564343d008SDavid Howells if (!mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_WRITEBACK) && 9574343d008SDavid Howells !mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_DIRTY)) { 9584343d008SDavid Howells list_for_each_entry_safe(wbk, tmp, &vnode->wb_keys, vnode_link) { 9594343d008SDavid Howells if (refcount_read(&wbk->usage) == 1) 9604343d008SDavid Howells list_move(&wbk->vnode_link, &graveyard); 9614343d008SDavid Howells } 9624343d008SDavid Howells } 9634343d008SDavid Howells 9644343d008SDavid Howells spin_unlock(&vnode->wb_lock); 9654343d008SDavid Howells 9664343d008SDavid Howells while (!list_empty(&graveyard)) { 9674343d008SDavid Howells wbk = list_entry(graveyard.next, struct afs_wb_key, vnode_link); 9684343d008SDavid Howells list_del(&wbk->vnode_link); 9694343d008SDavid Howells afs_put_wb_key(wbk); 9704343d008SDavid Howells } 9714343d008SDavid Howells } 9724343d008SDavid Howells 9734343d008SDavid Howells /* 9744343d008SDavid Howells * Clean up a page during invalidation. 9754343d008SDavid Howells */ 976a42442ddSMatthew Wilcox (Oracle) int afs_launder_folio(struct folio *folio) 9774343d008SDavid Howells { 97878525c74SDavid Howells struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio)); 979bd80d8a8SDavid Howells struct iov_iter iter; 980bd80d8a8SDavid Howells struct bio_vec bv[1]; 9814343d008SDavid Howells unsigned long priv; 9824343d008SDavid Howells unsigned int f, t; 9834343d008SDavid Howells int ret = 0; 9844343d008SDavid Howells 985a42442ddSMatthew Wilcox (Oracle) _enter("{%lx}", folio->index); 9864343d008SDavid Howells 98778525c74SDavid Howells priv = (unsigned long)folio_get_private(folio); 98878525c74SDavid Howells if (folio_clear_dirty_for_io(folio)) { 9894343d008SDavid Howells f = 0; 99078525c74SDavid Howells t = folio_size(folio); 99178525c74SDavid Howells if (folio_test_private(folio)) { 99278525c74SDavid Howells f = afs_folio_dirty_from(folio, priv); 99378525c74SDavid Howells t = afs_folio_dirty_to(folio, priv); 9944343d008SDavid Howells } 9954343d008SDavid Howells 99678525c74SDavid Howells bv[0].bv_page = &folio->page; 997bd80d8a8SDavid Howells bv[0].bv_offset = f; 998bd80d8a8SDavid Howells bv[0].bv_len = t - f; 999bd80d8a8SDavid Howells iov_iter_bvec(&iter, WRITE, bv, 1, bv[0].bv_len); 1000bd80d8a8SDavid Howells 100178525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("launder"), folio); 100278525c74SDavid Howells ret = afs_store_data(vnode, &iter, folio_pos(folio) + f, true); 10034343d008SDavid Howells } 10044343d008SDavid Howells 100578525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("laundered"), folio); 100678525c74SDavid Howells folio_detach_private(folio); 100778525c74SDavid Howells folio_wait_fscache(folio); 10084343d008SDavid Howells return ret; 100931143d5dSDavid Howells } 1010c7f75ef3SDavid Howells 1011c7f75ef3SDavid Howells /* 1012c7f75ef3SDavid Howells * Deal with the completion of writing the data to the cache. 1013c7f75ef3SDavid Howells */ 1014c7f75ef3SDavid Howells static void afs_write_to_cache_done(void *priv, ssize_t transferred_or_error, 1015c7f75ef3SDavid Howells bool was_async) 1016c7f75ef3SDavid Howells { 1017c7f75ef3SDavid Howells struct afs_vnode *vnode = priv; 1018c7f75ef3SDavid Howells 1019c7f75ef3SDavid Howells if (IS_ERR_VALUE(transferred_or_error) && 1020c7f75ef3SDavid Howells transferred_or_error != -ENOBUFS) 1021c7f75ef3SDavid Howells afs_invalidate_cache(vnode, 0); 1022c7f75ef3SDavid Howells } 1023c7f75ef3SDavid Howells 1024c7f75ef3SDavid Howells /* 1025c7f75ef3SDavid Howells * Save the write to the cache also. 1026c7f75ef3SDavid Howells */ 1027c7f75ef3SDavid Howells static void afs_write_to_cache(struct afs_vnode *vnode, 1028c7f75ef3SDavid Howells loff_t start, size_t len, loff_t i_size, 1029c7f75ef3SDavid Howells bool caching) 1030c7f75ef3SDavid Howells { 1031c7f75ef3SDavid Howells fscache_write_to_cache(afs_vnode_cache(vnode), 1032c7f75ef3SDavid Howells vnode->vfs_inode.i_mapping, start, len, i_size, 1033c7f75ef3SDavid Howells afs_write_to_cache_done, vnode, caching); 1034c7f75ef3SDavid Howells } 1035