blob: 322973d126146501f5da7a7d7427859062291415 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells08e0e7c2007-04-26 15:55:03 -07002/* AFS filesystem file handling
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
David Howells08e0e7c2007-04-26 15:55:03 -07004 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Written by David Howells ([email protected])
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/fs.h>
12#include <linux/pagemap.h>
David Howells31143d52007-05-09 02:33:46 -070013#include <linux/writeback.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/gfp.h>
David Howells91b467e02017-01-05 10:38:35 +000015#include <linux/task_io_accounting_ops.h>
Nikolay Borisovf86196e2019-01-03 15:29:02 -080016#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "internal.h"
18
David Howells1cf7a152017-11-02 15:27:52 +000019static int afs_file_mmap(struct file *file, struct vm_area_struct *vma);
David Howells416351f2007-05-09 02:33:45 -070020static int afs_readpage(struct file *file, struct page *page);
Lukas Czernerd47992f2013-05-21 23:17:23 -040021static void afs_invalidatepage(struct page *page, unsigned int offset,
22 unsigned int length);
David Howells416351f2007-05-09 02:33:45 -070023static int afs_releasepage(struct page *page, gfp_t gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
David Howells9b3f26c2009-04-03 16:42:41 +010025static int afs_readpages(struct file *filp, struct address_space *mapping,
26 struct list_head *pages, unsigned nr_pages);
27
David Howells00d3b7a2007-04-26 15:57:07 -070028const struct file_operations afs_file_operations = {
29 .open = afs_open,
30 .release = afs_release,
31 .llseek = generic_file_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -040032 .read_iter = generic_file_read_iter,
Al Viro50b55512014-04-03 14:13:46 -040033 .write_iter = afs_file_write,
David Howells1cf7a152017-11-02 15:27:52 +000034 .mmap = afs_file_mmap,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +020035 .splice_read = generic_file_splice_read,
David Howells06a17bb2020-10-27 09:39:04 +000036 .splice_write = iter_file_splice_write,
David Howells31143d52007-05-09 02:33:46 -070037 .fsync = afs_fsync,
David Howellse8d6c552007-07-15 23:40:12 -070038 .lock = afs_lock,
39 .flock = afs_flock,
David Howells00d3b7a2007-04-26 15:57:07 -070040};
41
Arjan van de Ven754661f2007-02-12 00:55:38 -080042const struct inode_operations afs_file_inode_operations = {
David Howells416351f2007-05-09 02:33:45 -070043 .getattr = afs_getattr,
David Howells31143d52007-05-09 02:33:46 -070044 .setattr = afs_setattr,
David Howells00d3b7a2007-04-26 15:57:07 -070045 .permission = afs_permission,
David Howellsd3e3b7ea2017-07-06 15:50:27 +010046 .listxattr = afs_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047};
48
Christoph Hellwigf5e54d62006-06-28 04:26:44 -070049const struct address_space_operations afs_fs_aops = {
David Howells416351f2007-05-09 02:33:45 -070050 .readpage = afs_readpage,
David Howells9b3f26c2009-04-03 16:42:41 +010051 .readpages = afs_readpages,
David Howells31143d52007-05-09 02:33:46 -070052 .set_page_dirty = afs_set_page_dirty,
53 .launder_page = afs_launder_page,
David Howells416351f2007-05-09 02:33:45 -070054 .releasepage = afs_releasepage,
55 .invalidatepage = afs_invalidatepage,
Nick Piggin15b46502008-10-15 22:04:32 -070056 .write_begin = afs_write_begin,
57 .write_end = afs_write_end,
David Howells31143d52007-05-09 02:33:46 -070058 .writepage = afs_writepage,
59 .writepages = afs_writepages,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
David Howells1cf7a152017-11-02 15:27:52 +000062static const struct vm_operations_struct afs_vm_ops = {
63 .fault = filemap_fault,
64 .map_pages = filemap_map_pages,
65 .page_mkwrite = afs_page_mkwrite,
66};
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/*
David Howells4343d002017-11-02 15:27:52 +000069 * Discard a pin on a writeback key.
70 */
71void afs_put_wb_key(struct afs_wb_key *wbk)
72{
David Howellse49c7b22020-04-10 20:51:51 +010073 if (wbk && refcount_dec_and_test(&wbk->usage)) {
David Howells4343d002017-11-02 15:27:52 +000074 key_put(wbk->key);
75 kfree(wbk);
76 }
77}
78
79/*
80 * Cache key for writeback.
81 */
82int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af)
83{
84 struct afs_wb_key *wbk, *p;
85
86 wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL);
87 if (!wbk)
88 return -ENOMEM;
89 refcount_set(&wbk->usage, 2);
90 wbk->key = af->key;
91
92 spin_lock(&vnode->wb_lock);
93 list_for_each_entry(p, &vnode->wb_keys, vnode_link) {
94 if (p->key == wbk->key)
95 goto found;
96 }
97
98 key_get(wbk->key);
99 list_add_tail(&wbk->vnode_link, &vnode->wb_keys);
100 spin_unlock(&vnode->wb_lock);
101 af->wb = wbk;
102 return 0;
103
104found:
105 refcount_inc(&p->usage);
106 spin_unlock(&vnode->wb_lock);
107 af->wb = p;
108 kfree(wbk);
109 return 0;
110}
111
112/*
David Howells00d3b7a2007-04-26 15:57:07 -0700113 * open an AFS file or directory and attach a key to it
114 */
115int afs_open(struct inode *inode, struct file *file)
116{
117 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells215804a2017-11-02 15:27:52 +0000118 struct afs_file *af;
David Howells00d3b7a2007-04-26 15:57:07 -0700119 struct key *key;
David Howells260a9802007-04-26 15:59:35 -0700120 int ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700121
David Howells3b6492d2018-10-20 00:57:57 +0100122 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
David Howells00d3b7a2007-04-26 15:57:07 -0700123
124 key = afs_request_key(vnode->volume->cell);
125 if (IS_ERR(key)) {
David Howells215804a2017-11-02 15:27:52 +0000126 ret = PTR_ERR(key);
127 goto error;
128 }
129
130 af = kzalloc(sizeof(*af), GFP_KERNEL);
131 if (!af) {
132 ret = -ENOMEM;
133 goto error_key;
David Howells00d3b7a2007-04-26 15:57:07 -0700134 }
David Howells4343d002017-11-02 15:27:52 +0000135 af->key = key;
David Howells00d3b7a2007-04-26 15:57:07 -0700136
David Howells260a9802007-04-26 15:59:35 -0700137 ret = afs_validate(vnode, key);
David Howells215804a2017-11-02 15:27:52 +0000138 if (ret < 0)
139 goto error_af;
David Howells260a9802007-04-26 15:59:35 -0700140
David Howells4343d002017-11-02 15:27:52 +0000141 if (file->f_mode & FMODE_WRITE) {
142 ret = afs_cache_wb_key(vnode, af);
143 if (ret < 0)
144 goto error_af;
145 }
David Howells5a813272018-04-06 14:17:26 +0100146
147 if (file->f_flags & O_TRUNC)
148 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
David Howells4343d002017-11-02 15:27:52 +0000149
David Howells215804a2017-11-02 15:27:52 +0000150 file->private_data = af;
David Howells00d3b7a2007-04-26 15:57:07 -0700151 _leave(" = 0");
152 return 0;
David Howells215804a2017-11-02 15:27:52 +0000153
154error_af:
155 kfree(af);
156error_key:
157 key_put(key);
158error:
159 _leave(" = %d", ret);
160 return ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700161}
162
163/*
164 * release an AFS file or directory and discard its key
165 */
166int afs_release(struct inode *inode, struct file *file)
167{
168 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells215804a2017-11-02 15:27:52 +0000169 struct afs_file *af = file->private_data;
David Howellsa1b879e2019-05-15 12:09:17 +0100170 int ret = 0;
David Howells00d3b7a2007-04-26 15:57:07 -0700171
David Howells3b6492d2018-10-20 00:57:57 +0100172 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
David Howells00d3b7a2007-04-26 15:57:07 -0700173
David Howells5a813272018-04-06 14:17:26 +0100174 if ((file->f_mode & FMODE_WRITE))
David Howellsa1b879e2019-05-15 12:09:17 +0100175 ret = vfs_fsync(file, 0);
David Howells5a813272018-04-06 14:17:26 +0100176
David Howells215804a2017-11-02 15:27:52 +0000177 file->private_data = NULL;
David Howells4343d002017-11-02 15:27:52 +0000178 if (af->wb)
179 afs_put_wb_key(af->wb);
David Howells215804a2017-11-02 15:27:52 +0000180 key_put(af->key);
181 kfree(af);
David Howells4343d002017-11-02 15:27:52 +0000182 afs_prune_wb_keys(vnode);
David Howellsa1b879e2019-05-15 12:09:17 +0100183 _leave(" = %d", ret);
184 return ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700185}
186
David Howells196ee9c2017-01-05 10:38:34 +0000187/*
188 * Dispose of a ref to a read record.
189 */
190void afs_put_read(struct afs_read *req)
191{
192 int i;
193
David Howellsf3ddee82018-04-06 14:17:25 +0100194 if (refcount_dec_and_test(&req->usage)) {
Jia-Ju Baia6eed4a2019-07-30 14:38:51 +0100195 if (req->pages) {
196 for (i = 0; i < req->nr_pages; i++)
197 if (req->pages[i])
198 put_page(req->pages[i]);
199 if (req->pages != req->array)
200 kfree(req->pages);
201 }
David Howells196ee9c2017-01-05 10:38:34 +0000202 kfree(req);
203 }
204}
205
Matt Kraai6566abd2009-04-17 12:56:38 +0100206#ifdef CONFIG_AFS_FSCACHE
David Howells00d3b7a2007-04-26 15:57:07 -0700207/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 * deal with notification that a page was read from the cache
209 */
David Howells9b3f26c2009-04-03 16:42:41 +0100210static void afs_file_readpage_read_complete(struct page *page,
211 void *data,
212 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
David Howells9b3f26c2009-04-03 16:42:41 +0100214 _enter("%p,%p,%d", page, data, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
David Howells9b3f26c2009-04-03 16:42:41 +0100216 /* if the read completes with an error, we just unlock the page and let
217 * the VM reissue the readpage */
218 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 SetPageUptodate(page);
220 unlock_page(page);
David Howellsec268152007-04-26 15:49:28 -0700221}
Matt Kraai6566abd2009-04-17 12:56:38 +0100222#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
David Howellse49c7b22020-04-10 20:51:51 +0100224static void afs_fetch_data_success(struct afs_operation *op)
225{
226 struct afs_vnode *vnode = op->file[0].vnode;
227
228 _enter("op=%08x", op->debug_id);
David Howellse49c7b22020-04-10 20:51:51 +0100229 afs_vnode_commit_status(op, &op->file[0]);
230 afs_stat_v(vnode, n_fetches);
231 atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes);
232}
233
234static void afs_fetch_data_put(struct afs_operation *op)
235{
236 afs_put_read(op->fetch.req);
237}
238
239static const struct afs_operation_ops afs_fetch_data_operation = {
240 .issue_afs_rpc = afs_fs_fetch_data,
241 .issue_yfs_rpc = yfs_fs_fetch_data,
242 .success = afs_fetch_data_success,
David Howells728279a2020-06-16 00:34:09 +0100243 .aborted = afs_check_for_remote_deletion,
David Howellse49c7b22020-04-10 20:51:51 +0100244 .put = afs_fetch_data_put,
245};
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247/*
David Howellsd2ddc772017-11-02 15:27:50 +0000248 * Fetch file data from the volume.
249 */
David Howells0b9c0172019-11-21 09:12:17 +0000250int afs_fetch_data(struct afs_vnode *vnode, struct key *key, struct afs_read *req)
David Howellsd2ddc772017-11-02 15:27:50 +0000251{
David Howellse49c7b22020-04-10 20:51:51 +0100252 struct afs_operation *op;
David Howellsd2ddc772017-11-02 15:27:50 +0000253
David Howells3b6492d2018-10-20 00:57:57 +0100254 _enter("%s{%llx:%llu.%u},%x,,,",
David Howellsd2ddc772017-11-02 15:27:50 +0000255 vnode->volume->name,
256 vnode->fid.vid,
257 vnode->fid.vnode,
258 vnode->fid.unique,
259 key_serial(key));
260
David Howellse49c7b22020-04-10 20:51:51 +0100261 op = afs_alloc_operation(key, vnode->volume);
262 if (IS_ERR(op))
263 return PTR_ERR(op);
David Howellsa58823a2019-05-09 15:16:10 +0100264
David Howellse49c7b22020-04-10 20:51:51 +0100265 afs_op_set_vnode(op, 0, vnode);
David Howellsa58823a2019-05-09 15:16:10 +0100266
David Howellse49c7b22020-04-10 20:51:51 +0100267 op->fetch.req = afs_get_read(req);
268 op->ops = &afs_fetch_data_operation;
269 return afs_do_sync_operation(op);
David Howellsd2ddc772017-11-02 15:27:50 +0000270}
271
272/*
Al Virof6d335c2010-05-21 15:27:09 +0100273 * read page from file, directory or symlink, given a key to use
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 */
Al Virof6d335c2010-05-21 15:27:09 +0100275int afs_page_filler(void *data, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Al Virof6d335c2010-05-21 15:27:09 +0100277 struct inode *inode = page->mapping->host;
278 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells196ee9c2017-01-05 10:38:34 +0000279 struct afs_read *req;
Al Virof6d335c2010-05-21 15:27:09 +0100280 struct key *key = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 int ret;
282
David Howells00d3b7a2007-04-26 15:57:07 -0700283 _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Matt Mackallcd7619d2005-05-01 08:59:01 -0700285 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 ret = -ESTALE;
David Howells08e0e7c2007-04-26 15:55:03 -0700288 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 goto error;
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 /* is it cached? */
David Howells9b3f26c2009-04-03 16:42:41 +0100292#ifdef CONFIG_AFS_FSCACHE
293 ret = fscache_read_or_alloc_page(vnode->cache,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 page,
295 afs_file_readpage_read_complete,
296 NULL,
297 GFP_KERNEL);
298#else
299 ret = -ENOBUFS;
300#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 switch (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 /* read BIO submitted (page in cache) */
303 case 0:
304 break;
305
David Howells9b3f26c2009-04-03 16:42:41 +0100306 /* page not yet cached */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 case -ENODATA:
David Howells9b3f26c2009-04-03 16:42:41 +0100308 _debug("cache said ENODATA");
309 goto go_on;
310
311 /* page will not be cached */
312 case -ENOBUFS:
313 _debug("cache said ENOBUFS");
Gustavo A. R. Silvae690c9e2019-01-10 15:52:25 -0600314
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500315 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 default:
David Howells9b3f26c2009-04-03 16:42:41 +0100317 go_on:
Zhengyuan Liuee102582019-06-20 18:12:17 +0100318 req = kzalloc(struct_size(req, array, 1), GFP_KERNEL);
David Howells196ee9c2017-01-05 10:38:34 +0000319 if (!req)
320 goto enomem;
321
David Howells6db3ac32017-03-16 16:27:44 +0000322 /* We request a full page. If the page is a partial one at the
323 * end of the file, the server will return a short read and the
324 * unmarshalling code will clear the unfilled space.
325 */
David Howellsf3ddee82018-04-06 14:17:25 +0100326 refcount_set(&req->usage, 1);
David Howells196ee9c2017-01-05 10:38:34 +0000327 req->pos = (loff_t)page->index << PAGE_SHIFT;
David Howells6db3ac32017-03-16 16:27:44 +0000328 req->len = PAGE_SIZE;
David Howells196ee9c2017-01-05 10:38:34 +0000329 req->nr_pages = 1;
David Howellsf3ddee82018-04-06 14:17:25 +0100330 req->pages = req->array;
David Howells196ee9c2017-01-05 10:38:34 +0000331 req->pages[0] = page;
332 get_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /* read the contents of the file from the server into the
335 * page */
David Howellsd2ddc772017-11-02 15:27:50 +0000336 ret = afs_fetch_data(vnode, key, req);
David Howells196ee9c2017-01-05 10:38:34 +0000337 afs_put_read(req);
David Howellsdab17c12017-11-02 15:27:52 +0000338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (ret < 0) {
David Howells08e0e7c2007-04-26 15:55:03 -0700340 if (ret == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 _debug("got NOENT from server"
342 " - marking file deleted and stale");
David Howells08e0e7c2007-04-26 15:55:03 -0700343 set_bit(AFS_VNODE_DELETED, &vnode->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 ret = -ESTALE;
345 }
David Howells9b3f26c2009-04-03 16:42:41 +0100346
347#ifdef CONFIG_AFS_FSCACHE
348 fscache_uncache_page(vnode->cache, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349#endif
David Howells9b3f26c2009-04-03 16:42:41 +0100350 BUG_ON(PageFsCache(page));
David Howells68ae8492017-03-16 16:27:48 +0000351
352 if (ret == -EINTR ||
353 ret == -ENOMEM ||
354 ret == -ERESTARTSYS ||
355 ret == -EAGAIN)
356 goto error;
357 goto io_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359
360 SetPageUptodate(page);
361
David Howells9b3f26c2009-04-03 16:42:41 +0100362 /* send the page to the cache */
363#ifdef CONFIG_AFS_FSCACHE
364 if (PageFsCache(page) &&
David Howellsee1235a2018-04-04 13:41:28 +0100365 fscache_write_page(vnode->cache, page, vnode->status.size,
366 GFP_KERNEL) != 0) {
David Howells9b3f26c2009-04-03 16:42:41 +0100367 fscache_uncache_page(vnode->cache, page);
368 BUG_ON(PageFsCache(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370#endif
David Howells9b3f26c2009-04-03 16:42:41 +0100371 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373
374 _leave(" = 0");
375 return 0;
376
David Howells68ae8492017-03-16 16:27:48 +0000377io_error:
378 SetPageError(page);
379 goto error;
David Howells196ee9c2017-01-05 10:38:34 +0000380enomem:
381 ret = -ENOMEM;
David Howells08e0e7c2007-04-26 15:55:03 -0700382error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 _leave(" = %d", ret);
385 return ret;
David Howellsec268152007-04-26 15:49:28 -0700386}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388/*
Al Virof6d335c2010-05-21 15:27:09 +0100389 * read page from file, directory or symlink, given a file to nominate the key
390 * to be used
391 */
392static int afs_readpage(struct file *file, struct page *page)
393{
394 struct key *key;
395 int ret;
396
397 if (file) {
David Howells215804a2017-11-02 15:27:52 +0000398 key = afs_file_key(file);
Al Virof6d335c2010-05-21 15:27:09 +0100399 ASSERT(key != NULL);
400 ret = afs_page_filler(key, page);
401 } else {
402 struct inode *inode = page->mapping->host;
David Howellsd2ddc772017-11-02 15:27:50 +0000403 key = afs_request_key(AFS_FS_S(inode->i_sb)->cell);
Al Virof6d335c2010-05-21 15:27:09 +0100404 if (IS_ERR(key)) {
405 ret = PTR_ERR(key);
406 } else {
407 ret = afs_page_filler(key, page);
408 key_put(key);
409 }
410 }
411 return ret;
412}
413
414/*
David Howells91b467e02017-01-05 10:38:35 +0000415 * Make pages available as they're filled.
416 */
David Howellsa58823a2019-05-09 15:16:10 +0100417static void afs_readpages_page_done(struct afs_read *req)
David Howells91b467e02017-01-05 10:38:35 +0000418{
Arnd Bergmann51c89e62017-01-13 14:46:19 +0000419#ifdef CONFIG_AFS_FSCACHE
David Howellsa58823a2019-05-09 15:16:10 +0100420 struct afs_vnode *vnode = req->vnode;
Arnd Bergmann51c89e62017-01-13 14:46:19 +0000421#endif
David Howells91b467e02017-01-05 10:38:35 +0000422 struct page *page = req->pages[req->index];
423
424 req->pages[req->index] = NULL;
425 SetPageUptodate(page);
426
427 /* send the page to the cache */
428#ifdef CONFIG_AFS_FSCACHE
429 if (PageFsCache(page) &&
David Howellsee1235a2018-04-04 13:41:28 +0100430 fscache_write_page(vnode->cache, page, vnode->status.size,
431 GFP_KERNEL) != 0) {
David Howells91b467e02017-01-05 10:38:35 +0000432 fscache_uncache_page(vnode->cache, page);
433 BUG_ON(PageFsCache(page));
434 }
435#endif
436 unlock_page(page);
437 put_page(page);
438}
439
440/*
441 * Read a contiguous set of pages.
442 */
443static int afs_readpages_one(struct file *file, struct address_space *mapping,
444 struct list_head *pages)
445{
446 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
447 struct afs_read *req;
448 struct list_head *p;
449 struct page *first, *page;
David Howells215804a2017-11-02 15:27:52 +0000450 struct key *key = afs_file_key(file);
David Howells91b467e02017-01-05 10:38:35 +0000451 pgoff_t index;
452 int ret, n, i;
453
454 /* Count the number of contiguous pages at the front of the list. Note
455 * that the list goes prev-wards rather than next-wards.
456 */
Nikolay Borisovf86196e2019-01-03 15:29:02 -0800457 first = lru_to_page(pages);
David Howells91b467e02017-01-05 10:38:35 +0000458 index = first->index + 1;
459 n = 1;
460 for (p = first->lru.prev; p != pages; p = p->prev) {
461 page = list_entry(p, struct page, lru);
462 if (page->index != index)
463 break;
464 index++;
465 n++;
466 }
467
Zhengyuan Liuee102582019-06-20 18:12:17 +0100468 req = kzalloc(struct_size(req, array, n), GFP_NOFS);
David Howells91b467e02017-01-05 10:38:35 +0000469 if (!req)
470 return -ENOMEM;
471
David Howellsf3ddee82018-04-06 14:17:25 +0100472 refcount_set(&req->usage, 1);
David Howellsa58823a2019-05-09 15:16:10 +0100473 req->vnode = vnode;
David Howells91b467e02017-01-05 10:38:35 +0000474 req->page_done = afs_readpages_page_done;
475 req->pos = first->index;
476 req->pos <<= PAGE_SHIFT;
David Howellsf3ddee82018-04-06 14:17:25 +0100477 req->pages = req->array;
David Howells91b467e02017-01-05 10:38:35 +0000478
479 /* Transfer the pages to the request. We add them in until one fails
480 * to add to the LRU and then we stop (as that'll make a hole in the
481 * contiguous run.
482 *
483 * Note that it's possible for the file size to change whilst we're
484 * doing this, but we rely on the server returning less than we asked
485 * for if the file shrank. We also rely on this to deal with a partial
486 * page at the end of the file.
487 */
488 do {
Nikolay Borisovf86196e2019-01-03 15:29:02 -0800489 page = lru_to_page(pages);
David Howells91b467e02017-01-05 10:38:35 +0000490 list_del(&page->lru);
491 index = page->index;
492 if (add_to_page_cache_lru(page, mapping, index,
493 readahead_gfp_mask(mapping))) {
494#ifdef CONFIG_AFS_FSCACHE
495 fscache_uncache_page(vnode->cache, page);
496#endif
497 put_page(page);
498 break;
499 }
500
501 req->pages[req->nr_pages++] = page;
502 req->len += PAGE_SIZE;
503 } while (req->nr_pages < n);
504
505 if (req->nr_pages == 0) {
506 kfree(req);
507 return 0;
508 }
509
David Howellsd2ddc772017-11-02 15:27:50 +0000510 ret = afs_fetch_data(vnode, key, req);
David Howells91b467e02017-01-05 10:38:35 +0000511 if (ret < 0)
512 goto error;
513
514 task_io_account_read(PAGE_SIZE * req->nr_pages);
515 afs_put_read(req);
516 return 0;
517
518error:
519 if (ret == -ENOENT) {
520 _debug("got NOENT from server"
521 " - marking file deleted and stale");
522 set_bit(AFS_VNODE_DELETED, &vnode->flags);
523 ret = -ESTALE;
524 }
525
526 for (i = 0; i < req->nr_pages; i++) {
527 page = req->pages[i];
528 if (page) {
529#ifdef CONFIG_AFS_FSCACHE
530 fscache_uncache_page(vnode->cache, page);
531#endif
532 SetPageError(page);
533 unlock_page(page);
534 }
535 }
536
537 afs_put_read(req);
538 return ret;
539}
540
541/*
David Howells9b3f26c2009-04-03 16:42:41 +0100542 * read a set of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 */
David Howells9b3f26c2009-04-03 16:42:41 +0100544static int afs_readpages(struct file *file, struct address_space *mapping,
545 struct list_head *pages, unsigned nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
David Howells215804a2017-11-02 15:27:52 +0000547 struct key *key = afs_file_key(file);
David Howells9b3f26c2009-04-03 16:42:41 +0100548 struct afs_vnode *vnode;
549 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Al Virof6d335c2010-05-21 15:27:09 +0100551 _enter("{%d},{%lu},,%d",
552 key_serial(key), mapping->host->i_ino, nr_pages);
553
554 ASSERT(key != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
David Howells9b3f26c2009-04-03 16:42:41 +0100556 vnode = AFS_FS_I(mapping->host);
Dan Carpenterad2a8e62012-03-20 16:58:06 +0000557 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
David Howells9b3f26c2009-04-03 16:42:41 +0100558 _leave(" = -ESTALE");
559 return -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561
David Howells9b3f26c2009-04-03 16:42:41 +0100562 /* attempt to read as many of the pages as possible */
563#ifdef CONFIG_AFS_FSCACHE
564 ret = fscache_read_or_alloc_pages(vnode->cache,
565 mapping,
566 pages,
567 &nr_pages,
568 afs_file_readpage_read_complete,
569 NULL,
570 mapping_gfp_mask(mapping));
571#else
572 ret = -ENOBUFS;
573#endif
574
575 switch (ret) {
576 /* all pages are being read from the cache */
577 case 0:
578 BUG_ON(!list_empty(pages));
579 BUG_ON(nr_pages != 0);
580 _leave(" = 0 [reading all]");
581 return 0;
582
583 /* there were pages that couldn't be read from the cache */
584 case -ENODATA:
585 case -ENOBUFS:
586 break;
587
588 /* other error */
589 default:
590 _leave(" = %d", ret);
591 return ret;
592 }
593
David Howells91b467e02017-01-05 10:38:35 +0000594 while (!list_empty(pages)) {
595 ret = afs_readpages_one(file, mapping, pages);
596 if (ret < 0)
597 break;
598 }
David Howells9b3f26c2009-04-03 16:42:41 +0100599
600 _leave(" = %d [netting]", ret);
601 return ret;
David Howellsec268152007-04-26 15:49:28 -0700602}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604/*
David Howells9b3f26c2009-04-03 16:42:41 +0100605 * invalidate part or all of a page
606 * - release a page and clean up its private data if offset is 0 (indicating
607 * the entire page)
608 */
Lukas Czernerd47992f2013-05-21 23:17:23 -0400609static void afs_invalidatepage(struct page *page, unsigned int offset,
610 unsigned int length)
David Howells9b3f26c2009-04-03 16:42:41 +0100611{
David Howells13524ab2017-11-02 15:27:53 +0000612 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
613 unsigned long priv;
614
Lukas Czernerd47992f2013-05-21 23:17:23 -0400615 _enter("{%lu},%u,%u", page->index, offset, length);
David Howells9b3f26c2009-04-03 16:42:41 +0100616
617 BUG_ON(!PageLocked(page));
618
619 /* we clean up only if the entire page is being invalidated */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300620 if (offset == 0 && length == PAGE_SIZE) {
David Howells9b3f26c2009-04-03 16:42:41 +0100621#ifdef CONFIG_AFS_FSCACHE
622 if (PageFsCache(page)) {
623 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
624 fscache_wait_on_page_write(vnode->cache, page);
625 fscache_uncache_page(vnode->cache, page);
David Howells9b3f26c2009-04-03 16:42:41 +0100626 }
627#endif
628
629 if (PagePrivate(page)) {
David Howellsfa04a402020-10-21 13:22:19 +0100630 priv = (unsigned long)detach_page_private(page);
David Howells13524ab2017-11-02 15:27:53 +0000631 trace_afs_page_dirty(vnode, tracepoint_string("inval"),
632 page->index, priv);
David Howells9b3f26c2009-04-03 16:42:41 +0100633 }
634 }
635
636 _leave("");
637}
638
639/*
640 * release a page and clean up its private state if it's not busy
641 * - return true if the page can now be released, false if not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 */
David Howells416351f2007-05-09 02:33:45 -0700643static int afs_releasepage(struct page *page, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
David Howells416351f2007-05-09 02:33:45 -0700645 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
David Howells13524ab2017-11-02 15:27:53 +0000646 unsigned long priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
David Howells3b6492d2018-10-20 00:57:57 +0100648 _enter("{{%llx:%llu}[%lu],%lx},%x",
David Howells416351f2007-05-09 02:33:45 -0700649 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
650 gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
David Howells9b3f26c2009-04-03 16:42:41 +0100652 /* deny if page is being written to the cache and the caller hasn't
653 * elected to wait */
654#ifdef CONFIG_AFS_FSCACHE
David Howells201a1542009-11-19 18:11:35 +0000655 if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) {
656 _leave(" = F [cache busy]");
657 return 0;
David Howells9b3f26c2009-04-03 16:42:41 +0100658 }
659#endif
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (PagePrivate(page)) {
David Howellsfa04a402020-10-21 13:22:19 +0100662 priv = (unsigned long)detach_page_private(page);
David Howells13524ab2017-11-02 15:27:53 +0000663 trace_afs_page_dirty(vnode, tracepoint_string("rel"),
664 page->index, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666
David Howells9b3f26c2009-04-03 16:42:41 +0100667 /* indicate that the page can be released */
668 _leave(" = T");
669 return 1;
David Howellsec268152007-04-26 15:49:28 -0700670}
David Howells1cf7a152017-11-02 15:27:52 +0000671
672/*
673 * Handle setting up a memory mapping on an AFS file.
674 */
675static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
676{
677 int ret;
678
679 ret = generic_file_mmap(file, vma);
680 if (ret == 0)
681 vma->vm_ops = &afs_vm_ops;
682 return ret;
683}