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