1 /* AFS filesystem file handling
3 * Copyright (C) 2002, 2007 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/slab.h>
17 #include <linux/pagemap.h>
20 static int afs_file_readpage(struct file *file, struct page *page);
21 static void afs_file_invalidatepage(struct page *page, unsigned long offset);
22 static int afs_file_releasepage(struct page *page, gfp_t gfp_flags);
24 const struct file_operations afs_file_operations = {
26 .release = afs_release,
27 .llseek = generic_file_llseek,
29 .aio_read = generic_file_aio_read,
30 .mmap = generic_file_readonly_mmap,
31 .sendfile = generic_file_sendfile,
34 const struct inode_operations afs_file_inode_operations = {
35 .getattr = afs_inode_getattr,
36 .permission = afs_permission,
39 const struct address_space_operations afs_fs_aops = {
40 .readpage = afs_file_readpage,
41 .set_page_dirty = __set_page_dirty_nobuffers,
42 .releasepage = afs_file_releasepage,
43 .invalidatepage = afs_file_invalidatepage,
47 * open an AFS file or directory and attach a key to it
49 int afs_open(struct inode *inode, struct file *file)
51 struct afs_vnode *vnode = AFS_FS_I(inode);
55 _enter("{%x:%x},", vnode->fid.vid, vnode->fid.vnode);
57 key = afs_request_key(vnode->volume->cell);
59 _leave(" = %ld [key]", PTR_ERR(key));
63 ret = afs_validate(vnode, key);
65 _leave(" = %d [val]", ret);
69 file->private_data = key;
75 * release an AFS file or directory and discard its key
77 int afs_release(struct inode *inode, struct file *file)
79 struct afs_vnode *vnode = AFS_FS_I(inode);
81 _enter("{%x:%x},", vnode->fid.vid, vnode->fid.vnode);
83 key_put(file->private_data);
89 * deal with notification that a page was read from the cache
91 #ifdef AFS_CACHING_SUPPORT
92 static void afs_file_readpage_read_complete(void *cookie_data,
97 _enter("%p,%p,%p,%d", cookie_data, page, data, error);
102 SetPageUptodate(page);
109 * deal with notification that a page was written to the cache
111 #ifdef AFS_CACHING_SUPPORT
112 static void afs_file_readpage_write_complete(void *cookie_data,
117 _enter("%p,%p,%p,%d", cookie_data, page, data, error);
124 * AFS read page from file (or symlink)
126 static int afs_file_readpage(struct file *file, struct page *page)
128 struct afs_vnode *vnode;
135 inode = page->mapping->host;
137 ASSERT(file != NULL);
138 key = file->private_data;
141 _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
143 vnode = AFS_FS_I(inode);
145 BUG_ON(!PageLocked(page));
148 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
151 #ifdef AFS_CACHING_SUPPORT
153 ret = cachefs_read_or_alloc_page(vnode->cache,
155 afs_file_readpage_read_complete,
163 /* read BIO submitted and wb-journal entry found */
165 BUG(); // TODO - handle wb-journal match
167 /* read BIO submitted (page in cache) */
171 /* no page available in cache */
175 offset = page->index << PAGE_CACHE_SHIFT;
176 len = min_t(size_t, i_size_read(inode) - offset, PAGE_SIZE);
178 /* read the contents of the file from the server into the
180 ret = afs_vnode_fetch_data(vnode, key, offset, len, page);
182 if (ret == -ENOENT) {
183 _debug("got NOENT from server"
184 " - marking file deleted and stale");
185 set_bit(AFS_VNODE_DELETED, &vnode->flags);
188 #ifdef AFS_CACHING_SUPPORT
189 cachefs_uncache_page(vnode->cache, page);
194 SetPageUptodate(page);
196 #ifdef AFS_CACHING_SUPPORT
197 if (cachefs_write_page(vnode->cache,
199 afs_file_readpage_write_complete,
203 cachefs_uncache_page(vnode->cache, page);
217 _leave(" = %d", ret);
222 * get a page cookie for the specified page
224 #ifdef AFS_CACHING_SUPPORT
225 int afs_cache_get_page_cookie(struct page *page,
226 struct cachefs_page **_page_cookie)
231 ret = cachefs_page_get_private(page,_page_cookie, GFP_NOIO);
233 _leave(" = %d", ret);
239 * invalidate part or all of a page
241 static void afs_file_invalidatepage(struct page *page, unsigned long offset)
245 _enter("{%lu},%lu", page->index, offset);
247 BUG_ON(!PageLocked(page));
249 if (PagePrivate(page)) {
250 #ifdef AFS_CACHING_SUPPORT
251 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
252 cachefs_uncache_page(vnode->cache,page);
255 /* We release buffers only if the entire page is being
257 * The get_block cached value has been unconditionally
258 * invalidated, so real IO is not possible anymore.
261 BUG_ON(!PageLocked(page));
264 if (!PageWriteback(page))
265 ret = page->mapping->a_ops->releasepage(page,
267 /* possibly should BUG_ON(!ret); - neilb */
271 _leave(" = %d", ret);
275 * release a page and cleanup its private data
277 static int afs_file_releasepage(struct page *page, gfp_t gfp_flags)
279 struct cachefs_page *pageio;
281 _enter("{%lu},%x", page->index, gfp_flags);
283 if (PagePrivate(page)) {
284 #ifdef AFS_CACHING_SUPPORT
285 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
286 cachefs_uncache_page(vnode->cache, page);
289 pageio = (struct cachefs_page *) page_private(page);
290 set_page_private(page, 0);
291 ClearPagePrivate(page);