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 | |
David Howells | 6db3ac3 | 2017-03-16 16:27:44 +0000 | [diff] [blame^] | 187 | /* We request a full page. If the page is a partial one at the |
| 188 | * end of the file, the server will return a short read and the |
| 189 | * unmarshalling code will clear the unfilled space. |
| 190 | */ |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 191 | atomic_set(&req->usage, 1); |
| 192 | req->pos = (loff_t)page->index << PAGE_SHIFT; |
David Howells | 6db3ac3 | 2017-03-16 16:27:44 +0000 | [diff] [blame^] | 193 | req->len = PAGE_SIZE; |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 194 | req->nr_pages = 1; |
| 195 | req->pages[0] = page; |
| 196 | get_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
| 198 | /* read the contents of the file from the server into the |
| 199 | * page */ |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 200 | ret = afs_vnode_fetch_data(vnode, key, req); |
| 201 | afs_put_read(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | if (ret < 0) { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 203 | if (ret == -ENOENT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | _debug("got NOENT from server" |
| 205 | " - marking file deleted and stale"); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 206 | set_bit(AFS_VNODE_DELETED, &vnode->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | ret = -ESTALE; |
| 208 | } |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 209 | |
| 210 | #ifdef CONFIG_AFS_FSCACHE |
| 211 | fscache_uncache_page(vnode->cache, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | #endif |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 213 | BUG_ON(PageFsCache(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | goto error; |
| 215 | } |
| 216 | |
| 217 | SetPageUptodate(page); |
| 218 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 219 | /* send the page to the cache */ |
| 220 | #ifdef CONFIG_AFS_FSCACHE |
| 221 | if (PageFsCache(page) && |
| 222 | fscache_write_page(vnode->cache, page, GFP_KERNEL) != 0) { |
| 223 | fscache_uncache_page(vnode->cache, page); |
| 224 | BUG_ON(PageFsCache(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | #endif |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 227 | unlock_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | _leave(" = 0"); |
| 231 | return 0; |
| 232 | |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 233 | enomem: |
| 234 | ret = -ENOMEM; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 235 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | SetPageError(page); |
| 237 | unlock_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | _leave(" = %d", ret); |
| 239 | return ret; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 240 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | /* |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 243 | * read page from file, directory or symlink, given a file to nominate the key |
| 244 | * to be used |
| 245 | */ |
| 246 | static int afs_readpage(struct file *file, struct page *page) |
| 247 | { |
| 248 | struct key *key; |
| 249 | int ret; |
| 250 | |
| 251 | if (file) { |
| 252 | key = file->private_data; |
| 253 | ASSERT(key != NULL); |
| 254 | ret = afs_page_filler(key, page); |
| 255 | } else { |
| 256 | struct inode *inode = page->mapping->host; |
| 257 | key = afs_request_key(AFS_FS_S(inode->i_sb)->volume->cell); |
| 258 | if (IS_ERR(key)) { |
| 259 | ret = PTR_ERR(key); |
| 260 | } else { |
| 261 | ret = afs_page_filler(key, page); |
| 262 | key_put(key); |
| 263 | } |
| 264 | } |
| 265 | return ret; |
| 266 | } |
| 267 | |
| 268 | /* |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 269 | * Make pages available as they're filled. |
| 270 | */ |
| 271 | static void afs_readpages_page_done(struct afs_call *call, struct afs_read *req) |
| 272 | { |
Arnd Bergmann | 51c89e6 | 2017-01-13 14:46:19 +0000 | [diff] [blame] | 273 | #ifdef CONFIG_AFS_FSCACHE |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 274 | struct afs_vnode *vnode = call->reply; |
Arnd Bergmann | 51c89e6 | 2017-01-13 14:46:19 +0000 | [diff] [blame] | 275 | #endif |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 276 | struct page *page = req->pages[req->index]; |
| 277 | |
| 278 | req->pages[req->index] = NULL; |
| 279 | SetPageUptodate(page); |
| 280 | |
| 281 | /* send the page to the cache */ |
| 282 | #ifdef CONFIG_AFS_FSCACHE |
| 283 | if (PageFsCache(page) && |
| 284 | fscache_write_page(vnode->cache, page, GFP_KERNEL) != 0) { |
| 285 | fscache_uncache_page(vnode->cache, page); |
| 286 | BUG_ON(PageFsCache(page)); |
| 287 | } |
| 288 | #endif |
| 289 | unlock_page(page); |
| 290 | put_page(page); |
| 291 | } |
| 292 | |
| 293 | /* |
| 294 | * Read a contiguous set of pages. |
| 295 | */ |
| 296 | static int afs_readpages_one(struct file *file, struct address_space *mapping, |
| 297 | struct list_head *pages) |
| 298 | { |
| 299 | struct afs_vnode *vnode = AFS_FS_I(mapping->host); |
| 300 | struct afs_read *req; |
| 301 | struct list_head *p; |
| 302 | struct page *first, *page; |
| 303 | struct key *key = file->private_data; |
| 304 | pgoff_t index; |
| 305 | int ret, n, i; |
| 306 | |
| 307 | /* Count the number of contiguous pages at the front of the list. Note |
| 308 | * that the list goes prev-wards rather than next-wards. |
| 309 | */ |
| 310 | first = list_entry(pages->prev, struct page, lru); |
| 311 | index = first->index + 1; |
| 312 | n = 1; |
| 313 | for (p = first->lru.prev; p != pages; p = p->prev) { |
| 314 | page = list_entry(p, struct page, lru); |
| 315 | if (page->index != index) |
| 316 | break; |
| 317 | index++; |
| 318 | n++; |
| 319 | } |
| 320 | |
| 321 | req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *) * n, |
| 322 | GFP_NOFS); |
| 323 | if (!req) |
| 324 | return -ENOMEM; |
| 325 | |
| 326 | atomic_set(&req->usage, 1); |
| 327 | req->page_done = afs_readpages_page_done; |
| 328 | req->pos = first->index; |
| 329 | req->pos <<= PAGE_SHIFT; |
| 330 | |
| 331 | /* Transfer the pages to the request. We add them in until one fails |
| 332 | * to add to the LRU and then we stop (as that'll make a hole in the |
| 333 | * contiguous run. |
| 334 | * |
| 335 | * Note that it's possible for the file size to change whilst we're |
| 336 | * doing this, but we rely on the server returning less than we asked |
| 337 | * for if the file shrank. We also rely on this to deal with a partial |
| 338 | * page at the end of the file. |
| 339 | */ |
| 340 | do { |
| 341 | page = list_entry(pages->prev, struct page, lru); |
| 342 | list_del(&page->lru); |
| 343 | index = page->index; |
| 344 | if (add_to_page_cache_lru(page, mapping, index, |
| 345 | readahead_gfp_mask(mapping))) { |
| 346 | #ifdef CONFIG_AFS_FSCACHE |
| 347 | fscache_uncache_page(vnode->cache, page); |
| 348 | #endif |
| 349 | put_page(page); |
| 350 | break; |
| 351 | } |
| 352 | |
| 353 | req->pages[req->nr_pages++] = page; |
| 354 | req->len += PAGE_SIZE; |
| 355 | } while (req->nr_pages < n); |
| 356 | |
| 357 | if (req->nr_pages == 0) { |
| 358 | kfree(req); |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | ret = afs_vnode_fetch_data(vnode, key, req); |
| 363 | if (ret < 0) |
| 364 | goto error; |
| 365 | |
| 366 | task_io_account_read(PAGE_SIZE * req->nr_pages); |
| 367 | afs_put_read(req); |
| 368 | return 0; |
| 369 | |
| 370 | error: |
| 371 | if (ret == -ENOENT) { |
| 372 | _debug("got NOENT from server" |
| 373 | " - marking file deleted and stale"); |
| 374 | set_bit(AFS_VNODE_DELETED, &vnode->flags); |
| 375 | ret = -ESTALE; |
| 376 | } |
| 377 | |
| 378 | for (i = 0; i < req->nr_pages; i++) { |
| 379 | page = req->pages[i]; |
| 380 | if (page) { |
| 381 | #ifdef CONFIG_AFS_FSCACHE |
| 382 | fscache_uncache_page(vnode->cache, page); |
| 383 | #endif |
| 384 | SetPageError(page); |
| 385 | unlock_page(page); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | afs_put_read(req); |
| 390 | return ret; |
| 391 | } |
| 392 | |
| 393 | /* |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 394 | * read a set of pages |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | */ |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 396 | static int afs_readpages(struct file *file, struct address_space *mapping, |
| 397 | struct list_head *pages, unsigned nr_pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | { |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 399 | struct key *key = file->private_data; |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 400 | struct afs_vnode *vnode; |
| 401 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 403 | _enter("{%d},{%lu},,%d", |
| 404 | key_serial(key), mapping->host->i_ino, nr_pages); |
| 405 | |
| 406 | ASSERT(key != NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 408 | vnode = AFS_FS_I(mapping->host); |
Dan Carpenter | ad2a8e6 | 2012-03-20 16:58:06 +0000 | [diff] [blame] | 409 | if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) { |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 410 | _leave(" = -ESTALE"); |
| 411 | return -ESTALE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | } |
| 413 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 414 | /* attempt to read as many of the pages as possible */ |
| 415 | #ifdef CONFIG_AFS_FSCACHE |
| 416 | ret = fscache_read_or_alloc_pages(vnode->cache, |
| 417 | mapping, |
| 418 | pages, |
| 419 | &nr_pages, |
| 420 | afs_file_readpage_read_complete, |
| 421 | NULL, |
| 422 | mapping_gfp_mask(mapping)); |
| 423 | #else |
| 424 | ret = -ENOBUFS; |
| 425 | #endif |
| 426 | |
| 427 | switch (ret) { |
| 428 | /* all pages are being read from the cache */ |
| 429 | case 0: |
| 430 | BUG_ON(!list_empty(pages)); |
| 431 | BUG_ON(nr_pages != 0); |
| 432 | _leave(" = 0 [reading all]"); |
| 433 | return 0; |
| 434 | |
| 435 | /* there were pages that couldn't be read from the cache */ |
| 436 | case -ENODATA: |
| 437 | case -ENOBUFS: |
| 438 | break; |
| 439 | |
| 440 | /* other error */ |
| 441 | default: |
| 442 | _leave(" = %d", ret); |
| 443 | return ret; |
| 444 | } |
| 445 | |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 446 | while (!list_empty(pages)) { |
| 447 | ret = afs_readpages_one(file, mapping, pages); |
| 448 | if (ret < 0) |
| 449 | break; |
| 450 | } |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 451 | |
| 452 | _leave(" = %d [netting]", ret); |
| 453 | return ret; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 454 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | /* |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 457 | * write back a dirty page |
| 458 | */ |
| 459 | static int afs_launder_page(struct page *page) |
| 460 | { |
| 461 | _enter("{%lu}", page->index); |
| 462 | |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | /* |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 467 | * invalidate part or all of a page |
| 468 | * - release a page and clean up its private data if offset is 0 (indicating |
| 469 | * the entire page) |
| 470 | */ |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 471 | static void afs_invalidatepage(struct page *page, unsigned int offset, |
| 472 | unsigned int length) |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 473 | { |
| 474 | struct afs_writeback *wb = (struct afs_writeback *) page_private(page); |
| 475 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 476 | _enter("{%lu},%u,%u", page->index, offset, length); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 477 | |
| 478 | BUG_ON(!PageLocked(page)); |
| 479 | |
| 480 | /* we clean up only if the entire page is being invalidated */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 481 | if (offset == 0 && length == PAGE_SIZE) { |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 482 | #ifdef CONFIG_AFS_FSCACHE |
| 483 | if (PageFsCache(page)) { |
| 484 | struct afs_vnode *vnode = AFS_FS_I(page->mapping->host); |
| 485 | fscache_wait_on_page_write(vnode->cache, page); |
| 486 | fscache_uncache_page(vnode->cache, page); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 487 | } |
| 488 | #endif |
| 489 | |
| 490 | if (PagePrivate(page)) { |
| 491 | if (wb && !PageWriteback(page)) { |
| 492 | set_page_private(page, 0); |
| 493 | afs_put_writeback(wb); |
| 494 | } |
| 495 | |
| 496 | if (!page_private(page)) |
| 497 | ClearPagePrivate(page); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | _leave(""); |
| 502 | } |
| 503 | |
| 504 | /* |
| 505 | * release a page and clean up its private state if it's not busy |
| 506 | * - return true if the page can now be released, false if not |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | */ |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 508 | static int afs_releasepage(struct page *page, gfp_t gfp_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | { |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 510 | struct afs_writeback *wb = (struct afs_writeback *) page_private(page); |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 511 | struct afs_vnode *vnode = AFS_FS_I(page->mapping->host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 513 | _enter("{{%x:%u}[%lu],%lx},%x", |
| 514 | vnode->fid.vid, vnode->fid.vnode, page->index, page->flags, |
| 515 | gfp_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 517 | /* deny if page is being written to the cache and the caller hasn't |
| 518 | * elected to wait */ |
| 519 | #ifdef CONFIG_AFS_FSCACHE |
David Howells | 201a154 | 2009-11-19 18:11:35 +0000 | [diff] [blame] | 520 | if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) { |
| 521 | _leave(" = F [cache busy]"); |
| 522 | return 0; |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 523 | } |
| 524 | #endif |
| 525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | if (PagePrivate(page)) { |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 527 | if (wb) { |
| 528 | set_page_private(page, 0); |
| 529 | afs_put_writeback(wb); |
| 530 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | ClearPagePrivate(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | } |
| 533 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 534 | /* indicate that the page can be released */ |
| 535 | _leave(" = T"); |
| 536 | return 1; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 537 | } |