blob: db035ae2a13451dc97fc10e97a6aeb50088898a4 [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 (dhowells@redhat.com)
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 Howells91b467e2017-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>
David Howells5cbf0392020-02-06 14:22:29 +000017#include <linux/netfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "internal.h"
19
David Howells1cf7a152017-11-02 15:27:52 +000020static int afs_file_mmap(struct file *file, struct vm_area_struct *vma);
David Howells416351f2007-05-09 02:33:45 -070021static int afs_readpage(struct file *file, struct page *page);
Lukas Czernerd47992f2013-05-21 23:17:23 -040022static void afs_invalidatepage(struct page *page, unsigned int offset,
23 unsigned int length);
David Howells416351f2007-05-09 02:33:45 -070024static int afs_releasepage(struct page *page, gfp_t gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
David Howells5cbf0392020-02-06 14:22:29 +000026static void afs_readahead(struct readahead_control *ractl);
David Howells9b3f26c2009-04-03 16:42:41 +010027
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 Howells5cbf0392020-02-06 14:22:29 +000050 .readahead = afs_readahead,
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/*
David Howells5cbf0392020-02-06 14:22:29 +0000187 * Allocate a new read record.
David Howellsc4508462020-02-06 14:22:28 +0000188 */
David Howells5cbf0392020-02-06 14:22:29 +0000189struct afs_read *afs_alloc_read(gfp_t gfp)
David Howellsc4508462020-02-06 14:22:28 +0000190{
David Howells5cbf0392020-02-06 14:22:29 +0000191 struct afs_read *req;
David Howellsc4508462020-02-06 14:22:28 +0000192
David Howells5cbf0392020-02-06 14:22:29 +0000193 req = kzalloc(sizeof(struct afs_read), gfp);
194 if (req)
195 refcount_set(&req->usage, 1);
David Howellsc4508462020-02-06 14:22:28 +0000196
David Howells5cbf0392020-02-06 14:22:29 +0000197 return req;
David Howellsc4508462020-02-06 14:22:28 +0000198}
199
200/*
David Howells196ee9c2017-01-05 10:38:34 +0000201 * Dispose of a ref to a read record.
202 */
203void afs_put_read(struct afs_read *req)
204{
David Howellsf3ddee82018-04-06 14:17:25 +0100205 if (refcount_dec_and_test(&req->usage)) {
David Howellsc4508462020-02-06 14:22:28 +0000206 if (req->cleanup)
207 req->cleanup(req);
David Howellsc69bf472020-02-06 14:22:27 +0000208 key_put(req->key);
David Howells196ee9c2017-01-05 10:38:34 +0000209 kfree(req);
210 }
211}
212
David Howellsdc419182020-09-18 09:11:15 +0100213static void afs_fetch_data_notify(struct afs_operation *op)
214{
215 struct afs_read *req = op->fetch.req;
David Howells5cbf0392020-02-06 14:22:29 +0000216 struct netfs_read_subrequest *subreq = req->subreq;
David Howellsdc419182020-09-18 09:11:15 +0100217 int error = op->error;
218
219 if (error == -ECONNABORTED)
220 error = afs_abort_to_error(op->ac.abort_code);
221 req->error = error;
222
David Howells5cbf0392020-02-06 14:22:29 +0000223 if (subreq) {
224 __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
225 netfs_subreq_terminated(subreq, error ?: req->actual_len, false);
226 req->subreq = NULL;
227 } else if (req->done) {
David Howellsdc419182020-09-18 09:11:15 +0100228 req->done(req);
David Howells5cbf0392020-02-06 14:22:29 +0000229 }
David Howellsdc419182020-09-18 09:11:15 +0100230}
231
David Howellse49c7b22020-04-10 20:51:51 +0100232static void afs_fetch_data_success(struct afs_operation *op)
233{
234 struct afs_vnode *vnode = op->file[0].vnode;
235
236 _enter("op=%08x", op->debug_id);
David Howellse49c7b22020-04-10 20:51:51 +0100237 afs_vnode_commit_status(op, &op->file[0]);
238 afs_stat_v(vnode, n_fetches);
239 atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes);
David Howellsdc419182020-09-18 09:11:15 +0100240 afs_fetch_data_notify(op);
David Howellse49c7b22020-04-10 20:51:51 +0100241}
242
243static void afs_fetch_data_put(struct afs_operation *op)
244{
David Howellsc4508462020-02-06 14:22:28 +0000245 op->fetch.req->error = op->error;
David Howellse49c7b22020-04-10 20:51:51 +0100246 afs_put_read(op->fetch.req);
247}
248
249static const struct afs_operation_ops afs_fetch_data_operation = {
250 .issue_afs_rpc = afs_fs_fetch_data,
251 .issue_yfs_rpc = yfs_fs_fetch_data,
252 .success = afs_fetch_data_success,
David Howells728279a2020-06-16 00:34:09 +0100253 .aborted = afs_check_for_remote_deletion,
David Howellsdc419182020-09-18 09:11:15 +0100254 .failed = afs_fetch_data_notify,
David Howellse49c7b22020-04-10 20:51:51 +0100255 .put = afs_fetch_data_put,
256};
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258/*
David Howellsd2ddc772017-11-02 15:27:50 +0000259 * Fetch file data from the volume.
260 */
David Howellsc69bf472020-02-06 14:22:27 +0000261int afs_fetch_data(struct afs_vnode *vnode, struct afs_read *req)
David Howellsd2ddc772017-11-02 15:27:50 +0000262{
David Howellse49c7b22020-04-10 20:51:51 +0100263 struct afs_operation *op;
David Howellsd2ddc772017-11-02 15:27:50 +0000264
David Howells3b6492d2018-10-20 00:57:57 +0100265 _enter("%s{%llx:%llu.%u},%x,,,",
David Howellsd2ddc772017-11-02 15:27:50 +0000266 vnode->volume->name,
267 vnode->fid.vid,
268 vnode->fid.vnode,
269 vnode->fid.unique,
David Howellsc69bf472020-02-06 14:22:27 +0000270 key_serial(req->key));
David Howellsd2ddc772017-11-02 15:27:50 +0000271
David Howellsc69bf472020-02-06 14:22:27 +0000272 op = afs_alloc_operation(req->key, vnode->volume);
David Howells5cbf0392020-02-06 14:22:29 +0000273 if (IS_ERR(op)) {
274 if (req->subreq)
275 netfs_subreq_terminated(req->subreq, PTR_ERR(op), false);
David Howellse49c7b22020-04-10 20:51:51 +0100276 return PTR_ERR(op);
David Howells5cbf0392020-02-06 14:22:29 +0000277 }
David Howellsa58823a2019-05-09 15:16:10 +0100278
David Howellse49c7b22020-04-10 20:51:51 +0100279 afs_op_set_vnode(op, 0, vnode);
David Howellsa58823a2019-05-09 15:16:10 +0100280
David Howellse49c7b22020-04-10 20:51:51 +0100281 op->fetch.req = afs_get_read(req);
282 op->ops = &afs_fetch_data_operation;
283 return afs_do_sync_operation(op);
David Howellsd2ddc772017-11-02 15:27:50 +0000284}
285
David Howells5cbf0392020-02-06 14:22:29 +0000286static void afs_req_issue_op(struct netfs_read_subrequest *subreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
David Howells5cbf0392020-02-06 14:22:29 +0000288 struct afs_vnode *vnode = AFS_FS_I(subreq->rreq->inode);
289 struct afs_read *fsreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
David Howells5cbf0392020-02-06 14:22:29 +0000291 fsreq = afs_alloc_read(GFP_NOFS);
292 if (!fsreq)
293 return netfs_subreq_terminated(subreq, -ENOMEM, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
David Howells5cbf0392020-02-06 14:22:29 +0000295 fsreq->subreq = subreq;
296 fsreq->pos = subreq->start + subreq->transferred;
297 fsreq->len = subreq->len - subreq->transferred;
298 fsreq->key = subreq->rreq->netfs_priv;
299 fsreq->vnode = vnode;
300 fsreq->iter = &fsreq->def_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
David Howells5cbf0392020-02-06 14:22:29 +0000302 iov_iter_xarray(&fsreq->def_iter, READ,
303 &fsreq->vnode->vfs_inode.i_mapping->i_pages,
304 fsreq->pos, fsreq->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
David Howells5cbf0392020-02-06 14:22:29 +0000306 afs_fetch_data(fsreq->vnode, fsreq);
David Howellsec268152007-04-26 15:49:28 -0700307}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
David Howells5cbf0392020-02-06 14:22:29 +0000309static int afs_symlink_readpage(struct page *page)
Al Virof6d335c2010-05-21 15:27:09 +0100310{
David Howells5cbf0392020-02-06 14:22:29 +0000311 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
312 struct afs_read *fsreq;
Al Virof6d335c2010-05-21 15:27:09 +0100313 int ret;
314
David Howells5cbf0392020-02-06 14:22:29 +0000315 fsreq = afs_alloc_read(GFP_NOFS);
316 if (!fsreq)
David Howells91b467e2017-01-05 10:38:35 +0000317 return -ENOMEM;
318
David Howells5cbf0392020-02-06 14:22:29 +0000319 fsreq->pos = page->index * PAGE_SIZE;
320 fsreq->len = PAGE_SIZE;
321 fsreq->vnode = vnode;
322 fsreq->iter = &fsreq->def_iter;
323 iov_iter_xarray(&fsreq->def_iter, READ, &page->mapping->i_pages,
324 fsreq->pos, fsreq->len);
David Howells91b467e2017-01-05 10:38:35 +0000325
David Howells5cbf0392020-02-06 14:22:29 +0000326 ret = afs_fetch_data(fsreq->vnode, fsreq);
327 page_endio(page, false, ret);
David Howells91b467e2017-01-05 10:38:35 +0000328 return ret;
329}
330
David Howells5cbf0392020-02-06 14:22:29 +0000331static void afs_init_rreq(struct netfs_read_request *rreq, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
David Howells5cbf0392020-02-06 14:22:29 +0000333 rreq->netfs_priv = key_get(afs_file_key(file));
334}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
David Howells3003bbd2020-02-06 14:22:29 +0000336static bool afs_is_cache_enabled(struct inode *inode)
337{
338 struct fscache_cookie *cookie = afs_vnode_cache(AFS_FS_I(inode));
339
340 return fscache_cookie_enabled(cookie) && !hlist_empty(&cookie->backing_objects);
341}
342
David Howells5cbf0392020-02-06 14:22:29 +0000343static int afs_begin_cache_operation(struct netfs_read_request *rreq)
344{
345 struct afs_vnode *vnode = AFS_FS_I(rreq->inode);
Al Virof6d335c2010-05-21 15:27:09 +0100346
David Howells5cbf0392020-02-06 14:22:29 +0000347 return fscache_begin_read_operation(rreq, afs_vnode_cache(vnode));
348}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
David Howells3003bbd2020-02-06 14:22:29 +0000350static int afs_check_write_begin(struct file *file, loff_t pos, unsigned len,
351 struct page *page, void **_fsdata)
352{
353 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
354
355 return test_bit(AFS_VNODE_DELETED, &vnode->flags) ? -ESTALE : 0;
356}
357
David Howells5cbf0392020-02-06 14:22:29 +0000358static void afs_priv_cleanup(struct address_space *mapping, void *netfs_priv)
359{
360 key_put(netfs_priv);
361}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
David Howells3003bbd2020-02-06 14:22:29 +0000363const struct netfs_read_request_ops afs_req_ops = {
David Howells5cbf0392020-02-06 14:22:29 +0000364 .init_rreq = afs_init_rreq,
David Howells3003bbd2020-02-06 14:22:29 +0000365 .is_cache_enabled = afs_is_cache_enabled,
David Howells5cbf0392020-02-06 14:22:29 +0000366 .begin_cache_operation = afs_begin_cache_operation,
David Howells3003bbd2020-02-06 14:22:29 +0000367 .check_write_begin = afs_check_write_begin,
David Howells5cbf0392020-02-06 14:22:29 +0000368 .issue_op = afs_req_issue_op,
369 .cleanup = afs_priv_cleanup,
370};
David Howells9b3f26c2009-04-03 16:42:41 +0100371
David Howells5cbf0392020-02-06 14:22:29 +0000372static int afs_readpage(struct file *file, struct page *page)
373{
374 if (!file)
375 return afs_symlink_readpage(page);
376
377 return netfs_readpage(file, page, &afs_req_ops, NULL);
378}
379
380static void afs_readahead(struct readahead_control *ractl)
381{
382 netfs_readahead(ractl, &afs_req_ops, NULL);
David Howellsec268152007-04-26 15:49:28 -0700383}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385/*
David Howellsf86726a2020-10-22 14:08:23 +0100386 * Adjust the dirty region of the page on truncation or full invalidation,
387 * getting rid of the markers altogether if the region is entirely invalidated.
388 */
389static void afs_invalidate_dirty(struct page *page, unsigned int offset,
390 unsigned int length)
391{
392 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
393 unsigned long priv;
394 unsigned int f, t, end = offset + length;
395
396 priv = page_private(page);
397
398 /* we clean up only if the entire page is being invalidated */
399 if (offset == 0 && length == thp_size(page))
400 goto full_invalidate;
401
402 /* If the page was dirtied by page_mkwrite(), the PTE stays writable
403 * and we don't get another notification to tell us to expand it
404 * again.
405 */
406 if (afs_is_page_dirty_mmapped(priv))
407 return;
408
409 /* We may need to shorten the dirty region */
David Howells67d78a62020-10-28 14:23:46 +0000410 f = afs_page_dirty_from(page, priv);
411 t = afs_page_dirty_to(page, priv);
David Howellsf86726a2020-10-22 14:08:23 +0100412
413 if (t <= offset || f >= end)
414 return; /* Doesn't overlap */
415
416 if (f < offset && t > end)
417 return; /* Splits the dirty region - just absorb it */
418
419 if (f >= offset && t <= end)
420 goto undirty;
421
422 if (f < offset)
423 t = offset;
424 else
425 f = end;
426 if (f == t)
427 goto undirty;
428
David Howells67d78a62020-10-28 14:23:46 +0000429 priv = afs_page_dirty(page, f, t);
David Howellsf86726a2020-10-22 14:08:23 +0100430 set_page_private(page, priv);
David Howells67d78a62020-10-28 14:23:46 +0000431 trace_afs_page_dirty(vnode, tracepoint_string("trunc"), page);
David Howellsf86726a2020-10-22 14:08:23 +0100432 return;
433
434undirty:
David Howells67d78a62020-10-28 14:23:46 +0000435 trace_afs_page_dirty(vnode, tracepoint_string("undirty"), page);
David Howellsf86726a2020-10-22 14:08:23 +0100436 clear_page_dirty_for_io(page);
437full_invalidate:
David Howells67d78a62020-10-28 14:23:46 +0000438 trace_afs_page_dirty(vnode, tracepoint_string("inval"), page);
David Howellse87b03f2020-10-20 09:33:45 +0100439 detach_page_private(page);
David Howellsf86726a2020-10-22 14:08:23 +0100440}
441
442/*
David Howells9b3f26c2009-04-03 16:42:41 +0100443 * invalidate part or all of a page
444 * - release a page and clean up its private data if offset is 0 (indicating
445 * the entire page)
446 */
Lukas Czernerd47992f2013-05-21 23:17:23 -0400447static void afs_invalidatepage(struct page *page, unsigned int offset,
448 unsigned int length)
David Howells9b3f26c2009-04-03 16:42:41 +0100449{
Lukas Czernerd47992f2013-05-21 23:17:23 -0400450 _enter("{%lu},%u,%u", page->index, offset, length);
David Howells9b3f26c2009-04-03 16:42:41 +0100451
452 BUG_ON(!PageLocked(page));
453
David Howellsf86726a2020-10-22 14:08:23 +0100454 if (PagePrivate(page))
455 afs_invalidate_dirty(page, offset, length);
David Howells9b3f26c2009-04-03 16:42:41 +0100456
David Howells630f5dd2020-02-06 14:22:28 +0000457 wait_on_page_fscache(page);
David Howells9b3f26c2009-04-03 16:42:41 +0100458 _leave("");
459}
460
461/*
462 * release a page and clean up its private state if it's not busy
463 * - return true if the page can now be released, false if not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 */
David Howells416351f2007-05-09 02:33:45 -0700465static int afs_releasepage(struct page *page, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
David Howells416351f2007-05-09 02:33:45 -0700467 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
David Howells3b6492d2018-10-20 00:57:57 +0100469 _enter("{{%llx:%llu}[%lu],%lx},%x",
David Howells416351f2007-05-09 02:33:45 -0700470 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
471 gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
David Howells9b3f26c2009-04-03 16:42:41 +0100473 /* deny if page is being written to the cache and the caller hasn't
474 * elected to wait */
David Howells630f5dd2020-02-06 14:22:28 +0000475#ifdef CONFIG_AFS_FSCACHE
476 if (PageFsCache(page)) {
477 if (!(gfp_flags & __GFP_DIRECT_RECLAIM) || !(gfp_flags & __GFP_FS))
478 return false;
479 wait_on_page_fscache(page);
480 }
481#endif
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (PagePrivate(page)) {
David Howells67d78a62020-10-28 14:23:46 +0000484 trace_afs_page_dirty(vnode, tracepoint_string("rel"), page);
David Howellse87b03f2020-10-20 09:33:45 +0100485 detach_page_private(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487
David Howells9b3f26c2009-04-03 16:42:41 +0100488 /* indicate that the page can be released */
489 _leave(" = T");
490 return 1;
David Howellsec268152007-04-26 15:49:28 -0700491}
David Howells1cf7a152017-11-02 15:27:52 +0000492
493/*
494 * Handle setting up a memory mapping on an AFS file.
495 */
496static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
497{
498 int ret;
499
500 ret = generic_file_mmap(file, vma);
501 if (ret == 0)
502 vma->vm_ops = &afs_vm_ops;
503 return ret;
504}