xref: /openbmc/linux/fs/afs/file.c (revision 26d0dfbb16fcb17d128a79dc70f3020ea6992af0)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
208e0e7c8SDavid Howells /* AFS filesystem file handling
31da177e4SLinus Torvalds  *
408e0e7c8SDavid Howells  * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
51da177e4SLinus Torvalds  * Written by David Howells (dhowells@redhat.com)
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include <linux/kernel.h>
91da177e4SLinus Torvalds #include <linux/module.h>
101da177e4SLinus Torvalds #include <linux/init.h>
111da177e4SLinus Torvalds #include <linux/fs.h>
121da177e4SLinus Torvalds #include <linux/pagemap.h>
1331143d5dSDavid Howells #include <linux/writeback.h>
145a0e3ad6STejun Heo #include <linux/gfp.h>
1591b467e0SDavid Howells #include <linux/task_io_accounting_ops.h>
16f86196eaSNikolay Borisov #include <linux/mm.h>
17d7bdba1cSDavid Howells #include <linux/swap.h>
185cbf0398SDavid Howells #include <linux/netfs.h>
191da177e4SLinus Torvalds #include "internal.h"
201da177e4SLinus Torvalds 
211cf7a151SDavid Howells static int afs_file_mmap(struct file *file, struct vm_area_struct *vma);
22d7e0f539SMatthew Wilcox (Oracle) static int afs_symlink_read_folio(struct file *file, struct folio *folio);
23fcf227daSMatthew Wilcox (Oracle) static void afs_invalidate_folio(struct folio *folio, size_t offset,
24fcf227daSMatthew Wilcox (Oracle) 			       size_t length);
25508cae68SMatthew Wilcox (Oracle) static bool afs_release_folio(struct folio *folio, gfp_t gfp_flags);
261da177e4SLinus Torvalds 
273978d816SDavid Howells static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter);
28d96d96eeSDavid Howells static ssize_t afs_file_splice_read(struct file *in, loff_t *ppos,
29d96d96eeSDavid Howells 				    struct pipe_inode_info *pipe,
30d96d96eeSDavid Howells 				    size_t len, unsigned int flags);
316e0e99d5SDavid Howells static void afs_vm_open(struct vm_area_struct *area);
326e0e99d5SDavid Howells static void afs_vm_close(struct vm_area_struct *area);
336e0e99d5SDavid Howells static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff);
349b3f26c9SDavid Howells 
3500d3b7a4SDavid Howells const struct file_operations afs_file_operations = {
3600d3b7a4SDavid Howells 	.open		= afs_open,
3700d3b7a4SDavid Howells 	.release	= afs_release,
3800d3b7a4SDavid Howells 	.llseek		= generic_file_llseek,
393978d816SDavid Howells 	.read_iter	= afs_file_read_iter,
4050b5551dSAl Viro 	.write_iter	= afs_file_write,
411cf7a151SDavid Howells 	.mmap		= afs_file_mmap,
42d96d96eeSDavid Howells 	.splice_read	= afs_file_splice_read,
4306a17bbeSDavid Howells 	.splice_write	= iter_file_splice_write,
4431143d5dSDavid Howells 	.fsync		= afs_fsync,
45e8d6c554SDavid Howells 	.lock		= afs_lock,
46e8d6c554SDavid Howells 	.flock		= afs_flock,
4700d3b7a4SDavid Howells };
4800d3b7a4SDavid Howells 
49754661f1SArjan van de Ven const struct inode_operations afs_file_inode_operations = {
50416351f2SDavid Howells 	.getattr	= afs_getattr,
5131143d5dSDavid Howells 	.setattr	= afs_setattr,
5200d3b7a4SDavid Howells 	.permission	= afs_permission,
531da177e4SLinus Torvalds };
541da177e4SLinus Torvalds 
5575bd228dSDavid Howells const struct address_space_operations afs_file_aops = {
566c62371bSMatthew Wilcox (Oracle) 	.read_folio	= netfs_read_folio,
57bc899ee1SDavid Howells 	.readahead	= netfs_readahead,
588fb72b4aSMatthew Wilcox (Oracle) 	.dirty_folio	= afs_dirty_folio,
59a42442ddSMatthew Wilcox (Oracle) 	.launder_folio	= afs_launder_folio,
60508cae68SMatthew Wilcox (Oracle) 	.release_folio	= afs_release_folio,
61fcf227daSMatthew Wilcox (Oracle) 	.invalidate_folio = afs_invalidate_folio,
6215b4650eSNick Piggin 	.write_begin	= afs_write_begin,
6315b4650eSNick Piggin 	.write_end	= afs_write_end,
6431143d5dSDavid Howells 	.writepages	= afs_writepages,
65a9eb558aSDavid Howells 	.migrate_folio	= filemap_migrate_folio,
661da177e4SLinus Torvalds };
671da177e4SLinus Torvalds 
6875bd228dSDavid Howells const struct address_space_operations afs_symlink_aops = {
69d7e0f539SMatthew Wilcox (Oracle) 	.read_folio	= afs_symlink_read_folio,
70508cae68SMatthew Wilcox (Oracle) 	.release_folio	= afs_release_folio,
71fcf227daSMatthew Wilcox (Oracle) 	.invalidate_folio = afs_invalidate_folio,
72a9eb558aSDavid Howells 	.migrate_folio	= filemap_migrate_folio,
7375bd228dSDavid Howells };
7475bd228dSDavid Howells 
751cf7a151SDavid Howells static const struct vm_operations_struct afs_vm_ops = {
766e0e99d5SDavid Howells 	.open		= afs_vm_open,
776e0e99d5SDavid Howells 	.close		= afs_vm_close,
781cf7a151SDavid Howells 	.fault		= filemap_fault,
796e0e99d5SDavid Howells 	.map_pages	= afs_vm_map_pages,
801cf7a151SDavid Howells 	.page_mkwrite	= afs_page_mkwrite,
811cf7a151SDavid Howells };
821cf7a151SDavid Howells 
831da177e4SLinus Torvalds /*
844343d008SDavid Howells  * Discard a pin on a writeback key.
854343d008SDavid Howells  */
afs_put_wb_key(struct afs_wb_key * wbk)864343d008SDavid Howells void afs_put_wb_key(struct afs_wb_key *wbk)
874343d008SDavid Howells {
88e49c7b2fSDavid Howells 	if (wbk && refcount_dec_and_test(&wbk->usage)) {
894343d008SDavid Howells 		key_put(wbk->key);
904343d008SDavid Howells 		kfree(wbk);
914343d008SDavid Howells 	}
924343d008SDavid Howells }
934343d008SDavid Howells 
944343d008SDavid Howells /*
954343d008SDavid Howells  * Cache key for writeback.
964343d008SDavid Howells  */
afs_cache_wb_key(struct afs_vnode * vnode,struct afs_file * af)974343d008SDavid Howells int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af)
984343d008SDavid Howells {
994343d008SDavid Howells 	struct afs_wb_key *wbk, *p;
1004343d008SDavid Howells 
1014343d008SDavid Howells 	wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL);
1024343d008SDavid Howells 	if (!wbk)
1034343d008SDavid Howells 		return -ENOMEM;
1044343d008SDavid Howells 	refcount_set(&wbk->usage, 2);
1054343d008SDavid Howells 	wbk->key = af->key;
1064343d008SDavid Howells 
1074343d008SDavid Howells 	spin_lock(&vnode->wb_lock);
1084343d008SDavid Howells 	list_for_each_entry(p, &vnode->wb_keys, vnode_link) {
1094343d008SDavid Howells 		if (p->key == wbk->key)
1104343d008SDavid Howells 			goto found;
1114343d008SDavid Howells 	}
1124343d008SDavid Howells 
1134343d008SDavid Howells 	key_get(wbk->key);
1144343d008SDavid Howells 	list_add_tail(&wbk->vnode_link, &vnode->wb_keys);
1154343d008SDavid Howells 	spin_unlock(&vnode->wb_lock);
1164343d008SDavid Howells 	af->wb = wbk;
1174343d008SDavid Howells 	return 0;
1184343d008SDavid Howells 
1194343d008SDavid Howells found:
1204343d008SDavid Howells 	refcount_inc(&p->usage);
1214343d008SDavid Howells 	spin_unlock(&vnode->wb_lock);
1224343d008SDavid Howells 	af->wb = p;
1234343d008SDavid Howells 	kfree(wbk);
1244343d008SDavid Howells 	return 0;
1254343d008SDavid Howells }
1264343d008SDavid Howells 
1274343d008SDavid Howells /*
12800d3b7a4SDavid Howells  * open an AFS file or directory and attach a key to it
12900d3b7a4SDavid Howells  */
afs_open(struct inode * inode,struct file * file)13000d3b7a4SDavid Howells int afs_open(struct inode *inode, struct file *file)
13100d3b7a4SDavid Howells {
13200d3b7a4SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(inode);
133215804a9SDavid Howells 	struct afs_file *af;
13400d3b7a4SDavid Howells 	struct key *key;
135260a9803SDavid Howells 	int ret;
13600d3b7a4SDavid Howells 
1373b6492dfSDavid Howells 	_enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
13800d3b7a4SDavid Howells 
13900d3b7a4SDavid Howells 	key = afs_request_key(vnode->volume->cell);
14000d3b7a4SDavid Howells 	if (IS_ERR(key)) {
141215804a9SDavid Howells 		ret = PTR_ERR(key);
142215804a9SDavid Howells 		goto error;
143215804a9SDavid Howells 	}
144215804a9SDavid Howells 
145215804a9SDavid Howells 	af = kzalloc(sizeof(*af), GFP_KERNEL);
146215804a9SDavid Howells 	if (!af) {
147215804a9SDavid Howells 		ret = -ENOMEM;
148215804a9SDavid Howells 		goto error_key;
14900d3b7a4SDavid Howells 	}
1504343d008SDavid Howells 	af->key = key;
15100d3b7a4SDavid Howells 
152260a9803SDavid Howells 	ret = afs_validate(vnode, key);
153215804a9SDavid Howells 	if (ret < 0)
154215804a9SDavid Howells 		goto error_af;
155260a9803SDavid Howells 
1564343d008SDavid Howells 	if (file->f_mode & FMODE_WRITE) {
1574343d008SDavid Howells 		ret = afs_cache_wb_key(vnode, af);
1584343d008SDavid Howells 		if (ret < 0)
1594343d008SDavid Howells 			goto error_af;
1604343d008SDavid Howells 	}
1614343d008SDavid Howells 
1625a813276SDavid Howells 	if (file->f_flags & O_TRUNC)
1635a813276SDavid Howells 		set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
1645a813276SDavid Howells 
165523d27cdSDavid Howells 	fscache_use_cookie(afs_vnode_cache(vnode), file->f_mode & FMODE_WRITE);
166523d27cdSDavid Howells 
167215804a9SDavid Howells 	file->private_data = af;
16800d3b7a4SDavid Howells 	_leave(" = 0");
16900d3b7a4SDavid Howells 	return 0;
170215804a9SDavid Howells 
171215804a9SDavid Howells error_af:
172215804a9SDavid Howells 	kfree(af);
173215804a9SDavid Howells error_key:
174215804a9SDavid Howells 	key_put(key);
175215804a9SDavid Howells error:
176215804a9SDavid Howells 	_leave(" = %d", ret);
177215804a9SDavid Howells 	return ret;
17800d3b7a4SDavid Howells }
17900d3b7a4SDavid Howells 
18000d3b7a4SDavid Howells /*
18100d3b7a4SDavid Howells  * release an AFS file or directory and discard its key
18200d3b7a4SDavid Howells  */
afs_release(struct inode * inode,struct file * file)18300d3b7a4SDavid Howells int afs_release(struct inode *inode, struct file *file)
18400d3b7a4SDavid Howells {
185523d27cdSDavid Howells 	struct afs_vnode_cache_aux aux;
18600d3b7a4SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(inode);
187215804a9SDavid Howells 	struct afs_file *af = file->private_data;
188523d27cdSDavid Howells 	loff_t i_size;
189a1b879eeSDavid Howells 	int ret = 0;
19000d3b7a4SDavid Howells 
1913b6492dfSDavid Howells 	_enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
19200d3b7a4SDavid Howells 
1935a813276SDavid Howells 	if ((file->f_mode & FMODE_WRITE))
194a1b879eeSDavid Howells 		ret = vfs_fsync(file, 0);
1955a813276SDavid Howells 
196215804a9SDavid Howells 	file->private_data = NULL;
1974343d008SDavid Howells 	if (af->wb)
1984343d008SDavid Howells 		afs_put_wb_key(af->wb);
199523d27cdSDavid Howells 
200523d27cdSDavid Howells 	if ((file->f_mode & FMODE_WRITE)) {
201874c8ca1SDavid Howells 		i_size = i_size_read(&vnode->netfs.inode);
202523d27cdSDavid Howells 		afs_set_cache_aux(vnode, &aux);
203523d27cdSDavid Howells 		fscache_unuse_cookie(afs_vnode_cache(vnode), &aux, &i_size);
204523d27cdSDavid Howells 	} else {
205523d27cdSDavid Howells 		fscache_unuse_cookie(afs_vnode_cache(vnode), NULL, NULL);
206523d27cdSDavid Howells 	}
207523d27cdSDavid Howells 
208215804a9SDavid Howells 	key_put(af->key);
209215804a9SDavid Howells 	kfree(af);
2104343d008SDavid Howells 	afs_prune_wb_keys(vnode);
211a1b879eeSDavid Howells 	_leave(" = %d", ret);
212a1b879eeSDavid Howells 	return ret;
21300d3b7a4SDavid Howells }
21400d3b7a4SDavid Howells 
215196ee9cdSDavid Howells /*
2165cbf0398SDavid Howells  * Allocate a new read record.
217c4508464SDavid Howells  */
afs_alloc_read(gfp_t gfp)2185cbf0398SDavid Howells struct afs_read *afs_alloc_read(gfp_t gfp)
219c4508464SDavid Howells {
2205cbf0398SDavid Howells 	struct afs_read *req;
221c4508464SDavid Howells 
2225cbf0398SDavid Howells 	req = kzalloc(sizeof(struct afs_read), gfp);
2235cbf0398SDavid Howells 	if (req)
2245cbf0398SDavid Howells 		refcount_set(&req->usage, 1);
225c4508464SDavid Howells 
2265cbf0398SDavid Howells 	return req;
227c4508464SDavid Howells }
228c4508464SDavid Howells 
229c4508464SDavid Howells /*
230196ee9cdSDavid Howells  * Dispose of a ref to a read record.
231196ee9cdSDavid Howells  */
afs_put_read(struct afs_read * req)232196ee9cdSDavid Howells void afs_put_read(struct afs_read *req)
233196ee9cdSDavid Howells {
234f3ddee8dSDavid Howells 	if (refcount_dec_and_test(&req->usage)) {
235c4508464SDavid Howells 		if (req->cleanup)
236c4508464SDavid Howells 			req->cleanup(req);
237c69bf479SDavid Howells 		key_put(req->key);
238196ee9cdSDavid Howells 		kfree(req);
239196ee9cdSDavid Howells 	}
240196ee9cdSDavid Howells }
241196ee9cdSDavid Howells 
afs_fetch_data_notify(struct afs_operation * op)242dc419184SDavid Howells static void afs_fetch_data_notify(struct afs_operation *op)
243dc419184SDavid Howells {
244dc419184SDavid Howells 	struct afs_read *req = op->fetch.req;
2456a19114bSDavid Howells 	struct netfs_io_subrequest *subreq = req->subreq;
246dc419184SDavid Howells 	int error = op->error;
247dc419184SDavid Howells 
248dc419184SDavid Howells 	if (error == -ECONNABORTED)
249dc419184SDavid Howells 		error = afs_abort_to_error(op->ac.abort_code);
250dc419184SDavid Howells 	req->error = error;
251dc419184SDavid Howells 
2525cbf0398SDavid Howells 	if (subreq) {
2535cbf0398SDavid Howells 		__set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
2545cbf0398SDavid Howells 		netfs_subreq_terminated(subreq, error ?: req->actual_len, false);
2555cbf0398SDavid Howells 		req->subreq = NULL;
2565cbf0398SDavid Howells 	} else if (req->done) {
257dc419184SDavid Howells 		req->done(req);
258dc419184SDavid Howells 	}
2595cbf0398SDavid Howells }
260dc419184SDavid Howells 
afs_fetch_data_success(struct afs_operation * op)261e49c7b2fSDavid Howells static void afs_fetch_data_success(struct afs_operation *op)
262e49c7b2fSDavid Howells {
263e49c7b2fSDavid Howells 	struct afs_vnode *vnode = op->file[0].vnode;
264e49c7b2fSDavid Howells 
265e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
266e49c7b2fSDavid Howells 	afs_vnode_commit_status(op, &op->file[0]);
267e49c7b2fSDavid Howells 	afs_stat_v(vnode, n_fetches);
268e49c7b2fSDavid Howells 	atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes);
269dc419184SDavid Howells 	afs_fetch_data_notify(op);
270e49c7b2fSDavid Howells }
271e49c7b2fSDavid Howells 
afs_fetch_data_put(struct afs_operation * op)272e49c7b2fSDavid Howells static void afs_fetch_data_put(struct afs_operation *op)
273e49c7b2fSDavid Howells {
274c4508464SDavid Howells 	op->fetch.req->error = op->error;
275e49c7b2fSDavid Howells 	afs_put_read(op->fetch.req);
276e49c7b2fSDavid Howells }
277e49c7b2fSDavid Howells 
278e49c7b2fSDavid Howells static const struct afs_operation_ops afs_fetch_data_operation = {
279e49c7b2fSDavid Howells 	.issue_afs_rpc	= afs_fs_fetch_data,
280e49c7b2fSDavid Howells 	.issue_yfs_rpc	= yfs_fs_fetch_data,
281e49c7b2fSDavid Howells 	.success	= afs_fetch_data_success,
282728279a5SDavid Howells 	.aborted	= afs_check_for_remote_deletion,
283dc419184SDavid Howells 	.failed		= afs_fetch_data_notify,
284e49c7b2fSDavid Howells 	.put		= afs_fetch_data_put,
285e49c7b2fSDavid Howells };
286e49c7b2fSDavid Howells 
2871da177e4SLinus Torvalds /*
288d2ddc776SDavid Howells  * Fetch file data from the volume.
289d2ddc776SDavid Howells  */
afs_fetch_data(struct afs_vnode * vnode,struct afs_read * req)290c69bf479SDavid Howells int afs_fetch_data(struct afs_vnode *vnode, struct afs_read *req)
291d2ddc776SDavid Howells {
292e49c7b2fSDavid Howells 	struct afs_operation *op;
293d2ddc776SDavid Howells 
2943b6492dfSDavid Howells 	_enter("%s{%llx:%llu.%u},%x,,,",
295d2ddc776SDavid Howells 	       vnode->volume->name,
296d2ddc776SDavid Howells 	       vnode->fid.vid,
297d2ddc776SDavid Howells 	       vnode->fid.vnode,
298d2ddc776SDavid Howells 	       vnode->fid.unique,
299c69bf479SDavid Howells 	       key_serial(req->key));
300d2ddc776SDavid Howells 
301c69bf479SDavid Howells 	op = afs_alloc_operation(req->key, vnode->volume);
3025cbf0398SDavid Howells 	if (IS_ERR(op)) {
3035cbf0398SDavid Howells 		if (req->subreq)
3045cbf0398SDavid Howells 			netfs_subreq_terminated(req->subreq, PTR_ERR(op), false);
305e49c7b2fSDavid Howells 		return PTR_ERR(op);
3065cbf0398SDavid Howells 	}
307a58823acSDavid Howells 
308e49c7b2fSDavid Howells 	afs_op_set_vnode(op, 0, vnode);
309a58823acSDavid Howells 
310e49c7b2fSDavid Howells 	op->fetch.req	= afs_get_read(req);
311e49c7b2fSDavid Howells 	op->ops		= &afs_fetch_data_operation;
312e49c7b2fSDavid Howells 	return afs_do_sync_operation(op);
313d2ddc776SDavid Howells }
314d2ddc776SDavid Howells 
afs_issue_read(struct netfs_io_subrequest * subreq)315f18a3785SDavid Howells static void afs_issue_read(struct netfs_io_subrequest *subreq)
3161da177e4SLinus Torvalds {
3175cbf0398SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(subreq->rreq->inode);
3185cbf0398SDavid Howells 	struct afs_read *fsreq;
3195cbf0398SDavid Howells 
3205cbf0398SDavid Howells 	fsreq = afs_alloc_read(GFP_NOFS);
3215cbf0398SDavid Howells 	if (!fsreq)
3225cbf0398SDavid Howells 		return netfs_subreq_terminated(subreq, -ENOMEM, false);
3235cbf0398SDavid Howells 
3245cbf0398SDavid Howells 	fsreq->subreq	= subreq;
3255cbf0398SDavid Howells 	fsreq->pos	= subreq->start + subreq->transferred;
3265cbf0398SDavid Howells 	fsreq->len	= subreq->len   - subreq->transferred;
327345e1ae0SDavid Howells 	fsreq->key	= key_get(subreq->rreq->netfs_priv);
3285cbf0398SDavid Howells 	fsreq->vnode	= vnode;
3295cbf0398SDavid Howells 	fsreq->iter	= &fsreq->def_iter;
3305cbf0398SDavid Howells 
331de4eda9dSAl Viro 	iov_iter_xarray(&fsreq->def_iter, ITER_DEST,
332874c8ca1SDavid Howells 			&fsreq->vnode->netfs.inode.i_mapping->i_pages,
3335cbf0398SDavid Howells 			fsreq->pos, fsreq->len);
3345cbf0398SDavid Howells 
3355cbf0398SDavid Howells 	afs_fetch_data(fsreq->vnode, fsreq);
336345e1ae0SDavid Howells 	afs_put_read(fsreq);
3375cbf0398SDavid Howells }
3385cbf0398SDavid Howells 
afs_symlink_read_folio(struct file * file,struct folio * folio)339d7e0f539SMatthew Wilcox (Oracle) static int afs_symlink_read_folio(struct file *file, struct folio *folio)
3405cbf0398SDavid Howells {
341d7e0f539SMatthew Wilcox (Oracle) 	struct afs_vnode *vnode = AFS_FS_I(folio->mapping->host);
3425cbf0398SDavid Howells 	struct afs_read *fsreq;
3431da177e4SLinus Torvalds 	int ret;
3441da177e4SLinus Torvalds 
3455cbf0398SDavid Howells 	fsreq = afs_alloc_read(GFP_NOFS);
3465cbf0398SDavid Howells 	if (!fsreq)
34791b467e0SDavid Howells 		return -ENOMEM;
34891b467e0SDavid Howells 
34978525c74SDavid Howells 	fsreq->pos	= folio_pos(folio);
35078525c74SDavid Howells 	fsreq->len	= folio_size(folio);
3515cbf0398SDavid Howells 	fsreq->vnode	= vnode;
3525cbf0398SDavid Howells 	fsreq->iter	= &fsreq->def_iter;
353de4eda9dSAl Viro 	iov_iter_xarray(&fsreq->def_iter, ITER_DEST, &folio->mapping->i_pages,
3545cbf0398SDavid Howells 			fsreq->pos, fsreq->len);
35591b467e0SDavid Howells 
3565cbf0398SDavid Howells 	ret = afs_fetch_data(fsreq->vnode, fsreq);
35778525c74SDavid Howells 	if (ret == 0)
358d7e0f539SMatthew Wilcox (Oracle) 		folio_mark_uptodate(folio);
359d7e0f539SMatthew Wilcox (Oracle) 	folio_unlock(folio);
36091b467e0SDavid Howells 	return ret;
36191b467e0SDavid Howells }
36291b467e0SDavid Howells 
afs_init_request(struct netfs_io_request * rreq,struct file * file)3632de16041SDavid Howells static int afs_init_request(struct netfs_io_request *rreq, struct file *file)
3641da177e4SLinus Torvalds {
3655cbf0398SDavid Howells 	rreq->netfs_priv = key_get(afs_file_key(file));
3662de16041SDavid Howells 	return 0;
3671da177e4SLinus Torvalds }
3681da177e4SLinus Torvalds 
afs_begin_cache_operation(struct netfs_io_request * rreq)3696a19114bSDavid Howells static int afs_begin_cache_operation(struct netfs_io_request *rreq)
3705cbf0398SDavid Howells {
3712cee6fbbSDavid Howells #ifdef CONFIG_AFS_FSCACHE
3725cbf0398SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(rreq->inode);
3735cbf0398SDavid Howells 
374523d27cdSDavid Howells 	return fscache_begin_read_operation(&rreq->cache_resources,
375523d27cdSDavid Howells 					    afs_vnode_cache(vnode));
3762cee6fbbSDavid Howells #else
3772cee6fbbSDavid Howells 	return -ENOBUFS;
3782cee6fbbSDavid Howells #endif
37991b467e0SDavid Howells }
3809b3f26c9SDavid Howells 
afs_check_write_begin(struct file * file,loff_t pos,unsigned len,struct folio ** foliop,void ** _fsdata)3813003bbd0SDavid Howells static int afs_check_write_begin(struct file *file, loff_t pos, unsigned len,
382fac47b43SXiubo Li 				 struct folio **foliop, void **_fsdata)
3833003bbd0SDavid Howells {
3843003bbd0SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
3853003bbd0SDavid Howells 
3863003bbd0SDavid Howells 	return test_bit(AFS_VNODE_DELETED, &vnode->flags) ? -ESTALE : 0;
3873003bbd0SDavid Howells }
3883003bbd0SDavid Howells 
afs_free_request(struct netfs_io_request * rreq)38940a81101SDavid Howells static void afs_free_request(struct netfs_io_request *rreq)
3905cbf0398SDavid Howells {
39140a81101SDavid Howells 	key_put(rreq->netfs_priv);
3925cbf0398SDavid Howells }
3935cbf0398SDavid Howells 
3946a19114bSDavid Howells const struct netfs_request_ops afs_req_ops = {
3956a19114bSDavid Howells 	.init_request		= afs_init_request,
39640a81101SDavid Howells 	.free_request		= afs_free_request,
3975cbf0398SDavid Howells 	.begin_cache_operation	= afs_begin_cache_operation,
3983003bbd0SDavid Howells 	.check_write_begin	= afs_check_write_begin,
399f18a3785SDavid Howells 	.issue_read		= afs_issue_read,
4005cbf0398SDavid Howells };
4015cbf0398SDavid Howells 
afs_write_inode(struct inode * inode,struct writeback_control * wbc)402c7f75ef3SDavid Howells int afs_write_inode(struct inode *inode, struct writeback_control *wbc)
403c7f75ef3SDavid Howells {
404c7f75ef3SDavid Howells 	fscache_unpin_writeback(wbc, afs_vnode_cache(AFS_FS_I(inode)));
405c7f75ef3SDavid Howells 	return 0;
406c7f75ef3SDavid Howells }
407c7f75ef3SDavid Howells 
4081da177e4SLinus Torvalds /*
409f86726a6SDavid Howells  * Adjust the dirty region of the page on truncation or full invalidation,
410f86726a6SDavid Howells  * getting rid of the markers altogether if the region is entirely invalidated.
411f86726a6SDavid Howells  */
afs_invalidate_dirty(struct folio * folio,size_t offset,size_t length)412fcf227daSMatthew Wilcox (Oracle) static void afs_invalidate_dirty(struct folio *folio, size_t offset,
413fcf227daSMatthew Wilcox (Oracle) 				 size_t length)
414f86726a6SDavid Howells {
41578525c74SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio));
416f86726a6SDavid Howells 	unsigned long priv;
417f86726a6SDavid Howells 	unsigned int f, t, end = offset + length;
418f86726a6SDavid Howells 
41978525c74SDavid Howells 	priv = (unsigned long)folio_get_private(folio);
420f86726a6SDavid Howells 
421f86726a6SDavid Howells 	/* we clean up only if the entire page is being invalidated */
42278525c74SDavid Howells 	if (offset == 0 && length == folio_size(folio))
423f86726a6SDavid Howells 		goto full_invalidate;
424f86726a6SDavid Howells 
425f86726a6SDavid Howells 	 /* If the page was dirtied by page_mkwrite(), the PTE stays writable
426f86726a6SDavid Howells 	  * and we don't get another notification to tell us to expand it
427f86726a6SDavid Howells 	  * again.
428f86726a6SDavid Howells 	  */
42978525c74SDavid Howells 	if (afs_is_folio_dirty_mmapped(priv))
430f86726a6SDavid Howells 		return;
431f86726a6SDavid Howells 
432f86726a6SDavid Howells 	/* We may need to shorten the dirty region */
43378525c74SDavid Howells 	f = afs_folio_dirty_from(folio, priv);
43478525c74SDavid Howells 	t = afs_folio_dirty_to(folio, priv);
435f86726a6SDavid Howells 
436f86726a6SDavid Howells 	if (t <= offset || f >= end)
437f86726a6SDavid Howells 		return; /* Doesn't overlap */
438f86726a6SDavid Howells 
439f86726a6SDavid Howells 	if (f < offset && t > end)
440f86726a6SDavid Howells 		return; /* Splits the dirty region - just absorb it */
441f86726a6SDavid Howells 
442f86726a6SDavid Howells 	if (f >= offset && t <= end)
443f86726a6SDavid Howells 		goto undirty;
444f86726a6SDavid Howells 
445f86726a6SDavid Howells 	if (f < offset)
446f86726a6SDavid Howells 		t = offset;
447f86726a6SDavid Howells 	else
448f86726a6SDavid Howells 		f = end;
449f86726a6SDavid Howells 	if (f == t)
450f86726a6SDavid Howells 		goto undirty;
451f86726a6SDavid Howells 
45278525c74SDavid Howells 	priv = afs_folio_dirty(folio, f, t);
45378525c74SDavid Howells 	folio_change_private(folio, (void *)priv);
45478525c74SDavid Howells 	trace_afs_folio_dirty(vnode, tracepoint_string("trunc"), folio);
455f86726a6SDavid Howells 	return;
456f86726a6SDavid Howells 
457f86726a6SDavid Howells undirty:
45878525c74SDavid Howells 	trace_afs_folio_dirty(vnode, tracepoint_string("undirty"), folio);
45978525c74SDavid Howells 	folio_clear_dirty_for_io(folio);
460f86726a6SDavid Howells full_invalidate:
46178525c74SDavid Howells 	trace_afs_folio_dirty(vnode, tracepoint_string("inval"), folio);
46278525c74SDavid Howells 	folio_detach_private(folio);
463f86726a6SDavid Howells }
464f86726a6SDavid Howells 
465f86726a6SDavid Howells /*
4669b3f26c9SDavid Howells  * invalidate part or all of a page
4679b3f26c9SDavid Howells  * - release a page and clean up its private data if offset is 0 (indicating
4689b3f26c9SDavid Howells  *   the entire page)
4699b3f26c9SDavid Howells  */
afs_invalidate_folio(struct folio * folio,size_t offset,size_t length)470fcf227daSMatthew Wilcox (Oracle) static void afs_invalidate_folio(struct folio *folio, size_t offset,
471fcf227daSMatthew Wilcox (Oracle) 			       size_t length)
4729b3f26c9SDavid Howells {
473fcf227daSMatthew Wilcox (Oracle) 	_enter("{%lu},%zu,%zu", folio->index, offset, length);
47478525c74SDavid Howells 
475fcf227daSMatthew Wilcox (Oracle) 	BUG_ON(!folio_test_locked(folio));
4769b3f26c9SDavid Howells 
477fcf227daSMatthew Wilcox (Oracle) 	if (folio_get_private(folio))
47878525c74SDavid Howells 		afs_invalidate_dirty(folio, offset, length);
4799b3f26c9SDavid Howells 
48078525c74SDavid Howells 	folio_wait_fscache(folio);
4819b3f26c9SDavid Howells 	_leave("");
4829b3f26c9SDavid Howells }
4839b3f26c9SDavid Howells 
4849b3f26c9SDavid Howells /*
4859b3f26c9SDavid Howells  * release a page and clean up its private state if it's not busy
4869b3f26c9SDavid Howells  * - return true if the page can now be released, false if not
4871da177e4SLinus Torvalds  */
afs_release_folio(struct folio * folio,gfp_t gfp)488508cae68SMatthew Wilcox (Oracle) static bool afs_release_folio(struct folio *folio, gfp_t gfp)
4891da177e4SLinus Torvalds {
49078525c74SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio));
4911da177e4SLinus Torvalds 
4923b6492dfSDavid Howells 	_enter("{{%llx:%llu}[%lu],%lx},%x",
49378525c74SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode, folio_index(folio), folio->flags,
494523d27cdSDavid Howells 	       gfp);
4951da177e4SLinus Torvalds 
496508cae68SMatthew Wilcox (Oracle) 	/* deny if folio is being written to the cache and the caller hasn't
4979b3f26c9SDavid Howells 	 * elected to wait */
498630f5ddaSDavid Howells #ifdef CONFIG_AFS_FSCACHE
49978525c74SDavid Howells 	if (folio_test_fscache(folio)) {
500d7bdba1cSDavid Howells 		if (current_is_kswapd() || !(gfp & __GFP_FS))
501630f5ddaSDavid Howells 			return false;
50278525c74SDavid Howells 		folio_wait_fscache(folio);
503630f5ddaSDavid Howells 	}
504523d27cdSDavid Howells 	fscache_note_page_release(afs_vnode_cache(vnode));
505630f5ddaSDavid Howells #endif
506630f5ddaSDavid Howells 
50778525c74SDavid Howells 	if (folio_test_private(folio)) {
50878525c74SDavid Howells 		trace_afs_folio_dirty(vnode, tracepoint_string("rel"), folio);
50978525c74SDavid Howells 		folio_detach_private(folio);
5109b3f26c9SDavid Howells 	}
5119b3f26c9SDavid Howells 
51278525c74SDavid Howells 	/* Indicate that the folio can be released */
5139b3f26c9SDavid Howells 	_leave(" = T");
51478525c74SDavid Howells 	return true;
515ec26815aSDavid Howells }
5161cf7a151SDavid Howells 
afs_add_open_mmap(struct afs_vnode * vnode)5176e0e99d5SDavid Howells static void afs_add_open_mmap(struct afs_vnode *vnode)
5186e0e99d5SDavid Howells {
5196e0e99d5SDavid Howells 	if (atomic_inc_return(&vnode->cb_nr_mmap) == 1) {
5206e0e99d5SDavid Howells 		down_write(&vnode->volume->cell->fs_open_mmaps_lock);
5216e0e99d5SDavid Howells 
5221744a22aSDavid Howells 		if (list_empty(&vnode->cb_mmap_link))
5236e0e99d5SDavid Howells 			list_add_tail(&vnode->cb_mmap_link,
5246e0e99d5SDavid Howells 				      &vnode->volume->cell->fs_open_mmaps);
5256e0e99d5SDavid Howells 
5266e0e99d5SDavid Howells 		up_write(&vnode->volume->cell->fs_open_mmaps_lock);
5276e0e99d5SDavid Howells 	}
5286e0e99d5SDavid Howells }
5296e0e99d5SDavid Howells 
afs_drop_open_mmap(struct afs_vnode * vnode)5306e0e99d5SDavid Howells static void afs_drop_open_mmap(struct afs_vnode *vnode)
5316e0e99d5SDavid Howells {
532*e968edf6SAl Viro 	if (atomic_add_unless(&vnode->cb_nr_mmap, -1, 1))
5336e0e99d5SDavid Howells 		return;
5346e0e99d5SDavid Howells 
5356e0e99d5SDavid Howells 	down_write(&vnode->volume->cell->fs_open_mmaps_lock);
5366e0e99d5SDavid Howells 
537*e968edf6SAl Viro 	read_seqlock_excl(&vnode->cb_lock);
538*e968edf6SAl Viro 	// the only place where ->cb_nr_mmap may hit 0
539*e968edf6SAl Viro 	// see __afs_break_callback() for the other side...
540*e968edf6SAl Viro 	if (atomic_dec_and_test(&vnode->cb_nr_mmap))
5416e0e99d5SDavid Howells 		list_del_init(&vnode->cb_mmap_link);
542*e968edf6SAl Viro 	read_sequnlock_excl(&vnode->cb_lock);
5436e0e99d5SDavid Howells 
5446e0e99d5SDavid Howells 	up_write(&vnode->volume->cell->fs_open_mmaps_lock);
5456e0e99d5SDavid Howells 	flush_work(&vnode->cb_work);
5466e0e99d5SDavid Howells }
5476e0e99d5SDavid Howells 
5481cf7a151SDavid Howells /*
5491cf7a151SDavid Howells  * Handle setting up a memory mapping on an AFS file.
5501cf7a151SDavid Howells  */
afs_file_mmap(struct file * file,struct vm_area_struct * vma)5511cf7a151SDavid Howells static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
5521cf7a151SDavid Howells {
5536e0e99d5SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
5541cf7a151SDavid Howells 	int ret;
5551cf7a151SDavid Howells 
5566e0e99d5SDavid Howells 	afs_add_open_mmap(vnode);
5576e0e99d5SDavid Howells 
5581cf7a151SDavid Howells 	ret = generic_file_mmap(file, vma);
5591cf7a151SDavid Howells 	if (ret == 0)
5601cf7a151SDavid Howells 		vma->vm_ops = &afs_vm_ops;
5616e0e99d5SDavid Howells 	else
5626e0e99d5SDavid Howells 		afs_drop_open_mmap(vnode);
5631cf7a151SDavid Howells 	return ret;
5641cf7a151SDavid Howells }
5653978d816SDavid Howells 
afs_vm_open(struct vm_area_struct * vma)5666e0e99d5SDavid Howells static void afs_vm_open(struct vm_area_struct *vma)
5676e0e99d5SDavid Howells {
5686e0e99d5SDavid Howells 	afs_add_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
5696e0e99d5SDavid Howells }
5706e0e99d5SDavid Howells 
afs_vm_close(struct vm_area_struct * vma)5716e0e99d5SDavid Howells static void afs_vm_close(struct vm_area_struct *vma)
5726e0e99d5SDavid Howells {
5736e0e99d5SDavid Howells 	afs_drop_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
5746e0e99d5SDavid Howells }
5756e0e99d5SDavid Howells 
afs_vm_map_pages(struct vm_fault * vmf,pgoff_t start_pgoff,pgoff_t end_pgoff)5766e0e99d5SDavid Howells static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff)
5776e0e99d5SDavid Howells {
5786e0e99d5SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(file_inode(vmf->vma->vm_file));
5796e0e99d5SDavid Howells 
5800050d7f5SMatthew Wilcox (Oracle) 	if (afs_pagecache_valid(vnode))
5816e0e99d5SDavid Howells 		return filemap_map_pages(vmf, start_pgoff, end_pgoff);
5820050d7f5SMatthew Wilcox (Oracle) 	return 0;
5836e0e99d5SDavid Howells }
5846e0e99d5SDavid Howells 
afs_file_read_iter(struct kiocb * iocb,struct iov_iter * iter)5853978d816SDavid Howells static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
5863978d816SDavid Howells {
5873978d816SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
5883978d816SDavid Howells 	struct afs_file *af = iocb->ki_filp->private_data;
5893978d816SDavid Howells 	int ret;
5903978d816SDavid Howells 
5913978d816SDavid Howells 	ret = afs_validate(vnode, af->key);
5923978d816SDavid Howells 	if (ret < 0)
5933978d816SDavid Howells 		return ret;
5943978d816SDavid Howells 
5953978d816SDavid Howells 	return generic_file_read_iter(iocb, iter);
5963978d816SDavid Howells }
597d96d96eeSDavid Howells 
afs_file_splice_read(struct file * in,loff_t * ppos,struct pipe_inode_info * pipe,size_t len,unsigned int flags)598d96d96eeSDavid Howells static ssize_t afs_file_splice_read(struct file *in, loff_t *ppos,
599d96d96eeSDavid Howells 				    struct pipe_inode_info *pipe,
600d96d96eeSDavid Howells 				    size_t len, unsigned int flags)
601d96d96eeSDavid Howells {
602d96d96eeSDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(file_inode(in));
603d96d96eeSDavid Howells 	struct afs_file *af = in->private_data;
604d96d96eeSDavid Howells 	int ret;
605d96d96eeSDavid Howells 
606d96d96eeSDavid Howells 	ret = afs_validate(vnode, af->key);
607d96d96eeSDavid Howells 	if (ret < 0)
608d96d96eeSDavid Howells 		return ret;
609d96d96eeSDavid Howells 
6102cb1e089SDavid Howells 	return filemap_splice_read(in, ppos, pipe, len, flags);
611d96d96eeSDavid Howells }
612