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