1 /* file.c: AFS filesystem file handling
3 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
18 #include <linux/pagemap.h>
19 #include <linux/pagevec.h>
20 #include <linux/buffer_head.h>
23 #include <rxrpc/call.h>
26 #define list_to_page(head) (list_entry((head)->prev, struct page, lru))
29 static int afs_file_open(struct inode *inode, struct file *file);
30 static int afs_file_release(struct inode *inode, struct file *file);
33 static int afs_file_readpage(struct file *file, struct page *page);
34 static void afs_file_invalidatepage(struct page *page, unsigned long offset);
35 static int afs_file_releasepage(struct page *page, gfp_t gfp_flags);
36 static int afs_file_mmap(struct file * file, struct vm_area_struct * vma);
38 #ifdef CONFIG_AFS_FSCACHE
39 static int afs_file_readpages(struct file *filp, struct address_space *mapping,
40 struct list_head *pages, unsigned nr_pages);
41 static int afs_file_page_mkwrite(struct vm_area_struct *vma, struct page *page);
44 struct inode_operations afs_file_inode_operations = {
45 .getattr = afs_inode_getattr,
48 const struct file_operations afs_file_file_operations = {
49 .llseek = generic_file_llseek,
50 .read = generic_file_read,
51 .mmap = afs_file_mmap,
52 .sendfile = generic_file_sendfile,
55 const struct address_space_operations afs_fs_aops = {
56 .readpage = afs_file_readpage,
57 #ifdef CONFIG_AFS_FSCACHE
58 .readpages = afs_file_readpages,
60 .sync_page = block_sync_page,
61 .set_page_dirty = __set_page_dirty_nobuffers,
62 .releasepage = afs_file_releasepage,
63 .invalidatepage = afs_file_invalidatepage,
66 static struct vm_operations_struct afs_fs_vm_operations = {
67 .nopage = filemap_nopage,
68 .populate = filemap_populate,
69 #ifdef CONFIG_AFS_FSCACHE
70 .page_mkwrite = afs_file_page_mkwrite,
74 /*****************************************************************************/
76 * set up a memory mapping on an AFS file
77 * - we set our own VMA ops so that we can catch the page becoming writable for
78 * userspace for shared-writable mmap
80 static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
85 vma->vm_ops = &afs_fs_vm_operations;
88 } /* end afs_file_mmap() */
90 /*****************************************************************************/
92 * deal with notification that a page was read from the cache
94 #ifdef CONFIG_AFS_FSCACHE
95 static void afs_file_readpage_read_complete(struct page *page,
99 _enter("%p,%p,%d", page, data, error);
101 /* if the read completes with an error, we just unlock the page and let
102 * the VM reissue the readpage */
104 SetPageUptodate(page);
107 } /* end afs_file_readpage_read_complete() */
110 /*****************************************************************************/
112 * deal with notification that a page was written to the cache
114 #ifdef CONFIG_AFS_FSCACHE
115 static void afs_file_readpage_write_complete(struct page *page,
119 _enter("%p,%p,%d", page, data, error);
121 /* note that the page has been written to the cache and can now be
123 end_page_fs_misc(page);
125 } /* end afs_file_readpage_write_complete() */
128 /*****************************************************************************/
130 * AFS read page from file (or symlink)
132 static int afs_file_readpage(struct file *file, struct page *page)
134 struct afs_rxfs_fetch_descriptor desc;
135 struct afs_vnode *vnode;
139 inode = page->mapping->host;
141 _enter("{%lu},%p{%lu}", inode->i_ino, page, page->index);
143 vnode = AFS_FS_I(inode);
145 BUG_ON(!PageLocked(page));
148 if (vnode->flags & AFS_VNODE_DELETED)
151 #ifdef CONFIG_AFS_FSCACHE
153 ret = fscache_read_or_alloc_page(vnode->cache,
155 afs_file_readpage_read_complete,
163 /* read BIO submitted (page in cache) */
167 /* page not yet cached */
169 _debug("cache said ENODATA");
172 /* page will not be cached */
174 _debug("cache said ENOBUFS");
177 desc.fid = vnode->fid;
178 desc.offset = page->index << PAGE_CACHE_SHIFT;
179 desc.size = min((size_t) (inode->i_size - desc.offset),
181 desc.buffer = kmap(page);
183 clear_page(desc.buffer);
185 /* read the contents of the file from the server into the
187 ret = afs_vnode_fetch_data(vnode, &desc);
190 if (ret == -ENOENT) {
191 kdebug("got NOENT from server"
192 " - marking file deleted and stale");
193 vnode->flags |= AFS_VNODE_DELETED;
197 #ifdef CONFIG_AFS_FSCACHE
198 fscache_uncache_page(vnode->cache, page);
199 ClearPagePrivate(page);
204 SetPageUptodate(page);
206 /* send the page to the cache */
207 #ifdef CONFIG_AFS_FSCACHE
208 if (PagePrivate(page)) {
209 if (TestSetPageFsMisc(page))
211 if (fscache_write_page(vnode->cache,
213 afs_file_readpage_write_complete,
217 fscache_uncache_page(vnode->cache, page);
218 ClearPagePrivate(page);
219 end_page_fs_misc(page);
233 _leave(" = %d", ret);
236 } /* end afs_file_readpage() */
238 /*****************************************************************************/
240 * read a set of pages
242 #ifdef CONFIG_AFS_FSCACHE
243 static int afs_file_readpages(struct file *filp, struct address_space *mapping,
244 struct list_head *pages, unsigned nr_pages)
246 struct afs_vnode *vnode;
248 struct pagevec lru_pvec;
253 _enter(",{%lu},,%d", mapping->host->i_ino, nr_pages);
255 vnode = AFS_FS_I(mapping->host);
256 if (vnode->flags & AFS_VNODE_DELETED) {
257 _leave(" = -ESTALE");
261 /* attempt to read as many of the pages as possible */
262 ret = fscache_read_or_alloc_pages(vnode->cache,
266 afs_file_readpage_read_complete,
268 mapping_gfp_mask(mapping));
271 /* all pages are being read from the cache */
273 BUG_ON(!list_empty(pages));
274 BUG_ON(nr_pages != 0);
275 _leave(" = 0 [reading all]");
278 /* there were pages that couldn't be read from the cache */
285 _leave(" = %d", ret);
289 /* load the missing pages from the network */
290 ret = read_cache_pages(mapping, pages,
291 (void *) afs_file_readpage, NULL);
293 _leave(" = %d [netting]", ret);
296 } /* end afs_file_readpages() */
299 /*****************************************************************************/
301 * invalidate part or all of a page
303 static void afs_file_invalidatepage(struct page *page, unsigned long offset)
305 _enter("{%lu},%lu", page->index, offset);
307 BUG_ON(!PageLocked(page));
309 if (PagePrivate(page)) {
310 /* We release buffers only if the entire page is being
312 * The get_block cached value has been unconditionally
313 * invalidated, so real IO is not possible anymore.
315 if (offset == 0 && !PageWriteback(page))
316 page->mapping->a_ops->releasepage(page, 0);
321 } /* end afs_file_invalidatepage() */
323 /*****************************************************************************/
325 * release a page and cleanup its private data
327 static int afs_file_releasepage(struct page *page, gfp_t gfp_flags)
329 _enter("{%lu},%x", page->index, gfp_flags);
331 #ifdef CONFIG_AFS_FSCACHE
332 wait_on_page_fs_misc(page);
333 fscache_uncache_page(AFS_FS_I(page->mapping->host)->cache, page);
334 ClearPagePrivate(page);
337 /* indicate that the page can be released */
341 } /* end afs_file_releasepage() */
343 /*****************************************************************************/
345 * wait for the disc cache to finish writing before permitting modification of
346 * our page in the page cache
348 #ifdef CONFIG_AFS_FSCACHE
349 static int afs_file_page_mkwrite(struct vm_area_struct *vma, struct page *page)
351 wait_on_page_fs_misc(page);
354 } /* end afs_file_page_mkwrite() */