blob: 960b64268623e4cf03f84f6f2587bfe24479ab87 [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,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
Christoph Hellwigf5e54d62006-06-28 04:26:44 -070048const struct address_space_operations afs_fs_aops = {
David Howells416351f2007-05-09 02:33:45 -070049 .readpage = afs_readpage,
David Howells9b3f26c2009-04-03 16:42:41 +010050 .readpages = afs_readpages,
David Howells31143d52007-05-09 02:33:46 -070051 .set_page_dirty = afs_set_page_dirty,
52 .launder_page = afs_launder_page,
David Howells416351f2007-05-09 02:33:45 -070053 .releasepage = afs_releasepage,
54 .invalidatepage = afs_invalidatepage,
Nick Piggin15b46502008-10-15 22:04:32 -070055 .write_begin = afs_write_begin,
56 .write_end = afs_write_end,
David Howells31143d52007-05-09 02:33:46 -070057 .writepage = afs_writepage,
58 .writepages = afs_writepages,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
David Howells1cf7a152017-11-02 15:27:52 +000061static const struct vm_operations_struct afs_vm_ops = {
62 .fault = filemap_fault,
63 .map_pages = filemap_map_pages,
64 .page_mkwrite = afs_page_mkwrite,
65};
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
David Howells4343d002017-11-02 15:27:52 +000068 * Discard a pin on a writeback key.
69 */
70void afs_put_wb_key(struct afs_wb_key *wbk)
71{
David Howellse49c7b22020-04-10 20:51:51 +010072 if (wbk && refcount_dec_and_test(&wbk->usage)) {
David Howells4343d002017-11-02 15:27:52 +000073 key_put(wbk->key);
74 kfree(wbk);
75 }
76}
77
78/*
79 * Cache key for writeback.
80 */
81int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af)
82{
83 struct afs_wb_key *wbk, *p;
84
85 wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL);
86 if (!wbk)
87 return -ENOMEM;
88 refcount_set(&wbk->usage, 2);
89 wbk->key = af->key;
90
91 spin_lock(&vnode->wb_lock);
92 list_for_each_entry(p, &vnode->wb_keys, vnode_link) {
93 if (p->key == wbk->key)
94 goto found;
95 }
96
97 key_get(wbk->key);
98 list_add_tail(&wbk->vnode_link, &vnode->wb_keys);
99 spin_unlock(&vnode->wb_lock);
100 af->wb = wbk;
101 return 0;
102
103found:
104 refcount_inc(&p->usage);
105 spin_unlock(&vnode->wb_lock);
106 af->wb = p;
107 kfree(wbk);
108 return 0;
109}
110
111/*
David Howells00d3b7a2007-04-26 15:57:07 -0700112 * open an AFS file or directory and attach a key to it
113 */
114int afs_open(struct inode *inode, struct file *file)
115{
116 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells215804a2017-11-02 15:27:52 +0000117 struct afs_file *af;
David Howells00d3b7a2007-04-26 15:57:07 -0700118 struct key *key;
David Howells260a9802007-04-26 15:59:35 -0700119 int ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700120
David Howells3b6492d2018-10-20 00:57:57 +0100121 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
David Howells00d3b7a2007-04-26 15:57:07 -0700122
123 key = afs_request_key(vnode->volume->cell);
124 if (IS_ERR(key)) {
David Howells215804a2017-11-02 15:27:52 +0000125 ret = PTR_ERR(key);
126 goto error;
127 }
128
129 af = kzalloc(sizeof(*af), GFP_KERNEL);
130 if (!af) {
131 ret = -ENOMEM;
132 goto error_key;
David Howells00d3b7a2007-04-26 15:57:07 -0700133 }
David Howells4343d002017-11-02 15:27:52 +0000134 af->key = key;
David Howells00d3b7a2007-04-26 15:57:07 -0700135
David Howells260a9802007-04-26 15:59:35 -0700136 ret = afs_validate(vnode, key);
David Howells215804a2017-11-02 15:27:52 +0000137 if (ret < 0)
138 goto error_af;
David Howells260a9802007-04-26 15:59:35 -0700139
David Howells4343d002017-11-02 15:27:52 +0000140 if (file->f_mode & FMODE_WRITE) {
141 ret = afs_cache_wb_key(vnode, af);
142 if (ret < 0)
143 goto error_af;
144 }
David Howells5a813272018-04-06 14:17:26 +0100145
146 if (file->f_flags & O_TRUNC)
147 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
David Howells4343d002017-11-02 15:27:52 +0000148
David Howells215804a2017-11-02 15:27:52 +0000149 file->private_data = af;
David Howells00d3b7a2007-04-26 15:57:07 -0700150 _leave(" = 0");
151 return 0;
David Howells215804a2017-11-02 15:27:52 +0000152
153error_af:
154 kfree(af);
155error_key:
156 key_put(key);
157error:
158 _leave(" = %d", ret);
159 return ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700160}
161
162/*
163 * release an AFS file or directory and discard its key
164 */
165int afs_release(struct inode *inode, struct file *file)
166{
167 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells215804a2017-11-02 15:27:52 +0000168 struct afs_file *af = file->private_data;
David Howellsa1b879e2019-05-15 12:09:17 +0100169 int ret = 0;
David Howells00d3b7a2007-04-26 15:57:07 -0700170
David Howells3b6492d2018-10-20 00:57:57 +0100171 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
David Howells00d3b7a2007-04-26 15:57:07 -0700172
David Howells5a813272018-04-06 14:17:26 +0100173 if ((file->f_mode & FMODE_WRITE))
David Howellsa1b879e2019-05-15 12:09:17 +0100174 ret = vfs_fsync(file, 0);
David Howells5a813272018-04-06 14:17:26 +0100175
David Howells215804a2017-11-02 15:27:52 +0000176 file->private_data = NULL;
David Howells4343d002017-11-02 15:27:52 +0000177 if (af->wb)
178 afs_put_wb_key(af->wb);
David Howells215804a2017-11-02 15:27:52 +0000179 key_put(af->key);
180 kfree(af);
David Howells4343d002017-11-02 15:27:52 +0000181 afs_prune_wb_keys(vnode);
David Howellsa1b879e2019-05-15 12:09:17 +0100182 _leave(" = %d", ret);
183 return ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700184}
185
David Howells196ee9c2017-01-05 10:38:34 +0000186/*
187 * Dispose of a ref to a read record.
188 */
189void afs_put_read(struct afs_read *req)
190{
191 int i;
192
David Howellsf3ddee82018-04-06 14:17:25 +0100193 if (refcount_dec_and_test(&req->usage)) {
Jia-Ju Baia6eed4a2019-07-30 14:38:51 +0100194 if (req->pages) {
195 for (i = 0; i < req->nr_pages; i++)
196 if (req->pages[i])
197 put_page(req->pages[i]);
198 if (req->pages != req->array)
199 kfree(req->pages);
200 }
David Howells196ee9c2017-01-05 10:38:34 +0000201 kfree(req);
202 }
203}
204
Matt Kraai6566abd2009-04-17 12:56:38 +0100205#ifdef CONFIG_AFS_FSCACHE
David Howells00d3b7a2007-04-26 15:57:07 -0700206/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * deal with notification that a page was read from the cache
208 */
David Howells9b3f26c2009-04-03 16:42:41 +0100209static void afs_file_readpage_read_complete(struct page *page,
210 void *data,
211 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
David Howells9b3f26c2009-04-03 16:42:41 +0100213 _enter("%p,%p,%d", page, data, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
David Howells9b3f26c2009-04-03 16:42:41 +0100215 /* if the read completes with an error, we just unlock the page and let
216 * the VM reissue the readpage */
217 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 SetPageUptodate(page);
219 unlock_page(page);
David Howellsec268152007-04-26 15:49:28 -0700220}
Matt Kraai6566abd2009-04-17 12:56:38 +0100221#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
David Howellse49c7b22020-04-10 20:51:51 +0100223static void afs_fetch_data_success(struct afs_operation *op)
224{
225 struct afs_vnode *vnode = op->file[0].vnode;
226
227 _enter("op=%08x", op->debug_id);
David Howellse49c7b22020-04-10 20:51:51 +0100228 afs_vnode_commit_status(op, &op->file[0]);
229 afs_stat_v(vnode, n_fetches);
230 atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes);
231}
232
233static void afs_fetch_data_put(struct afs_operation *op)
234{
235 afs_put_read(op->fetch.req);
236}
237
238static const struct afs_operation_ops afs_fetch_data_operation = {
239 .issue_afs_rpc = afs_fs_fetch_data,
240 .issue_yfs_rpc = yfs_fs_fetch_data,
241 .success = afs_fetch_data_success,
David Howells728279a2020-06-16 00:34:09 +0100242 .aborted = afs_check_for_remote_deletion,
David Howellse49c7b22020-04-10 20:51:51 +0100243 .put = afs_fetch_data_put,
244};
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246/*
David Howellsd2ddc772017-11-02 15:27:50 +0000247 * Fetch file data from the volume.
248 */
David Howells0b9c0172019-11-21 09:12:17 +0000249int afs_fetch_data(struct afs_vnode *vnode, struct key *key, struct afs_read *req)
David Howellsd2ddc772017-11-02 15:27:50 +0000250{
David Howellse49c7b22020-04-10 20:51:51 +0100251 struct afs_operation *op;
David Howellsd2ddc772017-11-02 15:27:50 +0000252
David Howells3b6492d2018-10-20 00:57:57 +0100253 _enter("%s{%llx:%llu.%u},%x,,,",
David Howellsd2ddc772017-11-02 15:27:50 +0000254 vnode->volume->name,
255 vnode->fid.vid,
256 vnode->fid.vnode,
257 vnode->fid.unique,
258 key_serial(key));
259
David Howellse49c7b22020-04-10 20:51:51 +0100260 op = afs_alloc_operation(key, vnode->volume);
261 if (IS_ERR(op))
262 return PTR_ERR(op);
David Howellsa58823a2019-05-09 15:16:10 +0100263
David Howellse49c7b22020-04-10 20:51:51 +0100264 afs_op_set_vnode(op, 0, vnode);
David Howellsa58823a2019-05-09 15:16:10 +0100265
David Howellse49c7b22020-04-10 20:51:51 +0100266 op->fetch.req = afs_get_read(req);
267 op->ops = &afs_fetch_data_operation;
268 return afs_do_sync_operation(op);
David Howellsd2ddc772017-11-02 15:27:50 +0000269}
270
271/*
Al Virof6d335c2010-05-21 15:27:09 +0100272 * read page from file, directory or symlink, given a key to use
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 */
Al Virof6d335c2010-05-21 15:27:09 +0100274int afs_page_filler(void *data, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Al Virof6d335c2010-05-21 15:27:09 +0100276 struct inode *inode = page->mapping->host;
277 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells196ee9c2017-01-05 10:38:34 +0000278 struct afs_read *req;
Al Virof6d335c2010-05-21 15:27:09 +0100279 struct key *key = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 int ret;
281
David Howells00d3b7a2007-04-26 15:57:07 -0700282 _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Matt Mackallcd7619d2005-05-01 08:59:01 -0700284 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 ret = -ESTALE;
David Howells08e0e7c2007-04-26 15:55:03 -0700287 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 goto error;
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 /* is it cached? */
David Howells9b3f26c2009-04-03 16:42:41 +0100291#ifdef CONFIG_AFS_FSCACHE
292 ret = fscache_read_or_alloc_page(vnode->cache,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 page,
294 afs_file_readpage_read_complete,
295 NULL,
296 GFP_KERNEL);
297#else
298 ret = -ENOBUFS;
299#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 switch (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 /* read BIO submitted (page in cache) */
302 case 0:
303 break;
304
David Howells9b3f26c2009-04-03 16:42:41 +0100305 /* page not yet cached */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 case -ENODATA:
David Howells9b3f26c2009-04-03 16:42:41 +0100307 _debug("cache said ENODATA");
308 goto go_on;
309
310 /* page will not be cached */
311 case -ENOBUFS:
312 _debug("cache said ENOBUFS");
Gustavo A. R. Silvae690c9e2019-01-10 15:52:25 -0600313
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500314 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 default:
David Howells9b3f26c2009-04-03 16:42:41 +0100316 go_on:
Zhengyuan Liuee102582019-06-20 18:12:17 +0100317 req = kzalloc(struct_size(req, array, 1), GFP_KERNEL);
David Howells196ee9c2017-01-05 10:38:34 +0000318 if (!req)
319 goto enomem;
320
David Howells6db3ac32017-03-16 16:27:44 +0000321 /* We request a full page. If the page is a partial one at the
322 * end of the file, the server will return a short read and the
323 * unmarshalling code will clear the unfilled space.
324 */
David Howellsf3ddee82018-04-06 14:17:25 +0100325 refcount_set(&req->usage, 1);
David Howells196ee9c2017-01-05 10:38:34 +0000326 req->pos = (loff_t)page->index << PAGE_SHIFT;
David Howells6db3ac32017-03-16 16:27:44 +0000327 req->len = PAGE_SIZE;
David Howells196ee9c2017-01-05 10:38:34 +0000328 req->nr_pages = 1;
David Howellsf3ddee82018-04-06 14:17:25 +0100329 req->pages = req->array;
David Howells196ee9c2017-01-05 10:38:34 +0000330 req->pages[0] = page;
331 get_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /* read the contents of the file from the server into the
334 * page */
David Howellsd2ddc772017-11-02 15:27:50 +0000335 ret = afs_fetch_data(vnode, key, req);
David Howells196ee9c2017-01-05 10:38:34 +0000336 afs_put_read(req);
David Howellsdab17c12017-11-02 15:27:52 +0000337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (ret < 0) {
David Howells08e0e7c2007-04-26 15:55:03 -0700339 if (ret == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 _debug("got NOENT from server"
341 " - marking file deleted and stale");
David Howells08e0e7c2007-04-26 15:55:03 -0700342 set_bit(AFS_VNODE_DELETED, &vnode->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 ret = -ESTALE;
344 }
David Howells9b3f26c2009-04-03 16:42:41 +0100345
346#ifdef CONFIG_AFS_FSCACHE
347 fscache_uncache_page(vnode->cache, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348#endif
David Howells9b3f26c2009-04-03 16:42:41 +0100349 BUG_ON(PageFsCache(page));
David Howells68ae8492017-03-16 16:27:48 +0000350
351 if (ret == -EINTR ||
352 ret == -ENOMEM ||
353 ret == -ERESTARTSYS ||
354 ret == -EAGAIN)
355 goto error;
356 goto io_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358
359 SetPageUptodate(page);
360
David Howells9b3f26c2009-04-03 16:42:41 +0100361 /* send the page to the cache */
362#ifdef CONFIG_AFS_FSCACHE
363 if (PageFsCache(page) &&
David Howellsee1235a2018-04-04 13:41:28 +0100364 fscache_write_page(vnode->cache, page, vnode->status.size,
365 GFP_KERNEL) != 0) {
David Howells9b3f26c2009-04-03 16:42:41 +0100366 fscache_uncache_page(vnode->cache, page);
367 BUG_ON(PageFsCache(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369#endif
David Howells9b3f26c2009-04-03 16:42:41 +0100370 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372
373 _leave(" = 0");
374 return 0;
375
David Howells68ae8492017-03-16 16:27:48 +0000376io_error:
377 SetPageError(page);
378 goto error;
David Howells196ee9c2017-01-05 10:38:34 +0000379enomem:
380 ret = -ENOMEM;
David Howells08e0e7c2007-04-26 15:55:03 -0700381error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 _leave(" = %d", ret);
384 return ret;
David Howellsec268152007-04-26 15:49:28 -0700385}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387/*
Al Virof6d335c2010-05-21 15:27:09 +0100388 * read page from file, directory or symlink, given a file to nominate the key
389 * to be used
390 */
391static int afs_readpage(struct file *file, struct page *page)
392{
393 struct key *key;
394 int ret;
395
396 if (file) {
David Howells215804a2017-11-02 15:27:52 +0000397 key = afs_file_key(file);
Al Virof6d335c2010-05-21 15:27:09 +0100398 ASSERT(key != NULL);
399 ret = afs_page_filler(key, page);
400 } else {
401 struct inode *inode = page->mapping->host;
David Howellsd2ddc772017-11-02 15:27:50 +0000402 key = afs_request_key(AFS_FS_S(inode->i_sb)->cell);
Al Virof6d335c2010-05-21 15:27:09 +0100403 if (IS_ERR(key)) {
404 ret = PTR_ERR(key);
405 } else {
406 ret = afs_page_filler(key, page);
407 key_put(key);
408 }
409 }
410 return ret;
411}
412
413/*
David Howells91b467e02017-01-05 10:38:35 +0000414 * Make pages available as they're filled.
415 */
David Howellsa58823a2019-05-09 15:16:10 +0100416static void afs_readpages_page_done(struct afs_read *req)
David Howells91b467e02017-01-05 10:38:35 +0000417{
Arnd Bergmann51c89e62017-01-13 14:46:19 +0000418#ifdef CONFIG_AFS_FSCACHE
David Howellsa58823a2019-05-09 15:16:10 +0100419 struct afs_vnode *vnode = req->vnode;
Arnd Bergmann51c89e62017-01-13 14:46:19 +0000420#endif
David Howells91b467e02017-01-05 10:38:35 +0000421 struct page *page = req->pages[req->index];
422
423 req->pages[req->index] = NULL;
424 SetPageUptodate(page);
425
426 /* send the page to the cache */
427#ifdef CONFIG_AFS_FSCACHE
428 if (PageFsCache(page) &&
David Howellsee1235a2018-04-04 13:41:28 +0100429 fscache_write_page(vnode->cache, page, vnode->status.size,
430 GFP_KERNEL) != 0) {
David Howells91b467e02017-01-05 10:38:35 +0000431 fscache_uncache_page(vnode->cache, page);
432 BUG_ON(PageFsCache(page));
433 }
434#endif
435 unlock_page(page);
436 put_page(page);
437}
438
439/*
440 * Read a contiguous set of pages.
441 */
442static int afs_readpages_one(struct file *file, struct address_space *mapping,
443 struct list_head *pages)
444{
445 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
446 struct afs_read *req;
447 struct list_head *p;
448 struct page *first, *page;
David Howells215804a2017-11-02 15:27:52 +0000449 struct key *key = afs_file_key(file);
David Howells91b467e02017-01-05 10:38:35 +0000450 pgoff_t index;
451 int ret, n, i;
452
453 /* Count the number of contiguous pages at the front of the list. Note
454 * that the list goes prev-wards rather than next-wards.
455 */
Nikolay Borisovf86196e2019-01-03 15:29:02 -0800456 first = lru_to_page(pages);
David Howells91b467e02017-01-05 10:38:35 +0000457 index = first->index + 1;
458 n = 1;
459 for (p = first->lru.prev; p != pages; p = p->prev) {
460 page = list_entry(p, struct page, lru);
461 if (page->index != index)
462 break;
463 index++;
464 n++;
465 }
466
Zhengyuan Liuee102582019-06-20 18:12:17 +0100467 req = kzalloc(struct_size(req, array, n), GFP_NOFS);
David Howells91b467e02017-01-05 10:38:35 +0000468 if (!req)
469 return -ENOMEM;
470
David Howellsf3ddee82018-04-06 14:17:25 +0100471 refcount_set(&req->usage, 1);
David Howellsa58823a2019-05-09 15:16:10 +0100472 req->vnode = vnode;
David Howells91b467e02017-01-05 10:38:35 +0000473 req->page_done = afs_readpages_page_done;
474 req->pos = first->index;
475 req->pos <<= PAGE_SHIFT;
David Howellsf3ddee82018-04-06 14:17:25 +0100476 req->pages = req->array;
David Howells91b467e02017-01-05 10:38:35 +0000477
478 /* Transfer the pages to the request. We add them in until one fails
479 * to add to the LRU and then we stop (as that'll make a hole in the
480 * contiguous run.
481 *
482 * Note that it's possible for the file size to change whilst we're
483 * doing this, but we rely on the server returning less than we asked
484 * for if the file shrank. We also rely on this to deal with a partial
485 * page at the end of the file.
486 */
487 do {
Nikolay Borisovf86196e2019-01-03 15:29:02 -0800488 page = lru_to_page(pages);
David Howells91b467e02017-01-05 10:38:35 +0000489 list_del(&page->lru);
490 index = page->index;
491 if (add_to_page_cache_lru(page, mapping, index,
492 readahead_gfp_mask(mapping))) {
493#ifdef CONFIG_AFS_FSCACHE
494 fscache_uncache_page(vnode->cache, page);
495#endif
496 put_page(page);
497 break;
498 }
499
500 req->pages[req->nr_pages++] = page;
501 req->len += PAGE_SIZE;
502 } while (req->nr_pages < n);
503
504 if (req->nr_pages == 0) {
505 kfree(req);
506 return 0;
507 }
508
David Howellsd2ddc772017-11-02 15:27:50 +0000509 ret = afs_fetch_data(vnode, key, req);
David Howells91b467e02017-01-05 10:38:35 +0000510 if (ret < 0)
511 goto error;
512
513 task_io_account_read(PAGE_SIZE * req->nr_pages);
514 afs_put_read(req);
515 return 0;
516
517error:
518 if (ret == -ENOENT) {
519 _debug("got NOENT from server"
520 " - marking file deleted and stale");
521 set_bit(AFS_VNODE_DELETED, &vnode->flags);
522 ret = -ESTALE;
523 }
524
525 for (i = 0; i < req->nr_pages; i++) {
526 page = req->pages[i];
527 if (page) {
528#ifdef CONFIG_AFS_FSCACHE
529 fscache_uncache_page(vnode->cache, page);
530#endif
531 SetPageError(page);
532 unlock_page(page);
533 }
534 }
535
536 afs_put_read(req);
537 return ret;
538}
539
540/*
David Howells9b3f26c2009-04-03 16:42:41 +0100541 * read a set of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 */
David Howells9b3f26c2009-04-03 16:42:41 +0100543static int afs_readpages(struct file *file, struct address_space *mapping,
544 struct list_head *pages, unsigned nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
David Howells215804a2017-11-02 15:27:52 +0000546 struct key *key = afs_file_key(file);
David Howells9b3f26c2009-04-03 16:42:41 +0100547 struct afs_vnode *vnode;
548 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Al Virof6d335c2010-05-21 15:27:09 +0100550 _enter("{%d},{%lu},,%d",
551 key_serial(key), mapping->host->i_ino, nr_pages);
552
553 ASSERT(key != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
David Howells9b3f26c2009-04-03 16:42:41 +0100555 vnode = AFS_FS_I(mapping->host);
Dan Carpenterad2a8e62012-03-20 16:58:06 +0000556 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
David Howells9b3f26c2009-04-03 16:42:41 +0100557 _leave(" = -ESTALE");
558 return -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560
David Howells9b3f26c2009-04-03 16:42:41 +0100561 /* attempt to read as many of the pages as possible */
562#ifdef CONFIG_AFS_FSCACHE
563 ret = fscache_read_or_alloc_pages(vnode->cache,
564 mapping,
565 pages,
566 &nr_pages,
567 afs_file_readpage_read_complete,
568 NULL,
569 mapping_gfp_mask(mapping));
570#else
571 ret = -ENOBUFS;
572#endif
573
574 switch (ret) {
575 /* all pages are being read from the cache */
576 case 0:
577 BUG_ON(!list_empty(pages));
578 BUG_ON(nr_pages != 0);
579 _leave(" = 0 [reading all]");
580 return 0;
581
582 /* there were pages that couldn't be read from the cache */
583 case -ENODATA:
584 case -ENOBUFS:
585 break;
586
587 /* other error */
588 default:
589 _leave(" = %d", ret);
590 return ret;
591 }
592
David Howells91b467e02017-01-05 10:38:35 +0000593 while (!list_empty(pages)) {
594 ret = afs_readpages_one(file, mapping, pages);
595 if (ret < 0)
596 break;
597 }
David Howells9b3f26c2009-04-03 16:42:41 +0100598
599 _leave(" = %d [netting]", ret);
600 return ret;
David Howellsec268152007-04-26 15:49:28 -0700601}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603/*
David Howellsf86726a2020-10-22 14:08:23 +0100604 * Adjust the dirty region of the page on truncation or full invalidation,
605 * getting rid of the markers altogether if the region is entirely invalidated.
606 */
607static void afs_invalidate_dirty(struct page *page, unsigned int offset,
608 unsigned int length)
609{
610 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
611 unsigned long priv;
612 unsigned int f, t, end = offset + length;
613
614 priv = page_private(page);
615
616 /* we clean up only if the entire page is being invalidated */
617 if (offset == 0 && length == thp_size(page))
618 goto full_invalidate;
619
620 /* If the page was dirtied by page_mkwrite(), the PTE stays writable
621 * and we don't get another notification to tell us to expand it
622 * again.
623 */
624 if (afs_is_page_dirty_mmapped(priv))
625 return;
626
627 /* We may need to shorten the dirty region */
628 f = afs_page_dirty_from(priv);
629 t = afs_page_dirty_to(priv);
630
631 if (t <= offset || f >= end)
632 return; /* Doesn't overlap */
633
634 if (f < offset && t > end)
635 return; /* Splits the dirty region - just absorb it */
636
637 if (f >= offset && t <= end)
638 goto undirty;
639
640 if (f < offset)
641 t = offset;
642 else
643 f = end;
644 if (f == t)
645 goto undirty;
646
647 priv = afs_page_dirty(f, t);
648 set_page_private(page, priv);
649 trace_afs_page_dirty(vnode, tracepoint_string("trunc"), page->index, priv);
650 return;
651
652undirty:
653 trace_afs_page_dirty(vnode, tracepoint_string("undirty"), page->index, priv);
654 clear_page_dirty_for_io(page);
655full_invalidate:
656 priv = (unsigned long)detach_page_private(page);
657 trace_afs_page_dirty(vnode, tracepoint_string("inval"), page->index, priv);
658}
659
660/*
David Howells9b3f26c2009-04-03 16:42:41 +0100661 * invalidate part or all of a page
662 * - release a page and clean up its private data if offset is 0 (indicating
663 * the entire page)
664 */
Lukas Czernerd47992f2013-05-21 23:17:23 -0400665static void afs_invalidatepage(struct page *page, unsigned int offset,
666 unsigned int length)
David Howells9b3f26c2009-04-03 16:42:41 +0100667{
Lukas Czernerd47992f2013-05-21 23:17:23 -0400668 _enter("{%lu},%u,%u", page->index, offset, length);
David Howells9b3f26c2009-04-03 16:42:41 +0100669
670 BUG_ON(!PageLocked(page));
671
David Howellsf86726a2020-10-22 14:08:23 +0100672#ifdef CONFIG_AFS_FSCACHE
David Howells9b3f26c2009-04-03 16:42:41 +0100673 /* we clean up only if the entire page is being invalidated */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300674 if (offset == 0 && length == PAGE_SIZE) {
David Howells9b3f26c2009-04-03 16:42:41 +0100675 if (PageFsCache(page)) {
676 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
677 fscache_wait_on_page_write(vnode->cache, page);
678 fscache_uncache_page(vnode->cache, page);
David Howells9b3f26c2009-04-03 16:42:41 +0100679 }
David Howellsf86726a2020-10-22 14:08:23 +0100680 }
David Howells9b3f26c2009-04-03 16:42:41 +0100681#endif
682
David Howellsf86726a2020-10-22 14:08:23 +0100683 if (PagePrivate(page))
684 afs_invalidate_dirty(page, offset, length);
David Howells9b3f26c2009-04-03 16:42:41 +0100685
686 _leave("");
687}
688
689/*
690 * release a page and clean up its private state if it's not busy
691 * - return true if the page can now be released, false if not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 */
David Howells416351f2007-05-09 02:33:45 -0700693static int afs_releasepage(struct page *page, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
David Howells416351f2007-05-09 02:33:45 -0700695 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
David Howells13524ab2017-11-02 15:27:53 +0000696 unsigned long priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
David Howells3b6492d2018-10-20 00:57:57 +0100698 _enter("{{%llx:%llu}[%lu],%lx},%x",
David Howells416351f2007-05-09 02:33:45 -0700699 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
700 gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
David Howells9b3f26c2009-04-03 16:42:41 +0100702 /* deny if page is being written to the cache and the caller hasn't
703 * elected to wait */
704#ifdef CONFIG_AFS_FSCACHE
David Howells201a1542009-11-19 18:11:35 +0000705 if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) {
706 _leave(" = F [cache busy]");
707 return 0;
David Howells9b3f26c2009-04-03 16:42:41 +0100708 }
709#endif
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (PagePrivate(page)) {
David Howellsfa04a402020-10-21 13:22:19 +0100712 priv = (unsigned long)detach_page_private(page);
David Howells13524ab2017-11-02 15:27:53 +0000713 trace_afs_page_dirty(vnode, tracepoint_string("rel"),
714 page->index, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716
David Howells9b3f26c2009-04-03 16:42:41 +0100717 /* indicate that the page can be released */
718 _leave(" = T");
719 return 1;
David Howellsec268152007-04-26 15:49:28 -0700720}
David Howells1cf7a152017-11-02 15:27:52 +0000721
722/*
723 * Handle setting up a memory mapping on an AFS file.
724 */
725static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
726{
727 int ret;
728
729 ret = generic_file_mmap(file, vma);
730 if (ret == 0)
731 vma->vm_ops = &afs_vm_ops;
732 return ret;
733}