Merge tag 'mm-hotfixes-stable-2025-07-11-16-16' of git://git.kernel.org/pub/scm/linux...
[linux-block.git] / fs / afs / file.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
08e0e7c8 2/* AFS filesystem file handling
1da177e4 3 *
08e0e7c8 4 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
1da177e4 5 * Written by David Howells (dhowells@redhat.com)
1da177e4
LT
6 */
7
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/init.h>
1da177e4
LT
11#include <linux/fs.h>
12#include <linux/pagemap.h>
31143d5d 13#include <linux/writeback.h>
5a0e3ad6 14#include <linux/gfp.h>
91b467e0 15#include <linux/task_io_accounting_ops.h>
f86196ea 16#include <linux/mm.h>
d7bdba1c 17#include <linux/swap.h>
5cbf0398 18#include <linux/netfs.h>
ee4cdf7b 19#include <trace/events/netfs.h>
1da177e4
LT
20#include "internal.h"
21
1cf7a151 22static int afs_file_mmap(struct file *file, struct vm_area_struct *vma);
1da177e4 23
3978d816 24static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter);
d96d96ee
DH
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);
6e0e99d5
DH
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);
9b3f26c9 31
00d3b7a4
DH
32const struct file_operations afs_file_operations = {
33 .open = afs_open,
34 .release = afs_release,
35 .llseek = generic_file_llseek,
3978d816 36 .read_iter = afs_file_read_iter,
3560358a 37 .write_iter = netfs_file_write_iter,
1cf7a151 38 .mmap = afs_file_mmap,
d96d96ee 39 .splice_read = afs_file_splice_read,
06a17bbe 40 .splice_write = iter_file_splice_write,
31143d5d 41 .fsync = afs_fsync,
e8d6c554
DH
42 .lock = afs_lock,
43 .flock = afs_flock,
00d3b7a4
DH
44};
45
754661f1 46const struct inode_operations afs_file_inode_operations = {
416351f2 47 .getattr = afs_getattr,
31143d5d 48 .setattr = afs_setattr,
00d3b7a4 49 .permission = afs_permission,
1da177e4
LT
50};
51
75bd228d 52const struct address_space_operations afs_file_aops = {
3560358a 53 .direct_IO = noop_direct_IO,
6c62371b 54 .read_folio = netfs_read_folio,
bc899ee1 55 .readahead = netfs_readahead,
c9c4ff12 56 .dirty_folio = netfs_dirty_folio,
c1ec4d7c
DH
57 .release_folio = netfs_release_folio,
58 .invalidate_folio = netfs_invalidate_folio,
a9eb558a 59 .migrate_folio = filemap_migrate_folio,
3560358a 60 .writepages = afs_writepages,
1da177e4
LT
61};
62
1cf7a151 63static const struct vm_operations_struct afs_vm_ops = {
6e0e99d5
DH
64 .open = afs_vm_open,
65 .close = afs_vm_close,
1cf7a151 66 .fault = filemap_fault,
6e0e99d5 67 .map_pages = afs_vm_map_pages,
1cf7a151
DH
68 .page_mkwrite = afs_page_mkwrite,
69};
70
4343d008
DH
71/*
72 * Discard a pin on a writeback key.
73 */
74void afs_put_wb_key(struct afs_wb_key *wbk)
75{
e49c7b2f 76 if (wbk && refcount_dec_and_test(&wbk->usage)) {
4343d008
DH
77 key_put(wbk->key);
78 kfree(wbk);
79 }
80}
81
82/*
83 * Cache key for writeback.
84 */
85int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af)
86{
87 struct afs_wb_key *wbk, *p;
88
89 wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL);
90 if (!wbk)
91 return -ENOMEM;
92 refcount_set(&wbk->usage, 2);
93 wbk->key = af->key;
94
95 spin_lock(&vnode->wb_lock);
96 list_for_each_entry(p, &vnode->wb_keys, vnode_link) {
97 if (p->key == wbk->key)
98 goto found;
99 }
100
101 key_get(wbk->key);
102 list_add_tail(&wbk->vnode_link, &vnode->wb_keys);
103 spin_unlock(&vnode->wb_lock);
104 af->wb = wbk;
105 return 0;
106
107found:
108 refcount_inc(&p->usage);
109 spin_unlock(&vnode->wb_lock);
110 af->wb = p;
111 kfree(wbk);
112 return 0;
113}
114
00d3b7a4
DH
115/*
116 * open an AFS file or directory and attach a key to it
117 */
118int afs_open(struct inode *inode, struct file *file)
119{
120 struct afs_vnode *vnode = AFS_FS_I(inode);
215804a9 121 struct afs_file *af;
00d3b7a4 122 struct key *key;
260a9803 123 int ret;
00d3b7a4 124
3b6492df 125 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
00d3b7a4
DH
126
127 key = afs_request_key(vnode->volume->cell);
128 if (IS_ERR(key)) {
215804a9
DH
129 ret = PTR_ERR(key);
130 goto error;
00d3b7a4
DH
131 }
132
215804a9
DH
133 af = kzalloc(sizeof(*af), GFP_KERNEL);
134 if (!af) {
135 ret = -ENOMEM;
136 goto error_key;
260a9803 137 }
4343d008 138 af->key = key;
260a9803 139
215804a9
DH
140 ret = afs_validate(vnode, key);
141 if (ret < 0)
142 goto error_af;
143
4343d008
DH
144 if (file->f_mode & FMODE_WRITE) {
145 ret = afs_cache_wb_key(vnode, af);
146 if (ret < 0)
147 goto error_af;
148 }
5a813276
DH
149
150 if (file->f_flags & O_TRUNC)
151 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
523d27cd
DH
152
153 fscache_use_cookie(afs_vnode_cache(vnode), file->f_mode & FMODE_WRITE);
154
215804a9 155 file->private_data = af;
00d3b7a4
DH
156 _leave(" = 0");
157 return 0;
215804a9
DH
158
159error_af:
160 kfree(af);
161error_key:
162 key_put(key);
163error:
164 _leave(" = %d", ret);
165 return ret;
00d3b7a4
DH
166}
167
168/*
169 * release an AFS file or directory and discard its key
170 */
171int afs_release(struct inode *inode, struct file *file)
172{
523d27cd 173 struct afs_vnode_cache_aux aux;
00d3b7a4 174 struct afs_vnode *vnode = AFS_FS_I(inode);
215804a9 175 struct afs_file *af = file->private_data;
523d27cd 176 loff_t i_size;
a1b879ee 177 int ret = 0;
00d3b7a4 178
3b6492df 179 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
00d3b7a4 180
5a813276 181 if ((file->f_mode & FMODE_WRITE))
a1b879ee 182 ret = vfs_fsync(file, 0);
5a813276 183
215804a9 184 file->private_data = NULL;
4343d008
DH
185 if (af->wb)
186 afs_put_wb_key(af->wb);
523d27cd
DH
187
188 if ((file->f_mode & FMODE_WRITE)) {
874c8ca1 189 i_size = i_size_read(&vnode->netfs.inode);
523d27cd
DH
190 afs_set_cache_aux(vnode, &aux);
191 fscache_unuse_cookie(afs_vnode_cache(vnode), &aux, &i_size);
192 } else {
193 fscache_unuse_cookie(afs_vnode_cache(vnode), NULL, NULL);
194 }
195
215804a9
DH
196 key_put(af->key);
197 kfree(af);
4343d008 198 afs_prune_wb_keys(vnode);
a1b879ee
DH
199 _leave(" = %d", ret);
200 return ret;
00d3b7a4
DH
201}
202
dc419184
DH
203static void afs_fetch_data_notify(struct afs_operation *op)
204{
f28fc201
DH
205 struct netfs_io_subrequest *subreq = op->fetch.subreq;
206
207 subreq->error = afs_op_error(op);
208 netfs_read_subreq_terminated(subreq);
dc419184
DH
209}
210
e49c7b2f
DH
211static void afs_fetch_data_success(struct afs_operation *op)
212{
213 struct afs_vnode *vnode = op->file[0].vnode;
214
215 _enter("op=%08x", op->debug_id);
e49c7b2f
DH
216 afs_vnode_commit_status(op, &op->file[0]);
217 afs_stat_v(vnode, n_fetches);
f28fc201 218 atomic_long_add(op->fetch.subreq->transferred, &op->net->n_fetch_bytes);
dc419184 219 afs_fetch_data_notify(op);
e49c7b2f
DH
220}
221
ee4cdf7b
DH
222static void afs_fetch_data_aborted(struct afs_operation *op)
223{
224 afs_check_for_remote_deletion(op);
225 afs_fetch_data_notify(op);
226}
227
f28fc201 228const struct afs_operation_ops afs_fetch_data_operation = {
e49c7b2f
DH
229 .issue_afs_rpc = afs_fs_fetch_data,
230 .issue_yfs_rpc = yfs_fs_fetch_data,
231 .success = afs_fetch_data_success,
ee4cdf7b 232 .aborted = afs_fetch_data_aborted,
dc419184 233 .failed = afs_fetch_data_notify,
e49c7b2f
DH
234};
235
eddf51f2
DH
236static void afs_issue_read_call(struct afs_operation *op)
237{
238 op->call_responded = false;
239 op->call_error = 0;
240 op->call_abort_code = 0;
241 if (test_bit(AFS_SERVER_FL_IS_YFS, &op->server->flags))
242 yfs_fs_fetch_data(op);
243 else
244 afs_fs_fetch_data(op);
245}
246
247static void afs_end_read(struct afs_operation *op)
248{
249 if (op->call_responded && op->server)
250 set_bit(AFS_SERVER_FL_RESPONDING, &op->server->flags);
251
252 if (!afs_op_error(op))
253 afs_fetch_data_success(op);
254 else if (op->cumul_error.aborted)
255 afs_fetch_data_aborted(op);
256 else
257 afs_fetch_data_notify(op);
258
259 afs_end_vnode_operation(op);
260 afs_put_operation(op);
261}
262
263/*
264 * Perform I/O processing on an asynchronous call. The work item carries a ref
265 * to the call struct that we either need to release or to pass on.
266 */
267static void afs_read_receive(struct afs_call *call)
268{
269 struct afs_operation *op = call->op;
270 enum afs_call_state state;
271
272 _enter("");
273
274 state = READ_ONCE(call->state);
275 if (state == AFS_CALL_COMPLETE)
276 return;
3c49e529 277 trace_afs_read_recv(op, call);
eddf51f2
DH
278
279 while (state < AFS_CALL_COMPLETE && READ_ONCE(call->need_attention)) {
280 WRITE_ONCE(call->need_attention, false);
281 afs_deliver_to_call(call);
282 state = READ_ONCE(call->state);
283 }
284
285 if (state < AFS_CALL_COMPLETE) {
286 netfs_read_subreq_progress(op->fetch.subreq);
287 if (rxrpc_kernel_check_life(call->net->socket, call->rxcall))
288 return;
289 /* rxrpc terminated the call. */
290 afs_set_call_complete(call, call->error, call->abort_code);
291 }
292
293 op->call_abort_code = call->abort_code;
294 op->call_error = call->error;
295 op->call_responded = call->responded;
296 op->call = NULL;
297 call->op = NULL;
298 afs_put_call(call);
299
300 /* If the call failed, then we need to crank the server rotation
301 * handle and try the next.
302 */
303 if (afs_select_fileserver(op)) {
304 afs_issue_read_call(op);
305 return;
306 }
307
308 afs_end_read(op);
309}
310
311void afs_fetch_data_async_rx(struct work_struct *work)
312{
313 struct afs_call *call = container_of(work, struct afs_call, async_work);
314
315 afs_read_receive(call);
316 afs_put_call(call);
317}
318
319void afs_fetch_data_immediate_cancel(struct afs_call *call)
320{
321 if (call->async) {
322 afs_get_call(call, afs_call_trace_wake);
323 if (!queue_work(afs_async_calls, &call->async_work))
324 afs_deferred_put_call(call);
325 flush_work(&call->async_work);
326 }
327}
328
d2ddc776
DH
329/*
330 * Fetch file data from the volume.
331 */
eddf51f2 332static void afs_issue_read(struct netfs_io_subrequest *subreq)
d2ddc776 333{
e49c7b2f 334 struct afs_operation *op;
f28fc201
DH
335 struct afs_vnode *vnode = AFS_FS_I(subreq->rreq->inode);
336 struct key *key = subreq->rreq->netfs_priv;
d2ddc776 337
3b6492df 338 _enter("%s{%llx:%llu.%u},%x,,,",
d2ddc776
DH
339 vnode->volume->name,
340 vnode->fid.vid,
341 vnode->fid.vnode,
342 vnode->fid.unique,
f28fc201 343 key_serial(key));
d2ddc776 344
f28fc201 345 op = afs_alloc_operation(key, vnode->volume);
5cbf0398 346 if (IS_ERR(op)) {
f28fc201
DH
347 subreq->error = PTR_ERR(op);
348 netfs_read_subreq_terminated(subreq);
349 return;
5cbf0398 350 }
a58823ac 351
e49c7b2f 352 afs_op_set_vnode(op, 0, vnode);
d2ddc776 353
f28fc201 354 op->fetch.subreq = subreq;
e49c7b2f 355 op->ops = &afs_fetch_data_operation;
c4508464 356
ee4cdf7b 357 trace_netfs_sreq(subreq, netfs_sreq_trace_submit);
1da177e4 358
eddf51f2
DH
359 if (subreq->rreq->origin == NETFS_READAHEAD ||
360 subreq->rreq->iocb) {
361 op->flags |= AFS_OPERATION_ASYNC;
362
363 if (!afs_begin_vnode_operation(op)) {
364 subreq->error = afs_put_operation(op);
365 netfs_read_subreq_terminated(subreq);
366 return;
367 }
368
369 if (!afs_select_fileserver(op)) {
370 afs_end_read(op);
371 return;
372 }
373
374 afs_issue_read_call(op);
375 } else {
376 afs_do_sync_operation(op);
377 }
2e45b922
DH
378}
379
2de16041 380static int afs_init_request(struct netfs_io_request *rreq, struct file *file)
5cbf0398 381{
b2604315
DH
382 struct afs_vnode *vnode = AFS_FS_I(rreq->inode);
383
3560358a
DH
384 if (file)
385 rreq->netfs_priv = key_get(afs_file_key(file));
386 rreq->rsize = 256 * 1024;
2df86547 387 rreq->wsize = 256 * 1024 * 1024;
b2604315
DH
388
389 switch (rreq->origin) {
390 case NETFS_READ_SINGLE:
391 if (!file) {
392 struct key *key = afs_request_key(vnode->volume->cell);
393
394 if (IS_ERR(key))
395 return PTR_ERR(key);
396 rreq->netfs_priv = key;
397 }
398 break;
6dd80936
DH
399 case NETFS_WRITEBACK:
400 case NETFS_WRITETHROUGH:
401 case NETFS_UNBUFFERED_WRITE:
402 case NETFS_DIO_WRITE:
403 if (S_ISREG(rreq->inode->i_mode))
404 rreq->io_streams[0].avail = true;
405 break;
406 case NETFS_WRITEBACK_SINGLE:
b2604315
DH
407 default:
408 break;
409 }
2de16041 410 return 0;
5cbf0398 411}
91b467e0 412
3003bbd0 413static int afs_check_write_begin(struct file *file, loff_t pos, unsigned len,
fac47b43 414 struct folio **foliop, void **_fsdata)
3003bbd0
DH
415{
416 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
417
418 return test_bit(AFS_VNODE_DELETED, &vnode->flags) ? -ESTALE : 0;
419}
420
40a81101 421static void afs_free_request(struct netfs_io_request *rreq)
1da177e4 422{
40a81101 423 key_put(rreq->netfs_priv);
1ecb146f 424 afs_put_wb_key(rreq->netfs_priv2);
5cbf0398 425}
f6d335c0 426
3560358a 427static void afs_update_i_size(struct inode *inode, loff_t new_i_size)
c7f75ef3 428{
3560358a
DH
429 struct afs_vnode *vnode = AFS_FS_I(inode);
430 loff_t i_size;
f86726a6 431
3560358a
DH
432 write_seqlock(&vnode->cb_lock);
433 i_size = i_size_read(&vnode->netfs.inode);
434 if (new_i_size > i_size) {
435 i_size_write(&vnode->netfs.inode, new_i_size);
436 inode_set_bytes(&vnode->netfs.inode, new_i_size);
437 }
438 write_sequnlock(&vnode->cb_lock);
439 fscache_update_cookie(afs_vnode_cache(vnode), NULL, &new_i_size);
f86726a6
DH
440}
441
3560358a 442static void afs_netfs_invalidate_cache(struct netfs_io_request *wreq)
9b3f26c9 443{
3560358a 444 struct afs_vnode *vnode = AFS_FS_I(wreq->inode);
9b3f26c9 445
3560358a 446 afs_invalidate_cache(vnode, 0);
9b3f26c9
DH
447}
448
6a19114b
DH
449const struct netfs_request_ops afs_req_ops = {
450 .init_request = afs_init_request,
40a81101 451 .free_request = afs_free_request,
3003bbd0 452 .check_write_begin = afs_check_write_begin,
f18a3785 453 .issue_read = afs_issue_read,
3560358a
DH
454 .update_i_size = afs_update_i_size,
455 .invalidate_cache = afs_netfs_invalidate_cache,
ed22e1db
DH
456 .begin_writeback = afs_begin_writeback,
457 .prepare_write = afs_prepare_write,
458 .issue_write = afs_issue_write,
2cf36327 459 .retry_request = afs_retry_request,
5cbf0398 460};
1cf7a151 461
6e0e99d5
DH
462static void afs_add_open_mmap(struct afs_vnode *vnode)
463{
464 if (atomic_inc_return(&vnode->cb_nr_mmap) == 1) {
453924de 465 down_write(&vnode->volume->open_mmaps_lock);
6e0e99d5 466
1744a22a 467 if (list_empty(&vnode->cb_mmap_link))
453924de 468 list_add_tail(&vnode->cb_mmap_link, &vnode->volume->open_mmaps);
6e0e99d5 469
453924de 470 up_write(&vnode->volume->open_mmaps_lock);
6e0e99d5
DH
471 }
472}
473
474static void afs_drop_open_mmap(struct afs_vnode *vnode)
475{
275655d3 476 if (atomic_add_unless(&vnode->cb_nr_mmap, -1, 1))
6e0e99d5
DH
477 return;
478
453924de 479 down_write(&vnode->volume->open_mmaps_lock);
6e0e99d5 480
275655d3
AV
481 read_seqlock_excl(&vnode->cb_lock);
482 // the only place where ->cb_nr_mmap may hit 0
483 // see __afs_break_callback() for the other side...
484 if (atomic_dec_and_test(&vnode->cb_nr_mmap))
6e0e99d5 485 list_del_init(&vnode->cb_mmap_link);
275655d3 486 read_sequnlock_excl(&vnode->cb_lock);
6e0e99d5 487
453924de 488 up_write(&vnode->volume->open_mmaps_lock);
6e0e99d5
DH
489 flush_work(&vnode->cb_work);
490}
491
1cf7a151
DH
492/*
493 * Handle setting up a memory mapping on an AFS file.
494 */
495static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
496{
6e0e99d5 497 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
1cf7a151
DH
498 int ret;
499
6e0e99d5
DH
500 afs_add_open_mmap(vnode);
501
1cf7a151
DH
502 ret = generic_file_mmap(file, vma);
503 if (ret == 0)
504 vma->vm_ops = &afs_vm_ops;
6e0e99d5
DH
505 else
506 afs_drop_open_mmap(vnode);
1cf7a151
DH
507 return ret;
508}
3978d816 509
6e0e99d5
DH
510static void afs_vm_open(struct vm_area_struct *vma)
511{
512 afs_add_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
513}
514
515static void afs_vm_close(struct vm_area_struct *vma)
516{
517 afs_drop_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
518}
519
520static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff)
521{
522 struct afs_vnode *vnode = AFS_FS_I(file_inode(vmf->vma->vm_file));
6e0e99d5 523
453924de 524 if (afs_check_validity(vnode))
6e0e99d5 525 return filemap_map_pages(vmf, start_pgoff, end_pgoff);
0050d7f5 526 return 0;
6e0e99d5
DH
527}
528
3978d816
DH
529static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
530{
3560358a
DH
531 struct inode *inode = file_inode(iocb->ki_filp);
532 struct afs_vnode *vnode = AFS_FS_I(inode);
3978d816 533 struct afs_file *af = iocb->ki_filp->private_data;
3560358a 534 ssize_t ret;
3978d816 535
3560358a
DH
536 if (iocb->ki_flags & IOCB_DIRECT)
537 return netfs_unbuffered_read_iter(iocb, iter);
538
539 ret = netfs_start_io_read(inode);
3978d816
DH
540 if (ret < 0)
541 return ret;
3560358a
DH
542 ret = afs_validate(vnode, af->key);
543 if (ret == 0)
544 ret = filemap_read(iocb, iter, 0);
545 netfs_end_io_read(inode);
546 return ret;
3978d816 547}
d96d96ee
DH
548
549static ssize_t afs_file_splice_read(struct file *in, loff_t *ppos,
550 struct pipe_inode_info *pipe,
551 size_t len, unsigned int flags)
552{
3560358a
DH
553 struct inode *inode = file_inode(in);
554 struct afs_vnode *vnode = AFS_FS_I(inode);
d96d96ee 555 struct afs_file *af = in->private_data;
3560358a 556 ssize_t ret;
d96d96ee 557
3560358a 558 ret = netfs_start_io_read(inode);
d96d96ee
DH
559 if (ret < 0)
560 return ret;
3560358a
DH
561 ret = afs_validate(vnode, af->key);
562 if (ret == 0)
563 ret = filemap_splice_read(in, ppos, pipe, len, flags);
564 netfs_end_io_read(inode);
565 return ret;
d96d96ee 566}