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