blob: 720818a7c166f99e2e4a1849e90fef9b1cd67fdf [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 Howellsd7bdba12021-12-22 17:21:04 +000017#include <linux/swap.h>
David Howells5cbf0392020-02-06 14:22:29 +000018#include <linux/netfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "internal.h"
20
David Howells1cf7a152017-11-02 15:27:52 +000021static int afs_file_mmap(struct file *file, struct vm_area_struct *vma);
David Howells416351f2007-05-09 02:33:45 -070022static int afs_readpage(struct file *file, struct page *page);
David Howells75bd2282021-06-29 22:23:13 +010023static int afs_symlink_readpage(struct file *file, struct page *page);
Lukas Czernerd47992f2013-05-21 23:17:23 -040024static void afs_invalidatepage(struct page *page, unsigned int offset,
25 unsigned int length);
David Howells416351f2007-05-09 02:33:45 -070026static int afs_releasepage(struct page *page, gfp_t gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
David Howells5cbf0392020-02-06 14:22:29 +000028static void afs_readahead(struct readahead_control *ractl);
David Howells3978d812021-09-01 19:22:50 +010029static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter);
David Howells6e0e99d2021-09-02 16:43:10 +010030static void afs_vm_open(struct vm_area_struct *area);
31static void afs_vm_close(struct vm_area_struct *area);
32static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff);
David Howells9b3f26c2009-04-03 16:42:41 +010033
David Howells00d3b7a2007-04-26 15:57:07 -070034const struct file_operations afs_file_operations = {
35 .open = afs_open,
36 .release = afs_release,
37 .llseek = generic_file_llseek,
David Howells3978d812021-09-01 19:22:50 +010038 .read_iter = afs_file_read_iter,
Al Viro50b55512014-04-03 14:13:46 -040039 .write_iter = afs_file_write,
David Howells1cf7a152017-11-02 15:27:52 +000040 .mmap = afs_file_mmap,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +020041 .splice_read = generic_file_splice_read,
David Howells06a17bb2020-10-27 09:39:04 +000042 .splice_write = iter_file_splice_write,
David Howells31143d52007-05-09 02:33:46 -070043 .fsync = afs_fsync,
David Howellse8d6c552007-07-15 23:40:12 -070044 .lock = afs_lock,
45 .flock = afs_flock,
David Howells00d3b7a2007-04-26 15:57:07 -070046};
47
Arjan van de Ven754661f2007-02-12 00:55:38 -080048const struct inode_operations afs_file_inode_operations = {
David Howells416351f2007-05-09 02:33:45 -070049 .getattr = afs_getattr,
David Howells31143d52007-05-09 02:33:46 -070050 .setattr = afs_setattr,
David Howells00d3b7a2007-04-26 15:57:07 -070051 .permission = afs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -070052};
53
David Howells75bd2282021-06-29 22:23:13 +010054const struct address_space_operations afs_file_aops = {
David Howells416351f2007-05-09 02:33:45 -070055 .readpage = afs_readpage,
David Howells5cbf0392020-02-06 14:22:29 +000056 .readahead = afs_readahead,
David Howells31143d52007-05-09 02:33:46 -070057 .set_page_dirty = afs_set_page_dirty,
58 .launder_page = afs_launder_page,
David Howells416351f2007-05-09 02:33:45 -070059 .releasepage = afs_releasepage,
60 .invalidatepage = afs_invalidatepage,
Nick Piggin15b46502008-10-15 22:04:32 -070061 .write_begin = afs_write_begin,
62 .write_end = afs_write_end,
David Howells31143d52007-05-09 02:33:46 -070063 .writepage = afs_writepage,
64 .writepages = afs_writepages,
Linus Torvalds1da177e2005-04-16 15:20:36 -070065};
66
David Howells75bd2282021-06-29 22:23:13 +010067const struct address_space_operations afs_symlink_aops = {
68 .readpage = afs_symlink_readpage,
69 .releasepage = afs_releasepage,
70 .invalidatepage = afs_invalidatepage,
71};
72
David Howells1cf7a152017-11-02 15:27:52 +000073static const struct vm_operations_struct afs_vm_ops = {
David Howells6e0e99d2021-09-02 16:43:10 +010074 .open = afs_vm_open,
75 .close = afs_vm_close,
David Howells1cf7a152017-11-02 15:27:52 +000076 .fault = filemap_fault,
David Howells6e0e99d2021-09-02 16:43:10 +010077 .map_pages = afs_vm_map_pages,
David Howells1cf7a152017-11-02 15:27:52 +000078 .page_mkwrite = afs_page_mkwrite,
79};
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/*
David Howells4343d002017-11-02 15:27:52 +000082 * Discard a pin on a writeback key.
83 */
84void afs_put_wb_key(struct afs_wb_key *wbk)
85{
David Howellse49c7b22020-04-10 20:51:51 +010086 if (wbk && refcount_dec_and_test(&wbk->usage)) {
David Howells4343d002017-11-02 15:27:52 +000087 key_put(wbk->key);
88 kfree(wbk);
89 }
90}
91
92/*
93 * Cache key for writeback.
94 */
95int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af)
96{
97 struct afs_wb_key *wbk, *p;
98
99 wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL);
100 if (!wbk)
101 return -ENOMEM;
102 refcount_set(&wbk->usage, 2);
103 wbk->key = af->key;
104
105 spin_lock(&vnode->wb_lock);
106 list_for_each_entry(p, &vnode->wb_keys, vnode_link) {
107 if (p->key == wbk->key)
108 goto found;
109 }
110
111 key_get(wbk->key);
112 list_add_tail(&wbk->vnode_link, &vnode->wb_keys);
113 spin_unlock(&vnode->wb_lock);
114 af->wb = wbk;
115 return 0;
116
117found:
118 refcount_inc(&p->usage);
119 spin_unlock(&vnode->wb_lock);
120 af->wb = p;
121 kfree(wbk);
122 return 0;
123}
124
125/*
David Howells00d3b7a2007-04-26 15:57:07 -0700126 * open an AFS file or directory and attach a key to it
127 */
128int afs_open(struct inode *inode, struct file *file)
129{
130 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells215804a2017-11-02 15:27:52 +0000131 struct afs_file *af;
David Howells00d3b7a2007-04-26 15:57:07 -0700132 struct key *key;
David Howells260a9802007-04-26 15:59:35 -0700133 int ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700134
David Howells3b6492d2018-10-20 00:57:57 +0100135 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
David Howells00d3b7a2007-04-26 15:57:07 -0700136
137 key = afs_request_key(vnode->volume->cell);
138 if (IS_ERR(key)) {
David Howells215804a2017-11-02 15:27:52 +0000139 ret = PTR_ERR(key);
140 goto error;
141 }
142
143 af = kzalloc(sizeof(*af), GFP_KERNEL);
144 if (!af) {
145 ret = -ENOMEM;
146 goto error_key;
David Howells00d3b7a2007-04-26 15:57:07 -0700147 }
David Howells4343d002017-11-02 15:27:52 +0000148 af->key = key;
David Howells00d3b7a2007-04-26 15:57:07 -0700149
David Howells260a9802007-04-26 15:59:35 -0700150 ret = afs_validate(vnode, key);
David Howells215804a2017-11-02 15:27:52 +0000151 if (ret < 0)
152 goto error_af;
David Howells260a9802007-04-26 15:59:35 -0700153
David Howells4343d002017-11-02 15:27:52 +0000154 if (file->f_mode & FMODE_WRITE) {
155 ret = afs_cache_wb_key(vnode, af);
156 if (ret < 0)
157 goto error_af;
158 }
David Howells5a813272018-04-06 14:17:26 +0100159
160 if (file->f_flags & O_TRUNC)
161 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
David Howells523d27c2020-02-06 14:22:21 +0000162
163 fscache_use_cookie(afs_vnode_cache(vnode), file->f_mode & FMODE_WRITE);
164
David Howells215804a2017-11-02 15:27:52 +0000165 file->private_data = af;
David Howells00d3b7a2007-04-26 15:57:07 -0700166 _leave(" = 0");
167 return 0;
David Howells215804a2017-11-02 15:27:52 +0000168
169error_af:
170 kfree(af);
171error_key:
172 key_put(key);
173error:
174 _leave(" = %d", ret);
175 return ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700176}
177
178/*
179 * release an AFS file or directory and discard its key
180 */
181int afs_release(struct inode *inode, struct file *file)
182{
David Howells523d27c2020-02-06 14:22:21 +0000183 struct afs_vnode_cache_aux aux;
David Howells00d3b7a2007-04-26 15:57:07 -0700184 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells215804a2017-11-02 15:27:52 +0000185 struct afs_file *af = file->private_data;
David Howells523d27c2020-02-06 14:22:21 +0000186 loff_t i_size;
David Howellsa1b879e2019-05-15 12:09:17 +0100187 int ret = 0;
David Howells00d3b7a2007-04-26 15:57:07 -0700188
David Howells3b6492d2018-10-20 00:57:57 +0100189 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
David Howells00d3b7a2007-04-26 15:57:07 -0700190
David Howells5a813272018-04-06 14:17:26 +0100191 if ((file->f_mode & FMODE_WRITE))
David Howellsa1b879e2019-05-15 12:09:17 +0100192 ret = vfs_fsync(file, 0);
David Howells5a813272018-04-06 14:17:26 +0100193
David Howells215804a2017-11-02 15:27:52 +0000194 file->private_data = NULL;
David Howells4343d002017-11-02 15:27:52 +0000195 if (af->wb)
196 afs_put_wb_key(af->wb);
David Howells523d27c2020-02-06 14:22:21 +0000197
198 if ((file->f_mode & FMODE_WRITE)) {
199 i_size = i_size_read(&vnode->vfs_inode);
200 afs_set_cache_aux(vnode, &aux);
201 fscache_unuse_cookie(afs_vnode_cache(vnode), &aux, &i_size);
202 } else {
203 fscache_unuse_cookie(afs_vnode_cache(vnode), NULL, NULL);
204 }
205
David Howells215804a2017-11-02 15:27:52 +0000206 key_put(af->key);
207 kfree(af);
David Howells4343d002017-11-02 15:27:52 +0000208 afs_prune_wb_keys(vnode);
David Howellsa1b879e2019-05-15 12:09:17 +0100209 _leave(" = %d", ret);
210 return ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700211}
212
David Howells196ee9c2017-01-05 10:38:34 +0000213/*
David Howells5cbf0392020-02-06 14:22:29 +0000214 * Allocate a new read record.
David Howellsc4508462020-02-06 14:22:28 +0000215 */
David Howells5cbf0392020-02-06 14:22:29 +0000216struct afs_read *afs_alloc_read(gfp_t gfp)
David Howellsc4508462020-02-06 14:22:28 +0000217{
David Howells5cbf0392020-02-06 14:22:29 +0000218 struct afs_read *req;
David Howellsc4508462020-02-06 14:22:28 +0000219
David Howells5cbf0392020-02-06 14:22:29 +0000220 req = kzalloc(sizeof(struct afs_read), gfp);
221 if (req)
222 refcount_set(&req->usage, 1);
David Howellsc4508462020-02-06 14:22:28 +0000223
David Howells5cbf0392020-02-06 14:22:29 +0000224 return req;
David Howellsc4508462020-02-06 14:22:28 +0000225}
226
227/*
David Howells196ee9c2017-01-05 10:38:34 +0000228 * Dispose of a ref to a read record.
229 */
230void afs_put_read(struct afs_read *req)
231{
David Howellsf3ddee82018-04-06 14:17:25 +0100232 if (refcount_dec_and_test(&req->usage)) {
David Howellsc4508462020-02-06 14:22:28 +0000233 if (req->cleanup)
234 req->cleanup(req);
David Howellsc69bf472020-02-06 14:22:27 +0000235 key_put(req->key);
David Howells196ee9c2017-01-05 10:38:34 +0000236 kfree(req);
237 }
238}
239
David Howellsdc419182020-09-18 09:11:15 +0100240static void afs_fetch_data_notify(struct afs_operation *op)
241{
242 struct afs_read *req = op->fetch.req;
David Howells5cbf0392020-02-06 14:22:29 +0000243 struct netfs_read_subrequest *subreq = req->subreq;
David Howellsdc419182020-09-18 09:11:15 +0100244 int error = op->error;
245
246 if (error == -ECONNABORTED)
247 error = afs_abort_to_error(op->ac.abort_code);
248 req->error = error;
249
David Howells5cbf0392020-02-06 14:22:29 +0000250 if (subreq) {
251 __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
252 netfs_subreq_terminated(subreq, error ?: req->actual_len, false);
253 req->subreq = NULL;
254 } else if (req->done) {
David Howellsdc419182020-09-18 09:11:15 +0100255 req->done(req);
David Howells5cbf0392020-02-06 14:22:29 +0000256 }
David Howellsdc419182020-09-18 09:11:15 +0100257}
258
David Howellse49c7b22020-04-10 20:51:51 +0100259static void afs_fetch_data_success(struct afs_operation *op)
260{
261 struct afs_vnode *vnode = op->file[0].vnode;
262
263 _enter("op=%08x", op->debug_id);
David Howellse49c7b22020-04-10 20:51:51 +0100264 afs_vnode_commit_status(op, &op->file[0]);
265 afs_stat_v(vnode, n_fetches);
266 atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes);
David Howellsdc419182020-09-18 09:11:15 +0100267 afs_fetch_data_notify(op);
David Howellse49c7b22020-04-10 20:51:51 +0100268}
269
270static void afs_fetch_data_put(struct afs_operation *op)
271{
David Howellsc4508462020-02-06 14:22:28 +0000272 op->fetch.req->error = op->error;
David Howellse49c7b22020-04-10 20:51:51 +0100273 afs_put_read(op->fetch.req);
274}
275
276static const struct afs_operation_ops afs_fetch_data_operation = {
277 .issue_afs_rpc = afs_fs_fetch_data,
278 .issue_yfs_rpc = yfs_fs_fetch_data,
279 .success = afs_fetch_data_success,
David Howells728279a2020-06-16 00:34:09 +0100280 .aborted = afs_check_for_remote_deletion,
David Howellsdc419182020-09-18 09:11:15 +0100281 .failed = afs_fetch_data_notify,
David Howellse49c7b22020-04-10 20:51:51 +0100282 .put = afs_fetch_data_put,
283};
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285/*
David Howellsd2ddc772017-11-02 15:27:50 +0000286 * Fetch file data from the volume.
287 */
David Howellsc69bf472020-02-06 14:22:27 +0000288int afs_fetch_data(struct afs_vnode *vnode, struct afs_read *req)
David Howellsd2ddc772017-11-02 15:27:50 +0000289{
David Howellse49c7b22020-04-10 20:51:51 +0100290 struct afs_operation *op;
David Howellsd2ddc772017-11-02 15:27:50 +0000291
David Howells3b6492d2018-10-20 00:57:57 +0100292 _enter("%s{%llx:%llu.%u},%x,,,",
David Howellsd2ddc772017-11-02 15:27:50 +0000293 vnode->volume->name,
294 vnode->fid.vid,
295 vnode->fid.vnode,
296 vnode->fid.unique,
David Howellsc69bf472020-02-06 14:22:27 +0000297 key_serial(req->key));
David Howellsd2ddc772017-11-02 15:27:50 +0000298
David Howellsc69bf472020-02-06 14:22:27 +0000299 op = afs_alloc_operation(req->key, vnode->volume);
David Howells5cbf0392020-02-06 14:22:29 +0000300 if (IS_ERR(op)) {
301 if (req->subreq)
302 netfs_subreq_terminated(req->subreq, PTR_ERR(op), false);
David Howellse49c7b22020-04-10 20:51:51 +0100303 return PTR_ERR(op);
David Howells5cbf0392020-02-06 14:22:29 +0000304 }
David Howellsa58823a2019-05-09 15:16:10 +0100305
David Howellse49c7b22020-04-10 20:51:51 +0100306 afs_op_set_vnode(op, 0, vnode);
David Howellsa58823a2019-05-09 15:16:10 +0100307
David Howellse49c7b22020-04-10 20:51:51 +0100308 op->fetch.req = afs_get_read(req);
309 op->ops = &afs_fetch_data_operation;
310 return afs_do_sync_operation(op);
David Howellsd2ddc772017-11-02 15:27:50 +0000311}
312
David Howells5cbf0392020-02-06 14:22:29 +0000313static void afs_req_issue_op(struct netfs_read_subrequest *subreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
David Howells5cbf0392020-02-06 14:22:29 +0000315 struct afs_vnode *vnode = AFS_FS_I(subreq->rreq->inode);
316 struct afs_read *fsreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
David Howells5cbf0392020-02-06 14:22:29 +0000318 fsreq = afs_alloc_read(GFP_NOFS);
319 if (!fsreq)
320 return netfs_subreq_terminated(subreq, -ENOMEM, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
David Howells5cbf0392020-02-06 14:22:29 +0000322 fsreq->subreq = subreq;
323 fsreq->pos = subreq->start + subreq->transferred;
324 fsreq->len = subreq->len - subreq->transferred;
David Howells345e1ae2021-08-27 23:22:02 +0100325 fsreq->key = key_get(subreq->rreq->netfs_priv);
David Howells5cbf0392020-02-06 14:22:29 +0000326 fsreq->vnode = vnode;
327 fsreq->iter = &fsreq->def_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
David Howells5cbf0392020-02-06 14:22:29 +0000329 iov_iter_xarray(&fsreq->def_iter, READ,
330 &fsreq->vnode->vfs_inode.i_mapping->i_pages,
331 fsreq->pos, fsreq->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
David Howells5cbf0392020-02-06 14:22:29 +0000333 afs_fetch_data(fsreq->vnode, fsreq);
David Howells345e1ae2021-08-27 23:22:02 +0100334 afs_put_read(fsreq);
David Howellsec268152007-04-26 15:49:28 -0700335}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
David Howells75bd2282021-06-29 22:23:13 +0100337static int afs_symlink_readpage(struct file *file, struct page *page)
Al Virof6d335c2010-05-21 15:27:09 +0100338{
David Howells5cbf0392020-02-06 14:22:29 +0000339 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
340 struct afs_read *fsreq;
David Howells78525c72021-08-11 09:49:13 +0100341 struct folio *folio = page_folio(page);
Al Virof6d335c2010-05-21 15:27:09 +0100342 int ret;
343
David Howells5cbf0392020-02-06 14:22:29 +0000344 fsreq = afs_alloc_read(GFP_NOFS);
345 if (!fsreq)
David Howells91b467e2017-01-05 10:38:35 +0000346 return -ENOMEM;
347
David Howells78525c72021-08-11 09:49:13 +0100348 fsreq->pos = folio_pos(folio);
349 fsreq->len = folio_size(folio);
David Howells5cbf0392020-02-06 14:22:29 +0000350 fsreq->vnode = vnode;
351 fsreq->iter = &fsreq->def_iter;
352 iov_iter_xarray(&fsreq->def_iter, READ, &page->mapping->i_pages,
353 fsreq->pos, fsreq->len);
David Howells91b467e2017-01-05 10:38:35 +0000354
David Howells5cbf0392020-02-06 14:22:29 +0000355 ret = afs_fetch_data(fsreq->vnode, fsreq);
David Howells78525c72021-08-11 09:49:13 +0100356 if (ret == 0)
357 SetPageUptodate(page);
358 unlock_page(page);
David Howells91b467e2017-01-05 10:38:35 +0000359 return ret;
360}
361
David Howells5cbf0392020-02-06 14:22:29 +0000362static void afs_init_rreq(struct netfs_read_request *rreq, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
David Howells5cbf0392020-02-06 14:22:29 +0000364 rreq->netfs_priv = key_get(afs_file_key(file));
365}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
David Howells3003bbd2020-02-06 14:22:29 +0000367static bool afs_is_cache_enabled(struct inode *inode)
368{
369 struct fscache_cookie *cookie = afs_vnode_cache(AFS_FS_I(inode));
370
David Howells523d27c2020-02-06 14:22:21 +0000371 return fscache_cookie_enabled(cookie) && cookie->cache_priv;
David Howells3003bbd2020-02-06 14:22:29 +0000372}
373
David Howells5cbf0392020-02-06 14:22:29 +0000374static int afs_begin_cache_operation(struct netfs_read_request *rreq)
375{
David Howells2cee6fb2021-10-25 21:53:44 +0100376#ifdef CONFIG_AFS_FSCACHE
David Howells5cbf0392020-02-06 14:22:29 +0000377 struct afs_vnode *vnode = AFS_FS_I(rreq->inode);
Al Virof6d335c2010-05-21 15:27:09 +0100378
David Howells523d27c2020-02-06 14:22:21 +0000379 return fscache_begin_read_operation(&rreq->cache_resources,
380 afs_vnode_cache(vnode));
David Howells2cee6fb2021-10-25 21:53:44 +0100381#else
382 return -ENOBUFS;
383#endif
David Howells5cbf0392020-02-06 14:22:29 +0000384}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
David Howells3003bbd2020-02-06 14:22:29 +0000386static int afs_check_write_begin(struct file *file, loff_t pos, unsigned len,
David Howells78525c72021-08-11 09:49:13 +0100387 struct folio *folio, void **_fsdata)
David Howells3003bbd2020-02-06 14:22:29 +0000388{
389 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
390
391 return test_bit(AFS_VNODE_DELETED, &vnode->flags) ? -ESTALE : 0;
392}
393
David Howells5cbf0392020-02-06 14:22:29 +0000394static void afs_priv_cleanup(struct address_space *mapping, void *netfs_priv)
395{
396 key_put(netfs_priv);
397}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
David Howells3003bbd2020-02-06 14:22:29 +0000399const struct netfs_read_request_ops afs_req_ops = {
David Howells5cbf0392020-02-06 14:22:29 +0000400 .init_rreq = afs_init_rreq,
David Howells3003bbd2020-02-06 14:22:29 +0000401 .is_cache_enabled = afs_is_cache_enabled,
David Howells5cbf0392020-02-06 14:22:29 +0000402 .begin_cache_operation = afs_begin_cache_operation,
David Howells3003bbd2020-02-06 14:22:29 +0000403 .check_write_begin = afs_check_write_begin,
David Howells5cbf0392020-02-06 14:22:29 +0000404 .issue_op = afs_req_issue_op,
405 .cleanup = afs_priv_cleanup,
406};
David Howells9b3f26c2009-04-03 16:42:41 +0100407
David Howells5cbf0392020-02-06 14:22:29 +0000408static int afs_readpage(struct file *file, struct page *page)
409{
David Howells78525c72021-08-11 09:49:13 +0100410 struct folio *folio = page_folio(page);
411
412 return netfs_readpage(file, folio, &afs_req_ops, NULL);
David Howells5cbf0392020-02-06 14:22:29 +0000413}
414
415static void afs_readahead(struct readahead_control *ractl)
416{
417 netfs_readahead(ractl, &afs_req_ops, NULL);
David Howellsec268152007-04-26 15:49:28 -0700418}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
David Howellsc7f75ef2020-02-06 14:22:30 +0000420int afs_write_inode(struct inode *inode, struct writeback_control *wbc)
421{
422 fscache_unpin_writeback(wbc, afs_vnode_cache(AFS_FS_I(inode)));
423 return 0;
424}
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426/*
David Howellsf86726a2020-10-22 14:08:23 +0100427 * Adjust the dirty region of the page on truncation or full invalidation,
428 * getting rid of the markers altogether if the region is entirely invalidated.
429 */
David Howells78525c72021-08-11 09:49:13 +0100430static void afs_invalidate_dirty(struct folio *folio, unsigned int offset,
David Howellsf86726a2020-10-22 14:08:23 +0100431 unsigned int length)
432{
David Howells78525c72021-08-11 09:49:13 +0100433 struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio));
David Howellsf86726a2020-10-22 14:08:23 +0100434 unsigned long priv;
435 unsigned int f, t, end = offset + length;
436
David Howells78525c72021-08-11 09:49:13 +0100437 priv = (unsigned long)folio_get_private(folio);
David Howellsf86726a2020-10-22 14:08:23 +0100438
439 /* we clean up only if the entire page is being invalidated */
David Howells78525c72021-08-11 09:49:13 +0100440 if (offset == 0 && length == folio_size(folio))
David Howellsf86726a2020-10-22 14:08:23 +0100441 goto full_invalidate;
442
443 /* If the page was dirtied by page_mkwrite(), the PTE stays writable
444 * and we don't get another notification to tell us to expand it
445 * again.
446 */
David Howells78525c72021-08-11 09:49:13 +0100447 if (afs_is_folio_dirty_mmapped(priv))
David Howellsf86726a2020-10-22 14:08:23 +0100448 return;
449
450 /* We may need to shorten the dirty region */
David Howells78525c72021-08-11 09:49:13 +0100451 f = afs_folio_dirty_from(folio, priv);
452 t = afs_folio_dirty_to(folio, priv);
David Howellsf86726a2020-10-22 14:08:23 +0100453
454 if (t <= offset || f >= end)
455 return; /* Doesn't overlap */
456
457 if (f < offset && t > end)
458 return; /* Splits the dirty region - just absorb it */
459
460 if (f >= offset && t <= end)
461 goto undirty;
462
463 if (f < offset)
464 t = offset;
465 else
466 f = end;
467 if (f == t)
468 goto undirty;
469
David Howells78525c72021-08-11 09:49:13 +0100470 priv = afs_folio_dirty(folio, f, t);
471 folio_change_private(folio, (void *)priv);
472 trace_afs_folio_dirty(vnode, tracepoint_string("trunc"), folio);
David Howellsf86726a2020-10-22 14:08:23 +0100473 return;
474
475undirty:
David Howells78525c72021-08-11 09:49:13 +0100476 trace_afs_folio_dirty(vnode, tracepoint_string("undirty"), folio);
477 folio_clear_dirty_for_io(folio);
David Howellsf86726a2020-10-22 14:08:23 +0100478full_invalidate:
David Howells78525c72021-08-11 09:49:13 +0100479 trace_afs_folio_dirty(vnode, tracepoint_string("inval"), folio);
480 folio_detach_private(folio);
David Howellsf86726a2020-10-22 14:08:23 +0100481}
482
483/*
David Howells9b3f26c2009-04-03 16:42:41 +0100484 * invalidate part or all of a page
485 * - release a page and clean up its private data if offset is 0 (indicating
486 * the entire page)
487 */
Lukas Czernerd47992f2013-05-21 23:17:23 -0400488static void afs_invalidatepage(struct page *page, unsigned int offset,
489 unsigned int length)
David Howells9b3f26c2009-04-03 16:42:41 +0100490{
David Howells78525c72021-08-11 09:49:13 +0100491 struct folio *folio = page_folio(page);
492
493 _enter("{%lu},%u,%u", folio_index(folio), offset, length);
David Howells9b3f26c2009-04-03 16:42:41 +0100494
495 BUG_ON(!PageLocked(page));
496
David Howellsf86726a2020-10-22 14:08:23 +0100497 if (PagePrivate(page))
David Howells78525c72021-08-11 09:49:13 +0100498 afs_invalidate_dirty(folio, offset, length);
David Howells9b3f26c2009-04-03 16:42:41 +0100499
David Howells78525c72021-08-11 09:49:13 +0100500 folio_wait_fscache(folio);
David Howells9b3f26c2009-04-03 16:42:41 +0100501 _leave("");
502}
503
504/*
505 * release a page and clean up its private state if it's not busy
506 * - return true if the page can now be released, false if not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 */
David Howells523d27c2020-02-06 14:22:21 +0000508static int afs_releasepage(struct page *page, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
David Howells78525c72021-08-11 09:49:13 +0100510 struct folio *folio = page_folio(page);
511 struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
David Howells3b6492d2018-10-20 00:57:57 +0100513 _enter("{{%llx:%llu}[%lu],%lx},%x",
David Howells78525c72021-08-11 09:49:13 +0100514 vnode->fid.vid, vnode->fid.vnode, folio_index(folio), folio->flags,
David Howells523d27c2020-02-06 14:22:21 +0000515 gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
David Howells9b3f26c2009-04-03 16:42:41 +0100517 /* deny if page is being written to the cache and the caller hasn't
518 * elected to wait */
David Howells630f5dd2020-02-06 14:22:28 +0000519#ifdef CONFIG_AFS_FSCACHE
David Howells78525c72021-08-11 09:49:13 +0100520 if (folio_test_fscache(folio)) {
David Howellsd7bdba12021-12-22 17:21:04 +0000521 if (current_is_kswapd() || !(gfp & __GFP_FS))
David Howells630f5dd2020-02-06 14:22:28 +0000522 return false;
David Howells78525c72021-08-11 09:49:13 +0100523 folio_wait_fscache(folio);
David Howells630f5dd2020-02-06 14:22:28 +0000524 }
David Howells523d27c2020-02-06 14:22:21 +0000525 fscache_note_page_release(afs_vnode_cache(vnode));
David Howells630f5dd2020-02-06 14:22:28 +0000526#endif
527
David Howells78525c72021-08-11 09:49:13 +0100528 if (folio_test_private(folio)) {
529 trace_afs_folio_dirty(vnode, tracepoint_string("rel"), folio);
530 folio_detach_private(folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532
David Howells78525c72021-08-11 09:49:13 +0100533 /* Indicate that the folio can be released */
David Howells9b3f26c2009-04-03 16:42:41 +0100534 _leave(" = T");
David Howells78525c72021-08-11 09:49:13 +0100535 return true;
David Howellsec268152007-04-26 15:49:28 -0700536}
David Howells1cf7a152017-11-02 15:27:52 +0000537
David Howells6e0e99d2021-09-02 16:43:10 +0100538static void afs_add_open_mmap(struct afs_vnode *vnode)
539{
540 if (atomic_inc_return(&vnode->cb_nr_mmap) == 1) {
541 down_write(&vnode->volume->cell->fs_open_mmaps_lock);
542
David Howells1744a222021-12-14 09:22:12 +0000543 if (list_empty(&vnode->cb_mmap_link))
544 list_add_tail(&vnode->cb_mmap_link,
545 &vnode->volume->cell->fs_open_mmaps);
David Howells6e0e99d2021-09-02 16:43:10 +0100546
547 up_write(&vnode->volume->cell->fs_open_mmaps_lock);
548 }
549}
550
551static void afs_drop_open_mmap(struct afs_vnode *vnode)
552{
553 if (!atomic_dec_and_test(&vnode->cb_nr_mmap))
554 return;
555
556 down_write(&vnode->volume->cell->fs_open_mmaps_lock);
557
558 if (atomic_read(&vnode->cb_nr_mmap) == 0)
559 list_del_init(&vnode->cb_mmap_link);
560
561 up_write(&vnode->volume->cell->fs_open_mmaps_lock);
562 flush_work(&vnode->cb_work);
563}
564
David Howells1cf7a152017-11-02 15:27:52 +0000565/*
566 * Handle setting up a memory mapping on an AFS file.
567 */
568static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
569{
David Howells6e0e99d2021-09-02 16:43:10 +0100570 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
David Howells1cf7a152017-11-02 15:27:52 +0000571 int ret;
572
David Howells6e0e99d2021-09-02 16:43:10 +0100573 afs_add_open_mmap(vnode);
574
David Howells1cf7a152017-11-02 15:27:52 +0000575 ret = generic_file_mmap(file, vma);
576 if (ret == 0)
577 vma->vm_ops = &afs_vm_ops;
David Howells6e0e99d2021-09-02 16:43:10 +0100578 else
579 afs_drop_open_mmap(vnode);
David Howells1cf7a152017-11-02 15:27:52 +0000580 return ret;
581}
David Howells3978d812021-09-01 19:22:50 +0100582
David Howells6e0e99d2021-09-02 16:43:10 +0100583static void afs_vm_open(struct vm_area_struct *vma)
584{
585 afs_add_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
586}
587
588static void afs_vm_close(struct vm_area_struct *vma)
589{
590 afs_drop_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
591}
592
593static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff)
594{
595 struct afs_vnode *vnode = AFS_FS_I(file_inode(vmf->vma->vm_file));
596 struct afs_file *af = vmf->vma->vm_file->private_data;
597
598 switch (afs_validate(vnode, af->key)) {
599 case 0:
600 return filemap_map_pages(vmf, start_pgoff, end_pgoff);
601 case -ENOMEM:
602 return VM_FAULT_OOM;
603 case -EINTR:
604 case -ERESTARTSYS:
605 return VM_FAULT_RETRY;
606 case -ESTALE:
607 default:
608 return VM_FAULT_SIGBUS;
609 }
610}
611
David Howells3978d812021-09-01 19:22:50 +0100612static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
613{
614 struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
615 struct afs_file *af = iocb->ki_filp->private_data;
616 int ret;
617
618 ret = afs_validate(vnode, af->key);
619 if (ret < 0)
620 return ret;
621
622 return generic_file_read_iter(iocb, iter);
623}