Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 2 | /* handling of writes to regular files and writing back to the server |
| 3 | * |
| 4 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. |
| 5 | * Written by David Howells (dhowells@redhat.com) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 6 | */ |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 7 | |
Alexey Dobriyan | 4af3c9c | 2007-10-16 23:29:23 -0700 | [diff] [blame] | 8 | #include <linux/backing-dev.h> |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 9 | #include <linux/slab.h> |
| 10 | #include <linux/fs.h> |
| 11 | #include <linux/pagemap.h> |
| 12 | #include <linux/writeback.h> |
| 13 | #include <linux/pagevec.h> |
David Howells | 3003bbd | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 14 | #include <linux/netfs.h> |
| 15 | #include <linux/fscache.h> |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 16 | #include "internal.h" |
| 17 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 18 | /* |
| 19 | * mark a page as having been made dirty and thus needing writeback |
| 20 | */ |
| 21 | int afs_set_page_dirty(struct page *page) |
| 22 | { |
| 23 | _enter(""); |
| 24 | return __set_page_dirty_nobuffers(page); |
| 25 | } |
| 26 | |
| 27 | /* |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 28 | * prepare to perform part of a write to a page |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 29 | */ |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 30 | int afs_write_begin(struct file *file, struct address_space *mapping, |
| 31 | loff_t pos, unsigned len, unsigned flags, |
David Howells | 21db2cd | 2020-10-22 14:03:03 +0100 | [diff] [blame] | 32 | struct page **_page, void **fsdata) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 33 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 34 | struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 35 | struct folio *folio; |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 36 | unsigned long priv; |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 37 | unsigned f, from; |
| 38 | unsigned t, to; |
| 39 | pgoff_t index; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 40 | int ret; |
| 41 | |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 42 | _enter("{%llx:%llu},%llx,%x", |
| 43 | vnode->fid.vid, vnode->fid.vnode, pos, len); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 44 | |
David Howells | 3003bbd | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 45 | /* Prefetch area to be written into the cache if we're caching this |
| 46 | * file. We need to do this before we get a lock on the page in case |
| 47 | * there's more than one writer competing for the same cache block. |
| 48 | */ |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 49 | ret = netfs_write_begin(file, mapping, pos, len, flags, &folio, fsdata, |
David Howells | 3003bbd | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 50 | &afs_req_ops, NULL); |
| 51 | if (ret < 0) |
| 52 | return ret; |
David Howells | 630f5dd | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 53 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 54 | index = folio_index(folio); |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 55 | from = pos - index * PAGE_SIZE; |
| 56 | to = from + len; |
| 57 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 58 | try_again: |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 59 | /* See if this page is already partially written in a way that we can |
| 60 | * merge the new write with. |
| 61 | */ |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 62 | if (folio_test_private(folio)) { |
| 63 | priv = (unsigned long)folio_get_private(folio); |
| 64 | f = afs_folio_dirty_from(folio, priv); |
| 65 | t = afs_folio_dirty_to(folio, priv); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 66 | ASSERTCMP(f, <=, t); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 67 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 68 | if (folio_test_writeback(folio)) { |
| 69 | trace_afs_folio_dirty(vnode, tracepoint_string("alrdy"), folio); |
David Howells | 5a039c3 | 2017-11-18 00:13:30 +0000 | [diff] [blame] | 70 | goto flush_conflicting_write; |
| 71 | } |
David Howells | 5a81327 | 2018-04-06 14:17:26 +0100 | [diff] [blame] | 72 | /* If the file is being filled locally, allow inter-write |
| 73 | * spaces to be merged into writes. If it's not, only write |
| 74 | * back what the user gives us. |
| 75 | */ |
| 76 | if (!test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags) && |
| 77 | (to < f || from > t)) |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 78 | goto flush_conflicting_write; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 79 | } |
| 80 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 81 | *_page = &folio->page; |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 82 | _leave(" = 0"); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 83 | return 0; |
| 84 | |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 85 | /* The previous write and this write aren't adjacent or overlapping, so |
| 86 | * flush the page out. |
| 87 | */ |
| 88 | flush_conflicting_write: |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 89 | _debug("flush conflict"); |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 90 | ret = folio_write_one(folio); |
David Howells | 21db2cd | 2020-10-22 14:03:03 +0100 | [diff] [blame] | 91 | if (ret < 0) |
| 92 | goto error; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 93 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 94 | ret = folio_lock_killable(folio); |
David Howells | 21db2cd | 2020-10-22 14:03:03 +0100 | [diff] [blame] | 95 | if (ret < 0) |
| 96 | goto error; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 97 | goto try_again; |
David Howells | 21db2cd | 2020-10-22 14:03:03 +0100 | [diff] [blame] | 98 | |
| 99 | error: |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 100 | folio_put(folio); |
David Howells | 21db2cd | 2020-10-22 14:03:03 +0100 | [diff] [blame] | 101 | _leave(" = %d", ret); |
| 102 | return ret; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* |
| 106 | * finalise part of a write to a page |
| 107 | */ |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 108 | int afs_write_end(struct file *file, struct address_space *mapping, |
| 109 | loff_t pos, unsigned len, unsigned copied, |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 110 | struct page *subpage, void *fsdata) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 111 | { |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 112 | struct folio *folio = page_folio(subpage); |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 113 | struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); |
David Howells | f792e3a | 2020-10-26 14:05:33 +0000 | [diff] [blame] | 114 | unsigned long priv; |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 115 | unsigned int f, from = offset_in_folio(folio, pos); |
David Howells | f792e3a | 2020-10-26 14:05:33 +0000 | [diff] [blame] | 116 | unsigned int t, to = from + copied; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 117 | loff_t i_size, maybe_i_size; |
| 118 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 119 | _enter("{%llx:%llu},{%lx}", |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 120 | vnode->fid.vid, vnode->fid.vnode, folio_index(folio)); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 121 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 122 | if (!folio_test_uptodate(folio)) { |
David Howells | 66e9c6a | 2021-06-14 14:13:41 +0100 | [diff] [blame] | 123 | if (copied < len) { |
| 124 | copied = 0; |
| 125 | goto out; |
| 126 | } |
| 127 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 128 | folio_mark_uptodate(folio); |
David Howells | 66e9c6a | 2021-06-14 14:13:41 +0100 | [diff] [blame] | 129 | } |
| 130 | |
David Howells | 3ad216e | 2020-11-14 17:27:57 +0000 | [diff] [blame] | 131 | if (copied == 0) |
| 132 | goto out; |
| 133 | |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 134 | maybe_i_size = pos + copied; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 135 | |
| 136 | i_size = i_size_read(&vnode->vfs_inode); |
| 137 | if (maybe_i_size > i_size) { |
David Howells | 1f32ef7 | 2020-06-12 23:58:51 +0100 | [diff] [blame] | 138 | write_seqlock(&vnode->cb_lock); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 139 | i_size = i_size_read(&vnode->vfs_inode); |
| 140 | if (maybe_i_size > i_size) |
David Howells | 9d37e1c | 2021-09-08 21:55:19 +0100 | [diff] [blame] | 141 | afs_set_i_size(vnode, maybe_i_size); |
David Howells | 1f32ef7 | 2020-06-12 23:58:51 +0100 | [diff] [blame] | 142 | write_sequnlock(&vnode->cb_lock); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 143 | } |
| 144 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 145 | if (folio_test_private(folio)) { |
| 146 | priv = (unsigned long)folio_get_private(folio); |
| 147 | f = afs_folio_dirty_from(folio, priv); |
| 148 | t = afs_folio_dirty_to(folio, priv); |
David Howells | f792e3a | 2020-10-26 14:05:33 +0000 | [diff] [blame] | 149 | if (from < f) |
| 150 | f = from; |
| 151 | if (to > t) |
| 152 | t = to; |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 153 | priv = afs_folio_dirty(folio, f, t); |
| 154 | folio_change_private(folio, (void *)priv); |
| 155 | trace_afs_folio_dirty(vnode, tracepoint_string("dirty+"), folio); |
David Howells | f792e3a | 2020-10-26 14:05:33 +0000 | [diff] [blame] | 156 | } else { |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 157 | priv = afs_folio_dirty(folio, from, to); |
| 158 | folio_attach_private(folio, (void *)priv); |
| 159 | trace_afs_folio_dirty(vnode, tracepoint_string("dirty"), folio); |
David Howells | f792e3a | 2020-10-26 14:05:33 +0000 | [diff] [blame] | 160 | } |
| 161 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 162 | if (folio_mark_dirty(folio)) |
| 163 | _debug("dirtied %lx", folio_index(folio)); |
David Howells | afae457 | 2018-01-02 10:02:19 +0000 | [diff] [blame] | 164 | |
| 165 | out: |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 166 | folio_unlock(folio); |
| 167 | folio_put(folio); |
David Howells | 3003bbd | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 168 | return copied; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* |
| 172 | * kill all the pages in the given range |
| 173 | */ |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 174 | static void afs_kill_pages(struct address_space *mapping, |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 175 | loff_t start, loff_t len) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 176 | { |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 177 | struct afs_vnode *vnode = AFS_FS_I(mapping->host); |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 178 | struct folio *folio; |
| 179 | pgoff_t index = start / PAGE_SIZE; |
| 180 | pgoff_t last = (start + len - 1) / PAGE_SIZE, next; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 181 | |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 182 | _enter("{%llx:%llu},%llx @%llx", |
| 183 | vnode->fid.vid, vnode->fid.vnode, len, start); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 184 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 185 | do { |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 186 | _debug("kill %lx (to %lx)", index, last); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 187 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 188 | folio = filemap_get_folio(mapping, index); |
| 189 | if (!folio) { |
| 190 | next = index + 1; |
| 191 | continue; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 192 | } |
| 193 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 194 | next = folio_next_index(folio); |
| 195 | |
| 196 | folio_clear_uptodate(folio); |
| 197 | folio_end_writeback(folio); |
| 198 | folio_lock(folio); |
| 199 | generic_error_remove_page(mapping, &folio->page); |
| 200 | folio_unlock(folio); |
| 201 | folio_put(folio); |
| 202 | |
| 203 | } while (index = next, index <= last); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 204 | |
| 205 | _leave(""); |
| 206 | } |
| 207 | |
| 208 | /* |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 209 | * Redirty all the pages in a given range. |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 210 | */ |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 211 | static void afs_redirty_pages(struct writeback_control *wbc, |
| 212 | struct address_space *mapping, |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 213 | loff_t start, loff_t len) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 214 | { |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 215 | struct afs_vnode *vnode = AFS_FS_I(mapping->host); |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 216 | struct folio *folio; |
| 217 | pgoff_t index = start / PAGE_SIZE; |
| 218 | pgoff_t last = (start + len - 1) / PAGE_SIZE, next; |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 219 | |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 220 | _enter("{%llx:%llu},%llx @%llx", |
| 221 | vnode->fid.vid, vnode->fid.vnode, len, start); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 222 | |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 223 | do { |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 224 | _debug("redirty %llx @%llx", len, start); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 225 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 226 | folio = filemap_get_folio(mapping, index); |
| 227 | if (!folio) { |
| 228 | next = index + 1; |
| 229 | continue; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 230 | } |
| 231 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 232 | next = index + folio_nr_pages(folio); |
| 233 | folio_redirty_for_writepage(wbc, folio); |
| 234 | folio_end_writeback(folio); |
| 235 | folio_put(folio); |
| 236 | } while (index = next, index <= last); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 237 | |
| 238 | _leave(""); |
| 239 | } |
| 240 | |
| 241 | /* |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 242 | * completion of write to server |
| 243 | */ |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 244 | static void afs_pages_written_back(struct afs_vnode *vnode, loff_t start, unsigned int len) |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 245 | { |
David Howells | bd80d8a | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 246 | struct address_space *mapping = vnode->vfs_inode.i_mapping; |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 247 | struct folio *folio; |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 248 | pgoff_t end; |
David Howells | bd80d8a | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 249 | |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 250 | XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 251 | |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 252 | _enter("{%llx:%llu},{%x @%llx}", |
| 253 | vnode->fid.vid, vnode->fid.vnode, len, start); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 254 | |
David Howells | bd80d8a | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 255 | rcu_read_lock(); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 256 | |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 257 | end = (start + len - 1) / PAGE_SIZE; |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 258 | xas_for_each(&xas, folio, end) { |
| 259 | if (!folio_test_writeback(folio)) { |
| 260 | kdebug("bad %x @%llx page %lx %lx", |
| 261 | len, start, folio_index(folio), end); |
| 262 | ASSERT(folio_test_writeback(folio)); |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 263 | } |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 264 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 265 | trace_afs_folio_dirty(vnode, tracepoint_string("clear"), folio); |
| 266 | folio_detach_private(folio); |
| 267 | folio_end_writeback(folio); |
David Howells | bd80d8a | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 268 | } |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 269 | |
David Howells | bd80d8a | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 270 | rcu_read_unlock(); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 271 | |
| 272 | afs_prune_wb_keys(vnode); |
| 273 | _leave(""); |
| 274 | } |
| 275 | |
| 276 | /* |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 277 | * Find a key to use for the writeback. We cached the keys used to author the |
| 278 | * writes on the vnode. *_wbk will contain the last writeback key used or NULL |
| 279 | * and we need to start from there if it's set. |
| 280 | */ |
| 281 | static int afs_get_writeback_key(struct afs_vnode *vnode, |
| 282 | struct afs_wb_key **_wbk) |
| 283 | { |
| 284 | struct afs_wb_key *wbk = NULL; |
| 285 | struct list_head *p; |
| 286 | int ret = -ENOKEY, ret2; |
| 287 | |
| 288 | spin_lock(&vnode->wb_lock); |
| 289 | if (*_wbk) |
| 290 | p = (*_wbk)->vnode_link.next; |
| 291 | else |
| 292 | p = vnode->wb_keys.next; |
| 293 | |
| 294 | while (p != &vnode->wb_keys) { |
| 295 | wbk = list_entry(p, struct afs_wb_key, vnode_link); |
| 296 | _debug("wbk %u", key_serial(wbk->key)); |
| 297 | ret2 = key_validate(wbk->key); |
| 298 | if (ret2 == 0) { |
| 299 | refcount_inc(&wbk->usage); |
| 300 | _debug("USE WB KEY %u", key_serial(wbk->key)); |
| 301 | break; |
| 302 | } |
| 303 | |
| 304 | wbk = NULL; |
| 305 | if (ret == -ENOKEY) |
| 306 | ret = ret2; |
| 307 | p = p->next; |
| 308 | } |
| 309 | |
| 310 | spin_unlock(&vnode->wb_lock); |
| 311 | if (*_wbk) |
| 312 | afs_put_wb_key(*_wbk); |
| 313 | *_wbk = wbk; |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static void afs_store_data_success(struct afs_operation *op) |
| 318 | { |
| 319 | struct afs_vnode *vnode = op->file[0].vnode; |
| 320 | |
David Howells | da8d075 | 2020-06-13 19:34:59 +0100 | [diff] [blame] | 321 | op->ctime = op->file[0].scb.status.mtime_client; |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 322 | afs_vnode_commit_status(op, &op->file[0]); |
| 323 | if (op->error == 0) { |
David Howells | d383e34 | 2020-10-22 14:40:31 +0100 | [diff] [blame] | 324 | if (!op->store.laundering) |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 325 | afs_pages_written_back(vnode, op->store.pos, op->store.size); |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 326 | afs_stat_v(vnode, n_stores); |
David Howells | bd80d8a | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 327 | atomic_long_add(op->store.size, &afs_v2net(vnode)->n_store_bytes); |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
| 331 | static const struct afs_operation_ops afs_store_data_operation = { |
| 332 | .issue_afs_rpc = afs_fs_store_data, |
| 333 | .issue_yfs_rpc = yfs_fs_store_data, |
| 334 | .success = afs_store_data_success, |
| 335 | }; |
| 336 | |
| 337 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 338 | * write to a file |
| 339 | */ |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 340 | static int afs_store_data(struct afs_vnode *vnode, struct iov_iter *iter, loff_t pos, |
David Howells | bd80d8a | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 341 | bool laundering) |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 342 | { |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 343 | struct afs_operation *op; |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 344 | struct afs_wb_key *wbk = NULL; |
David Howells | bd80d8a | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 345 | loff_t size = iov_iter_count(iter), i_size; |
| 346 | int ret = -ENOKEY; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 347 | |
David Howells | bd80d8a | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 348 | _enter("%s{%llx:%llu.%u},%llx,%llx", |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 349 | vnode->volume->name, |
| 350 | vnode->fid.vid, |
| 351 | vnode->fid.vnode, |
| 352 | vnode->fid.unique, |
David Howells | bd80d8a | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 353 | size, pos); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 354 | |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 355 | ret = afs_get_writeback_key(vnode, &wbk); |
| 356 | if (ret) { |
| 357 | _leave(" = %d [no keys]", ret); |
| 358 | return ret; |
| 359 | } |
| 360 | |
| 361 | op = afs_alloc_operation(wbk->key, vnode->volume); |
| 362 | if (IS_ERR(op)) { |
| 363 | afs_put_wb_key(wbk); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 364 | return -ENOMEM; |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 365 | } |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 366 | |
David Howells | bd80d8a | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 367 | i_size = i_size_read(&vnode->vfs_inode); |
| 368 | |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 369 | afs_op_set_vnode(op, 0, vnode); |
| 370 | op->file[0].dv_delta = 1; |
David Howells | 22650f1 | 2021-04-30 13:47:08 +0100 | [diff] [blame] | 371 | op->file[0].modification = true; |
David Howells | bd80d8a | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 372 | op->store.write_iter = iter; |
| 373 | op->store.pos = pos; |
David Howells | bd80d8a | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 374 | op->store.size = size; |
| 375 | op->store.i_size = max(pos + size, i_size); |
David Howells | d383e34 | 2020-10-22 14:40:31 +0100 | [diff] [blame] | 376 | op->store.laundering = laundering; |
David Howells | b359794 | 2020-06-11 21:50:24 +0100 | [diff] [blame] | 377 | op->mtime = vnode->vfs_inode.i_mtime; |
David Howells | 811f04b | 2020-07-08 09:27:07 +0100 | [diff] [blame] | 378 | op->flags |= AFS_OPERATION_UNINTR; |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 379 | op->ops = &afs_store_data_operation; |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 380 | |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 381 | try_next_key: |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 382 | afs_begin_vnode_operation(op); |
| 383 | afs_wait_for_operation(op); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 384 | |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 385 | switch (op->error) { |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 386 | case -EACCES: |
| 387 | case -EPERM: |
| 388 | case -ENOKEY: |
| 389 | case -EKEYEXPIRED: |
| 390 | case -EKEYREJECTED: |
| 391 | case -EKEYREVOKED: |
| 392 | _debug("next"); |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 393 | |
| 394 | ret = afs_get_writeback_key(vnode, &wbk); |
| 395 | if (ret == 0) { |
| 396 | key_put(op->key); |
| 397 | op->key = key_get(wbk->key); |
| 398 | goto try_next_key; |
| 399 | } |
| 400 | break; |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | afs_put_wb_key(wbk); |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 404 | _leave(" = %d", op->error); |
| 405 | return afs_put_operation(op); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /* |
David Howells | 810caa3 | 2020-10-30 10:01:09 +0000 | [diff] [blame] | 409 | * Extend the region to be written back to include subsequent contiguously |
| 410 | * dirty pages if possible, but don't sleep while doing so. |
| 411 | * |
| 412 | * If this page holds new content, then we can include filler zeros in the |
| 413 | * writeback. |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 414 | */ |
David Howells | 810caa3 | 2020-10-30 10:01:09 +0000 | [diff] [blame] | 415 | static void afs_extend_writeback(struct address_space *mapping, |
| 416 | struct afs_vnode *vnode, |
| 417 | long *_count, |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 418 | loff_t start, |
| 419 | loff_t max_len, |
| 420 | bool new_content, |
| 421 | unsigned int *_len) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 422 | { |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 423 | struct pagevec pvec; |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 424 | struct folio *folio; |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 425 | unsigned long priv; |
| 426 | unsigned int psize, filler = 0; |
| 427 | unsigned int f, t; |
| 428 | loff_t len = *_len; |
| 429 | pgoff_t index = (start + len) / PAGE_SIZE; |
| 430 | bool stop = true; |
| 431 | unsigned int i; |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 432 | |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 433 | XA_STATE(xas, &mapping->i_pages, index); |
| 434 | pagevec_init(&pvec); |
| 435 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 436 | do { |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 437 | /* Firstly, we gather up a batch of contiguous dirty pages |
| 438 | * under the RCU read lock - but we can't clear the dirty flags |
| 439 | * there if any of those pages are mapped. |
| 440 | */ |
| 441 | rcu_read_lock(); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 442 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 443 | xas_for_each(&xas, folio, ULONG_MAX) { |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 444 | stop = true; |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 445 | if (xas_retry(&xas, folio)) |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 446 | continue; |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 447 | if (xa_is_value(folio)) |
David Howells | 5a81327 | 2018-04-06 14:17:26 +0100 | [diff] [blame] | 448 | break; |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 449 | if (folio_index(folio) != index) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 450 | break; |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 451 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 452 | if (!folio_try_get_rcu(folio)) { |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 453 | xas_reset(&xas); |
| 454 | continue; |
| 455 | } |
| 456 | |
| 457 | /* Has the page moved or been split? */ |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 458 | if (unlikely(folio != xas_reload(&xas))) { |
| 459 | folio_put(folio); |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 460 | break; |
David Howells | 581b202 | 2021-09-01 09:15:21 +0100 | [diff] [blame] | 461 | } |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 462 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 463 | if (!folio_trylock(folio)) { |
| 464 | folio_put(folio); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 465 | break; |
David Howells | 581b202 | 2021-09-01 09:15:21 +0100 | [diff] [blame] | 466 | } |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 467 | if (!folio_test_dirty(folio) || folio_test_writeback(folio)) { |
| 468 | folio_unlock(folio); |
| 469 | folio_put(folio); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 470 | break; |
| 471 | } |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 472 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 473 | psize = folio_size(folio); |
| 474 | priv = (unsigned long)folio_get_private(folio); |
| 475 | f = afs_folio_dirty_from(folio, priv); |
| 476 | t = afs_folio_dirty_to(folio, priv); |
David Howells | 810caa3 | 2020-10-30 10:01:09 +0000 | [diff] [blame] | 477 | if (f != 0 && !new_content) { |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 478 | folio_unlock(folio); |
| 479 | folio_put(folio); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 480 | break; |
| 481 | } |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 482 | |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 483 | len += filler + t; |
| 484 | filler = psize - t; |
| 485 | if (len >= max_len || *_count <= 0) |
| 486 | stop = true; |
| 487 | else if (t == psize || new_content) |
| 488 | stop = false; |
| 489 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 490 | index += folio_nr_pages(folio); |
| 491 | if (!pagevec_add(&pvec, &folio->page)) |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 492 | break; |
| 493 | if (stop) |
| 494 | break; |
| 495 | } |
| 496 | |
| 497 | if (!stop) |
| 498 | xas_pause(&xas); |
| 499 | rcu_read_unlock(); |
| 500 | |
| 501 | /* Now, if we obtained any pages, we can shift them to being |
| 502 | * writable and mark them for caching. |
| 503 | */ |
| 504 | if (!pagevec_count(&pvec)) |
| 505 | break; |
| 506 | |
| 507 | for (i = 0; i < pagevec_count(&pvec); i++) { |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 508 | folio = page_folio(pvec.pages[i]); |
| 509 | trace_afs_folio_dirty(vnode, tracepoint_string("store+"), folio); |
David Howells | 13524ab | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 510 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 511 | if (!folio_clear_dirty_for_io(folio)) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 512 | BUG(); |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 513 | if (folio_start_writeback(folio)) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 514 | BUG(); |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 515 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 516 | *_count -= folio_nr_pages(folio); |
| 517 | folio_unlock(folio); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 518 | } |
| 519 | |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 520 | pagevec_release(&pvec); |
| 521 | cond_resched(); |
| 522 | } while (!stop); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 523 | |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 524 | *_len = len; |
David Howells | 810caa3 | 2020-10-30 10:01:09 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /* |
| 528 | * Synchronously write back the locked page and any subsequent non-locked dirty |
| 529 | * pages. |
| 530 | */ |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 531 | static ssize_t afs_write_back_from_locked_folio(struct address_space *mapping, |
| 532 | struct writeback_control *wbc, |
| 533 | struct folio *folio, |
| 534 | loff_t start, loff_t end) |
David Howells | 810caa3 | 2020-10-30 10:01:09 +0000 | [diff] [blame] | 535 | { |
| 536 | struct afs_vnode *vnode = AFS_FS_I(mapping->host); |
| 537 | struct iov_iter iter; |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 538 | unsigned long priv; |
| 539 | unsigned int offset, to, len, max_len; |
| 540 | loff_t i_size = i_size_read(&vnode->vfs_inode); |
David Howells | 810caa3 | 2020-10-30 10:01:09 +0000 | [diff] [blame] | 541 | bool new_content = test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags); |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 542 | long count = wbc->nr_to_write; |
David Howells | 810caa3 | 2020-10-30 10:01:09 +0000 | [diff] [blame] | 543 | int ret; |
| 544 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 545 | _enter(",%lx,%llx-%llx", folio_index(folio), start, end); |
David Howells | 810caa3 | 2020-10-30 10:01:09 +0000 | [diff] [blame] | 546 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 547 | if (folio_start_writeback(folio)) |
David Howells | 810caa3 | 2020-10-30 10:01:09 +0000 | [diff] [blame] | 548 | BUG(); |
| 549 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 550 | count -= folio_nr_pages(folio); |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 551 | |
David Howells | 810caa3 | 2020-10-30 10:01:09 +0000 | [diff] [blame] | 552 | /* Find all consecutive lockable dirty pages that have contiguous |
| 553 | * written regions, stopping when we find a page that is not |
| 554 | * immediately lockable, is not dirty or is missing, or we reach the |
| 555 | * end of the range. |
| 556 | */ |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 557 | priv = (unsigned long)folio_get_private(folio); |
| 558 | offset = afs_folio_dirty_from(folio, priv); |
| 559 | to = afs_folio_dirty_to(folio, priv); |
| 560 | trace_afs_folio_dirty(vnode, tracepoint_string("store"), folio); |
David Howells | 810caa3 | 2020-10-30 10:01:09 +0000 | [diff] [blame] | 561 | |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 562 | len = to - offset; |
| 563 | start += offset; |
| 564 | if (start < i_size) { |
| 565 | /* Trim the write to the EOF; the extra data is ignored. Also |
| 566 | * put an upper limit on the size of a single storedata op. |
| 567 | */ |
| 568 | max_len = 65536 * 4096; |
| 569 | max_len = min_t(unsigned long long, max_len, end - start + 1); |
| 570 | max_len = min_t(unsigned long long, max_len, i_size - start); |
David Howells | 810caa3 | 2020-10-30 10:01:09 +0000 | [diff] [blame] | 571 | |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 572 | if (len < max_len && |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 573 | (to == folio_size(folio) || new_content)) |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 574 | afs_extend_writeback(mapping, vnode, &count, |
| 575 | start, max_len, new_content, &len); |
| 576 | len = min_t(loff_t, len, max_len); |
| 577 | } |
David Howells | 810caa3 | 2020-10-30 10:01:09 +0000 | [diff] [blame] | 578 | |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 579 | /* We now have a contiguous set of dirty pages, each with writeback |
| 580 | * set; the first page is still locked at this point, but all the rest |
| 581 | * have been unlocked. |
| 582 | */ |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 583 | folio_unlock(folio); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 584 | |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 585 | if (start < i_size) { |
| 586 | _debug("write back %x @%llx [%llx]", len, start, i_size); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 587 | |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 588 | iov_iter_xarray(&iter, WRITE, &mapping->i_pages, start, len); |
| 589 | ret = afs_store_data(vnode, &iter, start, false); |
David Howells | bd80d8a | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 590 | } else { |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 591 | _debug("write discard %x @%llx [%llx]", len, start, i_size); |
| 592 | |
David Howells | bd80d8a | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 593 | /* The dirty region was entirely beyond the EOF. */ |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 594 | afs_pages_written_back(vnode, start, len); |
David Howells | bd80d8a | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 595 | ret = 0; |
| 596 | } |
| 597 | |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 598 | switch (ret) { |
| 599 | case 0: |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 600 | wbc->nr_to_write = count; |
| 601 | ret = len; |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 602 | break; |
| 603 | |
| 604 | default: |
| 605 | pr_notice("kAFS: Unexpected error from FS.StoreData %d\n", ret); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 606 | fallthrough; |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 607 | case -EACCES: |
| 608 | case -EPERM: |
| 609 | case -ENOKEY: |
| 610 | case -EKEYEXPIRED: |
| 611 | case -EKEYREJECTED: |
| 612 | case -EKEYREVOKED: |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 613 | afs_redirty_pages(wbc, mapping, start, len); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 614 | mapping_set_error(mapping, ret); |
| 615 | break; |
| 616 | |
| 617 | case -EDQUOT: |
| 618 | case -ENOSPC: |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 619 | afs_redirty_pages(wbc, mapping, start, len); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 620 | mapping_set_error(mapping, -ENOSPC); |
| 621 | break; |
| 622 | |
| 623 | case -EROFS: |
| 624 | case -EIO: |
| 625 | case -EREMOTEIO: |
| 626 | case -EFBIG: |
| 627 | case -ENOENT: |
| 628 | case -ENOMEDIUM: |
| 629 | case -ENXIO: |
David Howells | f51375c | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 630 | trace_afs_file_error(vnode, ret, afs_file_error_writeback_fail); |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 631 | afs_kill_pages(mapping, start, len); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 632 | mapping_set_error(mapping, ret); |
| 633 | break; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | _leave(" = %d", ret); |
| 637 | return ret; |
| 638 | } |
| 639 | |
| 640 | /* |
| 641 | * write a page back to the server |
| 642 | * - the caller locked the page for us |
| 643 | */ |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 644 | int afs_writepage(struct page *subpage, struct writeback_control *wbc) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 645 | { |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 646 | struct folio *folio = page_folio(subpage); |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 647 | ssize_t ret; |
| 648 | loff_t start; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 649 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 650 | _enter("{%lx},", folio_index(folio)); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 651 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 652 | start = folio_index(folio) * PAGE_SIZE; |
| 653 | ret = afs_write_back_from_locked_folio(folio_mapping(folio), wbc, |
| 654 | folio, start, LLONG_MAX - start); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 655 | if (ret < 0) { |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 656 | _leave(" = %zd", ret); |
| 657 | return ret; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 658 | } |
| 659 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 660 | _leave(" = 0"); |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | /* |
| 665 | * write a region of pages back to the server |
| 666 | */ |
Adrian Bunk | c1206a2 | 2007-10-16 23:26:41 -0700 | [diff] [blame] | 667 | static int afs_writepages_region(struct address_space *mapping, |
| 668 | struct writeback_control *wbc, |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 669 | loff_t start, loff_t end, loff_t *_next) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 670 | { |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 671 | struct folio *folio; |
| 672 | struct page *head_page; |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 673 | ssize_t ret; |
| 674 | int n; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 675 | |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 676 | _enter("%llx,%llx,", start, end); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 677 | |
| 678 | do { |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 679 | pgoff_t index = start / PAGE_SIZE; |
| 680 | |
| 681 | n = find_get_pages_range_tag(mapping, &index, end / PAGE_SIZE, |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 682 | PAGECACHE_TAG_DIRTY, 1, &head_page); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 683 | if (!n) |
| 684 | break; |
| 685 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 686 | folio = page_folio(head_page); |
| 687 | start = folio_pos(folio); /* May regress with THPs */ |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 688 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 689 | _debug("wback %lx", folio_index(folio)); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 690 | |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 691 | /* At this point we hold neither the i_pages lock nor the |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 692 | * page lock: the page may be truncated or invalidated |
| 693 | * (changing page->mapping to NULL), or even swizzled |
| 694 | * back from swapper_space to tmpfs file mapping |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 695 | */ |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 696 | if (wbc->sync_mode != WB_SYNC_NONE) { |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 697 | ret = folio_lock_killable(folio); |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 698 | if (ret < 0) { |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 699 | folio_put(folio); |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 700 | return ret; |
| 701 | } |
| 702 | } else { |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 703 | if (!folio_trylock(folio)) { |
| 704 | folio_put(folio); |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 705 | return 0; |
| 706 | } |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 707 | } |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 708 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 709 | if (folio_mapping(folio) != mapping || |
| 710 | !folio_test_dirty(folio)) { |
| 711 | start += folio_size(folio); |
| 712 | folio_unlock(folio); |
| 713 | folio_put(folio); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 714 | continue; |
| 715 | } |
| 716 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 717 | if (folio_test_writeback(folio)) { |
| 718 | folio_unlock(folio); |
David Howells | c5051c7 | 2017-03-16 16:27:49 +0000 | [diff] [blame] | 719 | if (wbc->sync_mode != WB_SYNC_NONE) |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 720 | folio_wait_writeback(folio); |
| 721 | folio_put(folio); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 722 | continue; |
| 723 | } |
| 724 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 725 | if (!folio_clear_dirty_for_io(folio)) |
David Howells | 65a1510 | 2017-03-16 16:27:49 +0000 | [diff] [blame] | 726 | BUG(); |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 727 | ret = afs_write_back_from_locked_folio(mapping, wbc, folio, start, end); |
| 728 | folio_put(folio); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 729 | if (ret < 0) { |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 730 | _leave(" = %zd", ret); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 731 | return ret; |
| 732 | } |
| 733 | |
Marc Dionne | dc25573 | 2021-06-06 21:21:27 +0100 | [diff] [blame] | 734 | start += ret; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 735 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 736 | cond_resched(); |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 737 | } while (wbc->nr_to_write > 0); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 738 | |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 739 | *_next = start; |
| 740 | _leave(" = 0 [%llx]", *_next); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | /* |
| 745 | * write some of the pending data back to the server |
| 746 | */ |
| 747 | int afs_writepages(struct address_space *mapping, |
| 748 | struct writeback_control *wbc) |
| 749 | { |
David Howells | ec0fa0b | 2020-10-07 14:22:12 +0100 | [diff] [blame] | 750 | struct afs_vnode *vnode = AFS_FS_I(mapping->host); |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 751 | loff_t start, next; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 752 | int ret; |
| 753 | |
| 754 | _enter(""); |
| 755 | |
David Howells | ec0fa0b | 2020-10-07 14:22:12 +0100 | [diff] [blame] | 756 | /* We have to be careful as we can end up racing with setattr() |
| 757 | * truncating the pagecache since the caller doesn't take a lock here |
| 758 | * to prevent it. |
| 759 | */ |
| 760 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 761 | down_read(&vnode->validate_lock); |
| 762 | else if (!down_read_trylock(&vnode->validate_lock)) |
| 763 | return 0; |
| 764 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 765 | if (wbc->range_cyclic) { |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 766 | start = mapping->writeback_index * PAGE_SIZE; |
| 767 | ret = afs_writepages_region(mapping, wbc, start, LLONG_MAX, &next); |
Tom Rix | afe6949 | 2021-04-30 08:50:31 -0700 | [diff] [blame] | 768 | if (ret == 0) { |
| 769 | mapping->writeback_index = next / PAGE_SIZE; |
| 770 | if (start > 0 && wbc->nr_to_write > 0) { |
| 771 | ret = afs_writepages_region(mapping, wbc, 0, |
| 772 | start, &next); |
| 773 | if (ret == 0) |
| 774 | mapping->writeback_index = |
| 775 | next / PAGE_SIZE; |
| 776 | } |
| 777 | } |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 778 | } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) { |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 779 | ret = afs_writepages_region(mapping, wbc, 0, LLONG_MAX, &next); |
Tom Rix | afe6949 | 2021-04-30 08:50:31 -0700 | [diff] [blame] | 780 | if (wbc->nr_to_write > 0 && ret == 0) |
David Howells | 5a97247 | 2021-07-12 17:04:47 +0100 | [diff] [blame] | 781 | mapping->writeback_index = next / PAGE_SIZE; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 782 | } else { |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 783 | ret = afs_writepages_region(mapping, wbc, |
| 784 | wbc->range_start, wbc->range_end, &next); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 785 | } |
| 786 | |
David Howells | ec0fa0b | 2020-10-07 14:22:12 +0100 | [diff] [blame] | 787 | up_read(&vnode->validate_lock); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 788 | _leave(" = %d", ret); |
| 789 | return ret; |
| 790 | } |
| 791 | |
| 792 | /* |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 793 | * write to an AFS file |
| 794 | */ |
Al Viro | 50b5551 | 2014-04-03 14:13:46 -0400 | [diff] [blame] | 795 | ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 796 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 797 | struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp)); |
David Howells | 3978d81 | 2021-09-01 19:22:50 +0100 | [diff] [blame] | 798 | struct afs_file *af = iocb->ki_filp->private_data; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 799 | ssize_t result; |
Al Viro | 50b5551 | 2014-04-03 14:13:46 -0400 | [diff] [blame] | 800 | size_t count = iov_iter_count(from); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 801 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 802 | _enter("{%llx:%llu},{%zu},", |
Al Viro | 50b5551 | 2014-04-03 14:13:46 -0400 | [diff] [blame] | 803 | vnode->fid.vid, vnode->fid.vnode, count); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 804 | |
| 805 | if (IS_SWAPFILE(&vnode->vfs_inode)) { |
| 806 | printk(KERN_INFO |
| 807 | "AFS: Attempt to write to active swap file!\n"); |
| 808 | return -EBUSY; |
| 809 | } |
| 810 | |
| 811 | if (!count) |
| 812 | return 0; |
| 813 | |
David Howells | 3978d81 | 2021-09-01 19:22:50 +0100 | [diff] [blame] | 814 | result = afs_validate(vnode, af->key); |
| 815 | if (result < 0) |
| 816 | return result; |
| 817 | |
Al Viro | 50b5551 | 2014-04-03 14:13:46 -0400 | [diff] [blame] | 818 | result = generic_file_write_iter(iocb, from); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 819 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 820 | _leave(" = %zd", result); |
| 821 | return result; |
| 822 | } |
| 823 | |
| 824 | /* |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 825 | * flush any dirty pages for this process, and check for write errors. |
| 826 | * - the return status from this call provides a reliable indication of |
| 827 | * whether any write errors occurred for this process. |
| 828 | */ |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 829 | int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 830 | { |
David Howells | 3978d81 | 2021-09-01 19:22:50 +0100 | [diff] [blame] | 831 | struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); |
| 832 | struct afs_file *af = file->private_data; |
| 833 | int ret; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 834 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 835 | _enter("{%llx:%llu},{n=%pD},%d", |
Al Viro | 3c981bf | 2013-09-03 13:37:45 -0400 | [diff] [blame] | 836 | vnode->fid.vid, vnode->fid.vnode, file, |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 837 | datasync); |
| 838 | |
David Howells | 3978d81 | 2021-09-01 19:22:50 +0100 | [diff] [blame] | 839 | ret = afs_validate(vnode, af->key); |
| 840 | if (ret < 0) |
| 841 | return ret; |
| 842 | |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 843 | return file_write_and_wait_range(file, start, end); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 844 | } |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 845 | |
| 846 | /* |
| 847 | * notification that a previously read-only page is about to become writable |
| 848 | * - if it returns an error, the caller will deliver a bus error signal |
| 849 | */ |
Souptick Joarder | 0722f18 | 2018-08-23 17:00:48 -0700 | [diff] [blame] | 850 | vm_fault_t afs_page_mkwrite(struct vm_fault *vmf) |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 851 | { |
Matthew Wilcox (Oracle) | 490e016 | 2021-03-04 11:09:17 -0500 | [diff] [blame] | 852 | struct folio *folio = page_folio(vmf->page); |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 853 | struct file *file = vmf->vma->vm_file; |
| 854 | struct inode *inode = file_inode(file); |
| 855 | struct afs_vnode *vnode = AFS_FS_I(inode); |
David Howells | 3978d81 | 2021-09-01 19:22:50 +0100 | [diff] [blame] | 856 | struct afs_file *af = file->private_data; |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 857 | unsigned long priv; |
Matthew Wilcox (Oracle) | 9620ad8 | 2021-06-16 22:22:28 +0100 | [diff] [blame] | 858 | vm_fault_t ret = VM_FAULT_RETRY; |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 859 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 860 | _enter("{{%llx:%llu}},{%lx}", vnode->fid.vid, vnode->fid.vnode, folio_index(folio)); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 861 | |
David Howells | 3978d81 | 2021-09-01 19:22:50 +0100 | [diff] [blame] | 862 | afs_validate(vnode, af->key); |
| 863 | |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 864 | sb_start_pagefault(inode->i_sb); |
| 865 | |
| 866 | /* Wait for the page to be written to the cache before we allow it to |
| 867 | * be modified. We then assume the entire page will need writing back. |
| 868 | */ |
David Howells | 630f5dd | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 869 | #ifdef CONFIG_AFS_FSCACHE |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 870 | if (folio_test_fscache(folio) && |
| 871 | folio_wait_fscache_killable(folio) < 0) |
Matthew Wilcox (Oracle) | 9620ad8 | 2021-06-16 22:22:28 +0100 | [diff] [blame] | 872 | goto out; |
David Howells | 630f5dd | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 873 | #endif |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 874 | |
Matthew Wilcox (Oracle) | 490e016 | 2021-03-04 11:09:17 -0500 | [diff] [blame] | 875 | if (folio_wait_writeback_killable(folio)) |
Matthew Wilcox (Oracle) | 9620ad8 | 2021-06-16 22:22:28 +0100 | [diff] [blame] | 876 | goto out; |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 877 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 878 | if (folio_lock_killable(folio) < 0) |
Matthew Wilcox (Oracle) | 9620ad8 | 2021-06-16 22:22:28 +0100 | [diff] [blame] | 879 | goto out; |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 880 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 881 | /* We mustn't change folio->private until writeback is complete as that |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 882 | * details the portion of the page we need to write back and we might |
| 883 | * need to redirty the page if there's a problem. |
| 884 | */ |
Matthew Wilcox (Oracle) | 490e016 | 2021-03-04 11:09:17 -0500 | [diff] [blame] | 885 | if (folio_wait_writeback_killable(folio) < 0) { |
| 886 | folio_unlock(folio); |
Matthew Wilcox (Oracle) | 9620ad8 | 2021-06-16 22:22:28 +0100 | [diff] [blame] | 887 | goto out; |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 888 | } |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 889 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 890 | priv = afs_folio_dirty(folio, 0, folio_size(folio)); |
| 891 | priv = afs_folio_dirty_mmapped(priv); |
| 892 | if (folio_test_private(folio)) { |
| 893 | folio_change_private(folio, (void *)priv); |
| 894 | trace_afs_folio_dirty(vnode, tracepoint_string("mkwrite+"), folio); |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 895 | } else { |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 896 | folio_attach_private(folio, (void *)priv); |
| 897 | trace_afs_folio_dirty(vnode, tracepoint_string("mkwrite"), folio); |
David Howells | e87b03f | 2020-10-20 09:33:45 +0100 | [diff] [blame] | 898 | } |
David Howells | bb41348 | 2020-06-12 00:15:13 +0100 | [diff] [blame] | 899 | file_update_time(file); |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 900 | |
Matthew Wilcox (Oracle) | 9620ad8 | 2021-06-16 22:22:28 +0100 | [diff] [blame] | 901 | ret = VM_FAULT_LOCKED; |
| 902 | out: |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 903 | sb_end_pagefault(inode->i_sb); |
Matthew Wilcox (Oracle) | 9620ad8 | 2021-06-16 22:22:28 +0100 | [diff] [blame] | 904 | return ret; |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 905 | } |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 906 | |
| 907 | /* |
| 908 | * Prune the keys cached for writeback. The caller must hold vnode->wb_lock. |
| 909 | */ |
| 910 | void afs_prune_wb_keys(struct afs_vnode *vnode) |
| 911 | { |
| 912 | LIST_HEAD(graveyard); |
| 913 | struct afs_wb_key *wbk, *tmp; |
| 914 | |
| 915 | /* Discard unused keys */ |
| 916 | spin_lock(&vnode->wb_lock); |
| 917 | |
| 918 | if (!mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_WRITEBACK) && |
| 919 | !mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_DIRTY)) { |
| 920 | list_for_each_entry_safe(wbk, tmp, &vnode->wb_keys, vnode_link) { |
| 921 | if (refcount_read(&wbk->usage) == 1) |
| 922 | list_move(&wbk->vnode_link, &graveyard); |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | spin_unlock(&vnode->wb_lock); |
| 927 | |
| 928 | while (!list_empty(&graveyard)) { |
| 929 | wbk = list_entry(graveyard.next, struct afs_wb_key, vnode_link); |
| 930 | list_del(&wbk->vnode_link); |
| 931 | afs_put_wb_key(wbk); |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | /* |
| 936 | * Clean up a page during invalidation. |
| 937 | */ |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 938 | int afs_launder_page(struct page *subpage) |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 939 | { |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 940 | struct folio *folio = page_folio(subpage); |
| 941 | struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio)); |
David Howells | bd80d8a | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 942 | struct iov_iter iter; |
| 943 | struct bio_vec bv[1]; |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 944 | unsigned long priv; |
| 945 | unsigned int f, t; |
| 946 | int ret = 0; |
| 947 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 948 | _enter("{%lx}", folio_index(folio)); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 949 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 950 | priv = (unsigned long)folio_get_private(folio); |
| 951 | if (folio_clear_dirty_for_io(folio)) { |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 952 | f = 0; |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 953 | t = folio_size(folio); |
| 954 | if (folio_test_private(folio)) { |
| 955 | f = afs_folio_dirty_from(folio, priv); |
| 956 | t = afs_folio_dirty_to(folio, priv); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 957 | } |
| 958 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 959 | bv[0].bv_page = &folio->page; |
David Howells | bd80d8a | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 960 | bv[0].bv_offset = f; |
| 961 | bv[0].bv_len = t - f; |
| 962 | iov_iter_bvec(&iter, WRITE, bv, 1, bv[0].bv_len); |
| 963 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 964 | trace_afs_folio_dirty(vnode, tracepoint_string("launder"), folio); |
| 965 | ret = afs_store_data(vnode, &iter, folio_pos(folio) + f, true); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 966 | } |
| 967 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 968 | trace_afs_folio_dirty(vnode, tracepoint_string("laundered"), folio); |
| 969 | folio_detach_private(folio); |
| 970 | folio_wait_fscache(folio); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 971 | return ret; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 972 | } |