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