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