blob: afe4b803f84b46d319e4885fdbc6ea8a1919cf40 [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);
David Howells75bd2282021-06-29 22:23:13 +010022static int afs_symlink_readpage(struct file *file, struct page *page);
Lukas Czernerd47992f2013-05-21 23:17:23 -040023static void afs_invalidatepage(struct page *page, unsigned int offset,
24 unsigned int length);
David Howells416351f2007-05-09 02:33:45 -070025static int afs_releasepage(struct page *page, gfp_t gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
David Howells5cbf0392020-02-06 14:22:29 +000027static void afs_readahead(struct readahead_control *ractl);
David Howells3978d812021-09-01 19:22:50 +010028static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter);
David Howells6e0e99d2021-09-02 16:43:10 +010029static void afs_vm_open(struct vm_area_struct *area);
30static void afs_vm_close(struct vm_area_struct *area);
31static 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 +010032
David Howells00d3b7a2007-04-26 15:57:07 -070033const struct file_operations afs_file_operations = {
34 .open = afs_open,
35 .release = afs_release,
36 .llseek = generic_file_llseek,
David Howells3978d812021-09-01 19:22:50 +010037 .read_iter = afs_file_read_iter,
Al Viro50b55512014-04-03 14:13:46 -040038 .write_iter = afs_file_write,
David Howells1cf7a152017-11-02 15:27:52 +000039 .mmap = afs_file_mmap,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +020040 .splice_read = generic_file_splice_read,
David Howells06a17bb2020-10-27 09:39:04 +000041 .splice_write = iter_file_splice_write,
David Howells31143d52007-05-09 02:33:46 -070042 .fsync = afs_fsync,
David Howellse8d6c552007-07-15 23:40:12 -070043 .lock = afs_lock,
44 .flock = afs_flock,
David Howells00d3b7a2007-04-26 15:57:07 -070045};
46
Arjan van de Ven754661f2007-02-12 00:55:38 -080047const struct inode_operations afs_file_inode_operations = {
David Howells416351f2007-05-09 02:33:45 -070048 .getattr = afs_getattr,
David Howells31143d52007-05-09 02:33:46 -070049 .setattr = afs_setattr,
David Howells00d3b7a2007-04-26 15:57:07 -070050 .permission = afs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
David Howells75bd2282021-06-29 22:23:13 +010053const struct address_space_operations afs_file_aops = {
David Howells416351f2007-05-09 02:33:45 -070054 .readpage = afs_readpage,
David Howells5cbf0392020-02-06 14:22:29 +000055 .readahead = afs_readahead,
David Howells31143d52007-05-09 02:33:46 -070056 .set_page_dirty = afs_set_page_dirty,
57 .launder_page = afs_launder_page,
David Howells416351f2007-05-09 02:33:45 -070058 .releasepage = afs_releasepage,
59 .invalidatepage = afs_invalidatepage,
Nick Piggin15b46502008-10-15 22:04:32 -070060 .write_begin = afs_write_begin,
61 .write_end = afs_write_end,
David Howells31143d52007-05-09 02:33:46 -070062 .writepage = afs_writepage,
63 .writepages = afs_writepages,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
David Howells75bd2282021-06-29 22:23:13 +010066const struct address_space_operations afs_symlink_aops = {
67 .readpage = afs_symlink_readpage,
68 .releasepage = afs_releasepage,
69 .invalidatepage = afs_invalidatepage,
70};
71
David Howells1cf7a152017-11-02 15:27:52 +000072static const struct vm_operations_struct afs_vm_ops = {
David Howells6e0e99d2021-09-02 16:43:10 +010073 .open = afs_vm_open,
74 .close = afs_vm_close,
David Howells1cf7a152017-11-02 15:27:52 +000075 .fault = filemap_fault,
David Howells6e0e99d2021-09-02 16:43:10 +010076 .map_pages = afs_vm_map_pages,
David Howells1cf7a152017-11-02 15:27:52 +000077 .page_mkwrite = afs_page_mkwrite,
78};
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
David Howells4343d002017-11-02 15:27:52 +000081 * Discard a pin on a writeback key.
82 */
83void afs_put_wb_key(struct afs_wb_key *wbk)
84{
David Howellse49c7b22020-04-10 20:51:51 +010085 if (wbk && refcount_dec_and_test(&wbk->usage)) {
David Howells4343d002017-11-02 15:27:52 +000086 key_put(wbk->key);
87 kfree(wbk);
88 }
89}
90
91/*
92 * Cache key for writeback.
93 */
94int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af)
95{
96 struct afs_wb_key *wbk, *p;
97
98 wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL);
99 if (!wbk)
100 return -ENOMEM;
101 refcount_set(&wbk->usage, 2);
102 wbk->key = af->key;
103
104 spin_lock(&vnode->wb_lock);
105 list_for_each_entry(p, &vnode->wb_keys, vnode_link) {
106 if (p->key == wbk->key)
107 goto found;
108 }
109
110 key_get(wbk->key);
111 list_add_tail(&wbk->vnode_link, &vnode->wb_keys);
112 spin_unlock(&vnode->wb_lock);
113 af->wb = wbk;
114 return 0;
115
116found:
117 refcount_inc(&p->usage);
118 spin_unlock(&vnode->wb_lock);
119 af->wb = p;
120 kfree(wbk);
121 return 0;
122}
123
124/*
David Howells00d3b7a2007-04-26 15:57:07 -0700125 * open an AFS file or directory and attach a key to it
126 */
127int afs_open(struct inode *inode, struct file *file)
128{
129 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells215804a2017-11-02 15:27:52 +0000130 struct afs_file *af;
David Howells00d3b7a2007-04-26 15:57:07 -0700131 struct key *key;
David Howells260a9802007-04-26 15:59:35 -0700132 int ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700133
David Howells3b6492d2018-10-20 00:57:57 +0100134 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
David Howells00d3b7a2007-04-26 15:57:07 -0700135
136 key = afs_request_key(vnode->volume->cell);
137 if (IS_ERR(key)) {
David Howells215804a2017-11-02 15:27:52 +0000138 ret = PTR_ERR(key);
139 goto error;
140 }
141
142 af = kzalloc(sizeof(*af), GFP_KERNEL);
143 if (!af) {
144 ret = -ENOMEM;
145 goto error_key;
David Howells00d3b7a2007-04-26 15:57:07 -0700146 }
David Howells4343d002017-11-02 15:27:52 +0000147 af->key = key;
David Howells00d3b7a2007-04-26 15:57:07 -0700148
David Howells260a9802007-04-26 15:59:35 -0700149 ret = afs_validate(vnode, key);
David Howells215804a2017-11-02 15:27:52 +0000150 if (ret < 0)
151 goto error_af;
David Howells260a9802007-04-26 15:59:35 -0700152
David Howells4343d002017-11-02 15:27:52 +0000153 if (file->f_mode & FMODE_WRITE) {
154 ret = afs_cache_wb_key(vnode, af);
155 if (ret < 0)
156 goto error_af;
157 }
David Howells5a813272018-04-06 14:17:26 +0100158
159 if (file->f_flags & O_TRUNC)
160 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
David Howells4343d002017-11-02 15:27:52 +0000161
David Howells215804a2017-11-02 15:27:52 +0000162 file->private_data = af;
David Howells00d3b7a2007-04-26 15:57:07 -0700163 _leave(" = 0");
164 return 0;
David Howells215804a2017-11-02 15:27:52 +0000165
166error_af:
167 kfree(af);
168error_key:
169 key_put(key);
170error:
171 _leave(" = %d", ret);
172 return ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700173}
174
175/*
176 * release an AFS file or directory and discard its key
177 */
178int afs_release(struct inode *inode, struct file *file)
179{
180 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells215804a2017-11-02 15:27:52 +0000181 struct afs_file *af = file->private_data;
David Howellsa1b879e2019-05-15 12:09:17 +0100182 int ret = 0;
David Howells00d3b7a2007-04-26 15:57:07 -0700183
David Howells3b6492d2018-10-20 00:57:57 +0100184 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
David Howells00d3b7a2007-04-26 15:57:07 -0700185
David Howells5a813272018-04-06 14:17:26 +0100186 if ((file->f_mode & FMODE_WRITE))
David Howellsa1b879e2019-05-15 12:09:17 +0100187 ret = vfs_fsync(file, 0);
David Howells5a813272018-04-06 14:17:26 +0100188
David Howells215804a2017-11-02 15:27:52 +0000189 file->private_data = NULL;
David Howells4343d002017-11-02 15:27:52 +0000190 if (af->wb)
191 afs_put_wb_key(af->wb);
David Howells215804a2017-11-02 15:27:52 +0000192 key_put(af->key);
193 kfree(af);
David Howells4343d002017-11-02 15:27:52 +0000194 afs_prune_wb_keys(vnode);
David Howellsa1b879e2019-05-15 12:09:17 +0100195 _leave(" = %d", ret);
196 return ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700197}
198
David Howells196ee9c2017-01-05 10:38:34 +0000199/*
David Howells5cbf0392020-02-06 14:22:29 +0000200 * Allocate a new read record.
David Howellsc4508462020-02-06 14:22:28 +0000201 */
David Howells5cbf0392020-02-06 14:22:29 +0000202struct afs_read *afs_alloc_read(gfp_t gfp)
David Howellsc4508462020-02-06 14:22:28 +0000203{
David Howells5cbf0392020-02-06 14:22:29 +0000204 struct afs_read *req;
David Howellsc4508462020-02-06 14:22:28 +0000205
David Howells5cbf0392020-02-06 14:22:29 +0000206 req = kzalloc(sizeof(struct afs_read), gfp);
207 if (req)
208 refcount_set(&req->usage, 1);
David Howellsc4508462020-02-06 14:22:28 +0000209
David Howells5cbf0392020-02-06 14:22:29 +0000210 return req;
David Howellsc4508462020-02-06 14:22:28 +0000211}
212
213/*
David Howells196ee9c2017-01-05 10:38:34 +0000214 * Dispose of a ref to a read record.
215 */
216void afs_put_read(struct afs_read *req)
217{
David Howellsf3ddee82018-04-06 14:17:25 +0100218 if (refcount_dec_and_test(&req->usage)) {
David Howellsc4508462020-02-06 14:22:28 +0000219 if (req->cleanup)
220 req->cleanup(req);
David Howellsc69bf472020-02-06 14:22:27 +0000221 key_put(req->key);
David Howells196ee9c2017-01-05 10:38:34 +0000222 kfree(req);
223 }
224}
225
David Howellsdc419182020-09-18 09:11:15 +0100226static void afs_fetch_data_notify(struct afs_operation *op)
227{
228 struct afs_read *req = op->fetch.req;
David Howells5cbf0392020-02-06 14:22:29 +0000229 struct netfs_read_subrequest *subreq = req->subreq;
David Howellsdc419182020-09-18 09:11:15 +0100230 int error = op->error;
231
232 if (error == -ECONNABORTED)
233 error = afs_abort_to_error(op->ac.abort_code);
234 req->error = error;
235
David Howells5cbf0392020-02-06 14:22:29 +0000236 if (subreq) {
237 __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
238 netfs_subreq_terminated(subreq, error ?: req->actual_len, false);
239 req->subreq = NULL;
240 } else if (req->done) {
David Howellsdc419182020-09-18 09:11:15 +0100241 req->done(req);
David Howells5cbf0392020-02-06 14:22:29 +0000242 }
David Howellsdc419182020-09-18 09:11:15 +0100243}
244
David Howellse49c7b22020-04-10 20:51:51 +0100245static void afs_fetch_data_success(struct afs_operation *op)
246{
247 struct afs_vnode *vnode = op->file[0].vnode;
248
249 _enter("op=%08x", op->debug_id);
David Howellse49c7b22020-04-10 20:51:51 +0100250 afs_vnode_commit_status(op, &op->file[0]);
251 afs_stat_v(vnode, n_fetches);
252 atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes);
David Howellsdc419182020-09-18 09:11:15 +0100253 afs_fetch_data_notify(op);
David Howellse49c7b22020-04-10 20:51:51 +0100254}
255
256static void afs_fetch_data_put(struct afs_operation *op)
257{
David Howellsc4508462020-02-06 14:22:28 +0000258 op->fetch.req->error = op->error;
David Howellse49c7b22020-04-10 20:51:51 +0100259 afs_put_read(op->fetch.req);
260}
261
262static const struct afs_operation_ops afs_fetch_data_operation = {
263 .issue_afs_rpc = afs_fs_fetch_data,
264 .issue_yfs_rpc = yfs_fs_fetch_data,
265 .success = afs_fetch_data_success,
David Howells728279a2020-06-16 00:34:09 +0100266 .aborted = afs_check_for_remote_deletion,
David Howellsdc419182020-09-18 09:11:15 +0100267 .failed = afs_fetch_data_notify,
David Howellse49c7b22020-04-10 20:51:51 +0100268 .put = afs_fetch_data_put,
269};
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271/*
David Howellsd2ddc772017-11-02 15:27:50 +0000272 * Fetch file data from the volume.
273 */
David Howellsc69bf472020-02-06 14:22:27 +0000274int afs_fetch_data(struct afs_vnode *vnode, struct afs_read *req)
David Howellsd2ddc772017-11-02 15:27:50 +0000275{
David Howellse49c7b22020-04-10 20:51:51 +0100276 struct afs_operation *op;
David Howellsd2ddc772017-11-02 15:27:50 +0000277
David Howells3b6492d2018-10-20 00:57:57 +0100278 _enter("%s{%llx:%llu.%u},%x,,,",
David Howellsd2ddc772017-11-02 15:27:50 +0000279 vnode->volume->name,
280 vnode->fid.vid,
281 vnode->fid.vnode,
282 vnode->fid.unique,
David Howellsc69bf472020-02-06 14:22:27 +0000283 key_serial(req->key));
David Howellsd2ddc772017-11-02 15:27:50 +0000284
David Howellsc69bf472020-02-06 14:22:27 +0000285 op = afs_alloc_operation(req->key, vnode->volume);
David Howells5cbf0392020-02-06 14:22:29 +0000286 if (IS_ERR(op)) {
287 if (req->subreq)
288 netfs_subreq_terminated(req->subreq, PTR_ERR(op), false);
David Howellse49c7b22020-04-10 20:51:51 +0100289 return PTR_ERR(op);
David Howells5cbf0392020-02-06 14:22:29 +0000290 }
David Howellsa58823a2019-05-09 15:16:10 +0100291
David Howellse49c7b22020-04-10 20:51:51 +0100292 afs_op_set_vnode(op, 0, vnode);
David Howellsa58823a2019-05-09 15:16:10 +0100293
David Howellse49c7b22020-04-10 20:51:51 +0100294 op->fetch.req = afs_get_read(req);
295 op->ops = &afs_fetch_data_operation;
296 return afs_do_sync_operation(op);
David Howellsd2ddc772017-11-02 15:27:50 +0000297}
298
David Howells5cbf0392020-02-06 14:22:29 +0000299static void afs_req_issue_op(struct netfs_read_subrequest *subreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
David Howells5cbf0392020-02-06 14:22:29 +0000301 struct afs_vnode *vnode = AFS_FS_I(subreq->rreq->inode);
302 struct afs_read *fsreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
David Howells5cbf0392020-02-06 14:22:29 +0000304 fsreq = afs_alloc_read(GFP_NOFS);
305 if (!fsreq)
306 return netfs_subreq_terminated(subreq, -ENOMEM, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
David Howells5cbf0392020-02-06 14:22:29 +0000308 fsreq->subreq = subreq;
309 fsreq->pos = subreq->start + subreq->transferred;
310 fsreq->len = subreq->len - subreq->transferred;
David Howells345e1ae2021-08-27 23:22:02 +0100311 fsreq->key = key_get(subreq->rreq->netfs_priv);
David Howells5cbf0392020-02-06 14:22:29 +0000312 fsreq->vnode = vnode;
313 fsreq->iter = &fsreq->def_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
David Howells5cbf0392020-02-06 14:22:29 +0000315 iov_iter_xarray(&fsreq->def_iter, READ,
316 &fsreq->vnode->vfs_inode.i_mapping->i_pages,
317 fsreq->pos, fsreq->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
David Howells5cbf0392020-02-06 14:22:29 +0000319 afs_fetch_data(fsreq->vnode, fsreq);
David Howells345e1ae2021-08-27 23:22:02 +0100320 afs_put_read(fsreq);
David Howellsec268152007-04-26 15:49:28 -0700321}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
David Howells75bd2282021-06-29 22:23:13 +0100323static int afs_symlink_readpage(struct file *file, struct page *page)
Al Virof6d335c2010-05-21 15:27:09 +0100324{
David Howells5cbf0392020-02-06 14:22:29 +0000325 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
326 struct afs_read *fsreq;
David Howells78525c72021-08-11 09:49:13 +0100327 struct folio *folio = page_folio(page);
Al Virof6d335c2010-05-21 15:27:09 +0100328 int ret;
329
David Howells5cbf0392020-02-06 14:22:29 +0000330 fsreq = afs_alloc_read(GFP_NOFS);
331 if (!fsreq)
David Howells91b467e2017-01-05 10:38:35 +0000332 return -ENOMEM;
333
David Howells78525c72021-08-11 09:49:13 +0100334 fsreq->pos = folio_pos(folio);
335 fsreq->len = folio_size(folio);
David Howells5cbf0392020-02-06 14:22:29 +0000336 fsreq->vnode = vnode;
337 fsreq->iter = &fsreq->def_iter;
338 iov_iter_xarray(&fsreq->def_iter, READ, &page->mapping->i_pages,
339 fsreq->pos, fsreq->len);
David Howells91b467e2017-01-05 10:38:35 +0000340
David Howells5cbf0392020-02-06 14:22:29 +0000341 ret = afs_fetch_data(fsreq->vnode, fsreq);
David Howells78525c72021-08-11 09:49:13 +0100342 if (ret == 0)
343 SetPageUptodate(page);
344 unlock_page(page);
David Howells91b467e2017-01-05 10:38:35 +0000345 return ret;
346}
347
David Howells5cbf0392020-02-06 14:22:29 +0000348static void afs_init_rreq(struct netfs_read_request *rreq, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
David Howells5cbf0392020-02-06 14:22:29 +0000350 rreq->netfs_priv = key_get(afs_file_key(file));
351}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
David Howells3003bbd2020-02-06 14:22:29 +0000353static bool afs_is_cache_enabled(struct inode *inode)
354{
355 struct fscache_cookie *cookie = afs_vnode_cache(AFS_FS_I(inode));
356
357 return fscache_cookie_enabled(cookie) && !hlist_empty(&cookie->backing_objects);
358}
359
David Howells5cbf0392020-02-06 14:22:29 +0000360static int afs_begin_cache_operation(struct netfs_read_request *rreq)
361{
362 struct afs_vnode *vnode = AFS_FS_I(rreq->inode);
Al Virof6d335c2010-05-21 15:27:09 +0100363
David Howells5cbf0392020-02-06 14:22:29 +0000364 return fscache_begin_read_operation(rreq, afs_vnode_cache(vnode));
365}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
David Howells3003bbd2020-02-06 14:22:29 +0000367static int afs_check_write_begin(struct file *file, loff_t pos, unsigned len,
David Howells78525c72021-08-11 09:49:13 +0100368 struct folio *folio, void **_fsdata)
David Howells3003bbd2020-02-06 14:22:29 +0000369{
370 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
371
372 return test_bit(AFS_VNODE_DELETED, &vnode->flags) ? -ESTALE : 0;
373}
374
David Howells5cbf0392020-02-06 14:22:29 +0000375static void afs_priv_cleanup(struct address_space *mapping, void *netfs_priv)
376{
377 key_put(netfs_priv);
378}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
David Howells3003bbd2020-02-06 14:22:29 +0000380const struct netfs_read_request_ops afs_req_ops = {
David Howells5cbf0392020-02-06 14:22:29 +0000381 .init_rreq = afs_init_rreq,
David Howells3003bbd2020-02-06 14:22:29 +0000382 .is_cache_enabled = afs_is_cache_enabled,
David Howells5cbf0392020-02-06 14:22:29 +0000383 .begin_cache_operation = afs_begin_cache_operation,
David Howells3003bbd2020-02-06 14:22:29 +0000384 .check_write_begin = afs_check_write_begin,
David Howells5cbf0392020-02-06 14:22:29 +0000385 .issue_op = afs_req_issue_op,
386 .cleanup = afs_priv_cleanup,
387};
David Howells9b3f26c2009-04-03 16:42:41 +0100388
David Howells5cbf0392020-02-06 14:22:29 +0000389static int afs_readpage(struct file *file, struct page *page)
390{
David Howells78525c72021-08-11 09:49:13 +0100391 struct folio *folio = page_folio(page);
392
393 return netfs_readpage(file, folio, &afs_req_ops, NULL);
David Howells5cbf0392020-02-06 14:22:29 +0000394}
395
396static void afs_readahead(struct readahead_control *ractl)
397{
398 netfs_readahead(ractl, &afs_req_ops, NULL);
David Howellsec268152007-04-26 15:49:28 -0700399}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401/*
David Howellsf86726a2020-10-22 14:08:23 +0100402 * Adjust the dirty region of the page on truncation or full invalidation,
403 * getting rid of the markers altogether if the region is entirely invalidated.
404 */
David Howells78525c72021-08-11 09:49:13 +0100405static void afs_invalidate_dirty(struct folio *folio, unsigned int offset,
David Howellsf86726a2020-10-22 14:08:23 +0100406 unsigned int length)
407{
David Howells78525c72021-08-11 09:49:13 +0100408 struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio));
David Howellsf86726a2020-10-22 14:08:23 +0100409 unsigned long priv;
410 unsigned int f, t, end = offset + length;
411
David Howells78525c72021-08-11 09:49:13 +0100412 priv = (unsigned long)folio_get_private(folio);
David Howellsf86726a2020-10-22 14:08:23 +0100413
414 /* we clean up only if the entire page is being invalidated */
David Howells78525c72021-08-11 09:49:13 +0100415 if (offset == 0 && length == folio_size(folio))
David Howellsf86726a2020-10-22 14:08:23 +0100416 goto full_invalidate;
417
418 /* If the page was dirtied by page_mkwrite(), the PTE stays writable
419 * and we don't get another notification to tell us to expand it
420 * again.
421 */
David Howells78525c72021-08-11 09:49:13 +0100422 if (afs_is_folio_dirty_mmapped(priv))
David Howellsf86726a2020-10-22 14:08:23 +0100423 return;
424
425 /* We may need to shorten the dirty region */
David Howells78525c72021-08-11 09:49:13 +0100426 f = afs_folio_dirty_from(folio, priv);
427 t = afs_folio_dirty_to(folio, priv);
David Howellsf86726a2020-10-22 14:08:23 +0100428
429 if (t <= offset || f >= end)
430 return; /* Doesn't overlap */
431
432 if (f < offset && t > end)
433 return; /* Splits the dirty region - just absorb it */
434
435 if (f >= offset && t <= end)
436 goto undirty;
437
438 if (f < offset)
439 t = offset;
440 else
441 f = end;
442 if (f == t)
443 goto undirty;
444
David Howells78525c72021-08-11 09:49:13 +0100445 priv = afs_folio_dirty(folio, f, t);
446 folio_change_private(folio, (void *)priv);
447 trace_afs_folio_dirty(vnode, tracepoint_string("trunc"), folio);
David Howellsf86726a2020-10-22 14:08:23 +0100448 return;
449
450undirty:
David Howells78525c72021-08-11 09:49:13 +0100451 trace_afs_folio_dirty(vnode, tracepoint_string("undirty"), folio);
452 folio_clear_dirty_for_io(folio);
David Howellsf86726a2020-10-22 14:08:23 +0100453full_invalidate:
David Howells78525c72021-08-11 09:49:13 +0100454 trace_afs_folio_dirty(vnode, tracepoint_string("inval"), folio);
455 folio_detach_private(folio);
David Howellsf86726a2020-10-22 14:08:23 +0100456}
457
458/*
David Howells9b3f26c2009-04-03 16:42:41 +0100459 * invalidate part or all of a page
460 * - release a page and clean up its private data if offset is 0 (indicating
461 * the entire page)
462 */
Lukas Czernerd47992f2013-05-21 23:17:23 -0400463static void afs_invalidatepage(struct page *page, unsigned int offset,
464 unsigned int length)
David Howells9b3f26c2009-04-03 16:42:41 +0100465{
David Howells78525c72021-08-11 09:49:13 +0100466 struct folio *folio = page_folio(page);
467
468 _enter("{%lu},%u,%u", folio_index(folio), offset, length);
David Howells9b3f26c2009-04-03 16:42:41 +0100469
470 BUG_ON(!PageLocked(page));
471
David Howellsf86726a2020-10-22 14:08:23 +0100472 if (PagePrivate(page))
David Howells78525c72021-08-11 09:49:13 +0100473 afs_invalidate_dirty(folio, offset, length);
David Howells9b3f26c2009-04-03 16:42:41 +0100474
David Howells78525c72021-08-11 09:49:13 +0100475 folio_wait_fscache(folio);
David Howells9b3f26c2009-04-03 16:42:41 +0100476 _leave("");
477}
478
479/*
480 * release a page and clean up its private state if it's not busy
481 * - return true if the page can now be released, false if not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 */
David Howells416351f2007-05-09 02:33:45 -0700483static int afs_releasepage(struct page *page, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
David Howells78525c72021-08-11 09:49:13 +0100485 struct folio *folio = page_folio(page);
486 struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
David Howells3b6492d2018-10-20 00:57:57 +0100488 _enter("{{%llx:%llu}[%lu],%lx},%x",
David Howells78525c72021-08-11 09:49:13 +0100489 vnode->fid.vid, vnode->fid.vnode, folio_index(folio), folio->flags,
David Howells416351f2007-05-09 02:33:45 -0700490 gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
David Howells9b3f26c2009-04-03 16:42:41 +0100492 /* deny if page is being written to the cache and the caller hasn't
493 * elected to wait */
David Howells630f5dd2020-02-06 14:22:28 +0000494#ifdef CONFIG_AFS_FSCACHE
David Howells78525c72021-08-11 09:49:13 +0100495 if (folio_test_fscache(folio)) {
David Howells630f5dd2020-02-06 14:22:28 +0000496 if (!(gfp_flags & __GFP_DIRECT_RECLAIM) || !(gfp_flags & __GFP_FS))
497 return false;
David Howells78525c72021-08-11 09:49:13 +0100498 folio_wait_fscache(folio);
David Howells630f5dd2020-02-06 14:22:28 +0000499 }
500#endif
501
David Howells78525c72021-08-11 09:49:13 +0100502 if (folio_test_private(folio)) {
503 trace_afs_folio_dirty(vnode, tracepoint_string("rel"), folio);
504 folio_detach_private(folio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506
David Howells78525c72021-08-11 09:49:13 +0100507 /* Indicate that the folio can be released */
David Howells9b3f26c2009-04-03 16:42:41 +0100508 _leave(" = T");
David Howells78525c72021-08-11 09:49:13 +0100509 return true;
David Howellsec268152007-04-26 15:49:28 -0700510}
David Howells1cf7a152017-11-02 15:27:52 +0000511
David Howells6e0e99d2021-09-02 16:43:10 +0100512static void afs_add_open_mmap(struct afs_vnode *vnode)
513{
514 if (atomic_inc_return(&vnode->cb_nr_mmap) == 1) {
515 down_write(&vnode->volume->cell->fs_open_mmaps_lock);
516
David Howells1744a222021-12-14 09:22:12 +0000517 if (list_empty(&vnode->cb_mmap_link))
518 list_add_tail(&vnode->cb_mmap_link,
519 &vnode->volume->cell->fs_open_mmaps);
David Howells6e0e99d2021-09-02 16:43:10 +0100520
521 up_write(&vnode->volume->cell->fs_open_mmaps_lock);
522 }
523}
524
525static void afs_drop_open_mmap(struct afs_vnode *vnode)
526{
527 if (!atomic_dec_and_test(&vnode->cb_nr_mmap))
528 return;
529
530 down_write(&vnode->volume->cell->fs_open_mmaps_lock);
531
532 if (atomic_read(&vnode->cb_nr_mmap) == 0)
533 list_del_init(&vnode->cb_mmap_link);
534
535 up_write(&vnode->volume->cell->fs_open_mmaps_lock);
536 flush_work(&vnode->cb_work);
537}
538
David Howells1cf7a152017-11-02 15:27:52 +0000539/*
540 * Handle setting up a memory mapping on an AFS file.
541 */
542static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
543{
David Howells6e0e99d2021-09-02 16:43:10 +0100544 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
David Howells1cf7a152017-11-02 15:27:52 +0000545 int ret;
546
David Howells6e0e99d2021-09-02 16:43:10 +0100547 afs_add_open_mmap(vnode);
548
David Howells1cf7a152017-11-02 15:27:52 +0000549 ret = generic_file_mmap(file, vma);
550 if (ret == 0)
551 vma->vm_ops = &afs_vm_ops;
David Howells6e0e99d2021-09-02 16:43:10 +0100552 else
553 afs_drop_open_mmap(vnode);
David Howells1cf7a152017-11-02 15:27:52 +0000554 return ret;
555}
David Howells3978d812021-09-01 19:22:50 +0100556
David Howells6e0e99d2021-09-02 16:43:10 +0100557static void afs_vm_open(struct vm_area_struct *vma)
558{
559 afs_add_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
560}
561
562static void afs_vm_close(struct vm_area_struct *vma)
563{
564 afs_drop_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
565}
566
567static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff)
568{
569 struct afs_vnode *vnode = AFS_FS_I(file_inode(vmf->vma->vm_file));
570 struct afs_file *af = vmf->vma->vm_file->private_data;
571
572 switch (afs_validate(vnode, af->key)) {
573 case 0:
574 return filemap_map_pages(vmf, start_pgoff, end_pgoff);
575 case -ENOMEM:
576 return VM_FAULT_OOM;
577 case -EINTR:
578 case -ERESTARTSYS:
579 return VM_FAULT_RETRY;
580 case -ESTALE:
581 default:
582 return VM_FAULT_SIGBUS;
583 }
584}
585
David Howells3978d812021-09-01 19:22:50 +0100586static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
587{
588 struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
589 struct afs_file *af = iocb->ki_filp->private_data;
590 int ret;
591
592 ret = afs_validate(vnode, af->key);
593 if (ret < 0)
594 return ret;
595
596 return generic_file_read_iter(iocb, iter);
597}