fc15497608c6145877dbb933cd41b27de7e0509f
[linux-block.git] / fs / afs / file.c
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 <trace/events/netfs.h>
20 #include "internal.h"
21
22 static int afs_file_mmap(struct file *file, struct vm_area_struct *vma);
23
24 static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter);
25 static 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);
28 static void afs_vm_open(struct vm_area_struct *area);
29 static void afs_vm_close(struct vm_area_struct *area);
30 static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff);
31
32 const 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
46 const struct inode_operations afs_file_inode_operations = {
47         .getattr        = afs_getattr,
48         .setattr        = afs_setattr,
49         .permission     = afs_permission,
50 };
51
52 const 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
63 static const struct vm_operations_struct afs_vm_ops = {
64         .open           = afs_vm_open,
65         .close          = afs_vm_close,
66         .fault          = filemap_fault,
67         .map_pages      = afs_vm_map_pages,
68         .page_mkwrite   = afs_page_mkwrite,
69 };
70
71 /*
72  * Discard a pin on a writeback key.
73  */
74 void afs_put_wb_key(struct afs_wb_key *wbk)
75 {
76         if (wbk && refcount_dec_and_test(&wbk->usage)) {
77                 key_put(wbk->key);
78                 kfree(wbk);
79         }
80 }
81
82 /*
83  * Cache key for writeback.
84  */
85 int 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
107 found:
108         refcount_inc(&p->usage);
109         spin_unlock(&vnode->wb_lock);
110         af->wb = p;
111         kfree(wbk);
112         return 0;
113 }
114
115 /*
116  * open an AFS file or directory and attach a key to it
117  */
118 int afs_open(struct inode *inode, struct file *file)
119 {
120         struct afs_vnode *vnode = AFS_FS_I(inode);
121         struct afs_file *af;
122         struct key *key;
123         int ret;
124
125         _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
126
127         key = afs_request_key(vnode->volume->cell);
128         if (IS_ERR(key)) {
129                 ret = PTR_ERR(key);
130                 goto error;
131         }
132
133         af = kzalloc(sizeof(*af), GFP_KERNEL);
134         if (!af) {
135                 ret = -ENOMEM;
136                 goto error_key;
137         }
138         af->key = key;
139
140         ret = afs_validate(vnode, key);
141         if (ret < 0)
142                 goto error_af;
143
144         if (file->f_mode & FMODE_WRITE) {
145                 ret = afs_cache_wb_key(vnode, af);
146                 if (ret < 0)
147                         goto error_af;
148         }
149
150         if (file->f_flags & O_TRUNC)
151                 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
152
153         fscache_use_cookie(afs_vnode_cache(vnode), file->f_mode & FMODE_WRITE);
154
155         file->private_data = af;
156         _leave(" = 0");
157         return 0;
158
159 error_af:
160         kfree(af);
161 error_key:
162         key_put(key);
163 error:
164         _leave(" = %d", ret);
165         return ret;
166 }
167
168 /*
169  * release an AFS file or directory and discard its key
170  */
171 int afs_release(struct inode *inode, struct file *file)
172 {
173         struct afs_vnode_cache_aux aux;
174         struct afs_vnode *vnode = AFS_FS_I(inode);
175         struct afs_file *af = file->private_data;
176         loff_t i_size;
177         int ret = 0;
178
179         _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
180
181         if ((file->f_mode & FMODE_WRITE))
182                 ret = vfs_fsync(file, 0);
183
184         file->private_data = NULL;
185         if (af->wb)
186                 afs_put_wb_key(af->wb);
187
188         if ((file->f_mode & FMODE_WRITE)) {
189                 i_size = i_size_read(&vnode->netfs.inode);
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
196         key_put(af->key);
197         kfree(af);
198         afs_prune_wb_keys(vnode);
199         _leave(" = %d", ret);
200         return ret;
201 }
202
203 static void afs_fetch_data_notify(struct afs_operation *op)
204 {
205         struct netfs_io_subrequest *subreq = op->fetch.subreq;
206
207         subreq->error = afs_op_error(op);
208         netfs_read_subreq_terminated(subreq);
209 }
210
211 static 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);
216         afs_vnode_commit_status(op, &op->file[0]);
217         afs_stat_v(vnode, n_fetches);
218         atomic_long_add(op->fetch.subreq->transferred, &op->net->n_fetch_bytes);
219         afs_fetch_data_notify(op);
220 }
221
222 static 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
228 const struct afs_operation_ops afs_fetch_data_operation = {
229         .issue_afs_rpc  = afs_fs_fetch_data,
230         .issue_yfs_rpc  = yfs_fs_fetch_data,
231         .success        = afs_fetch_data_success,
232         .aborted        = afs_fetch_data_aborted,
233         .failed         = afs_fetch_data_notify,
234 };
235
236 static 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
247 static 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  */
267 static 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;
277         trace_afs_read_recv(op, call);
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
311 void 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
319 void 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
329 /*
330  * Fetch file data from the volume.
331  */
332 static void afs_issue_read(struct netfs_io_subrequest *subreq)
333 {
334         struct afs_operation *op;
335         struct afs_vnode *vnode = AFS_FS_I(subreq->rreq->inode);
336         struct key *key = subreq->rreq->netfs_priv;
337
338         _enter("%s{%llx:%llu.%u},%x,,,",
339                vnode->volume->name,
340                vnode->fid.vid,
341                vnode->fid.vnode,
342                vnode->fid.unique,
343                key_serial(key));
344
345         op = afs_alloc_operation(key, vnode->volume);
346         if (IS_ERR(op)) {
347                 subreq->error = PTR_ERR(op);
348                 netfs_read_subreq_terminated(subreq);
349                 return;
350         }
351
352         afs_op_set_vnode(op, 0, vnode);
353
354         op->fetch.subreq = subreq;
355         op->ops         = &afs_fetch_data_operation;
356
357         trace_netfs_sreq(subreq, netfs_sreq_trace_submit);
358
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         }
378 }
379
380 static int afs_init_request(struct netfs_io_request *rreq, struct file *file)
381 {
382         struct afs_vnode *vnode = AFS_FS_I(rreq->inode);
383
384         if (file)
385                 rreq->netfs_priv = key_get(afs_file_key(file));
386         rreq->rsize = 256 * 1024;
387         rreq->wsize = 256 * 1024 * 1024;
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;
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:
407         default:
408                 break;
409         }
410         return 0;
411 }
412
413 static int afs_check_write_begin(struct file *file, loff_t pos, unsigned len,
414                                  struct folio **foliop, void **_fsdata)
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
421 static void afs_free_request(struct netfs_io_request *rreq)
422 {
423         key_put(rreq->netfs_priv);
424         afs_put_wb_key(rreq->netfs_priv2);
425 }
426
427 static void afs_update_i_size(struct inode *inode, loff_t new_i_size)
428 {
429         struct afs_vnode *vnode = AFS_FS_I(inode);
430         loff_t i_size;
431
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);
440 }
441
442 static void afs_netfs_invalidate_cache(struct netfs_io_request *wreq)
443 {
444         struct afs_vnode *vnode = AFS_FS_I(wreq->inode);
445
446         afs_invalidate_cache(vnode, 0);
447 }
448
449 const struct netfs_request_ops afs_req_ops = {
450         .init_request           = afs_init_request,
451         .free_request           = afs_free_request,
452         .check_write_begin      = afs_check_write_begin,
453         .issue_read             = afs_issue_read,
454         .update_i_size          = afs_update_i_size,
455         .invalidate_cache       = afs_netfs_invalidate_cache,
456         .begin_writeback        = afs_begin_writeback,
457         .prepare_write          = afs_prepare_write,
458         .issue_write            = afs_issue_write,
459         .retry_request          = afs_retry_request,
460 };
461
462 static void afs_add_open_mmap(struct afs_vnode *vnode)
463 {
464         if (atomic_inc_return(&vnode->cb_nr_mmap) == 1) {
465                 down_write(&vnode->volume->open_mmaps_lock);
466
467                 if (list_empty(&vnode->cb_mmap_link))
468                         list_add_tail(&vnode->cb_mmap_link, &vnode->volume->open_mmaps);
469
470                 up_write(&vnode->volume->open_mmaps_lock);
471         }
472 }
473
474 static void afs_drop_open_mmap(struct afs_vnode *vnode)
475 {
476         if (atomic_add_unless(&vnode->cb_nr_mmap, -1, 1))
477                 return;
478
479         down_write(&vnode->volume->open_mmaps_lock);
480
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))
485                 list_del_init(&vnode->cb_mmap_link);
486         read_sequnlock_excl(&vnode->cb_lock);
487
488         up_write(&vnode->volume->open_mmaps_lock);
489         flush_work(&vnode->cb_work);
490 }
491
492 /*
493  * Handle setting up a memory mapping on an AFS file.
494  */
495 static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
496 {
497         struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
498         int ret;
499
500         afs_add_open_mmap(vnode);
501
502         ret = generic_file_mmap(file, vma);
503         if (ret == 0)
504                 vma->vm_ops = &afs_vm_ops;
505         else
506                 afs_drop_open_mmap(vnode);
507         return ret;
508 }
509
510 static 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
515 static 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
520 static 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));
523
524         if (afs_check_validity(vnode))
525                 return filemap_map_pages(vmf, start_pgoff, end_pgoff);
526         return 0;
527 }
528
529 static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
530 {
531         struct inode *inode = file_inode(iocb->ki_filp);
532         struct afs_vnode *vnode = AFS_FS_I(inode);
533         struct afs_file *af = iocb->ki_filp->private_data;
534         ssize_t ret;
535
536         if (iocb->ki_flags & IOCB_DIRECT)
537                 return netfs_unbuffered_read_iter(iocb, iter);
538
539         ret = netfs_start_io_read(inode);
540         if (ret < 0)
541                 return ret;
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;
547 }
548
549 static 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 {
553         struct inode *inode = file_inode(in);
554         struct afs_vnode *vnode = AFS_FS_I(inode);
555         struct afs_file *af = in->private_data;
556         ssize_t ret;
557
558         ret = netfs_start_io_read(inode);
559         if (ret < 0)
560                 return ret;
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;
566 }