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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 27 | static int afs_readpages(struct file *filp, struct address_space *mapping, |
| 28 | struct list_head *pages, unsigned nr_pages); |
| 29 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 30 | const struct file_operations afs_file_operations = { |
| 31 | .open = afs_open, |
David Howells | 58fed94 | 2017-03-16 16:27:45 +0000 | [diff] [blame] | 32 | .flush = afs_flush, |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 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, |
David Howells | d3e3b7ea | 2017-07-06 15:50:27 +0100 | [diff] [blame] | 48 | .listxattr = afs_listxattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 51 | const struct address_space_operations afs_fs_aops = { |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 52 | .readpage = afs_readpage, |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 53 | .readpages = afs_readpages, |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 54 | .set_page_dirty = afs_set_page_dirty, |
| 55 | .launder_page = afs_launder_page, |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 56 | .releasepage = afs_releasepage, |
| 57 | .invalidatepage = afs_invalidatepage, |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 58 | .write_begin = afs_write_begin, |
| 59 | .write_end = afs_write_end, |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 60 | .writepage = afs_writepage, |
| 61 | .writepages = afs_writepages, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | /* |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame^] | 65 | * Discard a pin on a writeback key. |
| 66 | */ |
| 67 | void afs_put_wb_key(struct afs_wb_key *wbk) |
| 68 | { |
| 69 | if (refcount_dec_and_test(&wbk->usage)) { |
| 70 | key_put(wbk->key); |
| 71 | kfree(wbk); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * Cache key for writeback. |
| 77 | */ |
| 78 | int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af) |
| 79 | { |
| 80 | struct afs_wb_key *wbk, *p; |
| 81 | |
| 82 | wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL); |
| 83 | if (!wbk) |
| 84 | return -ENOMEM; |
| 85 | refcount_set(&wbk->usage, 2); |
| 86 | wbk->key = af->key; |
| 87 | |
| 88 | spin_lock(&vnode->wb_lock); |
| 89 | list_for_each_entry(p, &vnode->wb_keys, vnode_link) { |
| 90 | if (p->key == wbk->key) |
| 91 | goto found; |
| 92 | } |
| 93 | |
| 94 | key_get(wbk->key); |
| 95 | list_add_tail(&wbk->vnode_link, &vnode->wb_keys); |
| 96 | spin_unlock(&vnode->wb_lock); |
| 97 | af->wb = wbk; |
| 98 | return 0; |
| 99 | |
| 100 | found: |
| 101 | refcount_inc(&p->usage); |
| 102 | spin_unlock(&vnode->wb_lock); |
| 103 | af->wb = p; |
| 104 | kfree(wbk); |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | /* |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 109 | * open an AFS file or directory and attach a key to it |
| 110 | */ |
| 111 | int afs_open(struct inode *inode, struct file *file) |
| 112 | { |
| 113 | struct afs_vnode *vnode = AFS_FS_I(inode); |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 114 | struct afs_file *af; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 115 | struct key *key; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 116 | int ret; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 117 | |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 118 | _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 119 | |
| 120 | key = afs_request_key(vnode->volume->cell); |
| 121 | if (IS_ERR(key)) { |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 122 | ret = PTR_ERR(key); |
| 123 | goto error; |
| 124 | } |
| 125 | |
| 126 | af = kzalloc(sizeof(*af), GFP_KERNEL); |
| 127 | if (!af) { |
| 128 | ret = -ENOMEM; |
| 129 | goto error_key; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 130 | } |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame^] | 131 | af->key = key; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 132 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 133 | ret = afs_validate(vnode, key); |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 134 | if (ret < 0) |
| 135 | goto error_af; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 136 | |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame^] | 137 | if (file->f_mode & FMODE_WRITE) { |
| 138 | ret = afs_cache_wb_key(vnode, af); |
| 139 | if (ret < 0) |
| 140 | goto error_af; |
| 141 | } |
| 142 | |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 143 | file->private_data = af; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 144 | _leave(" = 0"); |
| 145 | return 0; |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 146 | |
| 147 | error_af: |
| 148 | kfree(af); |
| 149 | error_key: |
| 150 | key_put(key); |
| 151 | error: |
| 152 | _leave(" = %d", ret); |
| 153 | return ret; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /* |
| 157 | * release an AFS file or directory and discard its key |
| 158 | */ |
| 159 | int afs_release(struct inode *inode, struct file *file) |
| 160 | { |
| 161 | struct afs_vnode *vnode = AFS_FS_I(inode); |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 162 | struct afs_file *af = file->private_data; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 163 | |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 164 | _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 165 | |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 166 | file->private_data = NULL; |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame^] | 167 | if (af->wb) |
| 168 | afs_put_wb_key(af->wb); |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 169 | key_put(af->key); |
| 170 | kfree(af); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame^] | 171 | afs_prune_wb_keys(vnode); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 172 | _leave(" = 0"); |
| 173 | return 0; |
| 174 | } |
| 175 | |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 176 | /* |
| 177 | * Dispose of a ref to a read record. |
| 178 | */ |
| 179 | void afs_put_read(struct afs_read *req) |
| 180 | { |
| 181 | int i; |
| 182 | |
| 183 | if (atomic_dec_and_test(&req->usage)) { |
| 184 | for (i = 0; i < req->nr_pages; i++) |
| 185 | if (req->pages[i]) |
| 186 | put_page(req->pages[i]); |
| 187 | kfree(req); |
| 188 | } |
| 189 | } |
| 190 | |
Matt Kraai | 6566abd | 2009-04-17 12:56:38 +0100 | [diff] [blame] | 191 | #ifdef CONFIG_AFS_FSCACHE |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 192 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | * deal with notification that a page was read from the cache |
| 194 | */ |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 195 | static void afs_file_readpage_read_complete(struct page *page, |
| 196 | void *data, |
| 197 | int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 199 | _enter("%p,%p,%d", page, data, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 201 | /* if the read completes with an error, we just unlock the page and let |
| 202 | * the VM reissue the readpage */ |
| 203 | if (!error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | SetPageUptodate(page); |
| 205 | unlock_page(page); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 206 | } |
Matt Kraai | 6566abd | 2009-04-17 12:56:38 +0100 | [diff] [blame] | 207 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 210 | * Fetch file data from the volume. |
| 211 | */ |
| 212 | int afs_fetch_data(struct afs_vnode *vnode, struct key *key, struct afs_read *desc) |
| 213 | { |
| 214 | struct afs_fs_cursor fc; |
| 215 | int ret; |
| 216 | |
| 217 | _enter("%s{%x:%u.%u},%x,,,", |
| 218 | vnode->volume->name, |
| 219 | vnode->fid.vid, |
| 220 | vnode->fid.vnode, |
| 221 | vnode->fid.unique, |
| 222 | key_serial(key)); |
| 223 | |
| 224 | ret = -ERESTARTSYS; |
| 225 | if (afs_begin_vnode_operation(&fc, vnode, key)) { |
| 226 | while (afs_select_fileserver(&fc)) { |
| 227 | fc.cb_break = vnode->cb_break + vnode->cb_s_break; |
| 228 | afs_fs_fetch_data(&fc, desc); |
| 229 | } |
| 230 | |
| 231 | afs_check_for_remote_deletion(&fc, fc.vnode); |
| 232 | afs_vnode_commit_status(&fc, vnode, fc.cb_break); |
| 233 | ret = afs_end_vnode_operation(&fc); |
| 234 | } |
| 235 | |
| 236 | _leave(" = %d", ret); |
| 237 | return ret; |
| 238 | } |
| 239 | |
| 240 | /* |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 241 | * read page from file, directory or symlink, given a key to use |
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 | int afs_page_filler(void *data, struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | { |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 245 | struct inode *inode = page->mapping->host; |
| 246 | struct afs_vnode *vnode = AFS_FS_I(inode); |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 247 | struct afs_read *req; |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 248 | struct key *key = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | int ret; |
| 250 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 251 | _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
Matt Mackall | cd7619d | 2005-05-01 08:59:01 -0700 | [diff] [blame] | 253 | BUG_ON(!PageLocked(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
| 255 | ret = -ESTALE; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 256 | if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | goto error; |
| 258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | /* is it cached? */ |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 260 | #ifdef CONFIG_AFS_FSCACHE |
| 261 | ret = fscache_read_or_alloc_page(vnode->cache, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | page, |
| 263 | afs_file_readpage_read_complete, |
| 264 | NULL, |
| 265 | GFP_KERNEL); |
| 266 | #else |
| 267 | ret = -ENOBUFS; |
| 268 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | switch (ret) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | /* read BIO submitted (page in cache) */ |
| 271 | case 0: |
| 272 | break; |
| 273 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 274 | /* page not yet cached */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | case -ENODATA: |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 276 | _debug("cache said ENODATA"); |
| 277 | goto go_on; |
| 278 | |
| 279 | /* page will not be cached */ |
| 280 | case -ENOBUFS: |
| 281 | _debug("cache said ENOBUFS"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | default: |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 283 | go_on: |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 284 | req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *), |
| 285 | GFP_KERNEL); |
| 286 | if (!req) |
| 287 | goto enomem; |
| 288 | |
David Howells | 6db3ac3 | 2017-03-16 16:27:44 +0000 | [diff] [blame] | 289 | /* We request a full page. If the page is a partial one at the |
| 290 | * end of the file, the server will return a short read and the |
| 291 | * unmarshalling code will clear the unfilled space. |
| 292 | */ |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 293 | atomic_set(&req->usage, 1); |
| 294 | req->pos = (loff_t)page->index << PAGE_SHIFT; |
David Howells | 6db3ac3 | 2017-03-16 16:27:44 +0000 | [diff] [blame] | 295 | req->len = PAGE_SIZE; |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 296 | req->nr_pages = 1; |
| 297 | req->pages[0] = page; |
| 298 | get_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
| 300 | /* read the contents of the file from the server into the |
| 301 | * page */ |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 302 | ret = afs_fetch_data(vnode, key, req); |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 303 | afs_put_read(req); |
David Howells | dab17c1 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 304 | |
| 305 | if (ret >= 0 && S_ISDIR(inode->i_mode) && |
| 306 | !afs_dir_check_page(inode, page)) |
| 307 | ret = -EIO; |
| 308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | if (ret < 0) { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 310 | if (ret == -ENOENT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | _debug("got NOENT from server" |
| 312 | " - marking file deleted and stale"); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 313 | set_bit(AFS_VNODE_DELETED, &vnode->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | ret = -ESTALE; |
| 315 | } |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 316 | |
| 317 | #ifdef CONFIG_AFS_FSCACHE |
| 318 | fscache_uncache_page(vnode->cache, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | #endif |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 320 | BUG_ON(PageFsCache(page)); |
David Howells | 68ae849 | 2017-03-16 16:27:48 +0000 | [diff] [blame] | 321 | |
| 322 | if (ret == -EINTR || |
| 323 | ret == -ENOMEM || |
| 324 | ret == -ERESTARTSYS || |
| 325 | ret == -EAGAIN) |
| 326 | goto error; |
| 327 | goto io_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | SetPageUptodate(page); |
| 331 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 332 | /* send the page to the cache */ |
| 333 | #ifdef CONFIG_AFS_FSCACHE |
| 334 | if (PageFsCache(page) && |
| 335 | fscache_write_page(vnode->cache, page, GFP_KERNEL) != 0) { |
| 336 | fscache_uncache_page(vnode->cache, page); |
| 337 | BUG_ON(PageFsCache(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | #endif |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 340 | unlock_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | _leave(" = 0"); |
| 344 | return 0; |
| 345 | |
David Howells | 68ae849 | 2017-03-16 16:27:48 +0000 | [diff] [blame] | 346 | io_error: |
| 347 | SetPageError(page); |
| 348 | goto error; |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 349 | enomem: |
| 350 | ret = -ENOMEM; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 351 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | unlock_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | _leave(" = %d", ret); |
| 354 | return ret; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 355 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | /* |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 358 | * read page from file, directory or symlink, given a file to nominate the key |
| 359 | * to be used |
| 360 | */ |
| 361 | static int afs_readpage(struct file *file, struct page *page) |
| 362 | { |
| 363 | struct key *key; |
| 364 | int ret; |
| 365 | |
| 366 | if (file) { |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 367 | key = afs_file_key(file); |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 368 | ASSERT(key != NULL); |
| 369 | ret = afs_page_filler(key, page); |
| 370 | } else { |
| 371 | struct inode *inode = page->mapping->host; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 372 | key = afs_request_key(AFS_FS_S(inode->i_sb)->cell); |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 373 | if (IS_ERR(key)) { |
| 374 | ret = PTR_ERR(key); |
| 375 | } else { |
| 376 | ret = afs_page_filler(key, page); |
| 377 | key_put(key); |
| 378 | } |
| 379 | } |
| 380 | return ret; |
| 381 | } |
| 382 | |
| 383 | /* |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 384 | * Make pages available as they're filled. |
| 385 | */ |
| 386 | static void afs_readpages_page_done(struct afs_call *call, struct afs_read *req) |
| 387 | { |
Arnd Bergmann | 51c89e6 | 2017-01-13 14:46:19 +0000 | [diff] [blame] | 388 | #ifdef CONFIG_AFS_FSCACHE |
David Howells | 97e3043 | 2017-11-02 15:27:48 +0000 | [diff] [blame] | 389 | struct afs_vnode *vnode = call->reply[0]; |
Arnd Bergmann | 51c89e6 | 2017-01-13 14:46:19 +0000 | [diff] [blame] | 390 | #endif |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 391 | struct page *page = req->pages[req->index]; |
| 392 | |
| 393 | req->pages[req->index] = NULL; |
| 394 | SetPageUptodate(page); |
| 395 | |
| 396 | /* send the page to the cache */ |
| 397 | #ifdef CONFIG_AFS_FSCACHE |
| 398 | if (PageFsCache(page) && |
| 399 | fscache_write_page(vnode->cache, page, GFP_KERNEL) != 0) { |
| 400 | fscache_uncache_page(vnode->cache, page); |
| 401 | BUG_ON(PageFsCache(page)); |
| 402 | } |
| 403 | #endif |
| 404 | unlock_page(page); |
| 405 | put_page(page); |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * Read a contiguous set of pages. |
| 410 | */ |
| 411 | static int afs_readpages_one(struct file *file, struct address_space *mapping, |
| 412 | struct list_head *pages) |
| 413 | { |
| 414 | struct afs_vnode *vnode = AFS_FS_I(mapping->host); |
| 415 | struct afs_read *req; |
| 416 | struct list_head *p; |
| 417 | struct page *first, *page; |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 418 | struct key *key = afs_file_key(file); |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 419 | pgoff_t index; |
| 420 | int ret, n, i; |
| 421 | |
| 422 | /* Count the number of contiguous pages at the front of the list. Note |
| 423 | * that the list goes prev-wards rather than next-wards. |
| 424 | */ |
| 425 | first = list_entry(pages->prev, struct page, lru); |
| 426 | index = first->index + 1; |
| 427 | n = 1; |
| 428 | for (p = first->lru.prev; p != pages; p = p->prev) { |
| 429 | page = list_entry(p, struct page, lru); |
| 430 | if (page->index != index) |
| 431 | break; |
| 432 | index++; |
| 433 | n++; |
| 434 | } |
| 435 | |
| 436 | req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *) * n, |
| 437 | GFP_NOFS); |
| 438 | if (!req) |
| 439 | return -ENOMEM; |
| 440 | |
| 441 | atomic_set(&req->usage, 1); |
| 442 | req->page_done = afs_readpages_page_done; |
| 443 | req->pos = first->index; |
| 444 | req->pos <<= PAGE_SHIFT; |
| 445 | |
| 446 | /* Transfer the pages to the request. We add them in until one fails |
| 447 | * to add to the LRU and then we stop (as that'll make a hole in the |
| 448 | * contiguous run. |
| 449 | * |
| 450 | * Note that it's possible for the file size to change whilst we're |
| 451 | * doing this, but we rely on the server returning less than we asked |
| 452 | * for if the file shrank. We also rely on this to deal with a partial |
| 453 | * page at the end of the file. |
| 454 | */ |
| 455 | do { |
| 456 | page = list_entry(pages->prev, struct page, lru); |
| 457 | list_del(&page->lru); |
| 458 | index = page->index; |
| 459 | if (add_to_page_cache_lru(page, mapping, index, |
| 460 | readahead_gfp_mask(mapping))) { |
| 461 | #ifdef CONFIG_AFS_FSCACHE |
| 462 | fscache_uncache_page(vnode->cache, page); |
| 463 | #endif |
| 464 | put_page(page); |
| 465 | break; |
| 466 | } |
| 467 | |
| 468 | req->pages[req->nr_pages++] = page; |
| 469 | req->len += PAGE_SIZE; |
| 470 | } while (req->nr_pages < n); |
| 471 | |
| 472 | if (req->nr_pages == 0) { |
| 473 | kfree(req); |
| 474 | return 0; |
| 475 | } |
| 476 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 477 | ret = afs_fetch_data(vnode, key, req); |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 478 | if (ret < 0) |
| 479 | goto error; |
| 480 | |
| 481 | task_io_account_read(PAGE_SIZE * req->nr_pages); |
| 482 | afs_put_read(req); |
| 483 | return 0; |
| 484 | |
| 485 | error: |
| 486 | if (ret == -ENOENT) { |
| 487 | _debug("got NOENT from server" |
| 488 | " - marking file deleted and stale"); |
| 489 | set_bit(AFS_VNODE_DELETED, &vnode->flags); |
| 490 | ret = -ESTALE; |
| 491 | } |
| 492 | |
| 493 | for (i = 0; i < req->nr_pages; i++) { |
| 494 | page = req->pages[i]; |
| 495 | if (page) { |
| 496 | #ifdef CONFIG_AFS_FSCACHE |
| 497 | fscache_uncache_page(vnode->cache, page); |
| 498 | #endif |
| 499 | SetPageError(page); |
| 500 | unlock_page(page); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | afs_put_read(req); |
| 505 | return ret; |
| 506 | } |
| 507 | |
| 508 | /* |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 509 | * read a set of pages |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | */ |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 511 | static int afs_readpages(struct file *file, struct address_space *mapping, |
| 512 | struct list_head *pages, unsigned nr_pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | { |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 514 | struct key *key = afs_file_key(file); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 515 | struct afs_vnode *vnode; |
| 516 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 518 | _enter("{%d},{%lu},,%d", |
| 519 | key_serial(key), mapping->host->i_ino, nr_pages); |
| 520 | |
| 521 | ASSERT(key != NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 523 | vnode = AFS_FS_I(mapping->host); |
Dan Carpenter | ad2a8e6 | 2012-03-20 16:58:06 +0000 | [diff] [blame] | 524 | if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) { |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 525 | _leave(" = -ESTALE"); |
| 526 | return -ESTALE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | } |
| 528 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 529 | /* attempt to read as many of the pages as possible */ |
| 530 | #ifdef CONFIG_AFS_FSCACHE |
| 531 | ret = fscache_read_or_alloc_pages(vnode->cache, |
| 532 | mapping, |
| 533 | pages, |
| 534 | &nr_pages, |
| 535 | afs_file_readpage_read_complete, |
| 536 | NULL, |
| 537 | mapping_gfp_mask(mapping)); |
| 538 | #else |
| 539 | ret = -ENOBUFS; |
| 540 | #endif |
| 541 | |
| 542 | switch (ret) { |
| 543 | /* all pages are being read from the cache */ |
| 544 | case 0: |
| 545 | BUG_ON(!list_empty(pages)); |
| 546 | BUG_ON(nr_pages != 0); |
| 547 | _leave(" = 0 [reading all]"); |
| 548 | return 0; |
| 549 | |
| 550 | /* there were pages that couldn't be read from the cache */ |
| 551 | case -ENODATA: |
| 552 | case -ENOBUFS: |
| 553 | break; |
| 554 | |
| 555 | /* other error */ |
| 556 | default: |
| 557 | _leave(" = %d", ret); |
| 558 | return ret; |
| 559 | } |
| 560 | |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 561 | while (!list_empty(pages)) { |
| 562 | ret = afs_readpages_one(file, mapping, pages); |
| 563 | if (ret < 0) |
| 564 | break; |
| 565 | } |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 566 | |
| 567 | _leave(" = %d [netting]", ret); |
| 568 | return ret; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 569 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | /* |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 572 | * invalidate part or all of a page |
| 573 | * - release a page and clean up its private data if offset is 0 (indicating |
| 574 | * the entire page) |
| 575 | */ |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 576 | static void afs_invalidatepage(struct page *page, unsigned int offset, |
| 577 | unsigned int length) |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 578 | { |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 579 | _enter("{%lu},%u,%u", page->index, offset, length); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 580 | |
| 581 | BUG_ON(!PageLocked(page)); |
| 582 | |
| 583 | /* we clean up only if the entire page is being invalidated */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 584 | if (offset == 0 && length == PAGE_SIZE) { |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 585 | #ifdef CONFIG_AFS_FSCACHE |
| 586 | if (PageFsCache(page)) { |
| 587 | struct afs_vnode *vnode = AFS_FS_I(page->mapping->host); |
| 588 | fscache_wait_on_page_write(vnode->cache, page); |
| 589 | fscache_uncache_page(vnode->cache, page); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 590 | } |
| 591 | #endif |
| 592 | |
| 593 | if (PagePrivate(page)) { |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame^] | 594 | set_page_private(page, 0); |
| 595 | ClearPagePrivate(page); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | |
| 599 | _leave(""); |
| 600 | } |
| 601 | |
| 602 | /* |
| 603 | * release a page and clean up its private state if it's not busy |
| 604 | * - return true if the page can now be released, false if not |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | */ |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 606 | static int afs_releasepage(struct page *page, gfp_t gfp_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | { |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 608 | struct afs_vnode *vnode = AFS_FS_I(page->mapping->host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 610 | _enter("{{%x:%u}[%lu],%lx},%x", |
| 611 | vnode->fid.vid, vnode->fid.vnode, page->index, page->flags, |
| 612 | gfp_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 614 | /* deny if page is being written to the cache and the caller hasn't |
| 615 | * elected to wait */ |
| 616 | #ifdef CONFIG_AFS_FSCACHE |
David Howells | 201a154 | 2009-11-19 18:11:35 +0000 | [diff] [blame] | 617 | if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) { |
| 618 | _leave(" = F [cache busy]"); |
| 619 | return 0; |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 620 | } |
| 621 | #endif |
| 622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | if (PagePrivate(page)) { |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame^] | 624 | set_page_private(page, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | ClearPagePrivate(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | } |
| 627 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 628 | /* indicate that the page can be released */ |
| 629 | _leave(" = T"); |
| 630 | return 1; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 631 | } |