xref: /openbmc/linux/fs/afs/file.c (revision b4a6aaea)
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 	file->private_data = af;
163 	_leave(" = 0");
164 	return 0;
165 
166 error_af:
167 	kfree(af);
168 error_key:
169 	key_put(key);
170 error:
171 	_leave(" = %d", ret);
172 	return ret;
173 }
174 
175 /*
176  * release an AFS file or directory and discard its key
177  */
178 int afs_release(struct inode *inode, struct file *file)
179 {
180 	struct afs_vnode *vnode = AFS_FS_I(inode);
181 	struct afs_file *af = file->private_data;
182 	int ret = 0;
183 
184 	_enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
185 
186 	if ((file->f_mode & FMODE_WRITE))
187 		ret = vfs_fsync(file, 0);
188 
189 	file->private_data = NULL;
190 	if (af->wb)
191 		afs_put_wb_key(af->wb);
192 	key_put(af->key);
193 	kfree(af);
194 	afs_prune_wb_keys(vnode);
195 	_leave(" = %d", ret);
196 	return ret;
197 }
198 
199 /*
200  * Allocate a new read record.
201  */
202 struct afs_read *afs_alloc_read(gfp_t gfp)
203 {
204 	struct afs_read *req;
205 
206 	req = kzalloc(sizeof(struct afs_read), gfp);
207 	if (req)
208 		refcount_set(&req->usage, 1);
209 
210 	return req;
211 }
212 
213 /*
214  * Dispose of a ref to a read record.
215  */
216 void afs_put_read(struct afs_read *req)
217 {
218 	if (refcount_dec_and_test(&req->usage)) {
219 		if (req->cleanup)
220 			req->cleanup(req);
221 		key_put(req->key);
222 		kfree(req);
223 	}
224 }
225 
226 static void afs_fetch_data_notify(struct afs_operation *op)
227 {
228 	struct afs_read *req = op->fetch.req;
229 	struct netfs_read_subrequest *subreq = req->subreq;
230 	int error = op->error;
231 
232 	if (error == -ECONNABORTED)
233 		error = afs_abort_to_error(op->ac.abort_code);
234 	req->error = error;
235 
236 	if (subreq) {
237 		__set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
238 		netfs_subreq_terminated(subreq, error ?: req->actual_len, false);
239 		req->subreq = NULL;
240 	} else if (req->done) {
241 		req->done(req);
242 	}
243 }
244 
245 static void afs_fetch_data_success(struct afs_operation *op)
246 {
247 	struct afs_vnode *vnode = op->file[0].vnode;
248 
249 	_enter("op=%08x", op->debug_id);
250 	afs_vnode_commit_status(op, &op->file[0]);
251 	afs_stat_v(vnode, n_fetches);
252 	atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes);
253 	afs_fetch_data_notify(op);
254 }
255 
256 static void afs_fetch_data_put(struct afs_operation *op)
257 {
258 	op->fetch.req->error = op->error;
259 	afs_put_read(op->fetch.req);
260 }
261 
262 static const struct afs_operation_ops afs_fetch_data_operation = {
263 	.issue_afs_rpc	= afs_fs_fetch_data,
264 	.issue_yfs_rpc	= yfs_fs_fetch_data,
265 	.success	= afs_fetch_data_success,
266 	.aborted	= afs_check_for_remote_deletion,
267 	.failed		= afs_fetch_data_notify,
268 	.put		= afs_fetch_data_put,
269 };
270 
271 /*
272  * Fetch file data from the volume.
273  */
274 int afs_fetch_data(struct afs_vnode *vnode, struct afs_read *req)
275 {
276 	struct afs_operation *op;
277 
278 	_enter("%s{%llx:%llu.%u},%x,,,",
279 	       vnode->volume->name,
280 	       vnode->fid.vid,
281 	       vnode->fid.vnode,
282 	       vnode->fid.unique,
283 	       key_serial(req->key));
284 
285 	op = afs_alloc_operation(req->key, vnode->volume);
286 	if (IS_ERR(op)) {
287 		if (req->subreq)
288 			netfs_subreq_terminated(req->subreq, PTR_ERR(op), false);
289 		return PTR_ERR(op);
290 	}
291 
292 	afs_op_set_vnode(op, 0, vnode);
293 
294 	op->fetch.req	= afs_get_read(req);
295 	op->ops		= &afs_fetch_data_operation;
296 	return afs_do_sync_operation(op);
297 }
298 
299 static void afs_req_issue_op(struct netfs_read_subrequest *subreq)
300 {
301 	struct afs_vnode *vnode = AFS_FS_I(subreq->rreq->inode);
302 	struct afs_read *fsreq;
303 
304 	fsreq = afs_alloc_read(GFP_NOFS);
305 	if (!fsreq)
306 		return netfs_subreq_terminated(subreq, -ENOMEM, false);
307 
308 	fsreq->subreq	= subreq;
309 	fsreq->pos	= subreq->start + subreq->transferred;
310 	fsreq->len	= subreq->len   - subreq->transferred;
311 	fsreq->key	= key_get(subreq->rreq->netfs_priv);
312 	fsreq->vnode	= vnode;
313 	fsreq->iter	= &fsreq->def_iter;
314 
315 	iov_iter_xarray(&fsreq->def_iter, READ,
316 			&fsreq->vnode->vfs_inode.i_mapping->i_pages,
317 			fsreq->pos, fsreq->len);
318 
319 	afs_fetch_data(fsreq->vnode, fsreq);
320 	afs_put_read(fsreq);
321 }
322 
323 static int afs_symlink_readpage(struct file *file, struct page *page)
324 {
325 	struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
326 	struct afs_read *fsreq;
327 	struct folio *folio = page_folio(page);
328 	int ret;
329 
330 	fsreq = afs_alloc_read(GFP_NOFS);
331 	if (!fsreq)
332 		return -ENOMEM;
333 
334 	fsreq->pos	= folio_pos(folio);
335 	fsreq->len	= folio_size(folio);
336 	fsreq->vnode	= vnode;
337 	fsreq->iter	= &fsreq->def_iter;
338 	iov_iter_xarray(&fsreq->def_iter, READ, &page->mapping->i_pages,
339 			fsreq->pos, fsreq->len);
340 
341 	ret = afs_fetch_data(fsreq->vnode, fsreq);
342 	if (ret == 0)
343 		SetPageUptodate(page);
344 	unlock_page(page);
345 	return ret;
346 }
347 
348 static void afs_init_rreq(struct netfs_read_request *rreq, struct file *file)
349 {
350 	rreq->netfs_priv = key_get(afs_file_key(file));
351 }
352 
353 static bool afs_is_cache_enabled(struct inode *inode)
354 {
355 	struct fscache_cookie *cookie = afs_vnode_cache(AFS_FS_I(inode));
356 
357 	return fscache_cookie_enabled(cookie) && !hlist_empty(&cookie->backing_objects);
358 }
359 
360 static int afs_begin_cache_operation(struct netfs_read_request *rreq)
361 {
362 	struct afs_vnode *vnode = AFS_FS_I(rreq->inode);
363 
364 	return fscache_begin_read_operation(rreq, afs_vnode_cache(vnode));
365 }
366 
367 static int afs_check_write_begin(struct file *file, loff_t pos, unsigned len,
368 				 struct folio *folio, void **_fsdata)
369 {
370 	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
371 
372 	return test_bit(AFS_VNODE_DELETED, &vnode->flags) ? -ESTALE : 0;
373 }
374 
375 static void afs_priv_cleanup(struct address_space *mapping, void *netfs_priv)
376 {
377 	key_put(netfs_priv);
378 }
379 
380 const struct netfs_read_request_ops afs_req_ops = {
381 	.init_rreq		= afs_init_rreq,
382 	.is_cache_enabled	= afs_is_cache_enabled,
383 	.begin_cache_operation	= afs_begin_cache_operation,
384 	.check_write_begin	= afs_check_write_begin,
385 	.issue_op		= afs_req_issue_op,
386 	.cleanup		= afs_priv_cleanup,
387 };
388 
389 static int afs_readpage(struct file *file, struct page *page)
390 {
391 	struct folio *folio = page_folio(page);
392 
393 	return netfs_readpage(file, folio, &afs_req_ops, NULL);
394 }
395 
396 static void afs_readahead(struct readahead_control *ractl)
397 {
398 	netfs_readahead(ractl, &afs_req_ops, NULL);
399 }
400 
401 /*
402  * Adjust the dirty region of the page on truncation or full invalidation,
403  * getting rid of the markers altogether if the region is entirely invalidated.
404  */
405 static void afs_invalidate_dirty(struct folio *folio, unsigned int offset,
406 				 unsigned int length)
407 {
408 	struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio));
409 	unsigned long priv;
410 	unsigned int f, t, end = offset + length;
411 
412 	priv = (unsigned long)folio_get_private(folio);
413 
414 	/* we clean up only if the entire page is being invalidated */
415 	if (offset == 0 && length == folio_size(folio))
416 		goto full_invalidate;
417 
418 	 /* If the page was dirtied by page_mkwrite(), the PTE stays writable
419 	  * and we don't get another notification to tell us to expand it
420 	  * again.
421 	  */
422 	if (afs_is_folio_dirty_mmapped(priv))
423 		return;
424 
425 	/* We may need to shorten the dirty region */
426 	f = afs_folio_dirty_from(folio, priv);
427 	t = afs_folio_dirty_to(folio, priv);
428 
429 	if (t <= offset || f >= end)
430 		return; /* Doesn't overlap */
431 
432 	if (f < offset && t > end)
433 		return; /* Splits the dirty region - just absorb it */
434 
435 	if (f >= offset && t <= end)
436 		goto undirty;
437 
438 	if (f < offset)
439 		t = offset;
440 	else
441 		f = end;
442 	if (f == t)
443 		goto undirty;
444 
445 	priv = afs_folio_dirty(folio, f, t);
446 	folio_change_private(folio, (void *)priv);
447 	trace_afs_folio_dirty(vnode, tracepoint_string("trunc"), folio);
448 	return;
449 
450 undirty:
451 	trace_afs_folio_dirty(vnode, tracepoint_string("undirty"), folio);
452 	folio_clear_dirty_for_io(folio);
453 full_invalidate:
454 	trace_afs_folio_dirty(vnode, tracepoint_string("inval"), folio);
455 	folio_detach_private(folio);
456 }
457 
458 /*
459  * invalidate part or all of a page
460  * - release a page and clean up its private data if offset is 0 (indicating
461  *   the entire page)
462  */
463 static void afs_invalidatepage(struct page *page, unsigned int offset,
464 			       unsigned int length)
465 {
466 	struct folio *folio = page_folio(page);
467 
468 	_enter("{%lu},%u,%u", folio_index(folio), offset, length);
469 
470 	BUG_ON(!PageLocked(page));
471 
472 	if (PagePrivate(page))
473 		afs_invalidate_dirty(folio, offset, length);
474 
475 	folio_wait_fscache(folio);
476 	_leave("");
477 }
478 
479 /*
480  * release a page and clean up its private state if it's not busy
481  * - return true if the page can now be released, false if not
482  */
483 static int afs_releasepage(struct page *page, gfp_t gfp_flags)
484 {
485 	struct folio *folio = page_folio(page);
486 	struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio));
487 
488 	_enter("{{%llx:%llu}[%lu],%lx},%x",
489 	       vnode->fid.vid, vnode->fid.vnode, folio_index(folio), folio->flags,
490 	       gfp_flags);
491 
492 	/* deny if page is being written to the cache and the caller hasn't
493 	 * elected to wait */
494 #ifdef CONFIG_AFS_FSCACHE
495 	if (folio_test_fscache(folio)) {
496 		if (!(gfp_flags & __GFP_DIRECT_RECLAIM) || !(gfp_flags & __GFP_FS))
497 			return false;
498 		folio_wait_fscache(folio);
499 	}
500 #endif
501 
502 	if (folio_test_private(folio)) {
503 		trace_afs_folio_dirty(vnode, tracepoint_string("rel"), folio);
504 		folio_detach_private(folio);
505 	}
506 
507 	/* Indicate that the folio can be released */
508 	_leave(" = T");
509 	return true;
510 }
511 
512 static void afs_add_open_mmap(struct afs_vnode *vnode)
513 {
514 	if (atomic_inc_return(&vnode->cb_nr_mmap) == 1) {
515 		down_write(&vnode->volume->cell->fs_open_mmaps_lock);
516 
517 		list_add_tail(&vnode->cb_mmap_link,
518 			      &vnode->volume->cell->fs_open_mmaps);
519 
520 		up_write(&vnode->volume->cell->fs_open_mmaps_lock);
521 	}
522 }
523 
524 static void afs_drop_open_mmap(struct afs_vnode *vnode)
525 {
526 	if (!atomic_dec_and_test(&vnode->cb_nr_mmap))
527 		return;
528 
529 	down_write(&vnode->volume->cell->fs_open_mmaps_lock);
530 
531 	if (atomic_read(&vnode->cb_nr_mmap) == 0)
532 		list_del_init(&vnode->cb_mmap_link);
533 
534 	up_write(&vnode->volume->cell->fs_open_mmaps_lock);
535 	flush_work(&vnode->cb_work);
536 }
537 
538 /*
539  * Handle setting up a memory mapping on an AFS file.
540  */
541 static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
542 {
543 	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
544 	int ret;
545 
546 	afs_add_open_mmap(vnode);
547 
548 	ret = generic_file_mmap(file, vma);
549 	if (ret == 0)
550 		vma->vm_ops = &afs_vm_ops;
551 	else
552 		afs_drop_open_mmap(vnode);
553 	return ret;
554 }
555 
556 static void afs_vm_open(struct vm_area_struct *vma)
557 {
558 	afs_add_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
559 }
560 
561 static void afs_vm_close(struct vm_area_struct *vma)
562 {
563 	afs_drop_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
564 }
565 
566 static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff)
567 {
568 	struct afs_vnode *vnode = AFS_FS_I(file_inode(vmf->vma->vm_file));
569 	struct afs_file *af = vmf->vma->vm_file->private_data;
570 
571 	switch (afs_validate(vnode, af->key)) {
572 	case 0:
573 		return filemap_map_pages(vmf, start_pgoff, end_pgoff);
574 	case -ENOMEM:
575 		return VM_FAULT_OOM;
576 	case -EINTR:
577 	case -ERESTARTSYS:
578 		return VM_FAULT_RETRY;
579 	case -ESTALE:
580 	default:
581 		return VM_FAULT_SIGBUS;
582 	}
583 }
584 
585 static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
586 {
587 	struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
588 	struct afs_file *af = iocb->ki_filp->private_data;
589 	int ret;
590 
591 	ret = afs_validate(vnode, af->key);
592 	if (ret < 0)
593 		return ret;
594 
595 	return generic_file_read_iter(iocb, iter);
596 }
597