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 | */ |
Alexey Dobriyan | 4af3c9c | 2007-10-16 23:29:23 -0700 | [diff] [blame] | 11 | #include <linux/backing-dev.h> |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 12 | #include <linux/slab.h> |
| 13 | #include <linux/fs.h> |
| 14 | #include <linux/pagemap.h> |
| 15 | #include <linux/writeback.h> |
| 16 | #include <linux/pagevec.h> |
| 17 | #include "internal.h" |
| 18 | |
| 19 | static int afs_write_back_from_locked_page(struct afs_writeback *wb, |
| 20 | struct page *page); |
| 21 | |
| 22 | /* |
| 23 | * mark a page as having been made dirty and thus needing writeback |
| 24 | */ |
| 25 | int afs_set_page_dirty(struct page *page) |
| 26 | { |
| 27 | _enter(""); |
| 28 | return __set_page_dirty_nobuffers(page); |
| 29 | } |
| 30 | |
| 31 | /* |
| 32 | * unlink a writeback record because its usage has reached zero |
| 33 | * - must be called with the wb->vnode->writeback_lock held |
| 34 | */ |
| 35 | static void afs_unlink_writeback(struct afs_writeback *wb) |
| 36 | { |
| 37 | struct afs_writeback *front; |
| 38 | struct afs_vnode *vnode = wb->vnode; |
| 39 | |
| 40 | list_del_init(&wb->link); |
| 41 | if (!list_empty(&vnode->writebacks)) { |
| 42 | /* if an fsync rises to the front of the queue then wake it |
| 43 | * up */ |
| 44 | front = list_entry(vnode->writebacks.next, |
| 45 | struct afs_writeback, link); |
| 46 | if (front->state == AFS_WBACK_SYNCING) { |
| 47 | _debug("wake up sync"); |
| 48 | front->state = AFS_WBACK_COMPLETE; |
| 49 | wake_up(&front->waitq); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * free a writeback record |
| 56 | */ |
| 57 | static void afs_free_writeback(struct afs_writeback *wb) |
| 58 | { |
| 59 | _enter(""); |
| 60 | key_put(wb->key); |
| 61 | kfree(wb); |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * dispose of a reference to a writeback record |
| 66 | */ |
| 67 | void afs_put_writeback(struct afs_writeback *wb) |
| 68 | { |
| 69 | struct afs_vnode *vnode = wb->vnode; |
| 70 | |
| 71 | _enter("{%d}", wb->usage); |
| 72 | |
| 73 | spin_lock(&vnode->writeback_lock); |
| 74 | if (--wb->usage == 0) |
| 75 | afs_unlink_writeback(wb); |
| 76 | else |
| 77 | wb = NULL; |
| 78 | spin_unlock(&vnode->writeback_lock); |
| 79 | if (wb) |
| 80 | afs_free_writeback(wb); |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * partly or wholly fill a page that's under preparation for writing |
| 85 | */ |
| 86 | static int afs_fill_page(struct afs_vnode *vnode, struct key *key, |
Anton Blanchard | 5e7f233 | 2011-06-13 22:31:12 +0100 | [diff] [blame] | 87 | loff_t pos, struct page *page) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 88 | { |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 89 | loff_t i_size; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 90 | int ret; |
Anton Blanchard | 5e7f233 | 2011-06-13 22:31:12 +0100 | [diff] [blame] | 91 | int len; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 92 | |
Anton Blanchard | 5e7f233 | 2011-06-13 22:31:12 +0100 | [diff] [blame] | 93 | _enter(",,%llu", (unsigned long long)pos); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 94 | |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 95 | i_size = i_size_read(&vnode->vfs_inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 96 | if (pos + PAGE_SIZE > i_size) |
Anton Blanchard | 5e7f233 | 2011-06-13 22:31:12 +0100 | [diff] [blame] | 97 | len = i_size - pos; |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 98 | else |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 99 | len = PAGE_SIZE; |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 100 | |
Anton Blanchard | 5e7f233 | 2011-06-13 22:31:12 +0100 | [diff] [blame] | 101 | ret = afs_vnode_fetch_data(vnode, key, pos, len, page); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 102 | if (ret < 0) { |
| 103 | if (ret == -ENOENT) { |
| 104 | _debug("got NOENT from server" |
| 105 | " - marking file deleted and stale"); |
| 106 | set_bit(AFS_VNODE_DELETED, &vnode->flags); |
| 107 | ret = -ESTALE; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | _leave(" = %d", ret); |
| 112 | return ret; |
| 113 | } |
| 114 | |
| 115 | /* |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 116 | * prepare to perform part of a write to a page |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 117 | */ |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 118 | int afs_write_begin(struct file *file, struct address_space *mapping, |
| 119 | loff_t pos, unsigned len, unsigned flags, |
| 120 | struct page **pagep, void **fsdata) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 121 | { |
| 122 | struct afs_writeback *candidate, *wb; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 123 | struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 124 | struct page *page; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 125 | struct key *key = file->private_data; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 126 | unsigned from = pos & (PAGE_SIZE - 1); |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 127 | unsigned to = from + len; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 128 | pgoff_t index = pos >> PAGE_SHIFT; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 129 | int ret; |
| 130 | |
| 131 | _enter("{%x:%u},{%lx},%u,%u", |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 132 | vnode->fid.vid, vnode->fid.vnode, index, from, to); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 133 | |
| 134 | candidate = kzalloc(sizeof(*candidate), GFP_KERNEL); |
| 135 | if (!candidate) |
| 136 | return -ENOMEM; |
| 137 | candidate->vnode = vnode; |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 138 | candidate->first = candidate->last = index; |
| 139 | candidate->offset_first = from; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 140 | candidate->to_last = to; |
Anton Blanchard | f129ccc | 2011-02-25 15:33:02 +0000 | [diff] [blame] | 141 | INIT_LIST_HEAD(&candidate->link); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 142 | candidate->usage = 1; |
| 143 | candidate->state = AFS_WBACK_PENDING; |
| 144 | init_waitqueue_head(&candidate->waitq); |
| 145 | |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 146 | page = grab_cache_page_write_begin(mapping, index, flags); |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 147 | if (!page) { |
| 148 | kfree(candidate); |
| 149 | return -ENOMEM; |
| 150 | } |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 151 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 152 | if (!PageUptodate(page) && len != PAGE_SIZE) { |
| 153 | ret = afs_fill_page(vnode, key, index << PAGE_SHIFT, page); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 154 | if (ret < 0) { |
David Howells | 856bb4b | 2017-03-16 16:27:48 +0000 | [diff] [blame^] | 155 | unlock_page(page); |
| 156 | put_page(page); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 157 | kfree(candidate); |
| 158 | _leave(" = %d [prep]", ret); |
| 159 | return ret; |
| 160 | } |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 161 | SetPageUptodate(page); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 162 | } |
| 163 | |
David Howells | 856bb4b | 2017-03-16 16:27:48 +0000 | [diff] [blame^] | 164 | /* page won't leak in error case: it eventually gets cleaned off LRU */ |
| 165 | *pagep = page; |
| 166 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 167 | try_again: |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 168 | spin_lock(&vnode->writeback_lock); |
| 169 | |
| 170 | /* see if this page is already pending a writeback under a suitable key |
| 171 | * - if so we can just join onto that one */ |
| 172 | wb = (struct afs_writeback *) page_private(page); |
| 173 | if (wb) { |
| 174 | if (wb->key == key && wb->state == AFS_WBACK_PENDING) |
| 175 | goto subsume_in_current_wb; |
| 176 | goto flush_conflicting_wb; |
| 177 | } |
| 178 | |
| 179 | if (index > 0) { |
| 180 | /* see if we can find an already pending writeback that we can |
| 181 | * append this page to */ |
| 182 | list_for_each_entry(wb, &vnode->writebacks, link) { |
| 183 | if (wb->last == index - 1 && wb->key == key && |
| 184 | wb->state == AFS_WBACK_PENDING) |
| 185 | goto append_to_previous_wb; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | list_add_tail(&candidate->link, &vnode->writebacks); |
| 190 | candidate->key = key_get(key); |
| 191 | spin_unlock(&vnode->writeback_lock); |
| 192 | SetPagePrivate(page); |
| 193 | set_page_private(page, (unsigned long) candidate); |
| 194 | _leave(" = 0 [new]"); |
| 195 | return 0; |
| 196 | |
| 197 | subsume_in_current_wb: |
| 198 | _debug("subsume"); |
| 199 | ASSERTRANGE(wb->first, <=, index, <=, wb->last); |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 200 | if (index == wb->first && from < wb->offset_first) |
| 201 | wb->offset_first = from; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 202 | if (index == wb->last && to > wb->to_last) |
| 203 | wb->to_last = to; |
| 204 | spin_unlock(&vnode->writeback_lock); |
| 205 | kfree(candidate); |
| 206 | _leave(" = 0 [sub]"); |
| 207 | return 0; |
| 208 | |
| 209 | append_to_previous_wb: |
| 210 | _debug("append into %lx-%lx", wb->first, wb->last); |
| 211 | wb->usage++; |
| 212 | wb->last++; |
| 213 | wb->to_last = to; |
| 214 | spin_unlock(&vnode->writeback_lock); |
| 215 | SetPagePrivate(page); |
| 216 | set_page_private(page, (unsigned long) wb); |
| 217 | kfree(candidate); |
| 218 | _leave(" = 0 [app]"); |
| 219 | return 0; |
| 220 | |
| 221 | /* the page is currently bound to another context, so if it's dirty we |
| 222 | * need to flush it before we can use the new context */ |
| 223 | flush_conflicting_wb: |
| 224 | _debug("flush conflict"); |
| 225 | if (wb->state == AFS_WBACK_PENDING) |
| 226 | wb->state = AFS_WBACK_CONFLICTING; |
| 227 | spin_unlock(&vnode->writeback_lock); |
| 228 | if (PageDirty(page)) { |
| 229 | ret = afs_write_back_from_locked_page(wb, page); |
| 230 | if (ret < 0) { |
| 231 | afs_put_writeback(candidate); |
| 232 | _leave(" = %d", ret); |
| 233 | return ret; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /* the page holds a ref on the writeback record */ |
| 238 | afs_put_writeback(wb); |
| 239 | set_page_private(page, 0); |
| 240 | ClearPagePrivate(page); |
| 241 | goto try_again; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * finalise part of a write to a page |
| 246 | */ |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 247 | int afs_write_end(struct file *file, struct address_space *mapping, |
| 248 | loff_t pos, unsigned len, unsigned copied, |
| 249 | struct page *page, void *fsdata) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 250 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 251 | struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 252 | loff_t i_size, maybe_i_size; |
| 253 | |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 254 | _enter("{%x:%u},{%lx}", |
| 255 | vnode->fid.vid, vnode->fid.vnode, page->index); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 256 | |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 257 | maybe_i_size = pos + copied; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 258 | |
| 259 | i_size = i_size_read(&vnode->vfs_inode); |
| 260 | if (maybe_i_size > i_size) { |
| 261 | spin_lock(&vnode->writeback_lock); |
| 262 | i_size = i_size_read(&vnode->vfs_inode); |
| 263 | if (maybe_i_size > i_size) |
| 264 | i_size_write(&vnode->vfs_inode, maybe_i_size); |
| 265 | spin_unlock(&vnode->writeback_lock); |
| 266 | } |
| 267 | |
| 268 | set_page_dirty(page); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 269 | if (PageDirty(page)) |
| 270 | _debug("dirtied"); |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 271 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 272 | put_page(page); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 273 | |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 274 | return copied; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /* |
| 278 | * kill all the pages in the given range |
| 279 | */ |
| 280 | static void afs_kill_pages(struct afs_vnode *vnode, bool error, |
| 281 | pgoff_t first, pgoff_t last) |
| 282 | { |
| 283 | struct pagevec pv; |
| 284 | unsigned count, loop; |
| 285 | |
| 286 | _enter("{%x:%u},%lx-%lx", |
| 287 | vnode->fid.vid, vnode->fid.vnode, first, last); |
| 288 | |
| 289 | pagevec_init(&pv, 0); |
| 290 | |
| 291 | do { |
| 292 | _debug("kill %lx-%lx", first, last); |
| 293 | |
| 294 | count = last - first + 1; |
| 295 | if (count > PAGEVEC_SIZE) |
| 296 | count = PAGEVEC_SIZE; |
| 297 | pv.nr = find_get_pages_contig(vnode->vfs_inode.i_mapping, |
| 298 | first, count, pv.pages); |
| 299 | ASSERTCMP(pv.nr, ==, count); |
| 300 | |
| 301 | for (loop = 0; loop < count; loop++) { |
| 302 | ClearPageUptodate(pv.pages[loop]); |
| 303 | if (error) |
| 304 | SetPageError(pv.pages[loop]); |
| 305 | end_page_writeback(pv.pages[loop]); |
| 306 | } |
| 307 | |
| 308 | __pagevec_release(&pv); |
| 309 | } while (first < last); |
| 310 | |
| 311 | _leave(""); |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | * synchronously write back the locked page and any subsequent non-locked dirty |
| 316 | * pages also covered by the same writeback record |
| 317 | */ |
| 318 | static int afs_write_back_from_locked_page(struct afs_writeback *wb, |
| 319 | struct page *primary_page) |
| 320 | { |
| 321 | struct page *pages[8], *page; |
| 322 | unsigned long count; |
| 323 | unsigned n, offset, to; |
| 324 | pgoff_t start, first, last; |
| 325 | int loop, ret; |
| 326 | |
| 327 | _enter(",%lx", primary_page->index); |
| 328 | |
| 329 | count = 1; |
| 330 | if (!clear_page_dirty_for_io(primary_page)) |
| 331 | BUG(); |
| 332 | if (test_set_page_writeback(primary_page)) |
| 333 | BUG(); |
| 334 | |
| 335 | /* find all consecutive lockable dirty pages, stopping when we find a |
| 336 | * page that is not immediately lockable, is not dirty or is missing, |
| 337 | * or we reach the end of the range */ |
| 338 | start = primary_page->index; |
| 339 | if (start >= wb->last) |
| 340 | goto no_more; |
| 341 | start++; |
| 342 | do { |
| 343 | _debug("more %lx [%lx]", start, count); |
| 344 | n = wb->last - start + 1; |
| 345 | if (n > ARRAY_SIZE(pages)) |
| 346 | n = ARRAY_SIZE(pages); |
| 347 | n = find_get_pages_contig(wb->vnode->vfs_inode.i_mapping, |
| 348 | start, n, pages); |
| 349 | _debug("fgpc %u", n); |
| 350 | if (n == 0) |
| 351 | goto no_more; |
| 352 | if (pages[0]->index != start) { |
David Howells | 9d577b6 | 2007-05-10 22:22:19 -0700 | [diff] [blame] | 353 | do { |
| 354 | put_page(pages[--n]); |
| 355 | } while (n > 0); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 356 | goto no_more; |
| 357 | } |
| 358 | |
| 359 | for (loop = 0; loop < n; loop++) { |
| 360 | page = pages[loop]; |
| 361 | if (page->index > wb->last) |
| 362 | break; |
Nick Piggin | 529ae9a | 2008-08-02 12:01:03 +0200 | [diff] [blame] | 363 | if (!trylock_page(page)) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 364 | break; |
| 365 | if (!PageDirty(page) || |
| 366 | page_private(page) != (unsigned long) wb) { |
| 367 | unlock_page(page); |
| 368 | break; |
| 369 | } |
| 370 | if (!clear_page_dirty_for_io(page)) |
| 371 | BUG(); |
| 372 | if (test_set_page_writeback(page)) |
| 373 | BUG(); |
| 374 | unlock_page(page); |
| 375 | put_page(page); |
| 376 | } |
| 377 | count += loop; |
| 378 | if (loop < n) { |
| 379 | for (; loop < n; loop++) |
| 380 | put_page(pages[loop]); |
| 381 | goto no_more; |
| 382 | } |
| 383 | |
| 384 | start += loop; |
| 385 | } while (start <= wb->last && count < 65536); |
| 386 | |
| 387 | no_more: |
| 388 | /* we now have a contiguous set of dirty pages, each with writeback set |
| 389 | * and the dirty mark cleared; the first page is locked and must remain |
| 390 | * so, all the rest are unlocked */ |
| 391 | first = primary_page->index; |
| 392 | last = first + count - 1; |
| 393 | |
| 394 | offset = (first == wb->first) ? wb->offset_first : 0; |
| 395 | to = (last == wb->last) ? wb->to_last : PAGE_SIZE; |
| 396 | |
| 397 | _debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to); |
| 398 | |
| 399 | ret = afs_vnode_store_data(wb, first, last, offset, to); |
| 400 | if (ret < 0) { |
| 401 | switch (ret) { |
| 402 | case -EDQUOT: |
| 403 | case -ENOSPC: |
Michal Hocko | 5114a97 | 2016-10-11 13:56:01 -0700 | [diff] [blame] | 404 | mapping_set_error(wb->vnode->vfs_inode.i_mapping, -ENOSPC); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 405 | break; |
| 406 | case -EROFS: |
| 407 | case -EIO: |
| 408 | case -EREMOTEIO: |
| 409 | case -EFBIG: |
| 410 | case -ENOENT: |
| 411 | case -ENOMEDIUM: |
| 412 | case -ENXIO: |
| 413 | afs_kill_pages(wb->vnode, true, first, last); |
Michal Hocko | 5114a97 | 2016-10-11 13:56:01 -0700 | [diff] [blame] | 414 | mapping_set_error(wb->vnode->vfs_inode.i_mapping, -EIO); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 415 | break; |
| 416 | case -EACCES: |
| 417 | case -EPERM: |
| 418 | case -ENOKEY: |
| 419 | case -EKEYEXPIRED: |
| 420 | case -EKEYREJECTED: |
| 421 | case -EKEYREVOKED: |
| 422 | afs_kill_pages(wb->vnode, false, first, last); |
| 423 | break; |
| 424 | default: |
| 425 | break; |
| 426 | } |
| 427 | } else { |
| 428 | ret = count; |
| 429 | } |
| 430 | |
| 431 | _leave(" = %d", ret); |
| 432 | return ret; |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | * write a page back to the server |
| 437 | * - the caller locked the page for us |
| 438 | */ |
| 439 | int afs_writepage(struct page *page, struct writeback_control *wbc) |
| 440 | { |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 441 | struct afs_writeback *wb; |
| 442 | int ret; |
| 443 | |
| 444 | _enter("{%lx},", page->index); |
| 445 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 446 | wb = (struct afs_writeback *) page_private(page); |
| 447 | ASSERT(wb != NULL); |
| 448 | |
| 449 | ret = afs_write_back_from_locked_page(wb, page); |
| 450 | unlock_page(page); |
| 451 | if (ret < 0) { |
| 452 | _leave(" = %d", ret); |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | wbc->nr_to_write -= ret; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 457 | |
| 458 | _leave(" = 0"); |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | * write a region of pages back to the server |
| 464 | */ |
Adrian Bunk | c1206a2 | 2007-10-16 23:26:41 -0700 | [diff] [blame] | 465 | static int afs_writepages_region(struct address_space *mapping, |
| 466 | struct writeback_control *wbc, |
| 467 | pgoff_t index, pgoff_t end, pgoff_t *_next) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 468 | { |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 469 | struct afs_writeback *wb; |
| 470 | struct page *page; |
| 471 | int ret, n; |
| 472 | |
| 473 | _enter(",,%lx,%lx,", index, end); |
| 474 | |
| 475 | do { |
| 476 | n = find_get_pages_tag(mapping, &index, PAGECACHE_TAG_DIRTY, |
| 477 | 1, &page); |
| 478 | if (!n) |
| 479 | break; |
| 480 | |
| 481 | _debug("wback %lx", page->index); |
| 482 | |
| 483 | if (page->index > end) { |
| 484 | *_next = index; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 485 | put_page(page); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 486 | _leave(" = 0 [%lx]", *_next); |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | /* at this point we hold neither mapping->tree_lock nor lock on |
| 491 | * the page itself: the page may be truncated or invalidated |
| 492 | * (changing page->mapping to NULL), or even swizzled back from |
| 493 | * swapper_space to tmpfs file mapping |
| 494 | */ |
| 495 | lock_page(page); |
| 496 | |
| 497 | if (page->mapping != mapping) { |
| 498 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 499 | put_page(page); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 500 | continue; |
| 501 | } |
| 502 | |
| 503 | if (wbc->sync_mode != WB_SYNC_NONE) |
| 504 | wait_on_page_writeback(page); |
| 505 | |
| 506 | if (PageWriteback(page) || !PageDirty(page)) { |
| 507 | unlock_page(page); |
David Howells | c250fae | 2017-03-16 16:27:43 +0000 | [diff] [blame] | 508 | put_page(page); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 509 | continue; |
| 510 | } |
| 511 | |
| 512 | wb = (struct afs_writeback *) page_private(page); |
| 513 | ASSERT(wb != NULL); |
| 514 | |
| 515 | spin_lock(&wb->vnode->writeback_lock); |
| 516 | wb->state = AFS_WBACK_WRITING; |
| 517 | spin_unlock(&wb->vnode->writeback_lock); |
| 518 | |
| 519 | ret = afs_write_back_from_locked_page(wb, page); |
| 520 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 521 | put_page(page); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 522 | if (ret < 0) { |
| 523 | _leave(" = %d", ret); |
| 524 | return ret; |
| 525 | } |
| 526 | |
| 527 | wbc->nr_to_write -= ret; |
| 528 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 529 | cond_resched(); |
| 530 | } while (index < end && wbc->nr_to_write > 0); |
| 531 | |
| 532 | *_next = index; |
| 533 | _leave(" = 0 [%lx]", *_next); |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | /* |
| 538 | * write some of the pending data back to the server |
| 539 | */ |
| 540 | int afs_writepages(struct address_space *mapping, |
| 541 | struct writeback_control *wbc) |
| 542 | { |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 543 | pgoff_t start, end, next; |
| 544 | int ret; |
| 545 | |
| 546 | _enter(""); |
| 547 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 548 | if (wbc->range_cyclic) { |
| 549 | start = mapping->writeback_index; |
| 550 | end = -1; |
| 551 | ret = afs_writepages_region(mapping, wbc, start, end, &next); |
Wu Fengguang | 1b430be | 2010-10-26 14:21:26 -0700 | [diff] [blame] | 552 | if (start > 0 && wbc->nr_to_write > 0 && ret == 0) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 553 | ret = afs_writepages_region(mapping, wbc, 0, start, |
| 554 | &next); |
| 555 | mapping->writeback_index = next; |
| 556 | } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 557 | end = (pgoff_t)(LLONG_MAX >> PAGE_SHIFT); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 558 | ret = afs_writepages_region(mapping, wbc, 0, end, &next); |
| 559 | if (wbc->nr_to_write > 0) |
| 560 | mapping->writeback_index = next; |
| 561 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 562 | start = wbc->range_start >> PAGE_SHIFT; |
| 563 | end = wbc->range_end >> PAGE_SHIFT; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 564 | ret = afs_writepages_region(mapping, wbc, start, end, &next); |
| 565 | } |
| 566 | |
| 567 | _leave(" = %d", ret); |
| 568 | return ret; |
| 569 | } |
| 570 | |
| 571 | /* |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 572 | * completion of write to server |
| 573 | */ |
| 574 | void afs_pages_written_back(struct afs_vnode *vnode, struct afs_call *call) |
| 575 | { |
| 576 | struct afs_writeback *wb = call->wb; |
| 577 | struct pagevec pv; |
| 578 | unsigned count, loop; |
| 579 | pgoff_t first = call->first, last = call->last; |
| 580 | bool free_wb; |
| 581 | |
| 582 | _enter("{%x:%u},{%lx-%lx}", |
| 583 | vnode->fid.vid, vnode->fid.vnode, first, last); |
| 584 | |
| 585 | ASSERT(wb != NULL); |
| 586 | |
| 587 | pagevec_init(&pv, 0); |
| 588 | |
| 589 | do { |
David Howells | 5bbf5d3 | 2007-05-10 03:15:23 -0700 | [diff] [blame] | 590 | _debug("done %lx-%lx", first, last); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 591 | |
| 592 | count = last - first + 1; |
| 593 | if (count > PAGEVEC_SIZE) |
| 594 | count = PAGEVEC_SIZE; |
| 595 | pv.nr = find_get_pages_contig(call->mapping, first, count, |
| 596 | pv.pages); |
| 597 | ASSERTCMP(pv.nr, ==, count); |
| 598 | |
| 599 | spin_lock(&vnode->writeback_lock); |
| 600 | for (loop = 0; loop < count; loop++) { |
| 601 | struct page *page = pv.pages[loop]; |
| 602 | end_page_writeback(page); |
| 603 | if (page_private(page) == (unsigned long) wb) { |
| 604 | set_page_private(page, 0); |
| 605 | ClearPagePrivate(page); |
| 606 | wb->usage--; |
| 607 | } |
| 608 | } |
| 609 | free_wb = false; |
| 610 | if (wb->usage == 0) { |
| 611 | afs_unlink_writeback(wb); |
| 612 | free_wb = true; |
| 613 | } |
| 614 | spin_unlock(&vnode->writeback_lock); |
| 615 | first += count; |
| 616 | if (free_wb) { |
| 617 | afs_free_writeback(wb); |
| 618 | wb = NULL; |
| 619 | } |
| 620 | |
| 621 | __pagevec_release(&pv); |
David Howells | 5bbf5d3 | 2007-05-10 03:15:23 -0700 | [diff] [blame] | 622 | } while (first <= last); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 623 | |
| 624 | _leave(""); |
| 625 | } |
| 626 | |
| 627 | /* |
| 628 | * write to an AFS file |
| 629 | */ |
Al Viro | 50b5551 | 2014-04-03 14:13:46 -0400 | [diff] [blame] | 630 | ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 631 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 632 | struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp)); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 633 | ssize_t result; |
Al Viro | 50b5551 | 2014-04-03 14:13:46 -0400 | [diff] [blame] | 634 | size_t count = iov_iter_count(from); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 635 | |
Al Viro | 50b5551 | 2014-04-03 14:13:46 -0400 | [diff] [blame] | 636 | _enter("{%x.%u},{%zu},", |
| 637 | vnode->fid.vid, vnode->fid.vnode, count); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 638 | |
| 639 | if (IS_SWAPFILE(&vnode->vfs_inode)) { |
| 640 | printk(KERN_INFO |
| 641 | "AFS: Attempt to write to active swap file!\n"); |
| 642 | return -EBUSY; |
| 643 | } |
| 644 | |
| 645 | if (!count) |
| 646 | return 0; |
| 647 | |
Al Viro | 50b5551 | 2014-04-03 14:13:46 -0400 | [diff] [blame] | 648 | result = generic_file_write_iter(iocb, from); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 649 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 650 | _leave(" = %zd", result); |
| 651 | return result; |
| 652 | } |
| 653 | |
| 654 | /* |
| 655 | * flush the vnode to the fileserver |
| 656 | */ |
| 657 | int afs_writeback_all(struct afs_vnode *vnode) |
| 658 | { |
| 659 | struct address_space *mapping = vnode->vfs_inode.i_mapping; |
| 660 | struct writeback_control wbc = { |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 661 | .sync_mode = WB_SYNC_ALL, |
| 662 | .nr_to_write = LONG_MAX, |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 663 | .range_cyclic = 1, |
| 664 | }; |
| 665 | int ret; |
| 666 | |
| 667 | _enter(""); |
| 668 | |
| 669 | ret = mapping->a_ops->writepages(mapping, &wbc); |
| 670 | __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); |
| 671 | |
| 672 | _leave(" = %d", ret); |
| 673 | return ret; |
| 674 | } |
| 675 | |
| 676 | /* |
| 677 | * flush any dirty pages for this process, and check for write errors. |
| 678 | * - the return status from this call provides a reliable indication of |
| 679 | * whether any write errors occurred for this process. |
| 680 | */ |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 681 | 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] | 682 | { |
Al Viro | 3c981bf | 2013-09-03 13:37:45 -0400 | [diff] [blame] | 683 | struct inode *inode = file_inode(file); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 684 | struct afs_writeback *wb, *xwb; |
Al Viro | 3c981bf | 2013-09-03 13:37:45 -0400 | [diff] [blame] | 685 | struct afs_vnode *vnode = AFS_FS_I(inode); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 686 | int ret; |
| 687 | |
Al Viro | 3c981bf | 2013-09-03 13:37:45 -0400 | [diff] [blame] | 688 | _enter("{%x:%u},{n=%pD},%d", |
| 689 | vnode->fid.vid, vnode->fid.vnode, file, |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 690 | datasync); |
| 691 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 692 | ret = filemap_write_and_wait_range(inode->i_mapping, start, end); |
| 693 | if (ret) |
| 694 | return ret; |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 695 | inode_lock(inode); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 696 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 697 | /* use a writeback record as a marker in the queue - when this reaches |
| 698 | * the front of the queue, all the outstanding writes are either |
| 699 | * completed or rejected */ |
| 700 | wb = kzalloc(sizeof(*wb), GFP_KERNEL); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 701 | if (!wb) { |
| 702 | ret = -ENOMEM; |
| 703 | goto out; |
| 704 | } |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 705 | wb->vnode = vnode; |
| 706 | wb->first = 0; |
| 707 | wb->last = -1; |
| 708 | wb->offset_first = 0; |
| 709 | wb->to_last = PAGE_SIZE; |
| 710 | wb->usage = 1; |
| 711 | wb->state = AFS_WBACK_SYNCING; |
| 712 | init_waitqueue_head(&wb->waitq); |
| 713 | |
| 714 | spin_lock(&vnode->writeback_lock); |
| 715 | list_for_each_entry(xwb, &vnode->writebacks, link) { |
| 716 | if (xwb->state == AFS_WBACK_PENDING) |
| 717 | xwb->state = AFS_WBACK_CONFLICTING; |
| 718 | } |
| 719 | list_add_tail(&wb->link, &vnode->writebacks); |
| 720 | spin_unlock(&vnode->writeback_lock); |
| 721 | |
| 722 | /* push all the outstanding writebacks to the server */ |
| 723 | ret = afs_writeback_all(vnode); |
| 724 | if (ret < 0) { |
| 725 | afs_put_writeback(wb); |
| 726 | _leave(" = %d [wb]", ret); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 727 | goto out; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | /* wait for the preceding writes to actually complete */ |
| 731 | ret = wait_event_interruptible(wb->waitq, |
| 732 | wb->state == AFS_WBACK_COMPLETE || |
| 733 | vnode->writebacks.next == &wb->link); |
| 734 | afs_put_writeback(wb); |
| 735 | _leave(" = %d", ret); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 736 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 737 | inode_unlock(inode); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 738 | return ret; |
| 739 | } |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 740 | |
| 741 | /* |
David Howells | 7286fad | 2017-03-16 16:27:45 +0000 | [diff] [blame] | 742 | * Flush out all outstanding writes on a file opened for writing when it is |
| 743 | * closed. |
| 744 | */ |
| 745 | int afs_flush(struct file *file, fl_owner_t id) |
| 746 | { |
| 747 | _enter(""); |
| 748 | |
| 749 | if ((file->f_mode & FMODE_WRITE) == 0) |
| 750 | return 0; |
| 751 | |
| 752 | return vfs_fsync(file, 0); |
| 753 | } |
| 754 | |
| 755 | /* |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 756 | * notification that a previously read-only page is about to become writable |
| 757 | * - if it returns an error, the caller will deliver a bus error signal |
| 758 | */ |
| 759 | int afs_page_mkwrite(struct vm_area_struct *vma, struct page *page) |
| 760 | { |
| 761 | struct afs_vnode *vnode = AFS_FS_I(vma->vm_file->f_mapping->host); |
| 762 | |
| 763 | _enter("{{%x:%u}},{%lx}", |
| 764 | vnode->fid.vid, vnode->fid.vnode, page->index); |
| 765 | |
| 766 | /* wait for the page to be written to the cache before we allow it to |
| 767 | * be modified */ |
| 768 | #ifdef CONFIG_AFS_FSCACHE |
| 769 | fscache_wait_on_page_write(vnode->cache, page); |
| 770 | #endif |
| 771 | |
| 772 | _leave(" = 0"); |
| 773 | return 0; |
| 774 | } |