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