David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 1 | /* AFS filesystem file handling |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 3 | * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
| 11 | |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/fs.h> |
| 16 | #include <linux/pagemap.h> |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 17 | #include <linux/writeback.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/gfp.h> |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 19 | #include <linux/task_io_accounting_ops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "internal.h" |
| 21 | |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 22 | static int afs_readpage(struct file *file, struct page *page); |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 23 | static void afs_invalidatepage(struct page *page, unsigned int offset, |
| 24 | unsigned int length); |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 25 | static int afs_releasepage(struct page *page, gfp_t gfp_flags); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 26 | static int afs_launder_page(struct page *page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 28 | static int afs_readpages(struct file *filp, struct address_space *mapping, |
| 29 | struct list_head *pages, unsigned nr_pages); |
| 30 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 31 | const struct file_operations afs_file_operations = { |
| 32 | .open = afs_open, |
| 33 | .release = afs_release, |
| 34 | .llseek = generic_file_llseek, |
Al Viro | aad4f8b | 2014-04-02 14:33:16 -0400 | [diff] [blame] | 35 | .read_iter = generic_file_read_iter, |
Al Viro | 50b5551 | 2014-04-03 14:13:46 -0400 | [diff] [blame] | 36 | .write_iter = afs_file_write, |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 37 | .mmap = generic_file_readonly_mmap, |
Jens Axboe | 5ffc4ef | 2007-06-01 11:49:19 +0200 | [diff] [blame] | 38 | .splice_read = generic_file_splice_read, |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 39 | .fsync = afs_fsync, |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 40 | .lock = afs_lock, |
| 41 | .flock = afs_flock, |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 44 | const struct inode_operations afs_file_inode_operations = { |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 45 | .getattr = afs_getattr, |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 46 | .setattr = afs_setattr, |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 47 | .permission = afs_permission, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 50 | const struct address_space_operations afs_fs_aops = { |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 51 | .readpage = afs_readpage, |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 52 | .readpages = afs_readpages, |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 53 | .set_page_dirty = afs_set_page_dirty, |
| 54 | .launder_page = afs_launder_page, |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 55 | .releasepage = afs_releasepage, |
| 56 | .invalidatepage = afs_invalidatepage, |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 57 | .write_begin = afs_write_begin, |
| 58 | .write_end = afs_write_end, |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 59 | .writepage = afs_writepage, |
| 60 | .writepages = afs_writepages, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | /* |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 64 | * open an AFS file or directory and attach a key to it |
| 65 | */ |
| 66 | int afs_open(struct inode *inode, struct file *file) |
| 67 | { |
| 68 | struct afs_vnode *vnode = AFS_FS_I(inode); |
| 69 | struct key *key; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 70 | int ret; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 71 | |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 72 | _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 73 | |
| 74 | key = afs_request_key(vnode->volume->cell); |
| 75 | if (IS_ERR(key)) { |
| 76 | _leave(" = %ld [key]", PTR_ERR(key)); |
| 77 | return PTR_ERR(key); |
| 78 | } |
| 79 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 80 | ret = afs_validate(vnode, key); |
| 81 | if (ret < 0) { |
| 82 | _leave(" = %d [val]", ret); |
| 83 | return ret; |
| 84 | } |
| 85 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 86 | file->private_data = key; |
| 87 | _leave(" = 0"); |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * release an AFS file or directory and discard its key |
| 93 | */ |
| 94 | int afs_release(struct inode *inode, struct file *file) |
| 95 | { |
| 96 | struct afs_vnode *vnode = AFS_FS_I(inode); |
| 97 | |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 98 | _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 99 | |
| 100 | key_put(file->private_data); |
| 101 | _leave(" = 0"); |
| 102 | return 0; |
| 103 | } |
| 104 | |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 105 | /* |
| 106 | * Dispose of a ref to a read record. |
| 107 | */ |
| 108 | void afs_put_read(struct afs_read *req) |
| 109 | { |
| 110 | int i; |
| 111 | |
| 112 | if (atomic_dec_and_test(&req->usage)) { |
| 113 | for (i = 0; i < req->nr_pages; i++) |
| 114 | if (req->pages[i]) |
| 115 | put_page(req->pages[i]); |
| 116 | kfree(req); |
| 117 | } |
| 118 | } |
| 119 | |
Matt Kraai | 6566abd | 2009-04-17 12:56:38 +0100 | [diff] [blame] | 120 | #ifdef CONFIG_AFS_FSCACHE |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 121 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | * deal with notification that a page was read from the cache |
| 123 | */ |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 124 | static void afs_file_readpage_read_complete(struct page *page, |
| 125 | void *data, |
| 126 | int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | { |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 128 | _enter("%p,%p,%d", page, data, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 130 | /* if the read completes with an error, we just unlock the page and let |
| 131 | * the VM reissue the readpage */ |
| 132 | if (!error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | SetPageUptodate(page); |
| 134 | unlock_page(page); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 135 | } |
Matt Kraai | 6566abd | 2009-04-17 12:56:38 +0100 | [diff] [blame] | 136 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | /* |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 139 | * read page from file, directory or symlink, given a key to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | */ |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 141 | int afs_page_filler(void *data, struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 143 | struct inode *inode = page->mapping->host; |
| 144 | struct afs_vnode *vnode = AFS_FS_I(inode); |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 145 | struct afs_read *req; |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 146 | struct key *key = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | int ret; |
| 148 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 149 | _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
Matt Mackall | cd7619d | 2005-05-01 08:59:01 -0700 | [diff] [blame] | 151 | BUG_ON(!PageLocked(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
| 153 | ret = -ESTALE; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 154 | if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | goto error; |
| 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | /* is it cached? */ |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 158 | #ifdef CONFIG_AFS_FSCACHE |
| 159 | ret = fscache_read_or_alloc_page(vnode->cache, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | page, |
| 161 | afs_file_readpage_read_complete, |
| 162 | NULL, |
| 163 | GFP_KERNEL); |
| 164 | #else |
| 165 | ret = -ENOBUFS; |
| 166 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | switch (ret) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | /* read BIO submitted (page in cache) */ |
| 169 | case 0: |
| 170 | break; |
| 171 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 172 | /* page not yet cached */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | case -ENODATA: |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 174 | _debug("cache said ENODATA"); |
| 175 | goto go_on; |
| 176 | |
| 177 | /* page will not be cached */ |
| 178 | case -ENOBUFS: |
| 179 | _debug("cache said ENOBUFS"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | default: |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 181 | go_on: |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 182 | req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *), |
| 183 | GFP_KERNEL); |
| 184 | if (!req) |
| 185 | goto enomem; |
| 186 | |
| 187 | atomic_set(&req->usage, 1); |
| 188 | req->pos = (loff_t)page->index << PAGE_SHIFT; |
| 189 | req->len = min_t(size_t, i_size_read(inode) - req->pos, |
| 190 | PAGE_SIZE); |
| 191 | req->nr_pages = 1; |
| 192 | req->pages[0] = page; |
| 193 | get_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
| 195 | /* read the contents of the file from the server into the |
| 196 | * page */ |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 197 | ret = afs_vnode_fetch_data(vnode, key, req); |
| 198 | afs_put_read(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | if (ret < 0) { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 200 | if (ret == -ENOENT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | _debug("got NOENT from server" |
| 202 | " - marking file deleted and stale"); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 203 | set_bit(AFS_VNODE_DELETED, &vnode->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | ret = -ESTALE; |
| 205 | } |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 206 | |
| 207 | #ifdef CONFIG_AFS_FSCACHE |
| 208 | fscache_uncache_page(vnode->cache, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | #endif |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 210 | BUG_ON(PageFsCache(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | goto error; |
| 212 | } |
| 213 | |
| 214 | SetPageUptodate(page); |
| 215 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 216 | /* send the page to the cache */ |
| 217 | #ifdef CONFIG_AFS_FSCACHE |
| 218 | if (PageFsCache(page) && |
| 219 | fscache_write_page(vnode->cache, page, GFP_KERNEL) != 0) { |
| 220 | fscache_uncache_page(vnode->cache, page); |
| 221 | BUG_ON(PageFsCache(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | #endif |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 224 | unlock_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | _leave(" = 0"); |
| 228 | return 0; |
| 229 | |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 230 | enomem: |
| 231 | ret = -ENOMEM; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 232 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | SetPageError(page); |
| 234 | unlock_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | _leave(" = %d", ret); |
| 236 | return ret; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 237 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | /* |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 240 | * read page from file, directory or symlink, given a file to nominate the key |
| 241 | * to be used |
| 242 | */ |
| 243 | static int afs_readpage(struct file *file, struct page *page) |
| 244 | { |
| 245 | struct key *key; |
| 246 | int ret; |
| 247 | |
| 248 | if (file) { |
| 249 | key = file->private_data; |
| 250 | ASSERT(key != NULL); |
| 251 | ret = afs_page_filler(key, page); |
| 252 | } else { |
| 253 | struct inode *inode = page->mapping->host; |
| 254 | key = afs_request_key(AFS_FS_S(inode->i_sb)->volume->cell); |
| 255 | if (IS_ERR(key)) { |
| 256 | ret = PTR_ERR(key); |
| 257 | } else { |
| 258 | ret = afs_page_filler(key, page); |
| 259 | key_put(key); |
| 260 | } |
| 261 | } |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | /* |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 266 | * Make pages available as they're filled. |
| 267 | */ |
| 268 | static void afs_readpages_page_done(struct afs_call *call, struct afs_read *req) |
| 269 | { |
Arnd Bergmann | 51c89e6 | 2017-01-13 14:46:19 +0000 | [diff] [blame] | 270 | #ifdef CONFIG_AFS_FSCACHE |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 271 | struct afs_vnode *vnode = call->reply; |
Arnd Bergmann | 51c89e6 | 2017-01-13 14:46:19 +0000 | [diff] [blame] | 272 | #endif |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 273 | struct page *page = req->pages[req->index]; |
| 274 | |
| 275 | req->pages[req->index] = NULL; |
| 276 | SetPageUptodate(page); |
| 277 | |
| 278 | /* send the page to the cache */ |
| 279 | #ifdef CONFIG_AFS_FSCACHE |
| 280 | if (PageFsCache(page) && |
| 281 | fscache_write_page(vnode->cache, page, GFP_KERNEL) != 0) { |
| 282 | fscache_uncache_page(vnode->cache, page); |
| 283 | BUG_ON(PageFsCache(page)); |
| 284 | } |
| 285 | #endif |
| 286 | unlock_page(page); |
| 287 | put_page(page); |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * Read a contiguous set of pages. |
| 292 | */ |
| 293 | static int afs_readpages_one(struct file *file, struct address_space *mapping, |
| 294 | struct list_head *pages) |
| 295 | { |
| 296 | struct afs_vnode *vnode = AFS_FS_I(mapping->host); |
| 297 | struct afs_read *req; |
| 298 | struct list_head *p; |
| 299 | struct page *first, *page; |
| 300 | struct key *key = file->private_data; |
| 301 | pgoff_t index; |
| 302 | int ret, n, i; |
| 303 | |
| 304 | /* Count the number of contiguous pages at the front of the list. Note |
| 305 | * that the list goes prev-wards rather than next-wards. |
| 306 | */ |
| 307 | first = list_entry(pages->prev, struct page, lru); |
| 308 | index = first->index + 1; |
| 309 | n = 1; |
| 310 | for (p = first->lru.prev; p != pages; p = p->prev) { |
| 311 | page = list_entry(p, struct page, lru); |
| 312 | if (page->index != index) |
| 313 | break; |
| 314 | index++; |
| 315 | n++; |
| 316 | } |
| 317 | |
| 318 | req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *) * n, |
| 319 | GFP_NOFS); |
| 320 | if (!req) |
| 321 | return -ENOMEM; |
| 322 | |
| 323 | atomic_set(&req->usage, 1); |
| 324 | req->page_done = afs_readpages_page_done; |
| 325 | req->pos = first->index; |
| 326 | req->pos <<= PAGE_SHIFT; |
| 327 | |
| 328 | /* Transfer the pages to the request. We add them in until one fails |
| 329 | * to add to the LRU and then we stop (as that'll make a hole in the |
| 330 | * contiguous run. |
| 331 | * |
| 332 | * Note that it's possible for the file size to change whilst we're |
| 333 | * doing this, but we rely on the server returning less than we asked |
| 334 | * for if the file shrank. We also rely on this to deal with a partial |
| 335 | * page at the end of the file. |
| 336 | */ |
| 337 | do { |
| 338 | page = list_entry(pages->prev, struct page, lru); |
| 339 | list_del(&page->lru); |
| 340 | index = page->index; |
| 341 | if (add_to_page_cache_lru(page, mapping, index, |
| 342 | readahead_gfp_mask(mapping))) { |
| 343 | #ifdef CONFIG_AFS_FSCACHE |
| 344 | fscache_uncache_page(vnode->cache, page); |
| 345 | #endif |
| 346 | put_page(page); |
| 347 | break; |
| 348 | } |
| 349 | |
| 350 | req->pages[req->nr_pages++] = page; |
| 351 | req->len += PAGE_SIZE; |
| 352 | } while (req->nr_pages < n); |
| 353 | |
| 354 | if (req->nr_pages == 0) { |
| 355 | kfree(req); |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | ret = afs_vnode_fetch_data(vnode, key, req); |
| 360 | if (ret < 0) |
| 361 | goto error; |
| 362 | |
| 363 | task_io_account_read(PAGE_SIZE * req->nr_pages); |
| 364 | afs_put_read(req); |
| 365 | return 0; |
| 366 | |
| 367 | error: |
| 368 | if (ret == -ENOENT) { |
| 369 | _debug("got NOENT from server" |
| 370 | " - marking file deleted and stale"); |
| 371 | set_bit(AFS_VNODE_DELETED, &vnode->flags); |
| 372 | ret = -ESTALE; |
| 373 | } |
| 374 | |
| 375 | for (i = 0; i < req->nr_pages; i++) { |
| 376 | page = req->pages[i]; |
| 377 | if (page) { |
| 378 | #ifdef CONFIG_AFS_FSCACHE |
| 379 | fscache_uncache_page(vnode->cache, page); |
| 380 | #endif |
| 381 | SetPageError(page); |
| 382 | unlock_page(page); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | afs_put_read(req); |
| 387 | return ret; |
| 388 | } |
| 389 | |
| 390 | /* |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 391 | * read a set of pages |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | */ |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 393 | static int afs_readpages(struct file *file, struct address_space *mapping, |
| 394 | struct list_head *pages, unsigned nr_pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | { |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 396 | struct key *key = file->private_data; |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 397 | struct afs_vnode *vnode; |
| 398 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 400 | _enter("{%d},{%lu},,%d", |
| 401 | key_serial(key), mapping->host->i_ino, nr_pages); |
| 402 | |
| 403 | ASSERT(key != NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 405 | vnode = AFS_FS_I(mapping->host); |
Dan Carpenter | ad2a8e6 | 2012-03-20 16:58:06 +0000 | [diff] [blame] | 406 | if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) { |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 407 | _leave(" = -ESTALE"); |
| 408 | return -ESTALE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | } |
| 410 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 411 | /* attempt to read as many of the pages as possible */ |
| 412 | #ifdef CONFIG_AFS_FSCACHE |
| 413 | ret = fscache_read_or_alloc_pages(vnode->cache, |
| 414 | mapping, |
| 415 | pages, |
| 416 | &nr_pages, |
| 417 | afs_file_readpage_read_complete, |
| 418 | NULL, |
| 419 | mapping_gfp_mask(mapping)); |
| 420 | #else |
| 421 | ret = -ENOBUFS; |
| 422 | #endif |
| 423 | |
| 424 | switch (ret) { |
| 425 | /* all pages are being read from the cache */ |
| 426 | case 0: |
| 427 | BUG_ON(!list_empty(pages)); |
| 428 | BUG_ON(nr_pages != 0); |
| 429 | _leave(" = 0 [reading all]"); |
| 430 | return 0; |
| 431 | |
| 432 | /* there were pages that couldn't be read from the cache */ |
| 433 | case -ENODATA: |
| 434 | case -ENOBUFS: |
| 435 | break; |
| 436 | |
| 437 | /* other error */ |
| 438 | default: |
| 439 | _leave(" = %d", ret); |
| 440 | return ret; |
| 441 | } |
| 442 | |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 443 | while (!list_empty(pages)) { |
| 444 | ret = afs_readpages_one(file, mapping, pages); |
| 445 | if (ret < 0) |
| 446 | break; |
| 447 | } |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 448 | |
| 449 | _leave(" = %d [netting]", ret); |
| 450 | return ret; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 451 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | /* |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 454 | * write back a dirty page |
| 455 | */ |
| 456 | static int afs_launder_page(struct page *page) |
| 457 | { |
| 458 | _enter("{%lu}", page->index); |
| 459 | |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | /* |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 464 | * invalidate part or all of a page |
| 465 | * - release a page and clean up its private data if offset is 0 (indicating |
| 466 | * the entire page) |
| 467 | */ |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 468 | static void afs_invalidatepage(struct page *page, unsigned int offset, |
| 469 | unsigned int length) |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 470 | { |
| 471 | struct afs_writeback *wb = (struct afs_writeback *) page_private(page); |
| 472 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 473 | _enter("{%lu},%u,%u", page->index, offset, length); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 474 | |
| 475 | BUG_ON(!PageLocked(page)); |
| 476 | |
| 477 | /* we clean up only if the entire page is being invalidated */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 478 | if (offset == 0 && length == PAGE_SIZE) { |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 479 | #ifdef CONFIG_AFS_FSCACHE |
| 480 | if (PageFsCache(page)) { |
| 481 | struct afs_vnode *vnode = AFS_FS_I(page->mapping->host); |
| 482 | fscache_wait_on_page_write(vnode->cache, page); |
| 483 | fscache_uncache_page(vnode->cache, page); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 484 | } |
| 485 | #endif |
| 486 | |
| 487 | if (PagePrivate(page)) { |
| 488 | if (wb && !PageWriteback(page)) { |
| 489 | set_page_private(page, 0); |
| 490 | afs_put_writeback(wb); |
| 491 | } |
| 492 | |
| 493 | if (!page_private(page)) |
| 494 | ClearPagePrivate(page); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | _leave(""); |
| 499 | } |
| 500 | |
| 501 | /* |
| 502 | * release a page and clean up its private state if it's not busy |
| 503 | * - return true if the page can now be released, false if not |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | */ |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 505 | static int afs_releasepage(struct page *page, gfp_t gfp_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | { |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 507 | struct afs_writeback *wb = (struct afs_writeback *) page_private(page); |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 508 | struct afs_vnode *vnode = AFS_FS_I(page->mapping->host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 510 | _enter("{{%x:%u}[%lu],%lx},%x", |
| 511 | vnode->fid.vid, vnode->fid.vnode, page->index, page->flags, |
| 512 | gfp_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 514 | /* deny if page is being written to the cache and the caller hasn't |
| 515 | * elected to wait */ |
| 516 | #ifdef CONFIG_AFS_FSCACHE |
David Howells | 201a154 | 2009-11-19 18:11:35 +0000 | [diff] [blame] | 517 | if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) { |
| 518 | _leave(" = F [cache busy]"); |
| 519 | return 0; |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 520 | } |
| 521 | #endif |
| 522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | if (PagePrivate(page)) { |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 524 | if (wb) { |
| 525 | set_page_private(page, 0); |
| 526 | afs_put_writeback(wb); |
| 527 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | ClearPagePrivate(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | } |
| 530 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 531 | /* indicate that the page can be released */ |
| 532 | _leave(" = T"); |
| 533 | return 1; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 534 | } |