1// SPDX-License-Identifier: GPL-2.0-or-later
2/* AFS filesystem file handling
3 *
4 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/fs.h>
12#include <linux/pagemap.h>
13#include <linux/writeback.h>
14#include <linux/gfp.h>
15#include <linux/task_io_accounting_ops.h>
16#include <linux/mm.h>
17#include <linux/swap.h>
18#include <linux/netfs.h>
19#include "internal.h"
20
21static int afs_file_mmap(struct file *file, struct vm_area_struct *vma);
22static int afs_symlink_read_folio(struct file *file, struct folio *folio);
23
24static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter);
25static ssize_t afs_file_splice_read(struct file *in, loff_t *ppos,
26				    struct pipe_inode_info *pipe,
27				    size_t len, unsigned int flags);
28static void afs_vm_open(struct vm_area_struct *area);
29static void afs_vm_close(struct vm_area_struct *area);
30static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff);
31
32const struct file_operations afs_file_operations = {
33	.open		= afs_open,
34	.release	= afs_release,
35	.llseek		= generic_file_llseek,
36	.read_iter	= afs_file_read_iter,
37	.write_iter	= netfs_file_write_iter,
38	.mmap		= afs_file_mmap,
39	.splice_read	= afs_file_splice_read,
40	.splice_write	= iter_file_splice_write,
41	.fsync		= afs_fsync,
42	.lock		= afs_lock,
43	.flock		= afs_flock,
44};
45
46const struct inode_operations afs_file_inode_operations = {
47	.getattr	= afs_getattr,
48	.setattr	= afs_setattr,
49	.permission	= afs_permission,
50};
51
52const struct address_space_operations afs_file_aops = {
53	.direct_IO	= noop_direct_IO,
54	.read_folio	= netfs_read_folio,
55	.readahead	= netfs_readahead,
56	.dirty_folio	= netfs_dirty_folio,
57	.release_folio	= netfs_release_folio,
58	.invalidate_folio = netfs_invalidate_folio,
59	.migrate_folio	= filemap_migrate_folio,
60	.writepages	= afs_writepages,
61};
62
63const struct address_space_operations afs_symlink_aops = {
64	.read_folio	= afs_symlink_read_folio,
65	.release_folio	= netfs_release_folio,
66	.invalidate_folio = netfs_invalidate_folio,
67	.migrate_folio	= filemap_migrate_folio,
68};
69
70static const struct vm_operations_struct afs_vm_ops = {
71	.open		= afs_vm_open,
72	.close		= afs_vm_close,
73	.fault		= filemap_fault,
74	.map_pages	= afs_vm_map_pages,
75	.page_mkwrite	= afs_page_mkwrite,
76};
77
78/*
79 * Discard a pin on a writeback key.
80 */
81void afs_put_wb_key(struct afs_wb_key *wbk)
82{
83	if (wbk && refcount_dec_and_test(&wbk->usage)) {
84		key_put(wbk->key);
85		kfree(wbk);
86	}
87}
88
89/*
90 * Cache key for writeback.
91 */
92int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af)
93{
94	struct afs_wb_key *wbk, *p;
95
96	wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL);
97	if (!wbk)
98		return -ENOMEM;
99	refcount_set(&wbk->usage, 2);
100	wbk->key = af->key;
101
102	spin_lock(&vnode->wb_lock);
103	list_for_each_entry(p, &vnode->wb_keys, vnode_link) {
104		if (p->key == wbk->key)
105			goto found;
106	}
107
108	key_get(wbk->key);
109	list_add_tail(&wbk->vnode_link, &vnode->wb_keys);
110	spin_unlock(&vnode->wb_lock);
111	af->wb = wbk;
112	return 0;
113
114found:
115	refcount_inc(&p->usage);
116	spin_unlock(&vnode->wb_lock);
117	af->wb = p;
118	kfree(wbk);
119	return 0;
120}
121
122/*
123 * open an AFS file or directory and attach a key to it
124 */
125int afs_open(struct inode *inode, struct file *file)
126{
127	struct afs_vnode *vnode = AFS_FS_I(inode);
128	struct afs_file *af;
129	struct key *key;
130	int ret;
131
132	_enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
133
134	key = afs_request_key(vnode->volume->cell);
135	if (IS_ERR(key)) {
136		ret = PTR_ERR(key);
137		goto error;
138	}
139
140	af = kzalloc(sizeof(*af), GFP_KERNEL);
141	if (!af) {
142		ret = -ENOMEM;
143		goto error_key;
144	}
145	af->key = key;
146
147	ret = afs_validate(vnode, key);
148	if (ret < 0)
149		goto error_af;
150
151	if (file->f_mode & FMODE_WRITE) {
152		ret = afs_cache_wb_key(vnode, af);
153		if (ret < 0)
154			goto error_af;
155	}
156
157	if (file->f_flags & O_TRUNC)
158		set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
159
160	fscache_use_cookie(afs_vnode_cache(vnode), file->f_mode & FMODE_WRITE);
161
162	file->private_data = af;
163	_leave(" = 0");
164	return 0;
165
166error_af:
167	kfree(af);
168error_key:
169	key_put(key);
170error:
171	_leave(" = %d", ret);
172	return ret;
173}
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_cache_aux aux;
181	struct afs_vnode *vnode = AFS_FS_I(inode);
182	struct afs_file *af = file->private_data;
183	loff_t i_size;
184	int ret = 0;
185
186	_enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
187
188	if ((file->f_mode & FMODE_WRITE))
189		ret = vfs_fsync(file, 0);
190
191	file->private_data = NULL;
192	if (af->wb)
193		afs_put_wb_key(af->wb);
194
195	if ((file->f_mode & FMODE_WRITE)) {
196		i_size = i_size_read(&vnode->netfs.inode);
197		afs_set_cache_aux(vnode, &aux);
198		fscache_unuse_cookie(afs_vnode_cache(vnode), &aux, &i_size);
199	} else {
200		fscache_unuse_cookie(afs_vnode_cache(vnode), NULL, NULL);
201	}
202
203	key_put(af->key);
204	kfree(af);
205	afs_prune_wb_keys(vnode);
206	_leave(" = %d", ret);
207	return ret;
208}
209
210/*
211 * Allocate a new read record.
212 */
213struct afs_read *afs_alloc_read(gfp_t gfp)
214{
215	struct afs_read *req;
216
217	req = kzalloc(sizeof(struct afs_read), gfp);
218	if (req)
219		refcount_set(&req->usage, 1);
220
221	return req;
222}
223
224/*
225 * Dispose of a ref to a read record.
226 */
227void afs_put_read(struct afs_read *req)
228{
229	if (refcount_dec_and_test(&req->usage)) {
230		if (req->cleanup)
231			req->cleanup(req);
232		key_put(req->key);
233		kfree(req);
234	}
235}
236
237static void afs_fetch_data_notify(struct afs_operation *op)
238{
239	struct afs_read *req = op->fetch.req;
240	struct netfs_io_subrequest *subreq = req->subreq;
241	int error = afs_op_error(op);
242
243	req->error = error;
244	if (subreq) {
245		__set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
246		netfs_subreq_terminated(subreq, error ?: req->actual_len, false);
247		req->subreq = NULL;
248	} else if (req->done) {
249		req->done(req);
250	}
251}
252
253static void afs_fetch_data_success(struct afs_operation *op)
254{
255	struct afs_vnode *vnode = op->file[0].vnode;
256
257	_enter("op=%08x", op->debug_id);
258	afs_vnode_commit_status(op, &op->file[0]);
259	afs_stat_v(vnode, n_fetches);
260	atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes);
261	afs_fetch_data_notify(op);
262}
263
264static void afs_fetch_data_put(struct afs_operation *op)
265{
266	op->fetch.req->error = afs_op_error(op);
267	afs_put_read(op->fetch.req);
268}
269
270static const struct afs_operation_ops afs_fetch_data_operation = {
271	.issue_afs_rpc	= afs_fs_fetch_data,
272	.issue_yfs_rpc	= yfs_fs_fetch_data,
273	.success	= afs_fetch_data_success,
274	.aborted	= afs_check_for_remote_deletion,
275	.failed		= afs_fetch_data_notify,
276	.put		= afs_fetch_data_put,
277};
278
279/*
280 * Fetch file data from the volume.
281 */
282int afs_fetch_data(struct afs_vnode *vnode, struct afs_read *req)
283{
284	struct afs_operation *op;
285
286	_enter("%s{%llx:%llu.%u},%x,,,",
287	       vnode->volume->name,
288	       vnode->fid.vid,
289	       vnode->fid.vnode,
290	       vnode->fid.unique,
291	       key_serial(req->key));
292
293	op = afs_alloc_operation(req->key, vnode->volume);
294	if (IS_ERR(op)) {
295		if (req->subreq)
296			netfs_subreq_terminated(req->subreq, PTR_ERR(op), false);
297		return PTR_ERR(op);
298	}
299
300	afs_op_set_vnode(op, 0, vnode);
301
302	op->fetch.req	= afs_get_read(req);
303	op->ops		= &afs_fetch_data_operation;
304	return afs_do_sync_operation(op);
305}
306
307static void afs_issue_read(struct netfs_io_subrequest *subreq)
308{
309	struct afs_vnode *vnode = AFS_FS_I(subreq->rreq->inode);
310	struct afs_read *fsreq;
311
312	fsreq = afs_alloc_read(GFP_NOFS);
313	if (!fsreq)
314		return netfs_subreq_terminated(subreq, -ENOMEM, false);
315
316	fsreq->subreq	= subreq;
317	fsreq->pos	= subreq->start + subreq->transferred;
318	fsreq->len	= subreq->len   - subreq->transferred;
319	fsreq->key	= key_get(subreq->rreq->netfs_priv);
320	fsreq->vnode	= vnode;
321	fsreq->iter	= &subreq->io_iter;
322
323	afs_fetch_data(fsreq->vnode, fsreq);
324	afs_put_read(fsreq);
325}
326
327static int afs_symlink_read_folio(struct file *file, struct folio *folio)
328{
329	struct afs_vnode *vnode = AFS_FS_I(folio->mapping->host);
330	struct afs_read *fsreq;
331	int ret;
332
333	fsreq = afs_alloc_read(GFP_NOFS);
334	if (!fsreq)
335		return -ENOMEM;
336
337	fsreq->pos	= folio_pos(folio);
338	fsreq->len	= folio_size(folio);
339	fsreq->vnode	= vnode;
340	fsreq->iter	= &fsreq->def_iter;
341	iov_iter_xarray(&fsreq->def_iter, ITER_DEST, &folio->mapping->i_pages,
342			fsreq->pos, fsreq->len);
343
344	ret = afs_fetch_data(fsreq->vnode, fsreq);
345	if (ret == 0)
346		folio_mark_uptodate(folio);
347	folio_unlock(folio);
348	return ret;
349}
350
351static int afs_init_request(struct netfs_io_request *rreq, struct file *file)
352{
353	if (file)
354		rreq->netfs_priv = key_get(afs_file_key(file));
355	rreq->rsize = 256 * 1024;
356	rreq->wsize = 256 * 1024 * 1024;
357	return 0;
358}
359
360static int afs_check_write_begin(struct file *file, loff_t pos, unsigned len,
361				 struct folio **foliop, void **_fsdata)
362{
363	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
364
365	return test_bit(AFS_VNODE_DELETED, &vnode->flags) ? -ESTALE : 0;
366}
367
368static void afs_free_request(struct netfs_io_request *rreq)
369{
370	key_put(rreq->netfs_priv);
371	afs_put_wb_key(rreq->netfs_priv2);
372}
373
374static void afs_update_i_size(struct inode *inode, loff_t new_i_size)
375{
376	struct afs_vnode *vnode = AFS_FS_I(inode);
377	loff_t i_size;
378
379	write_seqlock(&vnode->cb_lock);
380	i_size = i_size_read(&vnode->netfs.inode);
381	if (new_i_size > i_size) {
382		i_size_write(&vnode->netfs.inode, new_i_size);
383		inode_set_bytes(&vnode->netfs.inode, new_i_size);
384	}
385	write_sequnlock(&vnode->cb_lock);
386	fscache_update_cookie(afs_vnode_cache(vnode), NULL, &new_i_size);
387}
388
389static void afs_netfs_invalidate_cache(struct netfs_io_request *wreq)
390{
391	struct afs_vnode *vnode = AFS_FS_I(wreq->inode);
392
393	afs_invalidate_cache(vnode, 0);
394}
395
396const struct netfs_request_ops afs_req_ops = {
397	.init_request		= afs_init_request,
398	.free_request		= afs_free_request,
399	.check_write_begin	= afs_check_write_begin,
400	.issue_read		= afs_issue_read,
401	.update_i_size		= afs_update_i_size,
402	.invalidate_cache	= afs_netfs_invalidate_cache,
403	.begin_writeback	= afs_begin_writeback,
404	.prepare_write		= afs_prepare_write,
405	.issue_write		= afs_issue_write,
406};
407
408static void afs_add_open_mmap(struct afs_vnode *vnode)
409{
410	if (atomic_inc_return(&vnode->cb_nr_mmap) == 1) {
411		down_write(&vnode->volume->open_mmaps_lock);
412
413		if (list_empty(&vnode->cb_mmap_link))
414			list_add_tail(&vnode->cb_mmap_link, &vnode->volume->open_mmaps);
415
416		up_write(&vnode->volume->open_mmaps_lock);
417	}
418}
419
420static void afs_drop_open_mmap(struct afs_vnode *vnode)
421{
422	if (atomic_add_unless(&vnode->cb_nr_mmap, -1, 1))
423		return;
424
425	down_write(&vnode->volume->open_mmaps_lock);
426
427	read_seqlock_excl(&vnode->cb_lock);
428	// the only place where ->cb_nr_mmap may hit 0
429	// see __afs_break_callback() for the other side...
430	if (atomic_dec_and_test(&vnode->cb_nr_mmap))
431		list_del_init(&vnode->cb_mmap_link);
432	read_sequnlock_excl(&vnode->cb_lock);
433
434	up_write(&vnode->volume->open_mmaps_lock);
435	flush_work(&vnode->cb_work);
436}
437
438/*
439 * Handle setting up a memory mapping on an AFS file.
440 */
441static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
442{
443	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
444	int ret;
445
446	afs_add_open_mmap(vnode);
447
448	ret = generic_file_mmap(file, vma);
449	if (ret == 0)
450		vma->vm_ops = &afs_vm_ops;
451	else
452		afs_drop_open_mmap(vnode);
453	return ret;
454}
455
456static void afs_vm_open(struct vm_area_struct *vma)
457{
458	afs_add_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
459}
460
461static void afs_vm_close(struct vm_area_struct *vma)
462{
463	afs_drop_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
464}
465
466static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff)
467{
468	struct afs_vnode *vnode = AFS_FS_I(file_inode(vmf->vma->vm_file));
469
470	if (afs_check_validity(vnode))
471		return filemap_map_pages(vmf, start_pgoff, end_pgoff);
472	return 0;
473}
474
475static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
476{
477	struct inode *inode = file_inode(iocb->ki_filp);
478	struct afs_vnode *vnode = AFS_FS_I(inode);
479	struct afs_file *af = iocb->ki_filp->private_data;
480	ssize_t ret;
481
482	if (iocb->ki_flags & IOCB_DIRECT)
483		return netfs_unbuffered_read_iter(iocb, iter);
484
485	ret = netfs_start_io_read(inode);
486	if (ret < 0)
487		return ret;
488	ret = afs_validate(vnode, af->key);
489	if (ret == 0)
490		ret = filemap_read(iocb, iter, 0);
491	netfs_end_io_read(inode);
492	return ret;
493}
494
495static ssize_t afs_file_splice_read(struct file *in, loff_t *ppos,
496				    struct pipe_inode_info *pipe,
497				    size_t len, unsigned int flags)
498{
499	struct inode *inode = file_inode(in);
500	struct afs_vnode *vnode = AFS_FS_I(inode);
501	struct afs_file *af = in->private_data;
502	ssize_t ret;
503
504	ret = netfs_start_io_read(inode);
505	if (ret < 0)
506		return ret;
507	ret = afs_validate(vnode, af->key);
508	if (ret == 0)
509		ret = filemap_splice_read(in, ppos, pipe, len, flags);
510	netfs_end_io_read(inode);
511	return ret;
512}
513