David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 1 | /* handling of writes to regular files and writing back to the server |
| 2 | * |
| 3 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 11 | |
Alexey Dobriyan | 4af3c9c | 2007-10-16 23:29:23 -0700 | [diff] [blame] | 12 | #include <linux/backing-dev.h> |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 13 | #include <linux/slab.h> |
| 14 | #include <linux/fs.h> |
| 15 | #include <linux/pagemap.h> |
| 16 | #include <linux/writeback.h> |
| 17 | #include <linux/pagevec.h> |
| 18 | #include "internal.h" |
| 19 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 20 | /* |
| 21 | * mark a page as having been made dirty and thus needing writeback |
| 22 | */ |
| 23 | int afs_set_page_dirty(struct page *page) |
| 24 | { |
| 25 | _enter(""); |
| 26 | return __set_page_dirty_nobuffers(page); |
| 27 | } |
| 28 | |
| 29 | /* |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 30 | * partly or wholly fill a page that's under preparation for writing |
| 31 | */ |
| 32 | static int afs_fill_page(struct afs_vnode *vnode, struct key *key, |
David Howells | e8e581a | 2017-03-16 16:27:44 +0000 | [diff] [blame] | 33 | loff_t pos, unsigned int len, struct page *page) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 34 | { |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 35 | struct afs_read *req; |
David Howells | 2a0b4f6 | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 36 | size_t p; |
| 37 | void *data; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 38 | int ret; |
| 39 | |
Anton Blanchard | 5e7f233 | 2011-06-13 22:31:12 +0100 | [diff] [blame] | 40 | _enter(",,%llu", (unsigned long long)pos); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 41 | |
David Howells | 2a0b4f6 | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 42 | if (pos >= vnode->vfs_inode.i_size) { |
| 43 | p = pos & ~PAGE_MASK; |
| 44 | ASSERTCMP(p + len, <=, PAGE_SIZE); |
| 45 | data = kmap(page); |
| 46 | memset(data + p, 0, len); |
| 47 | kunmap(page); |
| 48 | return 0; |
| 49 | } |
| 50 | |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 51 | req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *), |
| 52 | GFP_KERNEL); |
| 53 | if (!req) |
| 54 | return -ENOMEM; |
| 55 | |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 56 | refcount_set(&req->usage, 1); |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 57 | req->pos = pos; |
David Howells | e8e581a | 2017-03-16 16:27:44 +0000 | [diff] [blame] | 58 | req->len = len; |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 59 | req->nr_pages = 1; |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 60 | req->pages = req->array; |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 61 | req->pages[0] = page; |
David Howells | 5611ef2 | 2017-03-16 16:27:43 +0000 | [diff] [blame] | 62 | get_page(page); |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 63 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 64 | ret = afs_fetch_data(vnode, key, req); |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 65 | afs_put_read(req); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 66 | if (ret < 0) { |
| 67 | if (ret == -ENOENT) { |
| 68 | _debug("got NOENT from server" |
| 69 | " - marking file deleted and stale"); |
| 70 | set_bit(AFS_VNODE_DELETED, &vnode->flags); |
| 71 | ret = -ESTALE; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | _leave(" = %d", ret); |
| 76 | return ret; |
| 77 | } |
| 78 | |
| 79 | /* |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 80 | * prepare to perform part of a write to a page |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 81 | */ |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 82 | int afs_write_begin(struct file *file, struct address_space *mapping, |
| 83 | loff_t pos, unsigned len, unsigned flags, |
| 84 | struct page **pagep, void **fsdata) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 85 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 86 | struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 87 | struct page *page; |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 88 | struct key *key = afs_file_key(file); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 89 | unsigned long priv; |
| 90 | unsigned f, from = pos & (PAGE_SIZE - 1); |
| 91 | unsigned t, to = from + len; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 92 | pgoff_t index = pos >> PAGE_SHIFT; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 93 | int ret; |
| 94 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 95 | _enter("{%llx:%llu},{%lx},%u,%u", |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 96 | vnode->fid.vid, vnode->fid.vnode, index, from, to); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 97 | |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 98 | /* We want to store information about how much of a page is altered in |
| 99 | * page->private. |
| 100 | */ |
| 101 | BUILD_BUG_ON(PAGE_SIZE > 32768 && sizeof(page->private) < 8); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 102 | |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 103 | page = grab_cache_page_write_begin(mapping, index, flags); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 104 | if (!page) |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 105 | return -ENOMEM; |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 106 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 107 | if (!PageUptodate(page) && len != PAGE_SIZE) { |
David Howells | e8e581a | 2017-03-16 16:27:44 +0000 | [diff] [blame] | 108 | ret = afs_fill_page(vnode, key, pos & PAGE_MASK, PAGE_SIZE, page); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 109 | if (ret < 0) { |
David Howells | 6d06b0d | 2017-03-16 16:27:48 +0000 | [diff] [blame] | 110 | unlock_page(page); |
| 111 | put_page(page); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 112 | _leave(" = %d [prep]", ret); |
| 113 | return ret; |
| 114 | } |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 115 | SetPageUptodate(page); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 116 | } |
| 117 | |
David Howells | 6d06b0d | 2017-03-16 16:27:48 +0000 | [diff] [blame] | 118 | /* page won't leak in error case: it eventually gets cleaned off LRU */ |
| 119 | *pagep = page; |
| 120 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 121 | try_again: |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 122 | /* See if this page is already partially written in a way that we can |
| 123 | * merge the new write with. |
| 124 | */ |
| 125 | t = f = 0; |
| 126 | if (PagePrivate(page)) { |
| 127 | priv = page_private(page); |
| 128 | f = priv & AFS_PRIV_MAX; |
| 129 | t = priv >> AFS_PRIV_SHIFT; |
| 130 | ASSERTCMP(f, <=, t); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 131 | } |
| 132 | |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 133 | if (f != t) { |
David Howells | 5a039c3 | 2017-11-18 00:13:30 +0000 | [diff] [blame] | 134 | if (PageWriteback(page)) { |
| 135 | trace_afs_page_dirty(vnode, tracepoint_string("alrdy"), |
| 136 | page->index, priv); |
| 137 | goto flush_conflicting_write; |
| 138 | } |
David Howells | 5a81327 | 2018-04-06 14:17:26 +0100 | [diff] [blame] | 139 | /* If the file is being filled locally, allow inter-write |
| 140 | * spaces to be merged into writes. If it's not, only write |
| 141 | * back what the user gives us. |
| 142 | */ |
| 143 | if (!test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags) && |
| 144 | (to < f || from > t)) |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 145 | goto flush_conflicting_write; |
| 146 | if (from < f) |
| 147 | f = from; |
| 148 | if (to > t) |
| 149 | t = to; |
| 150 | } else { |
| 151 | f = from; |
| 152 | t = to; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 153 | } |
| 154 | |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 155 | priv = (unsigned long)t << AFS_PRIV_SHIFT; |
| 156 | priv |= f; |
David Howells | 13524ab | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 157 | trace_afs_page_dirty(vnode, tracepoint_string("begin"), |
| 158 | page->index, priv); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 159 | SetPagePrivate(page); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 160 | set_page_private(page, priv); |
| 161 | _leave(" = 0"); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 162 | return 0; |
| 163 | |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 164 | /* The previous write and this write aren't adjacent or overlapping, so |
| 165 | * flush the page out. |
| 166 | */ |
| 167 | flush_conflicting_write: |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 168 | _debug("flush conflict"); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 169 | ret = write_one_page(page); |
| 170 | if (ret < 0) { |
| 171 | _leave(" = %d", ret); |
| 172 | return ret; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 173 | } |
| 174 | |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 175 | ret = lock_page_killable(page); |
| 176 | if (ret < 0) { |
| 177 | _leave(" = %d", ret); |
| 178 | return ret; |
| 179 | } |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 180 | goto try_again; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * finalise part of a write to a page |
| 185 | */ |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 186 | int afs_write_end(struct file *file, struct address_space *mapping, |
| 187 | loff_t pos, unsigned len, unsigned copied, |
| 188 | struct page *page, void *fsdata) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 189 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 190 | struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 191 | struct key *key = afs_file_key(file); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 192 | loff_t i_size, maybe_i_size; |
David Howells | e8e581a | 2017-03-16 16:27:44 +0000 | [diff] [blame] | 193 | int ret; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 194 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 195 | _enter("{%llx:%llu},{%lx}", |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 196 | vnode->fid.vid, vnode->fid.vnode, page->index); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 197 | |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 198 | maybe_i_size = pos + copied; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 199 | |
| 200 | i_size = i_size_read(&vnode->vfs_inode); |
| 201 | if (maybe_i_size > i_size) { |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 202 | spin_lock(&vnode->wb_lock); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 203 | i_size = i_size_read(&vnode->vfs_inode); |
| 204 | if (maybe_i_size > i_size) |
| 205 | i_size_write(&vnode->vfs_inode, maybe_i_size); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 206 | spin_unlock(&vnode->wb_lock); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 207 | } |
| 208 | |
David Howells | e8e581a | 2017-03-16 16:27:44 +0000 | [diff] [blame] | 209 | if (!PageUptodate(page)) { |
| 210 | if (copied < len) { |
| 211 | /* Try and load any missing data from the server. The |
| 212 | * unmarshalling routine will take care of clearing any |
| 213 | * bits that are beyond the EOF. |
| 214 | */ |
| 215 | ret = afs_fill_page(vnode, key, pos + copied, |
| 216 | len - copied, page); |
| 217 | if (ret < 0) |
David Howells | afae457 | 2018-01-02 10:02:19 +0000 | [diff] [blame] | 218 | goto out; |
David Howells | e8e581a | 2017-03-16 16:27:44 +0000 | [diff] [blame] | 219 | } |
| 220 | SetPageUptodate(page); |
| 221 | } |
| 222 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 223 | set_page_dirty(page); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 224 | if (PageDirty(page)) |
| 225 | _debug("dirtied"); |
David Howells | afae457 | 2018-01-02 10:02:19 +0000 | [diff] [blame] | 226 | ret = copied; |
| 227 | |
| 228 | out: |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 229 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 230 | put_page(page); |
David Howells | afae457 | 2018-01-02 10:02:19 +0000 | [diff] [blame] | 231 | return ret; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* |
| 235 | * kill all the pages in the given range |
| 236 | */ |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 237 | static void afs_kill_pages(struct address_space *mapping, |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 238 | pgoff_t first, pgoff_t last) |
| 239 | { |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 240 | struct afs_vnode *vnode = AFS_FS_I(mapping->host); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 241 | struct pagevec pv; |
| 242 | unsigned count, loop; |
| 243 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 244 | _enter("{%llx:%llu},%lx-%lx", |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 245 | vnode->fid.vid, vnode->fid.vnode, first, last); |
| 246 | |
Mel Gorman | 8667982 | 2017-11-15 17:37:52 -0800 | [diff] [blame] | 247 | pagevec_init(&pv); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 248 | |
| 249 | do { |
| 250 | _debug("kill %lx-%lx", first, last); |
| 251 | |
| 252 | count = last - first + 1; |
| 253 | if (count > PAGEVEC_SIZE) |
| 254 | count = PAGEVEC_SIZE; |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 255 | pv.nr = find_get_pages_contig(mapping, first, count, pv.pages); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 256 | ASSERTCMP(pv.nr, ==, count); |
| 257 | |
| 258 | for (loop = 0; loop < count; loop++) { |
David Howells | 7286a35 | 2017-03-16 16:27:48 +0000 | [diff] [blame] | 259 | struct page *page = pv.pages[loop]; |
| 260 | ClearPageUptodate(page); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 261 | SetPageError(page); |
| 262 | end_page_writeback(page); |
David Howells | 7286a35 | 2017-03-16 16:27:48 +0000 | [diff] [blame] | 263 | if (page->index >= first) |
| 264 | first = page->index + 1; |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 265 | lock_page(page); |
| 266 | generic_error_remove_page(mapping, page); |
Marc Dionne | 21bd68f | 2019-04-13 08:37:37 +0100 | [diff] [blame] | 267 | unlock_page(page); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | __pagevec_release(&pv); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 271 | } while (first <= last); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 272 | |
| 273 | _leave(""); |
| 274 | } |
| 275 | |
| 276 | /* |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 277 | * Redirty all the pages in a given range. |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 278 | */ |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 279 | static void afs_redirty_pages(struct writeback_control *wbc, |
| 280 | struct address_space *mapping, |
| 281 | pgoff_t first, pgoff_t last) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 282 | { |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 283 | struct afs_vnode *vnode = AFS_FS_I(mapping->host); |
| 284 | struct pagevec pv; |
| 285 | unsigned count, loop; |
| 286 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 287 | _enter("{%llx:%llu},%lx-%lx", |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 288 | vnode->fid.vid, vnode->fid.vnode, first, last); |
| 289 | |
Linus Torvalds | 487e2c9 | 2017-11-16 11:41:22 -0800 | [diff] [blame] | 290 | pagevec_init(&pv); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 291 | |
| 292 | do { |
| 293 | _debug("redirty %lx-%lx", first, last); |
| 294 | |
| 295 | count = last - first + 1; |
| 296 | if (count > PAGEVEC_SIZE) |
| 297 | count = PAGEVEC_SIZE; |
| 298 | pv.nr = find_get_pages_contig(mapping, first, count, pv.pages); |
| 299 | ASSERTCMP(pv.nr, ==, count); |
| 300 | |
| 301 | for (loop = 0; loop < count; loop++) { |
| 302 | struct page *page = pv.pages[loop]; |
| 303 | |
| 304 | redirty_page_for_writepage(wbc, page); |
| 305 | end_page_writeback(page); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 306 | if (page->index >= first) |
| 307 | first = page->index + 1; |
| 308 | } |
| 309 | |
| 310 | __pagevec_release(&pv); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 311 | } while (first <= last); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 312 | |
| 313 | _leave(""); |
| 314 | } |
| 315 | |
| 316 | /* |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame^] | 317 | * completion of write to server |
| 318 | */ |
| 319 | static void afs_pages_written_back(struct afs_vnode *vnode, |
| 320 | pgoff_t first, pgoff_t last) |
| 321 | { |
| 322 | struct pagevec pv; |
| 323 | unsigned long priv; |
| 324 | unsigned count, loop; |
| 325 | |
| 326 | _enter("{%llx:%llu},{%lx-%lx}", |
| 327 | vnode->fid.vid, vnode->fid.vnode, first, last); |
| 328 | |
| 329 | pagevec_init(&pv); |
| 330 | |
| 331 | do { |
| 332 | _debug("done %lx-%lx", first, last); |
| 333 | |
| 334 | count = last - first + 1; |
| 335 | if (count > PAGEVEC_SIZE) |
| 336 | count = PAGEVEC_SIZE; |
| 337 | pv.nr = find_get_pages_contig(vnode->vfs_inode.i_mapping, |
| 338 | first, count, pv.pages); |
| 339 | ASSERTCMP(pv.nr, ==, count); |
| 340 | |
| 341 | for (loop = 0; loop < count; loop++) { |
| 342 | priv = page_private(pv.pages[loop]); |
| 343 | trace_afs_page_dirty(vnode, tracepoint_string("clear"), |
| 344 | pv.pages[loop]->index, priv); |
| 345 | set_page_private(pv.pages[loop], 0); |
| 346 | end_page_writeback(pv.pages[loop]); |
| 347 | } |
| 348 | first += count; |
| 349 | __pagevec_release(&pv); |
| 350 | } while (first <= last); |
| 351 | |
| 352 | afs_prune_wb_keys(vnode); |
| 353 | _leave(""); |
| 354 | } |
| 355 | |
| 356 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 357 | * write to a file |
| 358 | */ |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 359 | static int afs_store_data(struct address_space *mapping, |
| 360 | pgoff_t first, pgoff_t last, |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 361 | unsigned offset, unsigned to) |
| 362 | { |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 363 | struct afs_vnode *vnode = AFS_FS_I(mapping->host); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 364 | struct afs_fs_cursor fc; |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame^] | 365 | struct afs_status_cb *scb; |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 366 | struct afs_wb_key *wbk = NULL; |
| 367 | struct list_head *p; |
| 368 | int ret = -ENOKEY, ret2; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 369 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 370 | _enter("%s{%llx:%llu.%u},%lx,%lx,%x,%x", |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 371 | vnode->volume->name, |
| 372 | vnode->fid.vid, |
| 373 | vnode->fid.vnode, |
| 374 | vnode->fid.unique, |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 375 | first, last, offset, to); |
| 376 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame^] | 377 | scb = kzalloc(sizeof(struct afs_status_cb), GFP_NOFS); |
| 378 | if (!scb) |
| 379 | return -ENOMEM; |
| 380 | |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 381 | spin_lock(&vnode->wb_lock); |
| 382 | p = vnode->wb_keys.next; |
| 383 | |
| 384 | /* Iterate through the list looking for a valid key to use. */ |
| 385 | try_next_key: |
| 386 | while (p != &vnode->wb_keys) { |
| 387 | wbk = list_entry(p, struct afs_wb_key, vnode_link); |
| 388 | _debug("wbk %u", key_serial(wbk->key)); |
| 389 | ret2 = key_validate(wbk->key); |
| 390 | if (ret2 == 0) |
| 391 | goto found_key; |
| 392 | if (ret == -ENOKEY) |
| 393 | ret = ret2; |
| 394 | p = p->next; |
| 395 | } |
| 396 | |
| 397 | spin_unlock(&vnode->wb_lock); |
| 398 | afs_put_wb_key(wbk); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame^] | 399 | kfree(scb); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 400 | _leave(" = %d [no keys]", ret); |
| 401 | return ret; |
| 402 | |
| 403 | found_key: |
| 404 | refcount_inc(&wbk->usage); |
| 405 | spin_unlock(&vnode->wb_lock); |
| 406 | |
| 407 | _debug("USE WB KEY %u", key_serial(wbk->key)); |
| 408 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 409 | ret = -ERESTARTSYS; |
David Howells | 20b8391 | 2019-05-08 16:16:31 +0100 | [diff] [blame] | 410 | if (afs_begin_vnode_operation(&fc, vnode, wbk->key, false)) { |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame^] | 411 | afs_dataversion_t data_version = vnode->status.data_version + 1; |
| 412 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 413 | while (afs_select_fileserver(&fc)) { |
David Howells | 68251f0 | 2018-05-12 22:31:33 +0100 | [diff] [blame] | 414 | fc.cb_break = afs_calc_vnode_cb_break(vnode); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame^] | 415 | afs_fs_store_data(&fc, mapping, first, last, offset, to, scb); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 416 | } |
| 417 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame^] | 418 | afs_check_for_remote_deletion(&fc, vnode); |
| 419 | afs_vnode_commit_status(&fc, vnode, fc.cb_break, |
| 420 | &data_version, scb); |
| 421 | if (fc.ac.error == 0) |
| 422 | afs_pages_written_back(vnode, first, last); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 423 | ret = afs_end_vnode_operation(&fc); |
| 424 | } |
| 425 | |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 426 | switch (ret) { |
David Howells | 76a5cb6 | 2018-04-06 14:17:26 +0100 | [diff] [blame] | 427 | case 0: |
| 428 | afs_stat_v(vnode, n_stores); |
| 429 | atomic_long_add((last * PAGE_SIZE + to) - |
| 430 | (first * PAGE_SIZE + offset), |
| 431 | &afs_v2net(vnode)->n_store_bytes); |
| 432 | break; |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 433 | case -EACCES: |
| 434 | case -EPERM: |
| 435 | case -ENOKEY: |
| 436 | case -EKEYEXPIRED: |
| 437 | case -EKEYREJECTED: |
| 438 | case -EKEYREVOKED: |
| 439 | _debug("next"); |
| 440 | spin_lock(&vnode->wb_lock); |
| 441 | p = wbk->vnode_link.next; |
| 442 | afs_put_wb_key(wbk); |
| 443 | goto try_next_key; |
| 444 | } |
| 445 | |
| 446 | afs_put_wb_key(wbk); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame^] | 447 | kfree(scb); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 448 | _leave(" = %d", ret); |
| 449 | return ret; |
| 450 | } |
| 451 | |
| 452 | /* |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 453 | * Synchronously write back the locked page and any subsequent non-locked dirty |
| 454 | * pages. |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 455 | */ |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 456 | static int afs_write_back_from_locked_page(struct address_space *mapping, |
| 457 | struct writeback_control *wbc, |
| 458 | struct page *primary_page, |
| 459 | pgoff_t final_page) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 460 | { |
David Howells | 13524ab | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 461 | struct afs_vnode *vnode = AFS_FS_I(mapping->host); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 462 | struct page *pages[8], *page; |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 463 | unsigned long count, priv; |
| 464 | unsigned n, offset, to, f, t; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 465 | pgoff_t start, first, last; |
| 466 | int loop, ret; |
| 467 | |
| 468 | _enter(",%lx", primary_page->index); |
| 469 | |
| 470 | count = 1; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 471 | if (test_set_page_writeback(primary_page)) |
| 472 | BUG(); |
| 473 | |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 474 | /* Find all consecutive lockable dirty pages that have contiguous |
| 475 | * written regions, stopping when we find a page that is not |
| 476 | * immediately lockable, is not dirty or is missing, or we reach the |
| 477 | * end of the range. |
| 478 | */ |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 479 | start = primary_page->index; |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 480 | priv = page_private(primary_page); |
| 481 | offset = priv & AFS_PRIV_MAX; |
| 482 | to = priv >> AFS_PRIV_SHIFT; |
David Howells | 13524ab | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 483 | trace_afs_page_dirty(vnode, tracepoint_string("store"), |
| 484 | primary_page->index, priv); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 485 | |
| 486 | WARN_ON(offset == to); |
David Howells | 13524ab | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 487 | if (offset == to) |
| 488 | trace_afs_page_dirty(vnode, tracepoint_string("WARN"), |
| 489 | primary_page->index, priv); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 490 | |
David Howells | 5a81327 | 2018-04-06 14:17:26 +0100 | [diff] [blame] | 491 | if (start >= final_page || |
| 492 | (to < PAGE_SIZE && !test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags))) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 493 | goto no_more; |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 494 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 495 | start++; |
| 496 | do { |
| 497 | _debug("more %lx [%lx]", start, count); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 498 | n = final_page - start + 1; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 499 | if (n > ARRAY_SIZE(pages)) |
| 500 | n = ARRAY_SIZE(pages); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 501 | n = find_get_pages_contig(mapping, start, ARRAY_SIZE(pages), pages); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 502 | _debug("fgpc %u", n); |
| 503 | if (n == 0) |
| 504 | goto no_more; |
| 505 | if (pages[0]->index != start) { |
David Howells | 9d577b6 | 2007-05-10 22:22:19 -0700 | [diff] [blame] | 506 | do { |
| 507 | put_page(pages[--n]); |
| 508 | } while (n > 0); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 509 | goto no_more; |
| 510 | } |
| 511 | |
| 512 | for (loop = 0; loop < n; loop++) { |
| 513 | page = pages[loop]; |
David Howells | 5a81327 | 2018-04-06 14:17:26 +0100 | [diff] [blame] | 514 | if (to != PAGE_SIZE && |
| 515 | !test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags)) |
| 516 | break; |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 517 | if (page->index > final_page) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 518 | break; |
Nick Piggin | 529ae9a | 2008-08-02 12:01:03 +0200 | [diff] [blame] | 519 | if (!trylock_page(page)) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 520 | break; |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 521 | if (!PageDirty(page) || PageWriteback(page)) { |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 522 | unlock_page(page); |
| 523 | break; |
| 524 | } |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 525 | |
| 526 | priv = page_private(page); |
| 527 | f = priv & AFS_PRIV_MAX; |
| 528 | t = priv >> AFS_PRIV_SHIFT; |
David Howells | 5a81327 | 2018-04-06 14:17:26 +0100 | [diff] [blame] | 529 | if (f != 0 && |
| 530 | !test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags)) { |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 531 | unlock_page(page); |
| 532 | break; |
| 533 | } |
| 534 | to = t; |
| 535 | |
David Howells | 13524ab | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 536 | trace_afs_page_dirty(vnode, tracepoint_string("store+"), |
| 537 | page->index, priv); |
| 538 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 539 | if (!clear_page_dirty_for_io(page)) |
| 540 | BUG(); |
| 541 | if (test_set_page_writeback(page)) |
| 542 | BUG(); |
| 543 | unlock_page(page); |
| 544 | put_page(page); |
| 545 | } |
| 546 | count += loop; |
| 547 | if (loop < n) { |
| 548 | for (; loop < n; loop++) |
| 549 | put_page(pages[loop]); |
| 550 | goto no_more; |
| 551 | } |
| 552 | |
| 553 | start += loop; |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 554 | } while (start <= final_page && count < 65536); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 555 | |
| 556 | no_more: |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 557 | /* We now have a contiguous set of dirty pages, each with writeback |
| 558 | * set; the first page is still locked at this point, but all the rest |
| 559 | * have been unlocked. |
| 560 | */ |
| 561 | unlock_page(primary_page); |
| 562 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 563 | first = primary_page->index; |
| 564 | last = first + count - 1; |
| 565 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 566 | _debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to); |
| 567 | |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 568 | ret = afs_store_data(mapping, first, last, offset, to); |
| 569 | switch (ret) { |
| 570 | case 0: |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 571 | ret = count; |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 572 | break; |
| 573 | |
| 574 | default: |
| 575 | pr_notice("kAFS: Unexpected error from FS.StoreData %d\n", ret); |
| 576 | /* Fall through */ |
| 577 | case -EACCES: |
| 578 | case -EPERM: |
| 579 | case -ENOKEY: |
| 580 | case -EKEYEXPIRED: |
| 581 | case -EKEYREJECTED: |
| 582 | case -EKEYREVOKED: |
| 583 | afs_redirty_pages(wbc, mapping, first, last); |
| 584 | mapping_set_error(mapping, ret); |
| 585 | break; |
| 586 | |
| 587 | case -EDQUOT: |
| 588 | case -ENOSPC: |
| 589 | afs_redirty_pages(wbc, mapping, first, last); |
| 590 | mapping_set_error(mapping, -ENOSPC); |
| 591 | break; |
| 592 | |
| 593 | case -EROFS: |
| 594 | case -EIO: |
| 595 | case -EREMOTEIO: |
| 596 | case -EFBIG: |
| 597 | case -ENOENT: |
| 598 | case -ENOMEDIUM: |
| 599 | case -ENXIO: |
David Howells | f51375c | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 600 | trace_afs_file_error(vnode, ret, afs_file_error_writeback_fail); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 601 | afs_kill_pages(mapping, first, last); |
| 602 | mapping_set_error(mapping, ret); |
| 603 | break; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | _leave(" = %d", ret); |
| 607 | return ret; |
| 608 | } |
| 609 | |
| 610 | /* |
| 611 | * write a page back to the server |
| 612 | * - the caller locked the page for us |
| 613 | */ |
| 614 | int afs_writepage(struct page *page, struct writeback_control *wbc) |
| 615 | { |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 616 | int ret; |
| 617 | |
| 618 | _enter("{%lx},", page->index); |
| 619 | |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 620 | ret = afs_write_back_from_locked_page(page->mapping, wbc, page, |
| 621 | wbc->range_end >> PAGE_SHIFT); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 622 | if (ret < 0) { |
| 623 | _leave(" = %d", ret); |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | wbc->nr_to_write -= ret; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 628 | |
| 629 | _leave(" = 0"); |
| 630 | return 0; |
| 631 | } |
| 632 | |
| 633 | /* |
| 634 | * write a region of pages back to the server |
| 635 | */ |
Adrian Bunk | c1206a2 | 2007-10-16 23:26:41 -0700 | [diff] [blame] | 636 | static int afs_writepages_region(struct address_space *mapping, |
| 637 | struct writeback_control *wbc, |
| 638 | pgoff_t index, pgoff_t end, pgoff_t *_next) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 639 | { |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 640 | struct page *page; |
| 641 | int ret, n; |
| 642 | |
| 643 | _enter(",,%lx,%lx,", index, end); |
| 644 | |
| 645 | do { |
Jan Kara | aef6e41 | 2017-11-15 17:35:23 -0800 | [diff] [blame] | 646 | n = find_get_pages_range_tag(mapping, &index, end, |
| 647 | PAGECACHE_TAG_DIRTY, 1, &page); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 648 | if (!n) |
| 649 | break; |
| 650 | |
| 651 | _debug("wback %lx", page->index); |
| 652 | |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 653 | /* |
| 654 | * at this point we hold neither the i_pages lock nor the |
| 655 | * page lock: the page may be truncated or invalidated |
| 656 | * (changing page->mapping to NULL), or even swizzled |
| 657 | * back from swapper_space to tmpfs file mapping |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 658 | */ |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 659 | ret = lock_page_killable(page); |
| 660 | if (ret < 0) { |
| 661 | put_page(page); |
| 662 | _leave(" = %d", ret); |
| 663 | return ret; |
| 664 | } |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 665 | |
David Howells | c5051c7 | 2017-03-16 16:27:49 +0000 | [diff] [blame] | 666 | if (page->mapping != mapping || !PageDirty(page)) { |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 667 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 668 | put_page(page); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 669 | continue; |
| 670 | } |
| 671 | |
David Howells | c5051c7 | 2017-03-16 16:27:49 +0000 | [diff] [blame] | 672 | if (PageWriteback(page)) { |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 673 | unlock_page(page); |
David Howells | c5051c7 | 2017-03-16 16:27:49 +0000 | [diff] [blame] | 674 | if (wbc->sync_mode != WB_SYNC_NONE) |
| 675 | wait_on_page_writeback(page); |
David Howells | 29c8bbb | 2017-03-16 16:27:43 +0000 | [diff] [blame] | 676 | put_page(page); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 677 | continue; |
| 678 | } |
| 679 | |
David Howells | 65a1510 | 2017-03-16 16:27:49 +0000 | [diff] [blame] | 680 | if (!clear_page_dirty_for_io(page)) |
| 681 | BUG(); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 682 | ret = afs_write_back_from_locked_page(mapping, wbc, page, end); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 683 | put_page(page); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 684 | if (ret < 0) { |
| 685 | _leave(" = %d", ret); |
| 686 | return ret; |
| 687 | } |
| 688 | |
| 689 | wbc->nr_to_write -= ret; |
| 690 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 691 | cond_resched(); |
| 692 | } while (index < end && wbc->nr_to_write > 0); |
| 693 | |
| 694 | *_next = index; |
| 695 | _leave(" = 0 [%lx]", *_next); |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | /* |
| 700 | * write some of the pending data back to the server |
| 701 | */ |
| 702 | int afs_writepages(struct address_space *mapping, |
| 703 | struct writeback_control *wbc) |
| 704 | { |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 705 | pgoff_t start, end, next; |
| 706 | int ret; |
| 707 | |
| 708 | _enter(""); |
| 709 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 710 | if (wbc->range_cyclic) { |
| 711 | start = mapping->writeback_index; |
| 712 | end = -1; |
| 713 | ret = afs_writepages_region(mapping, wbc, start, end, &next); |
Wu Fengguang | 1b430be | 2010-10-26 14:21:26 -0700 | [diff] [blame] | 714 | if (start > 0 && wbc->nr_to_write > 0 && ret == 0) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 715 | ret = afs_writepages_region(mapping, wbc, 0, start, |
| 716 | &next); |
| 717 | mapping->writeback_index = next; |
| 718 | } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 719 | end = (pgoff_t)(LLONG_MAX >> PAGE_SHIFT); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 720 | ret = afs_writepages_region(mapping, wbc, 0, end, &next); |
| 721 | if (wbc->nr_to_write > 0) |
| 722 | mapping->writeback_index = next; |
| 723 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 724 | start = wbc->range_start >> PAGE_SHIFT; |
| 725 | end = wbc->range_end >> PAGE_SHIFT; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 726 | ret = afs_writepages_region(mapping, wbc, start, end, &next); |
| 727 | } |
| 728 | |
| 729 | _leave(" = %d", ret); |
| 730 | return ret; |
| 731 | } |
| 732 | |
| 733 | /* |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 734 | * write to an AFS file |
| 735 | */ |
Al Viro | 50b5551 | 2014-04-03 14:13:46 -0400 | [diff] [blame] | 736 | ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 737 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 738 | struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp)); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 739 | ssize_t result; |
Al Viro | 50b5551 | 2014-04-03 14:13:46 -0400 | [diff] [blame] | 740 | size_t count = iov_iter_count(from); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 741 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 742 | _enter("{%llx:%llu},{%zu},", |
Al Viro | 50b5551 | 2014-04-03 14:13:46 -0400 | [diff] [blame] | 743 | vnode->fid.vid, vnode->fid.vnode, count); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 744 | |
| 745 | if (IS_SWAPFILE(&vnode->vfs_inode)) { |
| 746 | printk(KERN_INFO |
| 747 | "AFS: Attempt to write to active swap file!\n"); |
| 748 | return -EBUSY; |
| 749 | } |
| 750 | |
| 751 | if (!count) |
| 752 | return 0; |
| 753 | |
Al Viro | 50b5551 | 2014-04-03 14:13:46 -0400 | [diff] [blame] | 754 | result = generic_file_write_iter(iocb, from); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 755 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 756 | _leave(" = %zd", result); |
| 757 | return result; |
| 758 | } |
| 759 | |
| 760 | /* |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 761 | * flush any dirty pages for this process, and check for write errors. |
| 762 | * - the return status from this call provides a reliable indication of |
| 763 | * whether any write errors occurred for this process. |
| 764 | */ |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 765 | 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] | 766 | { |
Al Viro | 3c981bf | 2013-09-03 13:37:45 -0400 | [diff] [blame] | 767 | struct inode *inode = file_inode(file); |
Al Viro | 3c981bf | 2013-09-03 13:37:45 -0400 | [diff] [blame] | 768 | struct afs_vnode *vnode = AFS_FS_I(inode); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 769 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 770 | _enter("{%llx:%llu},{n=%pD},%d", |
Al Viro | 3c981bf | 2013-09-03 13:37:45 -0400 | [diff] [blame] | 771 | vnode->fid.vid, vnode->fid.vnode, file, |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 772 | datasync); |
| 773 | |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 774 | return file_write_and_wait_range(file, start, end); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 775 | } |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 776 | |
| 777 | /* |
| 778 | * notification that a previously read-only page is about to become writable |
| 779 | * - if it returns an error, the caller will deliver a bus error signal |
| 780 | */ |
Souptick Joarder | 0722f18 | 2018-08-23 17:00:48 -0700 | [diff] [blame] | 781 | vm_fault_t afs_page_mkwrite(struct vm_fault *vmf) |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 782 | { |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 783 | struct file *file = vmf->vma->vm_file; |
| 784 | struct inode *inode = file_inode(file); |
| 785 | struct afs_vnode *vnode = AFS_FS_I(inode); |
| 786 | unsigned long priv; |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 787 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 788 | _enter("{{%llx:%llu}},{%lx}", |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 789 | vnode->fid.vid, vnode->fid.vnode, vmf->page->index); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 790 | |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 791 | sb_start_pagefault(inode->i_sb); |
| 792 | |
| 793 | /* Wait for the page to be written to the cache before we allow it to |
| 794 | * be modified. We then assume the entire page will need writing back. |
| 795 | */ |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 796 | #ifdef CONFIG_AFS_FSCACHE |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 797 | fscache_wait_on_page_write(vnode->cache, vmf->page); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 798 | #endif |
| 799 | |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 800 | if (PageWriteback(vmf->page) && |
| 801 | wait_on_page_bit_killable(vmf->page, PG_writeback) < 0) |
| 802 | return VM_FAULT_RETRY; |
| 803 | |
| 804 | if (lock_page_killable(vmf->page) < 0) |
| 805 | return VM_FAULT_RETRY; |
| 806 | |
| 807 | /* We mustn't change page->private until writeback is complete as that |
| 808 | * details the portion of the page we need to write back and we might |
| 809 | * need to redirty the page if there's a problem. |
| 810 | */ |
| 811 | wait_on_page_writeback(vmf->page); |
| 812 | |
| 813 | priv = (unsigned long)PAGE_SIZE << AFS_PRIV_SHIFT; /* To */ |
| 814 | priv |= 0; /* From */ |
David Howells | 13524ab | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 815 | trace_afs_page_dirty(vnode, tracepoint_string("mkwrite"), |
| 816 | vmf->page->index, priv); |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 817 | SetPagePrivate(vmf->page); |
| 818 | set_page_private(vmf->page, priv); |
| 819 | |
| 820 | sb_end_pagefault(inode->i_sb); |
| 821 | return VM_FAULT_LOCKED; |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 822 | } |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 823 | |
| 824 | /* |
| 825 | * Prune the keys cached for writeback. The caller must hold vnode->wb_lock. |
| 826 | */ |
| 827 | void afs_prune_wb_keys(struct afs_vnode *vnode) |
| 828 | { |
| 829 | LIST_HEAD(graveyard); |
| 830 | struct afs_wb_key *wbk, *tmp; |
| 831 | |
| 832 | /* Discard unused keys */ |
| 833 | spin_lock(&vnode->wb_lock); |
| 834 | |
| 835 | if (!mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_WRITEBACK) && |
| 836 | !mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_DIRTY)) { |
| 837 | list_for_each_entry_safe(wbk, tmp, &vnode->wb_keys, vnode_link) { |
| 838 | if (refcount_read(&wbk->usage) == 1) |
| 839 | list_move(&wbk->vnode_link, &graveyard); |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | spin_unlock(&vnode->wb_lock); |
| 844 | |
| 845 | while (!list_empty(&graveyard)) { |
| 846 | wbk = list_entry(graveyard.next, struct afs_wb_key, vnode_link); |
| 847 | list_del(&wbk->vnode_link); |
| 848 | afs_put_wb_key(wbk); |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | /* |
| 853 | * Clean up a page during invalidation. |
| 854 | */ |
| 855 | int afs_launder_page(struct page *page) |
| 856 | { |
| 857 | struct address_space *mapping = page->mapping; |
| 858 | struct afs_vnode *vnode = AFS_FS_I(mapping->host); |
| 859 | unsigned long priv; |
| 860 | unsigned int f, t; |
| 861 | int ret = 0; |
| 862 | |
| 863 | _enter("{%lx}", page->index); |
| 864 | |
| 865 | priv = page_private(page); |
| 866 | if (clear_page_dirty_for_io(page)) { |
| 867 | f = 0; |
| 868 | t = PAGE_SIZE; |
| 869 | if (PagePrivate(page)) { |
| 870 | f = priv & AFS_PRIV_MAX; |
| 871 | t = priv >> AFS_PRIV_SHIFT; |
| 872 | } |
| 873 | |
David Howells | 13524ab | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 874 | trace_afs_page_dirty(vnode, tracepoint_string("launder"), |
| 875 | page->index, priv); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 876 | ret = afs_store_data(mapping, page->index, page->index, t, f); |
| 877 | } |
| 878 | |
David Howells | 13524ab | 2017-11-02 15:27:53 +0000 | [diff] [blame] | 879 | trace_afs_page_dirty(vnode, tracepoint_string("laundered"), |
| 880 | page->index, priv); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 881 | set_page_private(page, 0); |
| 882 | ClearPagePrivate(page); |
| 883 | |
| 884 | #ifdef CONFIG_AFS_FSCACHE |
| 885 | if (PageFsCache(page)) { |
| 886 | fscache_wait_on_page_write(vnode->cache, page); |
| 887 | fscache_uncache_page(vnode->cache, page); |
| 888 | } |
| 889 | #endif |
| 890 | return ret; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 891 | } |