Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 2 | /* AFS filesystem file handling |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 4 | * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Written by David Howells (dhowells@redhat.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/fs.h> |
| 12 | #include <linux/pagemap.h> |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 13 | #include <linux/writeback.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/gfp.h> |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 15 | #include <linux/task_io_accounting_ops.h> |
Nikolay Borisov | f86196e | 2019-01-03 15:29:02 -0800 | [diff] [blame] | 16 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include "internal.h" |
| 18 | |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 19 | static int afs_file_mmap(struct file *file, struct vm_area_struct *vma); |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 20 | static int afs_readpage(struct file *file, struct page *page); |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 21 | static void afs_invalidatepage(struct page *page, unsigned int offset, |
| 22 | unsigned int length); |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 23 | static int afs_releasepage(struct page *page, gfp_t gfp_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 25 | static int afs_readpages(struct file *filp, struct address_space *mapping, |
| 26 | struct list_head *pages, unsigned nr_pages); |
| 27 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 28 | const struct file_operations afs_file_operations = { |
| 29 | .open = afs_open, |
| 30 | .release = afs_release, |
| 31 | .llseek = generic_file_llseek, |
Al Viro | aad4f8b | 2014-04-02 14:33:16 -0400 | [diff] [blame] | 32 | .read_iter = generic_file_read_iter, |
Al Viro | 50b5551 | 2014-04-03 14:13:46 -0400 | [diff] [blame] | 33 | .write_iter = afs_file_write, |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 34 | .mmap = afs_file_mmap, |
Jens Axboe | 5ffc4ef | 2007-06-01 11:49:19 +0200 | [diff] [blame] | 35 | .splice_read = generic_file_splice_read, |
David Howells | 06a17bb | 2020-10-27 09:39:04 +0000 | [diff] [blame] | 36 | .splice_write = iter_file_splice_write, |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 37 | .fsync = afs_fsync, |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 38 | .lock = afs_lock, |
| 39 | .flock = afs_flock, |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 42 | const struct inode_operations afs_file_inode_operations = { |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 43 | .getattr = afs_getattr, |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 44 | .setattr = afs_setattr, |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 45 | .permission = afs_permission, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 48 | const struct address_space_operations afs_fs_aops = { |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 49 | .readpage = afs_readpage, |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 50 | .readpages = afs_readpages, |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 51 | .set_page_dirty = afs_set_page_dirty, |
| 52 | .launder_page = afs_launder_page, |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 53 | .releasepage = afs_releasepage, |
| 54 | .invalidatepage = afs_invalidatepage, |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 55 | .write_begin = afs_write_begin, |
| 56 | .write_end = afs_write_end, |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 57 | .writepage = afs_writepage, |
| 58 | .writepages = afs_writepages, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 61 | static const struct vm_operations_struct afs_vm_ops = { |
| 62 | .fault = filemap_fault, |
| 63 | .map_pages = filemap_map_pages, |
| 64 | .page_mkwrite = afs_page_mkwrite, |
| 65 | }; |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | /* |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 68 | * Discard a pin on a writeback key. |
| 69 | */ |
| 70 | void afs_put_wb_key(struct afs_wb_key *wbk) |
| 71 | { |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 72 | if (wbk && refcount_dec_and_test(&wbk->usage)) { |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 73 | key_put(wbk->key); |
| 74 | kfree(wbk); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Cache key for writeback. |
| 80 | */ |
| 81 | int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af) |
| 82 | { |
| 83 | struct afs_wb_key *wbk, *p; |
| 84 | |
| 85 | wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL); |
| 86 | if (!wbk) |
| 87 | return -ENOMEM; |
| 88 | refcount_set(&wbk->usage, 2); |
| 89 | wbk->key = af->key; |
| 90 | |
| 91 | spin_lock(&vnode->wb_lock); |
| 92 | list_for_each_entry(p, &vnode->wb_keys, vnode_link) { |
| 93 | if (p->key == wbk->key) |
| 94 | goto found; |
| 95 | } |
| 96 | |
| 97 | key_get(wbk->key); |
| 98 | list_add_tail(&wbk->vnode_link, &vnode->wb_keys); |
| 99 | spin_unlock(&vnode->wb_lock); |
| 100 | af->wb = wbk; |
| 101 | return 0; |
| 102 | |
| 103 | found: |
| 104 | refcount_inc(&p->usage); |
| 105 | spin_unlock(&vnode->wb_lock); |
| 106 | af->wb = p; |
| 107 | kfree(wbk); |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | /* |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 112 | * open an AFS file or directory and attach a key to it |
| 113 | */ |
| 114 | int afs_open(struct inode *inode, struct file *file) |
| 115 | { |
| 116 | struct afs_vnode *vnode = AFS_FS_I(inode); |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 117 | struct afs_file *af; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 118 | struct key *key; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 119 | int ret; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 120 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 121 | _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 122 | |
| 123 | key = afs_request_key(vnode->volume->cell); |
| 124 | if (IS_ERR(key)) { |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 125 | ret = PTR_ERR(key); |
| 126 | goto error; |
| 127 | } |
| 128 | |
| 129 | af = kzalloc(sizeof(*af), GFP_KERNEL); |
| 130 | if (!af) { |
| 131 | ret = -ENOMEM; |
| 132 | goto error_key; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 133 | } |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 134 | af->key = key; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 135 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 136 | ret = afs_validate(vnode, key); |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 137 | if (ret < 0) |
| 138 | goto error_af; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 139 | |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 140 | if (file->f_mode & FMODE_WRITE) { |
| 141 | ret = afs_cache_wb_key(vnode, af); |
| 142 | if (ret < 0) |
| 143 | goto error_af; |
| 144 | } |
David Howells | 5a81327 | 2018-04-06 14:17:26 +0100 | [diff] [blame] | 145 | |
| 146 | if (file->f_flags & O_TRUNC) |
| 147 | set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 148 | |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 149 | file->private_data = af; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 150 | _leave(" = 0"); |
| 151 | return 0; |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 152 | |
| 153 | error_af: |
| 154 | kfree(af); |
| 155 | error_key: |
| 156 | key_put(key); |
| 157 | error: |
| 158 | _leave(" = %d", ret); |
| 159 | return ret; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /* |
| 163 | * release an AFS file or directory and discard its key |
| 164 | */ |
| 165 | int afs_release(struct inode *inode, struct file *file) |
| 166 | { |
| 167 | struct afs_vnode *vnode = AFS_FS_I(inode); |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 168 | struct afs_file *af = file->private_data; |
David Howells | a1b879e | 2019-05-15 12:09:17 +0100 | [diff] [blame] | 169 | int ret = 0; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 170 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 171 | _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 172 | |
David Howells | 5a81327 | 2018-04-06 14:17:26 +0100 | [diff] [blame] | 173 | if ((file->f_mode & FMODE_WRITE)) |
David Howells | a1b879e | 2019-05-15 12:09:17 +0100 | [diff] [blame] | 174 | ret = vfs_fsync(file, 0); |
David Howells | 5a81327 | 2018-04-06 14:17:26 +0100 | [diff] [blame] | 175 | |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 176 | file->private_data = NULL; |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 177 | if (af->wb) |
| 178 | afs_put_wb_key(af->wb); |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 179 | key_put(af->key); |
| 180 | kfree(af); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 181 | afs_prune_wb_keys(vnode); |
David Howells | a1b879e | 2019-05-15 12:09:17 +0100 | [diff] [blame] | 182 | _leave(" = %d", ret); |
| 183 | return ret; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 184 | } |
| 185 | |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 186 | /* |
| 187 | * Dispose of a ref to a read record. |
| 188 | */ |
| 189 | void afs_put_read(struct afs_read *req) |
| 190 | { |
| 191 | int i; |
| 192 | |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 193 | if (refcount_dec_and_test(&req->usage)) { |
Jia-Ju Bai | a6eed4a | 2019-07-30 14:38:51 +0100 | [diff] [blame] | 194 | if (req->pages) { |
| 195 | for (i = 0; i < req->nr_pages; i++) |
| 196 | if (req->pages[i]) |
| 197 | put_page(req->pages[i]); |
| 198 | if (req->pages != req->array) |
| 199 | kfree(req->pages); |
| 200 | } |
David Howells | c69bf47 | 2020-02-06 14:22:27 +0000 | [diff] [blame^] | 201 | key_put(req->key); |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 202 | kfree(req); |
| 203 | } |
| 204 | } |
| 205 | |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 206 | static void afs_fetch_data_success(struct afs_operation *op) |
| 207 | { |
| 208 | struct afs_vnode *vnode = op->file[0].vnode; |
| 209 | |
| 210 | _enter("op=%08x", op->debug_id); |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 211 | afs_vnode_commit_status(op, &op->file[0]); |
| 212 | afs_stat_v(vnode, n_fetches); |
| 213 | atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes); |
| 214 | } |
| 215 | |
| 216 | static void afs_fetch_data_put(struct afs_operation *op) |
| 217 | { |
| 218 | afs_put_read(op->fetch.req); |
| 219 | } |
| 220 | |
| 221 | static const struct afs_operation_ops afs_fetch_data_operation = { |
| 222 | .issue_afs_rpc = afs_fs_fetch_data, |
| 223 | .issue_yfs_rpc = yfs_fs_fetch_data, |
| 224 | .success = afs_fetch_data_success, |
David Howells | 728279a | 2020-06-16 00:34:09 +0100 | [diff] [blame] | 225 | .aborted = afs_check_for_remote_deletion, |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 226 | .put = afs_fetch_data_put, |
| 227 | }; |
| 228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 230 | * Fetch file data from the volume. |
| 231 | */ |
David Howells | c69bf47 | 2020-02-06 14:22:27 +0000 | [diff] [blame^] | 232 | int afs_fetch_data(struct afs_vnode *vnode, struct afs_read *req) |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 233 | { |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 234 | struct afs_operation *op; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 235 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 236 | _enter("%s{%llx:%llu.%u},%x,,,", |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 237 | vnode->volume->name, |
| 238 | vnode->fid.vid, |
| 239 | vnode->fid.vnode, |
| 240 | vnode->fid.unique, |
David Howells | c69bf47 | 2020-02-06 14:22:27 +0000 | [diff] [blame^] | 241 | key_serial(req->key)); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 242 | |
David Howells | c69bf47 | 2020-02-06 14:22:27 +0000 | [diff] [blame^] | 243 | op = afs_alloc_operation(req->key, vnode->volume); |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 244 | if (IS_ERR(op)) |
| 245 | return PTR_ERR(op); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 246 | |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 247 | afs_op_set_vnode(op, 0, vnode); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 248 | |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 249 | op->fetch.req = afs_get_read(req); |
| 250 | op->ops = &afs_fetch_data_operation; |
| 251 | return afs_do_sync_operation(op); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /* |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 255 | * read page from file, directory or symlink, given a key to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | */ |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 257 | int afs_page_filler(void *data, struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | { |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 259 | struct inode *inode = page->mapping->host; |
| 260 | struct afs_vnode *vnode = AFS_FS_I(inode); |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 261 | struct afs_read *req; |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 262 | struct key *key = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | int ret; |
| 264 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 265 | _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
Matt Mackall | cd7619d | 2005-05-01 08:59:01 -0700 | [diff] [blame] | 267 | BUG_ON(!PageLocked(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
| 269 | ret = -ESTALE; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 270 | if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | goto error; |
| 272 | |
David Howells | 03ffae9 | 2020-02-10 10:00:22 +0000 | [diff] [blame] | 273 | req = kzalloc(struct_size(req, array, 1), GFP_KERNEL); |
| 274 | if (!req) |
| 275 | goto enomem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
David Howells | 03ffae9 | 2020-02-10 10:00:22 +0000 | [diff] [blame] | 277 | /* We request a full page. If the page is a partial one at the |
| 278 | * end of the file, the server will return a short read and the |
| 279 | * unmarshalling code will clear the unfilled space. |
| 280 | */ |
| 281 | refcount_set(&req->usage, 1); |
David Howells | c69bf47 | 2020-02-06 14:22:27 +0000 | [diff] [blame^] | 282 | req->key = key_get(key); |
David Howells | 03ffae9 | 2020-02-10 10:00:22 +0000 | [diff] [blame] | 283 | req->pos = (loff_t)page->index << PAGE_SHIFT; |
| 284 | req->len = PAGE_SIZE; |
| 285 | req->nr_pages = 1; |
| 286 | req->pages = req->array; |
| 287 | req->pages[0] = page; |
| 288 | get_page(page); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 289 | |
David Howells | 03ffae9 | 2020-02-10 10:00:22 +0000 | [diff] [blame] | 290 | /* read the contents of the file from the server into the |
| 291 | * page */ |
David Howells | c69bf47 | 2020-02-06 14:22:27 +0000 | [diff] [blame^] | 292 | ret = afs_fetch_data(vnode, req); |
David Howells | 03ffae9 | 2020-02-10 10:00:22 +0000 | [diff] [blame] | 293 | afs_put_read(req); |
Gustavo A. R. Silva | e690c9e | 2019-01-10 15:52:25 -0600 | [diff] [blame] | 294 | |
David Howells | 03ffae9 | 2020-02-10 10:00:22 +0000 | [diff] [blame] | 295 | if (ret < 0) { |
| 296 | if (ret == -ENOENT) { |
| 297 | _debug("got NOENT from server" |
| 298 | " - marking file deleted and stale"); |
| 299 | set_bit(AFS_VNODE_DELETED, &vnode->flags); |
| 300 | ret = -ESTALE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | } |
| 302 | |
David Howells | 03ffae9 | 2020-02-10 10:00:22 +0000 | [diff] [blame] | 303 | if (ret == -EINTR || |
| 304 | ret == -ENOMEM || |
| 305 | ret == -ERESTARTSYS || |
| 306 | ret == -EAGAIN) |
| 307 | goto error; |
| 308 | goto io_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | } |
| 310 | |
David Howells | 03ffae9 | 2020-02-10 10:00:22 +0000 | [diff] [blame] | 311 | SetPageUptodate(page); |
| 312 | unlock_page(page); |
| 313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | _leave(" = 0"); |
| 315 | return 0; |
| 316 | |
David Howells | 68ae849 | 2017-03-16 16:27:48 +0000 | [diff] [blame] | 317 | io_error: |
| 318 | SetPageError(page); |
| 319 | goto error; |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 320 | enomem: |
| 321 | ret = -ENOMEM; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 322 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | unlock_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | _leave(" = %d", ret); |
| 325 | return ret; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 326 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | /* |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 329 | * read page from file, directory or symlink, given a file to nominate the key |
| 330 | * to be used |
| 331 | */ |
| 332 | static int afs_readpage(struct file *file, struct page *page) |
| 333 | { |
| 334 | struct key *key; |
| 335 | int ret; |
| 336 | |
| 337 | if (file) { |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 338 | key = afs_file_key(file); |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 339 | ASSERT(key != NULL); |
| 340 | ret = afs_page_filler(key, page); |
| 341 | } else { |
| 342 | struct inode *inode = page->mapping->host; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 343 | key = afs_request_key(AFS_FS_S(inode->i_sb)->cell); |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 344 | if (IS_ERR(key)) { |
| 345 | ret = PTR_ERR(key); |
| 346 | } else { |
| 347 | ret = afs_page_filler(key, page); |
| 348 | key_put(key); |
| 349 | } |
| 350 | } |
| 351 | return ret; |
| 352 | } |
| 353 | |
| 354 | /* |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 355 | * Make pages available as they're filled. |
| 356 | */ |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 357 | static void afs_readpages_page_done(struct afs_read *req) |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 358 | { |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 359 | struct page *page = req->pages[req->index]; |
| 360 | |
| 361 | req->pages[req->index] = NULL; |
| 362 | SetPageUptodate(page); |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 363 | unlock_page(page); |
| 364 | put_page(page); |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * Read a contiguous set of pages. |
| 369 | */ |
| 370 | static int afs_readpages_one(struct file *file, struct address_space *mapping, |
| 371 | struct list_head *pages) |
| 372 | { |
| 373 | struct afs_vnode *vnode = AFS_FS_I(mapping->host); |
| 374 | struct afs_read *req; |
| 375 | struct list_head *p; |
| 376 | struct page *first, *page; |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 377 | pgoff_t index; |
| 378 | int ret, n, i; |
| 379 | |
| 380 | /* Count the number of contiguous pages at the front of the list. Note |
| 381 | * that the list goes prev-wards rather than next-wards. |
| 382 | */ |
Nikolay Borisov | f86196e | 2019-01-03 15:29:02 -0800 | [diff] [blame] | 383 | first = lru_to_page(pages); |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 384 | index = first->index + 1; |
| 385 | n = 1; |
| 386 | for (p = first->lru.prev; p != pages; p = p->prev) { |
| 387 | page = list_entry(p, struct page, lru); |
| 388 | if (page->index != index) |
| 389 | break; |
| 390 | index++; |
| 391 | n++; |
| 392 | } |
| 393 | |
Zhengyuan Liu | ee10258 | 2019-06-20 18:12:17 +0100 | [diff] [blame] | 394 | req = kzalloc(struct_size(req, array, n), GFP_NOFS); |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 395 | if (!req) |
| 396 | return -ENOMEM; |
| 397 | |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 398 | refcount_set(&req->usage, 1); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 399 | req->vnode = vnode; |
David Howells | c69bf47 | 2020-02-06 14:22:27 +0000 | [diff] [blame^] | 400 | req->key = key_get(afs_file_key(file)); |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 401 | req->page_done = afs_readpages_page_done; |
| 402 | req->pos = first->index; |
| 403 | req->pos <<= PAGE_SHIFT; |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 404 | req->pages = req->array; |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 405 | |
| 406 | /* Transfer the pages to the request. We add them in until one fails |
| 407 | * to add to the LRU and then we stop (as that'll make a hole in the |
| 408 | * contiguous run. |
| 409 | * |
| 410 | * Note that it's possible for the file size to change whilst we're |
| 411 | * doing this, but we rely on the server returning less than we asked |
| 412 | * for if the file shrank. We also rely on this to deal with a partial |
| 413 | * page at the end of the file. |
| 414 | */ |
| 415 | do { |
Nikolay Borisov | f86196e | 2019-01-03 15:29:02 -0800 | [diff] [blame] | 416 | page = lru_to_page(pages); |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 417 | list_del(&page->lru); |
| 418 | index = page->index; |
| 419 | if (add_to_page_cache_lru(page, mapping, index, |
| 420 | readahead_gfp_mask(mapping))) { |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 421 | put_page(page); |
| 422 | break; |
| 423 | } |
| 424 | |
| 425 | req->pages[req->nr_pages++] = page; |
| 426 | req->len += PAGE_SIZE; |
| 427 | } while (req->nr_pages < n); |
| 428 | |
| 429 | if (req->nr_pages == 0) { |
David Howells | c69bf47 | 2020-02-06 14:22:27 +0000 | [diff] [blame^] | 430 | afs_put_read(req); |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 431 | return 0; |
| 432 | } |
| 433 | |
David Howells | c69bf47 | 2020-02-06 14:22:27 +0000 | [diff] [blame^] | 434 | ret = afs_fetch_data(vnode, req); |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 435 | if (ret < 0) |
| 436 | goto error; |
| 437 | |
| 438 | task_io_account_read(PAGE_SIZE * req->nr_pages); |
| 439 | afs_put_read(req); |
| 440 | return 0; |
| 441 | |
| 442 | error: |
| 443 | if (ret == -ENOENT) { |
| 444 | _debug("got NOENT from server" |
| 445 | " - marking file deleted and stale"); |
| 446 | set_bit(AFS_VNODE_DELETED, &vnode->flags); |
| 447 | ret = -ESTALE; |
| 448 | } |
| 449 | |
| 450 | for (i = 0; i < req->nr_pages; i++) { |
| 451 | page = req->pages[i]; |
| 452 | if (page) { |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 453 | SetPageError(page); |
| 454 | unlock_page(page); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | afs_put_read(req); |
| 459 | return ret; |
| 460 | } |
| 461 | |
| 462 | /* |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 463 | * read a set of pages |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | */ |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 465 | static int afs_readpages(struct file *file, struct address_space *mapping, |
| 466 | struct list_head *pages, unsigned nr_pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | { |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 468 | struct key *key = afs_file_key(file); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 469 | struct afs_vnode *vnode; |
| 470 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 472 | _enter("{%d},{%lu},,%d", |
| 473 | key_serial(key), mapping->host->i_ino, nr_pages); |
| 474 | |
| 475 | ASSERT(key != NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 477 | vnode = AFS_FS_I(mapping->host); |
Dan Carpenter | ad2a8e6 | 2012-03-20 16:58:06 +0000 | [diff] [blame] | 478 | if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) { |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 479 | _leave(" = -ESTALE"); |
| 480 | return -ESTALE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | } |
| 482 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 483 | /* attempt to read as many of the pages as possible */ |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 484 | while (!list_empty(pages)) { |
| 485 | ret = afs_readpages_one(file, mapping, pages); |
| 486 | if (ret < 0) |
| 487 | break; |
| 488 | } |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 489 | |
| 490 | _leave(" = %d [netting]", ret); |
| 491 | return ret; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 492 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | /* |
David Howells | f86726a | 2020-10-22 14:08:23 +0100 | [diff] [blame] | 495 | * Adjust the dirty region of the page on truncation or full invalidation, |
| 496 | * getting rid of the markers altogether if the region is entirely invalidated. |
| 497 | */ |
| 498 | static void afs_invalidate_dirty(struct page *page, unsigned int offset, |
| 499 | unsigned int length) |
| 500 | { |
| 501 | struct afs_vnode *vnode = AFS_FS_I(page->mapping->host); |
| 502 | unsigned long priv; |
| 503 | unsigned int f, t, end = offset + length; |
| 504 | |
| 505 | priv = page_private(page); |
| 506 | |
| 507 | /* we clean up only if the entire page is being invalidated */ |
| 508 | if (offset == 0 && length == thp_size(page)) |
| 509 | goto full_invalidate; |
| 510 | |
| 511 | /* If the page was dirtied by page_mkwrite(), the PTE stays writable |
| 512 | * and we don't get another notification to tell us to expand it |
| 513 | * again. |
| 514 | */ |
| 515 | if (afs_is_page_dirty_mmapped(priv)) |
| 516 | return; |
| 517 | |
| 518 | /* We may need to shorten the dirty region */ |
David Howells | 67d78a6 | 2020-10-28 14:23:46 +0000 | [diff] [blame] | 519 | f = afs_page_dirty_from(page, priv); |
| 520 | t = afs_page_dirty_to(page, priv); |
David Howells | f86726a | 2020-10-22 14:08:23 +0100 | [diff] [blame] | 521 | |
| 522 | if (t <= offset || f >= end) |
| 523 | return; /* Doesn't overlap */ |
| 524 | |
| 525 | if (f < offset && t > end) |
| 526 | return; /* Splits the dirty region - just absorb it */ |
| 527 | |
| 528 | if (f >= offset && t <= end) |
| 529 | goto undirty; |
| 530 | |
| 531 | if (f < offset) |
| 532 | t = offset; |
| 533 | else |
| 534 | f = end; |
| 535 | if (f == t) |
| 536 | goto undirty; |
| 537 | |
David Howells | 67d78a6 | 2020-10-28 14:23:46 +0000 | [diff] [blame] | 538 | priv = afs_page_dirty(page, f, t); |
David Howells | f86726a | 2020-10-22 14:08:23 +0100 | [diff] [blame] | 539 | set_page_private(page, priv); |
David Howells | 67d78a6 | 2020-10-28 14:23:46 +0000 | [diff] [blame] | 540 | trace_afs_page_dirty(vnode, tracepoint_string("trunc"), page); |
David Howells | f86726a | 2020-10-22 14:08:23 +0100 | [diff] [blame] | 541 | return; |
| 542 | |
| 543 | undirty: |
David Howells | 67d78a6 | 2020-10-28 14:23:46 +0000 | [diff] [blame] | 544 | trace_afs_page_dirty(vnode, tracepoint_string("undirty"), page); |
David Howells | f86726a | 2020-10-22 14:08:23 +0100 | [diff] [blame] | 545 | clear_page_dirty_for_io(page); |
| 546 | full_invalidate: |
David Howells | 67d78a6 | 2020-10-28 14:23:46 +0000 | [diff] [blame] | 547 | detach_page_private(page); |
| 548 | trace_afs_page_dirty(vnode, tracepoint_string("inval"), page); |
David Howells | f86726a | 2020-10-22 14:08:23 +0100 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | /* |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 552 | * invalidate part or all of a page |
| 553 | * - release a page and clean up its private data if offset is 0 (indicating |
| 554 | * the entire page) |
| 555 | */ |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 556 | static void afs_invalidatepage(struct page *page, unsigned int offset, |
| 557 | unsigned int length) |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 558 | { |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 559 | _enter("{%lu},%u,%u", page->index, offset, length); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 560 | |
| 561 | BUG_ON(!PageLocked(page)); |
| 562 | |
David Howells | f86726a | 2020-10-22 14:08:23 +0100 | [diff] [blame] | 563 | if (PagePrivate(page)) |
| 564 | afs_invalidate_dirty(page, offset, length); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 565 | |
| 566 | _leave(""); |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | * release a page and clean up its private state if it's not busy |
| 571 | * - return true if the page can now be released, false if not |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | */ |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 573 | static int afs_releasepage(struct page *page, gfp_t gfp_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | { |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 575 | struct afs_vnode *vnode = AFS_FS_I(page->mapping->host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 577 | _enter("{{%llx:%llu}[%lu],%lx},%x", |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 578 | vnode->fid.vid, vnode->fid.vnode, page->index, page->flags, |
| 579 | gfp_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 581 | /* deny if page is being written to the cache and the caller hasn't |
| 582 | * elected to wait */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | if (PagePrivate(page)) { |
David Howells | 67d78a6 | 2020-10-28 14:23:46 +0000 | [diff] [blame] | 584 | detach_page_private(page); |
| 585 | trace_afs_page_dirty(vnode, tracepoint_string("rel"), page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | } |
| 587 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 588 | /* indicate that the page can be released */ |
| 589 | _leave(" = T"); |
| 590 | return 1; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 591 | } |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 592 | |
| 593 | /* |
| 594 | * Handle setting up a memory mapping on an AFS file. |
| 595 | */ |
| 596 | static int afs_file_mmap(struct file *file, struct vm_area_struct *vma) |
| 597 | { |
| 598 | int ret; |
| 599 | |
| 600 | ret = generic_file_mmap(file, vma); |
| 601 | if (ret == 0) |
| 602 | vma->vm_ops = &afs_vm_ops; |
| 603 | return ret; |
| 604 | } |