xref: /openbmc/linux/fs/afs/file.c (revision 59b4412f)
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 "internal.h"
18 
19 static int afs_file_mmap(struct file *file, struct vm_area_struct *vma);
20 static int afs_readpage(struct file *file, struct page *page);
21 static void afs_invalidatepage(struct page *page, unsigned int offset,
22 			       unsigned int length);
23 static int afs_releasepage(struct page *page, gfp_t gfp_flags);
24 
25 static int afs_readpages(struct file *filp, struct address_space *mapping,
26 			 struct list_head *pages, unsigned nr_pages);
27 
28 const struct file_operations afs_file_operations = {
29 	.open		= afs_open,
30 	.release	= afs_release,
31 	.llseek		= generic_file_llseek,
32 	.read_iter	= generic_file_read_iter,
33 	.write_iter	= afs_file_write,
34 	.mmap		= afs_file_mmap,
35 	.splice_read	= generic_file_splice_read,
36 	.fsync		= afs_fsync,
37 	.lock		= afs_lock,
38 	.flock		= afs_flock,
39 };
40 
41 const struct inode_operations afs_file_inode_operations = {
42 	.getattr	= afs_getattr,
43 	.setattr	= afs_setattr,
44 	.permission	= afs_permission,
45 	.listxattr	= afs_listxattr,
46 };
47 
48 const struct address_space_operations afs_fs_aops = {
49 	.readpage	= afs_readpage,
50 	.readpages	= afs_readpages,
51 	.set_page_dirty	= afs_set_page_dirty,
52 	.launder_page	= afs_launder_page,
53 	.releasepage	= afs_releasepage,
54 	.invalidatepage	= afs_invalidatepage,
55 	.write_begin	= afs_write_begin,
56 	.write_end	= afs_write_end,
57 	.writepage	= afs_writepage,
58 	.writepages	= afs_writepages,
59 };
60 
61 static const struct vm_operations_struct afs_vm_ops = {
62 	.fault		= filemap_fault,
63 	.map_pages	= filemap_map_pages,
64 	.page_mkwrite	= afs_page_mkwrite,
65 };
66 
67 /*
68  * Discard a pin on a writeback key.
69  */
70 void afs_put_wb_key(struct afs_wb_key *wbk)
71 {
72 	if (wbk && refcount_dec_and_test(&wbk->usage)) {
73 		key_put(wbk->key);
74 		kfree(wbk);
75 	}
76 }
77 
78 /*
79  * Cache key for writeback.
80  */
81 int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af)
82 {
83 	struct afs_wb_key *wbk, *p;
84 
85 	wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL);
86 	if (!wbk)
87 		return -ENOMEM;
88 	refcount_set(&wbk->usage, 2);
89 	wbk->key = af->key;
90 
91 	spin_lock(&vnode->wb_lock);
92 	list_for_each_entry(p, &vnode->wb_keys, vnode_link) {
93 		if (p->key == wbk->key)
94 			goto found;
95 	}
96 
97 	key_get(wbk->key);
98 	list_add_tail(&wbk->vnode_link, &vnode->wb_keys);
99 	spin_unlock(&vnode->wb_lock);
100 	af->wb = wbk;
101 	return 0;
102 
103 found:
104 	refcount_inc(&p->usage);
105 	spin_unlock(&vnode->wb_lock);
106 	af->wb = p;
107 	kfree(wbk);
108 	return 0;
109 }
110 
111 /*
112  * open an AFS file or directory and attach a key to it
113  */
114 int afs_open(struct inode *inode, struct file *file)
115 {
116 	struct afs_vnode *vnode = AFS_FS_I(inode);
117 	struct afs_file *af;
118 	struct key *key;
119 	int ret;
120 
121 	_enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
122 
123 	key = afs_request_key(vnode->volume->cell);
124 	if (IS_ERR(key)) {
125 		ret = PTR_ERR(key);
126 		goto error;
127 	}
128 
129 	af = kzalloc(sizeof(*af), GFP_KERNEL);
130 	if (!af) {
131 		ret = -ENOMEM;
132 		goto error_key;
133 	}
134 	af->key = key;
135 
136 	ret = afs_validate(vnode, key);
137 	if (ret < 0)
138 		goto error_af;
139 
140 	if (file->f_mode & FMODE_WRITE) {
141 		ret = afs_cache_wb_key(vnode, af);
142 		if (ret < 0)
143 			goto error_af;
144 	}
145 
146 	if (file->f_flags & O_TRUNC)
147 		set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
148 
149 	file->private_data = af;
150 	_leave(" = 0");
151 	return 0;
152 
153 error_af:
154 	kfree(af);
155 error_key:
156 	key_put(key);
157 error:
158 	_leave(" = %d", ret);
159 	return ret;
160 }
161 
162 /*
163  * release an AFS file or directory and discard its key
164  */
165 int afs_release(struct inode *inode, struct file *file)
166 {
167 	struct afs_vnode *vnode = AFS_FS_I(inode);
168 	struct afs_file *af = file->private_data;
169 	int ret = 0;
170 
171 	_enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
172 
173 	if ((file->f_mode & FMODE_WRITE))
174 		ret = vfs_fsync(file, 0);
175 
176 	file->private_data = NULL;
177 	if (af->wb)
178 		afs_put_wb_key(af->wb);
179 	key_put(af->key);
180 	kfree(af);
181 	afs_prune_wb_keys(vnode);
182 	_leave(" = %d", ret);
183 	return ret;
184 }
185 
186 /*
187  * Dispose of a ref to a read record.
188  */
189 void afs_put_read(struct afs_read *req)
190 {
191 	int i;
192 
193 	if (refcount_dec_and_test(&req->usage)) {
194 		if (req->pages) {
195 			for (i = 0; i < req->nr_pages; i++)
196 				if (req->pages[i])
197 					put_page(req->pages[i]);
198 			if (req->pages != req->array)
199 				kfree(req->pages);
200 		}
201 		kfree(req);
202 	}
203 }
204 
205 #ifdef CONFIG_AFS_FSCACHE
206 /*
207  * deal with notification that a page was read from the cache
208  */
209 static void afs_file_readpage_read_complete(struct page *page,
210 					    void *data,
211 					    int error)
212 {
213 	_enter("%p,%p,%d", page, data, error);
214 
215 	/* if the read completes with an error, we just unlock the page and let
216 	 * the VM reissue the readpage */
217 	if (!error)
218 		SetPageUptodate(page);
219 	unlock_page(page);
220 }
221 #endif
222 
223 static void afs_fetch_data_success(struct afs_operation *op)
224 {
225 	struct afs_vnode *vnode = op->file[0].vnode;
226 
227 	_enter("op=%08x", op->debug_id);
228 	afs_vnode_commit_status(op, &op->file[0]);
229 	afs_stat_v(vnode, n_fetches);
230 	atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes);
231 }
232 
233 static void afs_fetch_data_put(struct afs_operation *op)
234 {
235 	afs_put_read(op->fetch.req);
236 }
237 
238 static const struct afs_operation_ops afs_fetch_data_operation = {
239 	.issue_afs_rpc	= afs_fs_fetch_data,
240 	.issue_yfs_rpc	= yfs_fs_fetch_data,
241 	.success	= afs_fetch_data_success,
242 	.aborted	= afs_check_for_remote_deletion,
243 	.put		= afs_fetch_data_put,
244 };
245 
246 /*
247  * Fetch file data from the volume.
248  */
249 int afs_fetch_data(struct afs_vnode *vnode, struct key *key, struct afs_read *req)
250 {
251 	struct afs_operation *op;
252 
253 	_enter("%s{%llx:%llu.%u},%x,,,",
254 	       vnode->volume->name,
255 	       vnode->fid.vid,
256 	       vnode->fid.vnode,
257 	       vnode->fid.unique,
258 	       key_serial(key));
259 
260 	op = afs_alloc_operation(key, vnode->volume);
261 	if (IS_ERR(op))
262 		return PTR_ERR(op);
263 
264 	afs_op_set_vnode(op, 0, vnode);
265 
266 	op->fetch.req	= afs_get_read(req);
267 	op->ops		= &afs_fetch_data_operation;
268 	return afs_do_sync_operation(op);
269 }
270 
271 /*
272  * read page from file, directory or symlink, given a key to use
273  */
274 int afs_page_filler(void *data, struct page *page)
275 {
276 	struct inode *inode = page->mapping->host;
277 	struct afs_vnode *vnode = AFS_FS_I(inode);
278 	struct afs_read *req;
279 	struct key *key = data;
280 	int ret;
281 
282 	_enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
283 
284 	BUG_ON(!PageLocked(page));
285 
286 	ret = -ESTALE;
287 	if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
288 		goto error;
289 
290 	/* is it cached? */
291 #ifdef CONFIG_AFS_FSCACHE
292 	ret = fscache_read_or_alloc_page(vnode->cache,
293 					 page,
294 					 afs_file_readpage_read_complete,
295 					 NULL,
296 					 GFP_KERNEL);
297 #else
298 	ret = -ENOBUFS;
299 #endif
300 	switch (ret) {
301 		/* read BIO submitted (page in cache) */
302 	case 0:
303 		break;
304 
305 		/* page not yet cached */
306 	case -ENODATA:
307 		_debug("cache said ENODATA");
308 		goto go_on;
309 
310 		/* page will not be cached */
311 	case -ENOBUFS:
312 		_debug("cache said ENOBUFS");
313 
314 		/* fall through */
315 	default:
316 	go_on:
317 		req = kzalloc(struct_size(req, array, 1), GFP_KERNEL);
318 		if (!req)
319 			goto enomem;
320 
321 		/* We request a full page.  If the page is a partial one at the
322 		 * end of the file, the server will return a short read and the
323 		 * unmarshalling code will clear the unfilled space.
324 		 */
325 		refcount_set(&req->usage, 1);
326 		req->pos = (loff_t)page->index << PAGE_SHIFT;
327 		req->len = PAGE_SIZE;
328 		req->nr_pages = 1;
329 		req->pages = req->array;
330 		req->pages[0] = page;
331 		get_page(page);
332 
333 		/* read the contents of the file from the server into the
334 		 * page */
335 		ret = afs_fetch_data(vnode, key, req);
336 		afs_put_read(req);
337 
338 		if (ret < 0) {
339 			if (ret == -ENOENT) {
340 				_debug("got NOENT from server"
341 				       " - marking file deleted and stale");
342 				set_bit(AFS_VNODE_DELETED, &vnode->flags);
343 				ret = -ESTALE;
344 			}
345 
346 #ifdef CONFIG_AFS_FSCACHE
347 			fscache_uncache_page(vnode->cache, page);
348 #endif
349 			BUG_ON(PageFsCache(page));
350 
351 			if (ret == -EINTR ||
352 			    ret == -ENOMEM ||
353 			    ret == -ERESTARTSYS ||
354 			    ret == -EAGAIN)
355 				goto error;
356 			goto io_error;
357 		}
358 
359 		SetPageUptodate(page);
360 
361 		/* send the page to the cache */
362 #ifdef CONFIG_AFS_FSCACHE
363 		if (PageFsCache(page) &&
364 		    fscache_write_page(vnode->cache, page, vnode->status.size,
365 				       GFP_KERNEL) != 0) {
366 			fscache_uncache_page(vnode->cache, page);
367 			BUG_ON(PageFsCache(page));
368 		}
369 #endif
370 		unlock_page(page);
371 	}
372 
373 	_leave(" = 0");
374 	return 0;
375 
376 io_error:
377 	SetPageError(page);
378 	goto error;
379 enomem:
380 	ret = -ENOMEM;
381 error:
382 	unlock_page(page);
383 	_leave(" = %d", ret);
384 	return ret;
385 }
386 
387 /*
388  * read page from file, directory or symlink, given a file to nominate the key
389  * to be used
390  */
391 static int afs_readpage(struct file *file, struct page *page)
392 {
393 	struct key *key;
394 	int ret;
395 
396 	if (file) {
397 		key = afs_file_key(file);
398 		ASSERT(key != NULL);
399 		ret = afs_page_filler(key, page);
400 	} else {
401 		struct inode *inode = page->mapping->host;
402 		key = afs_request_key(AFS_FS_S(inode->i_sb)->cell);
403 		if (IS_ERR(key)) {
404 			ret = PTR_ERR(key);
405 		} else {
406 			ret = afs_page_filler(key, page);
407 			key_put(key);
408 		}
409 	}
410 	return ret;
411 }
412 
413 /*
414  * Make pages available as they're filled.
415  */
416 static void afs_readpages_page_done(struct afs_read *req)
417 {
418 #ifdef CONFIG_AFS_FSCACHE
419 	struct afs_vnode *vnode = req->vnode;
420 #endif
421 	struct page *page = req->pages[req->index];
422 
423 	req->pages[req->index] = NULL;
424 	SetPageUptodate(page);
425 
426 	/* send the page to the cache */
427 #ifdef CONFIG_AFS_FSCACHE
428 	if (PageFsCache(page) &&
429 	    fscache_write_page(vnode->cache, page, vnode->status.size,
430 			       GFP_KERNEL) != 0) {
431 		fscache_uncache_page(vnode->cache, page);
432 		BUG_ON(PageFsCache(page));
433 	}
434 #endif
435 	unlock_page(page);
436 	put_page(page);
437 }
438 
439 /*
440  * Read a contiguous set of pages.
441  */
442 static int afs_readpages_one(struct file *file, struct address_space *mapping,
443 			     struct list_head *pages)
444 {
445 	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
446 	struct afs_read *req;
447 	struct list_head *p;
448 	struct page *first, *page;
449 	struct key *key = afs_file_key(file);
450 	pgoff_t index;
451 	int ret, n, i;
452 
453 	/* Count the number of contiguous pages at the front of the list.  Note
454 	 * that the list goes prev-wards rather than next-wards.
455 	 */
456 	first = lru_to_page(pages);
457 	index = first->index + 1;
458 	n = 1;
459 	for (p = first->lru.prev; p != pages; p = p->prev) {
460 		page = list_entry(p, struct page, lru);
461 		if (page->index != index)
462 			break;
463 		index++;
464 		n++;
465 	}
466 
467 	req = kzalloc(struct_size(req, array, n), GFP_NOFS);
468 	if (!req)
469 		return -ENOMEM;
470 
471 	refcount_set(&req->usage, 1);
472 	req->vnode = vnode;
473 	req->page_done = afs_readpages_page_done;
474 	req->pos = first->index;
475 	req->pos <<= PAGE_SHIFT;
476 	req->pages = req->array;
477 
478 	/* Transfer the pages to the request.  We add them in until one fails
479 	 * to add to the LRU and then we stop (as that'll make a hole in the
480 	 * contiguous run.
481 	 *
482 	 * Note that it's possible for the file size to change whilst we're
483 	 * doing this, but we rely on the server returning less than we asked
484 	 * for if the file shrank.  We also rely on this to deal with a partial
485 	 * page at the end of the file.
486 	 */
487 	do {
488 		page = lru_to_page(pages);
489 		list_del(&page->lru);
490 		index = page->index;
491 		if (add_to_page_cache_lru(page, mapping, index,
492 					  readahead_gfp_mask(mapping))) {
493 #ifdef CONFIG_AFS_FSCACHE
494 			fscache_uncache_page(vnode->cache, page);
495 #endif
496 			put_page(page);
497 			break;
498 		}
499 
500 		req->pages[req->nr_pages++] = page;
501 		req->len += PAGE_SIZE;
502 	} while (req->nr_pages < n);
503 
504 	if (req->nr_pages == 0) {
505 		kfree(req);
506 		return 0;
507 	}
508 
509 	ret = afs_fetch_data(vnode, key, req);
510 	if (ret < 0)
511 		goto error;
512 
513 	task_io_account_read(PAGE_SIZE * req->nr_pages);
514 	afs_put_read(req);
515 	return 0;
516 
517 error:
518 	if (ret == -ENOENT) {
519 		_debug("got NOENT from server"
520 		       " - marking file deleted and stale");
521 		set_bit(AFS_VNODE_DELETED, &vnode->flags);
522 		ret = -ESTALE;
523 	}
524 
525 	for (i = 0; i < req->nr_pages; i++) {
526 		page = req->pages[i];
527 		if (page) {
528 #ifdef CONFIG_AFS_FSCACHE
529 			fscache_uncache_page(vnode->cache, page);
530 #endif
531 			SetPageError(page);
532 			unlock_page(page);
533 		}
534 	}
535 
536 	afs_put_read(req);
537 	return ret;
538 }
539 
540 /*
541  * read a set of pages
542  */
543 static int afs_readpages(struct file *file, struct address_space *mapping,
544 			 struct list_head *pages, unsigned nr_pages)
545 {
546 	struct key *key = afs_file_key(file);
547 	struct afs_vnode *vnode;
548 	int ret = 0;
549 
550 	_enter("{%d},{%lu},,%d",
551 	       key_serial(key), mapping->host->i_ino, nr_pages);
552 
553 	ASSERT(key != NULL);
554 
555 	vnode = AFS_FS_I(mapping->host);
556 	if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
557 		_leave(" = -ESTALE");
558 		return -ESTALE;
559 	}
560 
561 	/* attempt to read as many of the pages as possible */
562 #ifdef CONFIG_AFS_FSCACHE
563 	ret = fscache_read_or_alloc_pages(vnode->cache,
564 					  mapping,
565 					  pages,
566 					  &nr_pages,
567 					  afs_file_readpage_read_complete,
568 					  NULL,
569 					  mapping_gfp_mask(mapping));
570 #else
571 	ret = -ENOBUFS;
572 #endif
573 
574 	switch (ret) {
575 		/* all pages are being read from the cache */
576 	case 0:
577 		BUG_ON(!list_empty(pages));
578 		BUG_ON(nr_pages != 0);
579 		_leave(" = 0 [reading all]");
580 		return 0;
581 
582 		/* there were pages that couldn't be read from the cache */
583 	case -ENODATA:
584 	case -ENOBUFS:
585 		break;
586 
587 		/* other error */
588 	default:
589 		_leave(" = %d", ret);
590 		return ret;
591 	}
592 
593 	while (!list_empty(pages)) {
594 		ret = afs_readpages_one(file, mapping, pages);
595 		if (ret < 0)
596 			break;
597 	}
598 
599 	_leave(" = %d [netting]", ret);
600 	return ret;
601 }
602 
603 /*
604  * invalidate part or all of a page
605  * - release a page and clean up its private data if offset is 0 (indicating
606  *   the entire page)
607  */
608 static void afs_invalidatepage(struct page *page, unsigned int offset,
609 			       unsigned int length)
610 {
611 	struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
612 	unsigned long priv;
613 
614 	_enter("{%lu},%u,%u", page->index, offset, length);
615 
616 	BUG_ON(!PageLocked(page));
617 
618 	/* we clean up only if the entire page is being invalidated */
619 	if (offset == 0 && length == PAGE_SIZE) {
620 #ifdef CONFIG_AFS_FSCACHE
621 		if (PageFsCache(page)) {
622 			struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
623 			fscache_wait_on_page_write(vnode->cache, page);
624 			fscache_uncache_page(vnode->cache, page);
625 		}
626 #endif
627 
628 		if (PagePrivate(page)) {
629 			priv = page_private(page);
630 			trace_afs_page_dirty(vnode, tracepoint_string("inval"),
631 					     page->index, priv);
632 			set_page_private(page, 0);
633 			ClearPagePrivate(page);
634 		}
635 	}
636 
637 	_leave("");
638 }
639 
640 /*
641  * release a page and clean up its private state if it's not busy
642  * - return true if the page can now be released, false if not
643  */
644 static int afs_releasepage(struct page *page, gfp_t gfp_flags)
645 {
646 	struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
647 	unsigned long priv;
648 
649 	_enter("{{%llx:%llu}[%lu],%lx},%x",
650 	       vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
651 	       gfp_flags);
652 
653 	/* deny if page is being written to the cache and the caller hasn't
654 	 * elected to wait */
655 #ifdef CONFIG_AFS_FSCACHE
656 	if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) {
657 		_leave(" = F [cache busy]");
658 		return 0;
659 	}
660 #endif
661 
662 	if (PagePrivate(page)) {
663 		priv = page_private(page);
664 		trace_afs_page_dirty(vnode, tracepoint_string("rel"),
665 				     page->index, priv);
666 		set_page_private(page, 0);
667 		ClearPagePrivate(page);
668 	}
669 
670 	/* indicate that the page can be released */
671 	_leave(" = T");
672 	return 1;
673 }
674 
675 /*
676  * Handle setting up a memory mapping on an AFS file.
677  */
678 static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
679 {
680 	int ret;
681 
682 	ret = generic_file_mmap(file, vma);
683 	if (ret == 0)
684 		vma->vm_ops = &afs_vm_ops;
685 	return ret;
686 }
687