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