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> |
David Howells | d7bdba1 | 2021-12-22 17:21:04 +0000 | [diff] [blame] | 17 | #include <linux/swap.h> |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 18 | #include <linux/netfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include "internal.h" |
| 20 | |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 21 | 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] | 22 | static int afs_readpage(struct file *file, struct page *page); |
David Howells | 75bd228 | 2021-06-29 22:23:13 +0100 | [diff] [blame] | 23 | static int afs_symlink_readpage(struct file *file, struct page *page); |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 24 | static void afs_invalidatepage(struct page *page, unsigned int offset, |
| 25 | unsigned int length); |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 26 | static int afs_releasepage(struct page *page, gfp_t gfp_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 28 | static void afs_readahead(struct readahead_control *ractl); |
David Howells | 3978d81 | 2021-09-01 19:22:50 +0100 | [diff] [blame] | 29 | static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter); |
David Howells | 6e0e99d | 2021-09-02 16:43:10 +0100 | [diff] [blame] | 30 | static void afs_vm_open(struct vm_area_struct *area); |
| 31 | static void afs_vm_close(struct vm_area_struct *area); |
| 32 | static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 33 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 34 | const struct file_operations afs_file_operations = { |
| 35 | .open = afs_open, |
| 36 | .release = afs_release, |
| 37 | .llseek = generic_file_llseek, |
David Howells | 3978d81 | 2021-09-01 19:22:50 +0100 | [diff] [blame] | 38 | .read_iter = afs_file_read_iter, |
Al Viro | 50b5551 | 2014-04-03 14:13:46 -0400 | [diff] [blame] | 39 | .write_iter = afs_file_write, |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 40 | .mmap = afs_file_mmap, |
Jens Axboe | 5ffc4ef | 2007-06-01 11:49:19 +0200 | [diff] [blame] | 41 | .splice_read = generic_file_splice_read, |
David Howells | 06a17bb | 2020-10-27 09:39:04 +0000 | [diff] [blame] | 42 | .splice_write = iter_file_splice_write, |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 43 | .fsync = afs_fsync, |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 44 | .lock = afs_lock, |
| 45 | .flock = afs_flock, |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 48 | const struct inode_operations afs_file_inode_operations = { |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 49 | .getattr = afs_getattr, |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 50 | .setattr = afs_setattr, |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 51 | .permission = afs_permission, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
David Howells | 75bd228 | 2021-06-29 22:23:13 +0100 | [diff] [blame] | 54 | const struct address_space_operations afs_file_aops = { |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 55 | .readpage = afs_readpage, |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 56 | .readahead = afs_readahead, |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 57 | .set_page_dirty = afs_set_page_dirty, |
| 58 | .launder_page = afs_launder_page, |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 59 | .releasepage = afs_releasepage, |
| 60 | .invalidatepage = afs_invalidatepage, |
Nick Piggin | 15b4650 | 2008-10-15 22:04:32 -0700 | [diff] [blame] | 61 | .write_begin = afs_write_begin, |
| 62 | .write_end = afs_write_end, |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 63 | .writepage = afs_writepage, |
| 64 | .writepages = afs_writepages, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
David Howells | 75bd228 | 2021-06-29 22:23:13 +0100 | [diff] [blame] | 67 | const struct address_space_operations afs_symlink_aops = { |
| 68 | .readpage = afs_symlink_readpage, |
| 69 | .releasepage = afs_releasepage, |
| 70 | .invalidatepage = afs_invalidatepage, |
| 71 | }; |
| 72 | |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 73 | static const struct vm_operations_struct afs_vm_ops = { |
David Howells | 6e0e99d | 2021-09-02 16:43:10 +0100 | [diff] [blame] | 74 | .open = afs_vm_open, |
| 75 | .close = afs_vm_close, |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 76 | .fault = filemap_fault, |
David Howells | 6e0e99d | 2021-09-02 16:43:10 +0100 | [diff] [blame] | 77 | .map_pages = afs_vm_map_pages, |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 78 | .page_mkwrite = afs_page_mkwrite, |
| 79 | }; |
| 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | /* |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 82 | * Discard a pin on a writeback key. |
| 83 | */ |
| 84 | void afs_put_wb_key(struct afs_wb_key *wbk) |
| 85 | { |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 86 | if (wbk && refcount_dec_and_test(&wbk->usage)) { |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 87 | key_put(wbk->key); |
| 88 | kfree(wbk); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * Cache key for writeback. |
| 94 | */ |
| 95 | int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af) |
| 96 | { |
| 97 | struct afs_wb_key *wbk, *p; |
| 98 | |
| 99 | wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL); |
| 100 | if (!wbk) |
| 101 | return -ENOMEM; |
| 102 | refcount_set(&wbk->usage, 2); |
| 103 | wbk->key = af->key; |
| 104 | |
| 105 | spin_lock(&vnode->wb_lock); |
| 106 | list_for_each_entry(p, &vnode->wb_keys, vnode_link) { |
| 107 | if (p->key == wbk->key) |
| 108 | goto found; |
| 109 | } |
| 110 | |
| 111 | key_get(wbk->key); |
| 112 | list_add_tail(&wbk->vnode_link, &vnode->wb_keys); |
| 113 | spin_unlock(&vnode->wb_lock); |
| 114 | af->wb = wbk; |
| 115 | return 0; |
| 116 | |
| 117 | found: |
| 118 | refcount_inc(&p->usage); |
| 119 | spin_unlock(&vnode->wb_lock); |
| 120 | af->wb = p; |
| 121 | kfree(wbk); |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | /* |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 126 | * open an AFS file or directory and attach a key to it |
| 127 | */ |
| 128 | int afs_open(struct inode *inode, struct file *file) |
| 129 | { |
| 130 | struct afs_vnode *vnode = AFS_FS_I(inode); |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 131 | struct afs_file *af; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 132 | struct key *key; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 133 | int ret; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 134 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 135 | _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 136 | |
| 137 | key = afs_request_key(vnode->volume->cell); |
| 138 | if (IS_ERR(key)) { |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 139 | ret = PTR_ERR(key); |
| 140 | goto error; |
| 141 | } |
| 142 | |
| 143 | af = kzalloc(sizeof(*af), GFP_KERNEL); |
| 144 | if (!af) { |
| 145 | ret = -ENOMEM; |
| 146 | goto error_key; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 147 | } |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 148 | af->key = key; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 149 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 150 | ret = afs_validate(vnode, key); |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 151 | if (ret < 0) |
| 152 | goto error_af; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 153 | |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 154 | if (file->f_mode & FMODE_WRITE) { |
| 155 | ret = afs_cache_wb_key(vnode, af); |
| 156 | if (ret < 0) |
| 157 | goto error_af; |
| 158 | } |
David Howells | 5a81327 | 2018-04-06 14:17:26 +0100 | [diff] [blame] | 159 | |
| 160 | if (file->f_flags & O_TRUNC) |
| 161 | set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags); |
David Howells | 523d27c | 2020-02-06 14:22:21 +0000 | [diff] [blame] | 162 | |
| 163 | fscache_use_cookie(afs_vnode_cache(vnode), file->f_mode & FMODE_WRITE); |
| 164 | |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 165 | file->private_data = af; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 166 | _leave(" = 0"); |
| 167 | return 0; |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 168 | |
| 169 | error_af: |
| 170 | kfree(af); |
| 171 | error_key: |
| 172 | key_put(key); |
| 173 | error: |
| 174 | _leave(" = %d", ret); |
| 175 | return ret; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /* |
| 179 | * release an AFS file or directory and discard its key |
| 180 | */ |
| 181 | int afs_release(struct inode *inode, struct file *file) |
| 182 | { |
David Howells | 523d27c | 2020-02-06 14:22:21 +0000 | [diff] [blame] | 183 | struct afs_vnode_cache_aux aux; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 184 | struct afs_vnode *vnode = AFS_FS_I(inode); |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 185 | struct afs_file *af = file->private_data; |
David Howells | 523d27c | 2020-02-06 14:22:21 +0000 | [diff] [blame] | 186 | loff_t i_size; |
David Howells | a1b879e | 2019-05-15 12:09:17 +0100 | [diff] [blame] | 187 | int ret = 0; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 188 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 189 | _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 190 | |
David Howells | 5a81327 | 2018-04-06 14:17:26 +0100 | [diff] [blame] | 191 | if ((file->f_mode & FMODE_WRITE)) |
David Howells | a1b879e | 2019-05-15 12:09:17 +0100 | [diff] [blame] | 192 | ret = vfs_fsync(file, 0); |
David Howells | 5a81327 | 2018-04-06 14:17:26 +0100 | [diff] [blame] | 193 | |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 194 | file->private_data = NULL; |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 195 | if (af->wb) |
| 196 | afs_put_wb_key(af->wb); |
David Howells | 523d27c | 2020-02-06 14:22:21 +0000 | [diff] [blame] | 197 | |
| 198 | if ((file->f_mode & FMODE_WRITE)) { |
| 199 | i_size = i_size_read(&vnode->vfs_inode); |
| 200 | afs_set_cache_aux(vnode, &aux); |
| 201 | fscache_unuse_cookie(afs_vnode_cache(vnode), &aux, &i_size); |
| 202 | } else { |
| 203 | fscache_unuse_cookie(afs_vnode_cache(vnode), NULL, NULL); |
| 204 | } |
| 205 | |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 206 | key_put(af->key); |
| 207 | kfree(af); |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 208 | afs_prune_wb_keys(vnode); |
David Howells | a1b879e | 2019-05-15 12:09:17 +0100 | [diff] [blame] | 209 | _leave(" = %d", ret); |
| 210 | return ret; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 211 | } |
| 212 | |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 213 | /* |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 214 | * Allocate a new read record. |
David Howells | c450846 | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 215 | */ |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 216 | struct afs_read *afs_alloc_read(gfp_t gfp) |
David Howells | c450846 | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 217 | { |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 218 | struct afs_read *req; |
David Howells | c450846 | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 219 | |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 220 | req = kzalloc(sizeof(struct afs_read), gfp); |
| 221 | if (req) |
| 222 | refcount_set(&req->usage, 1); |
David Howells | c450846 | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 223 | |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 224 | return req; |
David Howells | c450846 | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 228 | * Dispose of a ref to a read record. |
| 229 | */ |
| 230 | void afs_put_read(struct afs_read *req) |
| 231 | { |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 232 | if (refcount_dec_and_test(&req->usage)) { |
David Howells | c450846 | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 233 | if (req->cleanup) |
| 234 | req->cleanup(req); |
David Howells | c69bf47 | 2020-02-06 14:22:27 +0000 | [diff] [blame] | 235 | key_put(req->key); |
David Howells | 196ee9c | 2017-01-05 10:38:34 +0000 | [diff] [blame] | 236 | kfree(req); |
| 237 | } |
| 238 | } |
| 239 | |
David Howells | dc41918 | 2020-09-18 09:11:15 +0100 | [diff] [blame] | 240 | static void afs_fetch_data_notify(struct afs_operation *op) |
| 241 | { |
| 242 | struct afs_read *req = op->fetch.req; |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 243 | struct netfs_read_subrequest *subreq = req->subreq; |
David Howells | dc41918 | 2020-09-18 09:11:15 +0100 | [diff] [blame] | 244 | int error = op->error; |
| 245 | |
| 246 | if (error == -ECONNABORTED) |
| 247 | error = afs_abort_to_error(op->ac.abort_code); |
| 248 | req->error = error; |
| 249 | |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 250 | if (subreq) { |
| 251 | __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags); |
| 252 | netfs_subreq_terminated(subreq, error ?: req->actual_len, false); |
| 253 | req->subreq = NULL; |
| 254 | } else if (req->done) { |
David Howells | dc41918 | 2020-09-18 09:11:15 +0100 | [diff] [blame] | 255 | req->done(req); |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 256 | } |
David Howells | dc41918 | 2020-09-18 09:11:15 +0100 | [diff] [blame] | 257 | } |
| 258 | |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 259 | static void afs_fetch_data_success(struct afs_operation *op) |
| 260 | { |
| 261 | struct afs_vnode *vnode = op->file[0].vnode; |
| 262 | |
| 263 | _enter("op=%08x", op->debug_id); |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 264 | afs_vnode_commit_status(op, &op->file[0]); |
| 265 | afs_stat_v(vnode, n_fetches); |
| 266 | atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes); |
David Howells | dc41918 | 2020-09-18 09:11:15 +0100 | [diff] [blame] | 267 | afs_fetch_data_notify(op); |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static void afs_fetch_data_put(struct afs_operation *op) |
| 271 | { |
David Howells | c450846 | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 272 | op->fetch.req->error = op->error; |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 273 | afs_put_read(op->fetch.req); |
| 274 | } |
| 275 | |
| 276 | static const struct afs_operation_ops afs_fetch_data_operation = { |
| 277 | .issue_afs_rpc = afs_fs_fetch_data, |
| 278 | .issue_yfs_rpc = yfs_fs_fetch_data, |
| 279 | .success = afs_fetch_data_success, |
David Howells | 728279a | 2020-06-16 00:34:09 +0100 | [diff] [blame] | 280 | .aborted = afs_check_for_remote_deletion, |
David Howells | dc41918 | 2020-09-18 09:11:15 +0100 | [diff] [blame] | 281 | .failed = afs_fetch_data_notify, |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 282 | .put = afs_fetch_data_put, |
| 283 | }; |
| 284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 286 | * Fetch file data from the volume. |
| 287 | */ |
David Howells | c69bf47 | 2020-02-06 14:22:27 +0000 | [diff] [blame] | 288 | int afs_fetch_data(struct afs_vnode *vnode, struct afs_read *req) |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 289 | { |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 290 | struct afs_operation *op; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 291 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 292 | _enter("%s{%llx:%llu.%u},%x,,,", |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 293 | vnode->volume->name, |
| 294 | vnode->fid.vid, |
| 295 | vnode->fid.vnode, |
| 296 | vnode->fid.unique, |
David Howells | c69bf47 | 2020-02-06 14:22:27 +0000 | [diff] [blame] | 297 | key_serial(req->key)); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 298 | |
David Howells | c69bf47 | 2020-02-06 14:22:27 +0000 | [diff] [blame] | 299 | op = afs_alloc_operation(req->key, vnode->volume); |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 300 | if (IS_ERR(op)) { |
| 301 | if (req->subreq) |
| 302 | netfs_subreq_terminated(req->subreq, PTR_ERR(op), false); |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 303 | return PTR_ERR(op); |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 304 | } |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 305 | |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 306 | afs_op_set_vnode(op, 0, vnode); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 307 | |
David Howells | e49c7b2 | 2020-04-10 20:51:51 +0100 | [diff] [blame] | 308 | op->fetch.req = afs_get_read(req); |
| 309 | op->ops = &afs_fetch_data_operation; |
| 310 | return afs_do_sync_operation(op); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 311 | } |
| 312 | |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 313 | static void afs_req_issue_op(struct netfs_read_subrequest *subreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | { |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 315 | struct afs_vnode *vnode = AFS_FS_I(subreq->rreq->inode); |
| 316 | struct afs_read *fsreq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 318 | fsreq = afs_alloc_read(GFP_NOFS); |
| 319 | if (!fsreq) |
| 320 | return netfs_subreq_terminated(subreq, -ENOMEM, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 322 | fsreq->subreq = subreq; |
| 323 | fsreq->pos = subreq->start + subreq->transferred; |
| 324 | fsreq->len = subreq->len - subreq->transferred; |
David Howells | 345e1ae | 2021-08-27 23:22:02 +0100 | [diff] [blame] | 325 | fsreq->key = key_get(subreq->rreq->netfs_priv); |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 326 | fsreq->vnode = vnode; |
| 327 | fsreq->iter = &fsreq->def_iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 329 | iov_iter_xarray(&fsreq->def_iter, READ, |
| 330 | &fsreq->vnode->vfs_inode.i_mapping->i_pages, |
| 331 | fsreq->pos, fsreq->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 333 | afs_fetch_data(fsreq->vnode, fsreq); |
David Howells | 345e1ae | 2021-08-27 23:22:02 +0100 | [diff] [blame] | 334 | afs_put_read(fsreq); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 335 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
David Howells | 75bd228 | 2021-06-29 22:23:13 +0100 | [diff] [blame] | 337 | static int afs_symlink_readpage(struct file *file, struct page *page) |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 338 | { |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 339 | struct afs_vnode *vnode = AFS_FS_I(page->mapping->host); |
| 340 | struct afs_read *fsreq; |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 341 | struct folio *folio = page_folio(page); |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 342 | int ret; |
| 343 | |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 344 | fsreq = afs_alloc_read(GFP_NOFS); |
| 345 | if (!fsreq) |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 346 | return -ENOMEM; |
| 347 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 348 | fsreq->pos = folio_pos(folio); |
| 349 | fsreq->len = folio_size(folio); |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 350 | fsreq->vnode = vnode; |
| 351 | fsreq->iter = &fsreq->def_iter; |
| 352 | iov_iter_xarray(&fsreq->def_iter, READ, &page->mapping->i_pages, |
| 353 | fsreq->pos, fsreq->len); |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 354 | |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 355 | ret = afs_fetch_data(fsreq->vnode, fsreq); |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 356 | if (ret == 0) |
| 357 | SetPageUptodate(page); |
| 358 | unlock_page(page); |
David Howells | 91b467e | 2017-01-05 10:38:35 +0000 | [diff] [blame] | 359 | return ret; |
| 360 | } |
| 361 | |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 362 | static void afs_init_rreq(struct netfs_read_request *rreq, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 364 | rreq->netfs_priv = key_get(afs_file_key(file)); |
| 365 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
David Howells | 3003bbd | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 367 | static bool afs_is_cache_enabled(struct inode *inode) |
| 368 | { |
| 369 | struct fscache_cookie *cookie = afs_vnode_cache(AFS_FS_I(inode)); |
| 370 | |
David Howells | 523d27c | 2020-02-06 14:22:21 +0000 | [diff] [blame] | 371 | return fscache_cookie_enabled(cookie) && cookie->cache_priv; |
David Howells | 3003bbd | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 372 | } |
| 373 | |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 374 | static int afs_begin_cache_operation(struct netfs_read_request *rreq) |
| 375 | { |
David Howells | 2cee6fb | 2021-10-25 21:53:44 +0100 | [diff] [blame] | 376 | #ifdef CONFIG_AFS_FSCACHE |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 377 | struct afs_vnode *vnode = AFS_FS_I(rreq->inode); |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 378 | |
David Howells | 523d27c | 2020-02-06 14:22:21 +0000 | [diff] [blame] | 379 | return fscache_begin_read_operation(&rreq->cache_resources, |
| 380 | afs_vnode_cache(vnode)); |
David Howells | 2cee6fb | 2021-10-25 21:53:44 +0100 | [diff] [blame] | 381 | #else |
| 382 | return -ENOBUFS; |
| 383 | #endif |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 384 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
David Howells | 3003bbd | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 386 | static int afs_check_write_begin(struct file *file, loff_t pos, unsigned len, |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 387 | struct folio *folio, void **_fsdata) |
David Howells | 3003bbd | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 388 | { |
| 389 | struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); |
| 390 | |
| 391 | return test_bit(AFS_VNODE_DELETED, &vnode->flags) ? -ESTALE : 0; |
| 392 | } |
| 393 | |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 394 | static void afs_priv_cleanup(struct address_space *mapping, void *netfs_priv) |
| 395 | { |
| 396 | key_put(netfs_priv); |
| 397 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
David Howells | 3003bbd | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 399 | const struct netfs_read_request_ops afs_req_ops = { |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 400 | .init_rreq = afs_init_rreq, |
David Howells | 3003bbd | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 401 | .is_cache_enabled = afs_is_cache_enabled, |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 402 | .begin_cache_operation = afs_begin_cache_operation, |
David Howells | 3003bbd | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 403 | .check_write_begin = afs_check_write_begin, |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 404 | .issue_op = afs_req_issue_op, |
| 405 | .cleanup = afs_priv_cleanup, |
| 406 | }; |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 407 | |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 408 | static int afs_readpage(struct file *file, struct page *page) |
| 409 | { |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 410 | struct folio *folio = page_folio(page); |
| 411 | |
| 412 | return netfs_readpage(file, folio, &afs_req_ops, NULL); |
David Howells | 5cbf039 | 2020-02-06 14:22:29 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | static void afs_readahead(struct readahead_control *ractl) |
| 416 | { |
| 417 | netfs_readahead(ractl, &afs_req_ops, NULL); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 418 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
David Howells | c7f75ef | 2020-02-06 14:22:30 +0000 | [diff] [blame] | 420 | int afs_write_inode(struct inode *inode, struct writeback_control *wbc) |
| 421 | { |
| 422 | fscache_unpin_writeback(wbc, afs_vnode_cache(AFS_FS_I(inode))); |
| 423 | return 0; |
| 424 | } |
| 425 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | /* |
David Howells | f86726a | 2020-10-22 14:08:23 +0100 | [diff] [blame] | 427 | * Adjust the dirty region of the page on truncation or full invalidation, |
| 428 | * getting rid of the markers altogether if the region is entirely invalidated. |
| 429 | */ |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 430 | static void afs_invalidate_dirty(struct folio *folio, unsigned int offset, |
David Howells | f86726a | 2020-10-22 14:08:23 +0100 | [diff] [blame] | 431 | unsigned int length) |
| 432 | { |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 433 | struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio)); |
David Howells | f86726a | 2020-10-22 14:08:23 +0100 | [diff] [blame] | 434 | unsigned long priv; |
| 435 | unsigned int f, t, end = offset + length; |
| 436 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 437 | priv = (unsigned long)folio_get_private(folio); |
David Howells | f86726a | 2020-10-22 14:08:23 +0100 | [diff] [blame] | 438 | |
| 439 | /* we clean up only if the entire page is being invalidated */ |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 440 | if (offset == 0 && length == folio_size(folio)) |
David Howells | f86726a | 2020-10-22 14:08:23 +0100 | [diff] [blame] | 441 | goto full_invalidate; |
| 442 | |
| 443 | /* If the page was dirtied by page_mkwrite(), the PTE stays writable |
| 444 | * and we don't get another notification to tell us to expand it |
| 445 | * again. |
| 446 | */ |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 447 | if (afs_is_folio_dirty_mmapped(priv)) |
David Howells | f86726a | 2020-10-22 14:08:23 +0100 | [diff] [blame] | 448 | return; |
| 449 | |
| 450 | /* We may need to shorten the dirty region */ |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 451 | f = afs_folio_dirty_from(folio, priv); |
| 452 | t = afs_folio_dirty_to(folio, priv); |
David Howells | f86726a | 2020-10-22 14:08:23 +0100 | [diff] [blame] | 453 | |
| 454 | if (t <= offset || f >= end) |
| 455 | return; /* Doesn't overlap */ |
| 456 | |
| 457 | if (f < offset && t > end) |
| 458 | return; /* Splits the dirty region - just absorb it */ |
| 459 | |
| 460 | if (f >= offset && t <= end) |
| 461 | goto undirty; |
| 462 | |
| 463 | if (f < offset) |
| 464 | t = offset; |
| 465 | else |
| 466 | f = end; |
| 467 | if (f == t) |
| 468 | goto undirty; |
| 469 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 470 | priv = afs_folio_dirty(folio, f, t); |
| 471 | folio_change_private(folio, (void *)priv); |
| 472 | trace_afs_folio_dirty(vnode, tracepoint_string("trunc"), folio); |
David Howells | f86726a | 2020-10-22 14:08:23 +0100 | [diff] [blame] | 473 | return; |
| 474 | |
| 475 | undirty: |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 476 | trace_afs_folio_dirty(vnode, tracepoint_string("undirty"), folio); |
| 477 | folio_clear_dirty_for_io(folio); |
David Howells | f86726a | 2020-10-22 14:08:23 +0100 | [diff] [blame] | 478 | full_invalidate: |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 479 | trace_afs_folio_dirty(vnode, tracepoint_string("inval"), folio); |
| 480 | folio_detach_private(folio); |
David Howells | f86726a | 2020-10-22 14:08:23 +0100 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | /* |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 484 | * invalidate part or all of a page |
| 485 | * - release a page and clean up its private data if offset is 0 (indicating |
| 486 | * the entire page) |
| 487 | */ |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 488 | static void afs_invalidatepage(struct page *page, unsigned int offset, |
| 489 | unsigned int length) |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 490 | { |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 491 | struct folio *folio = page_folio(page); |
| 492 | |
| 493 | _enter("{%lu},%u,%u", folio_index(folio), offset, length); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 494 | |
| 495 | BUG_ON(!PageLocked(page)); |
| 496 | |
David Howells | f86726a | 2020-10-22 14:08:23 +0100 | [diff] [blame] | 497 | if (PagePrivate(page)) |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 498 | afs_invalidate_dirty(folio, offset, length); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 499 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 500 | folio_wait_fscache(folio); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 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 | 523d27c | 2020-02-06 14:22:21 +0000 | [diff] [blame] | 508 | static int afs_releasepage(struct page *page, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | { |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 510 | struct folio *folio = page_folio(page); |
| 511 | struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 513 | _enter("{{%llx:%llu}[%lu],%lx},%x", |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 514 | vnode->fid.vid, vnode->fid.vnode, folio_index(folio), folio->flags, |
David Howells | 523d27c | 2020-02-06 14:22:21 +0000 | [diff] [blame] | 515 | gfp); |
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 */ |
David Howells | 630f5dd | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 519 | #ifdef CONFIG_AFS_FSCACHE |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 520 | if (folio_test_fscache(folio)) { |
David Howells | d7bdba1 | 2021-12-22 17:21:04 +0000 | [diff] [blame] | 521 | if (current_is_kswapd() || !(gfp & __GFP_FS)) |
David Howells | 630f5dd | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 522 | return false; |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 523 | folio_wait_fscache(folio); |
David Howells | 630f5dd | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 524 | } |
David Howells | 523d27c | 2020-02-06 14:22:21 +0000 | [diff] [blame] | 525 | fscache_note_page_release(afs_vnode_cache(vnode)); |
David Howells | 630f5dd | 2020-02-06 14:22:28 +0000 | [diff] [blame] | 526 | #endif |
| 527 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 528 | if (folio_test_private(folio)) { |
| 529 | trace_afs_folio_dirty(vnode, tracepoint_string("rel"), folio); |
| 530 | folio_detach_private(folio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | } |
| 532 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 533 | /* Indicate that the folio can be released */ |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 534 | _leave(" = T"); |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 535 | return true; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 536 | } |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 537 | |
David Howells | 6e0e99d | 2021-09-02 16:43:10 +0100 | [diff] [blame] | 538 | static void afs_add_open_mmap(struct afs_vnode *vnode) |
| 539 | { |
| 540 | if (atomic_inc_return(&vnode->cb_nr_mmap) == 1) { |
| 541 | down_write(&vnode->volume->cell->fs_open_mmaps_lock); |
| 542 | |
David Howells | 1744a22 | 2021-12-14 09:22:12 +0000 | [diff] [blame] | 543 | if (list_empty(&vnode->cb_mmap_link)) |
| 544 | list_add_tail(&vnode->cb_mmap_link, |
| 545 | &vnode->volume->cell->fs_open_mmaps); |
David Howells | 6e0e99d | 2021-09-02 16:43:10 +0100 | [diff] [blame] | 546 | |
| 547 | up_write(&vnode->volume->cell->fs_open_mmaps_lock); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | static void afs_drop_open_mmap(struct afs_vnode *vnode) |
| 552 | { |
| 553 | if (!atomic_dec_and_test(&vnode->cb_nr_mmap)) |
| 554 | return; |
| 555 | |
| 556 | down_write(&vnode->volume->cell->fs_open_mmaps_lock); |
| 557 | |
| 558 | if (atomic_read(&vnode->cb_nr_mmap) == 0) |
| 559 | list_del_init(&vnode->cb_mmap_link); |
| 560 | |
| 561 | up_write(&vnode->volume->cell->fs_open_mmaps_lock); |
| 562 | flush_work(&vnode->cb_work); |
| 563 | } |
| 564 | |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 565 | /* |
| 566 | * Handle setting up a memory mapping on an AFS file. |
| 567 | */ |
| 568 | static int afs_file_mmap(struct file *file, struct vm_area_struct *vma) |
| 569 | { |
David Howells | 6e0e99d | 2021-09-02 16:43:10 +0100 | [diff] [blame] | 570 | struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 571 | int ret; |
| 572 | |
David Howells | 6e0e99d | 2021-09-02 16:43:10 +0100 | [diff] [blame] | 573 | afs_add_open_mmap(vnode); |
| 574 | |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 575 | ret = generic_file_mmap(file, vma); |
| 576 | if (ret == 0) |
| 577 | vma->vm_ops = &afs_vm_ops; |
David Howells | 6e0e99d | 2021-09-02 16:43:10 +0100 | [diff] [blame] | 578 | else |
| 579 | afs_drop_open_mmap(vnode); |
David Howells | 1cf7a15 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 580 | return ret; |
| 581 | } |
David Howells | 3978d81 | 2021-09-01 19:22:50 +0100 | [diff] [blame] | 582 | |
David Howells | 6e0e99d | 2021-09-02 16:43:10 +0100 | [diff] [blame] | 583 | static void afs_vm_open(struct vm_area_struct *vma) |
| 584 | { |
| 585 | afs_add_open_mmap(AFS_FS_I(file_inode(vma->vm_file))); |
| 586 | } |
| 587 | |
| 588 | static void afs_vm_close(struct vm_area_struct *vma) |
| 589 | { |
| 590 | afs_drop_open_mmap(AFS_FS_I(file_inode(vma->vm_file))); |
| 591 | } |
| 592 | |
| 593 | static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff) |
| 594 | { |
| 595 | struct afs_vnode *vnode = AFS_FS_I(file_inode(vmf->vma->vm_file)); |
| 596 | struct afs_file *af = vmf->vma->vm_file->private_data; |
| 597 | |
| 598 | switch (afs_validate(vnode, af->key)) { |
| 599 | case 0: |
| 600 | return filemap_map_pages(vmf, start_pgoff, end_pgoff); |
| 601 | case -ENOMEM: |
| 602 | return VM_FAULT_OOM; |
| 603 | case -EINTR: |
| 604 | case -ERESTARTSYS: |
| 605 | return VM_FAULT_RETRY; |
| 606 | case -ESTALE: |
| 607 | default: |
| 608 | return VM_FAULT_SIGBUS; |
| 609 | } |
| 610 | } |
| 611 | |
David Howells | 3978d81 | 2021-09-01 19:22:50 +0100 | [diff] [blame] | 612 | static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) |
| 613 | { |
| 614 | struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp)); |
| 615 | struct afs_file *af = iocb->ki_filp->private_data; |
| 616 | int ret; |
| 617 | |
| 618 | ret = afs_validate(vnode, af->key); |
| 619 | if (ret < 0) |
| 620 | return ret; |
| 621 | |
| 622 | return generic_file_read_iter(iocb, iter); |
| 623 | } |