xref: /linux/fs/afs/file.c (revision e0484344c0413e1fcd5642b77d49c7648fb194ec)
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/netfs.h>
18 #include "internal.h"
19 
20 static int afs_file_mmap(struct file *file, struct vm_area_struct *vma);
21 static int afs_readpage(struct file *file, struct page *page);
22 static int afs_symlink_readpage(struct file *file, struct page *page);
23 static void afs_invalidatepage(struct page *page, unsigned int offset,
24 			       unsigned int length);
25 static int afs_releasepage(struct page *page, gfp_t gfp_flags);
26 
27 static void afs_readahead(struct readahead_control *ractl);
28 static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter);
29 static void afs_vm_open(struct vm_area_struct *area);
30 static void afs_vm_close(struct vm_area_struct *area);
31 static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff);
32 
33 const struct file_operations afs_file_operations = {
34 	.open		= afs_open,
35 	.release	= afs_release,
36 	.llseek		= generic_file_llseek,
37 	.read_iter	= afs_file_read_iter,
38 	.write_iter	= afs_file_write,
39 	.mmap		= afs_file_mmap,
40 	.splice_read	= generic_file_splice_read,
41 	.splice_write	= iter_file_splice_write,
42 	.fsync		= afs_fsync,
43 	.lock		= afs_lock,
44 	.flock		= afs_flock,
45 };
46 
47 const struct inode_operations afs_file_inode_operations = {
48 	.getattr	= afs_getattr,
49 	.setattr	= afs_setattr,
50 	.permission	= afs_permission,
51 };
52 
53 const struct address_space_operations afs_file_aops = {
54 	.readpage	= afs_readpage,
55 	.readahead	= afs_readahead,
56 	.set_page_dirty	= afs_set_page_dirty,
57 	.launder_page	= afs_launder_page,
58 	.releasepage	= afs_releasepage,
59 	.invalidatepage	= afs_invalidatepage,
60 	.write_begin	= afs_write_begin,
61 	.write_end	= afs_write_end,
62 	.writepage	= afs_writepage,
63 	.writepages	= afs_writepages,
64 };
65 
66 const struct address_space_operations afs_symlink_aops = {
67 	.readpage	= afs_symlink_readpage,
68 	.releasepage	= afs_releasepage,
69 	.invalidatepage	= afs_invalidatepage,
70 };
71 
72 static const struct vm_operations_struct afs_vm_ops = {
73 	.open		= afs_vm_open,
74 	.close		= afs_vm_close,
75 	.fault		= filemap_fault,
76 	.map_pages	= afs_vm_map_pages,
77 	.page_mkwrite	= afs_page_mkwrite,
78 };
79 
80 /*
81  * Discard a pin on a writeback key.
82  */
83 void afs_put_wb_key(struct afs_wb_key *wbk)
84 {
85 	if (wbk && refcount_dec_and_test(&wbk->usage)) {
86 		key_put(wbk->key);
87 		kfree(wbk);
88 	}
89 }
90 
91 /*
92  * Cache key for writeback.
93  */
94 int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af)
95 {
96 	struct afs_wb_key *wbk, *p;
97 
98 	wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL);
99 	if (!wbk)
100 		return -ENOMEM;
101 	refcount_set(&wbk->usage, 2);
102 	wbk->key = af->key;
103 
104 	spin_lock(&vnode->wb_lock);
105 	list_for_each_entry(p, &vnode->wb_keys, vnode_link) {
106 		if (p->key == wbk->key)
107 			goto found;
108 	}
109 
110 	key_get(wbk->key);
111 	list_add_tail(&wbk->vnode_link, &vnode->wb_keys);
112 	spin_unlock(&vnode->wb_lock);
113 	af->wb = wbk;
114 	return 0;
115 
116 found:
117 	refcount_inc(&p->usage);
118 	spin_unlock(&vnode->wb_lock);
119 	af->wb = p;
120 	kfree(wbk);
121 	return 0;
122 }
123 
124 /*
125  * open an AFS file or directory and attach a key to it
126  */
127 int afs_open(struct inode *inode, struct file *file)
128 {
129 	struct afs_vnode *vnode = AFS_FS_I(inode);
130 	struct afs_file *af;
131 	struct key *key;
132 	int ret;
133 
134 	_enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
135 
136 	key = afs_request_key(vnode->volume->cell);
137 	if (IS_ERR(key)) {
138 		ret = PTR_ERR(key);
139 		goto error;
140 	}
141 
142 	af = kzalloc(sizeof(*af), GFP_KERNEL);
143 	if (!af) {
144 		ret = -ENOMEM;
145 		goto error_key;
146 	}
147 	af->key = key;
148 
149 	ret = afs_validate(vnode, key);
150 	if (ret < 0)
151 		goto error_af;
152 
153 	if (file->f_mode & FMODE_WRITE) {
154 		ret = afs_cache_wb_key(vnode, af);
155 		if (ret < 0)
156 			goto error_af;
157 	}
158 
159 	if (file->f_flags & O_TRUNC)
160 		set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
161 
162 	fscache_use_cookie(afs_vnode_cache(vnode), file->f_mode & FMODE_WRITE);
163 
164 	file->private_data = af;
165 	_leave(" = 0");
166 	return 0;
167 
168 error_af:
169 	kfree(af);
170 error_key:
171 	key_put(key);
172 error:
173 	_leave(" = %d", ret);
174 	return ret;
175 }
176 
177 /*
178  * release an AFS file or directory and discard its key
179  */
180 int afs_release(struct inode *inode, struct file *file)
181 {
182 	struct afs_vnode_cache_aux aux;
183 	struct afs_vnode *vnode = AFS_FS_I(inode);
184 	struct afs_file *af = file->private_data;
185 	loff_t i_size;
186 	int ret = 0;
187 
188 	_enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
189 
190 	if ((file->f_mode & FMODE_WRITE))
191 		ret = vfs_fsync(file, 0);
192 
193 	file->private_data = NULL;
194 	if (af->wb)
195 		afs_put_wb_key(af->wb);
196 
197 	if ((file->f_mode & FMODE_WRITE)) {
198 		i_size = i_size_read(&vnode->vfs_inode);
199 		afs_set_cache_aux(vnode, &aux);
200 		fscache_unuse_cookie(afs_vnode_cache(vnode), &aux, &i_size);
201 	} else {
202 		fscache_unuse_cookie(afs_vnode_cache(vnode), NULL, NULL);
203 	}
204 
205 	key_put(af->key);
206 	kfree(af);
207 	afs_prune_wb_keys(vnode);
208 	_leave(" = %d", ret);
209 	return ret;
210 }
211 
212 /*
213  * Allocate a new read record.
214  */
215 struct afs_read *afs_alloc_read(gfp_t gfp)
216 {
217 	struct afs_read *req;
218 
219 	req = kzalloc(sizeof(struct afs_read), gfp);
220 	if (req)
221 		refcount_set(&req->usage, 1);
222 
223 	return req;
224 }
225 
226 /*
227  * Dispose of a ref to a read record.
228  */
229 void afs_put_read(struct afs_read *req)
230 {
231 	if (refcount_dec_and_test(&req->usage)) {
232 		if (req->cleanup)
233 			req->cleanup(req);
234 		key_put(req->key);
235 		kfree(req);
236 	}
237 }
238 
239 static void afs_fetch_data_notify(struct afs_operation *op)
240 {
241 	struct afs_read *req = op->fetch.req;
242 	struct netfs_read_subrequest *subreq = req->subreq;
243 	int error = op->error;
244 
245 	if (error == -ECONNABORTED)
246 		error = afs_abort_to_error(op->ac.abort_code);
247 	req->error = error;
248 
249 	if (subreq) {
250 		__set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
251 		netfs_subreq_terminated(subreq, error ?: req->actual_len, false);
252 		req->subreq = NULL;
253 	} else if (req->done) {
254 		req->done(req);
255 	}
256 }
257 
258 static void afs_fetch_data_success(struct afs_operation *op)
259 {
260 	struct afs_vnode *vnode = op->file[0].vnode;
261 
262 	_enter("op=%08x", op->debug_id);
263 	afs_vnode_commit_status(op, &op->file[0]);
264 	afs_stat_v(vnode, n_fetches);
265 	atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes);
266 	afs_fetch_data_notify(op);
267 }
268 
269 static void afs_fetch_data_put(struct afs_operation *op)
270 {
271 	op->fetch.req->error = op->error;
272 	afs_put_read(op->fetch.req);
273 }
274 
275 static const struct afs_operation_ops afs_fetch_data_operation = {
276 	.issue_afs_rpc	= afs_fs_fetch_data,
277 	.issue_yfs_rpc	= yfs_fs_fetch_data,
278 	.success	= afs_fetch_data_success,
279 	.aborted	= afs_check_for_remote_deletion,
280 	.failed		= afs_fetch_data_notify,
281 	.put		= afs_fetch_data_put,
282 };
283 
284 /*
285  * Fetch file data from the volume.
286  */
287 int afs_fetch_data(struct afs_vnode *vnode, struct afs_read *req)
288 {
289 	struct afs_operation *op;
290 
291 	_enter("%s{%llx:%llu.%u},%x,,,",
292 	       vnode->volume->name,
293 	       vnode->fid.vid,
294 	       vnode->fid.vnode,
295 	       vnode->fid.unique,
296 	       key_serial(req->key));
297 
298 	op = afs_alloc_operation(req->key, vnode->volume);
299 	if (IS_ERR(op)) {
300 		if (req->subreq)
301 			netfs_subreq_terminated(req->subreq, PTR_ERR(op), false);
302 		return PTR_ERR(op);
303 	}
304 
305 	afs_op_set_vnode(op, 0, vnode);
306 
307 	op->fetch.req	= afs_get_read(req);
308 	op->ops		= &afs_fetch_data_operation;
309 	return afs_do_sync_operation(op);
310 }
311 
312 static void afs_req_issue_op(struct netfs_read_subrequest *subreq)
313 {
314 	struct afs_vnode *vnode = AFS_FS_I(subreq->rreq->inode);
315 	struct afs_read *fsreq;
316 
317 	fsreq = afs_alloc_read(GFP_NOFS);
318 	if (!fsreq)
319 		return netfs_subreq_terminated(subreq, -ENOMEM, false);
320 
321 	fsreq->subreq	= subreq;
322 	fsreq->pos	= subreq->start + subreq->transferred;
323 	fsreq->len	= subreq->len   - subreq->transferred;
324 	fsreq->key	= key_get(subreq->rreq->netfs_priv);
325 	fsreq->vnode	= vnode;
326 	fsreq->iter	= &fsreq->def_iter;
327 
328 	iov_iter_xarray(&fsreq->def_iter, READ,
329 			&fsreq->vnode->vfs_inode.i_mapping->i_pages,
330 			fsreq->pos, fsreq->len);
331 
332 	afs_fetch_data(fsreq->vnode, fsreq);
333 	afs_put_read(fsreq);
334 }
335 
336 static int afs_symlink_readpage(struct file *file, struct page *page)
337 {
338 	struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
339 	struct afs_read *fsreq;
340 	struct folio *folio = page_folio(page);
341 	int ret;
342 
343 	fsreq = afs_alloc_read(GFP_NOFS);
344 	if (!fsreq)
345 		return -ENOMEM;
346 
347 	fsreq->pos	= folio_pos(folio);
348 	fsreq->len	= folio_size(folio);
349 	fsreq->vnode	= vnode;
350 	fsreq->iter	= &fsreq->def_iter;
351 	iov_iter_xarray(&fsreq->def_iter, READ, &page->mapping->i_pages,
352 			fsreq->pos, fsreq->len);
353 
354 	ret = afs_fetch_data(fsreq->vnode, fsreq);
355 	if (ret == 0)
356 		SetPageUptodate(page);
357 	unlock_page(page);
358 	return ret;
359 }
360 
361 static void afs_init_rreq(struct netfs_read_request *rreq, struct file *file)
362 {
363 	rreq->netfs_priv = key_get(afs_file_key(file));
364 }
365 
366 static bool afs_is_cache_enabled(struct inode *inode)
367 {
368 	struct fscache_cookie *cookie = afs_vnode_cache(AFS_FS_I(inode));
369 
370 	return fscache_cookie_enabled(cookie) && cookie->cache_priv;
371 }
372 
373 static int afs_begin_cache_operation(struct netfs_read_request *rreq)
374 {
375 #ifdef CONFIG_AFS_FSCACHE
376 	struct afs_vnode *vnode = AFS_FS_I(rreq->inode);
377 
378 	return fscache_begin_read_operation(&rreq->cache_resources,
379 					    afs_vnode_cache(vnode));
380 #else
381 	return -ENOBUFS;
382 #endif
383 }
384 
385 static int afs_check_write_begin(struct file *file, loff_t pos, unsigned len,
386 				 struct folio *folio, void **_fsdata)
387 {
388 	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
389 
390 	return test_bit(AFS_VNODE_DELETED, &vnode->flags) ? -ESTALE : 0;
391 }
392 
393 static void afs_priv_cleanup(struct address_space *mapping, void *netfs_priv)
394 {
395 	key_put(netfs_priv);
396 }
397 
398 const struct netfs_read_request_ops afs_req_ops = {
399 	.init_rreq		= afs_init_rreq,
400 	.is_cache_enabled	= afs_is_cache_enabled,
401 	.begin_cache_operation	= afs_begin_cache_operation,
402 	.check_write_begin	= afs_check_write_begin,
403 	.issue_op		= afs_req_issue_op,
404 	.cleanup		= afs_priv_cleanup,
405 };
406 
407 static int afs_readpage(struct file *file, struct page *page)
408 {
409 	struct folio *folio = page_folio(page);
410 
411 	return netfs_readpage(file, folio, &afs_req_ops, NULL);
412 }
413 
414 static void afs_readahead(struct readahead_control *ractl)
415 {
416 	netfs_readahead(ractl, &afs_req_ops, NULL);
417 }
418 
419 int afs_write_inode(struct inode *inode, struct writeback_control *wbc)
420 {
421 	fscache_unpin_writeback(wbc, afs_vnode_cache(AFS_FS_I(inode)));
422 	return 0;
423 }
424 
425 /*
426  * Adjust the dirty region of the page on truncation or full invalidation,
427  * getting rid of the markers altogether if the region is entirely invalidated.
428  */
429 static void afs_invalidate_dirty(struct folio *folio, unsigned int offset,
430 				 unsigned int length)
431 {
432 	struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio));
433 	unsigned long priv;
434 	unsigned int f, t, end = offset + length;
435 
436 	priv = (unsigned long)folio_get_private(folio);
437 
438 	/* we clean up only if the entire page is being invalidated */
439 	if (offset == 0 && length == folio_size(folio))
440 		goto full_invalidate;
441 
442 	 /* If the page was dirtied by page_mkwrite(), the PTE stays writable
443 	  * and we don't get another notification to tell us to expand it
444 	  * again.
445 	  */
446 	if (afs_is_folio_dirty_mmapped(priv))
447 		return;
448 
449 	/* We may need to shorten the dirty region */
450 	f = afs_folio_dirty_from(folio, priv);
451 	t = afs_folio_dirty_to(folio, priv);
452 
453 	if (t <= offset || f >= end)
454 		return; /* Doesn't overlap */
455 
456 	if (f < offset && t > end)
457 		return; /* Splits the dirty region - just absorb it */
458 
459 	if (f >= offset && t <= end)
460 		goto undirty;
461 
462 	if (f < offset)
463 		t = offset;
464 	else
465 		f = end;
466 	if (f == t)
467 		goto undirty;
468 
469 	priv = afs_folio_dirty(folio, f, t);
470 	folio_change_private(folio, (void *)priv);
471 	trace_afs_folio_dirty(vnode, tracepoint_string("trunc"), folio);
472 	return;
473 
474 undirty:
475 	trace_afs_folio_dirty(vnode, tracepoint_string("undirty"), folio);
476 	folio_clear_dirty_for_io(folio);
477 full_invalidate:
478 	trace_afs_folio_dirty(vnode, tracepoint_string("inval"), folio);
479 	folio_detach_private(folio);
480 }
481 
482 /*
483  * invalidate part or all of a page
484  * - release a page and clean up its private data if offset is 0 (indicating
485  *   the entire page)
486  */
487 static void afs_invalidatepage(struct page *page, unsigned int offset,
488 			       unsigned int length)
489 {
490 	struct folio *folio = page_folio(page);
491 
492 	_enter("{%lu},%u,%u", folio_index(folio), offset, length);
493 
494 	BUG_ON(!PageLocked(page));
495 
496 	if (PagePrivate(page))
497 		afs_invalidate_dirty(folio, offset, length);
498 
499 	folio_wait_fscache(folio);
500 	_leave("");
501 }
502 
503 /*
504  * release a page and clean up its private state if it's not busy
505  * - return true if the page can now be released, false if not
506  */
507 static int afs_releasepage(struct page *page, gfp_t gfp)
508 {
509 	struct folio *folio = page_folio(page);
510 	struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio));
511 
512 	_enter("{{%llx:%llu}[%lu],%lx},%x",
513 	       vnode->fid.vid, vnode->fid.vnode, folio_index(folio), folio->flags,
514 	       gfp);
515 
516 	/* deny if page is being written to the cache and the caller hasn't
517 	 * elected to wait */
518 #ifdef CONFIG_AFS_FSCACHE
519 	if (folio_test_fscache(folio)) {
520 		if (!gfpflags_allow_blocking(gfp) || !(gfp & __GFP_FS))
521 			return false;
522 		folio_wait_fscache(folio);
523 	}
524 	fscache_note_page_release(afs_vnode_cache(vnode));
525 #endif
526 
527 	if (folio_test_private(folio)) {
528 		trace_afs_folio_dirty(vnode, tracepoint_string("rel"), folio);
529 		folio_detach_private(folio);
530 	}
531 
532 	/* Indicate that the folio can be released */
533 	_leave(" = T");
534 	return true;
535 }
536 
537 static void afs_add_open_mmap(struct afs_vnode *vnode)
538 {
539 	if (atomic_inc_return(&vnode->cb_nr_mmap) == 1) {
540 		down_write(&vnode->volume->cell->fs_open_mmaps_lock);
541 
542 		list_add_tail(&vnode->cb_mmap_link,
543 			      &vnode->volume->cell->fs_open_mmaps);
544 
545 		up_write(&vnode->volume->cell->fs_open_mmaps_lock);
546 	}
547 }
548 
549 static void afs_drop_open_mmap(struct afs_vnode *vnode)
550 {
551 	if (!atomic_dec_and_test(&vnode->cb_nr_mmap))
552 		return;
553 
554 	down_write(&vnode->volume->cell->fs_open_mmaps_lock);
555 
556 	if (atomic_read(&vnode->cb_nr_mmap) == 0)
557 		list_del_init(&vnode->cb_mmap_link);
558 
559 	up_write(&vnode->volume->cell->fs_open_mmaps_lock);
560 	flush_work(&vnode->cb_work);
561 }
562 
563 /*
564  * Handle setting up a memory mapping on an AFS file.
565  */
566 static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
567 {
568 	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
569 	int ret;
570 
571 	afs_add_open_mmap(vnode);
572 
573 	ret = generic_file_mmap(file, vma);
574 	if (ret == 0)
575 		vma->vm_ops = &afs_vm_ops;
576 	else
577 		afs_drop_open_mmap(vnode);
578 	return ret;
579 }
580 
581 static void afs_vm_open(struct vm_area_struct *vma)
582 {
583 	afs_add_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
584 }
585 
586 static void afs_vm_close(struct vm_area_struct *vma)
587 {
588 	afs_drop_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
589 }
590 
591 static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff)
592 {
593 	struct afs_vnode *vnode = AFS_FS_I(file_inode(vmf->vma->vm_file));
594 	struct afs_file *af = vmf->vma->vm_file->private_data;
595 
596 	switch (afs_validate(vnode, af->key)) {
597 	case 0:
598 		return filemap_map_pages(vmf, start_pgoff, end_pgoff);
599 	case -ENOMEM:
600 		return VM_FAULT_OOM;
601 	case -EINTR:
602 	case -ERESTARTSYS:
603 		return VM_FAULT_RETRY;
604 	case -ESTALE:
605 	default:
606 		return VM_FAULT_SIGBUS;
607 	}
608 }
609 
610 static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
611 {
612 	struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
613 	struct afs_file *af = iocb->ki_filp->private_data;
614 	int ret;
615 
616 	ret = afs_validate(vnode, af->key);
617 	if (ret < 0)
618 		return ret;
619 
620 	return generic_file_read_iter(iocb, iter);
621 }
622